#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;
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);
}
printf("VM (%s) created at /dev/v3-vm%d\n", vm_name, dev_idx);
-
- /* Close the file descriptor. */
- close(v3_fd);
-
return 0;
}
// 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);
int i = 0;
int offset = 0;
unsigned long long file_offset = 0;
+ struct mem_file_hdr * hdrs = NULL;
/* Image size is:
8 byte header +
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
*/
}
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);
*(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);
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);
-
-
int main(int argc, char** argv) {
char * filename = NULL;
char * name = NULL;
}
}
- 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];
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;
-}