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

[libvirt] [PATCH 16/27] Add API for using 'usb_add' for disk devices



* src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h: Add new
  qemuMonitorAddUSBDisk() API
* src/qemu/qemu_driver.c: Switch USB disk hotplug to the new
  src/qemu/qemu_driver.c API.
---
 src/qemu/qemu_driver.c       |   41 ++++++---------------------------------
 src/qemu/qemu_monitor_text.c |   43 ++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor_text.h |    4 +++
 3 files changed, 54 insertions(+), 34 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index da08af9..635fb84 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4698,59 +4698,32 @@ static int qemudDomainAttachUsbMassstorageDevice(virConnectPtr conn,
                                                  virDomainObjPtr vm,
                                                  virDomainDeviceDefPtr dev)
 {
-    int ret, i;
-    char *safe_path;
-    char *cmd, *reply;
+    int i;
 
     for (i = 0 ; i < vm->def->ndisks ; i++) {
         if (STREQ(vm->def->disks[i]->dst, dev->data.disk->dst)) {
-            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
                            _("target %s already exists"), dev->data.disk->dst);
             return -1;
         }
     }
 
-    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
-        virReportOOMError(conn);
-        return -1;
-    }
-
-    safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
-    if (!safe_path) {
-        virReportOOMError(conn);
+    if (!dev->data.disk->src) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                         "%s", _("disk source path is missing"));
         return -1;
     }
 
-    ret = virAsprintf(&cmd, "usb_add disk:%s", safe_path);
-    VIR_FREE(safe_path);
-    if (ret == -1) {
+    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
         virReportOOMError(conn);
-        return ret;
-    }
-
-    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
-        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                         "%s", _("cannot attach usb disk"));
-        VIR_FREE(cmd);
         return -1;
     }
 
-    DEBUG ("%s: attach_usb reply: %s",vm->def->name,  reply);
-    /* If the command failed qemu prints:
-     * Could not add ... */
-    if (strstr(reply, "Could not add ")) {
-        qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                          "%s",
-                          _("adding usb disk failed"));
-        VIR_FREE(reply);
-        VIR_FREE(cmd);
+    if (qemuMonitorAddUSBDisk(vm, dev->data.disk->src) < 0)
         return -1;
-    }
 
     virDomainDiskInsertPreAlloced(vm->def, dev->data.disk);
 
-    VIR_FREE(reply);
-    VIR_FREE(cmd);
     return 0;
 }
 
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index c154019..fd50cf2 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -1199,3 +1199,46 @@ cleanup:
     VIR_FREE(dest);
     return ret;
 }
+
+
+int qemuMonitorAddUSBDisk(const virDomainObjPtr vm,
+                          const char *path)
+{
+    char *cmd = NULL;
+    char *safepath;
+    int ret = -1;
+    char *info = NULL;
+
+    safepath = qemudEscapeMonitorArg(path);
+    if (!safepath) {
+        virReportOOMError(NULL);
+        return -1;
+    }
+
+    if (virAsprintf(&cmd, "usb_add disk:%s", safepath) < 0) {
+        virReportOOMError(NULL);
+        goto cleanup;
+    }
+
+    if (qemudMonitorCommand(vm, cmd, &info) < 0) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                         "%s", _("cannot run monitor command to add usb disk"));
+        goto cleanup;
+    }
+
+    DEBUG ("%s: usb_add reply: %s", vm->def->name, info);
+    /* If the command failed qemu prints:
+     * Could not add ... */
+    if (strstr(info, "Could not add ")) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED,
+                         _("unable to add USB disk %s: %s"), path, info);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(cmd);
+    VIR_FREE(safepath);
+    return ret;
+}
diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h
index ffed049..138e7a0 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -136,4 +136,8 @@ int qemuMonitorMigrateToCommand(const virDomainObjPtr vm,
                                 const char * const *argv,
                                 const char *target);
 
+
+int qemuMonitorAddUSBDisk(const virDomainObjPtr vm,
+                          const char *path);
+
 #endif /* QEMU_MONITOR_TEXT_H */
-- 
1.6.2.5


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