[PATCHv7 2/4] libvirt/qemu - support persistent disk modification

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

 



>From 4da9bdc62bb2b26610eb3b1ea8776d90af04dabc Mon Sep 17 00:00:00 2001
From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
Date: Fri, 25 Mar 2011 17:08:36 +0900
Subject: [PATCHv7 2/4] libvirt/qemu - support persistent disk modification

support changes of disks by VIR_DOMAIN_DEVICE_MODIFY_CONFIG
for qemu.

Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>

Changelog: v6->v7
 - removed error message, which seems redundunt.
   (another patch will add new checks.)
 - Fixed a bug which PCI address is not assigned to a new controller.
---
 src/conf/domain_conf.c   |   22 ++++++++++++++++++++++
 src/conf/domain_conf.h   |    3 ++-
 src/libvirt_private.syms |    2 ++
 src/qemu/qemu_driver.c   |   41 ++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index b681dc3..3e3f342 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -4859,6 +4859,19 @@ virVirtualPortProfileFormat(virBufferPtr buf,
     virBufferVSprintf(buf, "%s</virtualport>\n", indent);
 }
 
+int virDomainDiskIndexByName(virDomainDefPtr def, const char *name)
+{
+    virDomainDiskDefPtr vdisk;
+    int i;
+
+    for (i = 0; i < def->ndisks; i++) {
+        vdisk = def->disks[i];
+        if (STREQ(vdisk->dst, name))
+            return i;
+    }
+    return -1;
+}
+
 int virDomainDiskInsert(virDomainDefPtr def,
                         virDomainDiskDefPtr disk)
 {
@@ -4930,6 +4943,15 @@ void virDomainDiskRemove(virDomainDefPtr def, size_t i)
     }
 }
 
+int virDomainDiskRemoveByName(virDomainDefPtr def, const char *name)
+{
+    int i = virDomainDiskIndexByName(def, name);
+    if (i < 0)
+        return -1;
+    virDomainDiskRemove(def, i);
+    return 0;
+}
+
 
 int virDomainControllerInsert(virDomainDefPtr def,
                               virDomainControllerDefPtr controller)
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 1e8223f..b791714 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1255,7 +1255,7 @@ int virDomainCpuSetParse(const char **str,
                          int maxcpu);
 char *virDomainCpuSetFormat(char *cpuset,
                             int maxcpu);
-
+int virDomainDiskIndexByName(virDomainDefPtr def, const char *name);
 int virDomainDiskInsert(virDomainDefPtr def,
                         virDomainDiskDefPtr disk);
 void virDomainDiskInsertPreAlloced(virDomainDefPtr def,
@@ -1263,6 +1263,7 @@ void virDomainDiskInsertPreAlloced(virDomainDefPtr def,
 int virDomainDiskDefAssignAddress(virCapsPtr caps, virDomainDiskDefPtr def);
 
 void virDomainDiskRemove(virDomainDefPtr def, size_t i);
+int virDomainDiskRemoveByName(virDomainDefPtr def, const char *name);
 
 int virDomainControllerInsert(virDomainDefPtr def,
                               virDomainControllerDefPtr controller);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index b4b6c63..7459c40 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -243,11 +243,13 @@ virDomainDiskDefFree;
 virDomainDiskDeviceTypeToString;
 virDomainDiskErrorPolicyTypeFromString;
 virDomainDiskErrorPolicyTypeToString;
+virDomainDiskIndexByName;
 virDomainDiskInsert;
 virDomainDiskInsertPreAlloced;
 virDomainDiskIoTypeFromString;
 virDomainDiskIoTypeToString;
 virDomainDiskRemove;
+virDomainDiskRemoveByName;
 virDomainDiskTypeFromString;
 virDomainDiskTypeToString;
 virDomainFSDefFree;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index a4fb1cd..4d3b416 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4144,6 +4144,14 @@ cleanup:
     return ret;
 }
 
+static int qemuDomainDeviceAddressFixup(virDomainDefPtr vmdef, bool pci)
+{
+    if (!pci && virDomainDefAddImplicitControllers(vmdef))
+        return -1;
+    /* added controller requires PCI address */
+    return qemuDomainAssignPCIAddresses(vmdef);
+}
+
 /*
  * Attach a device given by XML, the change will be persistent
  * and domain XML definition file is updated.
@@ -4151,21 +4159,52 @@ cleanup:
 static int qemuDomainAttachDevicePersistent(virDomainDefPtr vmdef,
                                             virDomainDeviceDefPtr newdev)
 {
+    virDomainDiskDefPtr disk;
+    bool pci;
+    int ret;
 
     switch(newdev->type) {
+    case VIR_DOMAIN_DEVICE_DISK:
+        disk = newdev->data.disk;
+        if (virDomainDiskIndexByName(vmdef, disk->dst) >= 0) {
+            qemuReportError(VIR_ERR_INVALID_ARG,
+                            _("target %s already exists."), disk->dst);
+            return -1;
+        }
+        if (virDomainDiskInsert(vmdef, disk)) {
+            virReportOOMError();
+            return -1;
+        }
+        pci = (disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO);
+        ret = qemuDomainDeviceAddressFixup(vmdef, pci);
+        if (ret < 0)
+            virDomainDiskRemoveByName(vmdef, disk->dst);
+        else
+            newdev->data.disk = NULL;
+        break;
     default:
         qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                         _("Sorry, the device is not supported for now"));
         return -1;
     }
 
-    return 0;
+    return ret;
 }
 
 static int qemuDomainDetachDevicePersistent(virDomainDefPtr vmdef,
                                             virDomainDeviceDefPtr device)
 {
+    virDomainDiskDefPtr disk;
+
     switch(device->type) {
+    case VIR_DOMAIN_DEVICE_DISK:
+        disk = device->data.disk;
+        if (virDomainDiskRemoveByName(vmdef, disk->dst)) {
+            qemuReportError(VIR_ERR_INVALID_ARG,
+                            _("no target device %s"), disk->dst);
+            return -1;
+        }
+        break;
     default:
         qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                         _("Sorry, the device is not supported for now"));
-- 
1.7.4.1


--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[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]