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.


36057022bee8e93d0a17e8c03c993cac3dfa8229
[palacios.git] / linux_usr / v3_create.c
1 /* 
2  * V3 Control utility
3  * (c) Jack lange, 2010
4  */
5
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <fcntl.h> 
10 #include <sys/ioctl.h> 
11 #include <sys/stat.h> 
12 #include <sys/types.h> 
13 #include <unistd.h> 
14 #include <string.h>
15  
16 #include "v3_ctrl.h"
17
18 int read_file(int fd, int size, unsigned char * buf);
19
20 int main(int argc, char* argv[]) {
21     char * filename = argv[1];
22     char * name = argv[2];
23     int guest_fd = 0;
24     int v3_fd = 0;
25     struct v3_guest_img guest_img;
26     struct stat guest_stats;
27     int dev_idx = 0;
28
29     memset(&guest_img, 0, sizeof(struct v3_guest_img));
30
31     if (argc <= 2) {
32         printf("usage: v3_create <guest_img> <vm name>\n");
33         return -1;
34     }
35
36     printf("Creating guest: %s\n", filename);
37
38     guest_fd = open(filename, O_RDONLY); 
39
40     if (guest_fd == -1) {
41         printf("Error Opening guest image: %s\n", filename);
42         return -1;
43     }
44
45     if (fstat(guest_fd, &guest_stats) == -1) {
46         printf("ERROR: Could not stat guest image file -- %s\n", filename);
47         return -1;
48     }
49
50     
51     guest_img.size = guest_stats.st_size;
52     
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");
57             return -1;
58     }
59
60     read_file(guest_fd, guest_img.size, guest_img.guest_data);
61     
62     close(guest_fd);
63
64     printf("Loaded guest image. Creation begins.\n");
65     
66     strncpy(guest_img.name, name, 127);
67
68
69     v3_fd = open(v3_dev, O_RDONLY);
70
71     if (v3_fd == -1) {
72         printf("Error opening V3Vee control device\n");
73         return -1;
74     }
75
76     dev_idx = ioctl(v3_fd, V3_CREATE_GUEST, &guest_img); 
77
78
79     if (dev_idx < 0) {
80         printf("Error (%d) creating VM\n", dev_idx);
81         return -1;
82     }
83
84     printf("VM (%s) created at /dev/v3-vm%d\n", name, dev_idx);
85
86     /* Close the file descriptor.  */ 
87     close(v3_fd); 
88  
89
90
91     return 0; 
92
93
94
95
96 int read_file(int fd, int size, unsigned char * buf) {
97     int left_to_read = size;
98     int have_read = 0;
99
100     while (left_to_read != 0) {
101         int bytes_read = read(fd, buf + have_read, left_to_read);
102
103         if (bytes_read <= 0) {
104             break;
105         }
106
107         have_read += bytes_read;
108         left_to_read -= bytes_read;
109     }
110
111     if (left_to_read != 0) {
112         printf("Error could not finish reading file\n");
113         return -1;
114     }
115     
116     return 0;
117 }