[Pkg-libvirt-commits] [libguestfs] 145/233: lib: Use disk-create API instead of qemu-img/truncate inside the library.
Hilko Bengen
bengen at moszumanska.debian.org
Wed Feb 19 21:11:50 UTC 2014
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to branch experimental
in repository libguestfs.
commit 1e5eb0aebb80c2a37dfca018f589f0d84ee6e6ea
Author: Richard W.M. Jones <rjones at redhat.com>
Date: Tue Jan 28 15:03:09 2014 +0000
lib: Use disk-create API instead of qemu-img/truncate inside the library.
---
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);
- 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);
+
+ if (guestfs_disk_create (g, tmpfile, "raw", 4096, -1) == -1)
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);
- }
- guestfs___cmd_add_arg (cmd, overlay);
- r = guestfs___cmd_run (cmd);
- if (r == -1) {
- free (overlay);
- return NULL;
+ optargs.bitmask |= GUESTFS_DISK_CREATE_BACKINGFORMAT_BITMASK;
+ optargs.backingformat = format;
}
- 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
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git
More information about the Pkg-libvirt-commits
mailing list