Re: PATCH 10/20: switch QEMU to using the driver API

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is the 2nd key patch in the series. It switches the qemud/driver.c
file methods to all follow the official libvirt internal driver API.
NB, since we're using dummy statically declared virConnect object in
the dispatch.c file, we can't use the regular virGetDomain function
for constructing virDomainPtr objects at this time. Thus we have a
hack to just malloc a virDomain struct & manually fill in the fields.
This hack will go away in a later patch.

 conf.c     |    2 
 dispatch.c |  449 ++++++++++++++++++++++-----------
 driver.c   |  828 +++++++++++++++++++++++++++++++++++++++++++------------------
 driver.h   |  141 ++++------
 4 files changed, 951 insertions(+), 469 deletions(-)

Dan.
-- 
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
diff -r 685e00bb55b5 qemud/conf.c
--- a/qemud/conf.c	Thu Jun 21 21:01:55 2007 -0400
+++ b/qemud/conf.c	Thu Jun 21 21:13:27 2007 -0400
@@ -1610,7 +1610,7 @@ qemudAssignVMDef(struct qemud_driver *dr
     vm->monitor = -1;
     vm->pid = -1;
     vm->id = -1;
-    vm->state = QEMUD_STATE_STOPPED;
+    vm->state = VIR_DOMAIN_SHUTOFF;
     vm->def = def;
     vm->next = driver->vms;
 
diff -r 685e00bb55b5 qemud/dispatch.c
--- a/qemud/dispatch.c	Thu Jun 21 21:01:55 2007 -0400
+++ b/qemud/dispatch.c	Thu Jun 21 21:20:45 2007 -0400
@@ -37,22 +37,34 @@ extern struct qemud_driver *qemu_driver;
 extern struct qemud_driver *qemu_driver;
 
 
+static virConnect conn;
+
 static int qemudDispatchFailure(struct qemud_packet_server_data *out) {
     virErrorPtr err = virGetLastError();
+    if (!err)
+        err = virConnGetLastError(&conn);
 
     out->type = QEMUD_SERVER_PKT_FAILURE;
 
-    out->qemud_packet_server_data_u.failureReply.code = err->code;
-    strncpy(out->qemud_packet_server_data_u.failureReply.message,
-            err->message, QEMUD_MAX_ERROR_LEN-1);
-    out->qemud_packet_server_data_u.failureReply.message[QEMUD_MAX_ERROR_LEN-1] = '\0';
+    if (err) {
+        out->qemud_packet_server_data_u.failureReply.code = err->code;
+        strncpy(out->qemud_packet_server_data_u.failureReply.message,
+                err->message, QEMUD_MAX_ERROR_LEN-1);
+        out->qemud_packet_server_data_u.failureReply.message[QEMUD_MAX_ERROR_LEN-1] = '\0';
+    } else {
+        out->qemud_packet_server_data_u.failureReply.code = VIR_ERR_INTERNAL_ERROR;
+        strcpy(out->qemud_packet_server_data_u.failureReply.message,
+               "Unknown error");
+    }
     return 0;
 }
 
 
 static int qemudDispatchGetVersion(struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
-    int version = qemudGetVersion(qemu_driver);
-    if (version < 0) {
+    int ret;
+    unsigned long version;
+    ret = qemudGetVersion(&conn, &version);
+    if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
@@ -63,21 +75,25 @@ static int qemudDispatchGetVersion(struc
 }
 
 static int qemudDispatchGetNodeInfo(struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
-    if (qemudGetNodeInfo(&out->qemud_packet_server_data_u.getNodeInfoReply.memory,
-                         out->qemud_packet_server_data_u.getNodeInfoReply.model,
-                         sizeof(out->qemud_packet_server_data_u.getNodeInfoReply.model),
-                         &out->qemud_packet_server_data_u.getNodeInfoReply.cpus,
-                         &out->qemud_packet_server_data_u.getNodeInfoReply.mhz,
-                         &out->qemud_packet_server_data_u.getNodeInfoReply.nodes,
-                         &out->qemud_packet_server_data_u.getNodeInfoReply.sockets,
-                         &out->qemud_packet_server_data_u.getNodeInfoReply.cores,
-                         &out->qemud_packet_server_data_u.getNodeInfoReply.threads) < 0) {
-        if (qemudDispatchFailure(out) < 0)
-            return -1;
-        return 0;
-    }
+    virNodeInfo info;
+    if (qemudGetNodeInfo(&conn,
+                         &info) < 0) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
+    out->qemud_packet_server_data_u.getNodeInfoReply.memory = info.memory;
+    out->qemud_packet_server_data_u.getNodeInfoReply.cpus = info.cpus;
+    out->qemud_packet_server_data_u.getNodeInfoReply.mhz = info.mhz;
+    out->qemud_packet_server_data_u.getNodeInfoReply.nodes = info.nodes;
+    out->qemud_packet_server_data_u.getNodeInfoReply.sockets = info.sockets;
+    out->qemud_packet_server_data_u.getNodeInfoReply.cores = info.cores;
+    out->qemud_packet_server_data_u.getNodeInfoReply.threads = info.threads;
 
     out->type = QEMUD_SERVER_PKT_GET_NODEINFO;
+    strncpy(out->qemud_packet_server_data_u.getNodeInfoReply.model, info.model,
+            sizeof(out->qemud_packet_server_data_u.getNodeInfoReply.model)-1);
     out->qemud_packet_server_data_u.getNodeInfoReply.model[sizeof(out->qemud_packet_server_data_u.getNodeInfoReply.model)-1] = '\0';
 
     return 0;
@@ -87,10 +103,10 @@ qemudDispatchGetCapabilities (struct qem
 qemudDispatchGetCapabilities (struct qemud_packet_client_data *in ATTRIBUTE_UNUSED,
                               struct qemud_packet_server_data *out)
 {
-    char *xml = qemudGetCapabilities(qemu_driver);
+    char *xml = qemudGetCapabilities(&conn);
 
     if (strlen(xml) > QEMUD_MAX_XML_LEN) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_XML_ERROR, NULL);
+        qemudReportError(&conn, NULL, NULL, VIR_ERR_XML_ERROR, NULL);
         qemudDispatchFailure(out);
         free(xml);
         return 0;
@@ -105,7 +121,7 @@ static int qemudDispatchListDomains(stru
 static int qemudDispatchListDomains(struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
     int i, ndomains, domains[QEMUD_MAX_NUM_DOMAINS];
 
-    ndomains = qemudListDomains(qemu_driver,
+    ndomains = qemudListDomains(&conn,
                                 domains,
                                 QEMUD_MAX_NUM_DOMAINS);
     if (ndomains < 0) {
@@ -122,7 +138,7 @@ static int qemudDispatchListDomains(stru
 }
 
 static int qemudDispatchNumDomains(struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
-    int ndomains = qemudNumDomains(qemu_driver);
+    int ndomains = qemudNumDomains(&conn);
     if (ndomains < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -136,44 +152,47 @@ static int qemudDispatchDomainCreate(str
 static int qemudDispatchDomainCreate(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
     in->qemud_packet_client_data_u.domainCreateRequest.xml[QEMUD_MAX_XML_LEN-1] ='\0';
 
-    struct qemud_vm *vm = qemudDomainCreate(qemu_driver, in->qemud_packet_client_data_u.domainCreateRequest.xml);
-    if (!vm) {
+    virDomainPtr dom = qemudDomainCreate(&conn, in->qemud_packet_client_data_u.domainCreateRequest.xml, 0);
+    if (!dom) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DOMAIN_CREATE;
-        out->qemud_packet_server_data_u.domainCreateReply.id = vm->id;
-        memcpy(out->qemud_packet_server_data_u.domainCreateReply.uuid, vm->def->uuid, QEMUD_UUID_RAW_LEN);
-        strncpy(out->qemud_packet_server_data_u.domainCreateReply.name, vm->def->name, QEMUD_MAX_NAME_LEN-1);
+        out->qemud_packet_server_data_u.domainCreateReply.id = dom->id;
+        memcpy(out->qemud_packet_server_data_u.domainCreateReply.uuid, dom->uuid, QEMUD_UUID_RAW_LEN);
+        strncpy(out->qemud_packet_server_data_u.domainCreateReply.name, dom->name, QEMUD_MAX_NAME_LEN-1);
         out->qemud_packet_server_data_u.domainCreateReply.name[QEMUD_MAX_NAME_LEN-1] = '\0';
+        free(dom);
     }
     return 0;
 }
 
 static int qemudDispatchDomainLookupByID(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    struct qemud_vm *vm = qemudFindVMByID(qemu_driver, in->qemud_packet_client_data_u.domainLookupByIDRequest.id);
-    if (!vm) {
+    virDomainPtr dom = qemudDomainLookupByID(&conn, in->qemud_packet_client_data_u.domainLookupByIDRequest.id);
+    if (!dom) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DOMAIN_LOOKUP_BY_ID;
-        memcpy(out->qemud_packet_server_data_u.domainLookupByIDReply.uuid, vm->def->uuid, QEMUD_UUID_RAW_LEN);
-        strncpy(out->qemud_packet_server_data_u.domainLookupByIDReply.name, vm->def->name, QEMUD_MAX_NAME_LEN-1);
+        memcpy(out->qemud_packet_server_data_u.domainLookupByIDReply.uuid, dom->uuid, QEMUD_UUID_RAW_LEN);
+        strncpy(out->qemud_packet_server_data_u.domainLookupByIDReply.name, dom->name, QEMUD_MAX_NAME_LEN-1);
         out->qemud_packet_server_data_u.domainLookupByIDReply.name[QEMUD_MAX_NAME_LEN-1] = '\0';
+        free(dom);
     }
     return 0;
 }
 
 static int qemudDispatchDomainLookupByUUID(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    struct qemud_vm *vm = qemudFindVMByUUID(qemu_driver, in->qemud_packet_client_data_u.domainLookupByUUIDRequest.uuid);
-    if (!vm) {
+    virDomainPtr dom = qemudDomainLookupByUUID(&conn, in->qemud_packet_client_data_u.domainLookupByUUIDRequest.uuid);
+    if (!dom) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DOMAIN_LOOKUP_BY_UUID;
-        out->qemud_packet_server_data_u.domainLookupByUUIDReply.id = vm->id;
-        strncpy(out->qemud_packet_server_data_u.domainLookupByUUIDReply.name, vm->def->name, QEMUD_MAX_NAME_LEN-1);
+        out->qemud_packet_server_data_u.domainLookupByUUIDReply.id = dom->id;
+        strncpy(out->qemud_packet_server_data_u.domainLookupByUUIDReply.name, dom->name, QEMUD_MAX_NAME_LEN-1);
         out->qemud_packet_server_data_u.domainLookupByUUIDReply.name[QEMUD_MAX_NAME_LEN-1] = '\0';
+        free(dom);
     }
     return 0;
 }
@@ -181,20 +200,30 @@ static int qemudDispatchDomainLookupByNa
 static int qemudDispatchDomainLookupByName(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
     /* Paranoia NULL termination */
     in->qemud_packet_client_data_u.domainLookupByNameRequest.name[QEMUD_MAX_NAME_LEN-1] = '\0';
-    struct qemud_vm *vm = qemudFindVMByName(qemu_driver, in->qemud_packet_client_data_u.domainLookupByNameRequest.name);
-    if (!vm) {
+    virDomainPtr dom = qemudDomainLookupByName(&conn, in->qemud_packet_client_data_u.domainLookupByNameRequest.name);
+    if (!dom) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DOMAIN_LOOKUP_BY_NAME;
-        out->qemud_packet_server_data_u.domainLookupByNameReply.id = vm->id;
-        memcpy(out->qemud_packet_server_data_u.domainLookupByNameReply.uuid, vm->def->uuid, QEMUD_UUID_RAW_LEN);
+        out->qemud_packet_server_data_u.domainLookupByNameReply.id = dom->id;
+        memcpy(out->qemud_packet_server_data_u.domainLookupByNameReply.uuid, dom->uuid, QEMUD_UUID_RAW_LEN);
+        free(dom);
     }
     return 0;
 }
 
 static int qemudDispatchDomainSuspend(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int ret = qemudDomainSuspend(qemu_driver, in->qemud_packet_client_data_u.domainSuspendRequest.id);
+    virDomainPtr dom = qemudDomainLookupByID(&conn, in->qemud_packet_client_data_u.domainSuspendRequest.id);
+    int ret;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
+    ret = qemudDomainSuspend(dom);
+    free(dom);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -205,7 +234,16 @@ static int qemudDispatchDomainSuspend(st
 }
 
 static int qemudDispatchDomainResume(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int ret = qemudDomainResume(qemu_driver, in->qemud_packet_client_data_u.domainResumeRequest.id);
+    virDomainPtr dom = qemudDomainLookupByID(&conn, in->qemud_packet_client_data_u.domainResumeRequest.id);
+    int ret;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
+    ret = qemudDomainResume(dom);
+    free(dom);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -216,7 +254,17 @@ static int qemudDispatchDomainResume(str
 }
 
 static int qemudDispatchDomainDestroy(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    if (qemudDomainDestroy(qemu_driver, in->qemud_packet_client_data_u.domainDestroyRequest.id) < 0) {
+    virDomainPtr dom = qemudDomainLookupByID(&conn, in->qemud_packet_client_data_u.domainDestroyRequest.id);
+    int ret;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
+    ret = qemudDomainDestroy(dom);
+    free(dom);
+    if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
@@ -226,39 +274,44 @@ static int qemudDispatchDomainDestroy(st
 }
 
 static int qemudDispatchDomainGetInfo(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int runstate;
-    unsigned long long cpuTime;
-    unsigned long memory;
-    unsigned long maxmem;
-    unsigned int nrVirtCpu;
-
-    int ret = qemudDomainGetInfo(qemu_driver, in->qemud_packet_client_data_u.domainGetInfoRequest.uuid,
-                                 &runstate,
-                                 &cpuTime,
-                                 &maxmem,
-                                 &memory,
-                                 &nrVirtCpu);
-    if (ret < 0) {
+    virDomainInfo info;
+    virDomainPtr dom = qemudDomainLookupByUUID(&conn, in->qemud_packet_client_data_u.domainGetInfoRequest.uuid);
+
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
+    if (qemudDomainGetInfo(dom, &info) < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DOMAIN_GET_INFO;
-        out->qemud_packet_server_data_u.domainGetInfoReply.runstate = runstate;
-        out->qemud_packet_server_data_u.domainGetInfoReply.cpuTime = cpuTime;
-        out->qemud_packet_server_data_u.domainGetInfoReply.maxmem = maxmem;
-        out->qemud_packet_server_data_u.domainGetInfoReply.memory = memory;
-        out->qemud_packet_server_data_u.domainGetInfoReply.nrVirtCpu = nrVirtCpu;
+        out->qemud_packet_server_data_u.domainGetInfoReply.runstate = info.state;
+        out->qemud_packet_server_data_u.domainGetInfoReply.cpuTime = info.cpuTime;
+        out->qemud_packet_server_data_u.domainGetInfoReply.maxmem = info.maxMem;
+        out->qemud_packet_server_data_u.domainGetInfoReply.memory = info.memory;
+        out->qemud_packet_server_data_u.domainGetInfoReply.nrVirtCpu = info.nrVirtCpu;
     }
     return 0;
 }
 
 static int qemudDispatchDomainSave(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
+    virDomainPtr dom = qemudDomainLookupByID(&conn, in->qemud_packet_client_data_u.domainSaveRequest.id);
+    int ret;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
     /* Paranoia NULL termination */
     in->qemud_packet_client_data_u.domainSaveRequest.file[PATH_MAX-1] ='\0';
 
-    int ret = qemudDomainSave(qemu_driver,
-                              in->qemud_packet_client_data_u.domainSaveRequest.id,
-                              in->qemud_packet_client_data_u.domainSaveRequest.file);
+    ret = qemudDomainSave(dom,
+                          in->qemud_packet_client_data_u.domainSaveRequest.file);
+    free(dom);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -269,33 +322,42 @@ static int qemudDispatchDomainSave(struc
 }
 
 static int qemudDispatchDomainRestore(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int id;
+    int ret;
 
     /* Paranoia null termination */
     in->qemud_packet_client_data_u.domainRestoreRequest.file[PATH_MAX-1] ='\0';
 
-    id = qemudDomainRestore(qemu_driver, in->qemud_packet_client_data_u.domainRestoreRequest.file);
-    if (id < 0) {
+    ret = qemudDomainRestore(&conn, in->qemud_packet_client_data_u.domainRestoreRequest.file);
+    if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DOMAIN_RESTORE;
-        out->qemud_packet_server_data_u.domainRestoreReply.id = id;
+        out->qemud_packet_server_data_u.domainRestoreReply.id = ret;
     }
     return 0;
 }
 
 static int qemudDispatchDumpXML(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int ret;
-    ret = qemudDomainDumpXML(qemu_driver,
-                             in->qemud_packet_client_data_u.domainDumpXMLRequest.uuid,
-                             out->qemud_packet_server_data_u.domainDumpXMLReply.xml,
-                             QEMUD_MAX_XML_LEN);
-    if (ret < 0) {
+    virDomainPtr dom = qemudDomainLookupByUUID(&conn, in->qemud_packet_client_data_u.domainDumpXMLRequest.uuid);
+    char *ret;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
+    ret = qemudDomainDumpXML(dom, 0);
+    free(dom);
+    if (!ret) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DUMP_XML;
+        strncpy(out->qemud_packet_server_data_u.domainDumpXMLReply.xml,
+                ret, QEMUD_MAX_XML_LEN-1);
+        out->qemud_packet_server_data_u.domainDumpXMLReply.xml[QEMUD_MAX_XML_LEN-1] = '\0';
+        free(ret);
     }
     return 0;
 }
@@ -307,13 +369,16 @@ static int qemudDispatchListDefinedDomai
     if (!(names = malloc(sizeof(char *)*QEMUD_MAX_NUM_DOMAINS)))
         return -1;
 
-    for (i = 0 ; i < QEMUD_MAX_NUM_DOMAINS ; i++) {
-        names[i] = &out->qemud_packet_server_data_u.listDefinedDomainsReply.domains[i*QEMUD_MAX_NAME_LEN];
-    }
-
-    ndomains = qemudListDefinedDomains(qemu_driver,
+    ndomains = qemudListDefinedDomains(&conn,
                                        names,
                                        QEMUD_MAX_NUM_DOMAINS);
+    for (i = 0 ; i < ndomains ; i++) {
+        strncpy(&out->qemud_packet_server_data_u.listDefinedDomainsReply.domains[i*QEMUD_MAX_NAME_LEN],
+                names[i],
+                QEMUD_MAX_NAME_LEN);
+        free(names[i]);
+    }
+
     free(names);
     if (ndomains < 0) {
         if (qemudDispatchFailure(out) < 0)
@@ -322,15 +387,11 @@ static int qemudDispatchListDefinedDomai
         out->type = QEMUD_SERVER_PKT_LIST_DEFINED_DOMAINS;
         out->qemud_packet_server_data_u.listDefinedDomainsReply.numDomains = ndomains;
     }
-    printf("%d %d\n", out->type, out->qemud_packet_server_data_u.listDefinedDomainsReply.numDomains);
-    for (i = 0 ; i < ndomains;i++) {
-        printf("[%s]\n", &out->qemud_packet_server_data_u.listDefinedDomainsReply.domains[i*QEMUD_MAX_NAME_LEN]);
-    }
     return 0;
 }
 
 static int qemudDispatchNumDefinedDomains(struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
-    int ndomains = qemudNumDefinedDomains(qemu_driver);
+    int ndomains = qemudNumDefinedDomains(&conn);
     if (ndomains < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -342,14 +403,22 @@ static int qemudDispatchNumDefinedDomain
 }
 
 static int qemudDispatchDomainStart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    struct qemud_vm *vm;
-
-    if (!(vm = qemudDomainStart(qemu_driver, in->qemud_packet_client_data_u.domainStartRequest.uuid))) {
+    virDomainPtr dom = qemudDomainLookupByUUID(&conn, in->qemud_packet_client_data_u.domainStartRequest.uuid);
+    int ret;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
+    ret = qemudDomainStart(dom);
+    free(dom);
+    if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DOMAIN_START;
-        out->qemud_packet_server_data_u.domainStartReply.id = vm->id;
+        out->qemud_packet_server_data_u.domainStartReply.id = dom->id;
     }
     return 0;
 }
@@ -357,21 +426,29 @@ static int qemudDispatchDomainDefine(str
 static int qemudDispatchDomainDefine(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
     in->qemud_packet_client_data_u.domainDefineRequest.xml[QEMUD_MAX_XML_LEN-1] ='\0';
 
-    struct qemud_vm *vm = qemudDomainDefine(qemu_driver, in->qemud_packet_client_data_u.domainDefineRequest.xml);
-    if (!vm) {
+    virDomainPtr dom = qemudDomainDefine(&conn, in->qemud_packet_client_data_u.domainDefineRequest.xml);
+    if (!dom) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_DOMAIN_DEFINE;
-        memcpy(out->qemud_packet_server_data_u.domainDefineReply.uuid, vm->def->uuid, QEMUD_UUID_RAW_LEN);
-        strncpy(out->qemud_packet_server_data_u.domainDefineReply.name, vm->def->name, QEMUD_MAX_NAME_LEN-1);
+        memcpy(out->qemud_packet_server_data_u.domainDefineReply.uuid, dom->uuid, QEMUD_UUID_RAW_LEN);
+        strncpy(out->qemud_packet_server_data_u.domainDefineReply.name, dom->name, QEMUD_MAX_NAME_LEN-1);
         out->qemud_packet_server_data_u.domainDefineReply.name[QEMUD_MAX_NAME_LEN-1] = '\0';
     }
     return 0;
 }
 
 static int qemudDispatchDomainUndefine(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int ret = qemudDomainUndefine(qemu_driver, in->qemud_packet_client_data_u.domainUndefineRequest.uuid);
+    virDomainPtr dom = qemudDomainLookupByUUID(&conn, in->qemud_packet_client_data_u.domainUndefineRequest.uuid);
+    int ret;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+    ret = qemudDomainUndefine(dom);
+    free(dom);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -382,7 +459,7 @@ static int qemudDispatchDomainUndefine(s
 }
 
 static int qemudDispatchNumNetworks(struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
-    int nnetworks = qemudNumNetworks(qemu_driver);
+    int nnetworks = qemudNumNetworks(&conn);
     if (nnetworks < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -400,13 +477,15 @@ static int qemudDispatchListNetworks(str
     if (!(names = malloc(sizeof(char *)*QEMUD_MAX_NUM_NETWORKS)))
         return -1;
 
-    for (i = 0 ; i < QEMUD_MAX_NUM_NETWORKS ; i++) {
-        names[i] = &out->qemud_packet_server_data_u.listNetworksReply.networks[i*QEMUD_MAX_NAME_LEN];
-    }
-
-    int nnetworks = qemudListNetworks(qemu_driver,
+    int nnetworks = qemudListNetworks(&conn,
                                       names,
                                       QEMUD_MAX_NUM_NETWORKS);
+    for (i = 0 ; i < nnetworks ; i++) {
+        strncpy(&out->qemud_packet_server_data_u.listNetworksReply.networks[i*QEMUD_MAX_NAME_LEN],
+                names[i],
+                QEMUD_MAX_NAME_LEN);
+        free(names[i]);
+    }
     free(names);
     if (nnetworks < 0) {
         if (qemudDispatchFailure(out) < 0)
@@ -419,7 +498,7 @@ static int qemudDispatchListNetworks(str
 }
 
 static int qemudDispatchNumDefinedNetworks(struct qemud_packet_client_data *in ATTRIBUTE_UNUSED, struct qemud_packet_server_data *out) {
-    int nnetworks = qemudNumDefinedNetworks(qemu_driver);
+    int nnetworks = qemudNumDefinedNetworks(&conn);
     if (nnetworks < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -437,13 +516,16 @@ static int qemudDispatchListDefinedNetwo
     if (!(names = malloc(sizeof(char *)*QEMUD_MAX_NUM_NETWORKS)))
         return -1;
 
-    for (i = 0 ; i < QEMUD_MAX_NUM_NETWORKS ; i++) {
-        names[i] = &out->qemud_packet_server_data_u.listDefinedNetworksReply.networks[i*QEMUD_MAX_NAME_LEN];
-    }
-
-    int nnetworks = qemudListDefinedNetworks(qemu_driver,
+    int nnetworks = qemudListDefinedNetworks(&conn,
                                              names,
                                              QEMUD_MAX_NUM_NETWORKS);
+
+    for (i = 0 ; i < nnetworks ; i++) {
+        strncpy(&out->qemud_packet_server_data_u.listDefinedNetworksReply.networks[i*QEMUD_MAX_NAME_LEN],
+                names[i],
+                QEMUD_MAX_NAME_LEN);
+        free(names[i]);
+    }
     free(names);
     if (nnetworks < 0) {
         if (qemudDispatchFailure(out) < 0)
@@ -485,15 +567,16 @@ static int qemudDispatchNetworkCreate(st
 static int qemudDispatchNetworkCreate(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
     in->qemud_packet_client_data_u.networkCreateRequest.xml[QEMUD_MAX_XML_LEN-1] ='\0';
 
-    struct qemud_network *network = qemudNetworkCreate(qemu_driver, in->qemud_packet_client_data_u.networkCreateRequest.xml);
-    if (!network) {
+    virNetworkPtr net = qemudNetworkCreate(&conn, in->qemud_packet_client_data_u.networkCreateRequest.xml);
+    if (!net) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_NETWORK_CREATE;
-        memcpy(out->qemud_packet_server_data_u.networkCreateReply.uuid, network->def->uuid, QEMUD_UUID_RAW_LEN);
-        strncpy(out->qemud_packet_server_data_u.networkCreateReply.name, network->def->name, QEMUD_MAX_NAME_LEN-1);
+        memcpy(out->qemud_packet_server_data_u.networkCreateReply.uuid, net->uuid, QEMUD_UUID_RAW_LEN);
+        strncpy(out->qemud_packet_server_data_u.networkCreateReply.name, net->name, QEMUD_MAX_NAME_LEN-1);
         out->qemud_packet_server_data_u.networkCreateReply.name[QEMUD_MAX_NAME_LEN-1] = '\0';
+        free(net);
     }
     return 0;
 }
@@ -501,21 +584,30 @@ static int qemudDispatchNetworkDefine(st
 static int qemudDispatchNetworkDefine(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
     in->qemud_packet_client_data_u.networkDefineRequest.xml[QEMUD_MAX_XML_LEN-1] ='\0';
 
-    struct qemud_network *network = qemudNetworkDefine(qemu_driver, in->qemud_packet_client_data_u.networkDefineRequest.xml);
-    if (!network) {
+    virNetworkPtr net = qemudNetworkDefine(&conn, in->qemud_packet_client_data_u.networkDefineRequest.xml);
+    if (!net) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_NETWORK_DEFINE;
-        memcpy(out->qemud_packet_server_data_u.networkDefineReply.uuid, network->def->uuid, QEMUD_UUID_RAW_LEN);
-        strncpy(out->qemud_packet_server_data_u.networkDefineReply.name, network->def->name, QEMUD_MAX_NAME_LEN-1);
+        memcpy(out->qemud_packet_server_data_u.networkDefineReply.uuid, net->uuid, QEMUD_UUID_RAW_LEN);
+        strncpy(out->qemud_packet_server_data_u.networkDefineReply.name, net->name, QEMUD_MAX_NAME_LEN-1);
         out->qemud_packet_server_data_u.networkDefineReply.name[QEMUD_MAX_NAME_LEN-1] = '\0';
+        free(net);
     }
     return 0;
 }
 
 static int qemudDispatchNetworkUndefine(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int ret = qemudNetworkUndefine(qemu_driver, in->qemud_packet_client_data_u.networkUndefineRequest.uuid);
+    virNetworkPtr net = qemudNetworkLookupByUUID(&conn, in->qemud_packet_client_data_u.networkUndefineRequest.uuid);
+    int ret;
+    if (!net) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+    }
+
+    ret = qemudNetworkUndefine(net);
+    free(net);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -526,9 +618,16 @@ static int qemudDispatchNetworkUndefine(
 }
 
 static int qemudDispatchNetworkStart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    struct qemud_network *network;
-
-    if (!(network = qemudNetworkStart(qemu_driver, in->qemud_packet_client_data_u.networkStartRequest.uuid))) {
+    virNetworkPtr net = qemudNetworkLookupByUUID(&conn, in->qemud_packet_client_data_u.networkStartRequest.uuid);
+    int ret;
+    if (!net) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+    }
+
+    ret = qemudNetworkStart(net);
+    free(net);
+    if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
@@ -538,7 +637,16 @@ static int qemudDispatchNetworkStart(str
 }
 
 static int qemudDispatchNetworkDestroy(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    if (qemudNetworkDestroy(qemu_driver, in->qemud_packet_client_data_u.networkDestroyRequest.uuid) < 0) {
+    virNetworkPtr net = qemudNetworkLookupByUUID(&conn, in->qemud_packet_client_data_u.networkDestroyRequest.uuid);
+    int ret;
+    if (!net) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+    }
+
+    ret = qemudNetworkDestroy(net);
+    free(net);
+    if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
@@ -548,41 +656,64 @@ static int qemudDispatchNetworkDestroy(s
 }
 
 static int qemudDispatchNetworkDumpXML(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int ret = qemudNetworkDumpXML(qemu_driver,
-                                  in->qemud_packet_client_data_u.networkDumpXMLRequest.uuid,
-                                  out->qemud_packet_server_data_u.networkDumpXMLReply.xml, QEMUD_MAX_XML_LEN);
-    if (ret < 0) {
+    virNetworkPtr net = qemudNetworkLookupByUUID(&conn, in->qemud_packet_client_data_u.networkDumpXMLRequest.uuid);
+    char *ret;
+    if (!net) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+    }
+
+    ret = qemudNetworkDumpXML(net, 0);
+    free(net);
+    if (!ret) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_NETWORK_DUMP_XML;
+        strncpy(out->qemud_packet_server_data_u.networkDumpXMLReply.xml, ret, QEMUD_MAX_XML_LEN-1);
+        out->qemud_packet_server_data_u.networkDumpXMLReply.xml[QEMUD_MAX_XML_LEN-1] = '\0';
+        free(ret);
     }
     return 0;
 }
 
 static int qemudDispatchNetworkGetBridgeName(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
-    int ret = qemudNetworkGetBridgeName(qemu_driver,
-                                        in->qemud_packet_client_data_u.networkDumpXMLRequest.uuid,
-                                        out->qemud_packet_server_data_u.networkGetBridgeNameReply.ifname, QEMUD_MAX_IFNAME_LEN);
-    if (ret < 0) {
+    virNetworkPtr net = qemudNetworkLookupByUUID(&conn, in->qemud_packet_client_data_u.networkDumpXMLRequest.uuid);
+    char *ret;
+    if (!net) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+    }
+
+    ret = qemudNetworkGetBridgeName(net);
+    free(net);
+    if (!ret) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
     } else {
         out->type = QEMUD_SERVER_PKT_NETWORK_GET_BRIDGE_NAME;
-    }
-    return 0;
-}
-
-static int qemudDispatchDomainGetAutostart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out)
-{
+        strncpy(out->qemud_packet_server_data_u.networkGetBridgeNameReply.ifname, ret, QEMUD_MAX_IFNAME_LEN-1);
+        out->qemud_packet_server_data_u.networkGetBridgeNameReply.ifname[QEMUD_MAX_IFNAME_LEN-1] = '\0';
+        free(ret);
+    }
+    return 0;
+}
+
+static int qemudDispatchDomainGetAutostart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
+    virDomainPtr dom = qemudDomainLookupByUUID(&conn, in->qemud_packet_client_data_u.domainGetAutostartRequest.uuid);
     int ret;
     int autostart;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
 
     autostart = 0;
 
-    ret = qemudDomainGetAutostart(qemu_driver,
-                                  in->qemud_packet_client_data_u.domainGetAutostartRequest.uuid,
+    ret = qemudDomainGetAutostart(dom,
                                   &autostart);
+    free(dom);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -593,13 +724,18 @@ static int qemudDispatchDomainGetAutosta
     return 0;
 }
 
-static int qemudDispatchDomainSetAutostart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out)
-{
-    int ret;
-
-    ret = qemudDomainSetAutostart(qemu_driver,
-                                  in->qemud_packet_client_data_u.domainGetAutostartRequest.uuid,
+static int qemudDispatchDomainSetAutostart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
+    virDomainPtr dom = qemudDomainLookupByUUID(&conn, in->qemud_packet_client_data_u.domainSetAutostartRequest.uuid);
+    int ret;
+    if (!dom) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+        return 0;
+    }
+
+    ret = qemudDomainSetAutostart(dom,
                                   in->qemud_packet_client_data_u.domainSetAutostartRequest.autostart);
+    free(dom);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -609,16 +745,20 @@ static int qemudDispatchDomainSetAutosta
     return 0;
 }
 
-static int qemudDispatchNetworkGetAutostart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out)
-{
+static int qemudDispatchNetworkGetAutostart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
+    virNetworkPtr net = qemudNetworkLookupByUUID(&conn, in->qemud_packet_client_data_u.networkGetAutostartRequest.uuid);
     int ret;
     int autostart;
+    if (!net) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+    }
 
     autostart = 0;
 
-    ret = qemudNetworkGetAutostart(qemu_driver,
-                                   in->qemud_packet_client_data_u.networkGetAutostartRequest.uuid,
+    ret = qemudNetworkGetAutostart(net,
                                    &autostart);
+    free(net);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -629,13 +769,17 @@ static int qemudDispatchNetworkGetAutost
     return 0;
 }
 
-static int qemudDispatchNetworkSetAutostart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out)
-{
-    int ret;
-
-    ret = qemudNetworkSetAutostart(qemu_driver,
-                                   in->qemud_packet_client_data_u.networkGetAutostartRequest.uuid,
+static int qemudDispatchNetworkSetAutostart(struct qemud_packet_client_data *in, struct qemud_packet_server_data *out) {
+    virNetworkPtr net = qemudNetworkLookupByUUID(&conn, in->qemud_packet_client_data_u.networkGetAutostartRequest.uuid);
+    int ret;
+    if (!net) {
+        if (qemudDispatchFailure(out) < 0)
+            return -1;
+    }
+
+    ret = qemudNetworkSetAutostart(net,
                                    in->qemud_packet_client_data_u.networkSetAutostartRequest.autostart);
+    free(net);
     if (ret < 0) {
         if (qemudDispatchFailure(out) < 0)
             return -1;
@@ -750,7 +894,12 @@ int qemudDispatch(struct qemud_server *s
     qemudDebug("> Dispatching request type %d, readonly ? %d",
                in->type, client->readonly);
 
+    if (!conn.magic) {
+        qemudOpen(&conn, "qemu:///session", 0);
+        conn.magic = 1;
+    }
     virResetLastError();
+    virConnResetLastError(&conn);
 
     memset(out, 0, sizeof(*out));
 
diff -r 685e00bb55b5 qemud/driver.c
--- a/qemud/driver.c	Thu Jun 21 21:01:55 2007 -0400
+++ b/qemud/driver.c	Thu Jun 21 21:20:22 2007 -0400
@@ -648,7 +648,7 @@ int qemudStartVMDaemon(struct qemud_driv
 
     if (qemudExec(argv, &vm->pid, &vm->stdout, &vm->stderr) == 0) {
         vm->id = driver->nextvmid++;
-        vm->state = QEMUD_STATE_RUNNING;
+        vm->state = VIR_DOMAIN_RUNNING;
 
         driver->ninactivevms--;
         driver->nactivevms++;
@@ -757,7 +757,7 @@ int qemudShutdownVMDaemon(struct qemud_d
 
     vm->pid = -1;
     vm->id = -1;
-    vm->state = QEMUD_STATE_STOPPED;
+    vm->state = VIR_DOMAIN_SHUTOFF;
 
     if (vm->newDef) {
         qemudFreeVMDef(vm->def);
@@ -1260,10 +1260,10 @@ static void qemudDispatchVMEvent(int fd,
         qemudDispatchVMFailure(driver, vm, fd);
 }
 
-int qemudMonitorCommand(struct qemud_driver *driver ATTRIBUTE_UNUSED,
-                        struct qemud_vm *vm,
-                        const char *cmd,
-                        char **reply) {
+static int qemudMonitorCommand(struct qemud_driver *driver ATTRIBUTE_UNUSED,
+                               struct qemud_vm *vm,
+                               const char *cmd,
+                               char **reply) {
     int size = 0;
     char *buf = NULL;
 
@@ -1326,7 +1326,7 @@ int qemudMonitorCommand(struct qemud_dri
 }
 
 
-static int qemudGetMemInfo(unsigned int *memory) {
+static int qemudGetMemInfo(unsigned long *memory) {
     FILE *meminfo = fopen("/proc/meminfo", "r");
     char line[1024];
 
@@ -1401,28 +1401,77 @@ static int qemudGetCPUInfo(unsigned int 
     return 0;
 }
 
-int qemudGetNodeInfo(unsigned int *memory,
-                     char *cpuModel, int cpuModelLength,
-                     unsigned int *cpus, unsigned int *mhz,
-                     unsigned int *nodes, unsigned int *sockets,
-                     unsigned int *cores, unsigned int *threads) {
+virDrvOpenStatus qemudOpen(virConnectPtr conn,
+                           const char *name,
+                           int flags ATTRIBUTE_UNUSED) {
+    uid_t uid = getuid();
+
+    if (qemu_driver == NULL)
+        return VIR_DRV_OPEN_DECLINED;
+
+    if (uid) {
+        if (strcmp(name, "qemu:///session"))
+            return VIR_DRV_OPEN_DECLINED;
+    } else {
+        if (strcmp(name, "qemu:///system"))
+            return VIR_DRV_OPEN_DECLINED;
+    }
+
+    conn->privateData = qemu_driver;
+
+    return VIR_DRV_OPEN_SUCCESS;
+}
+
+static int qemudClose(virConnectPtr conn) {
+    /*struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;*/
+
+    conn->privateData = NULL;
+
+    return 0;
+}
+
+static const char *qemudGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
+    return "qemu";
+}
+
+static int qemudGetMaxVCPUs(virConnectPtr conn ATTRIBUTE_UNUSED,
+                            const char *type) {
+    if (!type)
+        return 16;
+
+    if (!strcmp(type, "qemu"))
+        return 16;
+
+    /* XXX future KVM will support SMP. Need to probe
+       kernel to figure out KVM module version i guess */
+    if (!strcmp(type, "kvm"))
+        return 1;
+
+    if (!strcmp(type, "kqemu"))
+        return 1;
+    return -1;
+}
+
+int qemudGetNodeInfo(virConnectPtr conn ATTRIBUTE_UNUSED,
+                     virNodeInfoPtr node) {
     struct utsname info;
 
     if (uname(&info) < 0)
         return -1;
 
-    strncpy(cpuModel, info.machine, cpuModelLength-1);
-    cpuModel[cpuModelLength-1] = '\0';
-
-    if (qemudGetMemInfo(memory) < 0)
-        return -1;
-
-    if (qemudGetCPUInfo(cpus, mhz, nodes, sockets, cores, threads) < 0)
-        return -1;
-    return 0;
-}
-
-char *qemudGetCapabilities(struct qemud_driver *driver ATTRIBUTE_UNUSED) {
+    strncpy(node->model, info.machine, sizeof(node->model)-1);
+    node->model[sizeof(node->model)-1] = '\0';
+
+    if (qemudGetMemInfo(&(node->memory)) < 0)
+        return -1;
+
+    if (qemudGetCPUInfo(&(node->cpus), &(node->mhz), &(node->nodes),
+                        &(node->sockets), &(node->cores), &(node->threads)) < 0)
+        return -1;
+    return 0;
+}
+
+char *qemudGetCapabilities(virConnectPtr conn ATTRIBUTE_UNUSED) {
     struct utsname utsname;
     int i, j, r;
     int have_kqemu = 0;
@@ -1438,7 +1487,7 @@ char *qemudGetCapabilities(struct qemud_
     /* Construct the XML. */
     xml = virBufferNew (1024);
     if (!xml) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
         return NULL;
     }
 
@@ -1454,7 +1503,7 @@ char *qemudGetCapabilities(struct qemud_
     if (r == -1) {
     vir_buffer_failed:
         virBufferFree (xml);
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL);
         return NULL;
     }
 
@@ -1647,14 +1696,87 @@ struct qemud_vm *qemudFindVMByName(const
     return NULL;
 }
 
-int qemudGetVersion(struct qemud_driver *driver) {
+virDomainPtr qemudDomainLookupByID(virConnectPtr conn ATTRIBUTE_UNUSED,
+                                   int id) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByID(driver, id);
+    virDomainPtr dom;
+
+    if (!vm) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "no domain with matching id");
+        return NULL;
+    }
+
+    dom = calloc(1, sizeof(struct _virDomain));
+    if (!dom) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virDomainPtr");
+        return NULL;
+    }
+
+    dom->conn = conn;
+    dom->id = vm->id;
+    dom->name = vm->def->name;
+    memcpy(dom->uuid, vm->def->uuid, sizeof(dom->uuid));
+    return dom;
+}
+virDomainPtr qemudDomainLookupByUUID(virConnectPtr conn ATTRIBUTE_UNUSED,
+                                     const unsigned char *uuid) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByUUID(driver, uuid);
+    virDomainPtr dom;
+
+    if (!vm) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "no domain with matching uuid");
+        return NULL;
+    }
+
+    dom = calloc(1, sizeof(struct _virDomain));
+    if (!dom) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virDomainPtr");
+        return NULL;
+    }
+
+    dom->conn = conn;
+    dom->id = vm->id;
+    dom->name = vm->def->name;
+    memcpy(dom->uuid, vm->def->uuid, sizeof(dom->uuid));
+    return dom;
+}
+virDomainPtr qemudDomainLookupByName(virConnectPtr conn ATTRIBUTE_UNUSED,
+                                     const char *name) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByName(driver, name);
+    virDomainPtr dom;
+
+    if (!vm) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "no domain with matching name");
+        return NULL;
+    }
+
+    dom = calloc(1, sizeof(struct _virDomain));
+    if (!dom) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virDomainPtr");
+        return NULL;
+    }
+
+    dom->conn = conn;
+    dom->id = vm->id;
+    dom->name = vm->def->name;
+    memcpy(dom->uuid, vm->def->uuid, sizeof(dom->uuid));
+    return dom;
+}
+
+int qemudGetVersion(virConnectPtr conn, unsigned long *version) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
     if (qemudExtractVersion(driver) < 0)
         return -1;
 
-    return driver->qemuVersion;
-}
-
-int qemudListDomains(struct qemud_driver *driver, int *ids, int nids) {
+    *version = qemu_driver->qemuVersion;
+    return 0;
+}
+
+int qemudListDomains(virConnectPtr conn, int *ids, int nids) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
     struct qemud_vm *vm = driver->vms;
     int got = 0;
     while (vm && got < nids) {
@@ -1666,13 +1788,16 @@ int qemudListDomains(struct qemud_driver
     }
     return got;
 }
-int qemudNumDomains(struct qemud_driver *driver) {
+int qemudNumDomains(virConnectPtr conn) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
     return driver->nactivevms;
 }
-struct qemud_vm *qemudDomainCreate(struct qemud_driver *driver, const char *xml) {
-
+virDomainPtr qemudDomainCreate(virConnectPtr conn, const char *xml,
+                               unsigned int flags ATTRIBUTE_UNUSED) {
     struct qemud_vm_def *def;
     struct qemud_vm *vm;
+    virDomainPtr dom;
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
 
     if (!(def = qemudParseVMDef(driver, xml, NULL)))
         return NULL;
@@ -1687,182 +1812,221 @@ struct qemud_vm *qemudDomainCreate(struc
         return NULL;
     }
 
-    return vm;
-}
-
-
-int qemudDomainSuspend(struct qemud_driver *driver, int id) {
+    dom = calloc(1, sizeof(struct _virDomain));
+    if (!dom) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virDomainPtr");
+        return NULL;
+    }
+
+    dom->conn = conn;
+    dom->id = vm->id;
+    dom->name = vm->def->name;
+    memcpy(dom->uuid, vm->def->uuid, sizeof(dom->uuid));
+
+    return dom;
+}
+
+
+int qemudDomainSuspend(virDomainPtr dom) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
     char *info;
-    struct qemud_vm *vm = qemudFindVMByID(driver, id);
+    struct qemud_vm *vm = qemudFindVMByID(driver, dom->id);
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", id);
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", dom->id);
         return -1;
     }
     if (!qemudIsActiveVM(vm)) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, "domain is not running");
-        return -1;
-    }
-    if (vm->state == QEMUD_STATE_PAUSED)
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "domain is not running");
+        return -1;
+    }
+    if (vm->state == VIR_DOMAIN_PAUSED)
         return 0;
 
     if (qemudMonitorCommand(driver, vm, "stop\n", &info) < 0) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, "suspend operation failed");
-        return -1;
-    }
-    vm->state = QEMUD_STATE_PAUSED;
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "suspend operation failed");
+        return -1;
+    }
+    vm->state = VIR_DOMAIN_PAUSED;
     qemudDebug("Reply %s", info);
     free(info);
     return 0;
 }
 
 
-int qemudDomainResume(struct qemud_driver *driver, int id) {
+int qemudDomainResume(virDomainPtr dom) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
     char *info;
-    struct qemud_vm *vm = qemudFindVMByID(driver, id);
+    struct qemud_vm *vm = qemudFindVMByID(driver, dom->id);
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", id);
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", dom->id);
         return -1;
     }
     if (!qemudIsActiveVM(vm)) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, "domain is not running");
-        return -1;
-    }
-    if (vm->state == QEMUD_STATE_RUNNING)
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "domain is not running");
+        return -1;
+    }
+    if (vm->state == VIR_DOMAIN_RUNNING)
         return 0;
     if (qemudMonitorCommand(driver, vm, "cont\n", &info) < 0) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, "resume operation failed");
-        return -1;
-    }
-    vm->state = QEMUD_STATE_RUNNING;
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "resume operation failed");
+        return -1;
+    }
+    vm->state = VIR_DOMAIN_RUNNING;
     qemudDebug("Reply %s", info);
     free(info);
     return 0;
 }
 
 
-int qemudDomainDestroy(struct qemud_driver *driver, int id) {
-    struct qemud_vm *vm = qemudFindVMByID(driver, id);
+int qemudDomainDestroy(virDomainPtr dom) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByID(driver, dom->id);
+    int ret;
 
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN,
-                         "no domain with matching id %d", id);
-        return -1;
-    }
-
-    return qemudShutdownVMDaemon(driver, vm);
-}
-
-
-int qemudDomainGetInfo(struct qemud_driver *driver, const unsigned char *uuid,
-                       int *runstate,
-                       unsigned long long *cputime,
-                       unsigned long *maxmem,
-                       unsigned long *memory,
-                       unsigned int *nrVirtCpu) {
-    struct qemud_vm *vm = qemudFindVMByUUID(driver, uuid);
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
+                         "no domain with matching id %d", dom->id);
+        return -1;
+    }
+
+    ret = qemudShutdownVMDaemon(driver, vm);
+    free(dom);
+    return ret;
+}
+
+
+static char *qemudDomainGetOSType(virDomainPtr dom) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByUUID(driver, dom->uuid);
+    char *type;
+
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
-        return -1;
-    }
-
-    *runstate = vm->state;
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
+                         "no domain with matching uuid");
+        return NULL;
+    }
+
+    if (!(type = strdup(vm->def->os.type))) {
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_MEMORY, "ostype");
+        return NULL;
+    }
+    return type;
+}
+
+int qemudDomainGetInfo(virDomainPtr dom,
+                       virDomainInfoPtr info) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByUUID(driver, dom->uuid);
+    if (!vm) {
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
+        return -1;
+    }
+
+    info->state = vm->state;
 
     if (!qemudIsActiveVM(vm)) {
-        *cputime = 0;
+        info->cpuTime = 0;
     } else {
-        if (qemudGetProcessInfo(cputime, vm->pid) < 0) {
-            qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, "cannot read cputime for domain");
+        if (qemudGetProcessInfo(&(info->cpuTime), vm->pid) < 0) {
+            qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "cannot read cputime for domain");
             return -1;
         }
     }
 
-    *maxmem = vm->def->maxmem;
-    *memory = vm->def->memory;
-    *nrVirtCpu = vm->def->vcpus;
-    return 0;
-}
-
-
-int qemudDomainSave(struct qemud_driver *driver, int id,
+    info->maxMem = vm->def->maxmem;
+    info->memory = vm->def->memory;
+    info->nrVirtCpu = vm->def->vcpus;
+    return 0;
+}
+
+
+int qemudDomainSave(virDomainPtr dom,
                     const char *path ATTRIBUTE_UNUSED) {
-    struct qemud_vm *vm = qemudFindVMByID(driver, id);
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByID(driver, dom->id);
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", id);
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching id %d", dom->id);
         return -1;
     }
     if (!qemudIsActiveVM(vm)) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, "domain is not running");
-        return -1;
-    }
-    qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, "save is not supported");
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "domain is not running");
+        return -1;
+    }
+    qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "save is not supported");
     return -1;
 }
 
 
-int qemudDomainRestore(struct qemud_driver *driver ATTRIBUTE_UNUSED,
+int qemudDomainRestore(virConnectPtr conn,
                        const char *path ATTRIBUTE_UNUSED) {
-    qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED, "restore is not supported");
+    /*struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;*/
+    qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED, "restore is not supported");
     return -1;
 }
 
 
-int qemudDomainDumpXML(struct qemud_driver *driver, const unsigned char *uuid, char *xml, int xmllen) {
-    struct qemud_vm *vm = qemudFindVMByUUID(driver, uuid);
-    char *vmxml;
+char *qemudDomainDumpXML(virDomainPtr dom,
+                         int flags ATTRIBUTE_UNUSED) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByUUID(driver, dom->uuid);
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
-        return -1;
-    }
-
-    vmxml = qemudGenerateXML(driver, vm, vm->def, 1);
-    if (!vmxml)
-        return -1;
-
-    strncpy(xml, vmxml, xmllen);
-    xml[xmllen-1] = '\0';
-
-    free(vmxml);
-
-    return 0;
-}
-
-
-int qemudListDefinedDomains(struct qemud_driver *driver, char *const*names, int nnames) {
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
+        return NULL;
+    }
+
+    return qemudGenerateXML(driver, vm, vm->def, 1);
+}
+
+
+int qemudListDefinedDomains(virConnectPtr conn,
+                            char **const names, int nnames) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
     struct qemud_vm *vm = driver->vms;
-    int got = 0;
+    int got = 0, i;
     while (vm && got < nnames) {
         if (!qemudIsActiveVM(vm)) {
-            strncpy(names[got], vm->def->name, QEMUD_MAX_NAME_LEN-1);
-            names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+            if (!(names[got] = strdup(vm->def->name))) {
+                qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "names");
+                goto cleanup;
+            }
             got++;
         }
         vm = vm->next;
     }
     return got;
-}
-
-
-int qemudNumDefinedDomains(struct qemud_driver *driver) {
+
+ cleanup:
+    for (i = 0 ; i < got ; i++)
+        free(names[i]);
+    return -1;
+}
+
+
+int qemudNumDefinedDomains(virConnectPtr conn) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
     return driver->ninactivevms;
 }
 
 
-struct qemud_vm *qemudDomainStart(struct qemud_driver *driver, const unsigned char *uuid) {
-    struct qemud_vm *vm = qemudFindVMByUUID(driver, uuid);
+int qemudDomainStart(virDomainPtr dom) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByUUID(driver, dom->uuid);
 
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN,
                          "no domain with matching uuid");
-        return NULL;
-    }
-
-    return qemudStartVMDaemon(driver, vm) < 0 ? NULL : vm;
-}
-
-
-struct qemud_vm *qemudDomainDefine(struct qemud_driver *driver, const char *xml) {
+        return -1;
+    }
+
+    return qemudStartVMDaemon(driver, vm);
+}
+
+
+virDomainPtr qemudDomainDefine(virConnectPtr conn, const char *xml) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
     struct qemud_vm_def *def;
     struct qemud_vm *vm;
+    virDomainPtr dom;
 
     if (!(def = qemudParseVMDef(driver, xml, NULL)))
         return NULL;
@@ -1877,19 +2041,30 @@ struct qemud_vm *qemudDomainDefine(struc
         return NULL;
     }
 
-    return vm;
-}
-
-int qemudDomainUndefine(struct qemud_driver *driver, const unsigned char *uuid) {
-    struct qemud_vm *vm = qemudFindVMByUUID(driver, uuid);
+    dom = calloc(1, sizeof(struct _virDomain));
+    if (!dom) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virDomainPtr");
+        return NULL;
+    }
+
+    dom->conn = conn;
+    dom->id = vm->id;
+    dom->name = vm->def->name;
+    memcpy(dom->uuid, vm->def->uuid, sizeof(dom->uuid));
+    return dom;
+}
+
+int qemudDomainUndefine(virDomainPtr dom) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByUUID(driver, dom->uuid);
 
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
         return -1;
     }
 
     if (qemudIsActiveVM(vm)) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "cannot delete active domain");
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR, "cannot delete active domain");
         return -1;
     }
 
@@ -1908,13 +2083,13 @@ int qemudDomainUndefine(struct qemud_dri
     return 0;
 }
 
-int qemudDomainGetAutostart(struct qemud_driver *driver,
-                             const unsigned char *uuid,
-                             int *autostart) {
-    struct qemud_vm *vm = qemudFindVMByUUID(driver, uuid);
+int qemudDomainGetAutostart(virDomainPtr dom,
+                            int *autostart) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByUUID(driver, dom->uuid);
 
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
         return -1;
     }
 
@@ -1923,13 +2098,13 @@ int qemudDomainGetAutostart(struct qemud
     return 0;
 }
 
-int qemudDomainSetAutostart(struct qemud_driver *driver,
-                             const unsigned char *uuid,
-                             int autostart) {
-    struct qemud_vm *vm = qemudFindVMByUUID(driver, uuid);
+int qemudDomainSetAutostart(virDomainPtr dom,
+                            int autostart) {
+    struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
+    struct qemud_vm *vm = qemudFindVMByUUID(driver, dom->uuid);
 
     if (!vm) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_DOMAIN, "no domain with matching uuid");
         return -1;
     }
 
@@ -1942,21 +2117,21 @@ int qemudDomainSetAutostart(struct qemud
         int err;
 
         if ((err = qemudEnsureDir(driver->autostartDir))) {
-            qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+            qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                              "cannot create autostart directory %s: %s",
                              driver->autostartDir, strerror(err));
             return -1;
         }
 
         if (symlink(vm->configFile, vm->autostartLink) < 0) {
-            qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+            qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                              "Failed to create symlink '%s' to '%s': %s",
                              vm->autostartLink, vm->configFile, strerror(errno));
             return -1;
         }
     } else {
         if (unlink(vm->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
-            qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+            qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
                              "Failed to delete symlink '%s': %s",
                              vm->autostartLink, strerror(errno));
             return -1;
@@ -1994,45 +2169,125 @@ struct qemud_network *qemudFindNetworkBy
     return NULL;
 }
 
-int qemudNumNetworks(struct qemud_driver *driver) {
+virNetworkPtr qemudNetworkLookupByUUID(virConnectPtr conn ATTRIBUTE_UNUSED,
+                                     const unsigned char *uuid) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
+    struct qemud_network *network = qemudFindNetworkByUUID(driver, uuid);
+    virNetworkPtr net;
+
+    if (!network) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "no network with matching uuid");
+        return NULL;
+    }
+
+    net = calloc(1, sizeof(struct _virNetwork));
+    if (!net) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virNetworkPtr");
+        return NULL;
+    }
+
+    net->conn = conn;
+    net->name = network->def->name;
+    memcpy(net->uuid, network->def->uuid, sizeof(net->uuid));
+    return net;
+}
+virNetworkPtr qemudNetworkLookupByName(virConnectPtr conn ATTRIBUTE_UNUSED,
+                                     const char *name) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
+    struct qemud_network *network = qemudFindNetworkByName(driver, name);
+    virNetworkPtr net;
+
+    if (!network) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "no network with matching name");
+        return NULL;
+    }
+
+    net = calloc(1, sizeof(struct _virNetwork));
+    if (!net) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virNetworkPtr");
+        return NULL;
+    }
+
+    net->conn = conn;
+    net->name = network->def->name;
+    memcpy(net->uuid, network->def->uuid, sizeof(net->uuid));
+    return net;
+}
+
+static virDrvOpenStatus qemudOpenNetwork(virConnectPtr conn,
+                                         const char *name ATTRIBUTE_UNUSED,
+                                         int flags ATTRIBUTE_UNUSED) {
+    if (!qemu_driver)
+        return VIR_DRV_OPEN_DECLINED;
+
+    conn->networkPrivateData = qemu_driver;
+    return VIR_DRV_OPEN_SUCCESS;
+}
+
+static int qemudCloseNetwork(virConnectPtr conn) {
+    conn->networkPrivateData = NULL;
+    return 0;
+}
+
+int qemudNumNetworks(virConnectPtr conn) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->networkPrivateData;
     return driver->nactivenetworks;
 }
 
-int qemudListNetworks(struct qemud_driver *driver, char *const*names, int nnames) {
+int qemudListNetworks(virConnectPtr conn, char **const names, int nnames) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->networkPrivateData;
     struct qemud_network *network = driver->networks;
-    int got = 0;
+    int got = 0, i;
     while (network && got < nnames) {
         if (qemudIsActiveNetwork(network)) {
-            strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
-            names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+            if (!(names[got] = strdup(network->def->name))) {
+                qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "names");
+                goto cleanup;
+            }
             got++;
         }
         network = network->next;
     }
     return got;
-}
-
-int qemudNumDefinedNetworks(struct qemud_driver *driver) {
+
+ cleanup:
+    for (i = 0 ; i < got ; i++)
+        free(names[i]);
+    return -1;
+}
+
+int qemudNumDefinedNetworks(virConnectPtr conn) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->networkPrivateData;
     return driver->ninactivenetworks;
 }
 
-int qemudListDefinedNetworks(struct qemud_driver *driver, char *const*names, int nnames) {
+int qemudListDefinedNetworks(virConnectPtr conn, char **const names, int nnames) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->networkPrivateData;
     struct qemud_network *network = driver->networks;
-    int got = 0;
+    int got = 0, i;
     while (network && got < nnames) {
         if (!qemudIsActiveNetwork(network)) {
-            strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
-            names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+            if (!(names[got] = strdup(network->def->name))) {
+                qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "names");
+                goto cleanup;
+            }
             got++;
         }
         network = network->next;
     }
     return got;
-}
-
-struct qemud_network *qemudNetworkCreate(struct qemud_driver *driver, const char *xml) {
+
+ cleanup:
+    for (i = 0 ; i < got ; i++)
+        free(names[i]);
+    return -1;
+}
+
+virNetworkPtr qemudNetworkCreate(virConnectPtr conn, const char *xml) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->networkPrivateData;
     struct qemud_network_def *def;
     struct qemud_network *network;
+    virNetworkPtr net;
 
     if (!(def = qemudParseNetworkDef(driver, xml, NULL)))
         return NULL;
@@ -2047,12 +2302,24 @@ struct qemud_network *qemudNetworkCreate
         return NULL;
     }
 
-    return network;
-}
-
-struct qemud_network *qemudNetworkDefine(struct qemud_driver *driver, const char *xml) {
+    net = calloc(1, sizeof(struct _virNetwork));
+    if (!net) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virNetworkPtr");
+        return NULL;
+    }
+
+    net->conn = conn;
+    net->name = network->def->name;
+    memcpy(net->uuid, network->def->uuid, sizeof(net->uuid));
+
+    return net;
+}
+
+virNetworkPtr qemudNetworkDefine(virConnectPtr conn, const char *xml) {
+    struct qemud_driver *driver = (struct qemud_driver *)conn->networkPrivateData;
     struct qemud_network_def *def;
     struct qemud_network *network;
+    virNetworkPtr net;
 
     if (!(def = qemudParseNetworkDef(driver, xml, NULL)))
         return NULL;
@@ -2067,14 +2334,25 @@ struct qemud_network *qemudNetworkDefine
         return NULL;
     }
 
-    return network;
-}
-
-int qemudNetworkUndefine(struct qemud_driver *driver, const unsigned char *uuid) {
-    struct qemud_network *network = qemudFindNetworkByUUID(driver, uuid);
+    net = calloc(1, sizeof(struct _virNetwork));
+    if (!net) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "virNetworkPtr");
+        return NULL;
+    }
+
+    net->conn = conn;
+    net->name = network->def->name;
+    memcpy(net->uuid, network->def->uuid, sizeof(net->uuid));
+
+    return net;
+}
+
+int qemudNetworkUndefine(virNetworkPtr net) {
+    struct qemud_driver *driver = (struct qemud_driver *)net->conn->networkPrivateData;
+    struct qemud_network *network = qemudFindNetworkByUUID(driver, net->uuid);
 
     if (!network) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_DOMAIN, "no network with matching uuid");
+        qemudReportError(net->conn, NULL, net, VIR_ERR_INVALID_DOMAIN, "no network with matching uuid");
         return -1;
     }
 
@@ -2093,23 +2371,25 @@ int qemudNetworkUndefine(struct qemud_dr
     return 0;
 }
 
-struct qemud_network *qemudNetworkStart(struct qemud_driver *driver, const unsigned char *uuid) {
-    struct qemud_network *network = qemudFindNetworkByUUID(driver, uuid);
+int qemudNetworkStart(virNetworkPtr net) {
+    struct qemud_driver *driver = (struct qemud_driver *)net->conn->networkPrivateData;
+    struct qemud_network *network = qemudFindNetworkByUUID(driver, net->uuid);
 
     if (!network) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_NETWORK,
+        qemudReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
                          "no network with matching uuid");
-        return NULL;
-    }
-
-    return qemudStartNetworkDaemon(driver, network) < 0 ? NULL : network;
-}
-
-int qemudNetworkDestroy(struct qemud_driver *driver, const unsigned char *uuid) {
-    struct qemud_network *network = qemudFindNetworkByUUID(driver, uuid);
+        return -1;
+    }
+
+    return qemudStartNetworkDaemon(driver, network);
+}
+
+int qemudNetworkDestroy(virNetworkPtr net) {
+    struct qemud_driver *driver = (struct qemud_driver *)net->conn->networkPrivateData;
+    struct qemud_network *network = qemudFindNetworkByUUID(driver, net->uuid);
 
     if (!network) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_NETWORK,
+        qemudReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
                          "no network with matching uuid");
         return -1;
     }
@@ -2117,47 +2397,43 @@ int qemudNetworkDestroy(struct qemud_dri
     return qemudShutdownNetworkDaemon(driver, network);
 }
 
-int qemudNetworkDumpXML(struct qemud_driver *driver, const unsigned char *uuid, char *xml, int xmllen) {
-    struct qemud_network *network = qemudFindNetworkByUUID(driver, uuid);
-    char *networkxml;
+char *qemudNetworkDumpXML(virNetworkPtr net, int flags ATTRIBUTE_UNUSED) {
+    struct qemud_driver *driver = (struct qemud_driver *)net->conn->networkPrivateData;
+    struct qemud_network *network = qemudFindNetworkByUUID(driver, net->uuid);
+
     if (!network) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_NETWORK, "no network with matching uuid");
-        return -1;
-    }
-
-    networkxml = qemudGenerateNetworkXML(driver, network, network->def);
-    if (!networkxml)
-        return -1;
-
-    strncpy(xml, networkxml, xmllen);
-    xml[xmllen-1] = '\0';
-
-    free(networkxml);
-
-    return 0;
-}
-
-int qemudNetworkGetBridgeName(struct qemud_driver *driver, const unsigned char *uuid, char *ifname, int ifnamelen) {
-    struct qemud_network *network = qemudFindNetworkByUUID(driver, uuid);
-
+        qemudReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK,
+                         "no network with matching uuid");
+        return NULL;
+    }
+
+    return qemudGenerateNetworkXML(driver, network, network->def);
+}
+
+char *qemudNetworkGetBridgeName(virNetworkPtr net) {
+    struct qemud_driver *driver = (struct qemud_driver *)net->conn->networkPrivateData;
+    struct qemud_network *network = qemudFindNetworkByUUID(driver, net->uuid);
+    char *bridge;
     if (!network) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_NETWORK, "no network with matching id");
-        return -1;
-    }
-
-    strncpy(ifname, network->bridge, ifnamelen);
-    ifname[ifnamelen-1] = '\0';
-
-    return 0;
-}
-
-int qemudNetworkGetAutostart(struct qemud_driver *driver,
-                             const unsigned char *uuid,
+        qemudReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK, "no network with matching id");
+        return NULL;
+    }
+
+    bridge = strdup(network->bridge);
+    if (!bridge) {
+        qemudReportError(net->conn, NULL, net, VIR_ERR_NO_MEMORY, "bridge");
+        return NULL;
+    }
+    return bridge;
+}
+
+int qemudNetworkGetAutostart(virNetworkPtr net,
                              int *autostart) {
-    struct qemud_network *network = qemudFindNetworkByUUID(driver, uuid);
+    struct qemud_driver *driver = (struct qemud_driver *)net->conn->networkPrivateData;
+    struct qemud_network *network = qemudFindNetworkByUUID(driver, net->uuid);
 
     if (!network) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_NETWORK, "no network with matching uuid");
+        qemudReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK, "no network with matching uuid");
         return -1;
     }
 
@@ -2166,13 +2442,13 @@ int qemudNetworkGetAutostart(struct qemu
     return 0;
 }
 
-int qemudNetworkSetAutostart(struct qemud_driver *driver,
-                             const unsigned char *uuid,
+int qemudNetworkSetAutostart(virNetworkPtr net,
                              int autostart) {
-    struct qemud_network *network = qemudFindNetworkByUUID(driver, uuid);
+    struct qemud_driver *driver = (struct qemud_driver *)net->conn->networkPrivateData;
+    struct qemud_network *network = qemudFindNetworkByUUID(driver, net->uuid);
 
     if (!network) {
-        qemudReportError(NULL, NULL, NULL, VIR_ERR_INVALID_NETWORK, "no network with matching uuid");
+        qemudReportError(net->conn, NULL, net, VIR_ERR_INVALID_NETWORK, "no network with matching uuid");
         return -1;
     }
 
@@ -2185,21 +2461,21 @@ int qemudNetworkSetAutostart(struct qemu
         int err;
 
         if ((err = qemudEnsureDir(driver->networkAutostartDir))) {
-            qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+            qemudReportError(net->conn, NULL, net, VIR_ERR_INTERNAL_ERROR,
                              "cannot create autostart directory %s: %s",
                              driver->networkAutostartDir, strerror(err));
             return -1;
         }
 
         if (symlink(network->configFile, network->autostartLink) < 0) {
-            qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+            qemudReportError(net->conn, NULL, net, VIR_ERR_INTERNAL_ERROR,
                              "Failed to create symlink '%s' to '%s': %s",
                              network->autostartLink, network->configFile, strerror(errno));
             return -1;
         }
     } else {
         if (unlink(network->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
-            qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+            qemudReportError(net->conn, NULL, net, VIR_ERR_INTERNAL_ERROR,
                              "Failed to delete symlink '%s': %s",
                              network->autostartLink, strerror(errno));
             return -1;
@@ -2210,6 +2486,76 @@ int qemudNetworkSetAutostart(struct qemu
 
     return 0;
 }
+
+static virDriver qemuDriver = {
+    VIR_DRV_QEMU,
+    "QEMU",
+    LIBVIR_VERSION_NUMBER,
+    qemudOpen, /* open */
+    qemudClose, /* close */
+    qemudGetType, /* type */
+    qemudGetVersion, /* version */
+    qemudGetMaxVCPUs, /* getMaxVcpus */
+    qemudGetNodeInfo, /* nodeGetInfo */
+    qemudGetCapabilities, /* getCapabilities */
+    qemudListDomains, /* listDomains */
+    qemudNumDomains, /* numOfDomains */
+    qemudDomainCreate, /* domainCreateLinux */
+    qemudDomainLookupByID, /* domainLookupByID */
+    qemudDomainLookupByUUID, /* domainLookupByUUID */
+    qemudDomainLookupByName, /* domainLookupByName */
+    qemudDomainSuspend, /* domainSuspend */
+    qemudDomainResume, /* domainResume */
+    qemudDomainDestroy, /* domainShutdown */
+    NULL, /* domainReboot */
+    qemudDomainDestroy, /* domainDestroy */
+    qemudDomainGetOSType, /* domainGetOSType */
+    NULL, /* domainGetMaxMemory */
+    NULL, /* domainSetMaxMemory */
+    NULL, /* domainSetMemory */
+    qemudDomainGetInfo, /* domainGetInfo */
+    qemudDomainSave, /* domainSave */
+    qemudDomainRestore, /* domainRestore */
+    NULL, /* domainCoreDump */
+    NULL, /* domainSetVcpus */
+    NULL, /* domainPinVcpu */
+    NULL, /* domainGetVcpus */
+    NULL, /* domainGetMaxVcpus */
+    qemudDomainDumpXML, /* domainDumpXML */
+    qemudListDefinedDomains, /* listDomains */
+    qemudNumDefinedDomains, /* numOfDomains */
+    qemudDomainStart, /* domainCreate */
+    qemudDomainDefine, /* domainDefineXML */
+    qemudDomainUndefine, /* domainUndefine */
+    NULL, /* domainAttachDevice */
+    NULL, /* domainDetachDevice */
+    qemudDomainGetAutostart, /* domainGetAutostart */
+    qemudDomainSetAutostart, /* domainSetAutostart */
+    NULL, /* domainGetSchedulerType */
+    NULL, /* domainGetSchedulerParameters */
+    NULL, /* domainSetSchedulerParameters */
+};
+
+static virNetworkDriver qemuNetworkDriver = {
+    qemudOpenNetwork, /* open */
+    qemudCloseNetwork, /* close */
+    qemudNumNetworks, /* numOfNetworks */
+    qemudListNetworks, /* listNetworks */
+    qemudNumDefinedNetworks, /* numOfDefinedNetworks */
+    qemudListDefinedNetworks, /* listDefinedNetworks */
+    qemudNetworkLookupByUUID, /* networkLookupByUUID */
+    qemudNetworkLookupByName, /* networkLookupByName */
+    qemudNetworkCreate, /* networkCreateXML */
+    qemudNetworkDefine, /* networkDefineXML */
+    qemudNetworkUndefine, /* networkUndefine */
+    qemudNetworkStart, /* networkCreate */
+    qemudNetworkDestroy, /* networkDestroy */
+    qemudNetworkDumpXML, /* networkDumpXML */
+    qemudNetworkGetBridgeName, /* networkGetBridgeName */
+    qemudNetworkGetAutostart, /* networkGetAutostart */
+    qemudNetworkSetAutostart, /* networkSetAutostart */
+};
+
 
 /*
  * Local variables:
diff -r 685e00bb55b5 qemud/driver.h
--- a/qemud/driver.h	Thu Jun 21 21:01:55 2007 -0400
+++ b/qemud/driver.h	Thu Jun 21 21:13:27 2007 -0400
@@ -26,6 +26,7 @@
 #define QEMUD_DRIVER_H
 
 #include "internal.h"
+#include "../src/internal.h"
 #include "conf.h"
 
 int qemudStartVMDaemon(struct qemud_driver *driver,
@@ -44,96 +45,82 @@ void qemudReload(void);
 void qemudReload(void);
 void qemudShutdown(void);
 
-int qemudGetNodeInfo(unsigned int *memory,
-                     char *cpuModel, int cpuModelLength,
-                     unsigned int *cpus, unsigned int *mhz,
-                     unsigned int *nodes, unsigned int *sockets,
-                     unsigned int *cores, unsigned int *threads);
 
-char *qemudGetCapabilities(struct qemud_driver *driver);
-int qemudMonitorCommand(struct qemud_driver *driver,
-                        struct qemud_vm *vm,
-                        const char *cmd,
-                        char **reply);
+virDrvOpenStatus qemudOpen(virConnectPtr conn,
+                           const char *name,
+                           int flags);
 
-int qemudGetVersion(struct qemud_driver *driver);
-int qemudListDomains(struct qemud_driver *driver,
+int qemudGetNodeInfo(virConnectPtr conn,
+                     virNodeInfoPtr info);
+
+char *qemudGetCapabilities(virConnectPtr conn);
+
+virDomainPtr qemudDomainLookupByID(virConnectPtr conn,
+                                   int id);
+virDomainPtr qemudDomainLookupByUUID(virConnectPtr conn,
+                                     const unsigned char *uuid);
+virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
+                                     const char *name);
+
+int qemudGetVersion(virConnectPtr conn, unsigned long *version);
+int qemudListDomains(virConnectPtr conn,
                      int *ids,
                      int nids);
-int qemudNumDomains(struct qemud_driver *driver);
-struct qemud_vm *qemudDomainCreate(struct qemud_driver *driver,
-                                   const char *xml);
-int qemudDomainSuspend(struct qemud_driver *driver,
-                       int id);
-int qemudDomainResume(struct qemud_driver *driver,
-                      int id);
-int qemudDomainDestroy(struct qemud_driver *driver,
-                       int id);
-int qemudDomainGetInfo(struct qemud_driver *driver,
-                       const unsigned char *uuid,
-                       int *runstate,
-                       unsigned long long *cputime,
-                       unsigned long *maxmem,
-                       unsigned long *memory,
-                       unsigned int *nrVirtCpu);
-int qemudDomainSave(struct qemud_driver *driver,
-                    int id,
+int qemudNumDomains(virConnectPtr conn);
+virDomainPtr qemudDomainCreate(virConnectPtr conn,
+                               const char *xml,
+                               unsigned int flags);
+int qemudDomainSuspend(virDomainPtr dom);
+int qemudDomainResume(virDomainPtr dom);
+int qemudDomainDestroy(virDomainPtr dom);
+int qemudDomainGetInfo(virDomainPtr dom,
+                       virDomainInfoPtr info);
+int qemudDomainSave(virDomainPtr dom,
                     const char *path);
-int qemudDomainRestore(struct qemud_driver *driver,
+int qemudDomainRestore(virConnectPtr conn,
                        const char *path);
-int qemudDomainDumpXML(struct qemud_driver *driver,
-                       const unsigned char *uuid,
-                       char *xml,
-                       int xmllen);
-int qemudListDefinedDomains(struct qemud_driver *driver,
-                            char *const*names,
+char *qemudDomainDumpXML(virDomainPtr dom,
+                         int flags);
+int qemudListDefinedDomains(virConnectPtr conn,
+                            char **const names,
                             int nnames);
-int qemudNumDefinedDomains(struct qemud_driver *driver);
-struct qemud_vm *qemudDomainStart(struct qemud_driver *driver,
-                                  const unsigned char *uuid);
-struct qemud_vm *qemudDomainDefine(struct qemud_driver *driver,
-                                   const char *xml);
-int qemudDomainUndefine(struct qemud_driver *driver,
-                        const unsigned char *uuid);
-int qemudDomainGetAutostart(struct qemud_driver *driver,
-                            const unsigned char *uuid,
+int qemudNumDefinedDomains(virConnectPtr conn);
+int qemudDomainStart(virDomainPtr dom);
+virDomainPtr qemudDomainDefine(virConnectPtr conn,
+                               const char *xml);
+int qemudDomainUndefine(virDomainPtr dom);
+int qemudDomainGetAutostart(virDomainPtr dom,
                             int *autostart);
-int qemudDomainSetAutostart(struct qemud_driver *driver,
-                            const unsigned char *uuid,
-                            int autostart);
+int qemudDomainSetAutostart(virDomainPtr dom,
+                              int autostart);
 
 
-int qemudNumNetworks(struct qemud_driver *driver);
-int qemudListNetworks(struct qemud_driver *driver,
-                      char *const*names,
+virNetworkPtr qemudNetworkLookupByUUID(virConnectPtr conn,
+                                       const unsigned char *uuid);
+virNetworkPtr qemudNetworkLookupByName(virConnectPtr conn,
+                                       const char *name);
+
+int qemudNumNetworks(virConnectPtr conn);
+int qemudListNetworks(virConnectPtr conn,
+                      char **const names,
                       int nnames);
-int qemudNumDefinedNetworks(struct qemud_driver *driver);
-int qemudListDefinedNetworks(struct qemud_driver *driver,
-                             char *const*names,
+int qemudNumDefinedNetworks(virConnectPtr conn);
+int qemudListDefinedNetworks(virConnectPtr conn,
+                             char **const names,
                              int nnames);
-struct qemud_network *qemudNetworkCreate(struct qemud_driver *driver,
-                                         const char *xml);
-struct qemud_network *qemudNetworkDefine(struct qemud_driver *driver,
-                                         const char *xml);
-struct qemud_network *qemudNetworkStart(struct qemud_driver *driver,
-                                        const unsigned char *uuid);
-int qemudNetworkUndefine(struct qemud_driver *driver,
-                         const unsigned char *uuid);
-int qemudNetworkDestroy(struct qemud_driver *driver,
-                        const unsigned char *uuid);
-int qemudNetworkDumpXML(struct qemud_driver *driver,
-                        const unsigned char *uuid,
-                        char *xml,
-                        int xmllen);
-int qemudNetworkGetBridgeName(struct qemud_driver *driver,
-                              const unsigned char *uuid,
-                              char *ifname,
-                              int ifnamelen);
-int qemudNetworkGetAutostart(struct qemud_driver *driver,
-                             const unsigned char *uuid,
+virNetworkPtr qemudNetworkCreate(virConnectPtr conn,
+                                 const char *xml);
+virNetworkPtr qemudNetworkDefine(virConnectPtr conn,
+                                 const char *xml);
+int qemudNetworkStart(virNetworkPtr net);
+int qemudNetworkUndefine(virNetworkPtr net);
+int qemudNetworkDestroy(virNetworkPtr net);
+char *qemudNetworkDumpXML(virNetworkPtr net,
+                          int flags);
+char *qemudNetworkGetBridgeName(virNetworkPtr net);
+int qemudNetworkGetAutostart(virNetworkPtr net,
                              int *autostart);
-int qemudNetworkSetAutostart(struct qemud_driver *driver,
-                             const unsigned char *uuid,
+int qemudNetworkSetAutostart(virNetworkPtr net,
                              int autostart);
 
 #endif

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]