set-append and set-kernel parameters are both nullable.
[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, Python, Ruby, Java
41 or Haskell).  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 For information on using multiple handles and threads, see the section
90 MULTIPLE HANDLES AND MULTIPLE THREADS below.
91
92 =head2 guestfs_create
93
94  guestfs_h *guestfs_create (void);
95
96 Create a connection handle.
97
98 You have to call C<guestfs_add_drive> on the handle at least once.
99
100 This function returns a non-NULL pointer to a handle on success or
101 NULL on error.
102
103 After configuring the handle, you have to call C<guestfs_launch> and
104 C<guestfs_wait_ready>.
105
106 You may also want to configure error handling for the handle.  See
107 ERROR HANDLING section below.
108
109 =head2 guestfs_close
110
111  void guestfs_close (guestfs_h *handle);
112
113 This closes the connection handle and frees up all resources used.
114
115 =head1 ERROR HANDLING
116
117 The convention in all functions that return C<int> is that they return
118 C<-1> to indicate an error.  You can get additional information on
119 errors by calling C<guestfs_last_error> and/or by setting up an error
120 handler with C<guestfs_set_error_handler>.
121
122 The default error handler prints the information string to C<stderr>.
123
124 Out of memory errors are handled differently.  The default action is
125 to call L<abort(3)>.  If this is undesirable, then you can set a
126 handler using C<guestfs_set_out_of_memory_handler>.
127
128 =head2 guestfs_last_error
129
130  const char *guestfs_last_error (guestfs_h *handle);
131
132 This returns the last error message that happened on C<handle>.  If
133 there has not been an error since the handle was created, then this
134 returns C<NULL>.
135
136 The lifetime of the returned string is until the next error occurs, or
137 C<guestfs_close> is called.
138
139 The error string is not localized (ie. is always in English), because
140 this makes searching for error messages in search engines give the
141 largest number of results.
142
143 =head2 guestfs_set_error_handler
144
145  typedef void (*guestfs_error_handler_cb) (guestfs_h *handle,
146                                            void *data,
147                                            const char *msg);
148  void guestfs_set_error_handler (guestfs_h *handle,
149                                  guestfs_error_handler_cb cb,
150                                  void *data);
151
152 The callback C<cb> will be called if there is an error.  The
153 parameters passed to the callback are an opaque data pointer and the
154 error message string.
155
156 Note that the message string C<msg> is freed as soon as the callback
157 function returns, so if you want to stash it somewhere you must make
158 your own copy.
159
160 The default handler prints messages on C<stderr>.
161
162 If you set C<cb> to C<NULL> then I<no> handler is called.
163
164 =head2 guestfs_get_error_handler
165
166  guestfs_error_handler_cb guestfs_get_error_handler (guestfs_h *handle,
167                                                      void **data_rtn);
168
169 Returns the current error handler callback.
170
171 =head2 guestfs_set_out_of_memory_handler
172
173  typedef void (*guestfs_abort_cb) (void);
174  int guestfs_set_out_of_memory_handler (guestfs_h *handle,
175                                         guestfs_abort_cb);
176
177 The callback C<cb> will be called if there is an out of memory
178 situation.  I<Note this callback must not return>.
179
180 The default is to call L<abort(3)>.
181
182 You cannot set C<cb> to C<NULL>.  You can't ignore out of memory
183 situations.
184
185 =head2 guestfs_get_out_of_memory_handler
186
187  guestfs_abort_fn guestfs_get_out_of_memory_handler (guestfs_h *handle);
188
189 This returns the current out of memory handler.
190
191 =head1 PATH
192
193 Libguestfs needs a kernel and initrd.img, which it finds by looking
194 along an internal path.
195
196 By default it looks for these in the directory C<$libdir/guestfs>
197 (eg. C</usr/local/lib/guestfs> or C</usr/lib64/guestfs>).
198
199 Use C<guestfs_set_path> or set the environment variable
200 C<LIBGUESTFS_PATH> to change the directories that libguestfs will
201 search in.  The value is a colon-separated list of paths.  The current
202 directory is I<not> searched unless the path contains an empty element
203 or C<.>.  For example C<LIBGUESTFS_PATH=:/usr/lib/guestfs> would
204 search the current directory and then C</usr/lib/guestfs>.
205
206 =head1 API OVERVIEW
207
208 This section provides additional documentation for groups of API
209 calls, which may not be obvious from reading about the individual
210 calls below.
211
212 =head2 LVM2
213
214 Libguestfs provides access to a large part of the LVM2 API.  It won't
215 make much sense unless you familiarize yourself with the concepts of
216 physical volumes, volume groups and logical volumes.
217
218 This author strongly recommends reading the LVM HOWTO, online at
219 L<http://tldp.org/HOWTO/LVM-HOWTO/>.
220
221 =head2 PARTITIONING
222
223 To create MBR-style (ie. normal PC) partitions use one of the
224 C<guestfs_sfdisk*> variants.  These calls use the external
225 L<sfdisk(8)> command.
226
227 The simplest call is:
228
229  char *lines[] = { ",", NULL };
230  guestfs_sfdiskM (g, "/dev/sda", lines);
231
232 This will create a single partition on C</dev/sda> called
233 C</dev/sda1> covering the whole disk.
234
235 In general MBR partitions are both unnecessarily complicated and
236 depend on archaic details, namely the Cylinder-Head-Sector (CHS)
237 geometry of the disk.  C<guestfs_sfdiskM> allows you to specify sizes
238 in megabytes instead of cylinders, which is a small win.
239 C<guestfs_sfdiskM> will choose the nearest cylinder to approximate the
240 requested size.  There's a lot of crazy stuff to do with IDE and
241 virtio disks having different, incompatible CHS geometries, that you
242 probably don't want to know about.  My advice: make a single partition
243 to cover the whole disk, then use LVM on top.
244
245 In future we aim to provide access to libparted.
246
247 =head2 UPLOADING
248
249 For small, single files, use C<guestfs_write_file>.  In some versions
250 of libguestfs there was a bug which limited this call to text files
251 (not containing ASCII NUL characters).
252
253 To upload a single file, use C<guestfs_upload>.  This call has no
254 limits on file content or size (even files larger than 4 GB).
255
256 To upload multiple files, see C<guestfs_tar_in> and C<guestfs_tgz_in>.
257
258 However the fastest way to upload I<large numbers of arbitrary files>
259 is to turn them into a squashfs or CD ISO (see L<mksquashfs(8)> and
260 L<mkisofs(8)>), then attach this using C<guestfs_add_drive_ro>.  If
261 you add the drive in a predictable way (eg. adding it last after all
262 other drives) then you can get the device name from
263 C<guestfs_list_devices> and mount it directly using
264 C<guestfs_mount_ro>.  Note that squashfs images are sometimes
265 non-portable between kernel versions, and they don't support labels or
266 UUIDs.  If you want to pre-build an image or you need to mount it
267 using a label or UUID, use an ISO image instead.
268
269 =head2 DOWNLOADING
270
271 Use C<guestfs_cat> to download small, text only files.  This call
272 is limited to files which are less than 2 MB and which cannot contain
273 any ASCII NUL (C<\0>) characters.  However it has a very simple
274 to use API.
275
276 C<guestfs_read_file> can be used to read files which contain
277 arbitrary 8 bit data, since it returns a (pointer, size) pair.
278 However it is still limited to "small" files, less than 2 MB.
279
280 C<guestfs_download> can be used to download any file, with no
281 limits on content or size (even files larger than 4 GB).
282
283 To download multiple files, see C<guestfs_tar_out> and
284 C<guestfs_tgz_out>.
285
286 =head2 RUNNING COMMANDS
287
288 Although libguestfs is a primarily an API for manipulating files
289 inside guest images, we also provide some limited facilities for
290 running commands inside guests.
291
292 There are many limitations to this:
293
294 =over 4
295
296 =item *
297
298 The kernel version that the command runs under will be different
299 from what it expects.
300
301 =item *
302
303 If the command needs to communicate with daemons, then most likely
304 they won't be running.
305
306 =item *
307
308 The command will be running in limited memory.
309
310 =item *
311
312 Only supports Linux guests (not Windows, BSD, etc).
313
314 =item *
315
316 Architecture limitations (eg. won't work for a PPC guest on
317 an X86 host).
318
319 =back
320
321 The two main API calls to run commands are C<guestfs_command> and
322 C<guestfs_sh> (there are also variations).
323
324 The difference is that C<guestfs_sh> runs commands using the shell, so
325 any shell globs, redirections, etc will work.
326
327 =head2 LISTING FILES
328
329 C<guestfs_ll> is just designed for humans to read (mainly when using
330 the L<guestfish(1)>-equivalent command C<ll>).
331
332 C<guestfs_ls> is a quick way to get a list of files in a directory
333 from programs.
334
335 C<guestfs_readdir> is a programmatic way to get a list of files in a
336 directory, plus additional information about each one.
337
338 C<guestfs_find> can be used to recursively list files.
339
340 =head1 HIGH-LEVEL API ACTIONS
341
342 =head2 ABI GUARANTEE
343
344 We guarantee the libguestfs ABI (binary interface), for public,
345 high-level actions as outlined in this section.  Although we will
346 deprecate some actions, for example if they get replaced by newer
347 calls, we will keep the old actions forever.  This allows you the
348 developer to program in confidence against libguestfs.
349
350 @ACTIONS@
351
352 =head1 STRUCTURES
353
354 @STRUCTS@
355
356 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
357
358 Internally, libguestfs is implemented by running a virtual machine
359 using L<qemu(1)>.  QEmu runs as a child process of the main program,
360 and most of this discussion won't make sense unless you understand
361 that the complexity is dealing with the (asynchronous) actions of the
362 child process.
363
364                             child process
365   ___________________       _________________________
366  /                   \     /                         \
367  | main program      |     | qemu +-----------------+|
368  |                   |     |      | Linux kernel    ||
369  +-------------------+     |      +-----------------+|
370  | libguestfs     <-------------->| guestfsd        ||
371  |                   |     |      +-----------------+|
372  \___________________/     \_________________________/
373
374 The diagram above shows libguestfs communicating with the guestfsd
375 daemon running inside the qemu child process.  There are several
376 points of failure here: qemu can fail to start, the virtual machine
377 inside qemu can fail to boot, guestfsd can fail to start or not
378 establish communication, any component can start successfully but fail
379 asynchronously later, and so on.
380
381 =head2 STATE MACHINE
382
383 libguestfs uses a state machine to model the child process:
384
385                          |
386                     guestfs_create
387                          |
388                          |
389                      ____V_____
390                     /          \
391                     |  CONFIG  |
392                     \__________/
393                      ^ ^   ^  \
394                     /  |    \  \ guestfs_launch
395                    /   |    _\__V______
396                   /    |   /           \
397                  /     |   | LAUNCHING |
398                 /      |   \___________/
399                /       |       /
400               /        |  guestfs_wait_ready
401              /         |     /
402     ______  /        __|____V
403    /      \ ------> /        \
404    | BUSY |         | READY  |
405    \______/ <------ \________/
406
407 The normal transitions are (1) CONFIG (when the handle is created, but
408 there is no child process), (2) LAUNCHING (when the child process is
409 booting up), (3) alternating between READY and BUSY as commands are
410 issued to, and carried out by, the child process.
411
412 The guest may be killed by C<guestfs_kill_subprocess>, or may die
413 asynchronously at any time (eg. due to some internal error), and that
414 causes the state to transition back to CONFIG.
415
416 Configuration commands for qemu such as C<guestfs_add_drive> can only
417 be issued when in the CONFIG state.
418
419 The high-level API offers two calls that go from CONFIG through
420 LAUNCHING to READY.  C<guestfs_launch> is a non-blocking call that
421 starts up the child process, immediately moving from CONFIG to
422 LAUNCHING.  C<guestfs_wait_ready> blocks until the child process is
423 READY to accept commands (or until some failure or timeout).  The
424 low-level event API described below provides a non-blocking way to
425 replace C<guestfs_wait_ready>.
426
427 High-level API actions such as C<guestfs_mount> can only be issued
428 when in the READY state.  These high-level API calls block waiting for
429 the command to be carried out (ie. the state to transition to BUSY and
430 then back to READY).  But using the low-level event API, you get
431 non-blocking versions.  (But you can still only carry out one
432 operation per handle at a time - that is a limitation of the
433 communications protocol we use).
434
435 Finally, the child process sends asynchronous messages back to the
436 main program, such as kernel log messages.  Mostly these are ignored
437 by the high-level API, but using the low-level event API you can
438 register to receive these messages.
439
440 =head2 SETTING CALLBACKS TO HANDLE EVENTS
441
442 The child process generates events in some situations.  Current events
443 include: receiving a reply message after some action, receiving a log
444 message, the child process exits, &c.
445
446 Use the C<guestfs_set_*_callback> functions to set a callback for
447 different types of events.
448
449 Only I<one callback of each type> can be registered for each handle.
450 Calling C<guestfs_set_*_callback> again overwrites the previous
451 callback of that type.  Cancel all callbacks of this type by calling
452 this function with C<cb> set to C<NULL>.
453
454 =head2 NON-BLOCKING ACTIONS
455
456 XXX This section was documented in previous versions but never
457 implemented in a way which matched the documentation.  For now I have
458 removed the documentation, pending a working implementation.  See also
459 C<src/guestfs-actions.c> in the source.
460
461
462 =head2 guestfs_set_send_callback
463
464  typedef void (*guestfs_send_cb) (guestfs_h *g, void *opaque);
465  void guestfs_set_send_callback (guestfs_h *handle,
466                                  guestfs_send_cb cb,
467                                  void *opaque);
468
469 The callback function C<cb> will be called whenever a message
470 which is queued for sending, has been sent.
471
472 =head2 guestfs_set_reply_callback
473
474  typedef void (*guestfs_reply_cb) (guestfs_h *g, void *opaque, XDR *xdr);
475  void guestfs_set_reply_callback (guestfs_h *handle,
476                                   guestfs_reply_cb cb,
477                                   void *opaque);
478
479 The callback function C<cb> will be called whenever a reply is
480 received from the child process.  (This corresponds to a transition
481 from the BUSY state to the READY state).
482
483 Note that the C<xdr> that you get in the callback is in C<XDR_DECODE>
484 mode, and you need to consume it before you return from the callback
485 function (since it gets destroyed after).
486
487 =head2 guestfs_set_log_message_callback
488
489  typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
490                                          char *buf, int len);
491  void guestfs_set_log_message_callback (guestfs_h *handle,
492                                         guestfs_log_message_cb cb,
493                                         void *opaque);
494
495 The callback function C<cb> will be called whenever qemu or the guest
496 writes anything to the console.
497
498 Use this function to capture kernel messages and similar.
499
500 Normally there is no log message handler, and log messages are just
501 discarded.
502
503 =head2 guestfs_set_subprocess_quit_callback
504
505  typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
506  void guestfs_set_subprocess_quit_callback (guestfs_h *handle,
507                                             guestfs_subprocess_quit_cb cb,
508                                             void *opaque);
509
510 The callback function C<cb> will be called when the child process
511 quits, either asynchronously or if killed by
512 C<guestfs_kill_subprocess>.  (This corresponds to a transition from
513 any state to the CONFIG state).
514
515 =head2 guestfs_set_launch_done_callback
516
517  typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
518  void guestfs_set_launch_done_callback (guestfs_h *handle,
519                                         guestfs_ready_cb cb,
520                                         void *opaque);
521
522 The callback function C<cb> will be called when the child process
523 becomes ready first time after it has been launched.  (This
524 corresponds to a transition from LAUNCHING to the READY state).
525
526 You can use this instead of C<guestfs_wait_ready> to implement a
527 non-blocking wait for the child process to finish booting up.
528
529 =head2 EVENT MAIN LOOP
530
531 To use the low-level event API and/or to use handles from multiple
532 threads, you have to provide an event "main loop".  You can write your
533 own, but if you don't want to write one, two types are provided for
534 you:
535
536 =over 4
537
538 =item libguestfs-select
539
540 A simple main loop that is implemented using L<select(2)>.
541
542 This is the default main loop for new guestfs handles, unless you
543 call C<guestfs_set_main_loop> after a handle is created.
544
545 =item libguestfs-glib
546
547 An implementation which can be used with GLib and GTK+ programs.  You
548 can use this to write graphical (GTK+) programs which use libguestfs
549 without hanging during long or slow operations.
550
551 =back
552
553 =head2 MULTIPLE HANDLES AND MULTIPLE THREADS
554
555 The support for multiple handles and multiple threads is modelled
556 after glib (although doesn't require glib, if you use the select-based
557 main loop).
558
559 L<http://library.gnome.org/devel/glib/unstable/glib-The-Main-Event-Loop.html>
560
561 You will need to create one main loop for each thread that wants to
562 use libguestfs.  Each guestfs handle should be confined to one thread.
563 If you try to pass guestfs handles between threads, you will get
564 undefined results.
565
566 If you only want to use guestfs handles from one thread in your
567 program, but your program has other threads doing other things, then
568 you don't need to do anything special.
569
570 =head2 SINGLE THREAD CASE
571
572 In the single thread case, there is a single select-based main loop
573 created for you.  All guestfs handles will use this main loop to
574 execute high level API actions.
575
576 =head2 MULTIPLE THREADS CASE
577
578 In the multiple threads case, you will need to create a main loop for
579 each thread that wants to use libguestfs.
580
581 To create main loops for other threads, use
582 C<guestfs_create_main_loop> or C<guestfs_glib_create_main_loop>.
583
584 Then you will need to attach each handle to the thread-specific main
585 loop by calling:
586
587  handle = guestfs_create ();
588  guestfs_set_main_loop (handle, main_loop_of_current_thread);
589
590 =head2 guestfs_set_main_loop
591
592  void guestfs_set_main_loop (guestfs_h *handle,
593                              guestfs_main_loop *main_loop);
594
595 Sets the main loop used by high level API actions for this handle.  By
596 default, the select-based main loop is used (see
597 C<guestfs_get_default_main_loop>).
598
599 You only need to use this in multi-threaded programs, where multiple
600 threads want to use libguestfs.  Create a main loop for each thread,
601 then call this function.
602
603 You cannot pass guestfs handles between threads.
604
605 =head2 guestfs_get_main_loop
606
607  guestfs_main_loop *guestfs_get_main_loop (guestfs_h *handle);
608
609 Return the main loop used by C<handle>.
610
611 =head2 guestfs_get_default_main_loop
612
613  guestfs_main_loop *guestfs_get_default_main_loop (void);
614
615 Return the default select-based main loop.
616
617 =head2 guestfs_create_main_loop
618
619  guestfs_main_loop *guestfs_create_main_loop (void);
620
621 This creates a select-based main loop.  You should create one main
622 loop for each additional thread that needs to use libguestfs.
623
624 =head2 guestfs_free_main_loop
625
626  void guestfs_free_main_loop (guestfs_main_loop *);
627
628 Free the select-based main loop which was previously allocated with
629 C<guestfs_create_main_loop>.
630
631 =head2 WRITING A CUSTOM MAIN LOOP
632
633 This isn't documented.  Please see the libguestfs-select and
634 libguestfs-glib implementations.
635
636 =head1 BLOCK DEVICE NAMING
637
638 In the kernel there is now quite a profusion of schemata for naming
639 block devices (in this context, by I<block device> I mean a physical
640 or virtual hard drive).  The original Linux IDE driver used names
641 starting with C</dev/hd*>.  SCSI devices have historically used a
642 different naming scheme, C</dev/sd*>.  When the Linux kernel I<libata>
643 driver became a popular replacement for the old IDE driver
644 (particularly for SATA devices) those devices also used the
645 C</dev/sd*> scheme.  Additionally we now have virtual machines with
646 paravirtualized drivers.  This has created several different naming
647 systems, such as C</dev/vd*> for virtio disks and C</dev/xvd*> for Xen
648 PV disks.
649
650 As discussed above, libguestfs uses a qemu appliance running an
651 embedded Linux kernel to access block devices.  We can run a variety
652 of appliances based on a variety of Linux kernels.
653
654 This causes a problem for libguestfs because many API calls use device
655 or partition names.  Working scripts and the recipe (example) scripts
656 that we make available over the internet could fail if the naming
657 scheme changes.
658
659 Therefore libguestfs defines C</dev/sd*> as the I<standard naming
660 scheme>.  Internally C</dev/sd*> names are translated, if necessary,
661 to other names as required.  For example, under RHEL 5 which uses the
662 C</dev/hd*> scheme, any device parameter C</dev/sda2> is translated to
663 C</dev/hda2> transparently.
664
665 Note that this I<only> applies to parameters.  The
666 C<guestfs_list_devices>, C<guestfs_list_partitions> and similar calls
667 return the true names of the devices and partitions as known to the
668 appliance.
669
670 =head2 ALGORITHM FOR BLOCK DEVICE NAME TRANSLATION
671
672 Usually this translation is transparent.  However in some (very rare)
673 cases you may need to know the exact algorithm.  Such cases include
674 where you use C<guestfs_config> to add a mixture of virtio and IDE
675 devices to the qemu-based appliance, so have a mixture of C</dev/sd*>
676 and C</dev/vd*> devices.
677
678 The algorithm is applied only to I<parameters> which are known to be
679 either device or partition names.  Return values from functions such
680 as C<guestfs_list_devices> are never changed.
681
682 =over 4
683
684 =item *
685
686 Is the string a parameter which is a device or partition name?
687
688 =item *
689
690 Does the string begin with C</dev/sd>?
691
692 =item *
693
694 Does the named device exist?  If so, we use that device.
695 However if I<not> then we continue with this algorithm.
696
697 =item *
698
699 Replace initial C</dev/sd> string with C</dev/hd>.
700
701 For example, change C</dev/sda2> to C</dev/hda2>.
702
703 If that named device exists, use it.  If not, continue.
704
705 =item *
706
707 Replace initial C</dev/sd> string with C</dev/vd>.
708
709 If that named device exists, use it.  If not, return an error.
710
711 =back
712
713 =head2 PORTABILITY CONCERNS
714
715 Although the standard naming scheme and automatic translation is
716 useful for simple programs and guestfish scripts, for larger programs
717 it is best not to rely on this mechanism.
718
719 Where possible for maximum future portability programs using
720 libguestfs should use these future-proof techniques:
721
722 =over 4
723
724 =item *
725
726 Use C<guestfs_list_devices> or C<guestfs_list_partitions> to list
727 actual device names, and then use those names directly.
728
729 Since those device names exist by definition, they will never be
730 translated.
731
732 =item *
733
734 Use higher level ways to identify filesystems, such as LVM names,
735 UUIDs and filesystem labels.
736
737 =back
738
739 =head1 INTERNALS
740
741 =head2 COMMUNICATION PROTOCOL
742
743 Don't rely on using this protocol directly.  This section documents
744 how it currently works, but it may change at any time.
745
746 The protocol used to talk between the library and the daemon running
747 inside the qemu virtual machine is a simple RPC mechanism built on top
748 of XDR (RFC 1014, RFC 1832, RFC 4506).
749
750 The detailed format of structures is in C<src/guestfs_protocol.x>
751 (note: this file is automatically generated).
752
753 There are two broad cases, ordinary functions that don't have any
754 C<FileIn> and C<FileOut> parameters, which are handled with very
755 simple request/reply messages.  Then there are functions that have any
756 C<FileIn> or C<FileOut> parameters, which use the same request and
757 reply messages, but they may also be followed by files sent using a
758 chunked encoding.
759
760 =head3 ORDINARY FUNCTIONS (NO FILEIN/FILEOUT PARAMS)
761
762 For ordinary functions, the request message is:
763
764  total length (header + arguments,
765       but not including the length word itself)
766  struct guestfs_message_header (encoded as XDR)
767  struct guestfs_<foo>_args (encoded as XDR)
768
769 The total length field allows the daemon to allocate a fixed size
770 buffer into which it slurps the rest of the message.  As a result, the
771 total length is limited to C<GUESTFS_MESSAGE_MAX> bytes (currently
772 4MB), which means the effective size of any request is limited to
773 somewhere under this size.
774
775 Note also that many functions don't take any arguments, in which case
776 the C<guestfs_I<foo>_args> is completely omitted.
777
778 The header contains the procedure number (C<guestfs_proc>) which is
779 how the receiver knows what type of args structure to expect, or none
780 at all.
781
782 The reply message for ordinary functions is:
783
784  total length (header + ret,
785       but not including the length word itself)
786  struct guestfs_message_header (encoded as XDR)
787  struct guestfs_<foo>_ret (encoded as XDR)
788
789 As above the C<guestfs_I<foo>_ret> structure may be completely omitted
790 for functions that return no formal return values.
791
792 As above the total length of the reply is limited to
793 C<GUESTFS_MESSAGE_MAX>.
794
795 In the case of an error, a flag is set in the header, and the reply
796 message is slightly changed:
797
798  total length (header + error,
799       but not including the length word itself)
800  struct guestfs_message_header (encoded as XDR)
801  struct guestfs_message_error (encoded as XDR)
802
803 The C<guestfs_message_error> structure contains the error message as a
804 string.
805
806 =head3 FUNCTIONS THAT HAVE FILEIN PARAMETERS
807
808 A C<FileIn> parameter indicates that we transfer a file I<into> the
809 guest.  The normal request message is sent (see above).  However this
810 is followed by a sequence of file chunks.
811
812  total length (header + arguments,
813       but not including the length word itself,
814       and not including the chunks)
815  struct guestfs_message_header (encoded as XDR)
816  struct guestfs_<foo>_args (encoded as XDR)
817  sequence of chunks for FileIn param #0
818  sequence of chunks for FileIn param #1 etc.
819
820 The "sequence of chunks" is:
821
822  length of chunk (not including length word itself)
823  struct guestfs_chunk (encoded as XDR)
824  length of chunk
825  struct guestfs_chunk (encoded as XDR)
826    ...
827  length of chunk
828  struct guestfs_chunk (with data.data_len == 0)
829
830 The final chunk has the C<data_len> field set to zero.  Additionally a
831 flag is set in the final chunk to indicate either successful
832 completion or early cancellation.
833
834 At time of writing there are no functions that have more than one
835 FileIn parameter.  However this is (theoretically) supported, by
836 sending the sequence of chunks for each FileIn parameter one after
837 another (from left to right).
838
839 Both the library (sender) I<and> the daemon (receiver) may cancel the
840 transfer.  The library does this by sending a chunk with a special
841 flag set to indicate cancellation.  When the daemon sees this, it
842 cancels the whole RPC, does I<not> send any reply, and goes back to
843 reading the next request.
844
845 The daemon may also cancel.  It does this by writing a special word
846 C<GUESTFS_CANCEL_FLAG> to the socket.  The library listens for this
847 during the transfer, and if it gets it, it will cancel the transfer
848 (it sends a cancel chunk).  The special word is chosen so that even if
849 cancellation happens right at the end of the transfer (after the
850 library has finished writing and has started listening for the reply),
851 the "spurious" cancel flag will not be confused with the reply
852 message.
853
854 This protocol allows the transfer of arbitrary sized files (no 32 bit
855 limit), and also files where the size is not known in advance
856 (eg. from pipes or sockets).  However the chunks are rather small
857 (C<GUESTFS_MAX_CHUNK_SIZE>), so that neither the library nor the
858 daemon need to keep much in memory.
859
860 =head3 FUNCTIONS THAT HAVE FILEOUT PARAMETERS
861
862 The protocol for FileOut parameters is exactly the same as for FileIn
863 parameters, but with the roles of daemon and library reversed.
864
865  total length (header + ret,
866       but not including the length word itself,
867       and not including the chunks)
868  struct guestfs_message_header (encoded as XDR)
869  struct guestfs_<foo>_ret (encoded as XDR)
870  sequence of chunks for FileOut param #0
871  sequence of chunks for FileOut param #1 etc.
872
873 =head3 INITIAL MESSAGE
874
875 Because the underlying channel (QEmu -net channel) doesn't have any
876 sort of connection control, when the daemon launches it sends an
877 initial word (C<GUESTFS_LAUNCH_FLAG>) which indicates that the guest
878 and daemon is alive.  This is what C<guestfs_wait_ready> waits for.
879
880 =head1 QEMU WRAPPERS
881
882 If you want to compile your own qemu, run qemu from a non-standard
883 location, or pass extra arguments to qemu, then you can write a
884 shell-script wrapper around qemu.
885
886 There is one important rule to remember: you I<must C<exec qemu>> as
887 the last command in the shell script (so that qemu replaces the shell
888 and becomes the direct child of the libguestfs-using program).  If you
889 don't do this, then the qemu process won't be cleaned up correctly.
890
891 Here is an example of a wrapper, where I have built my own copy of
892 qemu from source:
893
894  #!/bin/sh -
895  qemudir=/home/rjones/d/qemu
896  exec $qemudir/x86_64-softmmu/qemu-system-x86_64 -L $qemudir/pc-bios "$@"
897
898 Save this script as C</tmp/qemu.wrapper> (or wherever), C<chmod +x>,
899 and then use it by setting the LIBGUESTFS_QEMU environment variable.
900 For example:
901
902  LIBGUESTFS_QEMU=/tmp/qemu.wrapper guestfish
903
904 Note that libguestfs also calls qemu with the -help and -version
905 options in order to determine features.
906
907 =head1 ENVIRONMENT VARIABLES
908
909 =over 4
910
911 =item LIBGUESTFS_APPEND
912
913 Pass additional options to the guest kernel.
914
915 =item LIBGUESTFS_DEBUG
916
917 Set C<LIBGUESTFS_DEBUG=1> to enable verbose messages.  This
918 has the same effect as calling C<guestfs_set_verbose (handle, 1)>.
919
920 =item LIBGUESTFS_KERNEL
921
922 Override the ordinary selection of appliance kernel.
923
924 =item LIBGUESTFS_MEMSIZE
925
926 Set the memory allocated to the qemu process, in megabytes.  For
927 example:
928
929  LIBGUESTFS_MEMSIZE=700
930
931 =item LIBGUESTFS_PATH
932
933 Set the path that libguestfs uses to search for kernel and initrd.img.
934 See the discussion of paths in section PATH above.
935
936 =item LIBGUESTFS_QEMU
937
938 Set the default qemu binary that libguestfs uses.  If not set, then
939 the qemu which was found at compile time by the configure script is
940 used.
941
942 See also L<QEMU WRAPPERS> above.
943
944 =item TMPDIR
945
946 Location of temporary directory, defaults to C</tmp>.
947
948 If libguestfs was compiled to use the supermin appliance then each
949 handle will require rather a large amount of space in this directory
950 for short periods of time (~ 80 MB).  You can use C<$TMPDIR> to
951 configure another directory to use in case C</tmp> is not large
952 enough.
953
954 =back
955
956 =head1 SEE ALSO
957
958 L<guestfish(1)>,
959 L<qemu(1)>,
960 L<febootstrap(1)>,
961 L<http://libguestfs.org/>.
962
963 =head1 BUGS
964
965 To get a list of bugs against libguestfs use this link:
966
967 L<https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools>
968
969 To report a new bug against libguestfs use this link:
970
971 L<https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools>
972
973 When reporting a bug, please check:
974
975 =over 4
976
977 =item *
978
979 That the bug hasn't been reported already.
980
981 =item *
982
983 That you are testing a recent version.
984
985 =item *
986
987 Describe the bug accurately, and give a way to reproduce it.
988
989 =back
990
991 =head1 AUTHORS
992
993 Richard W.M. Jones (C<rjones at redhat dot com>)
994
995 =head1 COPYRIGHT
996
997 Copyright (C) 2009 Red Hat Inc.
998 L<http://libguestfs.org/>
999
1000 This library is free software; you can redistribute it and/or
1001 modify it under the terms of the GNU Lesser General Public
1002 License as published by the Free Software Foundation; either
1003 version 2 of the License, or (at your option) any later version.
1004
1005 This library is distributed in the hope that it will be useful,
1006 but WITHOUT ANY WARRANTY; without even the implied warranty of
1007 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1008 Lesser General Public License for more details.
1009
1010 You should have received a copy of the GNU Lesser General Public
1011 License along with this library; if not, write to the Free Software
1012 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA