1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

Refactor device parsing code

This commit is contained in:
Daniel P. Berrange 2007-10-27 01:18:38 +00:00
parent 79995893de
commit eed1b17791
3 changed files with 116 additions and 44 deletions

View File

@ -1,3 +1,7 @@
Fri Oct 26 21:14:44 EST 2007 Daniel P. Berrange <berrange@redhat.com>
* src/qemu_conf.h, src/qemu_conf.c: Refactor device parsing code
Fri Oct 26 21:10:44 EST 2007 Daniel P. Berrange <berrange@redhat.com> Fri Oct 26 21:10:44 EST 2007 Daniel P. Berrange <berrange@redhat.com>
* src/qemu_driver.c: make qemudMonitorCommand responsible for * src/qemu_driver.c: make qemudMonitorCommand responsible for

View File

@ -499,11 +499,14 @@ int qemudExtractVersion(virConnectPtr conn,
} }
/* Parse the XML definition for a disk */ /* Parse the XML definition for a disk
static struct qemud_vm_disk_def *qemudParseDiskXML(virConnectPtr conn, * @param disk pre-allocated & zero'd disk record
struct qemud_driver *driver ATTRIBUTE_UNUSED, * @param node XML nodeset to parse for disk definition
xmlNodePtr node) { * @return 0 on success, -1 on failure
struct qemud_vm_disk_def *disk = calloc(1, sizeof(struct qemud_vm_disk_def)); */
static int qemudParseDiskXML(virConnectPtr conn,
struct qemud_vm_disk_def *disk,
xmlNodePtr node) {
xmlNodePtr cur; xmlNodePtr cur;
xmlChar *device = NULL; xmlChar *device = NULL;
xmlChar *source = NULL; xmlChar *source = NULL;
@ -511,11 +514,6 @@ static struct qemud_vm_disk_def *qemudParseDiskXML(virConnectPtr conn,
xmlChar *type = NULL; xmlChar *type = NULL;
int typ = 0; int typ = 0;
if (!disk) {
qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "disk");
return NULL;
}
type = xmlGetProp(node, BAD_CAST "type"); type = xmlGetProp(node, BAD_CAST "type");
if (type != NULL) { if (type != NULL) {
if (xmlStrEqual(type, BAD_CAST "file")) if (xmlStrEqual(type, BAD_CAST "file"))
@ -612,7 +610,7 @@ static struct qemud_vm_disk_def *qemudParseDiskXML(virConnectPtr conn,
xmlFree(target); xmlFree(target);
xmlFree(source); xmlFree(source);
return disk; return 0;
error: error:
if (type) if (type)
@ -623,8 +621,7 @@ static struct qemud_vm_disk_def *qemudParseDiskXML(virConnectPtr conn,
xmlFree(source); xmlFree(source);
if (device) if (device)
xmlFree(device); xmlFree(device);
free(disk); return -1;
return NULL;
} }
static void qemudRandomMAC(struct qemud_vm_net_def *net) { static void qemudRandomMAC(struct qemud_vm_net_def *net) {
@ -637,11 +634,14 @@ static void qemudRandomMAC(struct qemud_vm_net_def *net) {
} }
/* Parse the XML definition for a network interface */ /* Parse the XML definition for a network interface
static struct qemud_vm_net_def *qemudParseInterfaceXML(virConnectPtr conn, * @param net pre-allocated & zero'd net record
struct qemud_driver *driver ATTRIBUTE_UNUSED, * @param node XML nodeset to parse for net definition
xmlNodePtr node) { * @return 0 on success, -1 on failure
struct qemud_vm_net_def *net = calloc(1, sizeof(struct qemud_vm_net_def)); */
static int qemudParseInterfaceXML(virConnectPtr conn,
struct qemud_vm_net_def *net,
xmlNodePtr node) {
xmlNodePtr cur; xmlNodePtr cur;
xmlChar *macaddr = NULL; xmlChar *macaddr = NULL;
xmlChar *type = NULL; xmlChar *type = NULL;
@ -652,11 +652,6 @@ static struct qemud_vm_net_def *qemudParseInterfaceXML(virConnectPtr conn,
xmlChar *address = NULL; xmlChar *address = NULL;
xmlChar *port = NULL; xmlChar *port = NULL;
if (!net) {
qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "net");
return NULL;
}
net->type = QEMUD_NET_USER; net->type = QEMUD_NET_USER;
type = xmlGetProp(node, BAD_CAST "type"); type = xmlGetProp(node, BAD_CAST "type");
@ -869,7 +864,7 @@ static struct qemud_vm_net_def *qemudParseInterfaceXML(virConnectPtr conn,
xmlFree(address); xmlFree(address);
} }
return net; return 0;
error: error:
if (network) if (network)
@ -884,24 +879,17 @@ static struct qemud_vm_net_def *qemudParseInterfaceXML(virConnectPtr conn,
xmlFree(script); xmlFree(script);
if (bridge) if (bridge)
xmlFree(bridge); xmlFree(bridge);
free(net); return -1;
return NULL;
} }
/* Parse the XML definition for a network interface */ /* Parse the XML definition for a network interface */
static struct qemud_vm_input_def *qemudParseInputXML(virConnectPtr conn, static int qemudParseInputXML(virConnectPtr conn,
struct qemud_driver *driver ATTRIBUTE_UNUSED, struct qemud_vm_input_def *input,
xmlNodePtr node) { xmlNodePtr node) {
struct qemud_vm_input_def *input = calloc(1, sizeof(struct qemud_vm_input_def));
xmlChar *type = NULL; xmlChar *type = NULL;
xmlChar *bus = NULL; xmlChar *bus = NULL;
if (!input) {
qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "input");
return NULL;
}
type = xmlGetProp(node, BAD_CAST "type"); type = xmlGetProp(node, BAD_CAST "type");
bus = xmlGetProp(node, BAD_CAST "bus"); bus = xmlGetProp(node, BAD_CAST "bus");
@ -944,7 +932,7 @@ static struct qemud_vm_input_def *qemudParseInputXML(virConnectPtr conn,
if (bus) if (bus)
xmlFree(bus); xmlFree(bus);
return input; return 0;
error: error:
if (type) if (type)
@ -952,8 +940,7 @@ static struct qemud_vm_input_def *qemudParseInputXML(virConnectPtr conn,
if (bus) if (bus)
xmlFree(bus); xmlFree(bus);
free(input); return -1;
return NULL;
} }
@ -1318,8 +1305,13 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
(obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
struct qemud_vm_disk_def *prev = NULL; struct qemud_vm_disk_def *prev = NULL;
for (i = 0; i < obj->nodesetval->nodeNr; i++) { for (i = 0; i < obj->nodesetval->nodeNr; i++) {
struct qemud_vm_disk_def *disk; struct qemud_vm_disk_def *disk = calloc(1, sizeof(struct qemud_vm_disk_def));
if (!(disk = qemudParseDiskXML(conn, driver, obj->nodesetval->nodeTab[i]))) { if (!disk) {
qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "disk");
goto error;
}
if (qemudParseDiskXML(conn, disk, obj->nodesetval->nodeTab[i]) < 0) {
free(disk);
goto error; goto error;
} }
def->ndisks++; def->ndisks++;
@ -1341,8 +1333,13 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
(obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
struct qemud_vm_net_def *prev = NULL; struct qemud_vm_net_def *prev = NULL;
for (i = 0; i < obj->nodesetval->nodeNr; i++) { for (i = 0; i < obj->nodesetval->nodeNr; i++) {
struct qemud_vm_net_def *net; struct qemud_vm_net_def *net = calloc(1, sizeof(struct qemud_vm_net_def));
if (!(net = qemudParseInterfaceXML(conn, driver, obj->nodesetval->nodeTab[i]))) { if (!net) {
qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "net");
goto error;
}
if (qemudParseInterfaceXML(conn, net, obj->nodesetval->nodeTab[i]) < 0) {
free(net);
goto error; goto error;
} }
def->nnets++; def->nnets++;
@ -1363,8 +1360,13 @@ static struct qemud_vm_def *qemudParseXML(virConnectPtr conn,
(obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
struct qemud_vm_input_def *prev = NULL; struct qemud_vm_input_def *prev = NULL;
for (i = 0; i < obj->nodesetval->nodeNr; i++) { for (i = 0; i < obj->nodesetval->nodeNr; i++) {
struct qemud_vm_input_def *input; struct qemud_vm_input_def *input = calloc(1, sizeof(struct qemud_vm_input_def));
if (!(input = qemudParseInputXML(conn, driver, obj->nodesetval->nodeTab[i]))) { if (!input) {
qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "input");
goto error;
}
if (qemudParseInputXML(conn, input, obj->nodesetval->nodeTab[i]) < 0) {
free(input);
goto error; goto error;
} }
/* Mouse + PS/2 is implicit with graphics, so don't store it */ /* Mouse + PS/2 is implicit with graphics, so don't store it */
@ -1929,6 +1931,51 @@ static int qemudSaveConfig(virConnectPtr conn,
return ret; return ret;
} }
struct qemud_vm_device_def *
qemudParseVMDeviceDef(virConnectPtr conn,
struct qemud_driver *driver ATTRIBUTE_UNUSED,
const char *xmlStr)
{
xmlDocPtr xml;
xmlNodePtr node;
struct qemud_vm_device_def *dev = calloc(1, sizeof(struct qemud_vm_device_def));
if (!(xml = xmlReadDoc(BAD_CAST xmlStr, "device.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) {
qemudReportError(conn, NULL, NULL, VIR_ERR_XML_ERROR, NULL);
return NULL;
}
node = xmlDocGetRootElement(xml);
if (node == NULL) {
qemudReportError(conn, NULL, NULL, VIR_ERR_XML_ERROR, "missing root element");
goto error;
}
if (xmlStrEqual(node->name, BAD_CAST "disk")) {
dev->type = QEMUD_DEVICE_DISK;
qemudParseDiskXML(conn, &(dev->data.disk), node);
} else if (xmlStrEqual(node->name, BAD_CAST "net")) {
dev->type = QEMUD_DEVICE_NET;
qemudParseInterfaceXML(conn, &(dev->data.net), node);
} else if (xmlStrEqual(node->name, BAD_CAST "input")) {
dev->type = QEMUD_DEVICE_DISK;
qemudParseInputXML(conn, &(dev->data.input), node);
} else {
qemudReportError(conn, NULL, NULL, VIR_ERR_XML_ERROR, "unknown device type");
goto error;
}
xmlFreeDoc(xml);
return dev;
error:
if (xml) xmlFreeDoc(xml);
if (dev) free(dev);
return NULL;
}
struct qemud_vm_def * struct qemud_vm_def *
qemudParseVMDef(virConnectPtr conn, qemudParseVMDef(virConnectPtr conn,
struct qemud_driver *driver, struct qemud_driver *driver,

View File

@ -125,6 +125,22 @@ struct qemud_vm_input_def {
struct qemud_vm_input_def *next; struct qemud_vm_input_def *next;
}; };
/* Flags for the 'type' field in next struct */
enum qemud_vm_device_type {
QEMUD_DEVICE_DISK,
QEMUD_DEVICE_NET,
QEMUD_DEVICE_INPUT,
};
struct qemud_vm_device_def {
int type;
union {
struct qemud_vm_disk_def disk;
struct qemud_vm_net_def net;
struct qemud_vm_input_def input;
} data;
};
#define QEMUD_MAX_BOOT_DEVS 4 #define QEMUD_MAX_BOOT_DEVS 4
/* 3 possible boot devices */ /* 3 possible boot devices */
@ -354,6 +370,11 @@ struct qemud_vm *
void qemudRemoveInactiveVM (struct qemud_driver *driver, void qemudRemoveInactiveVM (struct qemud_driver *driver,
struct qemud_vm *vm); struct qemud_vm *vm);
struct qemud_vm_device_def *
qemudParseVMDeviceDef (virConnectPtr conn,
struct qemud_driver *driver,
const char *xmlStr);
struct qemud_vm_def * struct qemud_vm_def *
qemudParseVMDef (virConnectPtr conn, qemudParseVMDef (virConnectPtr conn,
struct qemud_driver *driver, struct qemud_driver *driver,