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 or Perl).
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_set_error_handler>. The default error
121 handler prints the information string to C<stderr>.
123 Out of memory errors are handled differently. The default action is
124 to call L<abort(3)>. If this is undesirable, then you can set a
125 handler using C<guestfs_set_out_of_memory_handler>.
127 =head2 guestfs_set_error_handler
129 typedef void (*guestfs_error_handler_cb) (guestfs_h *handle,
132 void guestfs_set_error_handler (guestfs_h *handle,
133 guestfs_error_handler_cb cb,
136 The callback C<cb> will be called if there is an error. The
137 parameters passed to the callback are an opaque data pointer and the
138 error message string.
140 Note that the message string C<msg> is freed as soon as the callback
141 function returns, so if you want to stash it somewhere you must make
144 The default handler prints messages on C<stderr>.
146 If you set C<cb> to C<NULL> then I<no> handler is called and the error
147 message is completely discarded.
149 =head2 guestfs_get_error_handler
151 guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *handle,
154 Returns the current error handler callback.
156 =head2 guestfs_set_out_of_memory_handler
158 typedef void (*guestfs_abort_cb) (void);
159 int guestfs_set_out_of_memory_handler (guestfs_h *handle,
162 The callback C<cb> will be called if there is an out of memory
163 situation. I<Note this callback must not return>.
165 The default is to call L<abort(3)>.
167 You cannot set C<cb> to C<NULL>. You can't ignore out of memory
170 =head2 guestfs_get_out_of_memory_handler
172 guestfs_abort_fn guestfs_get_out_of_memory_handler (guestfs_h *handle);
174 This returns the current out of memory handler.
178 Libguestfs needs a kernel and initrd.img, which it finds by looking
179 along an internal path.
181 By default it looks for these in the directory C<$libdir/guestfs>
182 (eg. C</usr/local/lib/guestfs> or C</usr/lib64/guestfs>).
184 Use C<guestfs_set_path> or set the environment variable
185 C<LIBGUESTFS_PATH> to change the directories that libguestfs will
186 search in. The value is a colon-separated list of paths. The current
187 directory is I<not> searched unless the path contains an empty element
188 or C<.>. For example C<LIBGUESTFS_PATH=:/usr/lib/guestfs> would
189 search the current directory and then C</usr/lib/guestfs>.
191 =head1 HIGH-LEVEL API ACTIONS
199 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
201 Internally, libguestfs is implemented by running a virtual machine
202 using L<qemu(1)>. QEmu runs as a child process of the main program,
203 and most of this discussion won't make sense unless you understand
204 that the complexity is dealing with the (asynchronous) actions of the
208 ___________________ _________________________
210 | main program | | qemu +-----------------+|
211 | | | | Linux kernel ||
212 +-------------------+ | +-----------------+|
213 | libguestfs <-------------->| guestfsd ||
214 | | | +-----------------+|
215 \___________________/ \_________________________/
217 The diagram above shows libguestfs communicating with the guestfsd
218 daemon running inside the qemu child process. There are several
219 points of failure here: qemu can fail to start, the virtual machine
220 inside qemu can fail to boot, guestfsd can fail to start or not
221 establish communication, any component can start successfully but fail
222 asynchronously later, and so on.
226 libguestfs uses a state machine to model the child process:
237 / | \ \ guestfs_launch
243 / | guestfs_wait_ready
248 \______/ <------ \________/
250 The normal transitions are (1) CONFIG (when the handle is created, but
251 there is no child process), (2) LAUNCHING (when the child process is
252 booting up), (3) alternating between READY and BUSY as commands are
253 issued to, and carried out by, the child process.
255 The guest may be killed by C<guestfs_kill_subprocess>, or may die
256 asynchronously at any time (eg. due to some internal error), and that
257 causes the state to transition back to CONFIG.
259 Configuration commands for qemu such as C<guestfs_add_drive> can only
260 be issued when in the CONFIG state.
262 The high-level API offers two calls that go from CONFIG through
263 LAUNCHING to READY. C<guestfs_launch> is a non-blocking call that
264 starts up the child process, immediately moving from CONFIG to
265 LAUNCHING. C<guestfs_wait_ready> blocks until the child process is
266 READY to accept commands (or until some failure or timeout). The
267 low-level event API described below provides a non-blocking way to
268 replace C<guestfs_wait_ready>.
270 High-level API actions such as C<guestfs_mount> can only be issued
271 when in the READY state. These high-level API calls block waiting for
272 the command to be carried out (ie. the state to transition to BUSY and
273 then back to READY). But using the low-level event API, you get
274 non-blocking versions. (But you can still only carry out one
275 operation per handle at a time - that is a limitation of the
276 communications protocol we use).
278 Finally, the child process sends asynchronous messages back to the
279 main program, such as kernel log messages. Mostly these are ignored
280 by the high-level API, but using the low-level event API you can
281 register to receive these messages.
283 =head2 SETTING CALLBACKS TO HANDLE EVENTS
285 The child process generates events in some situations. Current events
286 include: receiving a reply message after some action, receiving a log
287 message, the child process exits, &c.
289 Use the C<guestfs_set_*_callback> functions to set a callback for
290 different types of events.
292 Only I<one callback of each type> can be registered for each handle.
293 Calling C<guestfs_set_*_callback> again overwrites the previous
294 callback of that type. Cancel all callbacks of this type by calling
295 this function with C<cb> set to C<NULL>.
297 =head2 NON-BLOCKING ACTIONS
299 XXX NOT IMPLEMENTED YET XXX
301 C<guestfs_set_reply_callback> is the most interesting callback to
302 play with, since it allows you to perform actions without blocking.
309 guestfs_main_loop_run (); /* --> blocks, then calls my_cb */
314 guestfs_set_reply_callback (handle, my_cb, data);
315 guestfs_nb_[action] (handle, [other parameters ...]);
316 /* returns immediately */
319 my_cb (guestfs_h *handle, void *data, XDR *xdr)
321 retval = guestfs_nb_[action]_r (handle, xdr);
325 There are C<guestfs_nb_*> and C<guestfs_nb_*_r> functions
326 corresponding to every C<guestfs_*> action in the high-level API.
328 =head2 guestfs_set_reply_callback
330 typedef void (*guestfs_reply_cb) (guestfs_h *g, void *opaque, XDR *xdr);
331 void guestfs_set_reply_callback (guestfs_h *handle,
335 The callback function C<cb> will be called whenever a reply is
336 received from the child process. (This corresponds to a transition
337 from the BUSY state to the READY state).
339 Note that the C<xdr> that you get in the callback is in C<XDR_DECODE>
340 mode, and you need to consume it before you return from the callback
341 function (since it gets destroyed after).
343 =head2 guestfs_set_log_message_callback
345 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
347 void guestfs_set_log_message_callback (guestfs_h *handle,
348 guestfs_log_message_cb cb,
351 The callback function C<cb> will be called whenever qemu or the guest
352 writes anything to the console.
354 Use this function to capture kernel messages and similar.
356 Normally there is no log message handler, and log messages are just
359 =head2 guestfs_set_subprocess_quit_callback
361 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
362 void guestfs_set_subprocess_quit_callback (guestfs_h *handle,
363 guestfs_subprocess_quit_cb cb,
366 The callback function C<cb> will be called when the child process
367 quits, either asynchronously or if killed by
368 C<guestfs_kill_subprocess>. (This corresponds to a transition from
369 any state to the CONFIG state).
371 =head2 guestfs_set_launch_done_callback
373 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
374 void guestfs_set_launch_done_callback (guestfs_h *handle,
378 The callback function C<cb> will be called when the child process
379 becomes ready first time after it has been launched. (This
380 corresponds to a transition from LAUNCHING to the READY state).
382 You can use this instead of C<guestfs_wait_ready> to implement a
383 non-blocking wait for the child process to finish booting up.
385 =head2 EVENT MAIN LOOP
387 To use the low-level event API, you have to provide an event "main
388 loop". You can write your own, but if you don't want to write one,
389 two are provided for you:
393 =item libguestfs-select
395 A simple main loop that is implemented using L<select(2)>.
397 This is the default main loop unless you call C<guestfs_set_main_loop>
398 or C<guestfs_glib_set_main_loop>.
400 =item libguestfs-glib
402 An implementation which can be used with GLib and GTK+ programs. You
403 can use this to write graphical (GTK+) programs which use libguestfs
404 without hanging during long or slow operations.
408 =head2 guestfs_set_main_loop
410 void guestfs_set_main_loop (guestfs_main_loop *);
412 This call sets the current main loop to the list of callbacks
413 contained in the C<guestfs_main_loop> structure.
415 Only one main loop implementation can be used by libguestfs, so
416 calling this replaces the previous one. (So this is something that
417 has to be done by the main program, but only the main program "knows"
418 that it is a GTK+ program or whatever).
420 You should call this early in the main program, certainly before
421 calling C<guestfs_create>.
423 =head2 guestfs_glib_set_main_loop
425 void guestfs_glib_set_main_loop (GMainLoop *);
427 This helper calls C<guestfs_set_main_loop> with the correct callbacks
428 for integrating with the GLib main loop.
430 The libguestfs-glib main loop is contained in a separate library, so
431 that libguestfs doesn't depend on the whole of GLib:
434 #include <guestfs-glib.h>
439 g_main_loop_new (g_main_context_default (), 1);
441 guestfs_glib_set_main_loop (loop);
443 g_main_loop_run (loop);
446 To use this main loop you must link with C<-lguestfs-glib>. (See also
447 the GLib and GTK+ documentation).
449 =head2 guestfs_main_loop_run
451 void guestfs_main_loop_run (void);
453 This calls the main loop.
455 For some types of main loop you may want or prefer to call another
456 function, eg. C<g_main_loop_run>, or the main loop may already be
457 invoked by another part of your program. In those cases, ignore this
460 =head2 guestfs_main_loop_quit
462 void guestfs_main_loop_quit (void);
464 This instructs the main loop to quit. In other words,
465 C<guestfs_main_loop_run> will return.
467 For some types of main loop you may want or prefer to call another
468 function, eg. C<g_main_loop_quit>. In those cases, ignore this call.
470 =head2 WRITING A CUSTOM MAIN LOOP
472 This isn't documented. Please see the libguestfs-select and
473 libguestfs-glib implementations.
475 =head1 ENVIRONMENT VARIABLES
479 =item LIBGUESTFS_DEBUG
481 Set C<LIBGUESTFS_DEBUG=1> to enable verbose messages. This
482 has the same effect as calling C<guestfs_set_verbose (handle, 1)>.
484 =item LIBGUESTFS_PATH
486 Set the path that libguestfs uses to search for kernel and initrd.img.
487 See the discussion of paths in section PATH above.
496 L<http://et.redhat.com/~rjones/libguestfs>.
500 Richard W.M. Jones (C<rjones at redhat dot com>)
504 Copyright (C) 2009 Red Hat Inc.
505 L<http://et.redhat.com/~rjones/libguestfs>
507 This library is free software; you can redistribute it and/or
508 modify it under the terms of the GNU Lesser General Public
509 License as published by the Free Software Foundation; either
510 version 2 of the License, or (at your option) any later version.
512 This library is distributed in the hope that it will be useful,
513 but WITHOUT ANY WARRANTY; without even the implied warranty of
514 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
515 Lesser General Public License for more details.
517 You should have received a copy of the GNU Lesser General Public
518 License along with this library; if not, write to the Free Software
519 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA