10 #include <sys/ioctl.h>
12 #include <sys/types.h>
18 int read_file(int fd, int size, unsigned char * buf);
20 int main(int argc, char* argv[]) {
21 char * filename = argv[1];
22 char * name = argv[2];
25 struct v3_guest_img guest_img;
26 struct stat guest_stats;
29 memset(&guest_img, 0, sizeof(struct v3_guest_img));
32 printf("usage: v3_create <guest_img> <vm name>\n");
36 printf("Creating guest: %s\n", filename);
38 guest_fd = open(filename, O_RDONLY);
41 printf("Error Opening guest image: %s\n", filename);
45 if (fstat(guest_fd, &guest_stats) == -1) {
46 printf("ERROR: Could not stat guest image file -- %s\n", filename);
51 guest_img.size = guest_stats.st_size;
53 // load guest image into user memory
54 guest_img.guest_data = malloc(guest_img.size);
55 if (!guest_img.guest_data) {
56 printf("ERROR: could not allocate memory for guest image\n");
60 read_file(guest_fd, guest_img.size, guest_img.guest_data);
64 printf("Loaded guest image. Creation begins.\n");
66 strncpy(guest_img.name, name, 127);
69 v3_fd = open(v3_dev, O_RDONLY);
72 printf("Error opening V3Vee control device\n");
76 dev_idx = ioctl(v3_fd, V3_CREATE_GUEST, &guest_img);
80 printf("Error (%d) creating VM\n", dev_idx);
84 printf("VM (%s) created at /dev/v3-vm%d\n", name, dev_idx);
86 /* Close the file descriptor. */
96 int read_file(int fd, int size, unsigned char * buf) {
97 int left_to_read = size;
100 while (left_to_read != 0) {
101 int bytes_read = read(fd, buf + have_read, left_to_read);
103 if (bytes_read <= 0) {
107 have_read += bytes_read;
108 left_to_read -= bytes_read;
111 if (left_to_read != 0) {
112 printf("Error could not finish reading file\n");