2 * V3 Creation with Binding utility
4 * (c) Ferrol Aderholdt, 2012
11 #include <sys/ioctl.h>
13 #include <sys/types.h>
19 int read_file(int fd, int size, unsigned char * buf);
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) {
26 /* Looking for <vm class="PC"> portion of xml file. */
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);
44 if(starting_point == -1) {
45 fprintf(stderr, "Could not find a starting point to make ");
46 fprintf(stderr, "modifications to the xml\n");
50 ptr = strstr(buf+starting_point, option);
51 /* essentially, return if the option isn't present */
55 /* find end of xml tag */
56 ptr = strchr(buf+(ptr-buf), '>');
57 end = strchr(buf+(ptr-buf), '<');
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);
64 for(i=0;i<strlen(value);i++) {
68 /* the new option is smaller than the old one */
69 if(strlen(value) < (end-ptr-1)) {
75 for(j;j<strlen(option);j++,i++) {
80 for(j=0;j< (end-ptr-1)-strlen(value);j++,i++) {
91 int main(int argc, char* argv[]) {
92 char * filename = argv[1];
93 char * name = argv[2];
96 struct v3_guest_img guest_img;
97 struct stat guest_stats;
100 memset(&guest_img, 0, sizeof(struct v3_guest_img));
103 printf("usage: v3_create_bind <guest_img> <vm name> [override options]\n");
104 printf(" override options are of the form KEY=VALUE\n");
108 printf("Creating guest: %s\n", filename);
110 guest_fd = open(filename, O_RDONLY);
112 if (guest_fd == -1) {
113 printf("Error Opening guest image: %s\n", filename);
117 if (fstat(guest_fd, &guest_stats) == -1) {
118 printf("ERROR: Could not stat guest image file -- %s\n", filename);
123 guest_img.size = guest_stats.st_size;
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");
133 read_file(guest_fd, guest_img.size, guest_img.guest_data);
137 printf("Loaded guest image. Binding now.\n");
140 /* these are options. parse them and override the xml */
142 for(i=3;i<argc;i++) {
143 char * argv_copy = malloc(strlen(argv[i]));
147 strcpy(argv_copy, argv[i]);
148 option = strtok(argv_copy, "=");
149 value = strtok(NULL, "=");
151 printf("Binding \"%s\" to \"%s\"\n",
153 override_option(guest_img.guest_data, option, value);
158 printf("Bound guest image. Launching to V3Vee\n");
160 strncpy(guest_img.name, name, 127);
163 v3_fd = open(v3_dev, O_RDONLY);
166 printf("Error opening V3Vee control device\n");
170 dev_idx = ioctl(v3_fd, V3_CREATE_GUEST, &guest_img);
174 printf("Error (%d) creating VM\n", dev_idx);
178 printf("VM (%s) created at /dev/v3-vm%d\n", name, dev_idx);
180 /* Close the file descriptor. */
190 int read_file(int fd, int size, unsigned char * buf) {
191 int left_to_read = size;
194 while (left_to_read != 0) {
195 int bytes_read = read(fd, buf + have_read, left_to_read);
197 if (bytes_read <= 0) {
201 have_read += bytes_read;
202 left_to_read -= bytes_read;
205 if (left_to_read != 0) {
206 printf("Error could not finish reading file\n");