5 guestfs - Library for accessing and modifying virtual machine images
11 guestfs_h *handle = guestfs_create ();
12 guestfs_add_drive (handle, "guest.img");
13 guestfs_launch (handle);
14 guestfs_wait_ready (handle);
15 guestfs_mount (handle, "/dev/sda1", "/");
16 guestfs_touch (handle, "/hello");
17 guestfs_sync (handle);
18 guestfs_close (handle);
22 Libguestfs is a library for accessing and modifying guest disk images.
23 Amongst the things this is good for: making batch configuration
24 changes to guests, getting disk used/free statistics (see also:
25 virt-df), migrating between virtualization systems (see also:
26 virt-p2v), performing partial backups, performing partial guest
27 clones, cloning guests and changing registry/UUID/hostname info, and
30 Libguestfs uses Linux kernel and qemu code, and can access any type of
31 guest filesystem that Linux and qemu can, including but not limited
32 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
33 schemes, qcow, qcow2, vmdk.
35 Libguestfs provides ways to enumerate guest storage (eg. partitions,
36 LVs, what filesystem is in each LV, etc.). It can also run commands
37 in the context of the guest. Also you can access filesystems over FTP.
39 Libguestfs is a library that can be linked with C and C++ management
40 programs (or management programs written in OCaml, Perl or Python).
41 You can also use it from shell scripts or the command line.
43 You don't need to be root to use libguestfs, although obviously you do
44 need enough permissions to access the disk images.
46 =head1 CONNECTION MANAGEMENT
48 If you are using the high-level API, then you should call the
49 functions in the following order:
51 guestfs_h *handle = guestfs_create ();
53 guestfs_add_drive (handle, "guest.img");
54 /* call guestfs_add_drive additional times if the guest has
58 guestfs_launch (handle);
59 guestfs_wait_ready (handle);
61 /* now you can examine what partitions, LVs etc are available
62 * you have to mount / at least
64 guestfs_mount (handle, "/dev/sda1", "/");
66 /* now you can perform actions on the guest disk image */
67 guestfs_touch (handle, "/hello");
69 /* you only need to call guestfs_sync if you have made
70 * changes to the guest image
72 guestfs_sync (handle);
74 guestfs_close (handle);
76 C<guestfs_wait_ready> and all of the actions including C<guestfs_sync>
77 are blocking calls. You can use the low-level event API to do
78 non-blocking operations instead.
80 All functions that return integers, return C<-1> on error. See
81 section ERROR HANDLING below for how to handle errors.
85 C<guestfs_h> is the opaque type representing a connection handle.
86 Create a handle by calling C<guestfs_create>. Call C<guestfs_close>
87 to free the handle and release all resources used.
89 Handles and operations on handles are not thread safe. However you
90 can use a separate handle for each thread (but not on the same disk
95 guestfs_h *guestfs_create (void);
97 Create a connection handle.
99 You have to call C<guestfs_add_drive> on the handle at least once.
101 This function returns a non-NULL pointer to a handle on success or
104 After configuring the handle, you have to call C<guestfs_launch> and
105 C<guestfs_wait_ready>.
107 You may also want to configure error handling for the handle. See
108 ERROR HANDLING section below.
112 void guestfs_close (guestfs_h *handle);
114 This closes the connection handle and frees up all resources used.
116 =head1 ERROR HANDLING
118 The convention in all functions that return C<int> is that they return
119 C<-1> to indicate an error. You can get additional information on
120 errors by calling C<guestfs_last_error> and/or by setting up an error
121 handler with C<guestfs_set_error_handler>.
123 The default error handler prints the information string to C<stderr>.
125 Out of memory errors are handled differently. The default action is
126 to call L<abort(3)>. If this is undesirable, then you can set a
127 handler using C<guestfs_set_out_of_memory_handler>.
129 =head2 guestfs_last_error
131 const char *guestfs_last_error (guestfs_h *handle);
133 This returns the last error message that happened on C<handle>. If
134 there has not been an error since the handle was created, then this
137 The lifetime of the returned string is until the next error occurs, or
138 C<guestfs_close> is called.
140 The error string is not localized (ie. is always in English), because
141 this makes searching for error messages in search engines give the
142 largest number of results.
144 =head2 guestfs_set_error_handler
146 typedef void (*guestfs_error_handler_cb) (guestfs_h *handle,
149 void guestfs_set_error_handler (guestfs_h *handle,
150 guestfs_error_handler_cb cb,
153 The callback C<cb> will be called if there is an error. The
154 parameters passed to the callback are an opaque data pointer and the
155 error message string.
157 Note that the message string C<msg> is freed as soon as the callback
158 function returns, so if you want to stash it somewhere you must make
161 The default handler prints messages on C<stderr>.
163 If you set C<cb> to C<NULL> then I<no> handler is called.
165 =head2 guestfs_get_error_handler
167 guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *handle,
170 Returns the current error handler callback.
172 =head2 guestfs_set_out_of_memory_handler
174 typedef void (*guestfs_abort_cb) (void);
175 int guestfs_set_out_of_memory_handler (guestfs_h *handle,
178 The callback C<cb> will be called if there is an out of memory
179 situation. I<Note this callback must not return>.
181 The default is to call L<abort(3)>.
183 You cannot set C<cb> to C<NULL>. You can't ignore out of memory
186 =head2 guestfs_get_out_of_memory_handler
188 guestfs_abort_fn guestfs_get_out_of_memory_handler (guestfs_h *handle);
190 This returns the current out of memory handler.
194 Libguestfs needs a kernel and initrd.img, which it finds by looking
195 along an internal path.
197 By default it looks for these in the directory C<$libdir/guestfs>
198 (eg. C</usr/local/lib/guestfs> or C</usr/lib64/guestfs>).
200 Use C<guestfs_set_path> or set the environment variable
201 C<LIBGUESTFS_PATH> to change the directories that libguestfs will
202 search in. The value is a colon-separated list of paths. The current
203 directory is I<not> searched unless the path contains an empty element
204 or C<.>. For example C<LIBGUESTFS_PATH=:/usr/lib/guestfs> would
205 search the current directory and then C</usr/lib/guestfs>.
207 =head1 HIGH-LEVEL API ACTIONS
215 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
217 Internally, libguestfs is implemented by running a virtual machine
218 using L<qemu(1)>. QEmu runs as a child process of the main program,
219 and most of this discussion won't make sense unless you understand
220 that the complexity is dealing with the (asynchronous) actions of the
224 ___________________ _________________________
226 | main program | | qemu +-----------------+|
227 | | | | Linux kernel ||
228 +-------------------+ | +-----------------+|
229 | libguestfs <-------------->| guestfsd ||
230 | | | +-----------------+|
231 \___________________/ \_________________________/
233 The diagram above shows libguestfs communicating with the guestfsd
234 daemon running inside the qemu child process. There are several
235 points of failure here: qemu can fail to start, the virtual machine
236 inside qemu can fail to boot, guestfsd can fail to start or not
237 establish communication, any component can start successfully but fail
238 asynchronously later, and so on.
242 libguestfs uses a state machine to model the child process:
253 / | \ \ guestfs_launch
259 / | guestfs_wait_ready
264 \______/ <------ \________/
266 The normal transitions are (1) CONFIG (when the handle is created, but
267 there is no child process), (2) LAUNCHING (when the child process is
268 booting up), (3) alternating between READY and BUSY as commands are
269 issued to, and carried out by, the child process.
271 The guest may be killed by C<guestfs_kill_subprocess>, or may die
272 asynchronously at any time (eg. due to some internal error), and that
273 causes the state to transition back to CONFIG.
275 Configuration commands for qemu such as C<guestfs_add_drive> can only
276 be issued when in the CONFIG state.
278 The high-level API offers two calls that go from CONFIG through
279 LAUNCHING to READY. C<guestfs_launch> is a non-blocking call that
280 starts up the child process, immediately moving from CONFIG to
281 LAUNCHING. C<guestfs_wait_ready> blocks until the child process is
282 READY to accept commands (or until some failure or timeout). The
283 low-level event API described below provides a non-blocking way to
284 replace C<guestfs_wait_ready>.
286 High-level API actions such as C<guestfs_mount> can only be issued
287 when in the READY state. These high-level API calls block waiting for
288 the command to be carried out (ie. the state to transition to BUSY and
289 then back to READY). But using the low-level event API, you get
290 non-blocking versions. (But you can still only carry out one
291 operation per handle at a time - that is a limitation of the
292 communications protocol we use).
294 Finally, the child process sends asynchronous messages back to the
295 main program, such as kernel log messages. Mostly these are ignored
296 by the high-level API, but using the low-level event API you can
297 register to receive these messages.
299 =head2 SETTING CALLBACKS TO HANDLE EVENTS
301 The child process generates events in some situations. Current events
302 include: receiving a reply message after some action, receiving a log
303 message, the child process exits, &c.
305 Use the C<guestfs_set_*_callback> functions to set a callback for
306 different types of events.
308 Only I<one callback of each type> can be registered for each handle.
309 Calling C<guestfs_set_*_callback> again overwrites the previous
310 callback of that type. Cancel all callbacks of this type by calling
311 this function with C<cb> set to C<NULL>.
313 =head2 NON-BLOCKING ACTIONS
315 XXX NOT IMPLEMENTED YET XXX
317 C<guestfs_set_reply_callback> is the most interesting callback to
318 play with, since it allows you to perform actions without blocking.
325 guestfs_main_loop_run (); /* --> blocks, then calls my_cb */
330 guestfs_set_reply_callback (handle, my_cb, data);
331 guestfs_nb_[action] (handle, [other parameters ...]);
332 /* returns immediately */
335 my_cb (guestfs_h *handle, void *data, XDR *xdr)
337 retval = guestfs_nb_[action]_r (handle, xdr);
341 There are C<guestfs_nb_*> and C<guestfs_nb_*_r> functions
342 corresponding to every C<guestfs_*> action in the high-level API.
344 =head2 guestfs_set_reply_callback
346 typedef void (*guestfs_reply_cb) (guestfs_h *g, void *opaque, XDR *xdr);
347 void guestfs_set_reply_callback (guestfs_h *handle,
351 The callback function C<cb> will be called whenever a reply is
352 received from the child process. (This corresponds to a transition
353 from the BUSY state to the READY state).
355 Note that the C<xdr> that you get in the callback is in C<XDR_DECODE>
356 mode, and you need to consume it before you return from the callback
357 function (since it gets destroyed after).
359 =head2 guestfs_set_log_message_callback
361 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
363 void guestfs_set_log_message_callback (guestfs_h *handle,
364 guestfs_log_message_cb cb,
367 The callback function C<cb> will be called whenever qemu or the guest
368 writes anything to the console.
370 Use this function to capture kernel messages and similar.
372 Normally there is no log message handler, and log messages are just
375 =head2 guestfs_set_subprocess_quit_callback
377 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
378 void guestfs_set_subprocess_quit_callback (guestfs_h *handle,
379 guestfs_subprocess_quit_cb cb,
382 The callback function C<cb> will be called when the child process
383 quits, either asynchronously or if killed by
384 C<guestfs_kill_subprocess>. (This corresponds to a transition from
385 any state to the CONFIG state).
387 =head2 guestfs_set_launch_done_callback
389 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
390 void guestfs_set_launch_done_callback (guestfs_h *handle,
394 The callback function C<cb> will be called when the child process
395 becomes ready first time after it has been launched. (This
396 corresponds to a transition from LAUNCHING to the READY state).
398 You can use this instead of C<guestfs_wait_ready> to implement a
399 non-blocking wait for the child process to finish booting up.
401 =head2 EVENT MAIN LOOP
403 To use the low-level event API, you have to provide an event "main
404 loop". You can write your own, but if you don't want to write one,
405 two are provided for you:
409 =item libguestfs-select
411 A simple main loop that is implemented using L<select(2)>.
413 This is the default main loop unless you call C<guestfs_set_main_loop>
414 or C<guestfs_glib_set_main_loop>.
416 =item libguestfs-glib
418 An implementation which can be used with GLib and GTK+ programs. You
419 can use this to write graphical (GTK+) programs which use libguestfs
420 without hanging during long or slow operations.
424 =head2 guestfs_set_main_loop
426 void guestfs_set_main_loop (guestfs_main_loop *);
428 This call sets the current main loop to the list of callbacks
429 contained in the C<guestfs_main_loop> structure.
431 Only one main loop implementation can be used by libguestfs, so
432 calling this replaces the previous one. (So this is something that
433 has to be done by the main program, but only the main program "knows"
434 that it is a GTK+ program or whatever).
436 You should call this early in the main program, certainly before
437 calling C<guestfs_create>.
439 =head2 guestfs_glib_set_main_loop
441 void guestfs_glib_set_main_loop (GMainLoop *);
443 This helper calls C<guestfs_set_main_loop> with the correct callbacks
444 for integrating with the GLib main loop.
446 The libguestfs-glib main loop is contained in a separate library, so
447 that libguestfs doesn't depend on the whole of GLib:
450 #include <guestfs-glib.h>
455 g_main_loop_new (g_main_context_default (), 1);
457 guestfs_glib_set_main_loop (loop);
459 g_main_loop_run (loop);
462 To use this main loop you must link with C<-lguestfs-glib>. (See also
463 the GLib and GTK+ documentation).
465 =head2 guestfs_main_loop_run
467 void guestfs_main_loop_run (void);
469 This calls the main loop.
471 For some types of main loop you may want or prefer to call another
472 function, eg. C<g_main_loop_run>, or the main loop may already be
473 invoked by another part of your program. In those cases, ignore this
476 =head2 guestfs_main_loop_quit
478 void guestfs_main_loop_quit (void);
480 This instructs the main loop to quit. In other words,
481 C<guestfs_main_loop_run> will return.
483 For some types of main loop you may want or prefer to call another
484 function, eg. C<g_main_loop_quit>. In those cases, ignore this call.
486 =head2 WRITING A CUSTOM MAIN LOOP
488 This isn't documented. Please see the libguestfs-select and
489 libguestfs-glib implementations.
491 =head1 ENVIRONMENT VARIABLES
495 =item LIBGUESTFS_DEBUG
497 Set C<LIBGUESTFS_DEBUG=1> to enable verbose messages. This
498 has the same effect as calling C<guestfs_set_verbose (handle, 1)>.
500 =item LIBGUESTFS_PATH
502 Set the path that libguestfs uses to search for kernel and initrd.img.
503 See the discussion of paths in section PATH above.
512 L<http://et.redhat.com/~rjones/libguestfs>.
516 Richard W.M. Jones (C<rjones at redhat dot com>)
520 Copyright (C) 2009 Red Hat Inc.
521 L<http://et.redhat.com/~rjones/libguestfs>
523 This library is free software; you can redistribute it and/or
524 modify it under the terms of the GNU Lesser General Public
525 License as published by the Free Software Foundation; either
526 version 2 of the License, or (at your option) any later version.
528 This library is distributed in the hope that it will be useful,
529 but WITHOUT ANY WARRANTY; without even the implied warranty of
530 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
531 Lesser General Public License for more details.
533 You should have received a copy of the GNU Lesser General Public
534 License along with this library; if not, write to the Free Software
535 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA