Este conteúdo não está disponível no idioma selecionado.
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:
guestfish --ro -a /path/to/disk/image
$ guestfish --ro -a /path/to/disk/imageguestfish --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), 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 Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
This section provides information on viewing file systems with guestfish.
21.4.1.1. Manual Listing and Viewing Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 -iguestfish --ro -d GuestName -iguestfish --ro -d GuestName -i
21.4.2. Adding Files with guestfish Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
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 toaug-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:
guestfish -N fs
guestfish -N fs
Or copying out whole directories from a disk image:
><fs> copy-out /home /tmp/home
><fs> copy-out /home /tmp/home
For more information see the man page guestfish(1).