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

[libvirt] [PATCH 12/13] Extract monitor parts from the qemu interaction for disk hotplug



From: Wolfgang Mauerer <wolfgang mauerer siemens com>

This separates the communication with qemu a bit from the more
libvirtish actions.

Signed-off-by: Wolfgang Mauerer <wolfgang mauerer siemens com>
Signed-off-by: Jan Kiszka <jan kiszka siemens com>
---
 src/qemu/qemu_driver.c       |   82 ++--------------------------------------
 src/qemu/qemu_monitor_text.c |   85 ++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor_text.h |    5 ++
 3 files changed, 95 insertions(+), 77 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 289c3c6..df22035 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4445,15 +4445,8 @@ static int qemudDomainAttachDiskDevice(virConnectPtr conn,
 				       virDomainDeviceDefPtr dev)
 {
     int ret, i;
-    char *cmd, *reply;
-    char *safe_path;
-    const char* type = NULL;
-    int tryOldSyntax = 0;
-    int bus_id, unit_id;
-    int domain, bus, slot;
     virDomainControllerDefPtr conPtr;
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
-    char* bus_unit_string;
+    const char* type = NULL;
 
     /* Both controller and disk can specify a type like SCSI. While
        a virtual disk as such is not bound to a specific bus type,
@@ -4485,13 +4478,6 @@ static int qemudDomainAttachDiskDevice(virConnectPtr conn,
         return -1;
     }
 
-try_command:
-    safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
-    if (!safe_path) {
-        virReportOOMError(conn);
-        return -1;
-    }
-
     conPtr = qemudGetDiskController(vm, dev, &ret);
     if (!conPtr) {
         switch (ret) {
@@ -4510,70 +4496,12 @@ try_command:
 
     /* At this point, we have a valid controller, regardless if an explicit
        controller has been specified or not. */
-    domain = conPtr->address->data.pci.domain;
-    bus    = conPtr->address->data.pci.bus;
-    slot   = conPtr->address->data.pci.slot;
-
-    if (dev->data.disk->bus_id != -1) {
-       virBufferVSprintf(&buf, ",bus=%d", dev->data.disk->bus_id);
-    }
-
-    if (dev->data.disk->unit_id != -1) {
-        virBufferVSprintf(&buf, ",unit=%d", dev->data.disk->unit_id);
-    }
-
-    bus_unit_string = virBufferContentAndReset(&buf);
-
-    VIR_DEBUG ("%s: drive_add %.2x:%.2x:%.2x file=%s,if=%s%s", vm->def->name,
-               domain, bus, slot, safe_path, type,
-               bus_unit_string ? bus_unit_string : "");
-    ret = virAsprintf(&cmd, "drive_add %s%.2x:%.2x:%.2x file=%s,if=%s%s",
-                      (tryOldSyntax ? "" : "pci_addr="), domain, bus,
-                      slot, safe_path, type,
-                      bus_unit_string ? bus_unit_string : "");
-
-    VIR_FREE(safe_path);
-    if (ret == -1) {
-        virReportOOMError(conn);
-        return ret;
-    }
+    ret = qemuMonitorAttachDiskDevice(vm, dev, conPtr, type);
 
-    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
-        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                         _("cannot attach %s disk"), type);
-        VIR_FREE(cmd);
-        return -1;
-    }
-
-    VIR_FREE(cmd);
-
-    if (qemudParseDriveAddReply(vm, reply, &bus_id, &unit_id) < 0) {
-        if (!tryOldSyntax && strstr(reply, "invalid char in expression")) {
-            VIR_FREE(reply);
-            tryOldSyntax = 1;
-            goto try_command;
-        }
-        qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                          _("adding %s disk failed: %s"), type, reply);
-        VIR_FREE(reply);
-        return -1;
-    }
-
-    if (dev->data.disk->bus_id == -1) {
-        dev->data.disk->bus_id = bus_id;
-    }
-
-    if (dev->data.disk->unit_id == -1) {
-        dev->data.disk->unit_id = unit_id;
-    }
-
-    dev->data.disk->pci_addr.domain = domain;
-    dev->data.disk->pci_addr.bus    = bus;
-    dev->data.disk->pci_addr.slot   = slot;
-
-    virDomainDiskInsertPreAlloced(vm->def, dev->data.disk);
+    if (!ret)
+        virDomainDiskInsertPreAlloced(vm->def, dev->data.disk);
 
-    return 0;
+    return ret;
 }
 
 static int qemudDomainAttachDiskController(virConnectPtr conn,
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index 7326d05..371388f 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -1594,6 +1594,91 @@ try_command:
     return 0;
 }
 
+int qemuMonitorAttachDiskDevice(virDomainObjPtr vm,
+                                virDomainDeviceDefPtr dev,
+                                virDomainControllerDefPtr conPtr,
+                                const char *type)
+{
+    int ret;
+    char *cmd, *reply;
+    char *safe_path;
+    int tryOldSyntax = 0;
+    int bus_id, unit_id;
+    int domain, bus, slot;
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char* bus_unit_string;
+
+    domain = conPtr->address->data.pci.domain;
+    bus    = conPtr->address->data.pci.bus;
+    slot   = conPtr->address->data.pci.slot;
+
+    if (dev->data.disk->bus_id != -1) {
+       virBufferVSprintf(&buf, ",bus=%d", dev->data.disk->bus_id);
+    }
+
+    if (dev->data.disk->unit_id != -1) {
+        virBufferVSprintf(&buf, ",unit=%d", dev->data.disk->unit_id);
+    }
+
+    bus_unit_string = virBufferContentAndReset(&buf);
+
+    safe_path = qemuMonitorEscapeArg(dev->data.disk->src);
+    if (!safe_path) {
+        virReportOOMError(NULL);
+        return -1;
+    }
+
+try_command:
+    VIR_DEBUG ("%s: drive_add %.2x:%.2x:%.2x file=%s,if=%s%s", vm->def->name,
+               domain, bus, slot, safe_path, type,
+               bus_unit_string ? bus_unit_string : "");
+    ret = virAsprintf(&cmd, "drive_add %s%.2x:%.2x:%.2x file=%s,if=%s%s",
+                      (tryOldSyntax ? "" : "pci_addr="), domain, bus,
+                      slot, safe_path, type,
+                      bus_unit_string ? bus_unit_string : "");
+
+    VIR_FREE(safe_path);
+    if (ret == -1) {
+        virReportOOMError(NULL);
+        return ret;
+    }
+
+    if (qemuMonitorCommand(vm, cmd, &reply) < 0) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED,
+                         _("cannot attach %s disk"), type);
+        VIR_FREE(cmd);
+        return -1;
+    }
+
+    VIR_FREE(cmd);
+
+    if (qemudParseDriveAddReply(vm, reply, &bus_id, &unit_id) < 0) {
+        if (!tryOldSyntax && strstr(reply, "invalid char in expression")) {
+            VIR_FREE(reply);
+            tryOldSyntax = 1;
+            goto try_command;
+        }
+        qemudReportError (NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED,
+                          _("adding %s disk failed: %s"), type, reply);
+        VIR_FREE(reply);
+        return -1;
+    }
+
+    if (dev->data.disk->bus_id == -1) {
+        dev->data.disk->bus_id = bus_id;
+    }
+
+    if (dev->data.disk->unit_id == -1) {
+        dev->data.disk->unit_id = unit_id;
+    }
+
+    dev->data.disk->pci_addr.domain = domain;
+    dev->data.disk->pci_addr.bus    = bus;
+    dev->data.disk->pci_addr.slot   = slot;
+
+    return 0;
+}
+
 int qemuMonitorAddPCIHostDevice(const virDomainObjPtr vm,
                                 unsigned hostDomain ATTRIBUTE_UNUSED,
                                 unsigned hostBus,
diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h
index c674507..6a2d898 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -156,6 +156,11 @@ int qemuMonitorAddPCINetwork(const virDomainObjPtr vm,
 
 int qemuMonitorAttachDiskController(virDomainObjPtr vm,
                                     virDomainDeviceDefPtr dev);
+int qemuMonitorAttachDiskDevice(virDomainObjPtr vm,
+                                virDomainDeviceDefPtr dev,
+                                virDomainControllerDefPtr conPtr,
+                                const char *type);
+
 int qemuMonitorRemovePCIDevice(const virDomainObjPtr vm,
                                unsigned guestDomain,
                                unsigned guestBus,
-- 
1.6.4


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