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