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.


Avoid strict-aliasing related issues when compiling with optimization
[palacios.git] / linux_usr / v3_create_bind.c
1 /* 
2  * V3 Creation with Binding utility
3  * (c) Jack lange, 2010
4  * (c) Ferrol Aderholdt, 2012 
5  */
6
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <fcntl.h> 
11 #include <sys/ioctl.h> 
12 #include <sys/stat.h> 
13 #include <sys/types.h> 
14 #include <unistd.h> 
15 #include <string.h>
16  
17 #include "v3_ctrl.h"
18
19 int read_file(int fd, int size, unsigned char * buf);
20
21 /* loop over the .dat/.img file that is in memory and look for the xml */
22 static int find_starting_point(unsigned char * buf) {
23     int i = 0;
24     for(;i<32;i++) {
25         if(*(buf+i) == '<') 
26             /* Looking for <vm class="PC"> portion of xml file. */
27             if(*(buf+i+1) == 'v')
28                 return i;
29     }
30     
31     return -1;
32 }
33
34 /* simple override option that just overwrites the characters in memory with
35    a new value. Supports string lengths of equal size or lesser size, but not
36    more than size. Kind of a rough hack to get it done, but should work for 
37    it's goal of changing mac addresses on the fly. */
38 int override_option(unsigned char * buf, char * option, char * value) {
39     int starting_point = find_starting_point(buf);
40     unsigned char * ptr;
41     unsigned char * end;
42     int i = 0;
43     
44     if(starting_point == -1) {
45         fprintf(stderr, "Could not find a starting point to make ");
46         fprintf(stderr, "modifications to the xml\n");
47         return -1;
48     }
49     
50     ptr = strstr(buf+starting_point, option);
51     /* essentially, return if the option isn't present */
52     if(ptr == NULL)
53         return -1;
54     
55     /* find end of xml tag */
56     ptr = strchr(buf+(ptr-buf), '>');
57     end = strchr(buf+(ptr-buf), '<');
58     
59     if(strlen(value) > (end-ptr-1)) {
60         fprintf(stderr, "Override option %s length is longer than currentlyin VM image file. This is not currently supported.\n", option);
61         return -1;
62     }
63     
64     for(i=0;i<strlen(value);i++) {
65         ptr[i+1] = value[i];
66     }
67     
68     /* the new option is smaller than the old one */
69     if(strlen(value) < (end-ptr-1)) {
70         int j = 0;
71         ptr[i+1] = '<';
72         i++;
73         ptr[i+1] = '/';
74         i++;
75         for(j;j<strlen(option);j++,i++) {
76             ptr[i+1] = option[j];
77         }
78         ptr[i+1] = '>';
79         i++;
80         for(j=0;j< (end-ptr-1)-strlen(value);j++,i++) {
81             ptr[i+1] = ' ';
82         }
83     }
84     
85     return 0;
86 }
87
88
89
90
91 int main(int argc, char* argv[]) {
92     char * filename = argv[1];
93     char * name = argv[2];
94     int guest_fd = 0;
95     int v3_fd = 0;
96     struct v3_guest_img guest_img;
97     struct stat guest_stats;
98     int dev_idx = 0;
99
100     memset(&guest_img, 0, sizeof(struct v3_guest_img));
101
102     if (argc <= 2) {
103         printf("usage: v3_create_bind <guest_img> <vm name> [override options]\n");
104         printf(" override options are of the form  KEY=VALUE\n");
105         return -1;
106     }
107
108     printf("Creating guest: %s\n", filename);
109
110     guest_fd = open(filename, O_RDONLY); 
111
112     if (guest_fd == -1) {
113         printf("Error Opening guest image: %s\n", filename);
114         return -1;
115     }
116
117     if (fstat(guest_fd, &guest_stats) == -1) {
118         printf("ERROR: Could not stat guest image file -- %s\n", filename);
119         return -1;
120     }
121
122     
123     guest_img.size = guest_stats.st_size;
124     
125     // load guest image into user memory
126     guest_img.guest_data = malloc(guest_img.size);
127     if (!guest_img.guest_data) {
128             printf("ERROR: could not allocate memory for guest image\n");
129             return -1;
130     }
131
132
133     read_file(guest_fd, guest_img.size, guest_img.guest_data);
134     
135     close(guest_fd);
136
137     printf("Loaded guest image. Binding now.\n");
138
139     if(argc > 3) {
140         /* these are options. parse them and override the xml */
141         int i = 0;
142         for(i=3;i<argc;i++) {
143             char * argv_copy = malloc(strlen(argv[i]));
144             char * option;
145             char * value;
146             
147             strcpy(argv_copy, argv[i]);
148             option = strtok(argv_copy, "=");
149             value = strtok(NULL, "=");
150             
151             printf("Binding \"%s\" to \"%s\"\n",
152                    option, value);
153             override_option(guest_img.guest_data, option, value);
154             free(argv_copy);
155         }
156     }
157     
158     printf("Bound guest image.  Launching to V3Vee\n");
159     
160     strncpy(guest_img.name, name, 127);
161
162
163     v3_fd = open(v3_dev, O_RDONLY);
164
165     if (v3_fd == -1) {
166         printf("Error opening V3Vee control device\n");
167         return -1;
168     }
169
170     dev_idx = ioctl(v3_fd, V3_CREATE_GUEST, &guest_img); 
171
172
173     if (dev_idx < 0) {
174         printf("Error (%d) creating VM\n", dev_idx);
175         return -1;
176     }
177
178     printf("VM (%s) created at /dev/v3-vm%d\n", name, dev_idx);
179
180     /* Close the file descriptor.  */ 
181     close(v3_fd); 
182  
183
184
185     return 0; 
186
187
188
189
190 int read_file(int fd, int size, unsigned char * buf) {
191     int left_to_read = size;
192     int have_read = 0;
193
194     while (left_to_read != 0) {
195         int bytes_read = read(fd, buf + have_read, left_to_read);
196
197         if (bytes_read <= 0) {
198             break;
199         }
200
201         have_read += bytes_read;
202         left_to_read -= bytes_read;
203     }
204
205     if (left_to_read != 0) {
206         printf("Error could not finish reading file\n");
207         return -1;
208     }
209     
210     return 0;
211 }