[PATCH v2 0/4] Support for detect-zeroes RHBZ#1130506
This time with support for libvirt. Patch for libvirt is on libvir-list@redhat.com (qemu: detect-zeroes feature for drives) See: https://lists.gnu.org/archive/html/qemu-devel/2012-07/msg03747.html Related do RHBZ#1130506. Maros Zatko (4): api: add_drive - add QEMU detect-zeroes support sparsify: support QEMU detect-zeroes in copying mode (RHBZ#1130506) libvirt backend: add detect-zeroes direct backend: add version check for detect-zeroes feature generator/actions.ml | 2 +- sparsify/copying.ml | 3 ++- src/drives.c | 11 +++++++++-- src/guestfs-internal.h | 1 + src/launch-direct.c | 15 +++++++++++---- src/launch-libvirt.c | 19 ++++++++++++++----- 6 files changed, 38 insertions(+), 13 deletions(-) -- 1.9.3
See: https://lists.gnu.org/archive/html/qemu-devel/2012-07/msg03747.html Related do RHBZ#1130506. --- generator/actions.ml | 2 +- src/drives.c | 11 +++++++++-- src/guestfs-internal.h | 1 + src/launch-direct.c | 8 +++++--- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/generator/actions.ml b/generator/actions.ml index 1a89869..4447de6 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -1322,7 +1322,7 @@ not all belong to a single logical operating system { defaults with name = "add_drive"; - style = RErr, [String "filename"], [OBool "readonly"; OString "format"; OString "iface"; OString "name"; OString "label"; OString "protocol"; OStringList "server"; OString "username"; OString "secret"; OString "cachemode"; OString "discard"; OBool "copyonread"]; + style = RErr, [String "filename"], [OBool "readonly"; OString "format"; OString "iface"; OString "name"; OString "label"; OString "protocol"; OStringList "server"; OString "username"; OString "secret"; OString "cachemode"; OString "discard"; OBool "copyonread"; OBool "detectzeros"]; once_had_no_optargs = true; blocking = false; fish_alias = ["add"]; diff --git a/src/drives.c b/src/drives.c index ad747ab..d1e680a 100644 --- a/src/drives.c +++ b/src/drives.c @@ -63,6 +63,7 @@ struct drive_create_data { const char *cachemode; enum discard discard; bool copyonread; + bool detectzeros; }; COMPILE_REGEXP (re_hostname_port, "(.*):(\\d+)$", 0) @@ -116,6 +117,7 @@ create_drive_file (guestfs_h *g, drv->cachemode = data->cachemode ? safe_strdup (g, data->cachemode) : NULL; drv->discard = data->discard; drv->copyonread = data->copyonread; + drv->detectzeros = data->detectzeros; if (data->readonly) { if (create_overlay (g, drv) == -1) { @@ -152,6 +154,7 @@ create_drive_non_file (guestfs_h *g, drv->cachemode = data->cachemode ? safe_strdup (g, data->cachemode) : NULL; drv->discard = data->discard; drv->copyonread = data->copyonread; + drv->detectzeros = data->detectzeros; if (data->readonly) { if (create_overlay (g, drv) == -1) { @@ -504,7 +507,7 @@ static char * drive_to_string (guestfs_h *g, const struct drive *drv) { return safe_asprintf - (g, "%s%s%s%s protocol=%s%s%s%s%s%s%s%s%s%s%s", + (g, "%s%s%s%s protocol=%s%s%s%s%s%s%s%s%s%s%s%s", drv->src.u.path, drv->readonly ? " readonly" : "", drv->src.format ? " format=" : "", @@ -520,7 +523,8 @@ drive_to_string (guestfs_h *g, const struct drive *drv) drv->cachemode ? : "", drv->discard == discard_disable ? "" : drv->discard == discard_enable ? " discard=enable" : " discard=besteffort", - drv->copyonread ? " copyonread" : ""); + drv->copyonread ? " copyonread" : "", + drv->detectzeros ? " detect-zeroes=on" : ""); } /* Add struct drive to the g->drives vector at the given index. */ @@ -746,6 +750,9 @@ guestfs_impl_add_drive_opts (guestfs_h *g, const char *filename, data.servers = NULL; data.exportname = filename; + data.detectzeros = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_DETECTZEROS_BITMASK + ? optargs->detectzeros : false; + data.readonly = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK ? optargs->readonly : false; data.format = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 414a634..ea0b716 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -269,6 +269,7 @@ struct drive { char *cachemode; enum discard discard; bool copyonread; + bool detectzeros; }; /* Extra hv parameters (from guestfs_config). */ diff --git a/src/launch-direct.c b/src/launch-direct.c index ea67ec9..7118db8 100644 --- a/src/launch-direct.c +++ b/src/launch-direct.c @@ -512,7 +512,7 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) * the if=... at the end. */ param = safe_asprintf - (g, "file=%s%s,cache=%s%s%s%s%s%s%s,id=hd%zu", + (g, "file=%s%s,cache=%s%s%s%s%s%s%s%s,id=hd%zu", escaped_file, drv->readonly ? ",snapshot=on" : "", drv->cachemode ? drv->cachemode : "writeback", @@ -521,6 +521,7 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) drv->src.format ? drv->src.format : "", drv->disk_label ? ",serial=" : "", drv->disk_label ? drv->disk_label : "", + drv->detectzeros ? ",detect-zeroes=on" : "", drv->copyonread ? ",copy-on-read=on" : "", i); } @@ -528,11 +529,12 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) /* Writable qcow2 overlay on top of read-only drive. */ escaped_file = qemu_escape_param (g, drv->overlay); param = safe_asprintf - (g, "file=%s,cache=unsafe,format=qcow2%s%s,id=hd%zu", + (g, "file=%s,cache=unsafe,format=qcow2%s%s,id=hd%zu,detect-zeroes=%s", escaped_file, drv->disk_label ? ",serial=" : "", drv->disk_label ? drv->disk_label : "", - i); + i, + drv->detectzeros ? "on" : "off"); } /* If there's an explicit 'iface', use it. Otherwise default to -- 1.9.3
On Fri, Apr 24, 2015 at 02:51:48PM +0200, Maros Zatko wrote:
See: https://lists.gnu.org/archive/html/qemu-devel/2012-07/msg03747.html Related do RHBZ#1130506. --- generator/actions.ml | 2 +- src/drives.c | 11 +++++++++-- src/guestfs-internal.h | 1 + src/launch-direct.c | 8 +++++--- 4 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/generator/actions.ml b/generator/actions.ml index 1a89869..4447de6 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -1322,7 +1322,7 @@ not all belong to a single logical operating system
{ defaults with name = "add_drive"; - style = RErr, [String "filename"], [OBool "readonly"; OString "format"; OString "iface"; OString "name"; OString "label"; OString "protocol"; OStringList "server"; OString "username"; OString "secret"; OString "cachemode"; OString "discard"; OBool "copyonread"]; + style = RErr, [String "filename"], [OBool "readonly"; OString "format"; OString "iface"; OString "name"; OString "label"; OString "protocol"; OStringList "server"; OString "username"; OString "secret"; OString "cachemode"; OString "discard"; OBool "copyonread"; OBool "detectzeros"]; once_had_no_optargs = true; blocking = false; fish_alias = ["add"];
This needs a change to the API documentation too. Rich.
diff --git a/src/drives.c b/src/drives.c index ad747ab..d1e680a 100644 --- a/src/drives.c +++ b/src/drives.c @@ -63,6 +63,7 @@ struct drive_create_data { const char *cachemode; enum discard discard; bool copyonread; + bool detectzeros; };
COMPILE_REGEXP (re_hostname_port, "(.*):(\\d+)$", 0) @@ -116,6 +117,7 @@ create_drive_file (guestfs_h *g, drv->cachemode = data->cachemode ? safe_strdup (g, data->cachemode) : NULL; drv->discard = data->discard; drv->copyonread = data->copyonread; + drv->detectzeros = data->detectzeros;
if (data->readonly) { if (create_overlay (g, drv) == -1) { @@ -152,6 +154,7 @@ create_drive_non_file (guestfs_h *g, drv->cachemode = data->cachemode ? safe_strdup (g, data->cachemode) : NULL; drv->discard = data->discard; drv->copyonread = data->copyonread; + drv->detectzeros = data->detectzeros;
if (data->readonly) { if (create_overlay (g, drv) == -1) { @@ -504,7 +507,7 @@ static char * drive_to_string (guestfs_h *g, const struct drive *drv) { return safe_asprintf - (g, "%s%s%s%s protocol=%s%s%s%s%s%s%s%s%s%s%s", + (g, "%s%s%s%s protocol=%s%s%s%s%s%s%s%s%s%s%s%s", drv->src.u.path, drv->readonly ? " readonly" : "", drv->src.format ? " format=" : "", @@ -520,7 +523,8 @@ drive_to_string (guestfs_h *g, const struct drive *drv) drv->cachemode ? : "", drv->discard == discard_disable ? "" : drv->discard == discard_enable ? " discard=enable" : " discard=besteffort", - drv->copyonread ? " copyonread" : ""); + drv->copyonread ? " copyonread" : "", + drv->detectzeros ? " detect-zeroes=on" : ""); }
/* Add struct drive to the g->drives vector at the given index. */ @@ -746,6 +750,9 @@ guestfs_impl_add_drive_opts (guestfs_h *g, const char *filename, data.servers = NULL; data.exportname = filename;
+ data.detectzeros = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_DETECTZEROS_BITMASK + ? optargs->detectzeros : false; + data.readonly = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK ? optargs->readonly : false; data.format = optargs->bitmask & GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 414a634..ea0b716 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -269,6 +269,7 @@ struct drive { char *cachemode; enum discard discard; bool copyonread; + bool detectzeros; };
/* Extra hv parameters (from guestfs_config). */ diff --git a/src/launch-direct.c b/src/launch-direct.c index ea67ec9..7118db8 100644 --- a/src/launch-direct.c +++ b/src/launch-direct.c @@ -512,7 +512,7 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) * the if=... at the end. */ param = safe_asprintf - (g, "file=%s%s,cache=%s%s%s%s%s%s%s,id=hd%zu", + (g, "file=%s%s,cache=%s%s%s%s%s%s%s%s,id=hd%zu", escaped_file, drv->readonly ? ",snapshot=on" : "", drv->cachemode ? drv->cachemode : "writeback", @@ -521,6 +521,7 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) drv->src.format ? drv->src.format : "", drv->disk_label ? ",serial=" : "", drv->disk_label ? drv->disk_label : "", + drv->detectzeros ? ",detect-zeroes=on" : "", drv->copyonread ? ",copy-on-read=on" : "", i); } @@ -528,11 +529,12 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) /* Writable qcow2 overlay on top of read-only drive. */ escaped_file = qemu_escape_param (g, drv->overlay); param = safe_asprintf - (g, "file=%s,cache=unsafe,format=qcow2%s%s,id=hd%zu", + (g, "file=%s,cache=unsafe,format=qcow2%s%s,id=hd%zu,detect-zeroes=%s", escaped_file, drv->disk_label ? ",serial=" : "", drv->disk_label ? drv->disk_label : "", - i); + i, + drv->detectzeros ? "on" : "off"); }
/* If there's an explicit 'iface', use it. Otherwise default to -- 1.9.3
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://www.redhat.com/mailman/listinfo/libguestfs
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Fixes RHBZ#1130506 See also: https://lists.gnu.org/archive/html/qemu-devel/2012-07/msg03747.html --- sparsify/copying.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sparsify/copying.ml b/sparsify/copying.ml index 9f788b9..111dfeb 100644 --- a/sparsify/copying.ml +++ b/sparsify/copying.ml @@ -190,7 +190,8 @@ You can ignore this warning or change it to a hard failure using the if verbose then g#set_verbose true; (* Note that the temporary overlay disk is always qcow2 format. *) - g#add_drive ~format:"qcow2" ~readonly:false ~cachemode:"unsafe" overlaydisk; + g#add_drive ~format:"qcow2" ~readonly:false ~cachemode:"unsafe" overlaydisk + ~detectzeros: true; if not quiet then Progress.set_up_progress_bar ~machine_readable g; g#launch (); -- 1.9.3
From: Maros Zatko <hacxman@gmail.com> --- src/launch-libvirt.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/launch-libvirt.c b/src/launch-libvirt.c index f46782c..d9df0f7 100644 --- a/src/launch-libvirt.c +++ b/src/launch-libvirt.c @@ -895,7 +895,7 @@ static int construct_libvirt_xml_devices (guestfs_h *g, const struct libvirt_xml static int construct_libvirt_xml_qemu_cmdline (guestfs_h *g, const struct libvirt_xml_params *params, xmlTextWriterPtr xo); static int construct_libvirt_xml_disk (guestfs_h *g, const struct backend_libvirt_data *data, xmlTextWriterPtr xo, struct drive *drv, size_t drv_index); static int construct_libvirt_xml_disk_target (guestfs_h *g, xmlTextWriterPtr xo, size_t drv_index); -static int construct_libvirt_xml_disk_driver_qemu (guestfs_h *g, const struct backend_libvirt_data *data, struct drive *drv, xmlTextWriterPtr xo, const char *format, const char *cachemode, enum discard discard, bool copyonread); +static int construct_libvirt_xml_disk_driver_qemu (guestfs_h *g, const struct backend_libvirt_data *data, struct drive *drv, xmlTextWriterPtr xo, const char *format, const char *cachemode, enum discard discard, bool copyonread, bool detect_zeroes); static int construct_libvirt_xml_disk_address (guestfs_h *g, xmlTextWriterPtr xo, size_t drv_index); static int construct_libvirt_xml_disk_source_hosts (guestfs_h *g, xmlTextWriterPtr xo, const struct drive_source *src); static int construct_libvirt_xml_disk_source_seclabel (guestfs_h *g, const struct backend_libvirt_data *data, xmlTextWriterPtr xo); @@ -1389,7 +1389,8 @@ construct_libvirt_xml_disk (guestfs_h *g, if (construct_libvirt_xml_disk_driver_qemu (g, data, drv, xo, "qcow2", "unsafe", - discard_disable, false) + discard_disable, false, + drv->detectzeros) == -1) return -1; } @@ -1534,7 +1535,7 @@ construct_libvirt_xml_disk (guestfs_h *g, if (construct_libvirt_xml_disk_driver_qemu (g, data, drv, xo, format, drv->cachemode ? : "writeback", - drv->discard, false) + drv->discard, false, drv->detectzeros) == -1) return -1; } @@ -1577,7 +1578,8 @@ construct_libvirt_xml_disk_driver_qemu (guestfs_h *g, const char *format, const char *cachemode, enum discard discard, - bool copyonread) + bool copyonread, + bool detect_zeroes) { bool discard_unmap = false; @@ -1605,6 +1607,10 @@ construct_libvirt_xml_disk_driver_qemu (guestfs_h *g, break; } + if (data->qemu_version < 2001000) { + detect_zeroes = false; + } + start_element ("driver") { attribute ("name", "qemu"); attribute ("type", format); @@ -1613,6 +1619,8 @@ construct_libvirt_xml_disk_driver_qemu (guestfs_h *g, attribute ("discard", "unmap"); if (copyonread) attribute ("copy_on_read", "on"); + if (detect_zeroes) + attribute ("detect_zeroes", "on"); } end_element (); return 0; @@ -1699,7 +1707,8 @@ construct_libvirt_xml_appliance (guestfs_h *g, if (construct_libvirt_xml_disk_driver_qemu (g, params->data, NULL, xo, "qcow2", "unsafe", - discard_disable, false) == -1) + discard_disable, false, + true) == -1) return -1; if (construct_libvirt_xml_disk_address (g, xo, params->appliance_index) -- 1.9.3
On Fri, Apr 24, 2015 at 02:51:50PM +0200, Maros Zatko wrote:
@@ -1605,6 +1607,10 @@ construct_libvirt_xml_disk_driver_qemu (guestfs_h *g, break; }
+ if (data->qemu_version < 2001000) { + detect_zeroes = false; + } + start_element ("driver") { attribute ("name", "qemu"); attribute ("type", format); @@ -1613,6 +1619,8 @@ construct_libvirt_xml_disk_driver_qemu (guestfs_h *g, attribute ("discard", "unmap"); if (copyonread) attribute ("copy_on_read", "on"); + if (detect_zeroes) + attribute ("detect_zeroes", "on"); } end_element ();
return 0;
To make the code less fragile, reduce long-range dependencies. Combine these two hunks into one: if (detect_zeroes && data->qemu_version < ...) { ... } Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
From: Maros Zatko <hacxman@gmail.com> --- src/launch-direct.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/launch-direct.c b/src/launch-direct.c index 7118db8..81a82f8 100644 --- a/src/launch-direct.c +++ b/src/launch-direct.c @@ -478,11 +478,16 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) ITER_DRIVES (g, i, drv) { CLEANUP_FREE char *file = NULL, *escaped_file = NULL, *param = NULL; + const char *detect_zeroes = ""; + int major = data->qemu_version_major, minor = data->qemu_version_minor; + unsigned long qemu_version = major * 1000000 + minor * 1000; + + if (drv->detectzeros && (major >= 2 || (major == 2 && minor >= 1))) { + detect_zeroes = ",detect-zeroes=on"; + } if (!drv->overlay) { const char *discard_mode = ""; - int major = data->qemu_version_major, minor = data->qemu_version_minor; - unsigned long qemu_version = major * 1000000 + minor * 1000; switch (drv->discard) { case discard_disable: @@ -521,7 +526,7 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) drv->src.format ? drv->src.format : "", drv->disk_label ? ",serial=" : "", drv->disk_label ? drv->disk_label : "", - drv->detectzeros ? ",detect-zeroes=on" : "", + detect_zeroes, drv->copyonread ? ",copy-on-read=on" : "", i); } @@ -529,12 +534,12 @@ launch_direct (guestfs_h *g, void *datav, const char *arg) /* Writable qcow2 overlay on top of read-only drive. */ escaped_file = qemu_escape_param (g, drv->overlay); param = safe_asprintf - (g, "file=%s,cache=unsafe,format=qcow2%s%s,id=hd%zu,detect-zeroes=%s", + (g, "file=%s,cache=unsafe,format=qcow2%s%s%s,id=hd%zu", escaped_file, drv->disk_label ? ",serial=" : "", drv->disk_label ? drv->disk_label : "", - i, - drv->detectzeros ? "on" : "off"); + detect_zeroes, + i); } /* If there's an explicit 'iface', use it. Otherwise default to -- 1.9.3
participants (2)
-
Maros Zatko -
Richard W.M. Jones