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

SEE ALSO

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

AUTHORS

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

LICENSE

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

BUGS

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