Fix verbose packet dumping functions.
[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 L</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 L</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 L</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 =item *
320
321 For SELinux guests, you may need to enable SELinux and load policy
322 first.  See L</SELINUX> in this manpage.
323
324 =back
325
326 The two main API calls to run commands are C<guestfs_command> and
327 C<guestfs_sh> (there are also variations).
328
329 The difference is that C<guestfs_sh> runs commands using the shell, so
330 any shell globs, redirections, etc will work.
331
332 =head2 LISTING FILES
333
334 C<guestfs_ll> is just designed for humans to read (mainly when using
335 the L<guestfish(1)>-equivalent command C<ll>).
336
337 C<guestfs_ls> is a quick way to get a list of files in a directory
338 from programs.
339
340 C<guestfs_readdir> is a programmatic way to get a list of files in a
341 directory, plus additional information about each one.
342
343 C<guestfs_find> can be used to recursively list files.
344
345 =head2 SELINUX
346
347 We support SELinux guests.  To ensure that labeling happens correctly
348 in SELinux guests, you need to enable SELinux and load the guest's
349 policy:
350
351 =over 4
352
353 =item 1.
354
355 Before launching, do:
356
357  guestfs_set_selinux (g, 1);
358
359 =item 2.
360
361 After mounting the guest's filesystem(s), load the policy.  This
362 is best done by running the L<load_policy(8)> command in the
363 guest itself:
364
365  guestfs_sh (g, "/usr/sbin/load_policy");
366
367 (Older versions of C<load_policy> require you to specify the
368 name of the policy file).
369
370 =item 3.
371
372 Optionally, set the security context for the API.  The correct
373 security context to use can only be known by inspecting the
374 guest.  As an example:
375
376  guestfs_setcon (g, "unconfined_u:unconfined_r:unconfined_t:s0");
377
378 =back
379
380 This will work for running commands and editing existing files.
381
382 When new files are created, you may need to label them explicitly,
383 for example by running the external command
384 C<restorecon pathname>.
385
386 =head1 HIGH-LEVEL API ACTIONS
387
388 =head2 ABI GUARANTEE
389
390 We guarantee the libguestfs ABI (binary interface), for public,
391 high-level actions as outlined in this section.  Although we will
392 deprecate some actions, for example if they get replaced by newer
393 calls, we will keep the old actions forever.  This allows you the
394 developer to program in confidence against libguestfs.
395
396 @ACTIONS@
397
398 =head1 STRUCTURES
399
400 @STRUCTS@
401
402 =head1 STATE MACHINE AND LOW-LEVEL EVENT API
403
404 Internally, libguestfs is implemented by running a virtual machine
405 using L<qemu(1)>.  QEmu runs as a child process of the main program,
406 and most of this discussion won't make sense unless you understand
407 that the complexity is dealing with the (asynchronous) actions of the
408 child process.
409
410                             child process
411   ___________________       _________________________
412  /                   \     /                         \
413  | main program      |     | qemu +-----------------+|
414  |                   |     |      | Linux kernel    ||
415  +-------------------+     |      +-----------------+|
416  | libguestfs     <-------------->| guestfsd        ||
417  |                   |     |      +-----------------+|
418  \___________________/     \_________________________/
419
420 The diagram above shows libguestfs communicating with the guestfsd
421 daemon running inside the qemu child process.  There are several
422 points of failure here: qemu can fail to start, the virtual machine
423 inside qemu can fail to boot, guestfsd can fail to start or not
424 establish communication, any component can start successfully but fail
425 asynchronously later, and so on.
426
427 =head2 STATE MACHINE
428
429 libguestfs uses a state machine to model the child process:
430
431                          |
432                     guestfs_create
433                          |
434                          |
435                      ____V_____
436                     /          \
437                     |  CONFIG  |
438                     \__________/
439                      ^ ^   ^  \
440                     /  |    \  \ guestfs_launch
441                    /   |    _\__V______
442                   /    |   /           \
443                  /     |   | LAUNCHING |
444                 /      |   \___________/
445                /       |       /
446               /        |  guestfs_wait_ready
447              /         |     /
448     ______  /        __|____V
449    /      \ ------> /        \
450    | BUSY |         | READY  |
451    \______/ <------ \________/
452
453 The normal transitions are (1) CONFIG (when the handle is created, but
454 there is no child process), (2) LAUNCHING (when the child process is
455 booting up), (3) alternating between READY and BUSY as commands are
456 issued to, and carried out by, the child process.
457
458 The guest may be killed by C<guestfs_kill_subprocess>, or may die
459 asynchronously at any time (eg. due to some internal error), and that
460 causes the state to transition back to CONFIG.
461
462 Configuration commands for qemu such as C<guestfs_add_drive> can only
463 be issued when in the CONFIG state.
464
465 The high-level API offers two calls that go from CONFIG through
466 LAUNCHING to READY.  C<guestfs_launch> is a non-blocking call that
467 starts up the child process, immediately moving from CONFIG to
468 LAUNCHING.  C<guestfs_wait_ready> blocks until the child process is
469 READY to accept commands (or until some failure or timeout).  The
470 low-level event API described below provides a non-blocking way to
471 replace C<guestfs_wait_ready>.
472
473 High-level API actions such as C<guestfs_mount> can only be issued
474 when in the READY state.  These high-level API calls block waiting for
475 the command to be carried out (ie. the state to transition to BUSY and
476 then back to READY).  But using the low-level event API, you get
477 non-blocking versions.  (But you can still only carry out one
478 operation per handle at a time - that is a limitation of the
479 communications protocol we use).
480
481 Finally, the child process sends asynchronous messages back to the
482 main program, such as kernel log messages.  Mostly these are ignored
483 by the high-level API, but using the low-level event API you can
484 register to receive these messages.
485
486 =head2 SETTING CALLBACKS TO HANDLE EVENTS
487
488 The child process generates events in some situations.  Current events
489 include: receiving a log message, the child process exits.
490
491 Use the C<guestfs_set_*_callback> functions to set a callback for
492 different types of events.
493
494 Only I<one callback of each type> can be registered for each handle.
495 Calling C<guestfs_set_*_callback> again overwrites the previous
496 callback of that type.  Cancel all callbacks of this type by calling
497 this function with C<cb> set to C<NULL>.
498
499 =head2 guestfs_set_log_message_callback
500
501  typedef void (*guestfs_log_message_cb) (guestfs_h *g, void *opaque,
502                                          char *buf, int len);
503  void guestfs_set_log_message_callback (guestfs_h *handle,
504                                         guestfs_log_message_cb cb,
505                                         void *opaque);
506
507 The callback function C<cb> will be called whenever qemu or the guest
508 writes anything to the console.
509
510 Use this function to capture kernel messages and similar.
511
512 Normally there is no log message handler, and log messages are just
513 discarded.
514
515 =head2 guestfs_set_subprocess_quit_callback
516
517  typedef void (*guestfs_subprocess_quit_cb) (guestfs_h *g, void *opaque);
518  void guestfs_set_subprocess_quit_callback (guestfs_h *handle,
519                                             guestfs_subprocess_quit_cb cb,
520                                             void *opaque);
521
522 The callback function C<cb> will be called when the child process
523 quits, either asynchronously or if killed by
524 C<guestfs_kill_subprocess>.  (This corresponds to a transition from
525 any state to the CONFIG state).
526
527 =head2 guestfs_set_launch_done_callback
528
529  typedef void (*guestfs_launch_done_cb) (guestfs_h *g, void *opaque);
530  void guestfs_set_launch_done_callback (guestfs_h *handle,
531                                         guestfs_ready_cb cb,
532                                         void *opaque);
533
534 The callback function C<cb> will be called when the child process
535 becomes ready first time after it has been launched.  (This
536 corresponds to a transition from LAUNCHING to the READY state).
537
538 You can use this instead of C<guestfs_wait_ready> to implement a
539 non-blocking wait for the child process to finish booting up.
540
541 =head1 BLOCK DEVICE NAMING
542
543 In the kernel there is now quite a profusion of schemata for naming
544 block devices (in this context, by I<block device> I mean a physical
545 or virtual hard drive).  The original Linux IDE driver used names
546 starting with C</dev/hd*>.  SCSI devices have historically used a
547 different naming scheme, C</dev/sd*>.  When the Linux kernel I<libata>
548 driver became a popular replacement for the old IDE driver
549 (particularly for SATA devices) those devices also used the
550 C</dev/sd*> scheme.  Additionally we now have virtual machines with
551 paravirtualized drivers.  This has created several different naming
552 systems, such as C</dev/vd*> for virtio disks and C</dev/xvd*> for Xen
553 PV disks.
554
555 As discussed above, libguestfs uses a qemu appliance running an
556 embedded Linux kernel to access block devices.  We can run a variety
557 of appliances based on a variety of Linux kernels.
558
559 This causes a problem for libguestfs because many API calls use device
560 or partition names.  Working scripts and the recipe (example) scripts
561 that we make available over the internet could fail if the naming
562 scheme changes.
563
564 Therefore libguestfs defines C</dev/sd*> as the I<standard naming
565 scheme>.  Internally C</dev/sd*> names are translated, if necessary,
566 to other names as required.  For example, under RHEL 5 which uses the
567 C</dev/hd*> scheme, any device parameter C</dev/sda2> is translated to
568 C</dev/hda2> transparently.
569
570 Note that this I<only> applies to parameters.  The
571 C<guestfs_list_devices>, C<guestfs_list_partitions> and similar calls
572 return the true names of the devices and partitions as known to the
573 appliance.
574
575 =head2 ALGORITHM FOR BLOCK DEVICE NAME TRANSLATION
576
577 Usually this translation is transparent.  However in some (very rare)
578 cases you may need to know the exact algorithm.  Such cases include
579 where you use C<guestfs_config> to add a mixture of virtio and IDE
580 devices to the qemu-based appliance, so have a mixture of C</dev/sd*>
581 and C</dev/vd*> devices.
582
583 The algorithm is applied only to I<parameters> which are known to be
584 either device or partition names.  Return values from functions such
585 as C<guestfs_list_devices> are never changed.
586
587 =over 4
588
589 =item *
590
591 Is the string a parameter which is a device or partition name?
592
593 =item *
594
595 Does the string begin with C</dev/sd>?
596
597 =item *
598
599 Does the named device exist?  If so, we use that device.
600 However if I<not> then we continue with this algorithm.
601
602 =item *
603
604 Replace initial C</dev/sd> string with C</dev/hd>.
605
606 For example, change C</dev/sda2> to C</dev/hda2>.
607
608 If that named device exists, use it.  If not, continue.
609
610 =item *
611
612 Replace initial C</dev/sd> string with C</dev/vd>.
613
614 If that named device exists, use it.  If not, return an error.
615
616 =back
617
618 =head2 PORTABILITY CONCERNS
619
620 Although the standard naming scheme and automatic translation is
621 useful for simple programs and guestfish scripts, for larger programs
622 it is best not to rely on this mechanism.
623
624 Where possible for maximum future portability programs using
625 libguestfs should use these future-proof techniques:
626
627 =over 4
628
629 =item *
630
631 Use C<guestfs_list_devices> or C<guestfs_list_partitions> to list
632 actual device names, and then use those names directly.
633
634 Since those device names exist by definition, they will never be
635 translated.
636
637 =item *
638
639 Use higher level ways to identify filesystems, such as LVM names,
640 UUIDs and filesystem labels.
641
642 =back
643
644 =head1 INTERNALS
645
646 =head2 COMMUNICATION PROTOCOL
647
648 Don't rely on using this protocol directly.  This section documents
649 how it currently works, but it may change at any time.
650
651 The protocol used to talk between the library and the daemon running
652 inside the qemu virtual machine is a simple RPC mechanism built on top
653 of XDR (RFC 1014, RFC 1832, RFC 4506).
654
655 The detailed format of structures is in C<src/guestfs_protocol.x>
656 (note: this file is automatically generated).
657
658 There are two broad cases, ordinary functions that don't have any
659 C<FileIn> and C<FileOut> parameters, which are handled with very
660 simple request/reply messages.  Then there are functions that have any
661 C<FileIn> or C<FileOut> parameters, which use the same request and
662 reply messages, but they may also be followed by files sent using a
663 chunked encoding.
664
665 =head3 ORDINARY FUNCTIONS (NO FILEIN/FILEOUT PARAMS)
666
667 For ordinary functions, the request message is:
668
669  total length (header + arguments,
670       but not including the length word itself)
671  struct guestfs_message_header (encoded as XDR)
672  struct guestfs_<foo>_args (encoded as XDR)
673
674 The total length field allows the daemon to allocate a fixed size
675 buffer into which it slurps the rest of the message.  As a result, the
676 total length is limited to C<GUESTFS_MESSAGE_MAX> bytes (currently
677 4MB), which means the effective size of any request is limited to
678 somewhere under this size.
679
680 Note also that many functions don't take any arguments, in which case
681 the C<guestfs_I<foo>_args> is completely omitted.
682
683 The header contains the procedure number (C<guestfs_proc>) which is
684 how the receiver knows what type of args structure to expect, or none
685 at all.
686
687 The reply message for ordinary functions is:
688
689  total length (header + ret,
690       but not including the length word itself)
691  struct guestfs_message_header (encoded as XDR)
692  struct guestfs_<foo>_ret (encoded as XDR)
693
694 As above the C<guestfs_I<foo>_ret> structure may be completely omitted
695 for functions that return no formal return values.
696
697 As above the total length of the reply is limited to
698 C<GUESTFS_MESSAGE_MAX>.
699
700 In the case of an error, a flag is set in the header, and the reply
701 message is slightly changed:
702
703  total length (header + error,
704       but not including the length word itself)
705  struct guestfs_message_header (encoded as XDR)
706  struct guestfs_message_error (encoded as XDR)
707
708 The C<guestfs_message_error> structure contains the error message as a
709 string.
710
711 =head3 FUNCTIONS THAT HAVE FILEIN PARAMETERS
712
713 A C<FileIn> parameter indicates that we transfer a file I<into> the
714 guest.  The normal request message is sent (see above).  However this
715 is followed by a sequence of file chunks.
716
717  total length (header + arguments,
718       but not including the length word itself,
719       and not including the chunks)
720  struct guestfs_message_header (encoded as XDR)
721  struct guestfs_<foo>_args (encoded as XDR)
722  sequence of chunks for FileIn param #0
723  sequence of chunks for FileIn param #1 etc.
724
725 The "sequence of chunks" is:
726
727  length of chunk (not including length word itself)
728  struct guestfs_chunk (encoded as XDR)
729  length of chunk
730  struct guestfs_chunk (encoded as XDR)
731    ...
732  length of chunk
733  struct guestfs_chunk (with data.data_len == 0)
734
735 The final chunk has the C<data_len> field set to zero.  Additionally a
736 flag is set in the final chunk to indicate either successful
737 completion or early cancellation.
738
739 At time of writing there are no functions that have more than one
740 FileIn parameter.  However this is (theoretically) supported, by
741 sending the sequence of chunks for each FileIn parameter one after
742 another (from left to right).
743
744 Both the library (sender) I<and> the daemon (receiver) may cancel the
745 transfer.  The library does this by sending a chunk with a special
746 flag set to indicate cancellation.  When the daemon sees this, it
747 cancels the whole RPC, does I<not> send any reply, and goes back to
748 reading the next request.
749
750 The daemon may also cancel.  It does this by writing a special word
751 C<GUESTFS_CANCEL_FLAG> to the socket.  The library listens for this
752 during the transfer, and if it gets it, it will cancel the transfer
753 (it sends a cancel chunk).  The special word is chosen so that even if
754 cancellation happens right at the end of the transfer (after the
755 library has finished writing and has started listening for the reply),
756 the "spurious" cancel flag will not be confused with the reply
757 message.
758
759 This protocol allows the transfer of arbitrary sized files (no 32 bit
760 limit), and also files where the size is not known in advance
761 (eg. from pipes or sockets).  However the chunks are rather small
762 (C<GUESTFS_MAX_CHUNK_SIZE>), so that neither the library nor the
763 daemon need to keep much in memory.
764
765 =head3 FUNCTIONS THAT HAVE FILEOUT PARAMETERS
766
767 The protocol for FileOut parameters is exactly the same as for FileIn
768 parameters, but with the roles of daemon and library reversed.
769
770  total length (header + ret,
771       but not including the length word itself,
772       and not including the chunks)
773  struct guestfs_message_header (encoded as XDR)
774  struct guestfs_<foo>_ret (encoded as XDR)
775  sequence of chunks for FileOut param #0
776  sequence of chunks for FileOut param #1 etc.
777
778 =head3 INITIAL MESSAGE
779
780 Because the underlying channel (QEmu -net channel) doesn't have any
781 sort of connection control, when the daemon launches it sends an
782 initial word (C<GUESTFS_LAUNCH_FLAG>) which indicates that the guest
783 and daemon is alive.  This is what C<guestfs_wait_ready> waits for.
784
785 =head1 QEMU WRAPPERS
786
787 If you want to compile your own qemu, run qemu from a non-standard
788 location, or pass extra arguments to qemu, then you can write a
789 shell-script wrapper around qemu.
790
791 There is one important rule to remember: you I<must C<exec qemu>> as
792 the last command in the shell script (so that qemu replaces the shell
793 and becomes the direct child of the libguestfs-using program).  If you
794 don't do this, then the qemu process won't be cleaned up correctly.
795
796 Here is an example of a wrapper, where I have built my own copy of
797 qemu from source:
798
799  #!/bin/sh -
800  qemudir=/home/rjones/d/qemu
801  exec $qemudir/x86_64-softmmu/qemu-system-x86_64 -L $qemudir/pc-bios "$@"
802
803 Save this script as C</tmp/qemu.wrapper> (or wherever), C<chmod +x>,
804 and then use it by setting the LIBGUESTFS_QEMU environment variable.
805 For example:
806
807  LIBGUESTFS_QEMU=/tmp/qemu.wrapper guestfish
808
809 Note that libguestfs also calls qemu with the -help and -version
810 options in order to determine features.
811
812 =head1 ENVIRONMENT VARIABLES
813
814 =over 4
815
816 =item LIBGUESTFS_APPEND
817
818 Pass additional options to the guest kernel.
819
820 =item LIBGUESTFS_DEBUG
821
822 Set C<LIBGUESTFS_DEBUG=1> to enable verbose messages.  This
823 has the same effect as calling C<guestfs_set_verbose (handle, 1)>.
824
825 =item LIBGUESTFS_MEMSIZE
826
827 Set the memory allocated to the qemu process, in megabytes.  For
828 example:
829
830  LIBGUESTFS_MEMSIZE=700
831
832 =item LIBGUESTFS_PATH
833
834 Set the path that libguestfs uses to search for kernel and initrd.img.
835 See the discussion of paths in section PATH above.
836
837 =item LIBGUESTFS_QEMU
838
839 Set the default qemu binary that libguestfs uses.  If not set, then
840 the qemu which was found at compile time by the configure script is
841 used.
842
843 See also L</QEMU WRAPPERS> above.
844
845 =item LIBGUESTFS_TRACE
846
847 Set C<LIBGUESTFS_TRACE=1> to enable command traces.  This
848 has the same effect as calling C<guestfs_set_trace (handle, 1)>.
849
850 =item TMPDIR
851
852 Location of temporary directory, defaults to C</tmp>.
853
854 If libguestfs was compiled to use the supermin appliance then each
855 handle will require rather a large amount of space in this directory
856 for short periods of time (~ 80 MB).  You can use C<$TMPDIR> to
857 configure another directory to use in case C</tmp> is not large
858 enough.
859
860 =back
861
862 =head1 SEE ALSO
863
864 L<guestfish(1)>,
865 L<qemu(1)>,
866 L<febootstrap(1)>,
867 L<http://libguestfs.org/>.
868
869 Tools with a similar purpose:
870 L<fdisk(8)>,
871 L<parted(8)>,
872 L<kpartx(8)>,
873 L<lvm(8)>,
874 L<disktype(1)>.
875
876 =head1 BUGS
877
878 To get a list of bugs against libguestfs use this link:
879
880 L<https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools>
881
882 To report a new bug against libguestfs use this link:
883
884 L<https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools>
885
886 When reporting a bug, please check:
887
888 =over 4
889
890 =item *
891
892 That the bug hasn't been reported already.
893
894 =item *
895
896 That you are testing a recent version.
897
898 =item *
899
900 Describe the bug accurately, and give a way to reproduce it.
901
902 =item *
903
904 Run libguestfs-test-tool and paste the B<complete, unedited>
905 output into the bug report.
906
907 =back
908
909 =head1 AUTHORS
910
911 Richard W.M. Jones (C<rjones at redhat dot com>)
912
913 =head1 COPYRIGHT
914
915 Copyright (C) 2009 Red Hat Inc.
916 L<http://libguestfs.org/>
917
918 This library is free software; you can redistribute it and/or
919 modify it under the terms of the GNU Lesser General Public
920 License as published by the Free Software Foundation; either
921 version 2 of the License, or (at your option) any later version.
922
923 This library is distributed in the hope that it will be useful,
924 but WITHOUT ANY WARRANTY; without even the implied warranty of
925 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
926 Lesser General Public License for more details.
927
928 You should have received a copy of the GNU Lesser General Public
929 License along with this library; if not, write to the Free Software
930 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA