Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

21.4. guestfish Shell

guestfish 是一个交互式 shell,您可以通过命令行或从 shell 脚本使用以访问 guest 虚拟机文件系统。libguestfs API 的所有功能均可从 shell 访问。
要开始查看或编辑虚拟机磁盘镜像,请输入以下命令,将路径替换为您预期的磁盘镜像:
$ guestfish --ro -a /path/to/disk/image
--ro 表示磁盘镜像以只读方式打开。这个模式总是安全,但不允许写入访问。仅在 确定 客户机虚拟机没有运行时省略这个选项,或者磁盘镜像没有附加到实时客户机虚拟机。无法使用 libguestfs 编辑实时客户机虚拟机,并尝试使磁盘不可逆损坏。
/path/to/disk/image 是到磁盘的路径。这可以是文件、主机物理机器逻辑卷(如 /dev/VG/LV)或 SAN LUN(/dev/sdf3)。
注意
libguestfs 和 guestfish 不需要 root 权限。当被访问的磁盘镜像需要 root 用户进行读取或写入时,您只需要以 root 用户身份运行它们。
当您以交互方式启动 guestfish 时,它将显示此提示:
$ 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>
在提示符处,键入 run 来启动库并附加磁盘镜像。第一次完成最多可能需要 30 秒。随后的开始将更快地完成。
注意
libguestfs 将使用硬件虚拟化加速(如 KVM(如果可用)来加快这个过程。
输入了 run 命令后,可以使用其他命令,作为以下部分所示。

21.4.1. 使用 guestfish 查看文件系统

这部分提供有关使用 guestfish 查看文件系统的信息。

21.4.1.1. 手动列出和查看

list-filesystems 命令将列出 libguestfs 找到的文件系统。这个输出显示了 Red Hat Enterprise Linux 4 磁盘镜像:
><fs> run
><fs> list-filesystems
/dev/vda1: ext3
/dev/VolGroup00/LogVol00: ext3
/dev/VolGroup00/LogVol01: swap
其他有用的命令包括 list-deviceslist-partitionslvspvsvfs-typefile。您可以通过键入 help 命令来获取 更多信息和有关任何命令的帮助,如下例所示:
><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".
要查看文件系统的实际内容,必须先挂载它。
您可以使用 lsll ll、cat更多下载和 tarout 等 guestfish 命令来查看和下载文件和目录。
注意
此 shell 中没有当前工作目录的概念。与一般的 shell 不同,您无法使用 cd 命令来更改目录。所有路径都必须在顶部以正斜杠()字符开始完全合格。使用 Tab 键完成路径。
要从 guestfish shell 退出,请键入 exit 或按 Ctrl+d

21.4.1.2. 通过 guestfish 检查

guestfish 本身可以检查映像并挂载文件系统,而不是手动列出和挂载文件系统。要做到这一点,在命令行中添加 -i 选项:
$ 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 需要启动 libguestfs 后端才能执行检查和挂载,因此在使用 -i 选项时不需要 run 命令。i 选项 适用于许多常见的 Linux 客户机虚拟机。

21.4.1.3. 按名称访问客户机虚拟机

当您指定给 libvirt 的名称时(换句话说,如 virsh list --all所示),可从命令行访问客户机虚拟机。使用 -d 选项按名称或不使用 -i 选项访问客户机虚拟机:
$ guestfish --ro -d GuestName -i