=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). Other libguestfs manual pages: =over 4 =item L Frequently Asked Questions (FAQ). =item L Examples of using the API from C. For examples in other languages, see L below. =item L Tips and recipes. =item L Performance tips and solutions. =item L =item L Help testing libguestfs. =item L How to build libguestfs from source. =item L Contribute code to libguestfs. =item L How libguestfs works. =item L Security information, including CVEs affecting libguestfs. =back =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); /* Either: examine what partitions, LVs etc are available: */ char **partitions = guestfs_list_partitions (g); char **logvols = guestfs_lvs (g); /* Or: ask libguestfs to find filesystems for you: */ char **filesystems = guestfs_list_filesystems (g); /* Or: use inspection (see INSPECTION section below). */ /* 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. The code above does not L the strings and arrays returned from functions. Consult the documentation for each function to find out how to free the return value. 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. If you use the readonly flag, 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 should usually add at least one disk image, and you may add multiple disk images. If adding multiple disk images, they usually have to be "related", ie. from the same guest. In the API, the disk images are usually referred to as F (for the first one you added), F (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. =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 F means literally the first partition (C<1>) of the first disk image that we added (F). If the disk contains Linux LVM2 logical volumes you could refer to those instead (eg. F). 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). You might also want 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 containing 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 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 (F) must be at least as large as the source (F). 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 F, F, F and F 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 relabel the guest after creating new files. See L below. =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 SYSTEMD JOURNAL FILES To read the systemd journal from a Linux guest, use the C APIs starting with L. Consult the journal documentation here: L, L. =head2 SELINUX We support SELinux guests. However it is not possible to load the SELinux policy of the guest into the appliance kernel. Therefore the strategy for dealing with SELinux guests is to relabel them after making changes. In libguestfs E 1.34 there is a new API, L, which can be used for this. To properly use this API you have to parse the guest SELinux configuration. See the L module F for how to do this. A simpler but slower alternative is to touch F in the guest, which means that the guest will relabel itself at next boot. Libguestfs E 1.32 had APIs C, C, C and C. These did not work properly, are deprecated, and should not be used in new code. =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 LABELS AND UUIDS Many filesystems, devices and logical volumes support either labels (short strings like "BOOT" which might not be unique) and/or UUIDs (globally unique IDs). For filesystems, use L or L to read the label or UUID. Some filesystems let you call L or L to change the label or UUID. You can locate a filesystem by its label or UUID using L or L. For LVM2 (which supports only UUIDs), there is a rich set of APIs for fetching UUIDs, fetching UUIDs of the contained objects, and changing UUIDs. See: L, L, L, L, L, L, L, L, L. Note when cloning a filesystem, device or whole guest, it is a good idea to set new randomly generated UUIDs on the copy. =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. Windows BitLocker is also supported. Use L to identify encrypted block devices. For LUKS it returns the string C. For Windows BitLocker it returns C. Then open these devices by calling L. Obviously you will require the passphrase! Passphrase-less unlocking is supported for LUKS (not BitLocker) block devices that have been encrypted with network-bound disk encryption (NBDE), using Clevis on the Linux guest side, and Tang on a separate Linux server. Open such devices with L. The appliance will need networking enabled (refer to L) and actual connectivity to the Tang servers noted in the C Clevis pins that are bound to the LUKS header. (This includes the ability to resolve the names of the Tang servers.) Opening an encrypted device creates a new device mapper device called F (where C is the string you supply to L or 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 an encrypted device. Unmount any logical volumes on it, deactivate the volume groups by calling C. Then close the mapper device by calling L on the F 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 REMOTE STORAGE =head3 CEPH Libguestfs can access Ceph (librbd/RBD) disks. To do this, set the optional C and C parameters of L like this: char **servers = { "ceph1.example.org:3000", /* ... */, NULL }; guestfs_add_drive_opts (g, "pool/image", GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, "rbd", GUESTFS_ADD_DRIVE_OPTS_SERVER, servers, GUESTFS_ADD_DRIVE_OPTS_USERNAME, "rbduser", GUESTFS_ADD_DRIVE_OPTS_SECRET, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==", -1); C (the C parameter) is a list of one or more Ceph servers. The server string is documented in L. The C and C parameters are also optional, and if not given, then no authentication will be used. An encrypted RBD disk -- I opening which would require the C and C parameters -- cannot be accessed if the following conditions all hold: =over 4 =item * the L is libvirt, =item * the image specified by the C parameter is different from the encrypted RBD disk, =item * the image specified by the C parameter has L, =item * the encrypted RBD disk is specified as a backing file at some level in the qcow2 backing chain. =back This limitation is due to libvirt's (justified) separate handling of disks vs. secrets. When the RBD username and secret are provided inside a qcow2 backing file specification, libvirt does not construct an ephemeral secret object from those, for Ceph authentication. Refer to L. =head3 FTP AND HTTP Libguestfs can access remote disks over FTP, FTPS, HTTP or HTTPS protocols. To do this, set the optional C and C parameters of L like this: char **servers = { "www.example.org", NULL }; guestfs_add_drive_opts (g, "/disk.img", GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, "http", GUESTFS_ADD_DRIVE_OPTS_SERVER, servers, -1); The C can be one of C<"ftp">, C<"ftps">, C<"http">, or C<"https">. C (the C parameter) is a list which must have a single element. The single element is a string defining the web or FTP server. The format of this string is documented in L. =head3 GLUSTER Glusterfs support was removed in libguestfs 1.54 (2024). =head3 ISCSI Libguestfs can access iSCSI disks remotely. To do this, set the optional C and C parameters like this: char **server = { "iscsi.example.org:3000", NULL }; guestfs_add_drive_opts (g, "target-iqn-name/lun", GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, "iscsi", GUESTFS_ADD_DRIVE_OPTS_SERVER, server, -1); The C parameter is a list which must have a single element. The single element is a string defining the iSCSI server. The format of this string is documented in L. =head3 NETWORK BLOCK DEVICE Libguestfs can access Network Block Device (NBD) disks remotely. To do this, set the optional C and C parameters of L like this: char **server = { "nbd.example.org:3000", NULL }; guestfs_add_drive_opts (g, "" /* export name - see below */, GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, "nbd", GUESTFS_ADD_DRIVE_OPTS_SERVER, server, -1); Notes: =over 4 =item * C is in fact a list of servers. For NBD you must always supply a list with a single element. (Other remote protocols require zero or more than one server, hence the requirement for this parameter to be a list). =item * The C string is documented in L. To connect to a local qemu-nbd instance over a Unix domain socket, use C<"unix:/path/to/socket">. =item * The C parameter is the NBD export name. Use an empty string to mean the default export. Many NBD servers, including qemu-nbd, do not support export names. =item * If using qemu-nbd as your server, you should always specify the C<-t> option. The reason is that libguestfs may open several connections to the server. =item * The libvirt backend requires that you set the C parameter of L accurately when you use writable NBD disks. =item * The libvirt backend has a bug that stops Unix domain socket connections from working: L =item * The direct backend does not support readonly connections because of a bug in qemu: L =back =head3 SHEEPDOG Sheepdog support was removed in libguestfs 1.54 (2024). =head3 SSH Libguestfs can access disks over a Secure Shell (SSH) connection. To do this, set the C and C and (optionally) C parameters of L like this: char **server = { "remote.example.com", NULL }; guestfs_add_drive_opts (g, "/path/to/disk.img", GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", GUESTFS_ADD_DRIVE_OPTS_PROTOCOL, "ssh", GUESTFS_ADD_DRIVE_OPTS_SERVER, server, GUESTFS_ADD_DRIVE_OPTS_USERNAME, "remoteuser", -1); The format of the server string is documented in L. =head2 INSPECTION Libguestfs has APIs for inspecting an unknown disk image to find out if it contains operating systems, an install CD or a live CD. Add all disks belonging to the unknown virtual machine and call L in the usual way. Then call L. This function uses other libguestfs calls and certain heuristics, and returns a list of operating systems that were found. An empty list means none were found. A single element is the root filesystem of the operating system. For dual- or multi-boot guests, multiple roots can be returned, each one corresponding to a separate operating system. (Multi-boot virtual machines are extremely rare in the world of virtualization, but since this scenario can happen, we have built libguestfs to deal with it.) For each root, you can then call various C functions to get additional details about that operating system. For example, call L to return the string C or C for Windows and Linux-based operating systems respectively. Un*x-like and Linux-based operating systems usually consist of several filesystems which are mounted at boot time (for example, a separate boot partition mounted on F). The inspection rules are able to detect how filesystems correspond to mount points. Call C to get this mapping. It might return a hash table like this example: /boot => /dev/sda1 / => /dev/vg_guest/lv_root /usr => /dev/vg_guest/lv_usr The caller can then make calls to L to mount the filesystems as suggested. Be careful to mount filesystems in the right order (eg. F before F). Sorting the keys of the hash by length, shortest first, should work. Inspection currently only works for some common operating systems. Contributors are welcome to send patches for other operating systems that we currently cannot detect. Encrypted disks must be opened before inspection. See L for more details. The L function just ignores any encrypted devices. A note on the implementation: The call L performs inspection and caches the results in the guest handle. Subsequent calls to C return this cached information, but I re-read the disks. If you change the content of the guest disks, you can redo inspection by calling L again. (L works a little differently from the other calls and does read the disks. See documentation for that function for details). =head3 INSPECTING INSTALL DISKS Libguestfs (since 1.9.4) can detect some install disks, install CDs, live CDs and more. Further information is available about the operating system that can be installed using the regular inspection APIs like L, L etc. =head2 SPECIAL CONSIDERATIONS FOR WINDOWS GUESTS Libguestfs can mount NTFS partitions. It does this using the L driver. =head3 DRIVE LETTERS AND PATHS DOS and Windows still use drive letters, and the filesystems are always treated as case insensitive by Windows itself, and therefore you might find a Windows configuration file referring to a path like C. When the filesystem is mounted in libguestfs, that directory might be referred to as F. Drive letter mappings can be found using inspection (see L and L) Dealing with separator characters (backslash vs forward slash) is outside the scope of libguestfs, but usually a simple character replacement will work. To resolve the case insensitivity of paths, call L. =head3 LONG FILENAMES ON NTFS NTFS supports filenames up to 255 characters long. "Character" means a 2 byte UTF-16 codepoint which can encode the most common Unicode codepoints. Most Linux filesystems support filenames up to 255 I. This means you may get an error: File name too long when you copy a file from NTFS to a Linux filesystem if the name, when reencoded as UTF-8, would exceed 255 bytes in length. This will most often happen when using non-ASCII names that are longer than ~127 characters (eg. Greek, Cyrillic) or longer than ~85 characters (Asian languages). A workaround is not to try to store such long filenames on Linux native filesystems. Since the L format can store unlimited length filenames, keep the files in a tarball. =head3 ACCESSING THE WINDOWS REGISTRY Libguestfs also provides some help for decoding Windows Registry "hive" files, through a separate C library called L. Before libguestfs 1.19.35 you had to download the hive file, operate on it locally using hivex, and upload it again. Since this version, we have included the major hivex APIs directly in the libguestfs API (see L). This means that if you have opened a Windows guest, you can read and write the registry directly. See also L. =head3 SYMLINKS ON NTFS-3G FILESYSTEMS Ntfs-3g tries to rewrite "Junction Points" and NTFS "symbolic links" to provide something which looks like a Linux symlink. The way it tries to do the rewriting is described here: L The essential problem is that ntfs-3g simply does not have enough information to do a correct job. NTFS links can contain drive letters and references to external device GUIDs that ntfs-3g has no way of resolving. It is almost certainly the case that libguestfs callers should ignore what ntfs-3g does (ie. don't use L on NTFS volumes). Instead if you encounter a symbolic link on an ntfs-3g filesystem, use L to read the C extended attribute, and read the raw reparse data from that (you can find the format documented in various places around the web). =head3 EXTENDED ATTRIBUTES ON NTFS-3G FILESYSTEMS There are other useful extended attributes that can be read from ntfs-3g filesystems (using L). See: L =head3 WINDOWS HIBERNATION AND WINDOWS 8 FAST STARTUP Windows guests which have been hibernated (instead of fully shut down) cannot be mounted. This is a limitation of ntfs-3g. You will see an error like this: The disk contains an unclean file system (0, 0). Metadata kept in Windows cache, refused to mount. Failed to mount '/dev/sda2': Operation not permitted The NTFS partition is in an unsafe state. Please resume and shutdown Windows fully (no hibernation or fast restarting), or mount the volume read-only with the 'ro' mount option. In Windows 8, the shutdown button does not shut down the guest at all. Instead it usually hibernates the guest. This is known as "fast startup". Some suggested workarounds are: =over 4 =item * Mount read-only (eg. L). =item * On Windows 8, turn off fast startup. It is in the Control Panel → Power Options → Choose what the power buttons do → Change settings that are currently unavailable → Turn on fast startup. =item * On Windows 7 and earlier, shut the guest off properly instead of hibernating it. =back =head2 RESIZE2FS ERRORS The L, L and L calls are used to resize ext2/3/4 filesystems. The underlying program (L) requires that the filesystem is clean and recently fsck'd before you can resize it. Also, if the resize operation fails for some reason, then you had to call fsck the filesystem again to fix it. In libguestfs C 1.17.14, you usually had to call L before the resize. However, in C 1.17.14, L is called automatically before the resize, so you no longer need to do this. The L program can still fail, in which case it prints an error message similar to: Please run 'e2fsck -fy ' to fix the filesystem after the aborted resize operation. You can do this by calling L with the C option. However in the context of disk images, it is usually better to avoid this situation, eg. by rolling back to an earlier snapshot, or by copying and resizing and on failure going back to the original. =head2 USING LIBGUESTFS WITH OTHER PROGRAMMING LANGUAGES Although we don’t want to discourage you from using the C API, we will mention here that the same API is also available in other languages. The API is broadly identical in all supported languages. This means that the C call C is C<$g-Eadd_drive_ro($file)> in Perl, C in Python, and C in OCaml. In other words, a straightforward, predictable isomorphism between each language. Error messages are automatically transformed into exceptions if the language supports it. We don’t try to "object orientify" parts of the API in OO languages, although contributors are welcome to write higher level APIs above what we provide in their favourite languages if they wish. =over 4 =item B You can use the I header file from C++ programs. The C++ API is identical to the C API. C++ classes and exceptions are not used. =item B The C# bindings are highly experimental. Please read the warnings at the top of F. =item B See L. =item B See L. =item B This language binding is working but incomplete: =over 4 =item * Functions with optional arguments are not bound. Implementing optional arguments in Haskell seems to be very complex. =item * Events are not bound. =item * Functions with the following return types are not bound: =over 4 =item * Any function returning a struct. =item * Any function returning a list of structs. =item * A few functions that return fixed length buffers (specifically ones declared C in the generator). =item * A tiny number of obscure functions that return constant strings (specifically ones declared C in the generator). =back =back =item B Full documentation is contained in the Javadoc which is distributed with libguestfs. For examples, see L. =item B See L. =item B See L. =item B See L and L. =item B For documentation see C supplied with libguestfs sources or in the php-libguestfs package for your distribution. The PHP binding only works correctly on 64 bit machines. =item B See L. =item B See L. For JRuby, use the Java bindings. =item B See L. =back =head2 LIBGUESTFS GOTCHAS L: "A feature of a system [...] that works in the way it is documented but is counterintuitive and almost invites mistakes." Since we developed libguestfs and the associated tools, there are several things we would have designed differently, but are now stuck with for backwards compatibility or other reasons. If there is ever a libguestfs 2.0 release, you can expect these to change. Beware of them. =over 4 =item Read-only should be the default. In L, I<--ro> should be the default, and you should have to specify I<--rw> if you want to make changes to the image. This would reduce the potential to corrupt live VM images. Note that many filesystems change the disk when you just mount and unmount, even if you didn't perform any writes. You need to use L to guarantee that the disk is not changed. =item guestfish command line is hard to use. F doesn't do what people expect (open F for examination). It tries to run a guestfish command F which doesn't exist, so it fails. In earlier versions of guestfish the error message was also unintuitive, but we have corrected this since. Like the Bourne shell, we should have used C to run commands. =item guestfish megabyte modifiers don’t work right on all commands In recent guestfish you can use C<1M> to mean 1 megabyte (and similarly for other modifiers). What guestfish actually does is to multiply the number part by the modifier part and pass the result to the C API. However this doesn't work for a few APIs which aren't expecting bytes, but are already expecting some other unit (eg. megabytes). The most common is L. The guestfish command: lvcreate LV VG 100M does not do what you might expect. Instead because L is already expecting megabytes, this tries to create a 100 I (100 megabytes * megabytes) logical volume. The error message you get from this is also a little obscure. This could be fixed in the generator by specially marking parameters and return values which take bytes or other units. =item Ambiguity between devices and paths There is a subtle ambiguity in the API between a device name (eg. F) and a similar pathname. A file might just happen to be called C in the directory F (consider some non-Unix VM image). In the current API we usually resolve this ambiguity by having two separate calls, for example L and L. Some API calls are ambiguous and (incorrectly) resolve the problem by detecting if the path supplied begins with F. To avoid both the ambiguity and the need to duplicate some calls, we could make paths/devices into structured names. One way to do this would be to use a notation like grub (C), although nobody really likes this aspect of grub. Another way would be to use a structured type, equivalent to this OCaml type: type path = Path of string | Device of int | Partition of int * int which would allow you to pass arguments like: Path "/foo/bar" Device 1 (* /dev/sdb, or perhaps /dev/sda *) Partition (1, 2) (* /dev/sdb2 (or is it /dev/sda2 or /dev/sdb3?) *) Path "/dev/sdb2" (* not a device *) As you can see there are still problems to resolve even with this representation. Also consider how it might work in guestfish. =back =head2 KEYS AND PASSPHRASES Certain libguestfs calls take a parameter that contains sensitive key material, passed in as a C string. In the future we would hope to change the libguestfs implementation so that keys are L-ed into physical RAM, and thus can never end up in swap. However this is I done at the moment, because of the complexity of such an implementation. Therefore you should be aware that any key parameter you pass to libguestfs might end up being written out to the swap partition. If this is a concern, scrub the swap partition or don't use libguestfs on encrypted devices. =head2 MULTIPLE HANDLES AND MULTIPLE THREADS All high-level libguestfs actions are synchronous. If you want to use libguestfs asynchronously then you must create a thread. =head3 Threads in libguestfs E 1.38 In libguestfs E 1.38, each handle (C) contains a lock which is acquired automatically when you call a libguestfs function. The practical effect of this is you can call libguestfs functions with the same handle from multiple threads without needing to do any locking. Also in libguestfs E 1.38, the last error on the handle (L, L) is stored in thread-local storage, so it is safe to write code like: if (guestfs_add_drive_ro (g, drive) == -1) fprintf (stderr, "error was: %s\n", guestfs_last_error (g)); even when other threads may be concurrently using the same handle C. =head3 Threads in libguestfs E 1.38 In libguestfs E 1.38, you must use the handle only from a single thread. Either use the handle exclusively from one thread, or provide your own mutex so that two threads cannot issue calls on the same handle at the same time. Even apparently innocent functions like L are I safe to be called from multiple threads without a mutex in libguestfs E 1.38. Use L to make it simpler to identify threads in trace output. =head2 PATH Libguestfs needs a supermin appliance, which it finds by looking along an internal path. By default it looks for these in the directory C<$libdir/guestfs> (eg. F or F). Use L or set the environment variable L to change the directories that libguestfs will search in. The value is a colon-separated list of paths. The current directory is I searched unless the path contains an empty element or C<.>. For example C would search the current directory and then F. =head2 QEMU WRAPPERS If you want to compile your own qemu, run qemu from a non-standard location, or pass extra arguments to qemu, then you can write a shell-script wrapper around qemu. There is one important rule to remember: you I> as the last command in the shell script (so that qemu replaces the shell and becomes the direct child of the libguestfs-using program). If you don't do this, then the qemu process won't be cleaned up correctly. Here is an example of a wrapper, where I have built my own copy of qemu from source: #!/bin/sh - qemudir=/home/rjones/d/qemu exec $qemudir/x86_64-softmmu/qemu-system-x86_64 -L $qemudir/pc-bios "$@" Save this script as F (or wherever), C, and then use it by setting the LIBGUESTFS_HV environment variable. For example: LIBGUESTFS_HV=/tmp/qemu.wrapper guestfish Note that libguestfs also calls qemu with the -help and -version options in order to determine features. Wrappers can also be used to edit the options passed to qemu. In the following example, the C<-machine ...> option (C<-machine> and the following argument) are removed from the command line and replaced with C<-machine pc,accel=tcg>. The while loop iterates over the options until it finds the right one to remove, putting the remaining options into the C array. #!/bin/bash - i=0 while [ $# -gt 0 ]; do case "$1" in -machine) shift 2;; *) args[i]="$1" (( i++ )) shift ;; esac done exec qemu-kvm -machine pc,accel=tcg "${args[@]}" =begin html =end html =head2 BACKEND The backend (previously known as the "attach method") controls how libguestfs creates and/or connects to the backend daemon, eg. by starting qemu directly, or using libvirt to manage an appliance, running User-Mode Linux, or connecting to an already running daemon. You can set the backend by calling L, or by setting the environment variable C. Possible backends are described below: =over 4 =item C =item C Run qemu directly to launch an appliance. C and C are synonyms. This is the ordinary method and normally the default, but see the note below. =item C =item C =item C> Use libvirt to launch and manage the appliance. C causes libguestfs to choose a suitable URI for creating session guests. If using the libvirt backend, you almost always should use this. C causes libguestfs to use the C connection URI, which causes libvirt to try to guess what the user meant. You probably don't want to use this. C> uses I as the libvirt connection URI (see L). The typical libvirt backend with a URI would be C The libvirt backend supports more features, including sVirt. =back C is usually the default backend. However since libguestfs E 1.19.24, libguestfs can be built with a different default by doing: ./configure --with-default-backend=... To find out if libguestfs was compiled with a different default backend, do: unset LIBGUESTFS_BACKEND guestfish get-backend =head2 BACKEND SETTINGS Each backend can be configured by passing a list of strings. You can either call L with a list of strings, or set the C environment variable to a colon-separated list of strings (before creating the handle). =head3 force_tcg Using: export LIBGUESTFS_BACKEND_SETTINGS=force_tcg will force the direct and libvirt backends to use TCG (software emulation) instead of KVM (hardware accelerated virtualization). =head3 force_kvm Using: export LIBGUESTFS_BACKEND_SETTINGS=force_kvm will force the direct and libvirt backends to use KVM (hardware accelerated virtualization) instead of TCG (software emulation). =head3 gdb The direct backend supports: export LIBGUESTFS_BACKEND_SETTINGS=gdb When this is set, qemu will not start running the appliance immediately. It will wait for you to connect to it using gdb: $ gdb (gdb) symbol-file /path/to/vmlinux (gdb) target remote tcp::1234 (gdb) cont You can then debug the appliance kernel, which is useful to debug boot failures (especially ones where there are no debug messages printed - tip: look in the kernel C). On Fedora, install C for the C file (containing symbols). Make sure the symbols precisely match the kernel being used. =head2 ABI GUARANTEE We guarantee the libguestfs ABI (binary interface), for public, high-level actions as outlined in this section. Although we will deprecate some actions, for example if they get replaced by newer calls, we will keep the old actions forever. This allows you the developer to program in confidence against the libguestfs API. =head2 BLOCK DEVICE NAMING Libguestfs defines F as the I for devices passed to API calls. So F means "the first device added by L", and F means "the third partition on the second device". Internally device names are sometimes translated, but this should not be visible at the API level. =head3 DISK LABELS In libguestfs E 1.20, you can give a label to a disk when you add it, using the optional C below. If no callback is registered: C is used (suitable for command-line programs only). =back =head2 EVENT API =head3 guestfs_set_event_callback int guestfs_set_event_callback (guestfs_h *g, guestfs_event_callback cb, uint64_t event_bitmask, int flags, void *opaque); This function registers a callback (C) for all event classes in the C. For example, to register for all log message events, you could call this function with the bitmask C. To register a single callback for all possible classes of events, use C. C should always be passed as 0. C is an opaque pointer which is passed to the callback. You can use it for any purpose. The return value is the event handle (an integer) which you can use to delete the callback (see below). If there is an error, this function returns C<-1>, and sets the error in the handle in the usual way (see L etc.) Callbacks remain in effect until they are deleted, or until the handle is closed. In the case where multiple callbacks are registered for a particular event class, all of the callbacks are called. The order in which multiple callbacks are called is not defined. =head3 guestfs_delete_event_callback void guestfs_delete_event_callback (guestfs_h *g, int event_handle); Delete a callback that was previously registered. C should be the integer that was returned by a previous call to C on the same handle. =head3 guestfs_event_to_string char *guestfs_event_to_string (uint64_t event); C is either a single event or a bitmask of events. This returns a string representation (useful for debugging or printing events). A single event is returned as the name in lower case, eg. C<"close">. A bitmask of several events is returned as a comma-separated list, eg. C<"close,progress">. If zero is passed, then the empty string C<""> is returned. On success this returns a string. On error it returns NULL and sets C. The returned string must be freed by the caller. =head3 guestfs_event_callback typedef void (*guestfs_event_callback) ( guestfs_h *g, void *opaque, uint64_t event, int event_handle, int flags, const char *buf, size_t buf_len, const uint64_t *array, size_t array_len); This is the type of the event callback function that you have to provide. The basic parameters are: the handle (C), the opaque user pointer (C), the event class (eg. C), the event handle, and C which in the current API you should ignore. The remaining parameters contain the event payload (if any). Each event may contain a payload, which usually relates to the event class, but for future proofing your code should be written to handle any payload for any event class. C and C contain a message buffer (if C, then there is no message buffer). Note that this message buffer can contain arbitrary 8 bit data, including NUL bytes. C and C is an array of 64 bit unsigned integers. At the moment this is only used for progress messages. =head2 EXAMPLE: CAPTURING LOG MESSAGES A working program demonstrating this can be found in F in the source of libguestfs. One motivation for the generic event API was to allow GUI programs to capture debug and other messages. In libguestfs E 1.8 these were sent unconditionally to C. Events associated with log messages are: C, C, C and C. (Note that error messages are not events; you must capture error messages separately). Programs have to set up a callback to capture the classes of events of interest: int eh = guestfs_set_event_callback (g, message_callback, GUESTFS_EVENT_LIBRARY | GUESTFS_EVENT_APPLIANCE | GUESTFS_EVENT_WARNING | GUESTFS_EVENT_TRACE, 0, NULL) == -1) if (eh == -1) { // handle error in the usual way } The callback can then direct messages to the appropriate place. In this example, messages are directed to syslog: static void message_callback ( guestfs_h *g, void *opaque, uint64_t event, int event_handle, int flags, const char *buf, size_t buf_len, const uint64_t *array, size_t array_len) { const int priority = LOG_USER|LOG_INFO; if (buf_len > 0) syslog (priority, "event 0x%lx: %s", event, buf); } =head2 LIBVIRT AUTHENTICATION Some libguestfs API calls can open libvirt connections. Currently the only ones are L; and L if the libvirt backend has been selected. Libvirt connections may require authentication, for example if they need to access a remote server or to access root services from non-root. Libvirt authentication happens via a callback mechanism, see L You may provide libvirt authentication data by registering a callback for events of type C. If no such event is registered, then libguestfs uses a libvirt function that provides command-line prompts (C). This is only suitable for command-line libguestfs programs. To provide authentication, first call L with the list of credentials your program knows how to provide. Second, register a callback for the C event. The event handler will be called when libvirt is requesting authentication information. In the event handler, call L to get a list of the credentials that libvirt is asking for. You then need to ask (eg. the user) for each credential, and call L with the answer. Note that for each credential, additional information may be available via the calls L, L or L. The example program below should make this clearer. There is also a more substantial working example program supplied with the libguestfs sources, called F. main () { guestfs_h *g; char *creds[] = { "authname", "passphrase", NULL }; int r, eh; g = guestfs_create (); if (!g) exit (EXIT_FAILURE); /* Tell libvirt what credentials the program supports. */ r = guestfs_set_libvirt_supported_credentials (g, creds); if (r == -1) exit (EXIT_FAILURE); /* Set up the event handler. */ eh = guestfs_set_event_callback ( g, do_auth, GUESTFS_EVENT_LIBVIRT_AUTH, 0, NULL); if (eh == -1) exit (EXIT_FAILURE); /* An example of a call that may ask for credentials. */ r = guestfs_add_domain ( g, "dom", GUESTFS_ADD_DOMAIN_LIBVIRTURI, "qemu:///system", -1); if (r == -1) exit (EXIT_FAILURE); exit (EXIT_SUCCESS); } static void do_auth (guestfs_h *g, void *opaque, uint64_t event, int event_handle, int flags, const char *buf, size_t buf_len, const uint64_t *array, size_t array_len) { char **creds; size_t i; char *prompt; char *reply; size_t replylen; int r; // buf will be the libvirt URI. buf_len may be ignored. printf ("Authentication required for libvirt conn '%s'\n", buf); // Ask libguestfs what credentials libvirt is demanding. creds = guestfs_get_libvirt_requested_credentials (g); if (creds == NULL) exit (EXIT_FAILURE); // Now ask the user for answers. for (i = 0; creds[i] != NULL; ++i) { if (strcmp (creds[i], "authname") == 0 || strcmp (creds[i], "passphrase") == 0) { prompt = guestfs_get_libvirt_requested_credential_prompt (g, i); if (prompt && strcmp (prompt, "") != 0) printf ("%s: ", prompt); free (prompt); // Some code here to ask for the credential. // ... // Put the reply in 'reply', length 'replylen' (bytes). r = guestfs_set_libvirt_requested_credential (g, i, reply, replylen); if (r == -1) exit (EXIT_FAILURE); } free (creds[i]); } free (creds); } =head1 CANCELLING LONG TRANSFERS Some operations can be cancelled by the caller while they are in progress. Currently only operations that involve uploading or downloading data can be cancelled (technically: operations that have C or C parameters in the generator). To cancel the transfer, call L. For more information, read the description of L. =head1 PRIVATE DATA AREA You can attach named pieces of private data to the libguestfs handle, fetch them by name, and walk over them, for the lifetime of the handle. This is called the private data area and is only available from the C API. To attach a named piece of data, use the following call: void guestfs_set_private (guestfs_h *g, const char *key, void *data); C is the name to associate with this data, and C is an arbitrary pointer (which can be C). Any previous item with the same key is overwritten. You can use any C string you want, but avoid keys beginning with an underscore character (libguestfs uses those for its own internal purposes, such as implementing language bindings). It is recommended that you prefix the key with some unique string to avoid collisions with other users. To retrieve the pointer, use: void *guestfs_get_private (guestfs_h *g, const char *key); This function returns C if either no data is found associated with C, or if the user previously set the C’s C pointer to C. Libguestfs does not try to look at or interpret the C pointer in any way. As far as libguestfs is concerned, it need not be a valid pointer at all. In particular, libguestfs does I try to free the data when the handle is closed. If the data must be freed, then the caller must either free it before calling L or must set up a close callback to do it (see L). To walk over all entries, use these two functions: void *guestfs_first_private (guestfs_h *g, const char **key_rtn); void *guestfs_next_private (guestfs_h *g, const char **key_rtn); C returns the first key, pointer pair ("first" does not have any particular meaning -- keys are not returned in any defined order). A pointer to the key is returned in C<*key_rtn> and the corresponding data pointer is returned from the function. C is returned if there are no keys stored in the handle. C returns the next key, pointer pair. The return value of this function is C if there are no further entries to return. Notes about walking over entries: =over 4 =item * You must not call C while walking over the entries. =item * The handle maintains an internal iterator which is reset when you call C. This internal iterator is invalidated when you call C. =item * If you have set the data pointer associated with a key to C, ie: guestfs_set_private (g, key, NULL); then that C is not returned when walking. =item * C<*key_rtn> is only valid until the next call to C, C or C. =back The following example code shows how to print all keys and data pointers that are associated with the handle C: const char *key; void *data = guestfs_first_private (g, &key); while (data != NULL) { printf ("key = %s, data = %p\n", key, data); data = guestfs_next_private (g, &key); } More commonly you are only interested in keys that begin with an application-specific prefix C. Modify the loop like so: const char *key; void *data = guestfs_first_private (g, &key); while (data != NULL) { if (strncmp (key, "foo_", strlen ("foo_")) == 0) printf ("key = %s, data = %p\n", key, data); data = guestfs_next_private (g, &key); } If you need to modify keys while walking, then you have to jump back to the beginning of the loop. For example, to delete all keys prefixed with C: const char *key; void *data; again: data = guestfs_first_private (g, &key); while (data != NULL) { if (strncmp (key, "foo_", strlen ("foo_")) == 0) { guestfs_set_private (g, key, NULL); /* note that 'key' pointer is now invalid, and so is the internal iterator */ goto again; } data = guestfs_next_private (g, &key); } Note that the above loop is guaranteed to terminate because the keys are being deleted, but other manipulations of keys within the loop might not terminate unless you also maintain an indication of which keys have been visited. =head1 LIBGUESTFS VERSION NUMBERS Since April 2010, libguestfs has started to make separate development and stable releases, along with corresponding branches in our git repository. These separate releases can be identified by version number: even numbers for stable: 1.2.x, 1.4.x, ... .-------- odd numbers for development: 1.3.x, 1.5.x, ... | v 1 . 3 . 5 ^ ^ | | | `-------- sub-version | `------ always '1' because we don't change the ABI Thus "1.3.5" is the 5th update to the development branch "1.3". As time passes we cherry pick fixes from the development branch and backport those into the stable branch, the effect being that the stable branch should get more stable and less buggy over time. So the stable releases are ideal for people who don't need new features but would just like the software to work. Our criteria for backporting changes are: =over 4 =item * Documentation changes which don’t affect any code are backported unless the documentation refers to a future feature which is not in stable. =item * Bug fixes which are not controversial, fix obvious problems, and have been well tested are backported. =item * Simple rearrangements of code which shouldn't affect how it works get backported. This is so that the code in the two branches doesn't get too far out of step, allowing us to backport future fixes more easily. =item * We I backport new features, new APIs, new tools etc, except in one exceptional case: the new feature is required in order to implement an important bug fix. =back A new stable branch starts when we think the new features in development are substantial and compelling enough over the current stable branch to warrant it. When that happens we create new stable and development versions 1.N.0 and 1.(N+1).0 [N is even]. The new dot-oh release won't necessarily be so stable at this point, but by backporting fixes from development, that branch will stabilize over time. =head1 LIMITS =head2 PROTOCOL LIMITS Internally libguestfs uses a message-based protocol to pass API calls and their responses to and from a small "appliance" (see L for plenty more detail about this). The maximum message size used by the protocol is slightly less than 4 MB. For some API calls you may need to be aware of this limit. The API calls which may be affected are individually documented, with a link back to this section of the documentation. In libguestfs E 1.19.32, several calls had to encode either their entire argument list or their entire return value (or sometimes both) in a single protocol message, and this gave them an arbitrary limitation on how much data they could handle. For example, L could only download a file if it was less than around 4 MB in size. In later versions of libguestfs, some of these limits have been removed. The APIs which were previously limited but are now unlimited (except perhaps by available memory) are listed below. To find out if a specific API is subject to protocol limits, check for the warning in the API documentation which links to this section, and remember to check the version of the documentation that matches the version of libguestfs you are using. L, L, L, L, L, L, L, L, L, L. See also L and L for further information about copying large amounts of data into or out of a filesystem. =head2 MAXIMUM NUMBER OF DISKS In libguestfs E 1.19.7, you can query the maximum number of disks that may be added by calling L. In earlier versions of libguestfs (ie. where this call is not available) you should assume the maximum is 25. The rest of this section covers implementation details, which could change in future. When using virtio-scsi disks (the default if available in qemu) the current limit is B<255> disks. When using virtio-blk (the old default) the limit is around B<27> disks, but may vary according to implementation details and whether the network is enabled. Virtio-scsi as used by libguestfs is configured to use one target per disk, and 256 targets are available. Virtio-blk consumes 1 virtual PCI slot per disk, and PCI is limited to 31 slots, but some of these are used for other purposes. One virtual disk is used by libguestfs internally. Before libguestfs 1.19.7, disk names had to be a single character (eg. F through F), and since one disk is reserved, that meant the limit was 25. This has been fixed in more recent versions. =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 F, and F through F). 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 ADVANCED MACHINE READABLE OUTPUT Some of the tools support a I<--machine-readable> option, which is generally used to make the output more machine friendly, for easier parsing for example. By default, this output goes to stdout. When using the I<--machine-readable> option, the progress, information, warning, and error messages are also printed in JSON format for easier log tracking. Thus, it is highly recommended to redirect the machine-readable output to a different stream. The format of these JSON messages is like the following (actually printed within a single line, below it is indented for readability): { "message": "Finishing off", "timestamp": "2019-03-22T14:46:49.067294446+01:00", "type": "message" } C can be: C for progress messages, C for information messages, C for warning messages, and C for error message. C is the L timestamp of the message. In addition to that, a subset of these tools support an extra string passed to the I<--machine-readable> option: this string specifies where the machine-readable output will go. The possible values are: =over 4 =item BI The output goes to the specified I, which is a file descriptor already opened for writing. =item BF The output goes to the specified F. =item B The output goes to stdout. This is basically the same as the default behaviour of I<--machine-readable> with no parameter, although stdout as output is specified explicitly. =item B The output goes to stderr. =back =head1 ENVIRONMENT VARIABLES =over 4 =item LIBGUESTFS_APPEND Pass additional options to the guest kernel. =item LIBGUESTFS_ATTACH_METHOD This is the old way to set C. =item LIBGUESTFS_BACKEND Choose the default way to create the appliance. See L and L. =item LIBGUESTFS_BACKEND_SETTINGS A colon-separated list of backend-specific settings. See L, 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 F 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_HV Set the default hypervisor (usually 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_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 This is the old way to set C. =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 F is used. See also L, L. =item LIBGUESTFS_TRACE Set C to enable command traces. This has the same effect as calling C. =item PATH Libguestfs may run some external programs, and relies on C<$PATH> being set to a reasonable value. If using the libvirt backend, libvirt will not work at all unless C<$PATH> contains the path of qemu/KVM. Note that PHP by default removes C<$PATH> from the environment which tends to break everything. =item SUPERMIN_KERNEL =item SUPERMIN_KERNEL_VERSION =item SUPERMIN_MODULES These three environment variables allow the kernel that libguestfs uses in the appliance to be selected. If C<$SUPERMIN_KERNEL> is not set, then the most recent host kernel is chosen. For more information about kernel selection, see L. =item TMPDIR See L, L. =item XDG_RUNTIME_DIR This directory represents a user-specific directory for storing non-essential runtime files. If it is set, then is used to store temporary sockets and PID files. Otherwise, F is used. See also L, L. =back =head1 SEE ALSO Examples written in C: L. Language bindings: L, L, L, L, L, L, L, L. Tools: 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. Other libguestfs topics: L, L, L, L, L, L, L, L, L, L. Related manual pages: L, L, L, L, L. Website: L Tools with a similar purpose: L, L, L, L, L. =head1 AUTHORS Richard W.M. Jones (C) =head1 COPYRIGHT Copyright (C) 2009-2025 Red Hat Inc.