21.4. The guestfish Shell guestfish is an interactive shell that you can use from the command line or from shell scripts to access guest virtual machine file systems. All of the functionality of the libguestfs API is available from the shell. To begin viewing or editing a virtual machine disk image, enter the following command, substituting the path to your intended disk image: --ro means that the disk image is opened read-only. This mode is always safe but does not allow write access. Only omit this option when you are certain that the guest virtual machine is not running, or the disk image is not attached to a live guest virtual machine. It is not possible to use libguestfs to edit a live guest virtual machine, and attempting to will result in irreversible disk corruption. /path/to/disk/image is the path to the disk. This can be a file, a host physical machine logical volume (such as /dev/VG/LV), or a SAN LUN (/dev/sdf3). Note libguestfs and guestfish do not require root privileges. You only need to run them as root if the disk image being accessed needs root to read or write or both. When you start guestfish interactively, it will display this prompt: At the prompt, type run to initiate the library and attach the disk image. This can take up to 30 seconds the first time it is done. Subsequent starts will complete much faster. Note libguestfs will use hardware virtualization acceleration such as KVM (if available) to speed up this process. Once the run command has been entered, other commands can be used, as the following section demonstrates. 21.4.1. Viewing File Systems with guestfish This section provides information on viewing file systems with guestfish. 21.4.1.1. Manual Listing and Viewing The list-filesystems command will list file systems found by libguestfs. This output shows a Red Hat Enterprise Linux 4 disk image: Other useful commands are list-devices , list-partitions , lvs , pvs , vfs-type and file . You can get more information and help on any command by typing help command , as shown in the following output: To view the actual contents of a file system, it must first be mounted. You can use guestfish commands such as ls , ll , cat , more , download and tar-out to view and download files and directories. Note There is no concept of a current working directory in this shell. Unlike ordinary shells, you cannot for example use the cd command to change directories. All paths must be fully qualified starting at the top with a forward slash ( / ) character. Use the Tab key to complete paths. To exit from the guestfish shell, type exit or enter Ctrl+d . 21.4.1.2. Via guestfish inspection Instead of listing and mounting file systems by hand, it is possible to let guestfish itself inspect the image and mount the file systems as they would be in the guest virtual machine. To do this, add the -i option on the command line: Because guestfish needs to start up the libguestfs back end in order to perform the inspection and mounting, the run command is not necessary when using the -i option. The -i option works for many common Linux guest virtual machines. 21.4.1.3. Accessing a guest virtual machine by name A guest virtual machine can be accessed from the command line when you specify its name as known to libvirt (in other words, as it appears in virsh list --all ). Use the -d option to access a guest virtual machine by its name, with or without the -i option: 21.4.2. Adding Files with guestfish To add a file with guestfish you need to have the complete URI. The file can be a local file or a file located on a network block device (NBD) or a remote block device (RBD). The format used for the URI should be like any of these examples. For local files, use ///: guestfish -a disk .img guestfish -a file:/// directory / disk .img guestfish -a nbd:// example.com [ : port ] guestfish -a nbd:// example.com [ : port ]/ exportname guestfish -a nbd://?socket=/ socket guestfish -a nbd:/// exportname ?socket=/ socket guestfish -a rbd:/// pool / disk guestfish -a rbd:// example.com [ : port ]/ pool / disk 21.4.3. Modifying Files with guestfish To modify files, create directories or make other changes to a guest virtual machine, first heed the warning at the beginning of this section: your guest virtual machine must be shut down . Editing or changing a running disk with guestfish will result in disk corruption. This section gives an example of editing the /boot/grub/grub.conf file. When you are sure the guest virtual machine is shut down you can omit the --ro flag in order to get write access using a command such as: Commands to edit files include edit , vi and emacs . Many commands also exist for creating files and directories, such as write , mkdir , upload and tar-in . 21.4.4. Other Actions with guestfish You can also format file systems, create partitions, create and resize LVM logical volumes and much more, with commands such as mkfs , part-add , lvresize , lvcreate , vgcreate and pvcreate . 21.4.5. Shell Scripting with guestfish Once you are familiar with using guestfish interactively, according to your needs, writing shell scripts with it may be useful. The following is a simple shell script to add a new MOTD (message of the day) to a guest: 21.4.6. Augeas and libguestfs Scripting Combining libguestfs with Augeas can help when writing scripts to manipulate Linux guest virtual machine configuration. For example, the following script uses Augeas to parse the keyboard configuration of a guest virtual machine, and to print out the layout. Note that this example only works with guest virtual machines running Red Hat Enterprise Linux: Augeas can also be used to modify configuration files. You can modify the above script to change the keyboard layout: Note the three changes between the two scripts: The --ro option has been removed in the second example, giving the ability to write to the guest virtual machine. The aug-get command has been changed to aug-set to modify the value instead of fetching it. The new value will be "gb" (including the quotes). The aug-save command is used here so Augeas will write the changes out to disk. Note More information about Augeas can be found on the website http://augeas.net . guestfish can do much more than we can cover in this introductory document. For example, creating disk images from scratch: Or copying out whole directories from a disk image: For more information see the man page guestfish(1).

Commands:
guestfish --ro -a /path/to/disk/image
guestfish --ro -a /path/to/disk/image Welcome to guestfish, the guest filesystem shell for editing virtual machine filesystems and disk images. Type: 'help' for help on commands 'man' to read the manual 'quit' to quit the shell ><fs>
><fs> run ><fs> list-filesystems /dev/vda1: ext3 /dev/VolGroup00/LogVol00: ext3 /dev/VolGroup00/LogVol01: swap
><fs> help vfs-type NAME vfs-type - get the Linux VFS type corresponding to a mounted device SYNOPSIS vfs-type mountable DESCRIPTION This command gets the filesystem type corresponding to the filesystem on "device". For most filesystems, the result is the name of the Linux VFS module which would be used to mount this filesystem if you mounted it without specifying the filesystem type. For example a string such as "ext3" or "ntfs".
guestfish --ro -a /path/to/disk/image -i Welcome to guestfish, the guest filesystem shell for editing virtual machine filesystems and disk images. Type: 'help' for help on commands 'man' to read the manual 'quit' to quit the shell Operating system: Red Hat Enterprise Linux AS release 4 (Nahant Update 8) /dev/VolGroup00/LogVol00 mounted on / /dev/vda1 mounted on /boot ><fs> ll / total 210 drwxr-xr-x. 24 root root 4096 Oct 28 09:09 . drwxr-xr-x 21 root root 4096 Nov 17 15:10 .. drwxr-xr-x. 2 root root 4096 Oct 27 22:37 bin drwxr-xr-x. 4 root root 1024 Oct 27 21:52 boot drwxr-xr-x. 4 root root 4096 Oct 27 21:21 dev drwxr-xr-x. 86 root root 12288 Oct 28 09:09 etc
guestfish --ro -d GuestName -i
guestfish -d RHEL3 -i Welcome to guestfish, the guest filesystem shell for editing virtual machine filesystems and disk images. Type: 'help' for help on commands 'man' to read the manual 'quit' to quit the shell Operating system: Red Hat Enterprise Linux AS release 3 (Taroon Update 9) /dev/vda2 mounted on / /dev/vda1 mounted on /boot ><fs> edit /boot/grub/grub.conf
#!/bin/bash - set -e guestname="USD1" guestfish -d "USDguestname" -i <<'EOF' write /etc/motd "Welcome to Acme Incorporated." chmod 0644 /etc/motd EOF
#!/bin/bash - set -e guestname="USD1" guestfish -d "USD1" -i --ro <<'EOF' aug-init / 0 aug-get /files/etc/sysconfig/keyboard/LAYOUT EOF
#!/bin/bash - set -e guestname="USD1" guestfish -d "USD1" -i <<'EOF' aug-init / 0 aug-set /files/etc/sysconfig/keyboard/LAYOUT '"gb"' aug-save EOF
guestfish -N fs
><fs> copy-out /home /tmp/home