This has to read the data dirs from qemu using the new 'qemu -L ?' flag added in qemu 2.7. --- src/guestfs-internal.h | 1 + src/qemu.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index d325f50..7904a9f 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -915,6 +915,7 @@ extern struct qemu_data *guestfs_int_test_qemu (guestfs_h *g); extern struct qemu_data *guestfs_int_init_qemu_version (guestfs_h *g, unsigned long qemu_version); extern int guestfs_int_qemu_supports (guestfs_h *g, const struct qemu_data *, const char *option); extern int guestfs_int_qemu_supports_device (guestfs_h *g, const struct qemu_data *, const char *device_name); +extern int guestfs_int_qemu_supports_bios (guestfs_h *g, const struct qemu_data *, const char *bios_name); extern int guestfs_int_qemu_supports_virtio_scsi (guestfs_h *g, struct qemu_data *); extern bool guestfs_int_qemu_version_le (const struct qemu_data *, int max_major, int max_minor); extern bool guestfs_int_qemu_version_ge (const struct qemu_data *, int min_major, int min_minor); diff --git a/src/qemu.c b/src/qemu.c index 259a6fd..0107160 100644 --- a/src/qemu.c +++ b/src/qemu.c @@ -50,6 +50,7 @@ COMPILE_REGEXP (re_major_minor, "(\\d+)\\.(\\d+)", 0) struct qemu_data { char *qemu_help; /* Output of qemu -help. */ char *qemu_devices; /* Output of qemu -device ? */ + char *qemu_datadirs; /* Output of qemu -L ? (NULL if not supported) */ /* qemu version (0, 0 if unable to parse). */ int qemu_version_major, qemu_version_minor; @@ -71,8 +72,9 @@ static void read_all (guestfs_h *g, void *retv, const char *buf, size_t len); /** * Test qemu binary (or wrapper) runs, and do C<qemu -help> so we know - * the version of qemu what options this qemu supports, and - * C<qemu -device ?> so we know what devices are available. + * the version of qemu what options this qemu supports, + * C<qemu -device ?> so we know what devices are available, + * and C<qemu -L ?> to list data directories. * * This caches the results in the cachedir so that as long as the qemu * binary does not change, calling this is effectively free. @@ -83,7 +85,8 @@ guestfs_int_test_qemu (guestfs_h *g) struct qemu_data *data; struct stat statbuf; CLEANUP_FREE char *cachedir = NULL, *qemu_stat_filename = NULL, - *qemu_help_filename = NULL, *qemu_devices_filename = NULL; + *qemu_help_filename = NULL, *qemu_devices_filename = NULL, + *qemu_datadirs_filename = NULL; FILE *fp; int generation; uint64_t prev_size, prev_mtime; @@ -100,6 +103,7 @@ guestfs_int_test_qemu (guestfs_h *g) qemu_stat_filename = safe_asprintf (g, "%s/qemu.stat", cachedir); qemu_help_filename = safe_asprintf (g, "%s/qemu.help", cachedir); qemu_devices_filename = safe_asprintf (g, "%s/qemu.devices", cachedir); + qemu_datadirs_filename = safe_asprintf (g, "%s/qemu.datadirs", cachedir); /* Did we previously test the same version of qemu? */ debug (g, "checking for previously cached test results of %s, in %s", @@ -122,7 +126,8 @@ guestfs_int_test_qemu (guestfs_h *g) * and qemu -devices ? output. */ if (access (qemu_help_filename, R_OK) == -1 || - access (qemu_devices_filename, R_OK) == -1) + access (qemu_devices_filename, R_OK) == -1 || + access (qemu_datadirs_filename, R_OK) == -1) goto do_test; debug (g, "loading previously cached test results"); @@ -143,6 +148,12 @@ guestfs_int_test_qemu (guestfs_h *g) return NULL; } + if (guestfs_int_read_whole_file (g, qemu_datadirs_filename, + &data->qemu_datadirs, NULL) == -1) { + guestfs_int_free_qemu_data (data); + return NULL; + } + return data; } @@ -183,6 +194,19 @@ guestfs_int_test_qemu (guestfs_h *g) if (fclose (fp) == -1) goto devices_error; + fp = fopen (qemu_datadirs_filename, "w"); + if (fp == NULL) { + datadirs_error: + perrorf (g, "%s", qemu_datadirs_filename); + if (fp != NULL) fclose (fp); + guestfs_int_free_qemu_data (data); + return NULL; + } + if (fprintf (fp, "%s", data->qemu_datadirs) == -1) + goto datadirs_error; + if (fclose (fp) == -1) + goto datadirs_error; + /* Write the qemu.stat file last so that its presence indicates that * the qemu.help and qemu.devices files ought to exist. */ @@ -214,6 +238,7 @@ test_qemu (guestfs_h *g, struct qemu_data *data) { CLEANUP_CMD_CLOSE struct command *cmd1 = guestfs_int_new_command (g); CLEANUP_CMD_CLOSE struct command *cmd2 = guestfs_int_new_command (g); + CLEANUP_CMD_CLOSE struct command *cmd3 = guestfs_int_new_command (g); int r; guestfs_int_cmd_add_arg (cmd1, g->hv); @@ -247,6 +272,28 @@ test_qemu (guestfs_h *g, struct qemu_data *data) if (r == -1 || !WIFEXITED (r) || WEXITSTATUS (r) != 0) goto error; + /* qemu -L ? only supported in qemu >= 2.7 */ + if (guestfs_int_qemu_version_ge (data, 2, 7)) { + guestfs_int_cmd_add_arg (cmd3, g->hv); + guestfs_int_cmd_add_arg (cmd3, "-display"); + guestfs_int_cmd_add_arg (cmd3, "none"); + guestfs_int_cmd_add_arg (cmd3, "-machine"); + guestfs_int_cmd_add_arg (cmd3, +#ifdef MACHINE_TYPE + MACHINE_TYPE "," +#endif + "accel=kvm:tcg"); + guestfs_int_cmd_add_arg (cmd3, "-L"); + guestfs_int_cmd_add_arg (cmd3, "?"); + guestfs_int_cmd_set_stdout_callback (cmd3, read_all, &data->qemu_datadirs, + CMD_STDOUT_FLAG_WHOLE_BUFFER); + r = guestfs_int_cmd_run (cmd3); + if (r == -1 || !WIFEXITED (r) || WEXITSTATUS (r) != 0) + goto error; + } + else + data->qemu_datadirs = safe_strdup (g, ""); + return 0; error: @@ -342,6 +389,32 @@ guestfs_int_qemu_supports_device (guestfs_h *g, return strstr (data->qemu_devices, device_name) != NULL; } +/** + * Test if a named BIOS is supported by qemu. + */ +int +guestfs_int_qemu_supports_bios (guestfs_h *g, + const struct qemu_data *data, + const char *bios_name) +{ + CLEANUP_FREE_STRING_LIST char **datadirs; + size_t i; + + datadirs = guestfs_int_split_string ('\n', data->qemu_datadirs); + if (datadirs == NULL) + return 0; /* ignore errors, return false which is safe */ + + for (i = 0; datadirs[i] != NULL; ++i) { + CLEANUP_FREE char *path; + + path = safe_asprintf (g, "%s/%s", datadirs[i], bios_name); + if (access (path, R_OK) == 0) + return 1; + } + + return 0; +} + static int old_or_broken_virtio_scsi (guestfs_h *g, const struct qemu_data *data) { -- 2.7.4