Note: This requires libguestfs ≥ 1.5.23
The example below demonstrates a couple of new features of libguestfs 1.5: the core inspection API, and add_drive_opts
with optional arguments. Along with the use of Augeas to parse configuration files with ease.
You can use this as a template for getting almost any information from a guest or disk image. Examples include:
- list the user accounts in the guest
- what repositories is it configured to use
- what NTP servers does it connect to
- what were the boot messages last time it booted
- list who was logged in recently
#!/usr/bin/perl -w use Sys::Guestfs; die "Usage: whichboot.pl linuxdisk.img\n" if @ARGV == 0; # Add the disk image. my $g = Sys::Guestfs->new (); foreach (@ARGV) { $g->add_drive_opts ($_, readonly => 1); } $g->launch (); # Inspect the operating system and mount disks correctly. my @roots = $g->inspect_os (); die "no operating system detected, or OS is multi-boot" unless @roots == 1; my %fses = $g->inspect_get_mountpoints ($roots[0]); my @fses = sort { length $a <=> length $b } keys %fses; foreach (@fses) { $g->mount_ro ($fses{$_}, $_); } # Use Augeas to parse configuration files. $g->aug_init ("/", 16); # Get GRUB default boot option. my $grub_conf = "//files/boot/grub/menu.lst"; my $default = $g->aug_get ("$grub_conf/default"); # Add 1 because GRUB counts from 0, Augeas counts from 1. $default++; # Get the kernel from GRUB. my $kernel = $g->aug_get ("$grub_conf/title[$default]/kernel"); print "default boot kernel: $kernel\n"