[PATCH 1/2] libvirt/qemu : allow persistent modification of disks via A(De)ttachDeviceFlags

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

 



Sorry, pervious mail was sent to wrong list...
==
>From 030135224dd6563af0fb8615dc6a4b8e6084410d Mon Sep 17 00:00:00 2001
From: KAMEZAWA Hiroyuki <kamezawa@bluextal.(none)>
Date: Wed, 23 Feb 2011 15:25:26 +0900
Subject: [PATCH 1/2] libvirt/qemu : support attach/detach-disk --persistent

Now, only Xen supports 'virsh attach/detach-disk XXXX --persistent',
modifying inactive domain definition via virsh.

This patch adds a support for qemu. With this patch,
virsh attach/detach-disk --persistent works well with qemu.

Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
---
 src/qemu/qemu_driver.c |  170 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 163 insertions(+), 7 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 0f25a2a..703f86a 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4082,16 +4082,174 @@ cleanup:
     return ret;
 }
 
+static int qemuDomainFindDiskByName(virDomainDefPtr vmdef, const char *name)
+{
+    virDomainDiskDefPtr vdisk;
+    int i;
+
+    for (i = 0; i < vmdef->ndisks; i++) {
+        vdisk = vmdef->disks[i];
+        if (!strcmp(vdisk->dst, name))
+            return i;
+    }
+    return -1;
+}
+/*
+ * Attach a device given by XML, the change will be persistent
+ * and domain XML definition file is updated.
+ */
+static int qemuDomainAttachDevicePersistent(virDomainDefPtr vmdef,
+                                            virDomainDeviceDefPtr newdev)
+{
+    virDomainDiskDefPtr disk;
+
+    /* At first, check device confliction */
+    switch(newdev->type) {
+    case VIR_DOMAIN_DEVICE_DISK:
+        disk = newdev->data.disk;
+        if (qemuDomainFindDiskByName(vmdef, disk->dst) >= 0) {
+            qemuReportError(VIR_ERR_INVALID_ARG,
+                 _("target %s already exists."), disk->dst);
+            return -1;
+        }
+
+        if (virDomainDiskInsert(vmdef, disk)) {
+            virReportOOMError();
+            return -1;
+        }
+        if (disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
+            /*
+             * Address/Drive information is NULL. If virtio, PCI address
+             * shoule be fixed. Other devices as IDE, SCSI...will get ID
+             * automatically
+             */
+            qemuDomainAssignPCIAddresses(vmdef);
+
+        }
+        newdev->data.disk = NULL;
+        break;
+    default:
+        qemuReportError(VIR_ERR_INVALID_ARG, "%s",
+                        _("Sorry, the device is not suppored for now"));
+        return -1;
+    }
+    
+    return 0;
+}
+
+static int qemuDomainDetachDevicePersistent(virDomainDefPtr vmdef,
+		virDomainDeviceDefPtr device)
+{
+    int x;
+    virDomainDiskDefPtr disk;
+
+    switch(device->type) {
+    case VIR_DOMAIN_DEVICE_DISK:
+        disk = device->data.disk;
+        x = qemuDomainFindDiskByName(vmdef, disk->dst);
+        if (x < 0) {
+            qemuReportError(VIR_ERR_INVALID_ARG,
+                 _("target %s doesn't exist."), disk->dst);
+            return -1;
+        }
+        virDomainDiskRemove(vmdef, x);
+        break;
+    default:
+        qemuReportError(VIR_ERR_INVALID_ARG, "%s",
+                        _("Sorry, the device is not suppored for now"));
+        return -1;
+    }
+    return 0;
+}
+
+static int qemuDomainModifyDevicePersistent(virDomainPtr dom,
+                                        const char *xml,
+                                        unsigned int attach)
+{
+    struct qemud_driver *driver;
+    virDomainDeviceDefPtr device;
+    virDomainDefPtr vmdef;
+    virDomainObjPtr vm;
+    int ret = -1;
+
+    if (!dom || !dom->conn || !dom->name || !xml) {
+        qemuReportError(VIR_ERR_INVALID_ARG,
+                        _("internal error : %s"),  __FUNCTION__);
+        return -1;
+    }
+
+    if (dom->conn->flags & VIR_CONNECT_RO)
+        return -1;
+
+    driver = dom->conn->privateData;
+    qemuDriverLock(driver);
+    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
+    if (!vm) {
+        qemuReportError(VIR_ERR_NO_DOMAIN, _("cannot find domain '%s'"),
+                        dom->name);
+        goto unlock_out;
+    }
+
+    if (qemuDomainObjBeginJobWithDriver(driver, vm) < 0) {
+	/*
+         * For now, just allow updating inactive domains. Further development
+	 * will allow updating both active domain and its config file at
+	 * the same time.
+	 */
+        qemuReportError(VIR_ERR_INVALID_ARG,
+                        _("cannot update alive domain : %s"),  __FUNCTION__);
+        goto endjob;
+    }
+    vmdef = virDomainObjGetPersistentDef(driver->caps, vm);
+
+    if (!vmdef)
+        goto endjob;
+
+    device = virDomainDeviceDefParse(driver->caps,
+                 vmdef, xml, VIR_DOMAIN_XML_INACTIVE);
+    if (!device)
+        goto endjob;
+
+    if (attach) {
+        ret = qemuDomainAttachDevicePersistent(vmdef, device);
+        if (ret < 0)
+            goto out;
+    } else {
+        ret = qemuDomainDetachDevicePersistent(vmdef, device);
+        if (ret < 0)
+            goto out;
+    }
+    ret = virDomainSaveConfig(driver->configDir, vmdef);
+
+out:
+    virDomainDeviceDefFree(device);
+endjob:
+    if (qemuDomainObjEndJob(vm) == 0)
+        vm = NULL;
+    if (vm)
+        virDomainObjUnlock(vm);
+    /* Note: insert of newdev is done by copy */
+unlock_out:
+    qemuDriverUnlock(driver);
+    return ret;
+}
+
 static int qemudDomainAttachDeviceFlags(virDomainPtr dom,
                                         const char *xml,
                                         unsigned int flags) {
     if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
-        qemuReportError(VIR_ERR_OPERATION_INVALID,
-                        "%s", _("cannot modify the persistent configuration of a domain"));
-        return -1;
+        /*
+	 * Because we can't update the live guest and XML
+         * in atomic, limiting modification as only-acrive and
+         * only-inactive. Need some idea to update both at the same time.
+         */
+        return qemuDomainModifyDevicePersistent(dom, xml, 1);
     }
 
-    return qemudDomainAttachDevice(dom, xml);
+    if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE)
+        return qemudDomainAttachDevice(dom, xml);
+
+    return -1;
 }
 
 
@@ -4304,9 +4462,7 @@ static int qemudDomainDetachDeviceFlags(virDomainPtr dom,
                                         const char *xml,
                                         unsigned int flags) {
     if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
-        qemuReportError(VIR_ERR_OPERATION_INVALID,
-                        "%s", _("cannot modify the persistent configuration of a domain"));
-        return -1;
+        return qemuDomainModifyDevicePersistent(dom, xml, 0);
     }
 
     return qemudDomainDetachDevice(dom, xml);
-- 
1.7.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]