[Libguestfs] [PATCH 03/10] lib: Use disk-create API instead of qemu-img/truncate inside the library.

Richard W.M. Jones
2014-01-28T16:24:50Z

---
 src/drives.c         | 35 ++++-------------------------------
 src/launch-direct.c  | 37 ++++++++++++-------------------------
 src/launch-libvirt.c | 38 +++++++++++---------------------------
 3 files changed, 27 insertions(+), 83 deletions(-)

diff --git a/src/drives.c b/src/drives.c
index dd2b96c..733a6c4 100644
--- a/src/drives.c
+++ b/src/drives.c
@@ -509,7 +509,6 @@ create_drive_dev_null (guestfs_h *g, bool readonly, const
char *format,
                        const char *disk_label)
 {
   CLEANUP_FREE char *tmpfile = NULL;
-  int fd = -1;

   if (format && STRNEQ (format, "raw")) {
     error (g, _("for device '/dev/null', format must be
'raw'"));
@@ -525,20 +524,9 @@ create_drive_dev_null (guestfs_h *g, bool readonly, const
char *format,
   readonly = false;

   tmpfile = safe_asprintf (g, "%s/devnull%d", g->tmpdir,
++g->unique);
-  fd = open (tmpfile, O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC, 0600);
-  if (fd == -1) {
-    perrorf (g, "open: %s", tmpfile);
+
+  if (guestfs_disk_create (g, tmpfile, "raw", 4096, -1) == -1)
     return NULL;
-  }
-  if (ftruncate (fd, 4096) == -1) {
-    perrorf (g, "truncate: %s", tmpfile);
-    close (fd);
-    return NULL;
-  }
-  if (close (fd) == -1) {
-    perrorf (g, "close: %s", tmpfile);
-    return NULL;
-  }

   return create_drive_file (g, tmpfile, readonly, format, iface, name,
                             disk_label, 0);
@@ -1083,11 +1071,10 @@ guestfs__add_drive_ro_with_if (guestfs_h *g, const char
*filename,

 int
 guestfs__add_drive_scratch (guestfs_h *g, int64_t size,
-                                 const struct guestfs_add_drive_scratch_argv
*optargs)
+                            const struct guestfs_add_drive_scratch_argv
*optargs)
 {
   struct guestfs_add_drive_opts_argv add_drive_optargs = { .bitmask = 0 };
   CLEANUP_FREE char *filename = NULL;
-  int fd;

   /* Some parameters we always set. */
   add_drive_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
@@ -1114,22 +1101,8 @@ guestfs__add_drive_scratch (guestfs_h *g, int64_t size,
   filename = safe_asprintf (g, "%s/scratch.%d", g->tmpdir,
++g->unique);

   /* Create a raw format temporary disk. */
-  fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC, 0600);
-  if (fd == -1) {
-    perrorf (g, "open: %s", filename);
+  if (guestfs_disk_create (g, filename, "raw", size, -1) == -1)
     return -1;
-  }
-
-  if (ftruncate (fd, size) == -1) {
-    perrorf (g, "ftruncate: %s", filename);
-    close (fd);
-    return -1;
-  }
-
-  if (close (fd) == -1) {
-    perrorf (g, "close: %s", filename);
-    return -1;
-  }

   /* Call guestfs_add_drive_opts to add the drive. */
   return guestfs_add_drive_opts_argv (g, filename, &add_drive_optargs);
diff --git a/src/launch-direct.c b/src/launch-direct.c
index 2b22ed0..964a507 100644
--- a/src/launch-direct.c
+++ b/src/launch-direct.c
@@ -107,48 +107,35 @@ static char *qemu_escape_param (guestfs_h *g, const char
*param);
 static char *
 create_cow_overlay_direct (guestfs_h *g, void *datav, struct drive *drv)
 {
-  char *overlay = NULL;
+  char *overlay;
   CLEANUP_FREE char *backing_drive = NULL;
-  CLEANUP_CMD_CLOSE struct command *cmd = guestfs___new_command (g);
-  int r;
+  struct guestfs_disk_create_argv optargs;

   backing_drive = guestfs___drive_source_qemu_param (g, &drv->src);
   if (!backing_drive)
-    goto error;
+    return NULL;

   if (guestfs___lazy_make_tmpdir (g) == -1)
-    goto error;
+    return NULL;

   overlay = safe_asprintf (g, "%s/overlay%d", g->tmpdir,
++g->unique);

-  guestfs___cmd_add_arg (cmd, "qemu-img");
-  guestfs___cmd_add_arg (cmd, "create");
-  guestfs___cmd_add_arg (cmd, "-f");
-  guestfs___cmd_add_arg (cmd, "qcow2");
-  guestfs___cmd_add_arg (cmd, "-b");
-  guestfs___cmd_add_arg (cmd, backing_drive);
+  optargs.bitmask = GUESTFS_DISK_CREATE_BACKINGFILE_BITMASK;
+  optargs.backingfile = backing_drive;
   if (drv->src.format) {
-    guestfs___cmd_add_arg (cmd, "-o");
-    guestfs___cmd_add_arg_format (cmd, "backing_fmt=%s",
drv->src.format);
+    optargs.bitmask |= GUESTFS_DISK_CREATE_BACKINGFORMAT_BITMASK;
+    optargs.backingformat = drv->src.format;
   }
-  guestfs___cmd_add_arg (cmd, overlay);
-  r = guestfs___cmd_run (cmd);
-  if (r == -1)
-    goto error;
-  if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
-    guestfs___external_command_failed (g, r, "qemu-img create",
backing_drive);
-    goto error;
+
+  if (guestfs_disk_create_argv (g, overlay, "qcow2", -1,
&optargs) == -1) {
+    free (overlay);
+    return NULL;
   }

   /* Caller sets g->overlay in the handle to this, and then manages
    * the memory.
    */
   return overlay;
-
- error:
-  free (overlay);
-
-  return NULL;
 }

 #ifdef QEMU_OPTIONS
diff --git a/src/launch-libvirt.c b/src/launch-libvirt.c
index 8cc261d..60213fd 100644
--- a/src/launch-libvirt.c
+++ b/src/launch-libvirt.c
@@ -145,33 +145,22 @@ static char *
 make_qcow2_overlay (guestfs_h *g, const char *backing_drive,
                     const char *format)
 {
-  CLEANUP_CMD_CLOSE struct command *cmd = guestfs___new_command (g);
-  char *overlay = NULL;
-  int r;
+  char *overlay;
+  struct guestfs_disk_create_argv optargs;

   if (guestfs___lazy_make_tmpdir (g) == -1)
     return NULL;

   overlay = safe_asprintf (g, "%s/overlay%d", g->tmpdir,
++g->unique);

-  guestfs___cmd_add_arg (cmd, "qemu-img");
-  guestfs___cmd_add_arg (cmd, "create");
-  guestfs___cmd_add_arg (cmd, "-f");
-  guestfs___cmd_add_arg (cmd, "qcow2");
-  guestfs___cmd_add_arg (cmd, "-b");
-  guestfs___cmd_add_arg (cmd, backing_drive);
+  optargs.bitmask = GUESTFS_DISK_CREATE_BACKINGFILE_BITMASK;
+  optargs.backingfile = backing_drive;
   if (format) {
-    guestfs___cmd_add_arg (cmd, "-o");
-    guestfs___cmd_add_arg_format (cmd, "backing_fmt=%s", format);
+    optargs.bitmask |= GUESTFS_DISK_CREATE_BACKINGFORMAT_BITMASK;
+    optargs.backingformat = format;
   }
-  guestfs___cmd_add_arg (cmd, overlay);
-  r = guestfs___cmd_run (cmd);
-  if (r == -1) {
-    free (overlay);
-    return NULL;
-  }
-  if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
-    guestfs___external_command_failed (g, r, "qemu-img create",
backing_drive);
+
+  if (guestfs_disk_create_argv (g, overlay, "qcow2", -1,
&optargs) == -1) {
     free (overlay);
     return NULL;
   }
@@ -184,15 +173,15 @@ create_cow_overlay_libvirt (guestfs_h *g, void *datav,
struct drive *drv)
 {
   struct backend_libvirt_data *data = datav;
   CLEANUP_FREE char *backing_drive = NULL;
-  char *overlay = NULL;
+  char *overlay;

   backing_drive = guestfs___drive_source_qemu_param (g, &drv->src);
   if (!backing_drive)
-    goto error;
+    return NULL;

   overlay = make_qcow2_overlay (g, backing_drive, drv->src.format);
   if (!overlay)
-    goto error;
+    return NULL;

 #if HAVE_LIBSELINUX
   if (data->selinux_imagelabel) {
@@ -208,11 +197,6 @@ create_cow_overlay_libvirt (guestfs_h *g, void *datav,
struct drive *drv)
    * the memory.
    */
   return overlay;
-
- error:
-  free (overlay);
-
-  return NULL;
 }

 static int
--
1.8.4.2