On Wednesday, 12 August 2020 17:36:10 CEST Andrey Shinkevich wrote:
For the appliance of the QCOW2 format, get the UUID of the disk by reading the first 256k bytes with 'qemu-img dd' command. Then pass the read block to the 'file' command. In case of failure, run the 'file' command again directly.
Suggested-by: Denis V. Lunev <den@openvz.org> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com> --- v2: 01: The order of the function calls to direct <file> command on the appliance and the indirect one (through the temporary file) has been swapped.
lib/appliance-kcmdline.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-)
diff --git a/lib/appliance-kcmdline.c b/lib/appliance-kcmdline.c index fbeb4f4..c78524b 100644 --- a/lib/appliance-kcmdline.c +++ b/lib/appliance-kcmdline.c @@ -71,7 +71,7 @@ read_uuid (guestfs_h *g, void *retv, const char *line, size_t len) * The L<file(1)> command does the hard work. */ static char * -get_root_uuid (guestfs_h *g, const char *appliance) +do_get_root_uuid (guestfs_h *g, const char *appliance)
get_root_uuid_with_file seems a better name, and gives less confusion than get_root_uuid & do_get_root_uuid.
{ CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g); char *ret = NULL; @@ -96,6 +96,74 @@ get_root_uuid (guestfs_h *g, const char *appliance) }
/** + * Read the first 256k bytes of the in_file with L<qemu-img(1)> command + * and write them into the out_file. That may be useful to get UUID of + * the QCOW2 disk image with further L<file(1)> command. + * The function returns zero if successful, otherwise -1. + */ +static int +run_qemu_img_dd (guestfs_h *g, const char *in_file, char *out_file) +{ + CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g); + int r; + + guestfs_int_cmd_add_arg (cmd, "qemu-img"); + guestfs_int_cmd_add_arg (cmd, "dd"); + guestfs_int_cmd_add_arg_format (cmd, "if=%s", in_file); + guestfs_int_cmd_add_arg_format (cmd, "of=%s", out_file); + guestfs_int_cmd_add_arg (cmd, "bs=256k"); + guestfs_int_cmd_add_arg (cmd, "count=1"); + + r = guestfs_int_cmd_run (cmd); + if (r == -1) { + error (g, "Failed to run qemu-img"); + return -1; + } + if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) { + guestfs_int_external_command_failed (g, r, "qemu-img dd", NULL); + return -1; + } + + return 0; +} + +/** + * Get the UUID from the appliance disk image. + */ +static char * +get_root_uuid (guestfs_h *g, const char *appliance) +{ + char *uuid = NULL; + int ret; + char tmp_file[] = "/tmp/libguestfsXXXXXX";
Please do not hardcode a location, and use the internal temporary directory instead. The internal guestfs_int_make_temp_path will do the right job. Thanks, -- Pino Toscano