Remove *~ files when doing 'make clean'.
[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 or Perl).
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_set_error_handler>.  The default error
121 handler prints the information string to C<stderr>.
122
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>.
126
127 =head2 guestfs_set_error_handler
128
129  typedef void (*guestfs_error_handler_cb) (guestfs_h *handle,
130                                            void *data,
131                                            const char *msg);
132  void guestfs_set_error_handler (guestfs_h *handle,
133                                  guestfs_error_handler_cb cb,
134                                  void *data);
135
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.
139
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
142 your own copy.
143
144 The default handler prints messages on C<stderr>.
145
146 If you set C<cb> to C<NULL> then I<no> handler is called and the error
147 message is completely discarded.
148
149 =head2 guestfs_get_error_handler
150
151  guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *handle,
152                                                      void **data_rtn);
153
154 Returns the current error handler callback.
155
156 =head2 guestfs_set_out_of_memory_handler 
157
158  typedef void (*guestfs_abort_cb) (void);
159  int guestfs_set_out_of_memory_handler (guestfs_h *handle,
160                                         guestfs_abort_cb);
161
162 The callback C<cb> will be called if there is an out of memory
163 situation.  I<Note this callback must not return>.
164
165 The default is to call L<abort(3)>.
166
167 You cannot set C<cb> to C<NULL>.  You can't ignore out of memory
168 situations.
169
170 =head2 guestfs_get_out_of_memory_handler
171
172  guestfs_abort_fn guestfs_get_out_of_memory_handler (guestfs_h *handle);
173
174 This returns the current out of memory handler.
175
176 =head1 PATH
177
178 Libguestfs needs a kernel and initrd.img, which it finds by looking
179 along an internal path.
180
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>).
183
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>.
190
191 =head1 HIGH-LEVEL API ACTIONS
192
193 @ACTIONS@
194
195 =head1 STRUCTURES
196
197 @STRUCTS@
198
199 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
200
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
205 child process.
206
207                             child process
208   ___________________       _________________________
209  /                   \     /                         \
210  | main program      |     | qemu +-----------------+|
211  |                   |     |      | Linux kernel    ||
212  +-------------------+     |      +-----------------+|
213  | libguestfs     <-------------->| guestfsd        ||
214  |                   |     |      +-----------------+|
215  \___________________/     \_________________________/
216
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.
223
224 =head2 STATE MACHINE
225
226 libguestfs uses a state machine to model the child process:
227
228                          |
229                     guestfs_create
230                          |
231                          |
232                      ____V_____
233                     /          \
234                     |  CONFIG  |
235                     \__________/
236                      ^ ^   ^  \
237                     /  |    \  \ guestfs_launch
238                    /   |    _\__V______
239                   /    |   /           \
240                  /     |   | LAUNCHING |
241                 /      |   \___________/
242                /       |       /
243               /        |  guestfs_wait_ready
244              /         |     /
245     ______  /        __|____V
246    /      \ ------> /        \
247    | BUSY |         | READY  |
248    \______/ <------ \________/
249
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.
254
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.
258
259 Configuration commands for qemu such as C<guestfs_add_drive> can only
260 be issued when in the CONFIG state.
261
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>.
269
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).
277
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.
282
283 =head2 SETTING CALLBACKS TO HANDLE EVENTS
284
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.
288
289 Use the C<guestfs_set_*_callback> functions to set a callback for
290 different types of events.
291
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>.
296
297 =head2 NON-BLOCKING ACTIONS
298
299 XXX NOT IMPLEMENTED YET XXX
300
301 C<guestfs_set_reply_callback> is the most interesting callback to
302 play with, since it allows you to perform actions without blocking.
303
304 For example:
305
306  do_it ()
307  {
308    start_call ();
309    guestfs_main_loop_run (); /* --> blocks, then calls my_cb */
310  }
311
312  start_call ()
313  {
314    guestfs_set_reply_callback (handle, my_cb, data);
315    guestfs_nb_[action] (handle, [other parameters ...]);
316    /* returns immediately */
317  }
318  
319  my_cb (guestfs_h *handle, void *data, XDR *xdr)
320  {
321    retval = guestfs_nb_[action]_r (handle, xdr);
322    /* ... */
323  }
324
325 There are C<guestfs_nb_*> and C<guestfs_nb_*_r> functions
326 corresponding to every C<guestfs_*> action in the high-level API.
327
328 =head2 guestfs_set_reply_callback
329
330  typedef void (*guestfs_reply_cb) (guestfs_h *g, void *opaque, XDR *xdr);
331  void guestfs_set_reply_callback (guestfs_h *handle,
332                                   guestfs_reply_cb cb,
333                                   void *opaque);
334
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).
338
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).
342
343 =head2 guestfs_set_log_message_callback
344
345  typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
346                                          char *buf, int len);
347  void guestfs_set_log_message_callback (guestfs_h *handle,
348                                         guestfs_log_message_cb cb,
349                                         void *opaque);
350
351 The callback function C<cb> will be called whenever qemu or the guest
352 writes anything to the console.
353
354 Use this function to capture kernel messages and similar.
355
356 Normally there is no log message handler, and log messages are just
357 discarded.
358
359 =head2 guestfs_set_subprocess_quit_callback
360
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,
364                                             void *opaque);
365
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).
370
371 =head2 guestfs_set_launch_done_callback
372
373  typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
374  void guestfs_set_launch_done_callback (guestfs_h *handle,
375                                         guestfs_ready_cb cb,
376                                         void *opaque);
377
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).
381
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.
384
385 =head2 EVENT MAIN LOOP
386
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:
390
391 =over 4
392
393 =item libguestfs-select
394
395 A simple main loop that is implemented using L<select(2)>.
396
397 This is the default main loop unless you call C<guestfs_set_main_loop>
398 or C<guestfs_glib_set_main_loop>.
399
400 =item libguestfs-glib
401
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.
405
406 =back
407
408 =head2 guestfs_set_main_loop
409
410  void guestfs_set_main_loop (guestfs_main_loop *);
411
412 This call sets the current main loop to the list of callbacks
413 contained in the C<guestfs_main_loop> structure.
414
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).
419
420 You should call this early in the main program, certainly before
421 calling C<guestfs_create>.
422
423 =head2 guestfs_glib_set_main_loop
424
425  void guestfs_glib_set_main_loop (GMainLoop *);
426
427 This helper calls C<guestfs_set_main_loop> with the correct callbacks
428 for integrating with the GLib main loop.
429
430 The libguestfs-glib main loop is contained in a separate library, so
431 that libguestfs doesn't depend on the whole of GLib:
432
433  #include <glib.h>
434  #include <guestfs-glib.h>
435
436  main ()
437  {
438    GMainLoop *loop =
439      g_main_loop_new (g_main_context_default (), 1);
440    ...
441    guestfs_glib_set_main_loop (loop);
442    ...
443    g_main_loop_run (loop);
444  }
445
446 To use this main loop you must link with C<-lguestfs-glib>.  (See also
447 the GLib and GTK+ documentation).
448
449 =head2 guestfs_main_loop_run
450
451  void guestfs_main_loop_run (void);
452
453 This calls the main loop.
454
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
458 call.
459
460 =head2 guestfs_main_loop_quit
461
462  void guestfs_main_loop_quit (void);
463
464 This instructs the main loop to quit.  In other words,
465 C<guestfs_main_loop_run> will return.
466
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.
469
470 =head2 WRITING A CUSTOM MAIN LOOP
471
472 This isn't documented.  Please see the libguestfs-select and
473 libguestfs-glib implementations.
474
475 =head1 ENVIRONMENT VARIABLES
476
477 =over 4
478
479 =item LIBGUESTFS_DEBUG
480
481 Set C<LIBGUESTFS_DEBUG=1> to enable verbose messages.  This
482 has the same effect as calling C<guestfs_set_verbose (handle, 1)>.
483
484 =item LIBGUESTFS_PATH
485
486 Set the path that libguestfs uses to search for kernel and initrd.img.
487 See the discussion of paths in section PATH above.
488
489 =back
490
491 =head1 SEE ALSO
492
493 L<guestfish(1)>,
494 L<qemu(1)>,
495 L<febootstrap(1)>,
496 L<http://et.redhat.com/~rjones/libguestfs>.
497
498 =head1 AUTHORS
499
500 Richard W.M. Jones (C<rjones at redhat dot com>)
501
502 =head1 COPYRIGHT
503
504 Copyright (C) 2009 Red Hat Inc.
505 L<http://et.redhat.com/~rjones/libguestfs>
506
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.
511
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.
516
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