From 36d597380e357d165b5ea42e5e9f78a72709cbaf Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Wed, 14 Feb 2007 15:58:21 +0000 Subject: [PATCH] Tue Feb 14 14:57:52 EST 2007 Mark McLoughlin +Tue Feb 14 14:57:52 EST 2007 Mark McLoughlin - - * qemud/protocol.h: add the protocol for virtual networks - - * qemud/dispatch.c: implement the protocol - - * qemud/driver.[ch]: add stubs for the driver - - * qemud/internal.h: add struct qemud_network - - * src/qemu_internal.c: add a virtual networks driver - -Tue Feb 14 15:43:28 IST 2007 Mark McLoughlin - - * src/virsh.c: add the net-* commands. - -Tue Feb 14 15:37:17 IST 2007 Mark McLoughlin - - Note: potential ABI break here, but people should - only really be using virError structs returned from - libvirt itself. - - * include/libvirt/virterror.h: add virNetwork - to virError - - * src/internal.h, src/virterror.c: add network param - to __virRaiseError() - - * src/conf.c, src/hash.c, src/libvirt.c, src/proxy_internal.c, - src/qemu_internal.c, src/sexpr.c, src/test.c, src/xen_internal.c, - src/xend_internal.c, src/xm_internal.c, src/xml.c, src/xmlrpc.c, - src/xs_internal.c: update. - -Tue Feb 14 15:33:05 IST 2007 Mark McLoughlin - - * include/libvirt/libvirt.h.in: add the networks APIs - - * include/libvirt/virterror.h: add some error codes - - * src/driver.h: add network driver vtable - - * src/hash.c: add networks hash - - * src/internal.h: add virNetwork - - * src/libvirt.c: hook up the APIs to the network - driver - - * src/libvirt_sym.version: add the new APIs - - * src/virterror.c: handle the new error codes - -Tue Feb 14 15:07:26 IST 2007 Mark McLoughlin - - * src/conf.h: fix merge error - remove the argc argument - from qemudBuildCommandLine() - -Tue Feb 14 15:03:22 IST 2007 Mark McLoughlin - - * src/virsh.c: Re-name some of the VSH_DOMBYFOO stuff - to VSH_BYFOO in order to re-use it for the network stuff. - -Tue Feb 14 14:58:35 IST 2007 Mark McLoughlin - - * src/hash.c, src/internal.h: Re-name virConnect->domains_mux - to virConnect->hashes_mux since it will also be used to - protect the networks hash. - -Tue Feb 14 14:57:52 IST 2007 Mark McLoughlin - - * qemud/conf.c: qemudSaveConfig() will always report a + * src/conf.c: qemudSaveConfig() will always report a more specific error, so we should avoid overwriting this error. -Tue Feb 14 14:54:25 IST 2007 Mark McLoughlin +Tue Feb 14 14:54:25 EST 2007 Mark McLoughlin * qemud/conf.c: don't return argc from qemudBuildCommandLine() as exec() doesn't need it. -Tue Feb 14 14:52:12 IST 2007 Mark McLoughlin +Tue Feb 14 14:52:12 EST 2007 Mark McLoughlin - split qemudScanConfigDir() out so that qemudScanConfigs() can scan multiple configDirs -Tue Feb 14 14:50:22 IST 2007 Mark McLoughlin +Tue Feb 14 14:50:22 EST 2007 Mark McLoughlin +Tue Feb 14 14:42:38 EST 2007 Mark McLoughlin qemud_fd so that xen and qemu don't share the handle member. * src/hash.c, src/qemu_internal.c: update -Tue Feb 14 14:40:52 IST 2007 Mark McLoughlin +Tue Feb 14 14:40:52 EST 2007 Mark McLoughlin +Tue Feb 14 14:39:18 EST 2007 Mark McLoughlin * driver.c, qemud.c: upd. -Tue Feb 14 14:33:22 IST 2007 Mark McLoughlin +Tue Feb 14 14:33:22 EST 2007 Mark McLoughlin networkConfigDir) < 0) { - goto cleanup; - } - - if ((fd = open(network->configFile, - O_WRONLY | O_CREAT | O_TRUNC, - S_IRUSR | S_IWUSR )) < 0) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot create config file %s", network->configFile); - goto cleanup; - } - - towrite = strlen(xml); - if (write(fd, xml, towrite) != towrite) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot write config file %s", network->configFile); - goto cleanup; - } - - if (close(fd) < 0) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot save config file %s", network->configFile); - goto cleanup; - } - - ret = 0; - - cleanup: - - free(xml); - - return ret; -} - - -static int qemudParseNetworkXML(struct qemud_server *server, - xmlDocPtr xml, - struct qemud_network *network) { - xmlNodePtr root = NULL; - xmlXPathContextPtr ctxt = NULL; - xmlXPathObjectPtr obj = NULL; - - /* Prepare parser / xpath context */ - root = xmlDocGetRootElement(xml); - if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "network"))) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "%s", "incorrect root element"); - goto error; - } - - ctxt = xmlXPathNewContext(xml); - if (ctxt == NULL) { - qemudReportError(server, VIR_ERR_NO_MEMORY, "xmlXPathContext"); - goto error; - } - - - /* Extract network name */ - obj = xmlXPathEval(BAD_CAST "string(/network/name[1])", ctxt); - if ((obj == NULL) || (obj->type != XPATH_STRING) || - (obj->stringval == NULL) || (obj->stringval[0] == 0)) { - qemudReportError(server, VIR_ERR_NO_NAME, NULL); - goto error; - } - if (strlen((const char *)obj->stringval) >= (QEMUD_MAX_NAME_LEN-1)) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "%s", "network name length too long"); - goto error; - } - strcpy(network->def.name, (const char *)obj->stringval); - xmlXPathFreeObject(obj); - - - /* Extract network uuid */ - obj = xmlXPathEval(BAD_CAST "string(/network/uuid[1])", ctxt); - if ((obj == NULL) || (obj->type != XPATH_STRING) || - (obj->stringval == NULL) || (obj->stringval[0] == 0)) { - /* XXX auto-generate a UUID */ - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "%s", "missing uuid element"); - goto error; - } - if (qemudParseUUID((const char *)obj->stringval, network->def.uuid) < 0) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "%s", "malformed uuid element"); - goto error; - } - xmlXPathFreeObject(obj); - - xmlXPathFreeContext(ctxt); - - return 0; - - error: - /* XXX free all the stuff in the qemud_network struct, or leave it upto - the caller ? */ - if (obj) - xmlXPathFreeObject(obj); - if (ctxt) - xmlXPathFreeContext(ctxt); - return -1; -} - - -struct qemud_network *qemudLoadNetworkConfigXML(struct qemud_server *server, - const char *file, - const char *doc, - int save) { - struct qemud_network *network = NULL; - xmlDocPtr xml; - - if (!(xml = xmlReadDoc(BAD_CAST doc, file ? file : "network.xml", NULL, - XML_PARSE_NOENT | XML_PARSE_NONET | - XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) { - qemudReportError(server, VIR_ERR_XML_ERROR, NULL); - return NULL; - } - - if (!(network = calloc(1, sizeof(struct qemud_network)))) { - qemudReportError(server, VIR_ERR_NO_MEMORY, "network"); - return NULL; - } - - if (qemudParseNetworkXML(server, xml, network) < 0) { - xmlFreeDoc(xml); - qemudFreeNetwork(network); - return NULL; - } - xmlFreeDoc(xml); - - if (qemudFindNetworkByUUID(server, network->def.uuid) || - qemudFindNetworkByName(server, network->def.name)) { - qemudReportError(server, VIR_ERR_NETWORK_EXIST, network->def.name); - qemudFreeNetwork(network); - return NULL; - } - - if (file) { - strncpy(network->configFile, file, PATH_MAX); - network->configFile[PATH_MAX-1] = '\0'; - } else { - if (save) { - if (qemudMakeConfigPath(server->networkConfigDir, network->def.name, ".xml", network->configFile, PATH_MAX) < 0) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot construct config file path"); - qemudFreeNetwork(network); - return NULL; - } - - if (qemudSaveNetworkConfig(server, network) < 0) { - qemudFreeNetwork(network); - return NULL; - } - } else { - network->configFile[0] = '\0'; - } - } - - return network; -} - - /* Load a guest from its persistent config file */ static void qemudLoadConfig(struct qemud_server *server, - const char *file, - int isGuest) { + const char *file) { FILE *fh; struct stat st; + struct qemud_vm *vm; char xml[QEMUD_MAX_XML_LEN]; int ret; if (!(fh = fopen(file, "r"))) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot open config file %s", file); + qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot open guest config file %s", file); return; } @@ -1287,7 +1118,7 @@ static void qemudLoadConfig(struct qemud_server *server, } if (st.st_size >= QEMUD_MAX_XML_LEN) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "config too large in file %s", file); + qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "guest config too large in file %s", file); goto cleanup; } @@ -1297,20 +1128,10 @@ static void qemudLoadConfig(struct qemud_server *server, } xml[st.st_size] = '\0'; - if (isGuest) { - struct qemud_vm *vm; - if ((vm = qemudLoadConfigXML(server, file, xml, 1))) { - vm->next = server->inactivevms; - server->inactivevms = vm; - server->ninactivevms++; - } - } else { - struct qemud_network *network; - if ((network = qemudLoadNetworkConfigXML(server, file, xml, 1))) { - network->next = server->inactivenetworks; - server->inactivenetworks = network; - server->ninactivenetworks++; - } + if ((vm = qemudLoadConfigXML(server, file, xml, 1))) { + vm->next = server->inactivevms; + server->inactivevms = vm; + server->ninactivevms++; } cleanup: @@ -1320,8 +1141,7 @@ static void qemudLoadConfig(struct qemud_server *server, static int qemudScanConfigDir(struct qemud_server *server, - const char *configDir, - int isGuest) { + const char *configDir) { DIR *dir; struct dirent *entry; @@ -1339,7 +1159,7 @@ int qemudScanConfigDir(struct qemud_server *server, if (qemudMakeConfigPath(configDir, entry->d_name, NULL, file, PATH_MAX) < 0) continue; - qemudLoadConfig(server, file, isGuest); + qemudLoadConfig(server, file); } closedir(dir); @@ -1349,9 +1169,7 @@ int qemudScanConfigDir(struct qemud_server *server, /* Scan for all guest and network config files */ int qemudScanConfigs(struct qemud_server *server) { - if (qemudScanConfigDir(server, server->configDir, 0) < 0) - return -1; - return qemudScanConfigDir(server, server->networkConfigDir, 1); + return qemudScanConfigDir(server, server->configDir); } /* Simple grow-on-demand string buffer */ @@ -1606,54 +1424,19 @@ char *qemudGenerateXML(struct qemud_server *server, struct qemud_vm *vm) { } -char *qemudGenerateNetworkXML(struct qemud_server *server, - struct qemud_network *network) { - struct qemudBuffer buf; - unsigned char *uuid; - - buf.len = QEMUD_MAX_XML_LEN; - buf.used = 0; - buf.data = malloc(buf.len); - - if (qemudBufferPrintf(&buf, "\n") < 0) - goto no_memory; - - if (qemudBufferPrintf(&buf, " %s\n", network->def.name) < 0) - goto no_memory; - - uuid = network->def.uuid; - if (qemudBufferPrintf(&buf, " %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", - uuid[0], uuid[1], uuid[2], uuid[3], - uuid[4], uuid[5], uuid[6], uuid[7], - uuid[8], uuid[9], uuid[10], uuid[11], - uuid[12], uuid[13], uuid[14], uuid[15]) < 0) - goto no_memory; - - if (qemudBufferAdd(&buf, "\n") < 0) - goto no_memory; - - return buf.data; - - no_memory: - qemudReportError(server, VIR_ERR_NO_MEMORY, "xml"); - free(buf.data); - return NULL; -} - - -int qemudDeleteConfig(struct qemud_server *server, - const char *configFile, - const char *name) { - if (!configFile[0]) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "no config file for %s", name); +int qemudDeleteConfigXML(struct qemud_server *server, struct qemud_vm *vm) { + if (!vm->configFile[0]) { + qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "no config file for guest %s", vm->def.name); return -1; } - if (unlink(configFile) < 0) { - qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot remove config for %s", name); + if (unlink(vm->configFile) < 0) { + qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot remove config for guest %s", vm->def.name); return -1; } + vm->configFile[0] = '\0'; + return 0; } -- GitLab