[Libguestfs] libguestfs question - multiple partitions in the guest

Richard W.M. Jones rjones at redhat.com
Wed Aug 1 21:22:02 UTC 2012


On Wed, Aug 01, 2012 at 03:31:19PM -0500, Shawn Kennedy wrote:
> I wonder - maybe an new tool (virt-grub) to dump out the grub
> content of the guest image (if linux)!! :-) :-) :-)

It's actually pretty simple to script this.  See attached
(requires a grub1 guest).

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
-------------- next part --------------
#!/usr/bin/python

import sys
import re
import guestfs

assert (len (sys.argv) == 2)
disk = sys.argv[1]

g = guestfs.GuestFS ()

# Attach the disk image read-only to libguestfs.
g.add_drive_opts (disk, readonly=1)

# Run the libguestfs back-end.
g.launch ()

# Ask libguestfs to inspect for operating systems.
roots = g.inspect_os ()

for root in roots:
    print ("root: %s" % root)

    # Mount up the disks, like guestfish -i.
    mps = g.inspect_get_mountpoints (root)
    def compare (a, b):
        if len(a[0]) > len(b[0]):
            return 1
        elif len(a[0]) == len(b[0]):
            return 0
        else:
            return -1
    mps.sort (compare)
    for mp_dev in mps:
        try:
            g.mount_ro (mp_dev[1], mp_dev[0])
        except RuntimeError as msg:
            print "%s (ignored)" % msg

    # Run Augeas on the guest.
    g.aug_init ("/", 0)

    # Display grub configuration recursively.
    def display (node):
        xs = g.aug_match ("%s/*" % node)
        for x in xs:
            try:
                y = g.aug_get (x)
                print x, " ", y
            except RuntimeError:
                pass
            display (x)

    display ("/files/etc/grub.conf")

    g.aug_close ()

    # Unmount everything.
    g.umount_all ()


More information about the Libguestfs mailing list