1guestfs-python(3)           Virtualization Support           guestfs-python(3)
2
3
4

NAME

6       guestfs-python - How to use libguestfs from Python
7

SYNOPSIS

9        import guestfs
10        g = guestfs.GuestFS(python_return_dict=True)
11        g.add_drive_opts("disk.img", format="raw", readonly=1)
12        g.launch()
13

DESCRIPTION

15       This manual page documents how to call libguestfs from the Python
16       programming language.  This page just documents the differences from
17       the C API and gives some examples.  If you are not familiar with using
18       libguestfs, you also need to read guestfs(3).
19
20   python_return_dict=True
21       All new code should construct the handle using:
22
23        g = guestfs.GuestFS(python_return_dict=True)
24
25       This indicates that your program wants to receive Python dicts for
26       methods in the API that return hashtables.
27
28       In a future version of libguestfs, this will become the default.
29
30   EXCEPTIONS
31       Errors from libguestfs functions are mapped into "RuntimeException"
32       with a single string argument which is the error message.
33
34   MORE DOCUMENTATION
35       Type:
36
37        $ python
38        >>> import guestfs
39        >>> help(guestfs)
40
41   USING PYTHON BINDINGS IN A VIRTUALENV
42       These bindings are not available in pypi owing to a licensing problem.
43       See https://bugzilla.redhat.com/1075594 for current status.
44
45       However we do publish a Python distribution for selected stable
46       versions of libguestfs in http://libguestfs.org/download/python.  You
47       can use it like this:
48
49        pip install http://libguestfs.org/download/python/guestfs-1.XX.YY.tar.gz
50

EXAMPLE 1: CREATE A DISK IMAGE

52        # Example showing how to create a disk image.
53
54        import guestfs
55
56        output = "disk.img"
57
58        # All new Python code should pass python_return_dict=True
59        # to the constructor.  It indicates that your program wants
60        # to receive Python dicts for methods in the API that return
61        # hashtables.
62        g = guestfs.GuestFS(python_return_dict=True)
63
64        # Create a raw-format sparse disk image, 512 MB in size.
65        g.disk_create(output, "raw", 512 * 1024 * 1024)
66
67        # Set the trace flag so that we can see each libguestfs call.
68        g.set_trace(1)
69
70        # Attach the disk image to libguestfs.
71        g.add_drive_opts(output, format="raw", readonly=0)
72
73        # Run the libguestfs back-end.
74        g.launch()
75
76        # Get the list of devices.  Because we only added one drive
77        # above, we expect that this list should contain a single
78        # element.
79        devices = g.list_devices()
80        assert(len(devices) == 1)
81
82        # Partition the disk as one single MBR partition.
83        g.part_disk(devices[0], "mbr")
84
85        # Get the list of partitions.  We expect a single element, which
86        # is the partition we have just created.
87        partitions = g.list_partitions()
88        assert(len(partitions) == 1)
89
90        # Create a filesystem on the partition.
91        g.mkfs("ext4", partitions[0])
92
93        # Now mount the filesystem so that we can add files.
94        g.mount(partitions[0], "/")
95
96        # Create some files and directories.
97        g.touch("/empty")
98        message = "Hello, world\n"
99        g.write("/hello", message)
100        g.mkdir("/foo")
101
102        # This one uploads the local file /etc/resolv.conf into
103        # the disk image.
104        g.upload("/etc/resolv.conf", "/foo/resolv.conf")
105
106        # Because we wrote to the disk and we want to detect write
107        # errors, call g.shutdown.  You don't need to do this:
108        # g.close will do it implicitly.
109        g.shutdown()
110
111        # Note also that handles are automatically closed if they are
112        # reaped by reference counting.  You only need to call close
113        # if you want to close the handle right away.
114        g.close()
115

EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE

117        # Example showing how to inspect a virtual machine disk.
118
119        import sys
120        import guestfs
121
122        assert(len(sys.argv) == 2)
123        disk = sys.argv[1]
124
125        # All new Python code should pass python_return_dict=True
126        # to the constructor.  It indicates that your program wants
127        # to receive Python dicts for methods in the API that return
128        # hashtables.
129        g = guestfs.GuestFS(python_return_dict=True)
130
131        # Attach the disk image read-only to libguestfs.
132        g.add_drive_opts(disk, readonly=1)
133
134        # Run the libguestfs back-end.
135        g.launch()
136
137        # Ask libguestfs to inspect for operating systems.
138        roots = g.inspect_os()
139        if len(roots) == 0:
140            raise(Error("inspect_vm: no operating systems found"))
141
142        for root in roots:
143            print "Root device: %s" % root
144
145            # Print basic information about the operating system.
146            print "  Product name: %s" % (g.inspect_get_product_name(root))
147            print "  Version:      %d.%d" % \
148                (g.inspect_get_major_version(root),
149                 g.inspect_get_minor_version(root))
150            print "  Type:         %s" % (g.inspect_get_type(root))
151            print "  Distro:       %s" % (g.inspect_get_distro(root))
152
153            # Mount up the disks, like guestfish -i.
154            #
155            # Sort keys by length, shortest first, so that we end up
156            # mounting the filesystems in the correct order.
157            mps = g.inspect_get_mountpoints(root)
158            def compare(a, b):
159                return len(a) - len(b)
160            for device in sorted(mps.keys(), compare):
161                try:
162                    g.mount_ro(mps[device], device)
163                except RuntimeError as msg:
164                    print "%s (ignored)" % msg
165
166            # If /etc/issue.net file exists, print up to 3 lines.
167            filename = "/etc/issue.net"
168            if g.is_file(filename):
169                print "--- %s ---" % filename
170                lines = g.head_n(3, filename)
171                for line in lines:
172                    print line
173
174            # Unmount everything.
175            g.umount_all()
176

SEE ALSO

178       guestfs(3), guestfs-examples(3), guestfs-erlang(3), guestfs-gobject(3),
179       guestfs-golang(3), guestfs-java(3), guestfs-lua(3), guestfs-ocaml(3),
180       guestfs-perl(3), guestfs-recipes(1), guestfs-ruby(3),
181       http://libguestfs.org/.
182

AUTHORS

184       Richard W.M. Jones ("rjones at redhat dot com")
185
187       Copyright (C) 2010-2012 Red Hat Inc.
188

LICENSE

190       This manual page contains examples which we hope you will use in your
191       programs.  The examples may be freely copied, modified and distributed
192       for any purpose without any restrictions.
193

BUGS

195       To get a list of bugs against libguestfs, use this link:
196       https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools
197
198       To report a new bug against libguestfs, use this link:
199       https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools
200
201       When reporting a bug, please supply:
202
203       ·   The version of libguestfs.
204
205       ·   Where you got libguestfs (eg. which Linux distro, compiled from
206           source, etc)
207
208       ·   Describe the bug accurately and give a way to reproduce it.
209
210       ·   Run libguestfs-test-tool(1) and paste the complete, unedited output
211           into the bug report.
212
213
214
215libguestfs-1.38.2                 2018-05-15                 guestfs-python(3)
Impressum