summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-09-03 16:45:48 +0100
committerRichard W.M. Jones <rjones@redhat.com>2012-09-03 17:45:14 +0100
commit9f8ba4e8d16e5c50c3cd657f9beae3518947b6ed (patch)
tree70622fc36427ecc386982b416d5daa0e3c9acd03 /src
parentefdc2bae40b3e74b90a61f82bcb7b18830697272 (diff)
launch: libvirt: Use guestfs_disk_format API to autodetect input format.
Diffstat (limited to 'src')
-rw-r--r--src/launch-libvirt.c104
1 files changed, 19 insertions, 85 deletions
diff --git a/src/launch-libvirt.c b/src/launch-libvirt.c
index 9f14add6..07b65571 100644
--- a/src/launch-libvirt.c
+++ b/src/launch-libvirt.c
@@ -94,7 +94,6 @@ xmlBufferDetach (xmlBufferPtr buf)
#endif
static xmlChar *construct_libvirt_xml (guestfs_h *g, const char *capabilities_xml, const char *kernel, const char *initrd, const char *appliance, const char *guestfsd_sock, const char *console_sock);
-static char *autodetect_format (guestfs_h *g, const char *path);
static void libvirt_error (guestfs_h *g, const char *fs, ...);
static int is_blk (const char *path);
static int random_chars (char *ret, size_t len);
@@ -838,10 +837,28 @@ construct_libvirt_xml_disk (guestfs_h *g, xmlTextWriterPtr xo,
BAD_CAST drv->format));
}
else {
- format = autodetect_format (g, path);
+ /* libvirt has disabled the feature of detecting the disk format,
+ * unless the administrator sets allow_disk_format_probing=1 in
+ * qemu.conf. There is no way to detect if this option is set, so we
+ * have to do format detection here using qemu-img and pass that to
+ * libvirt.
+ *
+ * This is still a security issue, so in most cases it is recommended
+ * the users pass the format to libguestfs which will faithfully pass
+ * that to libvirt and this function won't be used.
+ */
+ format = guestfs_disk_format (g, path);
if (!format)
goto err;
+ if (STREQ (format, "unknown")) {
+ error (g, _("could not auto-detect the format of '%s'\n"
+ "If the format is known, pass the format to libguestfs, eg. using the\n"
+ "'--format' option, or via the optional 'format' argument to 'add-drive'."),
+ path);
+ goto err;
+ }
+
XMLERROR (-1,
xmlTextWriterWriteAttribute (xo, BAD_CAST "type",
BAD_CAST format));
@@ -1070,89 +1087,6 @@ construct_libvirt_xml_qemu_cmdline (guestfs_h *g, xmlTextWriterPtr xo)
return -1;
}
-/* libvirt has disabled the feature of detecting the disk format,
- * unless the administrator sets allow_disk_format_probing=1 in
- * qemu.conf. There is no way to detect if this option is set, so we
- * have to do format detection here using qemu-img and pass that to
- * libvirt.
- *
- * This is still a security issue, so in most cases it is recommended
- * the users pass the format to libguestfs which will faithfully pass
- * that to libvirt and this function won't be used.
- */
-static char *
-autodetect_format (guestfs_h *g, const char *path)
-{
- pid_t pid;
- int fd[2];
- FILE *fp;
- char *line = NULL;
- size_t len;
- char *p;
- size_t n;
- char *ret = NULL;
-
- if (pipe2 (fd, O_CLOEXEC) == -1) {
- perrorf (g, "pipe2");
- return NULL;
- }
-
- pid = fork ();
- if (pid == -1) {
- perrorf (g, "fork");
- close (fd[0]);
- close (fd[1]);
- return NULL;
- }
-
- if (pid == 0) { /* child */
- close (fd[0]);
- dup2 (fd[1], 1);
- close (fd[1]);
-
- execlp ("qemu-img", "qemu-img", "info", path, NULL);
- perror ("could not execute 'qemu-img info'");
- _exit (EXIT_FAILURE);
- }
-
- close (fd[1]);
-
- fp = fdopen (fd[0], "r");
- if (fp == NULL) {
- perrorf (g, "fdopen: qemu-img info");
- close (fd[0]);
- waitpid (pid, NULL, 0);
- return NULL;
- }
-
- while (getline (&line, &len, fp) != -1) {
- if (STRPREFIX (line, "file format: ")) {
- p = &line[13];
- n = strlen (p);
- if (n > 0 && p[n-1] == '\n')
- n--;
- memmove (line, p, n);
- line[n] = '\0';
- ret = line;
- break;
- }
- }
-
- fclose (fp); /* also closes fd[0] */
- waitpid (pid, NULL, 0);
-
- if (ret == NULL) {
- error (g, _("could not auto-detect the format of '%s'\n"
- "If the format is known, pass the format to libguestfs, eg. using the\n"
- "'--format=...' option, or via the optional 'format' argument to 'add-drive'."),
- path);
- free (line);
- return NULL;
- }
-
- return ret; /* caller frees */
-}
-
static int
is_blk (const char *path)
{