Generated code for stat/lstat/statvfs changes.
[libguestfs.git] / guestfs.pod
1 =encoding utf8
2
3 =head1 NAME
4
5 guestfs - Library for accessing and modifying virtual machine images
6
7 =head1 SYNOPSIS
8
9  #include <guestfs.h>
10  
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);
19
20 =head1 DESCRIPTION
21
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
28 much else besides.
29
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.
34
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.
38
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.
42
43 You don't need to be root to use libguestfs, although obviously you do
44 need enough permissions to access the disk images.
45
46 =head1 CONNECTION MANAGEMENT
47
48 If you are using the high-level API, then you should call the
49 functions in the following order:
50
51  guestfs_h *handle = guestfs_create ();
52  
53  guestfs_add_drive (handle, "guest.img");
54  /* call guestfs_add_drive additional times if the guest has
55   * multiple disks
56   */
57  
58  guestfs_launch (handle);
59  guestfs_wait_ready (handle);
60
61  /* now you can examine what partitions, LVs etc are available
62   * you have to mount / at least
63   */ 
64  guestfs_mount (handle, "/dev/sda1", "/");
65
66  /* now you can perform actions on the guest disk image */
67  guestfs_touch (handle, "/hello");
68  
69  /* you only need to call guestfs_sync if you have made
70   * changes to the guest image
71   */
72  guestfs_sync (handle);
73  
74  guestfs_close (handle);
75
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.
79
80 All functions that return integers, return C<-1> on error.  See
81 section ERROR HANDLING below for how to handle errors.
82
83 =head2 guestfs_h *
84
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.
88
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
91 image).
92
93 =head2 guestfs_create
94
95  guestfs_h *guestfs_create (void);
96
97 Create a connection handle.
98
99 You have to call C<guestfs_add_drive> on the handle at least once.
100
101 This function returns a non-NULL pointer to a handle on success or
102 NULL on error.
103
104 After configuring the handle, you have to call C<guestfs_launch> and
105 C<guestfs_wait_ready>.
106
107 You may also want to configure error handling for the handle.  See
108 ERROR HANDLING section below.
109
110 =head2 guestfs_close
111
112  void guestfs_close (guestfs_h *handle);
113
114 This closes the connection handle and frees up all resources used.
115
116 =head1 ERROR HANDLING
117
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>.
122
123 The default error handler prints the information string to C<stderr>.
124
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>.
128
129 =head2 guestfs_last_error
130
131  const char *guestfs_last_error (guestfs_h *handle);
132
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
135 returns C<NULL>.
136
137 The lifetime of the returned string is until the next error occurs, or
138 C<guestfs_close> is called.
139
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.
143
144 =head2 guestfs_set_error_handler
145
146  typedef void (*guestfs_error_handler_cb) (guestfs_h *handle,
147                                            void *data,
148                                            const char *msg);
149  void guestfs_set_error_handler (guestfs_h *handle,
150                                  guestfs_error_handler_cb cb,
151                                  void *data);
152
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.
156
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
159 your own copy.
160
161 The default handler prints messages on C<stderr>.
162
163 If you set C<cb> to C<NULL> then I<no> handler is called.
164
165 =head2 guestfs_get_error_handler
166
167  guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *handle,
168                                                      void **data_rtn);
169
170 Returns the current error handler callback.
171
172 =head2 guestfs_set_out_of_memory_handler 
173
174  typedef void (*guestfs_abort_cb) (void);
175  int guestfs_set_out_of_memory_handler (guestfs_h *handle,
176                                         guestfs_abort_cb);
177
178 The callback C<cb> will be called if there is an out of memory
179 situation.  I<Note this callback must not return>.
180
181 The default is to call L<abort(3)>.
182
183 You cannot set C<cb> to C<NULL>.  You can't ignore out of memory
184 situations.
185
186 =head2 guestfs_get_out_of_memory_handler
187
188  guestfs_abort_fn guestfs_get_out_of_memory_handler (guestfs_h *handle);
189
190 This returns the current out of memory handler.
191
192 =head1 PATH
193
194 Libguestfs needs a kernel and initrd.img, which it finds by looking
195 along an internal path.
196
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>).
199
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>.
206
207 =head1 HIGH-LEVEL API ACTIONS
208
209 @ACTIONS@
210
211 =head1 STRUCTURES
212
213 @STRUCTS@
214
215 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
216
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
221 child process.
222
223                             child process
224   ___________________       _________________________
225  /                   \     /                         \
226  | main program      |     | qemu +-----------------+|
227  |                   |     |      | Linux kernel    ||
228  +-------------------+     |      +-----------------+|
229  | libguestfs     <-------------->| guestfsd        ||
230  |                   |     |      +-----------------+|
231  \___________________/     \_________________________/
232
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.
239
240 =head2 STATE MACHINE
241
242 libguestfs uses a state machine to model the child process:
243
244                          |
245                     guestfs_create
246                          |
247                          |
248                      ____V_____
249                     /          \
250                     |  CONFIG  |
251                     \__________/
252                      ^ ^   ^  \
253                     /  |    \  \ guestfs_launch
254                    /   |    _\__V______
255                   /    |   /           \
256                  /     |   | LAUNCHING |
257                 /      |   \___________/
258                /       |       /
259               /        |  guestfs_wait_ready
260              /         |     /
261     ______  /        __|____V
262    /      \ ------> /        \
263    | BUSY |         | READY  |
264    \______/ <------ \________/
265
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.
270
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.
274
275 Configuration commands for qemu such as C<guestfs_add_drive> can only
276 be issued when in the CONFIG state.
277
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>.
285
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).
293
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.
298
299 =head2 SETTING CALLBACKS TO HANDLE EVENTS
300
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.
304
305 Use the C<guestfs_set_*_callback> functions to set a callback for
306 different types of events.
307
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>.
312
313 =head2 NON-BLOCKING ACTIONS
314
315 XXX NOT IMPLEMENTED YET XXX
316
317 C<guestfs_set_reply_callback> is the most interesting callback to
318 play with, since it allows you to perform actions without blocking.
319
320 For example:
321
322  do_it ()
323  {
324    start_call ();
325    guestfs_main_loop_run (); /* --> blocks, then calls my_cb */
326  }
327
328  start_call ()
329  {
330    guestfs_set_reply_callback (handle, my_cb, data);
331    guestfs_nb_[action] (handle, [other parameters ...]);
332    /* returns immediately */
333  }
334  
335  my_cb (guestfs_h *handle, void *data, XDR *xdr)
336  {
337    retval = guestfs_nb_[action]_r (handle, xdr);
338    /* ... */
339  }
340
341 There are C<guestfs_nb_*> and C<guestfs_nb_*_r> functions
342 corresponding to every C<guestfs_*> action in the high-level API.
343
344 =head2 guestfs_set_reply_callback
345
346  typedef void (*guestfs_reply_cb) (guestfs_h *g, void *opaque, XDR *xdr);
347  void guestfs_set_reply_callback (guestfs_h *handle,
348                                   guestfs_reply_cb cb,
349                                   void *opaque);
350
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).
354
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).
358
359 =head2 guestfs_set_log_message_callback
360
361  typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
362                                          char *buf, int len);
363  void guestfs_set_log_message_callback (guestfs_h *handle,
364                                         guestfs_log_message_cb cb,
365                                         void *opaque);
366
367 The callback function C<cb> will be called whenever qemu or the guest
368 writes anything to the console.
369
370 Use this function to capture kernel messages and similar.
371
372 Normally there is no log message handler, and log messages are just
373 discarded.
374
375 =head2 guestfs_set_subprocess_quit_callback
376
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,
380                                             void *opaque);
381
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).
386
387 =head2 guestfs_set_launch_done_callback
388
389  typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
390  void guestfs_set_launch_done_callback (guestfs_h *handle,
391                                         guestfs_ready_cb cb,
392                                         void *opaque);
393
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).
397
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.
400
401 =head2 EVENT MAIN LOOP
402
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:
406
407 =over 4
408
409 =item libguestfs-select
410
411 A simple main loop that is implemented using L<select(2)>.
412
413 This is the default main loop unless you call C<guestfs_set_main_loop>
414 or C<guestfs_glib_set_main_loop>.
415
416 =item libguestfs-glib
417
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.
421
422 =back
423
424 =head2 guestfs_set_main_loop
425
426  void guestfs_set_main_loop (guestfs_main_loop *);
427
428 This call sets the current main loop to the list of callbacks
429 contained in the C<guestfs_main_loop> structure.
430
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).
435
436 You should call this early in the main program, certainly before
437 calling C<guestfs_create>.
438
439 =head2 guestfs_glib_set_main_loop
440
441  void guestfs_glib_set_main_loop (GMainLoop *);
442
443 This helper calls C<guestfs_set_main_loop> with the correct callbacks
444 for integrating with the GLib main loop.
445
446 The libguestfs-glib main loop is contained in a separate library, so
447 that libguestfs doesn't depend on the whole of GLib:
448
449  #include <glib.h>
450  #include <guestfs-glib.h>
451
452  main ()
453  {
454    GMainLoop *loop =
455      g_main_loop_new (g_main_context_default (), 1);
456    ...
457    guestfs_glib_set_main_loop (loop);
458    ...
459    g_main_loop_run (loop);
460  }
461
462 To use this main loop you must link with C<-lguestfs-glib>.  (See also
463 the GLib and GTK+ documentation).
464
465 =head2 guestfs_main_loop_run
466
467  void guestfs_main_loop_run (void);
468
469 This calls the main loop.
470
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
474 call.
475
476 =head2 guestfs_main_loop_quit
477
478  void guestfs_main_loop_quit (void);
479
480 This instructs the main loop to quit.  In other words,
481 C<guestfs_main_loop_run> will return.
482
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.
485
486 =head2 WRITING A CUSTOM MAIN LOOP
487
488 This isn't documented.  Please see the libguestfs-select and
489 libguestfs-glib implementations.
490
491 =head1 ENVIRONMENT VARIABLES
492
493 =over 4
494
495 =item LIBGUESTFS_DEBUG
496
497 Set C<LIBGUESTFS_DEBUG=1> to enable verbose messages.  This
498 has the same effect as calling C<guestfs_set_verbose (handle, 1)>.
499
500 =item LIBGUESTFS_PATH
501
502 Set the path that libguestfs uses to search for kernel and initrd.img.
503 See the discussion of paths in section PATH above.
504
505 =back
506
507 =head1 SEE ALSO
508
509 L<guestfish(1)>,
510 L<qemu(1)>,
511 L<febootstrap(1)>,
512 L<http://et.redhat.com/~rjones/libguestfs>.
513
514 =head1 AUTHORS
515
516 Richard W.M. Jones (C<rjones at redhat dot com>)
517
518 =head1 COPYRIGHT
519
520 Copyright (C) 2009 Red Hat Inc.
521 L<http://et.redhat.com/~rjones/libguestfs>
522
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.
527
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.
532
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