daemon: debug segv correct use of dereferencing NULL.
[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         try:
48             g.mount_ro (mp_dev[1], mp_dev[0])
49         except RuntimeError as msg:
50             print "%s (ignored)" % msg
51
52     # If /etc/issue.net file exists, print up to 3 lines.
53     filename = "/etc/issue.net"
54     if g.is_file (filename):
55         print "--- %s ---" % filename
56         lines = g.head_n (3, filename)
57         for line in lines: print line
58
59     # Unmount everything.
60     g.umount_all ()