lib: Remove qemu version when testing for discard feature

We can assume much more recent qemu, so we don't need to have special
cases for qemu 1.5/1.6.
This commit is contained in:
Richard W.M. Jones
2025-09-29 12:18:28 +01:00
committed by rwmjones
parent 5df7da7e81
commit 02d82a3c3a
4 changed files with 13 additions and 46 deletions

View File

@@ -797,7 +797,7 @@ extern struct version guestfs_int_qemu_version (guestfs_h *g, struct qemu_data *
extern int guestfs_int_qemu_supports_device (guestfs_h *g, const struct qemu_data *, const char *device_name);
extern bool guestfs_int_platform_has_kvm (guestfs_h *g, const struct qemu_data *data);
extern char *guestfs_int_drive_source_qemu_param (guestfs_h *g, const struct drive_source *src);
extern bool guestfs_int_discard_possible (guestfs_h *g, struct drive *drv, const struct version *qemu_version);
extern bool guestfs_int_discard_possible (guestfs_h *g, struct drive *drv);
extern char *guestfs_int_qemu_escape_param (guestfs_h *g, const char *param);
extern void guestfs_int_free_qemu_data (struct qemu_data *);

View File

@@ -221,21 +221,15 @@ add_drive_standard_params (guestfs_h *g, struct backend_direct_data *data,
switch (drv->discard) {
case discard_disable:
/* Since the default is always discard=ignore, don't specify it
* on the command line. This also avoids unnecessary breakage
* with qemu < 1.5 which didn't have the option at all.
* on the command line.
*/
break;
case discard_enable:
if (!guestfs_int_discard_possible (g, drv, &data->qemu_version))
if (!guestfs_int_discard_possible (g, drv))
return -1;
/*FALLTHROUGH*/
case discard_besteffort:
/* I believe from reading the code that this is always safe as
* long as qemu >= 1.5.
*/
if (guestfs_int_version_ge (&data->qemu_version, 1, 5, 0))
append_list ("discard=unmap");
break;
append_list ("discard=unmap");
}
}
else {

View File

@@ -1651,16 +1651,11 @@ construct_libvirt_xml_disk_driver_qemu (guestfs_h *g,
*/
break;
case discard_enable:
if (!guestfs_int_discard_possible (g, drv, &data->qemu_version))
if (!guestfs_int_discard_possible (g, drv))
return -1;
/*FALLTHROUGH*/
case discard_besteffort:
/* I believe from reading the code that this is always safe as
* long as qemu >= 1.5.
*/
if (guestfs_int_version_ge (&data->qemu_version, 1, 5, 0))
discard_unmap = true;
break;
discard_unmap = true;
}
start_element ("driver") {

View File

@@ -820,31 +820,15 @@ guestfs_int_drive_source_qemu_param (guestfs_h *g,
}
/**
* Test if discard is both supported by qemu AND possible with the
* underlying file or device. This returns C<1> if discard is
* possible. It returns C<0> if not possible and sets the error to
* the reason why.
* Test if discard is possible with the underlying file or device.
* This returns C<1> if discard is possible. It returns C<0> if not
* possible and sets the error to the reason why.
*
* This function is called when the user set C<discard == "enable">.
*/
bool
guestfs_int_discard_possible (guestfs_h *g, struct drive *drv,
const struct version *qemu_version)
guestfs_int_discard_possible (guestfs_h *g, struct drive *drv)
{
/* qemu >= 1.5. This was the first version that supported the
* discard option on -drive at all.
*/
bool qemu15 = guestfs_int_version_ge (qemu_version, 1, 5, 0);
/* qemu >= 1.6. This was the first version that supported unmap on
* qcow2 backing files.
*/
bool qemu16 = guestfs_int_version_ge (qemu_version, 1, 6, 0);
if (!qemu15)
NOT_SUPPORTED (g, false,
_("discard cannot be enabled on this drive: "
"qemu < 1.5"));
/* If it's an overlay, discard is not possible (on the underlying
* file). This has probably been caught earlier since we already
* checked that the drive is !readonly. Nevertheless ...
@@ -861,17 +845,11 @@ guestfs_int_discard_possible (guestfs_h *g, struct drive *drv,
_("discard cannot be enabled on this drive: "
"you have to specify the format of the file"));
}
else if (STREQ (drv->src.format, "raw"))
else if (STREQ (drv->src.format, "raw") || STREQ (drv->src.format, "qcow2"))
/* OK */ ;
else if (STREQ (drv->src.format, "qcow2")) {
if (!qemu16)
NOT_SUPPORTED (g, false,
_("discard cannot be enabled on this drive: "
"qemu < 1.6 cannot do discard on qcow2 files"));
}
else {
/* It's possible in future other formats will support discard, but
* currently (qemu 1.7) none of them do.
/* It's possible other formats support discard, but we can enable
* them on a case-by-case basis.
*/
NOT_SUPPORTED (g, false,
_("discard cannot be enabled on this drive: "