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 mount guest filesystems on
38 the host (requires root privs and NFS).
40 Libguestfs is a library that can be linked with C and C++ management
41 programs (or management programs written in other languages, if people
42 contribute the language bindings). You can also use it from shell
43 scripts or the command line.
45 You don't need to be root to use libguestfs, although obviously you do
46 need enough permissions to access the disk images.
48 =head1 CONNECTION MANAGEMENT
50 If you are using the high-level API, then you should call the
51 functions in the following order:
53 guestfs_h *handle = guestfs_create ();
55 guestfs_add_drive (handle, "guest.img");
56 /* call guestfs_add_drive additional times if the guest has
60 guestfs_launch (handle);
61 guestfs_wait_ready (handle);
63 /* now you can examine what partitions, LVs etc are available
64 * you have to mount / at least
66 guestfs_mount (handle, "/dev/sda1", "/");
68 /* now you can perform actions on the guest disk image */
69 guestfs_touch (handle, "/hello");
71 /* you only need to call guestfs_sync if you have made
72 * changes to the guest image
74 guestfs_sync (handle);
76 guestfs_close (handle);
78 C<guestfs_wait_ready> and all of the actions including C<guestfs_sync>
79 are blocking calls. You can use the low-level event API to do
80 non-blocking operations instead.
82 All functions that return integers, return C<-1> on error. See
83 section ERROR HANDLING below for how to handle errors.
87 C<guestfs_h> is the opaque type representing a connection handle.
88 Create a handle by calling C<guestfs_create>. Call C<guestfs_close>
89 to free the handle and release all resources used.
91 Handles and operations on handles are not thread safe. However you
92 can use a separate handle for each thread (but not on the same disk
97 guestfs_h *guestfs_create (void);
99 Create a connection handle.
101 You have to call C<guestfs_add_drive> on the handle at least once.
102 See CONFIGURATION MANAGEMENT section below.
104 This function returns a non-NULL pointer to a handle on success or
107 After configuring the handle, you have to call C<guestfs_launch> and
108 C<guestfs_wait_ready>.
110 You may also want to configure error handling for the handle. See
111 ERROR HANDLING section below.
115 void guestfs_close (guestfs_h *handle);
117 This closes the connection handle and frees up all resources used.
119 =head2 guestfs_launch, guestfs_wait_ready
121 int guestfs_launch (guestfs_h *handle);
122 int guestfs_wait_ready (guestfs_h *handle);
124 Internally libguestfs is implemented by running a virtual machine
125 using L<qemu(1)>. These calls are necessary in order to boot the
126 virtual machine. More discussion of this is available in the section
127 STATE MACHINE AND LOW-LEVEL EVENT API below.
129 You should call these two functions after configuring the handle
130 (eg. adding drives) but before performing any actions.
132 =head2 guestfs_kill_subprocess
134 int guestfs_kill_subprocess (guestfs_h *handle);
136 This kills the qemu subprocess. You should never need to call this.
138 =head1 CONFIGURATION MANAGEMENT
140 The configuration functions allow you to configure which drive images
141 will be examined or modified, and set other aspects of the L<qemu(1)>
142 virtual machine that we will be running. You need to call only
143 C<guestfs_add_drive> at least once for each guest image that you want
146 =head2 guestfs_add_drive
148 int guestfs_add_drive (guestfs_h *handle, const char *filename);
150 This function adds a virtual machine disk image C<filename> to the
151 guest. The first time you call this function, the disk appears as IDE
152 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
155 You don't necessarily need to be root when using libguestfs. However
156 you obviously do need sufficient permissions to access the filename
157 for whatever operations you want to perform (ie. read access if you
158 just want to read the image or write access if you want to modify the
161 This is equivalent to the qemu parameter C<-drive file=filename>.
163 =head2 guestfs_add_cdrom
165 int guestfs_add_cdrom (guestfs_h *handle, const char *filename);
167 This function adds a virtual CD-ROM disk image to the guest.
169 This is equivalent to the qemu parameter C<-cdrom filename>.
171 =head2 guestfs_config
173 int guestfs_config (guestfs_h *handle,
174 const char *qemu_param, const char *qemu_value);
176 This can be used to add arbitrary qemu command line parameters
177 of the form C<-param value>. Actually it's not quite arbitrary - we
178 prevent you from setting some parameters which would interfere with
179 parameters that we use.
181 The first character of C<qemu_param> string must be a C<-> (dash).
183 C<qemu_value> can be NULL.
185 =head1 ERROR HANDLING
187 The convention in all functions that return C<int> is that they return
188 C<-1> to indicate an error. You can get additional information on
189 errors by calling C<guestfs_set_error_handler>. The default error
190 handler prints the information string to C<stderr>.
192 Out of memory errors are handled differently. The default action is
193 to call L<abort(3)>. If this is undesirable, then you can set a
194 handler using C<guestfs_set_out_of_memory_handler>.
196 =head2 guestfs_set_error_handler
198 typedef void (*guestfs_error_handler_cb) (guestfs_h *handle,
201 void guestfs_set_error_handler (guestfs_h *handle,
202 guestfs_error_handler_cb cb,
205 The callback C<cb> will be called if there is an error. The
206 parameters passed to the callback are an opaque data pointer and the
207 error message string.
209 The default handler prints messages on C<stderr>.
211 If you set C<cb> to C<NULL> then I<no> handler is called and the error
212 message is completely discarded.
214 =head2 guestfs_get_error_handler
216 guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *handle,
219 Returns the current error handler callback.
221 =head2 guestfs_set_out_of_memory_handler
223 typedef void (*guestfs_abort_cb) (void);
224 int guestfs_set_out_of_memory_handler (guestfs_h *handle,
227 The callback C<cb> will be called if there is an out of memory
228 situation. I<Note this callback must not return>.
230 The default is to call L<abort(3)>.
232 You cannot set C<cb> to C<NULL>. You can't ignore out of memory
235 =head2 guestfs_get_out_of_memory_handler
237 guestfs_abort_fn guestfs_get_out_of_memory_handler (guestfs_h *handle);
239 This returns the current out of memory handler.
241 =head1 VERBOSE MESSAGES
243 =head2 guestfs_set_verbose
245 void guestfs_set_verbose (guestfs_h *handle, int verbose);
247 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
249 Verbose messages are disabled unless the environment variable
250 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.
252 =head2 guestfs_get_verbose
254 int guestfs_get_verbose (guestfs_h *handle);
256 This returns the verbose messages flag.
258 =head1 HIGH-LEVEL API ACTIONS
262 int guestfs_sync (guestfs_h *handle);
264 This syncs the disk, so that any writes are flushed through to the
265 underlying disk image.
267 You should always call this if you have modified a disk image, before
268 calling C<guestfs_close>.
270 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
272 Documentation will be auto-generated from here, including for
275 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
277 do_[action] ([parameters])
279 guestfs_set_reply_callback (handle, [action]_cb, data);
280 guestfs_nb_[action] (handle, [parameters ...]);
282 guestfs_main_loop_run (); /* --> blocks, then calls my_cb */
285 [action]_cb (guestfs_h *handle, void *data)
287 retval = guestfs_nb_[action]_r (handle);
289 guestfs_main_loop_quit ();
293 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
302 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
304 Internally, libguestfs is implemented by running a virtual machine
305 using L<qemu(1)>. QEmu runs as a child process of the main program,
306 and most of this discussion won't make sense unless you understand
307 that the complexity is dealing with the (asynchronous) actions of the
311 ___________________ _________________________
313 | main program | | qemu +-----------------+|
314 | | | | Linux kernel ||
315 +-------------------+ | +-----------------+|
316 | libguestfs <-------------->| guestfsd ||
317 | | | +-----------------+|
318 \___________________/ \_________________________/
320 The diagram above shows libguestfs communicating with the guestfsd
321 daemon running inside the qemu child process. There are several
322 points of failure here: qemu can fail to start, the virtual machine
323 inside qemu can fail to boot, guestfsd can fail to start or not
324 establish communication, any component can start successfully but fail
325 asynchronously later, and so on.
329 libguestfs uses a state machine to model the child process:
340 / | \ \ guestfs_launch
346 / | guestfs_wait_ready
351 \______/ <------ \________/
353 The normal transitions are (1) CONFIG (when the handle is created, but
354 there is no child process), (2) LAUNCHING (when the child process is
355 booting up), (3) alternating between READY and BUSY as commands are
356 issued to, and carried out by, the child process.
358 The guest may be killed by C<guestfs_kill_subprocess>, or may die
359 asynchronously at any time (eg. due to some internal error), and that
360 causes the state to transition back to CONFIG.
362 Configuration commands for qemu such as C<guestfs_add_drive> can only
363 be issued when in the CONFIG state.
365 The high-level API offers two calls that go from CONFIG through
366 LAUNCHING to READY. C<guestfs_launch> is a non-blocking call that
367 starts up the child process, immediately moving from CONFIG to
368 LAUNCHING. C<guestfs_wait_ready> blocks until the child process is
369 READY to accept commands (or until some failure or timeout). The
370 low-level event API described below provides a non-blocking way to
371 replace C<guestfs_wait_ready>.
373 High-level API actions such as C<guestfs_mount> can only be issued
374 when in the READY state. These high-level API calls block waiting for
375 the command to be carried out (ie. the state to transition to BUSY and
376 then back to READY). But using the low-level event API, you get
377 non-blocking versions. (But you can still only carry out one
378 operation per handle at a time - that is a limitation of the
379 communications protocol we use).
381 Finally, the child process sends asynchronous messages back to the
382 main program, such as kernel log messages. Mostly these are ignored
383 by the high-level API, but using the low-level event API you can
384 register to receive these messages.
386 =head2 SETTING CALLBACKS TO HANDLE EVENTS
388 The child process generates events in some situations. Current events
389 include: receiving a reply message after some action, receiving a log
390 message, the child process exits, &c.
392 Use the C<guestfs_set_*_callback> functions to set a callback for
393 different types of events.
395 Only I<one callback of each type> can be registered for each handle.
396 Calling C<guestfs_set_*_callback> again overwrites the previous
397 callback of that type. Cancel all callbacks of this type by calling
398 this function with C<cb> set to C<NULL>.
400 =head2 NON-BLOCKING ACTIONS
402 C<guestfs_set_reply_callback> is the most interesting callback to
403 play with, since it allows you to perform actions without blocking.
410 guestfs_main_loop_run (); /* --> blocks, then calls my_cb */
415 guestfs_set_reply_callback (handle, my_cb, data);
416 guestfs_nb_[action] (handle, [other parameters ...]);
417 /* returns immediately */
420 my_cb (guestfs_h *handle, void *data, XDR *xdr)
422 retval = guestfs_nb_[action]_r (handle, xdr);
426 There are C<guestfs_nb_*> and C<guestfs_nb_*_r> functions
427 corresponding to (very nearly) every C<guestfs_*> action in the
430 =head2 guestfs_set_reply_callback
432 typedef void (*guestfs_reply_cb) (guestfs_h *g, void *opaque, XDR *xdr);
433 void guestfs_set_reply_callback (guestfs_h *handle,
437 The callback function C<cb> will be called whenever a reply is
438 received from the child process. (This corresponds to a transition
439 from the BUSY state to the READY state).
441 =head2 guestfs_set_log_message_callback
443 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
445 void guestfs_set_log_message_callback (guestfs_h *handle,
446 guestfs_log_message_cb cb,
449 The callback function C<cb> will be called whenever qemu or the guest
450 writes anything to the console.
452 Use this function to capture kernel messages and similar.
454 Normally there is no log message handler, and log messages are just
457 =head2 guestfs_set_subprocess_quit_callback
459 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
460 void guestfs_set_subprocess_quit_callback (guestfs_h *handle,
461 guestfs_subprocess_quit_cb cb,
464 The callback function C<cb> will be called when the child process
465 quits, either asynchronously or if killed by
466 C<guestfs_kill_subprocess>. (This corresponds to a transition from
467 any state to the CONFIG state).
469 =head2 guestfs_set_launch_done_callback
471 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
472 void guestfs_set_launch_done_callback (guestfs_h *handle,
476 The callback function C<cb> will be called when the child process
477 becomes ready first time after it has been launched. (This
478 corresponds to a transition from LAUNCHING to the READY state).
480 You can use this instead of C<guestfs_wait_ready> to implement a
481 non-blocking wait for the child process to finish booting up.
483 =head2 EVENT MAIN LOOP
485 To use the low-level event API, you have to provide an event "main
486 loop". You can write your own, but if you don't want to write one,
487 two are provided for you:
491 =item libguestfs-select
493 A simple main loop that is implemented using L<select(2)>.
495 This is the default main loop unless you call C<guestfs_set_main_loop>
496 or C<guestfs_glib_set_main_loop>.
498 =item libguestfs-glib
500 An implementation which can be used with GLib and GTK+ programs. You
501 can use this to write graphical (GTK+) programs which use libguestfs
502 without hanging during long or slow operations.
506 =head2 guestfs_set_main_loop
508 void guestfs_set_main_loop (guestfs_main_loop *);
510 This call sets the current main loop to the list of callbacks
511 contained in the C<guestfs_main_loop> structure.
513 Only one main loop implementation can be used by libguestfs, so
514 calling this replaces the previous one. (So this is something that
515 has to be done by the main program, but only the main program "knows"
516 that it is a GTK+ program or whatever).
518 You should call this early in the main program, certainly before
519 calling C<guestfs_create>.
521 =head2 guestfs_glib_set_main_loop
523 void guestfs_glib_set_main_loop (GMainLoop *);
525 This helper calls C<guestfs_set_main_loop> with the correct callbacks
526 for integrating with the GLib main loop.
528 The libguestfs-glib main loop is contained in a separate library, so
529 that libguestfs doesn't depend on the whole of GLib:
532 #include <guestfs-glib.h>
537 g_main_loop_new (g_main_context_default (), 1);
539 guestfs_glib_set_main_loop (loop);
541 g_main_loop_run (loop);
544 To use this main loop you must link with C<-lguestfs-glib>. (See also
545 the GLib and GTK+ documentation).
547 =head2 guestfs_main_loop_run
549 void guestfs_main_loop_run (void);
551 This calls the main loop.
553 For some types of main loop you may want or prefer to call another
554 function, eg. C<g_main_loop_run>, or the main loop may already be
555 invoked by another part of your program. In those cases, ignore this
558 =head2 guestfs_main_loop_quit
560 void guestfs_main_loop_quit (void);
562 This instructs the main loop to quit. In other words,
563 C<guestfs_main_loop_run> will return.
565 For some types of main loop you may want or prefer to call another
566 function, eg. C<g_main_loop_quit>. In those cases, ignore this call.
568 =head2 WRITING A CUSTOM MAIN LOOP
570 This isn't documented. Please see the libguestfs-select and
571 libguestfs-glib implementations.
583 Richard W.M. Jones (C<rjones at redhat dot com>)
587 Copyright (C) 2009 Red Hat Inc.
588 L<http://et.redhat.com/~rjones/libguestfs>
590 This library is free software; you can redistribute it and/or
591 modify it under the terms of the GNU Lesser General Public
592 License as published by the Free Software Foundation; either
593 version 2 of the License, or (at your option) any later version.
595 This library is distributed in the hope that it will be useful,
596 but WITHOUT ANY WARRANTY; without even the implied warranty of
597 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
598 Lesser General Public License for more details.
600 You should have received a copy of the GNU Lesser General Public
601 License along with this library; if not, write to the Free Software
602 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA