[libvirt] [PATCH] [RFC] libxl: add discard support to libxl_device_disk

Translate libvirt discard settings into libxl-4.5 discard settings. It makes use of upcoming changes for xen-4.5: http://lists.xenproject.org/archives/html/xen-devel/2014-01/msg02632.html This patch is not compile tested. How is a config file supposed to look like to make use of a discard=on|off flag for a given disk image? Signed-off-by: Olaf Hering <olaf@aepfle.de> Cc: Jim Fehlig <jfehlig@suse.com> --- src/libxl/libxl_conf.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index 4cefadf..8fc5e75 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -707,6 +707,26 @@ error: return -1; } +static void +libxlDiskSetDiscard(libxl_device_disk *x_disk, int discard) +{ + if (!x_disk->readwrite) + return; +#if defined(LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_ENABLE) + /* libxl 4.5 */ + switch (discard) { + case VIR_DOMAIN_DISK_DISCARD_DEFAULT: + case VIR_DOMAIN_DISK_DISCARD_UNMAP: + x_disk->discard_enable = 1; + break; + default: + case VIR_DOMAIN_DISK_DISCARD_IGNORE: + x_disk->discard_enable = 0; + break; + } +#endif +} + int libxlMakeDisk(virDomainDiskDefPtr l_disk, libxl_device_disk *x_disk) { @@ -817,6 +837,7 @@ libxlMakeDisk(virDomainDiskDefPtr l_disk, libxl_device_disk *x_disk) x_disk->removable = 1; x_disk->readwrite = !l_disk->readonly; x_disk->is_cdrom = l_disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM ? 1 : 0; + libxlDiskSetDiscard(x_disk, l_disk->discard); if (l_disk->transient) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("libxenlight does not support transient disks"));

On Thu, Jan 30, 2014 at 09:35:13PM +0100, Olaf Hering wrote:
Translate libvirt discard settings into libxl-4.5 discard settings. It makes use of upcoming changes for xen-4.5: http://lists.xenproject.org/archives/html/xen-devel/2014-01/msg02632.html
This patch is not compile tested. How is a config file supposed to look like to make use of a discard=on|off flag for a given disk image?
Here's two examples: <disk type='file' device='disk'> <driver name='qemu' type='qcow2' discard='unmap'/> <source file='/var/lib/libvirt/images/f14.img'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu' type='raw' discard='ignore'/> <source file='/var/lib/libvirt/Fedora-14-x86_64-Live-KDE.iso'/> <target dev='hdc' bus='ide'/> <readonly/> <address type='drive' controller='0' bus='1' target='0' unit='0'/> </disk>
Signed-off-by: Olaf Hering <olaf@aepfle.de> Cc: Jim Fehlig <jfehlig@suse.com> --- src/libxl/libxl_conf.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index 4cefadf..8fc5e75 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -707,6 +707,26 @@ error: return -1; }
+static void +libxlDiskSetDiscard(libxl_device_disk *x_disk, int discard) +{ + if (!x_disk->readwrite) + return; +#if defined(LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_ENABLE) + /* libxl 4.5 */ + switch (discard) { + case VIR_DOMAIN_DISK_DISCARD_DEFAULT: + case VIR_DOMAIN_DISK_DISCARD_UNMAP: + x_disk->discard_enable = 1; + break; + default: + case VIR_DOMAIN_DISK_DISCARD_IGNORE: + x_disk->discard_enable = 0; + break; + } +#endif
In the case where LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_ENABLE is not defined, then you should call virReportError with the code VIR_ERR_CONFIG_UNSUPPORTED, if discard != VIR_DOMAIN_DISK_DISCARD_DEFAULT This ensures apps get an error if they had requested discard instead of being silently ignored. Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On Thu, Jan 30, Daniel P. Berrange wrote:
Here's two examples:
Thanks, this worked for me: <disk type='file' device='disk'> <driver name='qemu' type='raw' discard='ignore'/> <source file='/disk0.raw'/> <target dev='xvda' bus='xen'/> </disk> <disk type='file' device='disk'> <driver name='qemu' type='raw' discard='unmap'/> <source file='/disk0.file'/> <target dev='xvdb' bus='xen'/> </disk>
In the case where LIBXL_HAVE_LIBXL_DEVICE_DISK_DISCARD_ENABLE is not defined, then you should call virReportError with the code VIR_ERR_CONFIG_UNSUPPORTED, if discard != VIR_DOMAIN_DISK_DISCARD_DEFAULT This ensures apps get an error if they had requested discard instead of being silently ignored.
I will test this approach and resend. Olaf
participants (2)
-
Daniel P. Berrange
-
Olaf Hering