Qemu since 1.5 supports discard. However until qemu 1.6 it didn't
work for qcow2 files. Ref:
http://wiki.qemu.org/ChangeLog/1.5
http://wiki.qemu.org/ChangeLog/1.6
If qemu >= 1.6, add this to guest capabilities:
<features>
<drivediscard/>
<qcow2discard/> <---
</features>
---
src/conf/capabilities.c | 3 ++-
src/qemu/qemu_capabilities.c | 13 +++++++++++++
src/qemu/qemu_capabilities.h | 1 +
tests/qemucapabilitiesdata/caps_1.6.0-1.caps | 1 +
tests/qemucapabilitiesdata/caps_1.6.50-1.caps | 1 +
5 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index ff5912a..08c9b73 100644
--- a/src/conf/capabilities.c
+++ b/src/conf/capabilities.c
@@ -948,7 +948,8 @@ virCapabilitiesFormatXML(virCapsPtr caps)
STREQ(caps->guests[i]->features[j]->name,
"ia64_be") ||
STREQ(caps->guests[i]->features[j]->name,
"cpuselection") ||
STREQ(caps->guests[i]->features[j]->name,
"deviceboot") ||
- STREQ(caps->guests[i]->features[j]->name,
"drivediscard")) {
+ STREQ(caps->guests[i]->features[j]->name,
"drivediscard") ||
+ STREQ(caps->guests[i]->features[j]->name,
"qcow2discard")) {
virBufferAsprintf(&xml, " <%s/>\n",
caps->guests[i]->features[j]->name);
} else {
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index b755fb2..b527690 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -251,6 +251,7 @@ VIR_ENUM_IMPL(virQEMUCaps, QEMU_CAPS_LAST,
"spiceport",
"usb-kbd", /* 165 */
+ "qcow2-discard",
);
struct _virQEMUCaps {
@@ -782,6 +783,10 @@ virQEMUCapsInitGuest(virCapsPtr caps,
!virCapabilitiesAddGuestFeature(guest, "drivediscard", 0, 0))
goto error;
+ if (virQEMUCapsGet(qemubinCaps, QEMU_CAPS_QCOW2_DISCARD) &&
+ !virCapabilitiesAddGuestFeature(guest, "qcow2discard", 0, 0))
+ goto error;
+
if (virCapabilitiesAddGuestDomain(guest,
"qemu",
NULL,
@@ -1113,6 +1118,10 @@ virQEMUCapsComputeCmdFlags(const char *help,
if (strstr(help, "-machine"))
virQEMUCapsSet(qemuCaps, QEMU_CAPS_MACHINE_OPT);
+ /* discard option works for qcow2 backing files since 1.6 */
+ if (qemuCaps->version >= 1006000)
+ virQEMUCapsSet(qemuCaps, QEMU_CAPS_QCOW2_DISCARD);
+
/* USB option is supported v1.3.0 onwards */
if (qemuCaps->version >= 1003000)
virQEMUCapsSet(qemuCaps, QEMU_CAPS_MACHINE_USB_OPT);
@@ -2569,6 +2578,10 @@ virQEMUCapsInitQMPMonitor(virQEMUCapsPtr qemuCaps,
if (virQEMUCapsInitArchQMPBasic(qemuCaps, mon) < 0)
goto cleanup;
+ /* discard option works for qcow2 backing files since 1.6 */
+ if (qemuCaps->version >= 1006000)
+ virQEMUCapsSet(qemuCaps, QEMU_CAPS_QCOW2_DISCARD);
+
/* USB option is supported v1.3.0 onwards */
if (qemuCaps->version >= 1003000)
virQEMUCapsSet(qemuCaps, QEMU_CAPS_MACHINE_USB_OPT);
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
index b5445e7..a0ce506 100644
--- a/src/qemu/qemu_capabilities.h
+++ b/src/qemu/qemu_capabilities.h
@@ -204,6 +204,7 @@ enum virQEMUCapsFlags {
QEMU_CAPS_SPICE_FILE_XFER_DISABLE = 163, /* -spice disable-agent-file-xfer */
QEMU_CAPS_CHARDEV_SPICEPORT = 164, /* -chardev spiceport */
QEMU_CAPS_DEVICE_USB_KBD = 165, /* -device usb-kbd */
+ QEMU_CAPS_QCOW2_DISCARD = 166, /* discard works for qcow2 backing files */
QEMU_CAPS_LAST, /* this must always be the last item */
};
diff --git a/tests/qemucapabilitiesdata/caps_1.6.0-1.caps
b/tests/qemucapabilitiesdata/caps_1.6.0-1.caps
index 597f873..0ab5e22 100644
--- a/tests/qemucapabilitiesdata/caps_1.6.0-1.caps
+++ b/tests/qemucapabilitiesdata/caps_1.6.0-1.caps
@@ -142,4 +142,5 @@
<flag name='spice-file-xfer-disable'/>
<flag name='spiceport'/>
<flag name='usb-kbd'/>
+ <flag name='qcow2-discard'/>
</qemuCaps>
diff --git a/tests/qemucapabilitiesdata/caps_1.6.50-1.caps
b/tests/qemucapabilitiesdata/caps_1.6.50-1.caps
index 0c1dd87..d5cca41 100644
--- a/tests/qemucapabilitiesdata/caps_1.6.50-1.caps
+++ b/tests/qemucapabilitiesdata/caps_1.6.50-1.caps
@@ -140,4 +140,5 @@
<flag name='spice-file-xfer-disable'/>
<flag name='spiceport'/>
<flag name='usb-kbd'/>
+ <flag name='qcow2-discard'/>
</qemuCaps>
--
1.8.5.3