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

NAME

6       guestfs-lua - How to use libguestfs from Lua
7

SYNOPSIS

9        local G = require "guestfs"
10        g = G.create ()
11        g:add_drive ("test.img", { format = "raw", readonly = true })
12        g:launch ()
13        devices = g:list_devices ()
14        g:close ()
15

DESCRIPTION

17       This manual page documents how to call libguestfs from the Lua
18       programming language.  This page just documents the differences from
19       the C API and gives some examples.  If you are not familiar with using
20       libguestfs, you also need to read guestfs(3).
21
22   REQUIRING THE MODULE
23       "require "guestfs"" returns the module, so you have to assign it to a
24       local variable.  Typical usage is:
25
26        local G = require "guestfs"
27
28       (you can use any name you want instead of "G", but in the examples in
29       this man page we always use "G").
30
31   OPENING AND CLOSING THE HANDLE
32       To create a new handle, call:
33
34        g = G.create ()
35
36       You can also use the optional arguments:
37
38        g = G.create { environment = 0, close_on_exit = 0 }
39
40       to set the flags "GUESTFS_CREATE_NO_ENVIRONMENT" and/or
41       "GUESTFS_CREATE_NO_CLOSE_ON_EXIT".
42
43       The handle will be closed by the garbage collector, but you can also
44       close it explicitly by doing:
45
46        g:close ()
47
48   CALLING METHODS
49       Use the ordinary Lua convention for calling methods on the handle.  For
50       example:
51
52        g:set_verbose (true)
53
54   FUNCTIONS WITH OPTIONAL ARGUMENTS
55       For functions that take optional arguments, the first arguments are the
56       non-optional ones.  The optional final argument is a table supplying
57       the optional arguments.
58
59        g:add_drive ("test.img")
60
61       or:
62
63        g:add_drive ("test.img", { format = "raw", readonly = true })
64
65   64 BIT VALUES
66       Currently 64 bit values must be passed as strings, and are returned as
67       strings.  This is because 32 bit Lua cannot handle 64 bit integers
68       properly.  We hope to come up with a better solution later.
69
70   ERRORS
71       Most (but not all) errors are converted into objects (ie. tables)
72       containing the following fields:
73
74       msg The error message (corresponding to "guestfs_last_error" in
75           guestfs(3)).
76
77       code
78           The "errno" (corresponding to "guestfs_last_errno" in guestfs(3)).
79
80       These objects also have "__tostring" functions attached to them so you
81       can use "tostring" (or implicit conversion) to convert them into
82       printable strings.
83
84       Note that the library also throws some errors as plain strings.  You
85       may need to check the type.
86
87   EVENTS
88       Events can be registered by calling "set_event_callback":
89
90        eh = g:set_event_callback (cb, "close")
91
92       or to register a single callback for multiple events make the second
93       argument a list:
94
95        eh = g:set_event_callback (cb, { "appliance", "library", "trace" })
96
97       A list of all valid event types (strings) is in the global variable
98       "G.event_all".
99
100       The callback ("cb") is called with the following parameters:
101
102        function cb (g, event, eh, flags, buf, array)
103          -- g is the guestfs handle
104          -- event is a string which is the name of the event that fired
105          -- flags is always zero
106          -- buf is the data buffer (eg. log message etc)
107          -- array is the array of 64 bit ints (eg. progress bar status etc)
108          ...
109        end
110
111       You can also remove a callback using the event handle ("eh") that was
112       returned when you registered the callback:
113
114        g:delete_event_callback (eh)
115

EXAMPLE 1: CREATE A DISK IMAGE

117        -- Example showing how to create a disk image.
118
119        local G = require "guestfs"
120
121        local output = "disk.img"
122
123        local g = G.create ()
124
125        -- Create a raw-format sparse disk image, 512 MB in size.
126        file = io.open (output, "w")
127        file:seek ("set", 512 * 1024 * 1024)
128        file:write (' ')
129        file:close ()
130
131        -- Set the trace flag so that we can see each libguestfs call.
132        g:set_trace (true)
133
134        -- Attach the disk image to libguestfs.
135        g:add_drive (output, { format = "raw", readonly = false })
136
137        -- Run the libguestfs back-end.
138        g:launch ()
139
140        -- Get the list of devices.  Because we only added one drive
141        -- above, we expect that this list should contain a single
142        -- element.
143        devices = g:list_devices ()
144        if table.getn (devices) ~= 1 then
145           error "expected a single device from list-devices"
146        end
147
148        -- Partition the disk as one single MBR partition.
149        g:part_disk (devices[1], "mbr")
150
151        -- Get the list of partitions.  We expect a single element, which
152        -- is the partition we have just created.
153        partitions = g:list_partitions ()
154        if table.getn (partitions) ~= 1 then
155           error "expected a single partition from list-partitions"
156        end
157
158        -- Create a filesystem on the partition.
159        g:mkfs ("ext4", partitions[1])
160
161        -- Now mount the filesystem so that we can add files.
162        g:mount (partitions[1], "/")
163
164        -- Create some files and directories.
165        g:touch ("/empty")
166        message = "Hello, world\n"
167        g:write ("/hello", message)
168        g:mkdir ("/foo")
169
170        -- This one uploads the local file /etc/resolv.conf into
171        -- the disk image.
172        g:upload ("/etc/resolv.conf", "/foo/resolv.conf")
173
174        -- Because we wrote to the disk and we want to detect write
175        -- errors, call g:shutdown.  You don't need to do this:
176        -- g:close will do it implicitly.
177        g:shutdown ()
178
179        -- Note also that handles are automatically closed if they are
180        -- reaped by the garbage collector.  You only need to call close
181        -- if you want to close the handle right away.
182        g:close ()
183

EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE

185        -- Example showing how to inspect a virtual machine disk.
186
187        local G = require "guestfs"
188
189        if table.getn (arg) == 1 then
190           disk = arg[1]
191        else
192           error ("usage: inspect_vm disk.img")
193        end
194
195        local g = G.create ()
196
197        -- Attach the disk image read-only to libguestfs.
198        g:add_drive (disk, { -- format:"raw"
199                             readonly = true })
200
201        -- Run the libguestfs back-end.
202        g:launch ()
203
204        -- Ask libguestfs to inspect for operating systems.
205        local roots = g:inspect_os ()
206        if table.getn (roots) == 0 then
207           error ("inspect_vm: no operating systems found")
208        end
209
210        for _, root in ipairs (roots) do
211           print ("Root device: ", root)
212
213           -- Print basic information about the operating system.
214           print ("  Product name: ", g:inspect_get_product_name (root))
215           print ("  Version:      ",
216                  g:inspect_get_major_version (root),
217                  g:inspect_get_minor_version (root))
218           print ("  Type:         ", g:inspect_get_type (root))
219           print ("  Distro:       ", g:inspect_get_distro (root))
220
221           -- Mount up the disks, like guestfish -i.
222           --
223           -- Sort keys by length, shortest first, so that we end up
224           -- mounting the filesystems in the correct order.
225           mps = g:inspect_get_mountpoints (root)
226           table.sort (mps,
227                       function (a, b)
228                          return string.len (a) < string.len (b)
229                       end)
230           for mp,dev in pairs (mps) do
231              pcall (function () g:mount_ro (dev, mp) end)
232           end
233
234           -- If /etc/issue.net file exists, print up to 3 lines.
235           filename = "/etc/issue.net"
236           if g:is_file (filename) then
237              print ("--- ", filename, " ---")
238              lines = g:head_n (3, filename)
239              for _, line in ipairs (lines) do
240                 print (line)
241              end
242           end
243
244           -- Unmount everything.
245           g:umount_all ()
246        end
247

SEE ALSO

249       guestfs(3), guestfs-examples(3), guestfs-erlang(3), guestfs-gobject(3),
250       guestfs-golang(3), guestfs-java(3), guestfs-ocaml(3), guestfs-perl(3),
251       guestfs-python(3), guestfs-recipes(1), guestfs-ruby(3),
252       http://www.lua.org/, http://libguestfs.org/.
253

AUTHORS

255       Richard W.M. Jones ("rjones at redhat dot com")
256
258       Copyright (C) 2012 Red Hat Inc.
259

LICENSE

261       This manual page contains examples which we hope you will use in your
262       programs.  The examples may be freely copied, modified and distributed
263       for any purpose without any restrictions.
264

BUGS

266       To get a list of bugs against libguestfs, use this link:
267       https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools
268
269       To report a new bug against libguestfs, use this link:
270       https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools
271
272       When reporting a bug, please supply:
273
274       •   The version of libguestfs.
275
276       •   Where you got libguestfs (eg. which Linux distro, compiled from
277           source, etc)
278
279       •   Describe the bug accurately and give a way to reproduce it.
280
281       •   Run libguestfs-test-tool(1) and paste the complete, unedited output
282           into the bug report.
283
284
285
286libguestfs-1.45.4                 2021-04-03                    guestfs-lua(3)
Impressum