0ba6e3e97b292b7604dbf621611fbbc0a3bcd30f
[libguestfs.git] / python / examples / inspect_vm.py
1 # Example showing how to inspect a virtual machine disk.
2
3 import sys
4 import guestfs
5
6 assert (len (sys.argv) == 2)
7 disk = sys.argv[1]
8
9 g = guestfs.GuestFS ()
10
11 # Attach the disk image read-only to libguestfs.
12 g.add_drive_opts (disk, readonly=1)
13
14 # Run the libguestfs back-end.
15 g.launch ()
16
17 # Ask libguestfs to inspect for operating systems.
18 roots = g.inspect_os ()
19 if len (roots) == 0:
20     raise (Error ("inspect_vm: no operating systems found"))
21
22 for root in roots:
23     print "Root device: %s" % root
24
25     # Print basic information about the operating system.
26     print "  Product name: %s" % (g.inspect_get_product_name (root))
27     print "  Version:      %d.%d" % \
28         (g.inspect_get_major_version (root),
29          g.inspect_get_minor_version (root))
30     print "  Type:         %s" % (g.inspect_get_type (root))
31     print "  Distro:       %s" % (g.inspect_get_distro (root))
32
33     # Mount up the disks, like guestfish -i.
34     #
35     # Sort keys by length, shortest first, so that we end up
36     # mounting the filesystems in the correct order.
37     mps = g.inspect_get_mountpoints (root)
38     def compare (a, b):
39         if len(a[0]) > len(b[0]):
40             return 1
41         elif len(a[0]) == len(b[0]):
42             return 0
43         else:
44             return -1
45     mps.sort (compare)
46     for mp_dev in mps:
47         g.mount_ro (mp_dev[1], mp_dev[0])
48
49     # If /etc/issue.net file exists, print up to 3 lines.
50     filename = "/etc/issue.net"
51     if g.is_file (filename):
52         print "--- %s ---" % filename
53         lines = g.head_n (3, filename)
54         for line in lines: print line
55
56     # Unmount everything.
57     g.umount_all ()