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 For information on using multiple handles and threads, see the section
90 MULTIPLE HANDLES AND MULTIPLE THREADS below.
94 guestfs_h *guestfs_create (void);
96 Create a connection handle.
98 You have to call C<guestfs_add_drive> on the handle at least once.
100 This function returns a non-NULL pointer to a handle on success or
103 After configuring the handle, you have to call C<guestfs_launch> and
104 C<guestfs_wait_ready>.
106 You may also want to configure error handling for the handle. See
107 ERROR HANDLING section below.
111 void guestfs_close (guestfs_h *handle);
113 This closes the connection handle and frees up all resources used.
115 =head1 ERROR HANDLING
117 The convention in all functions that return C<int> is that they return
118 C<-1> to indicate an error. You can get additional information on
119 errors by calling C<guestfs_last_error> and/or by setting up an error
120 handler with C<guestfs_set_error_handler>.
122 The default error handler prints the information string to C<stderr>.
124 Out of memory errors are handled differently. The default action is
125 to call L<abort(3)>. If this is undesirable, then you can set a
126 handler using C<guestfs_set_out_of_memory_handler>.
128 =head2 guestfs_last_error
130 const char *guestfs_last_error (guestfs_h *handle);
132 This returns the last error message that happened on C<handle>. If
133 there has not been an error since the handle was created, then this
136 The lifetime of the returned string is until the next error occurs, or
137 C<guestfs_close> is called.
139 The error string is not localized (ie. is always in English), because
140 this makes searching for error messages in search engines give the
141 largest number of results.
143 =head2 guestfs_set_error_handler
145 typedef void (*guestfs_error_handler_cb) (guestfs_h *handle,
148 void guestfs_set_error_handler (guestfs_h *handle,
149 guestfs_error_handler_cb cb,
152 The callback C<cb> will be called if there is an error. The
153 parameters passed to the callback are an opaque data pointer and the
154 error message string.
156 Note that the message string C<msg> is freed as soon as the callback
157 function returns, so if you want to stash it somewhere you must make
160 The default handler prints messages on C<stderr>.
162 If you set C<cb> to C<NULL> then I<no> handler is called.
164 =head2 guestfs_get_error_handler
166 guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *handle,
169 Returns the current error handler callback.
171 =head2 guestfs_set_out_of_memory_handler
173 typedef void (*guestfs_abort_cb) (void);
174 int guestfs_set_out_of_memory_handler (guestfs_h *handle,
177 The callback C<cb> will be called if there is an out of memory
178 situation. I<Note this callback must not return>.
180 The default is to call L<abort(3)>.
182 You cannot set C<cb> to C<NULL>. You can't ignore out of memory
185 =head2 guestfs_get_out_of_memory_handler
187 guestfs_abort_fn guestfs_get_out_of_memory_handler (guestfs_h *handle);
189 This returns the current out of memory handler.
193 Libguestfs needs a kernel and initrd.img, which it finds by looking
194 along an internal path.
196 By default it looks for these in the directory C<$libdir/guestfs>
197 (eg. C</usr/local/lib/guestfs> or C</usr/lib64/guestfs>).
199 Use C<guestfs_set_path> or set the environment variable
200 C<LIBGUESTFS_PATH> to change the directories that libguestfs will
201 search in. The value is a colon-separated list of paths. The current
202 directory is I<not> searched unless the path contains an empty element
203 or C<.>. For example C<LIBGUESTFS_PATH=:/usr/lib/guestfs> would
204 search the current directory and then C</usr/lib/guestfs>.
206 =head1 HIGH-LEVEL API ACTIONS
214 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
216 Internally, libguestfs is implemented by running a virtual machine
217 using L<qemu(1)>. QEmu runs as a child process of the main program,
218 and most of this discussion won't make sense unless you understand
219 that the complexity is dealing with the (asynchronous) actions of the
223 ___________________ _________________________
225 | main program | | qemu +-----------------+|
226 | | | | Linux kernel ||
227 +-------------------+ | +-----------------+|
228 | libguestfs <-------------->| guestfsd ||
229 | | | +-----------------+|
230 \___________________/ \_________________________/
232 The diagram above shows libguestfs communicating with the guestfsd
233 daemon running inside the qemu child process. There are several
234 points of failure here: qemu can fail to start, the virtual machine
235 inside qemu can fail to boot, guestfsd can fail to start or not
236 establish communication, any component can start successfully but fail
237 asynchronously later, and so on.
241 libguestfs uses a state machine to model the child process:
252 / | \ \ guestfs_launch
258 / | guestfs_wait_ready
263 \______/ <------ \________/
265 The normal transitions are (1) CONFIG (when the handle is created, but
266 there is no child process), (2) LAUNCHING (when the child process is
267 booting up), (3) alternating between READY and BUSY as commands are
268 issued to, and carried out by, the child process.
270 The guest may be killed by C<guestfs_kill_subprocess>, or may die
271 asynchronously at any time (eg. due to some internal error), and that
272 causes the state to transition back to CONFIG.
274 Configuration commands for qemu such as C<guestfs_add_drive> can only
275 be issued when in the CONFIG state.
277 The high-level API offers two calls that go from CONFIG through
278 LAUNCHING to READY. C<guestfs_launch> is a non-blocking call that
279 starts up the child process, immediately moving from CONFIG to
280 LAUNCHING. C<guestfs_wait_ready> blocks until the child process is
281 READY to accept commands (or until some failure or timeout). The
282 low-level event API described below provides a non-blocking way to
283 replace C<guestfs_wait_ready>.
285 High-level API actions such as C<guestfs_mount> can only be issued
286 when in the READY state. These high-level API calls block waiting for
287 the command to be carried out (ie. the state to transition to BUSY and
288 then back to READY). But using the low-level event API, you get
289 non-blocking versions. (But you can still only carry out one
290 operation per handle at a time - that is a limitation of the
291 communications protocol we use).
293 Finally, the child process sends asynchronous messages back to the
294 main program, such as kernel log messages. Mostly these are ignored
295 by the high-level API, but using the low-level event API you can
296 register to receive these messages.
298 =head2 SETTING CALLBACKS TO HANDLE EVENTS
300 The child process generates events in some situations. Current events
301 include: receiving a reply message after some action, receiving a log
302 message, the child process exits, &c.
304 Use the C<guestfs_set_*_callback> functions to set a callback for
305 different types of events.
307 Only I<one callback of each type> can be registered for each handle.
308 Calling C<guestfs_set_*_callback> again overwrites the previous
309 callback of that type. Cancel all callbacks of this type by calling
310 this function with C<cb> set to C<NULL>.
312 =head2 NON-BLOCKING ACTIONS
314 XXX This section was documented in previous versions but never
315 implemented in a way which matched the documentation. For now I have
316 removed the documentation, pending a working implementation. See also
317 C<src/guestfs-actions.c> in the source.
320 =head2 guestfs_set_send_callback
322 typedef void (*guestfs_send_cb) (guestfs_h *g, void *opaque);
323 void guestfs_set_send_callback (guestfs_h *handle,
327 The callback function C<cb> will be called whenever a message
328 which is queued for sending, has been sent.
330 =head2 guestfs_set_reply_callback
332 typedef void (*guestfs_reply_cb) (guestfs_h *g, void *opaque, XDR *xdr);
333 void guestfs_set_reply_callback (guestfs_h *handle,
337 The callback function C<cb> will be called whenever a reply is
338 received from the child process. (This corresponds to a transition
339 from the BUSY state to the READY state).
341 Note that the C<xdr> that you get in the callback is in C<XDR_DECODE>
342 mode, and you need to consume it before you return from the callback
343 function (since it gets destroyed after).
345 =head2 guestfs_set_log_message_callback
347 typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
349 void guestfs_set_log_message_callback (guestfs_h *handle,
350 guestfs_log_message_cb cb,
353 The callback function C<cb> will be called whenever qemu or the guest
354 writes anything to the console.
356 Use this function to capture kernel messages and similar.
358 Normally there is no log message handler, and log messages are just
361 =head2 guestfs_set_subprocess_quit_callback
363 typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
364 void guestfs_set_subprocess_quit_callback (guestfs_h *handle,
365 guestfs_subprocess_quit_cb cb,
368 The callback function C<cb> will be called when the child process
369 quits, either asynchronously or if killed by
370 C<guestfs_kill_subprocess>. (This corresponds to a transition from
371 any state to the CONFIG state).
373 =head2 guestfs_set_launch_done_callback
375 typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
376 void guestfs_set_launch_done_callback (guestfs_h *handle,
380 The callback function C<cb> will be called when the child process
381 becomes ready first time after it has been launched. (This
382 corresponds to a transition from LAUNCHING to the READY state).
384 You can use this instead of C<guestfs_wait_ready> to implement a
385 non-blocking wait for the child process to finish booting up.
387 =head2 EVENT MAIN LOOP
389 To use the low-level event API and/or to use handles from multiple
390 threads, you have to provide an event "main loop". You can write your
391 own, but if you don't want to write one, two types are provided for
396 =item libguestfs-select
398 A simple main loop that is implemented using L<select(2)>.
400 This is the default main loop for new guestfs handles, unless you
401 call C<guestfs_set_main_loop> after a handle is created.
403 =item libguestfs-glib
405 An implementation which can be used with GLib and GTK+ programs. You
406 can use this to write graphical (GTK+) programs which use libguestfs
407 without hanging during long or slow operations.
411 =head2 MULTIPLE HANDLES AND MULTIPLE THREADS
413 The support for multiple handles and multiple threads is modelled
414 after glib (although doesn't require glib, if you use the select-based
417 L<http://library.gnome.org/devel/glib/unstable/glib-The-Main-Event-Loop.html>
419 You will need to create one main loop for each thread that wants to
420 use libguestfs. Each guestfs handle should be confined to one thread.
421 If you try to pass guestfs handles between threads, you will get
424 If you only want to use guestfs handles from one thread in your
425 program, but your program has other threads doing other things, then
426 you don't need to do anything special.
428 =head2 SINGLE THREAD CASE
430 In the single thread case, there is a single select-based main loop
431 created for you. All guestfs handles will use this main loop to
432 execute high level API actions.
434 =head2 MULTIPLE THREADS CASE
436 In the multiple threads case, you will need to create a main loop for
437 each thread that wants to use libguestfs.
439 To create main loops for other threads, use
440 C<guestfs_create_main_loop> or C<guestfs_glib_create_main_loop>.
442 Then you will need to attach each handle to the thread-specific main
445 handle = guestfs_create ();
446 guestfs_set_main_loop (handle, main_loop_of_current_thread);
448 =head2 guestfs_set_main_loop
450 void guestfs_set_main_loop (guestfs_h *handle,
451 guestfs_main_loop *main_loop);
453 Sets the main loop used by high level API actions for this handle. By
454 default, the select-based main loop is used (see
455 C<guestfs_get_default_main_loop>).
457 You only need to use this in multi-threaded programs, where multiple
458 threads want to use libguestfs. Create a main loop for each thread,
459 then call this function.
461 You cannot pass guestfs handles between threads.
463 =head2 guestfs_get_main_loop
465 guestfs_main_loop *guestfs_get_main_loop (guestfs_h *handle);
467 Return the main loop used by C<handle>.
469 =head2 guestfs_get_default_main_loop
471 guestfs_main_loop *guestfs_get_default_main_loop (void);
473 Return the default select-based main loop.
475 =head2 guestfs_create_main_loop
477 guestfs_main_loop *guestfs_create_main_loop (void);
479 This creates a select-based main loop. You should create one main
480 loop for each additional thread that needs to use libguestfs.
482 =head2 guestfs_free_main_loop
484 void guestfs_free_main_loop (guestfs_main_loop *);
486 Free the select-based main loop which was previously allocated with
487 C<guestfs_create_main_loop>.
489 =head2 WRITING A CUSTOM MAIN LOOP
491 This isn't documented. Please see the libguestfs-select and
492 libguestfs-glib implementations.
494 =head1 ENVIRONMENT VARIABLES
498 =item LIBGUESTFS_DEBUG
500 Set C<LIBGUESTFS_DEBUG=1> to enable verbose messages. This
501 has the same effect as calling C<guestfs_set_verbose (handle, 1)>.
503 =item LIBGUESTFS_PATH
505 Set the path that libguestfs uses to search for kernel and initrd.img.
506 See the discussion of paths in section PATH above.
515 L<http://et.redhat.com/~rjones/libguestfs>.
519 Richard W.M. Jones (C<rjones at redhat dot com>)
523 Copyright (C) 2009 Red Hat Inc.
524 L<http://et.redhat.com/~rjones/libguestfs>
526 This library is free software; you can redistribute it and/or
527 modify it under the terms of the GNU Lesser General Public
528 License as published by the Free Software Foundation; either
529 version 2 of the License, or (at your option) any later version.
531 This library is distributed in the hope that it will be useful,
532 but WITHOUT ANY WARRANTY; without even the implied warranty of
533 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
534 Lesser General Public License for more details.
536 You should have received a copy of the GNU Lesser General Public
537 License along with this library; if not, write to the Free Software
538 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA