File: inspect_vm.py

package info (click to toggle)
libguestfs 1%3A1.40.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,660 kB
  • sloc: ansic: 460,074; ml: 63,059; sh: 14,955; java: 9,512; makefile: 9,133; cs: 6,300; haskell: 5,652; python: 3,856; perl: 3,619; erlang: 2,435; xml: 1,683; ruby: 350; pascal: 255; lex: 135; yacc: 128; cpp: 10
file content (59 lines) | stat: -rw-r--r-- 1,814 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Example showing how to inspect a virtual machine disk.

import sys
import guestfs

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

# All new Python code should pass python_return_dict=True
# to the constructor.  It indicates that your program wants
# to receive Python dicts for methods in the API that return
# hashtables.
g = guestfs.GuestFS(python_return_dict=True)

# 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):
        return len(a) - len(b)
    for device in sorted(mps.keys(), compare):
        try:
            g.mount_ro(mps[device], device)
        except RuntimeError as msg:
            print "%s (ignored)" % msg

    # 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()