Sys::Guestfs::Lib: Exit with error if a libvirt domain appears to have no disks.
[libguestfs.git] / perl / lib / Sys / Guestfs / Lib.pm
index fc5173a..27a7b9e 100644 (file)
@@ -21,6 +21,7 @@ use strict;
 use warnings;
 
 use Sys::Guestfs;
+use File::Temp qw/tempdir/;
 
 # Optional:
 eval "use Sys::Virt;";
@@ -35,10 +36,14 @@ Sys::Guestfs::Lib - Useful functions for using libguestfs from Perl
 
 =head1 SYNOPSIS
 
- use Sys::Guestfs::Lib qw(#any symbols you want to use);
+ use Sys::Guestfs::Lib qw(open_guest inspect_all_partitions ...);
 
  $g = open_guest ($name);
 
+ %fses = inspect_all_partitions ($g, \@partitions);
+
+(and many more calls - see the rest of this manpage)
+
 =head1 DESCRIPTION
 
 C<Sys::Guestfs::Lib> is an extra library of useful functions for using
@@ -114,6 +119,7 @@ and this function can only open disk images.
 
 sub open_guest
 {
+    local $_;
     my $first = shift;
     my %params = @_;
 
@@ -168,6 +174,10 @@ sub open_guest
 
        my $p = XML::XPath->new (xml => $xml);
        my @disks = $p->findnodes ('//devices/disk/source/@dev');
+       push (@disks, $p->findnodes ('//devices/disk/source/@file'));
+
+       die "$images[0] seems to have no disk devices\n" unless @disks;
+
        @images = map { $_->getData } @disks;
     }
 
@@ -204,14 +214,14 @@ sub get_partitions
 
     my @partitions = $g->list_partitions ();
     my @pvs = $g->pvs ();
-    @partitions = grep { ! is_pv ($_, @pvs) } @partitions;
+    @partitions = grep { ! _is_pv ($_, @pvs) } @partitions;
 
     my @lvs = $g->lvs ();
 
     return sort (@lvs, @partitions);
 }
 
-sub is_pv {
+sub _is_pv {
     local $_;
     my $t = shift;
 
@@ -528,7 +538,7 @@ sub inspect_partition
        if ($g->is_file ("/grub/menu.lst") ||
            $g->is_file ("/grub/grub.conf")) {
            $r{content} = "linux-grub";
-           check_grub ($g, \%r);
+           _check_grub ($g, \%r);
            goto OUT;
        }
 
@@ -537,7 +547,7 @@ sub inspect_partition
            $g->is_file ("/etc/fstab")) {
            $r{content} = "linux-root";
            $r{is_root} = 1;
-           check_linux_root ($g, \%r);
+           _check_linux_root ($g, \%r);
            goto OUT;
        }
 
@@ -568,7 +578,7 @@ sub inspect_partition
            $r{fsos} = "windows";
            $r{content} = "windows-root";
            $r{is_root} = 1;
-           check_windows_root ($g, \%r, $use_windows_registry);
+           _check_windows_root ($g, \%r, $use_windows_registry);
            goto OUT;
        }
     }
@@ -578,7 +588,7 @@ sub inspect_partition
     return \%r;
 }
 
-sub check_linux_root
+sub _check_linux_root
 {
     local $_;
     my $g = shift;
@@ -639,7 +649,7 @@ sub check_linux_root
 # XXX We could parse this better.  This won't work if /boot.ini is on
 # a different drive from the %systemroot%, and in other unusual cases.
 
-sub check_windows_root
+sub _check_windows_root
 {
     local $_;
     my $g = shift;
@@ -669,13 +679,13 @@ sub check_windows_root
        if (defined $systemroot) {
            $r->{systemroot} = resolve_windows_path ($g, "/$systemroot");
            if (defined $r->{systemroot} && $use_windows_registry) {
-               check_windows_registry ($g, $r, $r->{systemroot});
+               _check_windows_registry ($g, $r, $r->{systemroot});
            }
        }
     }
 }
 
-sub check_windows_registry
+sub _check_windows_registry
 {
     local $_;
     my $g = shift;
@@ -689,18 +699,18 @@ sub check_windows_registry
     if (defined $configdir) {
        my $softwaredir = resolve_windows_path ($g, "$configdir/software");
        if (defined $softwaredir) {
-           load_windows_registry ($g, $r, $softwaredir,
-                                  "HKEY_LOCAL_MACHINE\\SOFTWARE");
+           _load_windows_registry ($g, $r, $softwaredir,
+                                   "HKEY_LOCAL_MACHINE\\SOFTWARE");
        }
        my $systemdir = resolve_windows_path ($g, "$configdir/system");
        if (defined $systemdir) {
-           load_windows_registry ($g, $r, $systemdir,
-                                  "HKEY_LOCAL_MACHINE\\System");
+           _load_windows_registry ($g, $r, $systemdir,
+                                   "HKEY_LOCAL_MACHINE\\System");
        }
     }
 }
 
-sub load_windows_registry
+sub _load_windows_registry
 {
     local $_;
     my $g = shift;
@@ -751,7 +761,7 @@ sub load_windows_registry
     $r->{registry} = \@registry;
 }
 
-sub check_grub
+sub _check_grub
 {
     local $_;
     my $g = shift;
@@ -843,8 +853,8 @@ sub inspect_operating_systems
                root => $fses->{$_},
                root_device => $_
                );
-           get_os_version ($g, \%r);
-           assign_mount_points ($g, $fses, \%r);
+           _get_os_version ($g, \%r);
+           _assign_mount_points ($g, $fses, \%r);
            $oses{$_} = \%r;
        }
     }
@@ -852,7 +862,7 @@ sub inspect_operating_systems
     return \%oses;
 }
 
-sub get_os_version
+sub _get_os_version
 {
     local $_;
     my $g = shift;
@@ -863,7 +873,7 @@ sub get_os_version
     $r->{version} = $r->{root}->{osversion} if exists $r->{root}->{osversion};
 }
 
-sub assign_mount_points
+sub _assign_mount_points
 {
     local $_;
     my $g = shift;
@@ -879,7 +889,7 @@ sub assign_mount_points
        foreach (@fstab) {
            my ($spec, $file) = @$_;
 
-           my ($dev, $fs) = find_filesystem ($g, $fses, $spec);
+           my ($dev, $fs) = _find_filesystem ($g, $fses, $spec);
            if ($dev) {
                $r->{mounts}->{$file} = $dev;
                $r->{filesystems}->{$dev} = $fs;
@@ -895,7 +905,7 @@ sub assign_mount_points
 }
 
 # Find filesystem by device name, LABEL=.. or UUID=..
-sub find_filesystem
+sub _find_filesystem
 {
     my $g = shift;
     my $fses = shift;
@@ -1022,15 +1032,15 @@ sub inspect_in_detail
     my $g = shift;
     my $os = shift;
 
-    check_for_applications ($g, $os);
-    check_for_kernels ($g, $os);
+    _check_for_applications ($g, $os);
+    _check_for_kernels ($g, $os);
     if ($os->{os} eq "linux") {
-       check_for_modprobe_aliases ($g, $os);
-       check_for_initrd ($g, $os);
+       _check_for_modprobe_aliases ($g, $os);
+       _check_for_initrd ($g, $os);
     }
 }
 
-sub check_for_applications
+sub _check_for_applications
 {
     local $_;
     my $g = shift;
@@ -1073,7 +1083,7 @@ sub check_for_applications
     $os->{apps} = \@apps;
 }
 
-sub check_for_kernels
+sub _check_for_kernels
 {
     local $_;
     my $g = shift;
@@ -1121,7 +1131,7 @@ sub check_for_kernels
 #
 # XXX This doesn't look beyond /etc/modprobe.conf, eg. in /etc/modprobe.d/
 
-sub check_for_modprobe_aliases
+sub _check_for_modprobe_aliases
 {
     local $_;
     my $g = shift;
@@ -1185,7 +1195,7 @@ sub check_for_modprobe_aliases
 # Get a listing of device drivers in any initrd corresponding to a
 # kernel.  This is an indication of what can possibly be booted.
 
-sub check_for_initrd
+sub _check_for_initrd
 {
     local $_;
     my $g = shift;