16.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, run the following command, substituting the path to your desired disk image:
guestfish --ro -a /path/to/disk/image
guestfish --ro -a /path/to/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), a host physical machine device (/dev/cdrom) 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.
16.4.1. Viewing File Systems with guestfish Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
This section provides information about viewing files with guestfish.
16.4.1.1. Manual Listing and Viewing Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The
list-filesystems
command will list file systems found by libguestfs. This output shows a Red Hat Enterprise Linux 4 disk image:
This output shows a Windows disk image:
><fs> run ><fs> list-filesystems /dev/vda1: ntfs /dev/vda2: ntfs
><fs> run
><fs> list-filesystems
/dev/vda1: ntfs
/dev/vda2: ntfs
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. This example uses one of the Windows partitions shown in the previous output (
/dev/vda2
), which in this case is known to correspond to the C:\ drive:
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
.
16.4.1.2. Using guestfish inspection Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 and Windows guest virtual machines.
16.4.1.3. Accessing a guest virtual machine by name Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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:
guestfish --ro -d GuestName -i
guestfish --ro -d GuestName -i