=encoding utf8 =head1 NAME guestfs - Library for accessing and modifying virtual machine images =head1 SYNOPSIS #include guestfs_h *g = guestfs_create (); guestfs_add_drive (g, "guest.img"); guestfs_launch (g); guestfs_mount (g, "/dev/sda1", "/"); guestfs_touch (g, "/hello"); guestfs_umount (g, "/"); guestfs_shutdown (g); guestfs_close (g); cc prog.c -o prog -lguestfs or: cc prog.c -o prog `pkg-config libguestfs --cflags --libs` =head1 DESCRIPTION Libguestfs is a library for accessing and modifying disk images and virtual machines. This manual page documents the C API. If you are looking for an introduction to libguestfs, see the web site: L Each virt tool has its own man page (for a full list, go to L at the end of this file). The libguestfs FAQ contains many useful answers: L. For examples of using the API from C, see L. For examples in other languages, see L below. For tips and recipes, see L. If you are having performance problems, read L. To help test libguestfs, read L and L. =head1 API OVERVIEW This section provides a gentler overview of the libguestfs API. We also try to group API calls together, where that may not be obvious from reading about the individual calls in the main section of this manual. =head2 HANDLES Before you can use libguestfs calls, you have to create a handle. Then you must add at least one disk image to the handle, followed by launching the handle, then performing whatever operations you want, and finally closing the handle. By convention we use the single letter C for the name of the handle variable, although of course you can use any name you want. The general structure of all libguestfs-using programs looks like this: guestfs_h *g = guestfs_create (); /* Call guestfs_add_drive additional times if there are * multiple disk images. */ guestfs_add_drive (g, "guest.img"); /* Most manipulation calls won't work until you've launched * the handle 'g'. You have to do this _after_ adding drives * and _before_ other commands. */ guestfs_launch (g); /* Now you can examine what partitions, LVs etc are available. */ char **partitions = guestfs_list_partitions (g); char **logvols = guestfs_lvs (g); /* To access a filesystem in the image, you must mount it. */ guestfs_mount (g, "/dev/sda1", "/"); /* Now you can perform filesystem actions on the guest * disk image. */ guestfs_touch (g, "/hello"); /* Synchronize the disk. This is the opposite of guestfs_launch. */ guestfs_shutdown (g); /* Close and free the handle 'g'. */ guestfs_close (g); The code above doesn't include any error checking. In real code you should check return values carefully for errors. In general all functions that return integers return C<-1> on error, and all functions that return pointers return C on error. See section L below for how to handle errors, and consult the documentation for each function call below to see precisely how they return error indications. See L for fully worked examples. =head2 DISK IMAGES The image filename (C<"guest.img"> in the example above) could be a disk image from a virtual machine, a L copy of a physical hard disk, an actual block device, or simply an empty file of zeroes that you have created through L. Libguestfs lets you do useful things to all of these. The call you should use in modern code for adding drives is L. To add a disk image, allowing writes, and specifying that the format is raw, do: guestfs_add_drive_opts (g, filename, GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", -1); You can add a disk read-only using: guestfs_add_drive_opts (g, filename, GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", GUESTFS_ADD_DRIVE_OPTS_READONLY, 1, -1); or by calling the older function L. In either case libguestfs won't modify the file. (See also L below). Be extremely cautious if the disk image is in use, eg. if it is being used by a virtual machine. Adding it read-write will almost certainly cause disk corruption, but adding it read-only is safe. You must add at least one disk image, and you may add multiple disk images. In the API, the disk images are usually referred to as C (for the first one you added), C (for the second one you added), etc. Once L has been called you cannot add any more images. You can call L to get a list of the device names, in the order that you added them. See also L below. There are slightly different rules when hotplugging disks (in libguestfs E 1.20). See L below. =head2 MOUNTING Before you can read or write files, create directories and so on in a disk image that contains filesystems, you have to mount those filesystems using L or L. If you already know that a disk image contains (for example) one partition with a filesystem on that partition, then you can mount it directly: guestfs_mount (g, "/dev/sda1", "/"); where C means literally the first partition (C<1>) of the first disk image that we added (C). If the disk contains Linux LVM2 logical volumes you could refer to those instead (eg. C). Note that these are libguestfs virtual devices, and are nothing to do with host devices. If you are given a disk image and you don't know what it contains then you have to find out. Libguestfs can do that too: use L and L to list possible partitions and LVs, and either try mounting each to see what is mountable, or else examine them with L or L. To list just filesystems, use L. Libguestfs also has a set of APIs for inspection of unknown disk images (see L below). But you might find it easier to look at higher level programs built on top of libguestfs, in particular L. To mount a filesystem read-only, use L. There are several other variations of the C call. =head2 FILESYSTEM ACCESS AND MODIFICATION The majority of the libguestfs API consists of fairly low-level calls for accessing and modifying the files, directories, symlinks etc on mounted filesystems. There are over a hundred such calls which you can find listed in detail below in this man page, and we don't even pretend to cover them all in this overview. Specify filenames as full paths, starting with C<"/"> and including the mount point. For example, if you mounted a filesystem at C<"/"> and you want to read the file called C<"etc/passwd"> then you could do: char *data = guestfs_cat (g, "/etc/passwd"); This would return C as a newly allocated buffer containing the full content of that file (with some conditions: see also L below), or C if there was an error. As another example, to create a top-level directory on that filesystem called C<"var"> you would do: guestfs_mkdir (g, "/var"); To create a symlink you could do: guestfs_ln_s (g, "/etc/init.d/portmap", "/etc/rc3.d/S30portmap"); Libguestfs will reject attempts to use relative paths and there is no concept of a current working directory. Libguestfs can return errors in many situations: for example if the filesystem isn't writable, or if a file or directory that you requested doesn't exist. If you are using the C API (documented here) you have to check for those error conditions after each call. (Other language bindings turn these errors into exceptions). File writes are affected by the per-handle umask, set by calling L and defaulting to 022. See L. Since libguestfs 1.18, it is possible to mount the libguestfs filesystem on a local directory, subject to some restrictions. See L below. =head2 PARTITIONING Libguestfs contains API calls to read, create and modify partition tables on disk images. In the common case where you want to create a single partition covering the whole disk, you should use the L call: const char *parttype = "mbr"; if (disk_is_larger_than_2TB) parttype = "gpt"; guestfs_part_disk (g, "/dev/sda", parttype); Obviously this effectively wipes anything that was on that disk image before. =head2 LVM2 Libguestfs provides access to a large part of the LVM2 API, such as L and L. It won't make much sense unless you familiarize yourself with the concepts of physical volumes, volume groups and logical volumes. This author strongly recommends reading the LVM HOWTO, online at L. =head2 DOWNLOADING Use L to download small, text only files. This call cannot handle files contain any ASCII NUL (C<\0>) characters. However the API is very simple to use. L can be used to read files which contain arbitrary 8 bit data, since it returns a (pointer, size) pair. L can be used to download any file, with no limits on content or size. To download multiple files, see L and L. =head2 UPLOADING It's often the case that you want to write a file or files to the disk image. To write a small file with fixed content, use L. To create a file of all zeroes, use L (sparse) or L (with all disk blocks allocated). There are a variety of other functions for creating test files, for example L and L. To upload a single file, use L. This call has no limits on file content or size. To upload multiple files, see L and L. However the fastest way to upload I is to turn them into a squashfs or CD ISO (see L and L), then attach this using L. If you add the drive in a predictable way (eg. adding it last after all other drives) then you can get the device name from L and mount it directly using L. Note that squashfs images are sometimes non-portable between kernel versions, and they don't support labels or UUIDs. If you want to pre-build an image or you need to mount it using a label or UUID, use an ISO image instead. =head2 COPYING There are various different commands for copying between files and devices and in and out of the guest filesystem. These are summarised in the table below. =over 4 =item B to B Use L to copy a single file, or L to copy directories recursively. To copy part of a file (offset and size) use L. =item B to B =item B to B =item B to B Use L, L, or L. Example: duplicate the contents of an LV: guestfs_copy_device_to_device (g, "/dev/VG/Original", "/dev/VG/Copy", /* -1 marks the end of the list of optional parameters */ -1); The destination (C) must be at least as large as the source (C). To copy less than the whole source device, use the optional C parameter: guestfs_copy_device_to_device (g, "/dev/VG/Original", "/dev/VG/Copy", GUESTFS_COPY_DEVICE_TO_DEVICE_SIZE, 10000, -1); =item B to B Use L. See L above. =item B to B Use L. See L above. =back =head2 UPLOADING AND DOWNLOADING TO PIPES AND FILE DESCRIPTORS Calls like L, L, L, L etc appear to only take filenames as arguments, so it appears you can only upload and download to files. However many Un*x-like hosts let you use the special device files C, C, C and C to read and write from stdin, stdout, stderr, and arbitrary file descriptor N. For example, L writes its output to stdout by doing: guestfs_download (g, filename, "/dev/stdout"); and you can write tar output to a file descriptor C by doing: char devfd[64]; snprintf (devfd, sizeof devfd, "/dev/fd/%d", fd); guestfs_tar_out (g, "/", devfd); =head2 LISTING FILES L is just designed for humans to read (mainly when using the L-equivalent command C). L is a quick way to get a list of files in a directory from programs, as a flat list of strings. L is a programmatic way to get a list of files in a directory, plus additional information about each one. It is more equivalent to using the L call on a local filesystem. L and L can be used to recursively list files. =head2 RUNNING COMMANDS Although libguestfs is primarily an API for manipulating files inside guest images, we also provide some limited facilities for running commands inside guests. There are many limitations to this: =over 4 =item * The kernel version that the command runs under will be different from what it expects. =item * If the command needs to communicate with daemons, then most likely they won't be running. =item * The command will be running in limited memory. =item * The network may not be available unless you enable it (see L). =item * Only supports Linux guests (not Windows, BSD, etc). =item * Architecture limitations (eg. won't work for a PPC guest on an X86 host). =item * For SELinux guests, you may need to enable SELinux and load policy first. See L in this manpage. =item * I It is not safe to run commands from untrusted, possibly malicious guests. These commands may attempt to exploit your program by sending unexpected output. They could also try to exploit the Linux kernel or qemu provided by the libguestfs appliance. They could use the network provided by the libguestfs appliance to bypass ordinary network partitions and firewalls. They could use the elevated privileges or different SELinux context of your program to their advantage. A secure alternative is to use libguestfs to install a "firstboot" script (a script which runs when the guest next boots normally), and to have this script run the commands you want in the normal context of the running guest, network security and so on. For information about other security issues, see L. =back The two main API calls to run commands are L and L (there are also variations). The difference is that L runs commands using the shell, so any shell globs, redirections, etc will work. =head2 CONFIGURATION FILES To read and write configuration files in Linux guest filesystems, we strongly recommend using Augeas. For example, Augeas understands how to read and write, say, a Linux shadow password file or X.org configuration file, and so avoids you having to write that code. The main Augeas calls are bound through the C APIs. We don't document Augeas itself here because there is excellent documentation on the L website. If you don't want to use Augeas (you fool!) then try calling L to get the file as a list of lines which you can iterate over. =head2 SELINUX We support SELinux guests. To ensure that labeling happens correctly in SELinux guests, you need to enable SELinux and load the guest's policy: =over 4 =item 1. Before launching, do: guestfs_set_selinux (g, 1); =item 2. After mounting the guest's filesystem(s), load the policy. This is best done by running the L command in the guest itself: guestfs_sh (g, "/usr/sbin/load_policy"); (Older versions of C require you to specify the name of the policy file). =item 3. Optionally, set the security context for the API. The correct security context to use can only be known by inspecting the guest. As an example: guestfs_setcon (g, "unconfined_u:unconfined_r:unconfined_t:s0"); =back This will work for running commands and editing existing files. When new files are created, you may need to label them explicitly, for example by running the external command C. =head2 UMASK Certain calls are affected by the current file mode creation mask (the "umask"). In particular ones which create files or directories, such as L, L or L. This affects either the default mode that the file is created with or modifies the mode that you supply. The default umask is C<022>, so files are created with modes such as C<0644> and directories with C<0755>. There are two ways to avoid being affected by umask. Either set umask to 0 (call C early after launching). Or call L after creating each file or directory. For more information about umask, see L. =head2 ENCRYPTED DISKS Libguestfs allows you to access Linux guests which have been encrypted using whole disk encryption that conforms to the Linux Unified Key Setup (LUKS) standard. This includes nearly all whole disk encryption systems used by modern Linux guests. Use L to identify LUKS-encrypted block devices (it returns the string C). Then open these devices by calling L. Obviously you will require the passphrase! Opening a LUKS device creates a new device mapper device called C (where C is the string you supply to L). Reads and writes to this mapper device are decrypted from and encrypted to the underlying block device respectively. LVM volume groups on the device can be made visible by calling L followed by L. The logical volume(s) can now be mounted in the usual way. Use the reverse process to close a LUKS device. Unmount any logical volumes on it, deactivate the volume groups by caling C. Then close the mapper device by calling L on the C device (I the underlying encrypted block device). =head2 MOUNT LOCAL In libguestfs E 1.18, it is possible to mount the libguestfs filesystem on a local directory and access it using ordinary POSIX calls and programs. Availability of this is subject to a number of restrictions: it requires FUSE (the Filesystem in USErspace), and libfuse must also have been available when libguestfs was compiled. FUSE may require that a kernel module is loaded, and it may be necessary to add the current user to a special C group. See the documentation for your distribution and L for further information. The call to mount the libguestfs filesystem on a local directory is L (q.v.) followed by L. The latter does not return until you unmount the filesystem. The reason is that the call enters the FUSE main loop and processes kernel requests, turning them into libguestfs calls. An alternative design would have been to create a background thread to do this, but libguestfs doesn't require pthreads. This way is also more flexible: for example the user can create another thread for L. L needs a certain amount of time to set up the mountpoint. The mountpoint is not ready to use until the call returns. At this point, accesses to the filesystem will block until the main loop is entered (ie. L). So if you need to start another process to access the filesystem, put the fork between L and L. =head3 MOUNT LOCAL COMPATIBILITY Since local mounting was only added in libguestfs 1.18, and may not be available even in these builds, you should consider writing code so that it doesn't depend on this feature, and can fall back to using libguestfs file system calls. If libguestfs was compiled without support for L then calling it will return an error with errno set to C (see L). =head3 MOUNT LOCAL PERFORMANCE Libguestfs on top of FUSE performs quite poorly. For best performance do not use it. Use ordinary libguestfs filesystem calls, upload, download etc. instead. =head2 HOTPLUGGING In libguestfs E 1.20, you may add drives and remove after calling L. There are some restrictions, see below. This is called I. Only a subset of the attach-method backends support hotplugging (currently only the libvirt attach-method has support). It also requires that you use libvirt E 0.10.3 and qemu E 1.2. To hot-add a disk, simply call L after L. It is mandatory to specify the C. =head2 MAXIMUM NUMBER OF PARTITIONS PER DISK Virtio limits the maximum number of partitions per disk to B<15>. This is because it reserves 4 bits for the minor device number (thus C, and C through C). If you attach a disk with more than 15 partitions, the extra partitions are ignored by libguestfs. =head2 MAXIMUM SIZE OF A DISK Probably the limit is between 2**63-1 and 2**64-1 bytes. We have tested block devices up to 1 exabyte (2**60 or 1,152,921,504,606,846,976 bytes) using sparse files backed by an XFS host filesystem. Although libguestfs probably does not impose any limit, the underlying host storage will. If you store disk images on a host ext4 filesystem, then the maximum size will be limited by the maximum ext4 file size (currently 16 TB). If you store disk images as host logical volumes then you are limited by the maximum size of an LV. For the hugest disk image files, we recommend using XFS on the host for storage. =head2 MAXIMUM SIZE OF A PARTITION The MBR (ie. classic MS-DOS) partitioning scheme uses 32 bit sector numbers. Assuming a 512 byte sector size, this means that MBR cannot address a partition located beyond 2 TB on the disk. It is recommended that you use GPT partitions on disks which are larger than this size. GPT uses 64 bit sector numbers and so can address partitions which are theoretically larger than the largest disk we could support. =head2 MAXIMUM SIZE OF A FILESYSTEM, FILES, DIRECTORIES This depends on the filesystem type. libguestfs itself does not impose any known limit. Consult Wikipedia or the filesystem documentation to find out what these limits are. =head2 MAXIMUM UPLOAD AND DOWNLOAD The API functions L, L, L, L and the like allow unlimited sized uploads and downloads. =head2 INSPECTION LIMITS The inspection code has several arbitrary limits on things like the size of Windows Registry hive it will read, and the length of product name. These are intended to stop a malicious guest from consuming arbitrary amounts of memory and disk space on the host, and should not be reached in practice. See the source code for more information. =head1 ENVIRONMENT VARIABLES =over 4 =item FEBOOTSTRAP_KERNEL =item FEBOOTSTRAP_MODULES These two environment variables allow the kernel that libguestfs uses in the appliance to be selected. If C<$FEBOOTSTRAP_KERNEL> is not set, then the most recent host kernel is chosen. For more information about kernel selection, see L. This feature is only available in febootstrap E 3.8. =item LIBGUESTFS_APPEND Pass additional options to the guest kernel. =item LIBGUESTFS_ATTACH_METHOD Choose the default way to create the appliance. See L. =item LIBGUESTFS_CACHEDIR The location where libguestfs will cache its appliance, when using a supermin appliance. The appliance is cached and shared between all handles which have the same effective user ID. If C is not set, then C is used. If C is not set, then C is used. See also L, L. =item LIBGUESTFS_DEBUG Set C to enable verbose messages. This has the same effect as calling C. =item LIBGUESTFS_MEMSIZE Set the memory allocated to the qemu process, in megabytes. For example: LIBGUESTFS_MEMSIZE=700 =item LIBGUESTFS_PATH Set the path that libguestfs uses to search for a supermin appliance. See the discussion of paths in section L above. =item LIBGUESTFS_QEMU Set the default qemu binary that libguestfs uses. If not set, then the qemu which was found at compile time by the configure script is used. See also L above. =item LIBGUESTFS_TMPDIR The location where libguestfs will store temporary files used by each handle. If C is not set, then C is used. If C is not set, then C is used. See also L, L. =item LIBGUESTFS_TRACE Set C to enable command traces. This has the same effect as calling C. =item TMPDIR See L, L. =back =head1 SEE ALSO L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L. Tools with a similar purpose: L, L, L, L, L. =head1 AUTHORS Richard W.M. Jones (C) =head1 COPYRIGHT Copyright (C) 2009-2012 Red Hat Inc.