=cut
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
+=cut
+
my $output = "text";
=back
GetOptions ("help|?" => \$help,
"version" => \$version,
"connect|c=s" => \$uri,
+ "format=s" => \$format,
"text" => sub { $output = "text" },
"none" => sub { $output = "none" },
"xml" => sub { $output = "xml" },
if ($uri) {
my ($conn, $dom);
($g, $conn, $dom, @images) =
- open_guest (\@ARGV, rw => $rw, address => $uri);
+ open_guest (\@ARGV, rw => $rw, address => $uri, format => $format);
} else {
my ($conn, $dom);
($g, $conn, $dom, @images) =
- open_guest (\@ARGV, rw => $rw);
+ open_guest (\@ARGV, rw => $rw, format => $format);
}
$g->launch ();
}
foreach (@images) {
- printf "-a %s ", shell_quote ($_);
+ unless (defined $_->[1]) {
+ printf "-a %s ", shell_quote ($_->[0]);
+ } else {
+ printf "--format %s -a %s ",
+ shell_quote ($_->[1]), shell_quote ($_->[0]);
+ }
}
my $mounts = $oses->{$root_dev}->{mounts};
$g = open_guest ($name, address => $uri, ...);
- $g = open_guest ([$img1, $img2, ...], address => $uri, ...);
+ $g = open_guest ([$img1, $img2, ...], address => $uri, format => $format, ...);
($g, $conn, $dom, @images) = open_guest ($name);
The first parameter is either a string referring to a libvirt domain
or a disk image, or (if a guest has several disk images) an arrayref
-C<[$img1, $img2, ...]>.
+C<[$img1, $img2, ...]>. For disk images, if the C<format> parameter
+is specified then that format is forced.
The handle is I<read-only> by default. Use the optional parameter
C<rw =E<gt> 1> to open a read-write handle. However if you open a
you call the function in C<wantarray> context, in which case the
function returns a tuple of: the open libguestfs handle, the open
libvirt handle, and the open libvirt domain handle, and a list of
-images. (This is useful if you want to do other things like pulling
-the XML description of the guest). Note that if this is a straight
-disk image, then C<$conn> and C<$dom> will be C<undef>.
+[image,format] pairs. (This is useful if you want to do other things
+like pulling the XML description of the guest). Note that if this is
+a straight disk image, then C<$conn> and C<$dom> will be C<undef>.
If the C<Sys::Virt> module is not available, then libvirt is bypassed,
and this function can only open disk images.
-The optional C<interface> parameter can be used to open devices with
-C<add_drive{,_ro}_with_if>. See
-L<Sys::Guestfs/guestfs_add_drive_with_if> for more details.
+The optional C<interface> parameter can be used to open devices with a
+specified qemu interface. See L<Sys::Guestfs/guestfs_add_drive_opts>
+for more details.
=cut
my $rw = $params{rw};
my $address = $params{address};
my $interface = $params{interface};
+ my $format = $params{format}; # undef == autodetect
my @images = ();
if (ref ($first) eq "ARRAY") {
imagename => $_)
unless -r $_;
}
+
+ @images = map { [ $_, $format ] } @images;
} else {
die __"open_guest: no libvirt support (install Sys::Virt, XML::XPath and XML::XPath::XMLParser)"
unless exists $INC{"Sys/Virt.pm"} &&
my $xml = $dom->get_xml_description ();
my $p = XML::XPath->new (xml => $xml);
- my @disks = $p->findnodes ('//devices/disk/source/@dev');
- push (@disks, $p->findnodes ('//devices/disk/source/@file'));
+ my $nodes = $p->find ('//devices/disk');
+
+ my @disks = ();
+ my $node;
+ foreach $node ($nodes->get_nodelist) {
+ # The filename can be in dev or file attribute, hence:
+ my $filename = $p->find ('./source/@dev', $node);
+ unless ($filename) {
+ $filename = $p->find ('./source/@file', $node);
+ next unless $filename;
+ }
+ $filename = $filename->to_literal;
+
+ # Get the disk format (may not be set).
+ my $format = $p->find ('./driver/@type', $node);
+ $format = $format->to_literal if $format;
+
+ push @disks, [ $filename, $format ];
+ }
die __x("{imagename} seems to have no disk devices\n",
imagename => $images[0])
unless @disks;
- @images = map { $_->getData } @disks;
+ @images = @disks;
}
# We've now got the list of @images, so feed them to libguestfs.
my $g = Sys::Guestfs->new ();
foreach (@images) {
- if ($rw) {
- if ($interface) {
- $g->add_drive_with_if ($_, $interface);
- } else {
- $g->add_drive ($_);
- }
- } else {
- if ($interface) {
- $g->add_drive_ro_with_if ($_, $interface);
- } else {
- $g->add_drive_ro ($_);
- }
- }
+ my @args = ($_->[0]);
+ push @args, format => $_->[1] if defined $_->[1];
+ push @args, readonly => 1 unless $rw;
+ push @args, iface => $interface if defined $interface;
+ $g->add_drive_opts (@args);
}
return wantarray ? ($g, $conn, $dom, @images) : $g
If you specify guest block devices directly, then libvirt is not used
at all.
+=cut
+
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
=back
=cut
GetOptions ("help|?" => \$help,
"version" => \$version,
"connect|c=s" => \$uri,
+ "format=s" => \$format,
) or pod2usage (2);
pod2usage (1) if $help;
if ($version) {
my $g;
if ($uri) {
- $g = open_guest (\@ARGV, address => $uri);
+ $g = open_guest (\@ARGV, address => $uri, format => $format);
} else {
- $g = open_guest (\@ARGV);
+ $g = open_guest (\@ARGV, format => $format);
}
$g->launch ();
=cut
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
+=cut
+
my $expr;
=item B<--expr EXPR> | B<-e EXPR>
GetOptions ("help|?" => \$help,
"version" => \$version,
"connect|c=s" => \$uri,
+ "format=s" => \$format,
"expr|e=s" => \$expr,
"backup|b=s" => \$backup,
) or pod2usage (2);
my $g;
if ($uri) {
- $g = open_guest (\@ARGV, address => $uri, rw => 1);
+ $g = open_guest (\@ARGV, address => $uri, rw => 1, format => $format);
} else {
- $g = open_guest (\@ARGV, rw => 1);
+ $g = open_guest (\@ARGV, rw => 1, format => $format);
}
$g->launch ();
=cut
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
+=cut
+
my $long;
=item B<-l> | B<--long>
GetOptions ("help|?" => \$help,
"version" => \$version,
"connect|c=s" => \$uri,
+ "format=s" => \$format,
"long|l" => \$long,
"all|a" => \$all,
) or pod2usage (2);
my $g;
if ($uri) {
- $g = open_guest (\@ARGV, address => $uri);
+ $g = open_guest (\@ARGV, address => $uri, format => $format);
} else {
- $g = open_guest (\@ARGV);
+ $g = open_guest (\@ARGV, format => $format);
}
$g->launch ();
=cut
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
+=cut
+
my $human;
=item B<-h> | B<--human-readable>
GetOptions ("help|?" => \$help,
"version" => \$version,
"connect|c=s" => \$uri,
+ "format=s" => \$format,
"human-readable|h" => \$human,
"long|l" => \$long,
"total|t" => \$total,
my $g;
if ($uri) {
- $g = open_guest (\@ARGV, address => $uri);
+ $g = open_guest (\@ARGV, address => $uri, format => $format);
} else {
- $g = open_guest (\@ARGV);
+ $g = open_guest (\@ARGV, format => $format);
}
$g->launch ();
=cut
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
+=cut
+
my $mode;
=item B<-l> | B<--long>
GetOptions ("help|?" => \$help,
"version" => \$version,
"connect|c=s" => \$uri,
+ "format=s" => \$format,
"long|l" => \&set_mode_l,
"recursive|R" => \&set_mode_R,
) or pod2usage (2);
my $g;
if ($uri) {
- $g = open_guest (\@ARGV, address => $uri);
+ $g = open_guest (\@ARGV, address => $uri, format => $format);
} else {
- $g = open_guest (\@ARGV);
+ $g = open_guest (\@ARGV, format => $format);
}
$g->launch ();
=cut
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
+=cut
+
my $memsize;
=item B<--memsize MB> | B<-m MB>
"version" => \$version,
"append=s" => \$append,
"connect|c=s" => \$uri,
+ "format=s" => \$format,
"memsize|m=i" => \$memsize,
"ro|r" => \$readonly,
"selinux" => \$selinux,
my @args = (\@ARGV);
push @args, address => $uri if $uri;
push @args, rw => 1 unless $readonly;
+push @args, format => $format if defined $format;
my $g = open_guest (@args);
# Setting "direct mode" is required for the rescue appliance.
=cut
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
+=cut
+
my $mode;
=item B<-x> | B<--extract> | B<--download>
GetOptions ("help|?" => \$help,
"version" => \$version,
"connect|c=s" => \$uri,
+ "format=s" => \$format,
"extract|download|x" => \&set_mode_x,
"upload|u" => \&set_mode_u,
"gzip|z" => \$gzip,
my @args = (\@ARGV);
push @args, address => $uri if $uri;
push @args, rw => 1 if $mode eq "u";
+push @args, format => $format if defined $format;
my $g = open_guest (@args);
$g->launch ();
=cut
+my $format;
+
+=item B<--format> raw
+
+Specify the format of disk images given on the command line. If this
+is omitted then the format is autodetected from the content of the
+disk image.
+
+If disk images are requested from libvirt, then this program asks
+libvirt for this information. In this case, the value of the format
+parameter is ignored.
+
+If working with untrusted raw-format guest disk images, you should
+ensure the format is always specified.
+
+=cut
+
my $merge;
=item B<--merge>
"version" => \$version,
"connect|c=s" => \$uri,
"debug|d" => \$debug,
+ "format=s" => \$format,
"merge" => \$merge,
"encoding=s" => \$encoding,
) or pod2usage (2);
my @lib_args = ([$domname_or_image]);
push @lib_args, address => $uri if $uri;
push @lib_args, rw => 1 if $merge;
+push @lib_args, format => $format if defined $format;
my $g = open_guest (@lib_args);
$g->launch ();