Added guestfish(1) manpage.
[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 other languages, if people
41 contribute the language bindings).  You can also use it from shell
42 scripts or the command line.
43
44 You don't need to be root to use libguestfs, although obviously you do
45 need enough permissions to access the disk images.
46
47 =head1 CONNECTION MANAGEMENT
48
49 If you are using the high-level API, then you should call the
50 functions in the following order:
51
52  guestfs_h *handle = guestfs_create ();
53  
54  guestfs_add_drive (handle, "guest.img");
55  /* call guestfs_add_drive additional times if the guest has
56   * multiple disks
57   */
58  
59  guestfs_launch (handle);
60  guestfs_wait_ready (handle);
61
62  /* now you can examine what partitions, LVs etc are available
63   * you have to mount / at least
64   */ 
65  guestfs_mount (handle, "/dev/sda1", "/");
66
67  /* now you can perform actions on the guest disk image */
68  guestfs_touch (handle, "/hello");
69  
70  /* you only need to call guestfs_sync if you have made
71   * changes to the guest image
72   */
73  guestfs_sync (handle);
74  
75  guestfs_close (handle);
76
77 C<guestfs_wait_ready> and all of the actions including C<guestfs_sync>
78 are blocking calls.  You can use the low-level event API to do
79 non-blocking operations instead.
80
81 All functions that return integers, return C<-1> on error.  See
82 section ERROR HANDLING below for how to handle errors.
83
84 =head2 guestfs_h *
85
86 C<guestfs_h> is the opaque type representing a connection handle.
87 Create a handle by calling C<guestfs_create>.  Call C<guestfs_close>
88 to free the handle and release all resources used.
89
90 Handles and operations on handles are not thread safe.  However you
91 can use a separate handle for each thread (but not on the same disk
92 image).
93
94 =head2 guestfs_create
95
96  guestfs_h *guestfs_create (void);
97
98 Create a connection handle.
99
100 You have to call C<guestfs_add_drive> on the handle at least once.
101 See CONFIGURATION MANAGEMENT section below.
102
103 This function returns a non-NULL pointer to a handle on success or
104 NULL on error.
105
106 After configuring the handle, you have to call C<guestfs_launch> and
107 C<guestfs_wait_ready>.
108
109 You may also want to configure error handling for the handle.  See
110 ERROR HANDLING section below.
111
112 =head2 guestfs_close
113
114  void guestfs_close (guestfs_h *handle);
115
116 This closes the connection handle and frees up all resources used.
117
118 =head2 guestfs_launch, guestfs_wait_ready
119
120  int guestfs_launch (guestfs_h *handle);
121  int guestfs_wait_ready (guestfs_h *handle);
122
123 Internally libguestfs is implemented by running a virtual machine
124 using L<qemu(1)>.  These calls are necessary in order to boot the
125 virtual machine.  More discussion of this is available in the section
126 STATE MACHINE AND LOW-LEVEL EVENT API below.
127
128 You should call these two functions after configuring the handle
129 (eg. adding drives) but before performing any actions.
130
131 =head2 guestfs_kill_subprocess
132
133  int guestfs_kill_subprocess (guestfs_h *handle);
134
135 This kills the qemu subprocess.  You should never need to call this.
136
137 =head1 CONFIGURATION MANAGEMENT
138
139 The configuration functions allow you to configure which drive images
140 will be examined or modified, and set other aspects of the L<qemu(1)>
141 virtual machine that we will be running.  You need to call only
142 C<guestfs_add_drive> at least once for each guest image that you want
143 to examine.
144
145 =head2 guestfs_add_drive
146
147  int guestfs_add_drive (guestfs_h *handle, const char *filename);
148
149 This function adds a virtual machine disk image C<filename> to the
150 guest.  The first time you call this function, the disk appears as IDE
151 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
152 so on.
153
154 You don't necessarily need to be root when using libguestfs.  However
155 you obviously do need sufficient permissions to access the filename
156 for whatever operations you want to perform (ie. read access if you
157 just want to read the image or write access if you want to modify the
158 image).
159
160 This is equivalent to the qemu parameter C<-drive file=filename>.
161
162 =head2 guestfs_add_cdrom
163
164  int guestfs_add_cdrom (guestfs_h *handle, const char *filename);
165
166 This function adds a virtual CD-ROM disk image to the guest.
167
168 This is equivalent to the qemu parameter C<-cdrom filename>.
169
170 =head2 guestfs_config
171
172  int guestfs_config (guestfs_h *handle,
173                      const char *qemu_param, const char *qemu_value);
174
175 This can be used to add arbitrary qemu command line parameters
176 of the form C<-param value>.  Actually it's not quite arbitrary - we
177 prevent you from setting some parameters which would interfere with
178 parameters that we use.
179
180 The first character of C<qemu_param> string must be a C<-> (dash).
181
182 C<qemu_value> can be NULL.
183
184 =head1 ERROR HANDLING
185
186 The convention in all functions that return C<int> is that they return
187 C<-1> to indicate an error.  You can get additional information on
188 errors by calling C<guestfs_set_error_handler>.  The default error
189 handler prints the information string to C<stderr>.
190
191 Out of memory errors are handled differently.  The default action is
192 to call L<abort(3)>.  If this is undesirable, then you can set a
193 handler using C<guestfs_set_out_of_memory_handler>.
194
195 =head2 guestfs_set_error_handler
196
197  typedef void (*guestfs_error_handler_cb) (guestfs_h *handle,
198                                            void *data,
199                                            const char *msg);
200  void guestfs_set_error_handler (guestfs_h *handle,
201                                  guestfs_error_handler_cb cb,
202                                  void *data);
203
204 The callback C<cb> will be called if there is an error.  The
205 parameters passed to the callback are an opaque data pointer and the
206 error message string.
207
208 The default handler prints messages on C<stderr>.
209
210 If you set C<cb> to C<NULL> then I<no> handler is called and the error
211 message is completely discarded.
212
213 =head2 guestfs_get_error_handler
214
215  guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *handle,
216                                                      void **data_rtn);
217
218 Returns the current error handler callback.
219
220 =head2 guestfs_set_out_of_memory_handler 
221
222  typedef void (*guestfs_abort_cb) (void);
223  int guestfs_set_out_of_memory_handler (guestfs_h *handle,
224                                         guestfs_abort_cb);
225
226 The callback C<cb> will be called if there is an out of memory
227 situation.  I<Note this callback must not return>.
228
229 The default is to call L<abort(3)>.
230
231 You cannot set C<cb> to C<NULL>.  You can't ignore out of memory
232 situations.
233
234 =head2 guestfs_get_out_of_memory_handler
235
236  guestfs_abort_fn guestfs_get_out_of_memory_handler (guestfs_h *handle);
237
238 This returns the current out of memory handler.
239
240 =head1 PATH
241
242 Libguestfs needs a kernel and initrd.img, which it finds by looking
243 along an internal path.
244
245 By default it looks for these in the directory C<$libdir/guestfs>
246 (eg. C</usr/local/lib/guestfs> or C</usr/lib64/guestfs>).
247
248 Use C<guestfs_set_path> or set the environment variable
249 C<LIBGUESTFS_PATH> to change the directories that libguestfs will
250 search in.  The value is a colon-separated list of paths.  The current
251 directory is I<not> searched unless the path contains an empty element
252 or C<.>.  For example C<LIBGUESTFS_PATH=:/usr/lib/guestfs> would
253 search the current directory and then C</usr/lib/guestfs>.
254
255 =head2 guestfs_set_path
256
257  void guestfs_set_path (guestfs_h *handle, const char *path);
258
259 Set the path that libguestfs searches for kernel and initrd.img.
260
261 The default is C<$libdir/guestfs> unless overridden by setting
262 C<LIBGUESTFS_PATH> environment variable.
263
264 The string C<path> is stashed in the libguestfs handle, so the caller
265 must make sure it remains valid for the lifetime of the handle.
266
267 Setting C<path> to C<NULL> restores the default path.
268
269 =head2 guestfs_get_path
270
271  const char *guestfs_get_path (guestfs_h *handle);
272
273 Return the current search path.
274
275 This is always non-NULL.  If it wasn't set already, then this will
276 return the default path.
277
278 =head1 AUTOSYNC
279
280 =head2 guestfs_set_autosync
281
282  void guestfs_set_autosync (guestfs_h *handle, int autosync);
283
284 If C<autosync> is true, this enables autosync.  Libguestfs will make a
285 best effort attempt to run C<guestfs_sync> when the handle is closed
286 (also if the program exits without closing handles).
287
288 =head2 guestfs_get_autosync
289
290  int guestfs_get_autosync (guestfs_h *handle);
291
292 Get the autosync flag.
293
294 =head1 VERBOSE MESSAGES
295
296 =head2 guestfs_set_verbose
297
298  void guestfs_set_verbose (guestfs_h *handle, int verbose);
299
300 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
301
302 Verbose messages are disabled unless the environment variable
303 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.
304
305 =head2 guestfs_get_verbose
306
307  int guestfs_get_verbose (guestfs_h *handle);
308
309 This returns the verbose messages flag.
310
311 =head1 HIGH-LEVEL API ACTIONS
312
313 @ACTIONS@
314
315 =head1 STRUCTURES
316
317 @STRUCTS@
318
319 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
320
321 Internally, libguestfs is implemented by running a virtual machine
322 using L<qemu(1)>.  QEmu runs as a child process of the main program,
323 and most of this discussion won't make sense unless you understand
324 that the complexity is dealing with the (asynchronous) actions of the
325 child process.
326
327                             child process
328   ___________________       _________________________
329  /                   \     /                         \
330  | main program      |     | qemu +-----------------+|
331  |                   |     |      | Linux kernel    ||
332  +-------------------+     |      +-----------------+|
333  | libguestfs     <-------------->| guestfsd        ||
334  |                   |     |      +-----------------+|
335  \___________________/     \_________________________/
336
337 The diagram above shows libguestfs communicating with the guestfsd
338 daemon running inside the qemu child process.  There are several
339 points of failure here: qemu can fail to start, the virtual machine
340 inside qemu can fail to boot, guestfsd can fail to start or not
341 establish communication, any component can start successfully but fail
342 asynchronously later, and so on.
343
344 =head2 STATE MACHINE
345
346 libguestfs uses a state machine to model the child process:
347
348                          |
349                     guestfs_create
350                          |
351                          |
352                      ____V_____
353                     /          \
354                     |  CONFIG  |
355                     \__________/
356                      ^ ^   ^  \
357                     /  |    \  \ guestfs_launch
358                    /   |    _\__V______
359                   /    |   /           \
360                  /     |   | LAUNCHING |
361                 /      |   \___________/
362                /       |       /
363               /        |  guestfs_wait_ready
364              /         |     /
365     ______  /        __|____V
366    /      \ ------> /        \
367    | BUSY |         | READY  |
368    \______/ <------ \________/
369
370 The normal transitions are (1) CONFIG (when the handle is created, but
371 there is no child process), (2) LAUNCHING (when the child process is
372 booting up), (3) alternating between READY and BUSY as commands are
373 issued to, and carried out by, the child process.
374
375 The guest may be killed by C<guestfs_kill_subprocess>, or may die
376 asynchronously at any time (eg. due to some internal error), and that
377 causes the state to transition back to CONFIG.
378
379 Configuration commands for qemu such as C<guestfs_add_drive> can only
380 be issued when in the CONFIG state.
381
382 The high-level API offers two calls that go from CONFIG through
383 LAUNCHING to READY.  C<guestfs_launch> is a non-blocking call that
384 starts up the child process, immediately moving from CONFIG to
385 LAUNCHING.  C<guestfs_wait_ready> blocks until the child process is
386 READY to accept commands (or until some failure or timeout).  The
387 low-level event API described below provides a non-blocking way to
388 replace C<guestfs_wait_ready>.
389
390 High-level API actions such as C<guestfs_mount> can only be issued
391 when in the READY state.  These high-level API calls block waiting for
392 the command to be carried out (ie. the state to transition to BUSY and
393 then back to READY).  But using the low-level event API, you get
394 non-blocking versions.  (But you can still only carry out one
395 operation per handle at a time - that is a limitation of the
396 communications protocol we use).
397
398 Finally, the child process sends asynchronous messages back to the
399 main program, such as kernel log messages.  Mostly these are ignored
400 by the high-level API, but using the low-level event API you can
401 register to receive these messages.
402
403 =head2 SETTING CALLBACKS TO HANDLE EVENTS
404
405 The child process generates events in some situations.  Current events
406 include: receiving a reply message after some action, receiving a log
407 message, the child process exits, &c.
408
409 Use the C<guestfs_set_*_callback> functions to set a callback for
410 different types of events.
411
412 Only I<one callback of each type> can be registered for each handle.
413 Calling C<guestfs_set_*_callback> again overwrites the previous
414 callback of that type.  Cancel all callbacks of this type by calling
415 this function with C<cb> set to C<NULL>.
416
417 =head2 NON-BLOCKING ACTIONS
418
419 XXX NOT IMPLEMENTED YET XXX
420
421 C<guestfs_set_reply_callback> is the most interesting callback to
422 play with, since it allows you to perform actions without blocking.
423
424 For example:
425
426  do_it ()
427  {
428    start_call ();
429    guestfs_main_loop_run (); /* --> blocks, then calls my_cb */
430  }
431
432  start_call ()
433  {
434    guestfs_set_reply_callback (handle, my_cb, data);
435    guestfs_nb_[action] (handle, [other parameters ...]);
436    /* returns immediately */
437  }
438  
439  my_cb (guestfs_h *handle, void *data, XDR *xdr)
440  {
441    retval = guestfs_nb_[action]_r (handle, xdr);
442    /* ... */
443  }
444
445 There are C<guestfs_nb_*> and C<guestfs_nb_*_r> functions
446 corresponding to every C<guestfs_*> action in the high-level API.
447
448 =head2 guestfs_set_reply_callback
449
450  typedef void (*guestfs_reply_cb) (guestfs_h *g, void *opaque, XDR *xdr);
451  void guestfs_set_reply_callback (guestfs_h *handle,
452                                   guestfs_reply_cb cb,
453                                   void *opaque);
454
455 The callback function C<cb> will be called whenever a reply is
456 received from the child process.  (This corresponds to a transition
457 from the BUSY state to the READY state).
458
459 Note that the C<xdr> that you get in the callback is in C<XDR_DECODE>
460 mode, and you need to consume it before you return from the callback
461 function (since it gets destroyed after).
462
463 =head2 guestfs_set_log_message_callback
464
465  typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
466                                          char *buf, int len);
467  void guestfs_set_log_message_callback (guestfs_h *handle,
468                                         guestfs_log_message_cb cb,
469                                         void *opaque);
470
471 The callback function C<cb> will be called whenever qemu or the guest
472 writes anything to the console.
473
474 Use this function to capture kernel messages and similar.
475
476 Normally there is no log message handler, and log messages are just
477 discarded.
478
479 =head2 guestfs_set_subprocess_quit_callback
480
481  typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
482  void guestfs_set_subprocess_quit_callback (guestfs_h *handle,
483                                             guestfs_subprocess_quit_cb cb,
484                                             void *opaque);
485
486 The callback function C<cb> will be called when the child process
487 quits, either asynchronously or if killed by
488 C<guestfs_kill_subprocess>.  (This corresponds to a transition from
489 any state to the CONFIG state).
490
491 =head2 guestfs_set_launch_done_callback
492
493  typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
494  void guestfs_set_launch_done_callback (guestfs_h *handle,
495                                         guestfs_ready_cb cb,
496                                         void *opaque);
497
498 The callback function C<cb> will be called when the child process
499 becomes ready first time after it has been launched.  (This
500 corresponds to a transition from LAUNCHING to the READY state).
501
502 You can use this instead of C<guestfs_wait_ready> to implement a
503 non-blocking wait for the child process to finish booting up.
504
505 =head2 EVENT MAIN LOOP
506
507 To use the low-level event API, you have to provide an event "main
508 loop".  You can write your own, but if you don't want to write one,
509 two are provided for you:
510
511 =over 4
512
513 =item libguestfs-select
514
515 A simple main loop that is implemented using L<select(2)>.
516
517 This is the default main loop unless you call C<guestfs_set_main_loop>
518 or C<guestfs_glib_set_main_loop>.
519
520 =item libguestfs-glib
521
522 An implementation which can be used with GLib and GTK+ programs.  You
523 can use this to write graphical (GTK+) programs which use libguestfs
524 without hanging during long or slow operations.
525
526 =back
527
528 =head2 guestfs_set_main_loop
529
530  void guestfs_set_main_loop (guestfs_main_loop *);
531
532 This call sets the current main loop to the list of callbacks
533 contained in the C<guestfs_main_loop> structure.
534
535 Only one main loop implementation can be used by libguestfs, so
536 calling this replaces the previous one.  (So this is something that
537 has to be done by the main program, but only the main program "knows"
538 that it is a GTK+ program or whatever).
539
540 You should call this early in the main program, certainly before
541 calling C<guestfs_create>.
542
543 =head2 guestfs_glib_set_main_loop
544
545  void guestfs_glib_set_main_loop (GMainLoop *);
546
547 This helper calls C<guestfs_set_main_loop> with the correct callbacks
548 for integrating with the GLib main loop.
549
550 The libguestfs-glib main loop is contained in a separate library, so
551 that libguestfs doesn't depend on the whole of GLib:
552
553  #include <glib.h>
554  #include <guestfs-glib.h>
555
556  main ()
557  {
558    GMainLoop *loop =
559      g_main_loop_new (g_main_context_default (), 1);
560    ...
561    guestfs_glib_set_main_loop (loop);
562    ...
563    g_main_loop_run (loop);
564  }
565
566 To use this main loop you must link with C<-lguestfs-glib>.  (See also
567 the GLib and GTK+ documentation).
568
569 =head2 guestfs_main_loop_run
570
571  void guestfs_main_loop_run (void);
572
573 This calls the main loop.
574
575 For some types of main loop you may want or prefer to call another
576 function, eg. C<g_main_loop_run>, or the main loop may already be
577 invoked by another part of your program.  In those cases, ignore this
578 call.
579
580 =head2 guestfs_main_loop_quit
581
582  void guestfs_main_loop_quit (void);
583
584 This instructs the main loop to quit.  In other words,
585 C<guestfs_main_loop_run> will return.
586
587 For some types of main loop you may want or prefer to call another
588 function, eg. C<g_main_loop_quit>.  In those cases, ignore this call.
589
590 =head2 WRITING A CUSTOM MAIN LOOP
591
592 This isn't documented.  Please see the libguestfs-select and
593 libguestfs-glib implementations.
594
595 =head1 ENVIRONMENT VARIABLES
596
597 =over 4
598
599 =item LIBGUESTFS_DEBUG
600
601 Set C<LIBGUESTFS_DEBUG=1> to enable verbose messages.  This
602 has the same effect as calling C<guestfs_set_verbose (handle, 1)>.
603
604 =item LIBGUESTFS_PATH
605
606 Set the path that libguestfs uses to search for kernel and initrd.img.
607 See the discussion of paths in C<guestfs_set_path> above.
608
609 =back
610
611 =head1 SEE ALSO
612
613 L<guestfish(1)>,
614 L<qemu(1)>,
615 L<febootstrap(1)>,
616 L<http://et.redhat.com/~rjones/libguestfs>.
617
618 =head1 AUTHORS
619
620 Richard W.M. Jones (C<rjones at redhat dot com>)
621
622 =head1 COPYRIGHT
623
624 Copyright (C) 2009 Red Hat Inc.
625 L<http://et.redhat.com/~rjones/libguestfs>
626
627 This library is free software; you can redistribute it and/or
628 modify it under the terms of the GNU Lesser General Public
629 License as published by the Free Software Foundation; either
630 version 2 of the License, or (at your option) any later version.
631
632 This library is distributed in the hope that it will be useful,
633 but WITHOUT ANY WARRANTY; without even the implied warranty of
634 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
635 Lesser General Public License for more details.
636
637 You should have received a copy of the GNU Lesser General Public
638 License along with this library; if not, write to the Free Software
639 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA