[Libguestfs] [PATCH 10/10] virt-make-fs: Use disk-create API instead of calling qemu-img create.

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

Strictly speaking this reduces the number of formats that virt-make-fs
can output to, but it's likely that no one cares and if they do we can
add new formats in future.
---
 tools/virt-make-fs | 44 ++++++--------------------------------------
 1 file changed, 6 insertions(+), 38 deletions(-)

diff --git a/tools/virt-make-fs b/tools/virt-make-fs
index 605d067..f1cc09d 100755
--- a/tools/virt-make-fs
+++ b/tools/virt-make-fs
@@ -231,9 +231,6 @@ Choose the output disk image format.

 The default is C<raw> (raw sparse disk image).

-For other choices, see the L<qemu-img(1)> manpage.  The only other
-choice that would really make sense here is C<qcow2>.
-
 =cut

 my $type = "ext2";
@@ -425,46 +422,17 @@ if (!defined $size) {

 $size = int ($size);

-# Create the output disk.
-#
-# Use qemu-img so we can control the output format, but capture any
-# output temporarily and only display it if the command fails.
-
-my @options = ();
-@options = ("-o", "preallocation=metadata") if $format eq
"qcow2";
-
-my @cmd = ("qemu-img", "create", "-f", $format,
@options, $output, $size);
-if ($debug) {
-    print STDERR ("running: ", join (" ", @cmd),
"\n");
-}
-
-{
-    my $tmpfh = tempfile ();
-    my ($r, $oldout, $olderr);
-
-    open $oldout, ">&STDOUT" or die __"cannot dup
STDOUT";
-    open $olderr, ">&STDERR" or die __"cannot dup
STDERR";
-    close STDOUT;
-    close STDERR;
-    open STDOUT, ">&", \$tmpfh or die __"cannot redirect
STDOUT";
-    open STDERR, ">&", \$tmpfh or die __"cannot redirect
STDERR";
-    $r = system (@cmd);
-    open STDOUT, ">&", $oldout or die __"cannot restore
STDOUT";
-    open STDERR, ">&", $olderr or die __"cannot restore
STDERR";
-
-    unless ($r == 0) {
-        print STDERR __"qemu-img create: failed to create disk
image:\n";
-        seek $tmpfh, 0, SEEK_SET;
-        print STDERR $_ while <$tmpfh>;
-        die "\n";
-    }
-}
-
 eval {
     print STDERR "starting libguestfs ...\n" if $debug;

     # Run libguestfs.
     my $g = Sys::Guestfs->new ();
+
+    # Create the output disk.
+    my %options = ();
+    $options{preallocation} = "metadata" if $format eq
"qcow2";
+    $g->disk_create ($output, $format, $size, %options);
+
     $g->add_drive ($output, format => $format);
     $g->launch ();

--
1.8.4.2