[libvirt] [PATCH] qemu: Support copy on read for disk

Osier Yang jyang at redhat.com
Thu Dec 22 09:50:02 UTC 2011


Upstream QEMU supports it since commit fb0490f69.
---
 docs/formatdomain.html.in     |    9 +++++++++
 docs/schemas/domaincommon.rng |   11 +++++++++++
 src/conf/domain_conf.c        |   24 +++++++++++++++++++++++-
 src/conf/domain_conf.h        |   10 ++++++++++
 src/libvirt_private.syms      |    2 ++
 src/qemu/qemu_capabilities.c  |    3 +++
 src/qemu/qemu_capabilities.h  |    1 +
 src/qemu/qemu_command.c       |   11 +++++++++++
 8 files changed, 70 insertions(+), 1 deletions(-)

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index 9cf0f12..779317f 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -1187,6 +1187,15 @@
             <b>In general you should leave this option alone, unless you
             are very certain you know what you are doing.</b>
           </li>
+          <li>
+            The optional <code>copy_on_read</code> attribute controls
+            wether to copy read backing file into the image file. The
+            value can be either "on" or "off".
+            Copy-on-read avoids accessing the same backing file sectors
+            repeatedly and is useful when the backing file is over a slow
+            network. By default copy-on-read is off.
+            <span class='since'>Since 0.9.9 (QEMU and KVM only)</span>
+          </li>
         </ul>
       </dd>
       <dt><code>boot</code></dt>
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index 553a6f0..b511ec2 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -959,6 +959,14 @@
       </choice>
     </attribute>
   </define>
+  <define name="copy_on_read">
+    <attribute name='copy_on_read'>
+      <choice>
+        <value>on</value>
+        <value>off</value>
+      </choice>
+    </attribute>
+  </define>
   <define name="controller">
     <element name="controller">
       <choice>
@@ -1308,6 +1316,9 @@
           <optional>
             <ref name="event_idx"/>
           </optional>
+          <optional>
+            <ref name="copy_on_read"/>
+          </optional>
           <empty/>
         </element>
       </optional>
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 2897b4a..a51eb40 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -208,6 +208,11 @@ VIR_ENUM_IMPL(virDomainVirtioEventIdx, VIR_DOMAIN_VIRTIO_EVENT_IDX_LAST,
               "on",
               "off")
 
+VIR_ENUM_IMPL(virDomainDiskCopyOnRead, VIR_DOMAIN_DISK_COPY_ON_READ_LAST,
+              "default",
+              "on",
+              "off")
+
 VIR_ENUM_IMPL(virDomainDiskSnapshot, VIR_DOMAIN_DISK_SNAPSHOT_LAST,
               "default",
               "no",
@@ -2621,6 +2626,7 @@ virDomainDiskDefParseXML(virCapsPtr caps,
     char *iotag = NULL;
     char *ioeventfd = NULL;
     char *event_idx = NULL;
+    char *copy_on_read = NULL;
     char *devaddr = NULL;
     virStorageEncryptionPtr encryption = NULL;
     char *serial = NULL;
@@ -2749,6 +2755,7 @@ virDomainDiskDefParseXML(virCapsPtr caps,
                 iotag = virXMLPropString(cur, "io");
                 ioeventfd = virXMLPropString(cur, "ioeventfd");
                 event_idx = virXMLPropString(cur, "event_idx");
+                copy_on_read = virXMLPropString(cur, "copy_on_read");
             } else if (xmlStrEqual(cur->name, BAD_CAST "auth")) {
                 authUsername = virXMLPropString(cur, "username");
                 if (authUsername == NULL) {
@@ -3053,6 +3060,17 @@ virDomainDiskDefParseXML(virCapsPtr caps,
         def->event_idx = idx;
     }
 
+    if (copy_on_read) {
+        int cor;
+        if ((cor = virDomainDiskCopyOnReadTypeFromString(copy_on_read)) <= 0) {
+            virDomainReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                                 _("unknown disk copy_on_read mode '%s'"),
+                                 copy_on_read);
+            goto error;
+        }
+        def->copy_on_read = cor;
+    }
+
     if (devaddr) {
         if (virDomainParseLegacyDeviceAddress(devaddr,
                                               &def->info.addr.pci) < 0) {
@@ -3146,6 +3164,7 @@ cleanup:
     VIR_FREE(iotag);
     VIR_FREE(ioeventfd);
     VIR_FREE(event_idx);
+    VIR_FREE(copy_on_read);
     VIR_FREE(devaddr);
     VIR_FREE(serial);
     virStorageEncryptionFree(encryption);
@@ -9768,6 +9787,7 @@ virDomainDiskDefFormat(virBufferPtr buf,
     const char *iomode = virDomainDiskIoTypeToString(def->iomode);
     const char *ioeventfd = virDomainIoEventFdTypeToString(def->ioeventfd);
     const char *event_idx = virDomainVirtioEventIdxTypeToString(def->event_idx);
+    const char *copy_on_read = virDomainVirtioEventIdxTypeToString(def->copy_on_read);
     const char *startupPolicy = virDomainStartupPolicyTypeToString(def->startupPolicy);
 
     char uuidstr[VIR_UUID_STRING_BUFLEN];
@@ -9808,7 +9828,7 @@ virDomainDiskDefFormat(virBufferPtr buf,
     virBufferAddLit(buf, ">\n");
 
     if (def->driverName || def->driverType || def->cachemode ||
-        def->ioeventfd || def->event_idx) {
+        def->ioeventfd || def->event_idx || def->copy_on_read) {
         virBufferAddLit(buf, "      <driver");
         if (def->driverName)
             virBufferAsprintf(buf, " name='%s'", def->driverName);
@@ -9826,6 +9846,8 @@ virDomainDiskDefFormat(virBufferPtr buf,
             virBufferAsprintf(buf, " ioeventfd='%s'", ioeventfd);
         if (def->event_idx)
             virBufferAsprintf(buf, " event_idx='%s'", event_idx);
+        if (def->copy_on_read)
+            virBufferAsprintf(buf, " copy_on_read='%s'", copy_on_read);
         virBufferAddLit(buf, "/>\n");
     }
 
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 1f6e442..4575ab2 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -288,6 +288,14 @@ enum virDomainVirtioEventIdx {
     VIR_DOMAIN_VIRTIO_EVENT_IDX_LAST
 };
 
+enum virDomainDiskCopyOnRead {
+    VIR_DOMAIN_DISK_COPY_ON_READ_DEFAULT = 0,
+    VIR_DOMAIN_DISK_COPY_ON_READ_ON,
+    VIR_DOMAIN_DISK_COPY_ON_READ_OFF,
+
+    VIR_DOMAIN_DISK_COPY_ON_READ_LAST
+};
+
 enum virDomainDiskSnapshot {
     VIR_DOMAIN_DISK_SNAPSHOT_DEFAULT = 0,
     VIR_DOMAIN_DISK_SNAPSHOT_NO,
@@ -363,6 +371,7 @@ struct _virDomainDiskDef {
     int iomode;
     int ioeventfd;
     int event_idx;
+    int copy_on_read;
     int snapshot; /* enum virDomainDiskSnapshot */
     int startupPolicy; /* enum virDomainStartupPolicy */
     unsigned int readonly : 1;
@@ -1968,6 +1977,7 @@ VIR_ENUM_DECL(virDomainDiskSecretType)
 VIR_ENUM_DECL(virDomainDiskSnapshot)
 VIR_ENUM_DECL(virDomainIoEventFd)
 VIR_ENUM_DECL(virDomainVirtioEventIdx)
+VIR_ENUM_DECL(virDomainDiskCopyOnRead)
 VIR_ENUM_DECL(virDomainController)
 VIR_ENUM_DECL(virDomainControllerModelSCSI)
 VIR_ENUM_DECL(virDomainControllerModelUSB)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4a86bdc..63c2947 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -290,6 +290,8 @@ virDomainDeviceTypeToString;
 virDomainDiskBusTypeToString;
 virDomainDiskCacheTypeFromString;
 virDomainDiskCacheTypeToString;
+virDomainDiskCopyOnReadTypeFromString;
+virDomainDiskCopyOnReadTypeToString;
 virDomainDiskDefAssignAddress;
 virDomainDiskDefForeachPath;
 virDomainDiskDefFree;
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 43c7578..f32c792 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -144,6 +144,7 @@ VIR_ENUM_IMPL(qemuCaps, QEMU_CAPS_LAST,
               "ich9-ahci",
               "no-acpi",
               "fsdev-readonly",
+              "drive-copy-on-read",
     );
 
 struct qemu_feature_flags {
@@ -1023,6 +1024,8 @@ qemuCapsComputeCmdFlags(const char *help,
             qemuCapsSet(flags, QEMU_CAPS_DRIVE_READONLY);
         if (strstr(help, "aio=threads|native"))
             qemuCapsSet(flags, QEMU_CAPS_DRIVE_AIO);
+        if (strstr(help, "copy-on-read=on|off"))
+            qemuCapsSet(flags, QEMU_CAPS_DRIVE_COPY_ON_READ);
     }
     if ((p = strstr(help, "-vga")) && !strstr(help, "-std-vga")) {
         const char *nl = strstr(p, "\n");
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index c759baf..38106dd 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -117,6 +117,7 @@ enum qemuCapsFlags {
     QEMU_CAPS_ICH9_AHCI         = 77, /* -device ich9-ahci */
     QEMU_CAPS_NO_ACPI		= 78, /* -no-acpi */
     QEMU_CAPS_FSDEV_READONLY    =79, /* -fsdev readonly supported */
+    QEMU_CAPS_DRIVE_COPY_ON_READ = 80, /* -drive copy-on-read */
 
     QEMU_CAPS_LAST,                   /* this must always be the last item */
 };
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index ea1b763..629f13d 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1926,6 +1926,17 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED,
         virBufferAddLit(&opt, ",cache=off");
     }
 
+    if (disk->copy_on_read) {
+        if (qemuCapsGet(qemuCaps, QEMU_CAPS_DRIVE_COPY_ON_READ)) {
+            virBufferAsprintf(&opt, ",copy-on-read=%s",
+                              virDomainDiskCopyOnReadTypeToString(disk->copy_on_read));
+        } else {
+            qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                            _("copy_on_read is not supported by this QEMU binary"));
+            goto error;
+        }
+    }
+
     if (qemuCapsGet(qemuCaps, QEMU_CAPS_MONITOR_JSON)) {
         const char *wpolicy = NULL, *rpolicy = NULL;
 
-- 
1.7.7.3




More information about the libvir-list mailing list