Query: guestfs
OS: suse
Section: 3
Format: Original Unix Latex Style Formatted with HTML and a Horizontal Scroll Bar
guestfs(3) Virtualization Support guestfs(3)NAMEguestfs - Library for accessing and modifying virtual machine imagesSYNOPSIS#include <guestfs.h> 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_close (g); cc prog.c -o prog -lguestfs or: cc prog.c -o prog `pkg-config libguestfs --cflags --libs`DESCRIPTIONLibguestfs 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: <http://libguestfs.org/> Each virt tool has its own man page (for a full list, go to "SEE ALSO" at the end of this file). The libguestfs FAQ contains many useful answers: guestfs-faq(1). For examples of using the API from C, see guestfs-examples(3). For examples in other languages, see "USING LIBGUESTFS WITH OTHER PROGRAMMING LANGUAGES" below.API OVERVIEWThis 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. 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 "g" 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"); /* This is only needed for libguestfs < 1.5.24. Since then * it is done automatically when you close the handle. See * discussion of autosync in this page. */ guestfs_sync (g); /* Close 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 "-1" on error, and all functions that return pointers return "NULL" on error. See section "ERROR HANDLING" below for how to handle errors, and consult the documentation for each function call below to see precisely how they return error indications. See guestfs-examples(3) for fully worked examples. DISK IMAGES The image filename ("guest.img" in the example above) could be a disk image from a virtual machine, a dd(1) copy of a physical hard disk, an actual block device, or simply an empty file of zeroes that you have created through posix_fallocate(3). Libguestfs lets you do useful things to all of these. The call you should use in modern code for adding drives is "guestfs_add_drive_opts". 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 "guestfs_add_drive_ro". In either case libguestfs won't modify the file. 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 "/dev/sda" (for the first one you added), "/dev/sdb" (for the second one you added), etc. Once "guestfs_launch" has been called you cannot add any more images. You can call "guestfs_list_devices" to get a list of the device names, in the order that you added them. See also "BLOCK DEVICE NAMING" below. 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 "guestfs_mount_options" or "guestfs_mount_ro". 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_options (g, "", "/dev/sda1", "/"); where "/dev/sda1" means literally the first partition (1) of the first disk image that we added ("/dev/sda"). If the disk contains Linux LVM2 logical volumes you could refer to those instead (eg. "/dev/VG/LV"). 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 "guestfs_list_partitions" and "guestfs_lvs" to list possible partitions and LVs, and either try mounting each to see what is mountable, or else examine them with "guestfs_vfs_type" or "guestfs_file". To list just filesystems, use "guestfs_list_filesystems". Libguestfs also has a set of APIs for inspection of unknown disk images (see "INSPECTION" below). But you might find it easier to look at higher level programs built on top of libguestfs, in particular virt-inspector(1). To mount a filesystem read-only, use "guestfs_mount_ro". There are several other variations of the "guestfs_mount_*" call. 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 "/" and including the mount point. For example, if you mounted a filesystem at "/" and you want to read the file called "etc/passwd" then you could do: char *data = guestfs_cat (g, "/etc/passwd"); This would return "data" as a newly allocated buffer containing the full content of that file (with some conditions: see also "DOWNLOADING" below), or "NULL" if there was an error. As another example, to create a top-level directory on that filesystem called "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 "guestfs_umask" and defaulting to 022. See "UMASK". Since libguestfs 1.18, it is possible to mount the libguestfs filesystem on a local directory, subject to some restrictions. See "MOUNT LOCAL" below. 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 "guestfs_part_disk" 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. LVM2 Libguestfs provides access to a large part of the LVM2 API, such as "guestfs_lvcreate" and "guestfs_vgremove". 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 http://tldp.org/HOWTO/LVM-HOWTO/ <http://tldp.org/HOWTO/LVM-HOWTO/>. DOWNLOADING Use "guestfs_cat" to download small, text only files. This call is limited to files which are less than 2 MB and which cannot contain any ASCII NUL ("