summaryrefslogtreecommitdiffstats
path: root/python/examples/inspect_vm.py
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2010-11-24 18:10:50 +0000
committerRichard W.M. Jones <rjones@redhat.com>2010-11-24 20:12:50 +0000
commit472722a72df89895bb11a1244eafa7915b1af116 (patch)
tree691e4b23823d7395cc6b594ab2cca3acaa904de8 /python/examples/inspect_vm.py
parent086bd1f7bfab4c10d890ecca3506a1b091c0d398 (diff)
downloadlibguestfs-472722a72df89895bb11a1244eafa7915b1af116.tar.gz
libguestfs-472722a72df89895bb11a1244eafa7915b1af116.tar.xz
libguestfs-472722a72df89895bb11a1244eafa7915b1af116.zip
python: Translate C examples into Python and include documentation.
Diffstat (limited to 'python/examples/inspect_vm.py')
-rw-r--r--python/examples/inspect_vm.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/python/examples/inspect_vm.py b/python/examples/inspect_vm.py
new file mode 100644
index 00000000..0ba6e3e9
--- /dev/null
+++ b/python/examples/inspect_vm.py
@@ -0,0 +1,57 @@
+# Example showing how to inspect a virtual machine disk.
+
+import sys
+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 ()
+if len (roots) == 0:
+ raise (Error ("inspect_vm: no operating systems found"))
+
+for root in roots:
+ print "Root device: %s" % root
+
+ # Print basic information about the operating system.
+ print " Product name: %s" % (g.inspect_get_product_name (root))
+ print " Version: %d.%d" % \
+ (g.inspect_get_major_version (root),
+ g.inspect_get_minor_version (root))
+ print " Type: %s" % (g.inspect_get_type (root))
+ print " Distro: %s" % (g.inspect_get_distro (root))
+
+ # Mount up the disks, like guestfish -i.
+ #
+ # Sort keys by length, shortest first, so that we end up
+ # mounting the filesystems in the correct order.
+ 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:
+ g.mount_ro (mp_dev[1], mp_dev[0])
+
+ # If /etc/issue.net file exists, print up to 3 lines.
+ filename = "/etc/issue.net"
+ if g.is_file (filename):
+ print "--- %s ---" % filename
+ lines = g.head_n (3, filename)
+ for line in lines: print line
+
+ # Unmount everything.
+ g.umount_all ()