ia64/xen-unstable

changeset 1062:ff3df62df71e

bitkeeper revision 1.701 (401d8ce3FywXm-ccGL70gVcXWGsk7g)

Add support for compressed initrd ramdisks in xc_linux_build
author iap10@labyrinth.cl.cam.ac.uk[iap10]
date Sun Feb 01 23:33:55 2004 +0000 (2004-02-01)
parents 9ccd60698e09
children 2dcb5a482b05
files tools/xc/lib/xc_linux_build.c
line diff
     1.1 --- a/tools/xc/lib/xc_linux_build.c	Sun Feb 01 22:30:47 2004 +0000
     1.2 +++ b/tools/xc/lib/xc_linux_build.c	Sun Feb 01 23:33:55 2004 +0000
     1.3 @@ -98,7 +98,7 @@ static int copy_to_domain_page(int pm_ha
     1.4  static int setup_guestos(int xc_handle,
     1.5                           int dom, 
     1.6                           gzFile kernel_gfd, 
     1.7 -                         int initrd_fd, 
     1.8 +                         gzFile initrd_gfd, 
     1.9                           unsigned long tot_pages,
    1.10                           unsigned long *virt_startinfo_addr, 
    1.11                           unsigned long virt_load_addr, 
    1.12 @@ -164,38 +164,30 @@ static int setup_guestos(int xc_handle,
    1.13      ksize = i;
    1.14  
    1.15      /* Load the initial ramdisk image. */
    1.16 -    if ( initrd_fd >= 0 )
    1.17 +    if ( initrd_gfd )
    1.18      {
    1.19 -        struct stat stat;
    1.20 -        unsigned long isize;
    1.21 +	int size;
    1.22  
    1.23 -        if ( fstat(initrd_fd, &stat) < 0 )
    1.24 -        {
    1.25 -            PERROR("Could not stat the initrd image");
    1.26 -            goto error_out;
    1.27 -        }
    1.28 -        isize = stat.st_size;
    1.29 -        if ( (isize + ksize) > ((tot_pages/2) * PAGE_SIZE) )
    1.30 -        {
    1.31 -            ERROR("Kernel/initrd too big to safely fit in domain memory");
    1.32 +	for ( j=0, i=ksize; i < ((tot_pages/2) * PAGE_SIZE); i += PAGE_SIZE )
    1.33 +	{
    1.34 +	    char page[PAGE_SIZE];
    1.35 +            if ( (size = gzread(initrd_gfd, page, PAGE_SIZE)) == -1 )
    1.36 +            {
    1.37 +		PERROR("Error reading initrd image, could not");
    1.38 +		goto error_out;
    1.39 +	    }
    1.40 +	    j += size;
    1.41 +	    if ( size > 0 )
    1.42 +		copy_to_domain_page(pm_handle, page_array[i>>PAGE_SHIFT], page);
    1.43 +	    if ( size < PAGE_SIZE )
    1.44 +		goto initrd_copied;
    1.45 +	}
    1.46 +	ERROR("Kernel/initrd too big to safely fit in domain memory");
    1.47              goto error_out;
    1.48 -        }
    1.49 -
    1.50 +        
    1.51 + initrd_copied:	
    1.52          initrd_addr = virt_load_addr + ksize;
    1.53 -        initrd_len  = isize;
    1.54 -
    1.55 -        for ( j = 0, i = ksize; j < isize; j += PAGE_SIZE, i += PAGE_SIZE )
    1.56 -        {
    1.57 -            char page[PAGE_SIZE];
    1.58 -            int size = ((isize-j) < PAGE_SIZE) ? (isize-j) : PAGE_SIZE;
    1.59 -            if ( read(initrd_fd, page, size) != size )
    1.60 -            {
    1.61 -                PERROR("Error reading initrd image, could not"
    1.62 -                       " read the whole image.");
    1.63 -                goto error_out;
    1.64 -            } 
    1.65 -            copy_to_domain_page(pm_handle, page_array[i>>PAGE_SHIFT], page);
    1.66 -        }
    1.67 +        initrd_len  = j;
    1.68      }
    1.69  
    1.70      alloc_index = tot_pages - 1;
    1.71 @@ -328,8 +320,8 @@ int xc_linux_build(int xc_handle,
    1.72      dom0_op_t launch_op, op;
    1.73      unsigned long load_addr;
    1.74      long tot_pages;
    1.75 -    int kernel_fd, initrd_fd = -1;
    1.76 -    gzFile kernel_gfd;
    1.77 +    int kernel_fd = -1, initrd_fd = -1;
    1.78 +    gzFile kernel_gfd = NULL, initrd_gfd = NULL;
    1.79      int rc, i;
    1.80      full_execution_context_t *ctxt;
    1.81      unsigned long virt_startinfo_addr;
    1.82 @@ -380,6 +372,13 @@ int xc_linux_build(int xc_handle,
    1.83              PERROR("Could not open the initial ramdisk image");
    1.84              goto error_out;
    1.85          }
    1.86 +
    1.87 +	if ( (initrd_gfd = gzdopen(initrd_fd, "rb")) == NULL )
    1.88 +	{
    1.89 +	    PERROR("Could not allocate decompression state for initrd");
    1.90 +	    goto error_out;
    1.91 +	}
    1.92 +
    1.93      }
    1.94  
    1.95      op.cmd = DOM0_GETDOMAININFO;
    1.96 @@ -397,7 +396,7 @@ int xc_linux_build(int xc_handle,
    1.97          goto error_out;
    1.98      }
    1.99  
   1.100 -    if ( setup_guestos(xc_handle, domid, kernel_gfd, initrd_fd, tot_pages,
   1.101 +    if ( setup_guestos(xc_handle, domid, kernel_gfd, initrd_gfd, tot_pages,
   1.102                         &virt_startinfo_addr,
   1.103                         load_addr, &launch_op.u.builddomain, cmdline,
   1.104                         op.u.getdomaininfo.shared_info_frame) < 0 )
   1.105 @@ -406,9 +405,14 @@ int xc_linux_build(int xc_handle,
   1.106          goto error_out;
   1.107      }
   1.108  
   1.109 +    if ( kernel_fd >= 0 )
   1.110 +        close(kernel_fd);
   1.111 +    if( kernel_gfd )
   1.112 +	gzclose(kernel_gfd);
   1.113      if ( initrd_fd >= 0 )
   1.114          close(initrd_fd);
   1.115 -    gzclose(kernel_gfd);
   1.116 +    if( initrd_gfd )
   1.117 +	gzclose(initrd_gfd);
   1.118  
   1.119      ctxt = &launch_op.u.builddomain.ctxt;
   1.120  
   1.121 @@ -473,8 +477,14 @@ int xc_linux_build(int xc_handle,
   1.122      return rc;
   1.123  
   1.124   error_out:
   1.125 +    if ( kernel_fd >= 0 )
   1.126 +        close(kernel_fd);
   1.127 +    if( kernel_gfd )
   1.128 +	gzclose(kernel_gfd);
   1.129      if ( initrd_fd >= 0 )
   1.130          close(initrd_fd);
   1.131 -    gzclose(kernel_gfd);
   1.132 +    if( initrd_gfd )
   1.133 +	gzclose(initrd_gfd);
   1.134 +
   1.135      return -1;
   1.136  }