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