Skip to contentSkip to conversation

Support disk-hotremove and controller hotplugging

PATCH In review Wolfgang Mauerer 3 participants 14 messages Sep 18, 2009 – Oct 13, 2009 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

cover Support disk-hotremove and controller hotplugging

Hi,

this patch reworks libvirt's disk-hotadd support and introduces support for disk controller hotplugging and disk-hotremove (see http://thread.gmane.org/gmane.comp.emulators.libvirt/15860 for more details). Currently, it targets only qemu and also requires some additions to qemu that have only recently been submitted, which is why this is cross-posted to qemu-devel. Hopefully the lack of support in qemu does not prevent the community from reviewing the code, though ;-)

Notice that no documentation and testcases are included yet; I'll supply those once the interface is fixed and possible issues are settled.

Thanks for your comments,

Wolfgang

 docs/schemas/domain.rng  |  145 ++++++++++----
 src/domain_conf.c        |  208 ++++++++++++++++++++-
 src/domain_conf.h        |   39 ++++-
 src/libvirt_private.syms |    4 +-
 src/qemu_driver.c        |  479 +++++++++++++++++++++++++++++++++++++++++++---
 5 files changed, 802 insertions(+), 73 deletions(-)
Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#1 lore raw

PATCH 1/9 Clarify documentation for private symbols

The instruction "See Makefile.am" in libvirt.private_syms always makes me think that this file is autogenerated and should not be touched manually. This patch spares every reader of libvirt.private_syms the hassle of reading Makefile.am before augmenting libvirt.private_syms.

---
 src/libvirt_private.syms |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
src/libvirt_private.syms +2 -1
index 867678f..f724493 100644
@@ -1,5 +1,6 @@
 #
-# General private symbols. See Makefile.am.
+# General private symbols. Add symbols here, and see Makefile.am for
+# more details.
 #
 
 
Signature · 1 line
1.6.4
#2 lore raw

PATCH 2/9 Extend <disk> element with controller information

In reply to Wolfgang Mauerer

This allows us to connect a disk with a specific controller, which is required for disk hotadd/remove. A new XML child element is added to the <disk> container:

<disk>
...

	<controller pci_addr="<addr>" id="<identifier>" bus="<number>" unit="<number>"/>

</disk>

Either id _or_ pci_addr can be specified. When the controller has been brought into the system via tghe hotplug mechanism also included in this patch series, it will typically have an ID. In other cases, the controller can also be specified with the PCI address.

Wrt. the data structures touched in this commit, notice that while "bus" as subelement of target specifies the type of bus (e.g., scsi, ide,...), it is used to enumerate the buses on the controller here.

---
 src/domain_conf.c |   29 +++++++++++++++++++++++++++++
 src/domain_conf.h |    5 ++++-
 2 files changed, 33 insertions(+), 1 deletions(-)
src/domain_conf.c +29 -0
index 5ae0775..a6120c8 100644
@@ -650,7 +650,12 @@ virDomainDiskDefParseXML(virConnectPtr conn,
     char *driverType = NULL;
     char *source = NULL;
     char *target = NULL;
+    char *controller = NULL;
+    char *bus_id = NULL;
+    char *unit_id = NULL;
+    char *controller_id = NULL;
     char *bus = NULL;
+    char *unit = NULL;
     char *cachetag = NULL;
     char *devaddr = NULL;
     virStorageEncryptionPtr encryption = NULL;
@@ -694,12 +699,18 @@ virDomainDiskDefParseXML(virConnectPtr conn,
                        (xmlStrEqual(cur->name, BAD_CAST "target"))) {
                 target = virXMLPropString(cur, "dev");
                 bus = virXMLPropString(cur, "bus");
+                unit = virXMLPropString(cur, "unit");
 
                 /* HACK: Work around for compat with Xen
                  * driver in previous libvirt releases */
                 if (target &&
                     STRPREFIX(target, "ioemu:"))
                     memmove(target, target+6, strlen(target)-5);
+            } else if ((controller == NULL) &&
+                       (xmlStrEqual(cur->name, BAD_CAST "controller"))) {
+	      controller_id = virXMLPropString(cur, "id");
+	      bus_id = virXMLPropString(cur, "bus");
+	      unit_id = virXMLPropString(cur, "unit");
             } else if ((driverName == NULL) &&
                        (xmlStrEqual(cur->name, BAD_CAST "driver"))) {
                 driverName = virXMLPropString(cur, "name");
@@ -800,6 +811,24 @@ virDomainDiskDefParseXML(virConnectPtr conn,
         }
     }
 
+    if (controller) {
+        def->controller_id = controller_id;
+
+	def->bus_id = -1;
+	if (bus_id && virStrToLong_i(bus_id, NULL, 10, &def->bus_id) < 0) {
+	    virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
+			 _("Cannot parse <controller> 'bus' attribute"));
+	    goto error;
+      } 
+
+	def->unit_id = -1;
+	if (unit_id && virStrToLong_i(unit_id, NULL, 10, &def->unit_id) < 0) {
+	    virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
+			 _("Cannot parse <controller> 'unit' attribute"));
+	    goto error;
+	} 
+    }
+
     if (def->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY &&
         def->bus != VIR_DOMAIN_DISK_BUS_FDC) {
         virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR,
src/domain_conf.h +4 -1
index 09368d9..898f6c9 100644
@@ -105,9 +105,12 @@ typedef virDomainDiskDef *virDomainDiskDefPtr;
 struct _virDomainDiskDef {
     int type;
     int device;
-    int bus;
+    int bus;          /* Bus type, e.g. scsi or ide */
+    int bus_id;       /* Bus number on the controller */
+    int unit_id;      /* Unit on the controller */
     char *src;
     char *dst;
+    char *controller_id;
     char *driverName;
     char *driverType;
     char *serial;
Signature · 1 line
1.6.4
#3 lore raw

PATCH 3/9 Add new domain device: "controller"

In reply to Wolfgang Mauerer

This augments virDomainDevice with a <controller> element that is used to represent disk controllers (e.g., scsi controllers). The XML format is given by

<controller type="scsi" id="<my_id>">
<bus addr="<Domain>:<Bus>:<Slot>">
</controller>

where type denotes the disk interface (scsi, ide,...), id is an arbitrary string that identifies the controller for disk hotadd/remove, and bus addr denotes the controller address on the PCI bus.

The bus element can be omitted; in this case, an address will be assigned automatically. Currently, only hotplugging scsi controllers on a PCI bus is supported, and this only for qemu guests

Routines for parsing this definition and the associated data structures are included in this commit.

---
 docs/schemas/domain.rng |  145 ++++++++++++++++++++++++++++++++++-------------
 src/domain_conf.c       |  125 ++++++++++++++++++++++++++++++++++++++++
 src/domain_conf.h       |   24 ++++++++
 3 files changed, 255 insertions(+), 39 deletions(-)
docs/schemas/domain.rng +106 -39
index 70e98a7..dc9b849 100644
@@ -364,49 +364,116 @@
   <define name="disk">
     <element name="disk">
       <optional>
-        <attribute name="device">
+	<attribute name="device">
+	  <choice>
+	    <value>floppy</value>
+	    <value>disk</value>
+	    <value>cdrom</value>
+	  </choice>
+	</attribute>
+      </optional>
+      <optional>
+	<element name="controller">
+	  <optional>
+	    <attribute name="bus">
+	      <ref name="unsignedInt"/>
+	    </attribute>
+	  </optional>
+	  <optional>
+	    <attribute name="unit">
+	      <ref name="unsignedInt"/>
+	    </attribute>
+	  </optional>
+	  <choice>
+	    <attribute name="id">
+	      <ref name="genericName"/>
+	    </attribute>
+	    <attribute name="pciaddr">
+	      <!-- Just for testing -->
+	      <ref name="deviceName"/>
+	    </attribute>
+	  </choice>
+	</element>
+      </optional>
+      <choice>
+	<group>
+	  <attribute name="type">
+	    <value>file</value>
+	  </attribute>
+	  <interleave>
+	    <optional>
+	      <element name="source">
+		<attribute name="file">
+		  <ref name="absFilePath"/>
+		</attribute>
+		<empty/>
+	      </element>
+	    </optional>
+	    <ref name="diskspec"/>
+	  </interleave>
+	</group>
+	<group>
+	  <attribute name="type">
+	    <value>block</value>
+	  </attribute>
+	  <interleave>
+	    <optional>
+	      <element name="source">
+		<attribute name="dev">
+		  <ref name="deviceName"/>
+		</attribute>
+		<empty/>
+	      </element>
+	    </optional>
+	    <ref name="diskspec"/>
+	  </interleave>
+	</group>
+	<ref name="diskspec"/>
+      </choice>
+    </element>
+  </define>
+  <define name="target">
+    <element name="target">
+      <attribute name="dev">
+        <ref name="deviceName"/>
+      </attribute>
+      <optional>
+        <attribute name="bus">
           <choice>
-            <value>floppy</value>
-            <value>disk</value>
-            <value>cdrom</value>
+            <value>ide</value>
+            <value>fdc</value>
+            <value>scsi</value>
+            <value>virtio</value>
+            <value>xen</value>
+            <value>usb</value>
+            <value>uml</value>
           </choice>
         </attribute>
       </optional>
-      <choice>
-        <group>
-          <attribute name="type">
-            <value>file</value>
-          </attribute>
-          <interleave>
-            <optional>
-              <element name="source">
-                <attribute name="file">
-                  <ref name="absFilePath"/>
-                </attribute>
-                <empty/>
-              </element>
-            </optional>
-            <ref name="diskspec"/>
-          </interleave>
-        </group>
-        <group>
-          <attribute name="type">
-            <value>block</value>
-          </attribute>
-          <interleave>
-            <optional>
-              <element name="source">
-                <attribute name="dev">
-                  <ref name="deviceName"/>
-                </attribute>
-                <empty/>
-              </element>
-            </optional>
-            <ref name="diskspec"/>
-          </interleave>
-        </group>
-        <ref name="diskspec"/>
-      </choice>
+    </element>
+  </define>
+
+  <define name="controller">
+    <element name="controller">
+      <interleave>
+	<attribute name="type">
+	  <choice>
+	    <!-- For now, only SCSI is supported -->
+	    <value>scsi</value>
+	  </choice>
+	</attribute>
+	<attribute name="id">
+	  <ref name="genericName"/>
+	</attribute>
+      </interleave>
+      <optional>
+	<element name="bus">
+	  <attribute name="addr">
+	    <!-- Just for testing, should be pciaddress -->
+	    <ref name="deviceName"/>
+	  </attribute>
+	</element>
+      </optional>
     </element>
   </define>
   <define name="target">
src/domain_conf.c +125 -0

173 lines not on this page · raw message

src/domain_conf.h +24 -0
index 898f6c9..6b3cb09 100644
@@ -111,6 +111,11 @@ struct _virDomainDiskDef {
     char *src;
     char *dst;
     char *controller_id;
+    struct {
+        unsigned domain;
+        unsigned bus;
+        unsigned slot;
+    } controller_pci_addr;
     char *driverName;
     char *driverType;
     char *serial;
@@ -125,6 +130,19 @@ struct _virDomainDiskDef {
     virStorageEncryptionPtr encryption;
 };
 
+/* Stores the virtual disk controller configuration */
+typedef struct _virDomainControllerDef virDomainControllerDef;
+typedef virDomainControllerDef *virDomainControllerDefPtr;
+struct _virDomainControllerDef {
+    int type;
+    char *id;
+    struct {
+        unsigned domain;
+        unsigned bus;
+        unsigned slot;
+    } pci_addr;
+};
+
 static inline int
 virDiskHasValidPciAddr(virDomainDiskDefPtr def)
 {
@@ -441,6 +459,7 @@ enum virDomainDeviceType {
     VIR_DOMAIN_DEVICE_SOUND,
     VIR_DOMAIN_DEVICE_VIDEO,
     VIR_DOMAIN_DEVICE_HOSTDEV,
+    VIR_DOMAIN_DEVICE_CONTROLLER,
 
     VIR_DOMAIN_DEVICE_LAST,
 };
@@ -451,6 +470,7 @@ struct _virDomainDeviceDef {
     int type;
     union {
         virDomainDiskDefPtr disk;
+        virDomainControllerDefPtr controller;
         virDomainFSDefPtr fs;
         virDomainNetDefPtr net;
         virDomainInputDefPtr input;
@@ -561,6 +581,9 @@ struct _virDomainDef {
     int ndisks;
     virDomainDiskDefPtr *disks;
 
+    int ncontrollers;
+    virDomainControllerDefPtr *controllers;
+
     int nfss;
     virDomainFSDefPtr *fss;
 
@@ -637,6 +660,7 @@ virDomainObjPtr virDomainFindByName(const virDomainObjListPtr doms,
 void virDomainGraphicsDefFree(virDomainGraphicsDefPtr def);
 void virDomainInputDefFree(virDomainInputDefPtr def);
 void virDomainDiskDefFree(virDomainDiskDefPtr def);
+void virDomainControllerDefFree(virDomainControllerDefPtr def);
 void virDomainFSDefFree(virDomainFSDefPtr def);
 void virDomainNetDefFree(virDomainNetDefPtr def);
 void virDomainChrDefFree(virDomainChrDefPtr def);
Signature · 1 line
1.6.4
#4 lore raw

[Qemu-devel] Re: [libvirt] [PATCH 3/9] Add new domain device: "controller"

Quoted 13 lines · Wolfgang Mauerer

This augments virDomainDevice with a <controller> element that is used to represent disk controllers (e.g., scsi controllers). The XML format is given by

<controller type="scsi" id="<my_id>">
<bus addr="<Domain>:<Bus>:<Slot>">
</controller>

where type denotes the disk interface (scsi, ide,...), id is an arbitrary string that identifies the controller for disk hotadd/remove, and bus addr denotes the controller address on the PCI bus.

Quoted 4 lines

The bus element can be omitted; in this case, an address will be assigned automatically. Currently, only hotplugging scsi controllers on a PCI bus is supported, and this only for qemu guests

As mentioned in the previous patch, I reckon 'id' is better called 'name'.

For PCI addresses, it is desirable to fully normalize the XML format, by which I mean have separate attributes for domain, bus and slot. We already have a syntax for PCI addresses used for host device passthrough, so it'd make sense to use the same syntax here for controllers. More broadly, we're probably going to have to add a PCI address element to all our devices. While it is unlikely we'll need non-PCI addresses, it doesn't hurt to make it explicit by adding a type='pci' attribute

Thus I'd suggest

    <address type='pci' domain='0x0000' bus='0x06' slot='0x12'/>

Instead of

    <bus addr="<Domain>:<Bus>:<Slot>">

In the domain_conf.c/.h parser, we could have a datatype like

   enum {
      VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI,

VIR_DOMAIN_DEVICE_ADDRESS_TYPE_LAST
};

   typedef struct _virDomainDevicePCIAddress virDomainDevicePCIAddress;
   struct _virDomainDevicePCIAddress {
        unsigned domain;
        unsigned bus;
        unsigned slot;
        unsigned function;
   };
   typedef struct _virDomainDeviceAddress virDomainDeviceAddress;
   struct _virDomainDeviceAddress {
      int type;
      union {
         virDomainDevicePCIAddress pci;
      } data;
   };

Which we then use in all the places in domain_conf.h wanting address information. Obviously we'd not use the 'function' field in most places, but doesn't hurt to have it.

And a pair of methods for parsing/formatting this address info we can call from all the appropriate locations.

src/domain_conf.h struct _virDomainDiskDef { · line 118

Quoted 13 lines
src/domain_conf.h +5 -0
index 898f6c9..6b3cb09 100644
@@ -111,6 +111,11 @@ struct _virDomainDiskDef {
     char *src;
     char *dst;
     char *controller_id;
+    struct {
+        unsigned domain;
+        unsigned bus;
+        unsigned slot;
+    } controller_pci_addr;

I think we should stick to just using the controller name as the mandatory identifier for cross-referencing disks to controllers.

Quoted 9 lines
src/domain_conf.h +2 -0
     char *driverName;
     char *driverType;
     char *serial;
@@ -125,6 +130,19 @@ struct _virDomainDiskDef {
     virStorageEncryptionPtr encryption;
 };
 
+/* Stores the virtual disk controller configuration */
+typedef struct _virDomainControllerDef virDomainControllerDef;

src/domain_conf.h struct _virDomainDiskDef { · line 144

Quoted 10 lines
src/domain_conf.h +10 -0
+typedef virDomainControllerDef *virDomainControllerDefPtr;
+struct _virDomainControllerDef {
+    int type;
+    char *id;
+    struct {
+        unsigned domain;
+        unsigned bus;
+        unsigned slot;
+    } pci_addr;
+};

With the generic address data type and s/id/name/, this would be just

  struct _virDomainControllerDef {
     int type;
     char *name;
     virDomainDeviceAddress addr;
  };

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#5 lore raw

PATCH 4/9 Add disk-based hotplugging for the qemu backend

In reply to Wolfgang Mauerer

When disks are added to a qemu backend with attach-device, not just the disk, but a complete new PCI controller with the disk attached is brought into the system. This patch implements a proper disk hotplugging scheme for qemu.

---
 src/domain_conf.c |   46 ++++++++--
 src/domain_conf.h |    2 +-
 src/qemu_driver.c |  257 ++++++++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 277 insertions(+), 28 deletions(-)
src/domain_conf.c +39 -7
index bbaf944..d0fda64 100644
@@ -663,7 +663,6 @@ virDomainDiskDefParseXML(virConnectPtr conn,
     char *driverType = NULL;
     char *source = NULL;
     char *target = NULL;
-    char *controller = NULL;
     char *controller_pci_addr = NULL;
     char *bus_id = NULL;
     char *unit_id = NULL;
@@ -720,12 +719,13 @@ virDomainDiskDefParseXML(virConnectPtr conn,
                 if (target &&
                     STRPREFIX(target, "ioemu:"))
                     memmove(target, target+6, strlen(target)-5);
-            } else if ((controller == NULL) &&
+            } else if ((controller_id == NULL && 
+                        controller_pci_addr == NULL) &&
                        (xmlStrEqual(cur->name, BAD_CAST "controller"))) {
-	      controller_id = virXMLPropString(cur, "id");
-	      controller_pci_addr =  virXMLPropString(cur, "pci_addr");
-	      bus_id = virXMLPropString(cur, "bus");
-	      unit_id = virXMLPropString(cur, "unit");
+                controller_id = virXMLPropString(cur, "id");
+                controller_pci_addr = virXMLPropString(cur, "pciaddr");
+                bus_id = virXMLPropString(cur, "bus");
+                unit_id = virXMLPropString(cur, "unit");
             } else if ((driverName == NULL) &&
                        (xmlStrEqual(cur->name, BAD_CAST "driver"))) {
                 driverName = virXMLPropString(cur, "name");
@@ -826,7 +826,13 @@ virDomainDiskDefParseXML(virConnectPtr conn,
         }
     }
 
-    if (controller) {
+    if (controller_id && controller_pci_addr) {
+        virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s",
+                             _("Both controller ID and address specified!"));
+        goto error;
+    }
+
+    if (controller_id) {
         def->controller_id = controller_id;
 
 	def->bus_id = -1;
@@ -857,12 +863,35 @@ virDomainDiskDefParseXML(virConnectPtr conn,
 	}
     }
 
+    /* TODO: Should we re-use devaddr for this purpose,
+       or is an extra field justified? */
+    def->controller_pci_addr.domain = -1;
+    def->controller_pci_addr.bus = -1;
+    def->controller_pci_addr.slot = -1;
+
+    if (controller_pci_addr &&
+        sscanf(controller_pci_addr, "%x:%x:%x",
+               &def->controller_pci_addr.domain,
+               &def->controller_pci_addr.bus,
+               &def->controller_pci_addr.slot) < 3) {
+        virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR,
+                             _("Unable to parse <controller> 'pciaddr' parameter '%s'"),
+                             controller_pci_addr);
+        goto error;
+    } 
+    
+    VIR_DEBUG("Controller PCI addr for disk parsed as %d:%d:%d\n",
+              def->controller_pci_addr.domain,
+              def->controller_pci_addr.bus,
+              def->controller_pci_addr.slot);
+
     if (def->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY &&
         def->bus != VIR_DOMAIN_DISK_BUS_FDC) {
         virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR,
                              _("Invalid bus type '%s' for floppy disk"), bus);
         goto error;
     }
+
     if (def->device != VIR_DOMAIN_DISK_DEVICE_FLOPPY &&
         def->bus == VIR_DOMAIN_DISK_BUS_FDC) {
         virDomainReportError(conn, VIR_ERR_INTERNAL_ERROR,
@@ -888,6 +917,9 @@ virDomainDiskDefParseXML(virConnectPtr conn,
         goto error;
     }
 
+    VIR_DEBUG("Disk PCI address parsed as %d:%d:%d\n", 
+              def->pci_addr.domain, def->pci_addr.bus, def->pci_addr.slot);
+
     def->src = source;
     source = NULL;
     def->dst = target;
src/domain_conf.h +1 -1
index 6b3cb09..41df8f6 100644
@@ -107,7 +107,7 @@ struct _virDomainDiskDef {
     int device;
     int bus;          /* Bus type, e.g. scsi or ide */
     int bus_id;       /* Bus number on the controller */
-    int unit_id;      /* Unit on the controller */
+    int unit_id;      /* Unit on the controller (both -1 if unspecified) */
     char *src;
     char *dst;
     char *controller_id;
src/qemu_driver.c +237 -20

301 lines not on this page · raw message

Signature · 1 line
1.6.4
#6 lore raw

PATCH 5/9 Implement controller hotplugging

In reply to Wolfgang Mauerer

This enables to hot-add disk controllers without attached disks into the system. Previously, it was only possible to (implicitly) add disk controllers in the static machine configuration.

Notice that the actual functionality is only available for qemu at present, but other emulators can be extended likewise.

---
 src/domain_conf.c        |   26 +++++++++++++++++++++++---
 src/domain_conf.h        |    2 ++
 src/libvirt_private.syms |    1 +
 src/qemu_driver.c        |   21 +++++++++++++++++----
 4 files changed, 43 insertions(+), 7 deletions(-)
src/domain_conf.c +23 -3
index d0fda64..ea51fda 100644
@@ -647,7 +647,6 @@ void virDomainRemoveInactive(virDomainObjListPtr doms,
 
 }
 
-
 /* Parse the XML definition for a disk
  * @param node XML nodeset to parse for disk definition
  */
@@ -2554,6 +2553,27 @@ virDomainDeviceDefPtr virDomainDeviceDefParse(virConnectPtr conn,
 #endif
 
 
+static int virDomainControllerCompare(virDomainControllerDefPtr a,
+                                      virDomainControllerDefPtr b) {
+    if (a->pci_addr.bus == b->pci_addr.bus) {
+        if (a->pci_addr.domain == b->pci_addr.domain) 
+            return a->pci_addr.slot - b->pci_addr.slot;
+
+        return a->pci_addr.domain - b->pci_addr.domain;
+    }
+
+    return a->pci_addr.bus - b->pci_addr.bus;
+}
+
+
+int virDomainControllerQSort(const void *a, const void *b)
+{
+    const virDomainControllerDefPtr *da = a;
+    const virDomainControllerDefPtr *db = b;
+
+    return virDomainControllerCompare(*da, *db);
+}
+
 int virDomainDiskInsert(virDomainDefPtr def,
                         virDomainDiskDefPtr disk)
 {
@@ -2935,8 +2955,8 @@ static virDomainDefPtr virDomainDefParseXML(virConnectPtr conn,
 
         def->controllers[def->ncontrollers++] = controller;
     }
-    /*    qsort(def->controllers, def->ncontrollers, sizeof(*def->controllers),
-          virDomainControllerQSort); */
+    qsort(def->controllers, def->ncontrollers, sizeof(*def->controllers),
+          virDomainControllerQSort);
     VIR_FREE(nodes);
 
 
src/domain_conf.h +2 -0
index 41df8f6..17f8b14 100644
@@ -728,6 +728,8 @@ int virDomainDiskInsert(virDomainDefPtr def,
 void virDomainDiskInsertPreAlloced(virDomainDefPtr def,
                                    virDomainDiskDefPtr disk);
 
+int virDomainControllerQSort(const void *a, const void *b);
+
 int virDomainSaveXML(virConnectPtr conn,
                      const char *configDir,
                      virDomainDefPtr def,
src/libvirt_private.syms +1 -0
index f724493..ecc1123 100644
@@ -86,6 +86,7 @@ virDomainDiskDefFree;
 virDomainDiskDeviceTypeToString;
 virDomainDiskInsert;
 virDomainDiskInsertPreAlloced;
+virDomainControllerQSort;
 virDomainFindByID;
 virDomainFindByName;
 virDomainFindByUUID;
src/qemu_driver.c +17 -4
index 4a30615..3bdd2d7 100644
@@ -5424,6 +5424,15 @@ try_command:
 	bus    = dev->data.disk->controller_pci_addr.bus;
 	slot   = dev->data.disk->controller_pci_addr.slot;
 
+        if (dev->data.disk->controller_id) {
+            /* TODO: Obtain the PCI address of the controller
+               from the data structures using the ID */
+        } else {
+            domain = dev->data.disk->controller_pci_addr.domain;
+            bus    = dev->data.disk->controller_pci_addr.bus;
+            slot   = dev->data.disk->controller_pci_addr.slot;
+	}
+
         if (dev->data.disk->bus_id != -1) {
             virBufferVSprintf(&buf, ",bus=%d", dev->data.disk->bus_id);
         }
@@ -5582,6 +5591,8 @@ try_command:
 
     VIR_FREE(cmd);
 
+    /* Naturally, the controller hotplug reply is identical with
+       any other PCI hotplug reply */
     if (qemudParsePciAddReply(vm, reply, &domain, &bus, &slot) < 0) {
         if (!tryOldSyntax && strstr(reply, "invalid char in expression")) {
 	    VIR_FREE(reply);
@@ -5596,17 +5607,17 @@ try_command:
     }
 	
     /* Also fill in when the address was explicitely specified in
-       case qemu changed it (TODO: Can this really happen?) */
+       case qemu changed it */
     dev->data.controller->pci_addr.domain = domain;
     dev->data.controller->pci_addr.bus    = bus;
     dev->data.controller->pci_addr.slot   = slot;
 
     VIR_FREE(reply);
 
-    /* NOTE: Sort function is added in a later commit */
     vm->def->controllers[vm->def->ncontrollers++] = dev->data.controller;
-    /*    qsort(vm->def->disks, vm->def->ndisks, sizeof(*vm->def->disks),
-    virDomainDiskQSort);*/
+    qsort(vm->def->controllers, vm->def->ncontrollers, 
+          sizeof(*vm->def->controllers),
+          virDomainControllerQSort);
 
     return 0;
 }
@@ -6104,6 +6115,8 @@ static int qemudDomainAttachDevice(virDomainPtr dom,
             virCgroupDenyDevicePath(cgroup,
                                     dev->data.disk->src);
         }
+    } else if (dev->type == VIR_DOMAIN_DEVICE_CONTROLLER) {
+        ret = qemudDomainAttachDiskController(dom->conn, vm, dev);
     } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
         ret = qemudDomainAttachNetDevice(dom->conn, driver, vm, dev, qemuCmdFlags);
     } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
Signature · 1 line
1.6.4
#7 lore raw

PATCH 6/9 Allow controller selection by ID

In reply to Wolfgang Mauerer

... by simply traversing the list of controllers
to find the associated PCI address.

---
 src/qemu_driver.c |   41 +++++++++++++++++++++++++++++++----------
 1 files changed, 31 insertions(+), 10 deletions(-)
src/qemu_driver.c +31 -10
index 3bdd2d7..990f05a 100644
@@ -5418,19 +5418,40 @@ try_command:
     }
 
     if (controller_specified) {
-        /* NOTE: Proper check for the controller will be implemented
-           in a later commit */
-	domain = dev->data.disk->controller_pci_addr.domain;
-	bus    = dev->data.disk->controller_pci_addr.bus;
-	slot   = dev->data.disk->controller_pci_addr.slot;
-
         if (dev->data.disk->controller_id) {
-            /* TODO: Obtain the PCI address of the controller
-               from the data structures using the ID */
+            for (i = 0 ; i < vm->def->ncontrollers ; i++) {
+                if (STREQ(dev->data.disk->controller_id, 
+                          vm->def->controllers[i]->id)) {
+                    break;
+                }
+            }
+
+            if (i == vm->def->ncontrollers) {
+                qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                                 _("Controller does not exist"));
+                return -1;
+            }
+
+            domain = vm->def->controllers[i]->pci_addr.domain;
+            bus    = vm->def->controllers[i]->pci_addr.bus;
+            slot   = vm->def->controllers[i]->pci_addr.slot;
         } else {
             domain = dev->data.disk->controller_pci_addr.domain;
             bus    = dev->data.disk->controller_pci_addr.bus;
             slot   = dev->data.disk->controller_pci_addr.slot;
+
+            for (i = 0 ; i < vm->def->ncontrollers ; i++) {
+                if (domain ==  vm->def->controllers[i]->pci_addr.domain &&
+                    bus    ==  vm->def->controllers[i]->pci_addr.bus &&
+                    slot   ==  vm->def->controllers[i]->pci_addr.slot)
+                    break;
+            }
+
+            if (i == vm->def->ncontrollers) {
+                qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                                 _("Controller does not exist"));
+                return -1;
+            }
 	}
 
         if (dev->data.disk->bus_id != -1) {
@@ -5457,13 +5478,13 @@ try_command:
         ret = virAsprintf(&cmd, "pci_add %s storage file=%s,if=%s",
                    (tryOldSyntax ? "0": "pci_addr=auto"), safe_path, type);
     }
-
+    
     VIR_FREE(safe_path);
     if (ret == -1) {
         virReportOOMError(conn);
         return ret;
     }
-
+    
     if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
         qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                          _("cannot attach %s disk"), type);
Signature · 1 line
1.6.4
#8 lore raw

PATCH 7/9 Remove surprises in the semantics of disk-hotadd

In reply to Wolfgang Mauerer

When a disk is added without an explicitly specified controller as host, then try to find the first available controller. If none exists, do not (as in previous versions) add a new PCI controller device with the disk attached, but bail out with an error. Notice that this patch changes the behaviour as compared to older libvirt releases, as has been discussed on the mailing list (see
http://thread.gmane.org/gmane.comp.emulators.libvirt/15860)

---
 src/qemu_driver.c |  172 ++++++++++++++++++++++++++---------------------------
 1 files changed, 85 insertions(+), 87 deletions(-)
src/qemu_driver.c +84 -86
index 990f05a..f1c2a45 100644
@@ -5417,68 +5417,81 @@ try_command:
         controller_specified = 1;
     }
 
-    if (controller_specified) {
-        if (dev->data.disk->controller_id) {
-            for (i = 0 ; i < vm->def->ncontrollers ; i++) {
-                if (STREQ(dev->data.disk->controller_id, 
-                          vm->def->controllers[i]->id)) {
-                    break;
-                }
-            }
-
-            if (i == vm->def->ncontrollers) {
-                qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                                 _("Controller does not exist"));
-                return -1;
-            }
+    if (!controller_specified) {
+        /* Find an appropriate controller for disk-hotadd. Notice that
+           the admissible controller types (SCSI, virtio) have already
+           been checked in qemudDomainAttachDevice. */
+        for (i = 0 ; i < vm->def->ncontrollers ; i++) {
+            if (vm->def->controllers[i]->type ==  dev->data.disk->type)
+                break;
+        }
+        
+        if (i == vm->def->ncontrollers) {
+            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                          _("Cannot find appropriate controller for hot-add"));
+            return -1;
+        }
 
-            domain = vm->def->controllers[i]->pci_addr.domain;
-            bus    = vm->def->controllers[i]->pci_addr.bus;
-            slot   = vm->def->controllers[i]->pci_addr.slot;
-        } else {
-            domain = dev->data.disk->controller_pci_addr.domain;
-            bus    = dev->data.disk->controller_pci_addr.bus;
-            slot   = dev->data.disk->controller_pci_addr.slot;
-
-            for (i = 0 ; i < vm->def->ncontrollers ; i++) {
-                if (domain ==  vm->def->controllers[i]->pci_addr.domain &&
-                    bus    ==  vm->def->controllers[i]->pci_addr.bus &&
-                    slot   ==  vm->def->controllers[i]->pci_addr.slot)
-                    break;
-            }
+        domain = vm->def->controllers[i]->pci_addr.domain;
+        bus    = vm->def->controllers[i]->pci_addr.bus;
+        slot   = vm->def->controllers[i]->pci_addr.slot;
+    }
 
-            if (i == vm->def->ncontrollers) {
-                qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                                 _("Controller does not exist"));
-                return -1;
+    if (controller_specified && dev->data.disk->controller_id) {
+        for (i = 0 ; i < vm->def->ncontrollers ; i++) {
+            if (STREQ(dev->data.disk->controller_id, 
+                      vm->def->controllers[i]->id)) {
+                break;
             }
-	}
-
-        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);
+        if (i == vm->def->ncontrollers) {
+            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                             _("Controller does not exist"));
+            return -1;
+        }
+        
+        domain = vm->def->controllers[i]->pci_addr.domain;
+        bus    = vm->def->controllers[i]->pci_addr.bus;
+        slot   = vm->def->controllers[i]->pci_addr.slot;
+    } else if (controller_specified) {
+        domain = dev->data.disk->controller_pci_addr.domain;
+        bus    = dev->data.disk->controller_pci_addr.bus;
+        slot   = dev->data.disk->controller_pci_addr.slot;
+        
+        for (i = 0 ; i < vm->def->ncontrollers ; i++) {
+            if (domain ==  vm->def->controllers[i]->pci_addr.domain &&
+                bus    ==  vm->def->controllers[i]->pci_addr.bus &&
+                slot   ==  vm->def->controllers[i]->pci_addr.slot)
+                break;
         }
+        
+        if (i == vm->def->ncontrollers) {
+            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                             _("Controller does not exist"));
+            return -1;
+        }
+    }
 
-        bus_unit_string = virBufferContentAndReset(&buf);
-
-        VIR_DEBUG ("%s: drive_add %d:%d:%d file=%s,if=%s%s", vm->def->name,
-                   domain, bus, slot, safe_path, type, bus_unit_string);
-        ret = virAsprintf(&cmd, "drive_add %s%d:%d:%d file=%s,if=%s%s",
-			  (tryOldSyntax ? "" : "pci_addr="), domain, bus, 
-                          slot, safe_path, type, bus_unit_string);
+    /* At this point, we have a valid (domain, bus, slot) triple
+       that identifies the controller, regardless if an explicit
+       controller has been given or not. */
+    if (dev->data.disk->bus_id != -1) {
+        virBufferVSprintf(&buf, ",bus=%d", dev->data.disk->bus_id);
     }
-    else {
-        /* Old-style behaviour: If no <controller> reference is given, then
-           hotplug a new controller with the disk attached. */
-        VIR_DEBUG ("%s: pci_add %s storage file=%s,if=%s", vm->def->name,
-                   (tryOldSyntax ? "0": "pci_addr=auto"), safe_path, type);
-        ret = virAsprintf(&cmd, "pci_add %s storage file=%s,if=%s",
-                   (tryOldSyntax ? "0": "pci_addr=auto"), safe_path, type);
+    
+    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 %d:%d:%d file=%s,if=%s%s", vm->def->name,
+               domain, bus, slot, safe_path, type, bus_unit_string);
+    ret = virAsprintf(&cmd, "drive_add %s%d:%d:%d file=%s,if=%s%s",
+                      (tryOldSyntax ? "" : "pci_addr="), domain, bus, 
+                      slot, safe_path, type, bus_unit_string);
+    
     VIR_FREE(safe_path);
     if (ret == -1) {
         virReportOOMError(conn);
@@ -5494,41 +5507,26 @@ try_command:
 
     VIR_FREE(cmd);
 
-    if (controller_specified) {
-        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;
-	}
-    } else {
-        if (qemudParsePciAddReply(vm, reply, &domain, &bus, &slot) < 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 (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;
Signature · 1 line
1.6.4
#9 lore raw

[Qemu-devel] Re: [libvirt] [PATCH 7/9] Remove surprises in the semantics of disk-hotadd

Quoted 8 lines · Wolfgang Mauerer

When a disk is added without an explicitly specified controller as host, then try to find the first available controller. If none exists, do not (as in previous versions) add a new PCI controller device with the disk attached, but bail out with an error. Notice that this patch changes the behaviour as compared to older libvirt releases, as has been discussed on the mailing list (see
http://thread.gmane.org/gmane.comp.emulators.libvirt/15860)

Yes, I still think is the good way to go

Daniel

Quoted 213 lines
---
 src/qemu_driver.c |  172 ++++++++++++++++++++++++++---------------------------
 1 files changed, 85 insertions(+), 87 deletions(-)
src/qemu_driver.c +84 -86
index 990f05a..f1c2a45 100644
@@ -5417,68 +5417,81 @@ try_command:
         controller_specified = 1;
     }
 
-    if (controller_specified) {
-        if (dev->data.disk->controller_id) {
-            for (i = 0 ; i < vm->def->ncontrollers ; i++) {
-                if (STREQ(dev->data.disk->controller_id, 
-                          vm->def->controllers[i]->id)) {
-                    break;
-                }
-            }
-
-            if (i == vm->def->ncontrollers) {
-                qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                                 _("Controller does not exist"));
-                return -1;
-            }
+    if (!controller_specified) {
+        /* Find an appropriate controller for disk-hotadd. Notice that
+           the admissible controller types (SCSI, virtio) have already
+           been checked in qemudDomainAttachDevice. */
+        for (i = 0 ; i < vm->def->ncontrollers ; i++) {
+            if (vm->def->controllers[i]->type ==  dev->data.disk->type)
+                break;
+        }
+        
+        if (i == vm->def->ncontrollers) {
+            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                          _("Cannot find appropriate controller for hot-add"));
+            return -1;
+        }
 
-            domain = vm->def->controllers[i]->pci_addr.domain;
-            bus    = vm->def->controllers[i]->pci_addr.bus;
-            slot   = vm->def->controllers[i]->pci_addr.slot;
-        } else {
-            domain = dev->data.disk->controller_pci_addr.domain;
-            bus    = dev->data.disk->controller_pci_addr.bus;
-            slot   = dev->data.disk->controller_pci_addr.slot;
-
-            for (i = 0 ; i < vm->def->ncontrollers ; i++) {
-                if (domain ==  vm->def->controllers[i]->pci_addr.domain &&
-                    bus    ==  vm->def->controllers[i]->pci_addr.bus &&
-                    slot   ==  vm->def->controllers[i]->pci_addr.slot)
-                    break;
-            }
+        domain = vm->def->controllers[i]->pci_addr.domain;
+        bus    = vm->def->controllers[i]->pci_addr.bus;
+        slot   = vm->def->controllers[i]->pci_addr.slot;
+    }
 
-            if (i == vm->def->ncontrollers) {
-                qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                                 _("Controller does not exist"));
-                return -1;
+    if (controller_specified && dev->data.disk->controller_id) {
+        for (i = 0 ; i < vm->def->ncontrollers ; i++) {
+            if (STREQ(dev->data.disk->controller_id, 
+                      vm->def->controllers[i]->id)) {
+                break;
             }
-	}
-
-        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);
+        if (i == vm->def->ncontrollers) {
+            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                             _("Controller does not exist"));
+            return -1;
+        }
+        
+        domain = vm->def->controllers[i]->pci_addr.domain;
+        bus    = vm->def->controllers[i]->pci_addr.bus;
+        slot   = vm->def->controllers[i]->pci_addr.slot;
+    } else if (controller_specified) {
+        domain = dev->data.disk->controller_pci_addr.domain;
+        bus    = dev->data.disk->controller_pci_addr.bus;
+        slot   = dev->data.disk->controller_pci_addr.slot;
+        
+        for (i = 0 ; i < vm->def->ncontrollers ; i++) {
+            if (domain ==  vm->def->controllers[i]->pci_addr.domain &&
+                bus    ==  vm->def->controllers[i]->pci_addr.bus &&
+                slot   ==  vm->def->controllers[i]->pci_addr.slot)
+                break;
         }
+        
+        if (i == vm->def->ncontrollers) {
+            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                             _("Controller does not exist"));
+            return -1;
+        }
+    }
 
-        bus_unit_string = virBufferContentAndReset(&buf);
-
-        VIR_DEBUG ("%s: drive_add %d:%d:%d file=%s,if=%s%s", vm->def->name,
-                   domain, bus, slot, safe_path, type, bus_unit_string);
-        ret = virAsprintf(&cmd, "drive_add %s%d:%d:%d file=%s,if=%s%s",
-			  (tryOldSyntax ? "" : "pci_addr="), domain, bus, 
-                          slot, safe_path, type, bus_unit_string);
+    /* At this point, we have a valid (domain, bus, slot) triple
+       that identifies the controller, regardless if an explicit
+       controller has been given or not. */
+    if (dev->data.disk->bus_id != -1) {
+        virBufferVSprintf(&buf, ",bus=%d", dev->data.disk->bus_id);
     }
-    else {
-        /* Old-style behaviour: If no <controller> reference is given, then
-           hotplug a new controller with the disk attached. */
-        VIR_DEBUG ("%s: pci_add %s storage file=%s,if=%s", vm->def->name,
-                   (tryOldSyntax ? "0": "pci_addr=auto"), safe_path, type);
-        ret = virAsprintf(&cmd, "pci_add %s storage file=%s,if=%s",
-                   (tryOldSyntax ? "0": "pci_addr=auto"), safe_path, type);
+    
+    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 %d:%d:%d file=%s,if=%s%s", vm->def->name,
+               domain, bus, slot, safe_path, type, bus_unit_string);
+    ret = virAsprintf(&cmd, "drive_add %s%d:%d:%d file=%s,if=%s%s",
+                      (tryOldSyntax ? "" : "pci_addr="), domain, bus, 
+                      slot, safe_path, type, bus_unit_string);
+    
     VIR_FREE(safe_path);
     if (ret == -1) {
         virReportOOMError(conn);
@@ -5494,41 +5507,26 @@ try_command:
 
     VIR_FREE(cmd);
 
-    if (controller_specified) {
-        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;
-	}
-    } else {
-        if (qemudParsePciAddReply(vm, reply, &domain, &bus, &slot) < 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 (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;
Signature · 6 lines
1.6.4

--
Libvir-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/libvir-list
Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#10 lore raw

PATCH 8/9 Factor out the method to get the PCI address of a controller for a given disk

In reply to Wolfgang Mauerer

We need this multiple times later on.

---
 src/qemu_driver.c |  155 +++++++++++++++++++++++++++++++++--------------------
 1 files changed, 97 insertions(+), 58 deletions(-)
src/qemu_driver.c +97 -58
index f1c2a45..ddc46f6 100644
@@ -5370,44 +5370,20 @@ qemudParseDriveAddReply(virDomainObjPtr vm,
     return 0;
 }
 
-static int qemudDomainAttachDiskDevice(virConnectPtr conn,
-				       virDomainObjPtr vm,
-				       virDomainDeviceDefPtr dev)
-{
-    int ret, i;
-    char *cmd, *reply;
-    char *safe_path;
-    /* NOTE: Later patch will use type of the controller instead
-       of the disk */
-    const char* type = virDomainDiskBusTypeToString(dev->data.disk->bus);
-    int tryOldSyntax = 0;
-    unsigned domain, bus, slot;
-    int bus_id, unit_id;
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
-    char* bus_unit_string;
-    int controller_specified;
-
-    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,
-                           _("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;
-    }
-
-try_command:
-    safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
-    if (!safe_path) {
-        virReportOOMError(conn);
-        return -1;
-    }
+enum {
+    CONTROLLER_FOUND      =  0,
+    NO_CONTROLLER_FOUND   = -1, /* None specified, and none found */
+    CONTROLLER_INEXISTENT = -2, /* Controller specified, but not found */
+};
+static virDomainControllerDefPtr 
+qemudGetDiskController(virDomainObjPtr vm, virDomainDeviceDefPtr dev,
+                       int* ret) {
+    int controller_specified = 0;
+    int domain, bus, slot;
+    virDomainControllerDefPtr conPtr = NULL;
+    int i;
+    *ret = CONTROLLER_FOUND;
 
-    controller_specified = 0;
     /* Partially specified PCI addresses (should not happen, anyway)
        don't count as specification */
     if (dev->data.disk->controller_id != NULL || 
@@ -5422,38 +5398,28 @@ try_command:
            the admissible controller types (SCSI, virtio) have already
            been checked in qemudDomainAttachDevice. */
         for (i = 0 ; i < vm->def->ncontrollers ; i++) {
-            if (vm->def->controllers[i]->type ==  dev->data.disk->type)
+            if (vm->def->controllers[i]->type == dev->data.disk->type)
+                conPtr = vm->def->controllers[i]; 
                 break;
         }
         
-        if (i == vm->def->ncontrollers) {
-            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                          _("Cannot find appropriate controller for hot-add"));
-            return -1;
+        if (!conPtr) {
+            *ret = NO_CONTROLLER_FOUND;
         }
-
-        domain = vm->def->controllers[i]->pci_addr.domain;
-        bus    = vm->def->controllers[i]->pci_addr.bus;
-        slot   = vm->def->controllers[i]->pci_addr.slot;
     }
 
     if (controller_specified && dev->data.disk->controller_id) {
         for (i = 0 ; i < vm->def->ncontrollers ; i++) {
             if (STREQ(dev->data.disk->controller_id, 
                       vm->def->controllers[i]->id)) {
+                conPtr = vm->def->controllers[i]; 
                 break;
             }
         }
         
-        if (i == vm->def->ncontrollers) {
-            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                             _("Controller does not exist"));
-            return -1;
+        if (!conPtr) {
+            *ret = CONTROLLER_INEXISTENT;
         }
-        
-        domain = vm->def->controllers[i]->pci_addr.domain;
-        bus    = vm->def->controllers[i]->pci_addr.bus;
-        slot   = vm->def->controllers[i]->pci_addr.slot;
     } else if (controller_specified) {
         domain = dev->data.disk->controller_pci_addr.domain;
         bus    = dev->data.disk->controller_pci_addr.bus;
@@ -5463,19 +5429,92 @@ try_command:
             if (domain ==  vm->def->controllers[i]->pci_addr.domain &&
                 bus    ==  vm->def->controllers[i]->pci_addr.bus &&
                 slot   ==  vm->def->controllers[i]->pci_addr.slot)
+                conPtr = vm->def->controllers[i];
                 break;
         }
         
-        if (i == vm->def->ncontrollers) {
+        if (!conPtr) {
+            *ret = CONTROLLER_INEXISTENT;
+        }
+    }
+
+    return conPtr;
+}
+
+static int qemudDomainAttachDiskDevice(virConnectPtr conn,
+				       virDomainObjPtr vm,
+				       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;
+
+    /* Both controller and disk can specify a type like SCSI. While
+       a virtual disk as such is not bound to a specific bus type,
+       the specification is here for historical reasons. When controller
+       and disk type specification disagree, the former takes precedence
+       and we print a warning message, but still add the disk. */
+    if (virDiskHasValidController(dev->data.disk)) {
+        conPtr = qemudGetDiskController(vm, dev, &ret);
+        if (conPtr && dev->data.disk->bus != conPtr->type) {
+            VIR_WARN0(_("Controller and disk bus type disagree, controller " \
+                        "takes precedence\n"));
+            type = virDomainDiskBusTypeToString(conPtr->type);
+        }
+    }
+    if (!type) {
+        type = virDomainDiskBusTypeToString(dev->data.disk->bus);
+    }
+
+    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,
+                           _("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;
+    }
+
+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) {
+        case CONTROLLER_INEXISTENT:
             qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
                              _("Controller does not exist"));
             return -1;
+            break;  /* A bit pointless */
+        case NO_CONTROLLER_FOUND:
+            qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+                          _("Cannot find appropriate controller for hot-add"));
+            return -1;
+            break;
         }
     }
 
-    /* At this point, we have a valid (domain, bus, slot) triple
-       that identifies the controller, regardless if an explicit
-       controller has been given or not. */
+    /* At this point, we have a valid controller, regardless if an explicit
+       controller has been specified or not. */
+    domain = conPtr->pci_addr.domain;
+    bus = conPtr->pci_addr.bus;
+    slot = conPtr->pci_addr.slot;
+
     if (dev->data.disk->bus_id != -1) {
         virBufferVSprintf(&buf, ",bus=%d", dev->data.disk->bus_id);
     }
Signature · 1 line
1.6.4
#11 lore raw

PATCH 9/9 Implement disk- and controller hotremove

In reply to Wolfgang Mauerer

Since both disks and disk controllers can be dynamically added to the system, it makes sense to be also able to remove them.

---
 src/domain_conf.h |    8 +++
 src/qemu_driver.c |  161 +++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 153 insertions(+), 16 deletions(-)
src/domain_conf.h +8 -0
index 17f8b14..c7d49cf 100644
@@ -149,6 +149,14 @@ virDiskHasValidPciAddr(virDomainDiskDefPtr def)
     return def->pci_addr.domain || def->pci_addr.domain || def->pci_addr.slot;
 }
 
+static inline int
+virDiskHasValidController(virDomainDiskDefPtr def)
+{
+    return def->controller_id != NULL ||
+        def->controller_pci_addr.domain || def->controller_pci_addr.domain 
+        || def->controller_pci_addr.slot;
+}
+
 
 /* Two types of disk backends */
 enum virDomainFSType {
src/qemu_driver.c +145 -16

209 lines not on this page · raw message

Signature · 1 line
1.6.4

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw
Quoted 14 lines · Daniel Veillard
Quoted 1 line · Wolfgang Mauerer

Hi,

Hi Wolfgang,

Quoted 8 lines

this patch reworks libvirt's disk-hotadd support and introduces support for disk controller hotplugging and disk-hotremove (see http://thread.gmane.org/gmane.comp.emulators.libvirt/15860 for more details). Currently, it targets only qemu and also requires some additions to qemu that have only recently been submitted, which is why this is cross-posted to qemu-devel. Hopefully the lack of support in qemu does not prevent the community from reviewing the code, though ;-)

Quoted 4 lines

Could you update us on the status for those patches, is there
any chance to get this in QEmu upstream ? I have only seen feddback from Gerd Hoffmann on that thread but maybe I'm missing some of the action !

Note: upstream qemu got device_add and device_del monitor commands meanwhile which can be used to hotplug devices.

device_add accepts the same syntax the -device command line switch expects.

device_del expectes an id.

For libvirt it probably makes sense to start supporting this when switching over to -device (probably for qemu 0.12+).

cheers,
Gerd

#14 lore raw

Hi,

Quoted 13 lines · Gerd Hoffmann
Quoted 12 lines · Daniel Veillard
Quoted 1 line · Wolfgang Mauerer

Hi,

Hi Wolfgang,

Quoted 8 lines

this patch reworks libvirt's disk-hotadd support and introduces support for disk controller hotplugging and disk-hotremove (see http://thread.gmane.org/gmane.comp.emulators.libvirt/15860 for more details). Currently, it targets only qemu and also requires some additions to qemu that have only recently been submitted, which is why this is cross-posted to qemu-devel. Hopefully the lack of support in qemu does not prevent the community from reviewing the code, though ;-)

Quoted 4 lines
Quoted 4 lines

Could you update us on the status for those patches, is there
any chance to get this in QEmu upstream ? I have only seen feddback from Gerd Hoffmann on that thread but maybe I'm missing some of the action !

sorry, I did not get that mail for some reason, so I did not reply.

Quoted 10 lines

Note: upstream qemu got device_add and device_del monitor commands meanwhile which can be used to hotplug devices.

device_add accepts the same syntax the -device command line switch expects.

device_del expectes an id.

For libvirt it probably makes sense to start supporting this when switching over to -device (probably for qemu 0.12+).

Since it does most likely not make sense to include my drive_del implementation into qemu 0.11 mainline, I'll add libvirt support for these after I finish doing the structural changes suggested by Daniel Berrange.

Cheers, Wolfgang