/* * V3 Creation with Binding utility * (c) Jack lange, 2010 * (c) Ferrol Aderholdt, 2012 */ #include #include #include #include #include #include #include #include #include "v3_ctrl.h" int read_file(int fd, int size, unsigned char * buf); /* loop over the .dat/.img file that is in memory and look for the xml */ static int find_starting_point(unsigned char * buf) { int i = 0; for(;i<32;i++) { if(*(buf+i) == '<') /* Looking for portion of xml file. */ if(*(buf+i+1) == 'v') return i; } return -1; } /* simple override option that just overwrites the characters in memory with a new value. Supports string lengths of equal size or lesser size, but not more than size. Kind of a rough hack to get it done, but should work for it's goal of changing mac addresses on the fly. */ int override_option(unsigned char * buf, char * option, char * value) { int starting_point = find_starting_point(buf); unsigned char * ptr; unsigned char * end; int i = 0; if(starting_point == -1) { fprintf(stderr, "Could not find a starting point to make "); fprintf(stderr, "modifications to the xml\n"); return -1; } ptr = strstr(buf+starting_point, option); /* essentially, return if the option isn't present */ if(ptr == NULL) return -1; /* find end of xml tag */ ptr = strchr(buf+(ptr-buf), '>'); end = strchr(buf+(ptr-buf), '<'); if(strlen(value) > (end-ptr-1)) { fprintf(stderr, "Override option %s length is longer than currentlyin VM image file. This is not currently supported.\n", option); return -1; } for(i=0;i [override options]\n"); printf(" override options are of the form KEY=VALUE\n"); return -1; } printf("Creating guest: %s\n", filename); guest_fd = open(filename, O_RDONLY); if (guest_fd == -1) { printf("Error Opening guest image: %s\n", filename); return -1; } if (fstat(guest_fd, &guest_stats) == -1) { printf("ERROR: Could not stat guest image file -- %s\n", filename); return -1; } guest_img.size = guest_stats.st_size; // load guest image into user memory guest_img.guest_data = malloc(guest_img.size); if (!guest_img.guest_data) { printf("ERROR: could not allocate memory for guest image\n"); return -1; } read_file(guest_fd, guest_img.size, guest_img.guest_data); close(guest_fd); printf("Loaded guest image. Binding now.\n"); if(argc > 3) { /* these are options. parse them and override the xml */ int i = 0; for(i=3;i