Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


HVM synchronous operation facility
[palacios.git] / linux_usr / v3_create.c
index 02bb873..4a0b9ff 100644 (file)
 #include "v3_ctrl.h"
 #include "ezxml.h"
 
-struct cfg_value {
-    char * tag;
-    char * value;
-};
-
-struct xml_option {
-    char * tag;
-    ezxml_t location;
-    struct xml_option * next;
-};
-
-
-struct file_info {
-    int size;
-    char filename[2048];
-    char id[256];
-};
 
 #define MAX_FILES 256
 unsigned long long num_files = 0;
 struct file_info files[MAX_FILES];
 
 
-int read_file(int fd, int size, unsigned char * buf);
-
-
-
 int create_vm(char * vm_name, void * img_data, unsigned int img_size) {
     struct v3_guest_img guest_img;
     int v3_fd = 0;
@@ -55,16 +34,7 @@ int create_vm(char * vm_name, void * img_data, unsigned int img_size) {
     guest_img.guest_data = img_data;
     strncpy(guest_img.name, vm_name, 127);
 
-
-    v3_fd = open(v3_dev, O_RDONLY);
-
-    if (v3_fd == -1) {
-       printf("Error opening V3Vee control device\n");
-       return -1;
-    }
-
-    dev_idx = ioctl(v3_fd, V3_CREATE_GUEST, &guest_img); 
-
+    dev_idx = v3_dev_ioctl(V3_CREATE_GUEST, &guest_img); 
 
     if (dev_idx < 0) {
        printf("Error (%d) creating VM\n", dev_idx);
@@ -72,10 +42,6 @@ int create_vm(char * vm_name, void * img_data, unsigned int img_size) {
     }
 
     printf("VM (%s) created at /dev/v3-vm%d\n", vm_name, dev_idx);
-
-    /* Close the file descriptor.  */ 
-    close(v3_fd); 
-
     return 0;
 }
 
@@ -103,7 +69,7 @@ int load_image(char * vm_name, char * filename) {
     // load guest image into user memory
     img_data = malloc(img_size);
 
-    read_file(guest_fd, img_size, img_data);
+    v3_read_file(guest_fd, img_size, img_data);
     
     close(guest_fd);
 
@@ -281,6 +247,7 @@ int build_image(char * vm_name, char * filename, struct cfg_value * cfg_vals, in
        int i = 0;
        int offset = 0;
        unsigned long long file_offset = 0;
+        struct mem_file_hdr * hdrs = NULL;
 
        /* Image size is: 
           8 byte header + 
@@ -288,7 +255,7 @@ int build_image(char * vm_name, char * filename, struct cfg_value * cfg_vals, in
           xml strlen + 
           8 bytes of zeros + 
           8 bytes (number of files) + 
-          num_files * 16 byte file header + 
+          num_files * (16+sizeof(unsigned long)) byte file header + 
           8 bytes of zeroes + 
           file data
        */
@@ -297,13 +264,16 @@ int build_image(char * vm_name, char * filename, struct cfg_value * cfg_vals, in
        }
 
        guest_img_size = 8 + 4 + strlen(new_xml_str) + 8 + 8 + 
-           (num_files * 16) + 8 + file_data_size;
+           (num_files * (16*sizeof(unsigned long))) + 8 + file_data_size;
            
 
        guest_img_data = malloc(guest_img_size);
        memset(guest_img_data, 0, guest_img_size);
 
-       memcpy(guest_img_data, "v3vee\0\0\0", 8);
+       //
+       // Dynamically built guests are version 1 by default now
+       //
+       memcpy(guest_img_data, "v3vee\0\0\1", 8);
        offset += 8;
 
        *(unsigned int *)(guest_img_data + offset) = strlen(new_xml_str);
@@ -318,20 +288,29 @@ int build_image(char * vm_name, char * filename, struct cfg_value * cfg_vals, in
        *(unsigned long long *)(guest_img_data + offset) = num_files;
        offset += 8;
 
+        hdrs = guest_img_data + offset;
        
        // The file offset starts at the end of the file list
-       file_offset = offset + (16 * num_files) + 8;
+       file_offset = offset + ((sizeof(unsigned long) + 16) * num_files) + 8;
 
        for (i = 0; i < num_files; i++) {
-           *(unsigned int *)(guest_img_data + offset) = i;
+            /*
+           unsigned int *)(guest_img_data + offset) = i;
            offset += 4;
            *(unsigned int *)(guest_img_data + offset) = files[i].size;
            offset += 4;
            *(unsigned long long *)(guest_img_data + offset) = file_offset;
            offset += 8;
-
+            *(unsigned long *)(guest_img_data + offset) = 0;
+            offset += sizeof(unsigned long);
+            */
+            hdrs[i].file_idx     = i;
+            hdrs[i].file_size    = files[i].size;
+            hdrs[i].file_offset  = file_offset;
+            hdrs[i].file_hash    = 0;
+
+            offset +=  16 + sizeof(unsigned long);
            file_offset += files[i].size;
-
        }
 
        memset(guest_img_data + offset, 0, 8);
@@ -340,13 +319,18 @@ int build_image(char * vm_name, char * filename, struct cfg_value * cfg_vals, in
 
        for (i = 0; i < num_files; i++) {
            int fd = open(files[i].filename, O_RDONLY);
+            unsigned char * faddr = (unsigned char *)(guest_img_data + offset);
 
            if (fd == -1) {
                printf("Error: Could not open aux file (%s)\n", files[i].filename);
                return -1;
            }
 
-           read_file(fd, files[i].size, (unsigned char *)(guest_img_data + offset));
+           v3_read_file(fd, files[i].size, faddr);
+
+            /* store a hash of the file blob for integrity checking later */
+            hdrs[i].file_hash = v3_hash_buffer(faddr, files[i].size);
+            printf("File Hash: %llx\n", hdrs[i].file_hash);
 
            close(fd);
 
@@ -365,8 +349,6 @@ int build_image(char * vm_name, char * filename, struct cfg_value * cfg_vals, in
 
 
 
-
-
 int main(int argc, char** argv) {
     char * filename = NULL;
     char * name = NULL;
@@ -383,10 +365,8 @@ int main(int argc, char** argv) {
        }
     }
 
-    if (argc - optind + 1 < 3) {
-       printf("usage: v3_create [-b] <guest_img> <vm name> [cfg options]\n");
-       return -1;
-    }
+    if (argc - optind + 1 < 3) 
+        v3_usage("[-b] <guest_img> <vm_name> [cfg options]\n");
 
     filename = argv[optind];
     name = argv[optind + 1];
@@ -436,28 +416,3 @@ int main(int argc, char** argv) {
 
     return 0; 
 } 
-
-
-
-int read_file(int fd, int size, unsigned char * buf) {
-    int left_to_read = size;
-    int have_read = 0;
-
-    while (left_to_read != 0) {
-       int bytes_read = read(fd, buf + have_read, left_to_read);
-
-       if (bytes_read <= 0) {
-           break;
-       }
-
-       have_read += bytes_read;
-       left_to_read -= bytes_read;
-    }
-
-    if (left_to_read != 0) {
-       printf("Error could not finish reading file\n");
-       return -1;
-    }
-    
-    return 0;
-}