From: Hyman Huang(黄勇) <yong.huang(a)smartx.com>
Generate cmd line for virtio-blk.discard and virtio-blk.write-zeroes
properties. Also, validate that the requested feature is supported by QEMU.
Signed-off-by: Hyman Huang(黄勇) <yong.huang(a)smartx.com>
---
src/qemu/qemu_command.c | 2 +
src/qemu/qemu_validate.c | 14 +++++
.../disk-virtio-discard.x86_64-latest.args | 44 +++++++++++++++
.../qemuxml2argvdata/disk-virtio-discard.xml | 56 +++++++++++++++++++
tests/qemuxml2argvtest.c | 1 +
5 files changed, 117 insertions(+)
create mode 100644 tests/qemuxml2argvdata/disk-virtio-discard.x86_64-latest.args
create mode 100644 tests/qemuxml2argvdata/disk-virtio-discard.xml
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index ad224571f3..6954fc5d75 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1834,6 +1834,8 @@ qemuBuildDiskDeviceProps(const virDomainDef *def,
"T:scsi", scsi,
"p:num-queues", disk->queues,
"p:queue-size", disk->queue_size,
+ "T:discard", disk->virtio_discard,
+ "T:write-zeroes",
disk->virtio_write_zeroes,
NULL) < 0)
return NULL;
}
diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c
index 7e09e2c52f..5abe82e1a5 100644
--- a/src/qemu/qemu_validate.c
+++ b/src/qemu/qemu_validate.c
@@ -2989,6 +2989,20 @@ qemuValidateDomainDeviceDefDiskFrontend(const virDomainDiskDef
*disk,
_("queue-size property isn't supported by this QEMU
binary"));
return -1;
}
+ if (disk->virtio_discard &&
+ !virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BLK_DISCARD)) {
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("discard property for virtio-blk device isn't
supported "
+ "by this QEMU binary"));
+ return -1;
+ }
+ if (disk->virtio_write_zeroes &&
+ !virQEMUCapsGet(qemuCaps, QEMU_CAPS_VIRTIO_BLK_WRITE_ZEROES)) {
+ virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+ _("write_zeroes property for virtio-blk device isn't
"
+ "supported by this QEMU binary"));
+ return -1;
+ }
break;
case VIR_DOMAIN_DISK_BUS_USB:
diff --git a/tests/qemuxml2argvdata/disk-virtio-discard.x86_64-latest.args
b/tests/qemuxml2argvdata/disk-virtio-discard.x86_64-latest.args
new file mode 100644
index 0000000000..30596a57a5
--- /dev/null
+++ b/tests/qemuxml2argvdata/disk-virtio-discard.x86_64-latest.args
@@ -0,0 +1,44 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/var/lib/libvirt/qemu/domain--1-QEMUGuest1 \
+USER=test \
+LOGNAME=test \
+XDG_DATA_HOME=/var/lib/libvirt/qemu/domain--1-QEMUGuest1/.local/share \
+XDG_CACHE_HOME=/var/lib/libvirt/qemu/domain--1-QEMUGuest1/.cache \
+XDG_CONFIG_HOME=/var/lib/libvirt/qemu/domain--1-QEMUGuest1/.config \
+/usr/bin/qemu-system-x86_64 \
+-name guest=QEMUGuest1,debug-threads=on \
+-S \
+-object
'{"qom-type":"secret","id":"masterKey0","format":"raw","file":"/var/lib/libvirt/qemu/domain--1-QEMUGuest1/master-key.aes"}'
\
+-machine pc,usb=off,dump-guest-core=off,memory-backend=pc.ram,acpi=off \
+-accel tcg \
+-cpu qemu64 \
+-m size=219136k \
+-object
'{"qom-type":"memory-backend-ram","id":"pc.ram","size":224395264}'
\
+-overcommit mem-lock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,fd=1729,server=on,wait=off \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-boot strict=on \
+-device
'{"driver":"piix3-usb-uhci","id":"usb","bus":"pci.0","addr":"0x1.0x2"}'
\
+-blockdev
'{"driver":"file","filename":"/tmp/data.img","node-name":"libvirt-4-storage","auto-read-only":true,"discard":"unmap"}'
\
+-blockdev
'{"node-name":"libvirt-4-format","read-only":false,"driver":"raw","file":"libvirt-4-storage"}'
\
+-device
'{"driver":"virtio-blk-pci","discard":true,"write-zeroes":true,"bus":"pci.0","addr":"0x3","drive":"libvirt-4-format","id":"virtio-disk0","bootindex":1}'
\
+-blockdev
'{"driver":"file","filename":"/tmp/data1.img","node-name":"libvirt-3-storage","auto-read-only":true,"discard":"unmap"}'
\
+-blockdev
'{"node-name":"libvirt-3-format","read-only":false,"driver":"raw","file":"libvirt-3-storage"}'
\
+-device
'{"driver":"virtio-blk-pci","discard":true,"write-zeroes":false,"bus":"pci.0","addr":"0x4","drive":"libvirt-3-format","id":"virtio-disk1"}'
\
+-blockdev
'{"driver":"file","filename":"/tmp/data2.img","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"}'
\
+-blockdev
'{"node-name":"libvirt-2-format","read-only":false,"driver":"raw","file":"libvirt-2-storage"}'
\
+-device
'{"driver":"virtio-blk-pci","discard":false,"write-zeroes":true,"bus":"pci.0","addr":"0x5","drive":"libvirt-2-format","id":"virtio-disk2"}'
\
+-blockdev
'{"driver":"file","filename":"/tmp/data3.img","node-name":"libvirt-1-storage","auto-read-only":true,"discard":"unmap"}'
\
+-blockdev
'{"node-name":"libvirt-1-format","read-only":false,"driver":"raw","file":"libvirt-1-storage"}'
\
+-device
'{"driver":"virtio-blk-pci","discard":false,"write-zeroes":false,"bus":"pci.0","addr":"0x6","drive":"libvirt-1-format","id":"virtio-disk3"}'
\
+-audiodev
'{"id":"audio1","driver":"none"}' \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/disk-virtio-discard.xml
b/tests/qemuxml2argvdata/disk-virtio-discard.xml
new file mode 100644
index 0000000000..54d2a56d18
--- /dev/null
+++ b/tests/qemuxml2argvdata/disk-virtio-discard.xml
@@ -0,0 +1,56 @@
+<domain type='qemu'>
+ <name>QEMUGuest1</name>
+ <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+ <memory unit='KiB'>219136</memory>
+ <currentMemory unit='KiB'>219136</currentMemory>
+ <vcpu placement='static'>1</vcpu>
+ <os>
+ <type arch='x86_64' machine='pc'>hvm</type>
+ <boot dev='hd'/>
+ </os>
+ <cpu mode='custom' match='exact' check='none'>
+ <model fallback='forbid'>qemu64</model>
+ </cpu>
+ <clock offset='utc'/>
+ <on_poweroff>destroy</on_poweroff>
+ <on_reboot>restart</on_reboot>
+ <on_crash>destroy</on_crash>
+ <devices>
+ <emulator>/usr/bin/qemu-system-x86_64</emulator>
+ <disk type='file' device='disk'>
+ <driver name='qemu' type='raw' virtio_discard='on'
virtio_write_zeroes='on'/>
+ <source file='/tmp/data.img'/>
+ <target dev='vda' bus='virtio'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x03' function='0x0'/>
+ </disk>
+ <disk type='file' device='disk'>
+ <driver name='qemu' type='raw' virtio_discard='on'
virtio_write_zeroes='off'/>
+ <source file='/tmp/data1.img'/>
+ <target dev='vdb' bus='virtio'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x04' function='0x0'/>
+ </disk>
+ <disk type='file' device='disk'>
+ <driver name='qemu' type='raw' virtio_discard='off'
virtio_write_zeroes='on'/>
+ <source file='/tmp/data2.img'/>
+ <target dev='vdc' bus='virtio'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x05' function='0x0'/>
+ </disk>
+ <disk type='file' device='disk'>
+ <driver name='qemu' type='raw' virtio_discard='off'
virtio_write_zeroes='off'/>
+ <source file='/tmp/data3.img'/>
+ <target dev='vdd' bus='virtio'/>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x06' function='0x0'/>
+ </disk>
+ <controller type='usb' index='0' model='piix3-uhci'>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x01' function='0x2'/>
+ </controller>
+ <controller type='ide' index='0'>
+ <address type='pci' domain='0x0000' bus='0x00'
slot='0x01' function='0x1'/>
+ </controller>
+ <controller type='pci' index='0' model='pci-root'/>
+ <input type='mouse' bus='ps2'/>
+ <input type='keyboard' bus='ps2'/>
+ <audio id='1' type='none'/>
+ <memballoon model='none'/>
+ </devices>
+</domain>
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 534eb9e699..9c7ff63ff1 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -1420,6 +1420,7 @@ mymain(void)
DO_TEST_CAPS_LATEST("net-vdpa");
DO_TEST_CAPS_LATEST("net-vdpa-multiqueue");
DO_TEST_CAPS_LATEST("net-virtio-rss");
+ DO_TEST_CAPS_LATEST("disk-virtio-discard");
DO_TEST("hostdev-pci-multifunction",
QEMU_CAPS_KVM,
--
2.38.5