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

Re: [libvirt] [virt-tools-list] [RFC] virt-disk : a command line tool for modify domain XML's disk of inactive domains.



On Mon, 21 Feb 2011 17:27:13 -0700
Eric Blake <eblake redhat com> wrote:

> On 02/21/2011 05:20 PM, Eric Blake wrote:
> >> 1. When we asked "Is it a spec that we cannot modify inactive domain ?" to
> >>    a Redhat guy, he answered "it's a spec".
> >>    Do you, maintainers, have some concensus about this ?
> > 
> > 'virsh attach-disk --persistent' is supposed to be able to modify an
> > inactive domain.  If it doesn't do so for qemu, then that's because no
> > one has yet implemented it correctly, which means libvirt has a bug that
> > needs to be patched.  For example, see:
> > 
> > https://bugzilla.redhat.com/show_bug.cgi?id=669549
> > 
> > about 'virsh setmem --config' not working for qemu.
> 
> And https://bugzilla.redhat.com/show_bug.cgi?id=658713 for attach-disk
> --persistent.
> 


This is a quick hack aginst attach-disk. Seems to work fine for me.
But, for example, 'What lock should be held ?' is not very clear to me ;)

CC'ed to my co-workers for sharing. Please point out bad things you notice.
Total update with detach-disk and attach/detach interfaces will be posted.

==
>From 14e1de99d433ba68917a80cc5b82da7f9436d419 Mon Sep 17 00:00:00 2001
From: KAMEZAWA Hiroyuki <kamezawa bluextal (none)>
Date: Tue, 22 Feb 2011 16:09:21 +0900
Subject: [PATCH] This patch is for allowing inactive domain by virsh command.

As the first step, this just only supports attach-disk.
Further update will allow to support other commands.

After this patch,
% virsh attach-disk ... --persistent
will modify domain XML format of inactive domain. For now,
--persistent against active domain returns error.

Note: At this stage, I can't guarantee the change will be
      applied against both of alive guest and its XML definition
      in atomic. And this is the same behavior Xen driver does.

Signed-off-by: KAMEZAWA Hiroyuki <kamezawa hiroyu jp fujitsu com>
---
 src/qemu/qemu_driver.c |  117 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 113 insertions(+), 4 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 0f25a2a..80cb724 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4082,16 +4082,125 @@ cleanup:
     return ret;
 }
 
+/*
+ * Attach a device given by XML, the change will be persistent
+ * and domain XML definition file is updated.
+ */
+static int qemuDomainAttachDevicePersistent(virDomainPtr dom,
+                                     const char *xml)
+{
+    struct qemud_driver *driver;
+    virDomainDeviceDefPtr newdev;
+    virDomainDefPtr vmdef;
+    virDomainObjPtr vm;
+    int i, ret = -1;
+    int bus_update = -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)
+        goto unlock_out;
+
+    if (virDomainObjIsActive(vm)) {
+	/*
+         * 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;
+    }
+
+    if (vm->newDef)
+        vmdef = vm->newDef;
+    else
+        vmdef = vm->def;
+
+    newdev = virDomainDeviceDefParse(driver->caps,
+                 vmdef, xml, VIR_DOMAIN_XML_INACTIVE);
+    if (!newdev)
+        goto endjob;
+    /* At first, check device confliction */
+    switch(newdev->type) {
+    case VIR_DOMAIN_DEVICE_DISK:
+        for (i = 0; i < vmdef->ndisks; i++) {
+            virDomainDiskDefPtr vdisk;
+            //virDomainDiskDefPtr vdisk;
+            vdisk = vm->def->disks[i];
+            if (strcmp(vdisk->dst, newdev->data.disk->dst) == 0) {
+                qemuReportError(VIR_ERR_INVALID_ARG,
+                    _("target %s already exists."), vdisk->dst);
+                goto endjob;
+            }
+        }
+        if (virDomainDiskInsert(vmdef, newdev->data.disk)) {
+            virReportOOMError();
+            goto endjob;
+        }
+        qemuReportError(VIR_ERR_INTERNAL_ERROR, "address info %p",
+			newdev->data.disk->info);
+        if (newdev->data.disk->info == NULL) {
+            /* Auto generate PCI address */
+            if (newdev->data.disk->bus == VIR_DOMAIN_DISK_BUS_VIRTIO)
+                qemuDomainAssignPCIAddresses(vmdef);
+            /* Other devices as IDE, SCSI...will get ID automatically */
+        }
+        newdev->data.disk = NULL;
+        break;
+    default:
+        qemuReportError(VIR_ERR_INVALID_ARG, "%s",
+                        _("Sorry, the device is not suppored for now"));
+        goto endjob;
+    }
+
+    ret = virDomainSaveConfig(driver->configDir, vmdef);
+
+endjob:
+    if (qemuDomainObjEndJob(vm) == 0)
+        vm = NULL;
+    if (vm)
+        virDomainObjUnlock(vm);
+    /* Note: insert of newdev is done by copy */
+    virDomainDeviceDefFree(newdev);
+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 qemuDomainAttachDevicePersistent(dom, xml);
     }
 
-    return qemudDomainAttachDevice(dom, xml);
+    if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE)
+        return qemudDomainAttachDevice(dom, xml);
+
+    return -1;
 }
 
 
-- 
1.7.1




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