libguestfs 是一组 Linux 下的 C 语言的 API ,用来访问虚拟机的磁盘映像文件。其项目主页是http://libguestfs.org/ ,该工具包内包含的工具有virt-cat、virt-df、virt-ls、virt-copy-in、virt-copy-out、virt-edit、guestfs、guestmount、virt-list-filesystems、virt-list-partitions等工具,具体用法也可以参看官网。该工具可以在不启动KVM guest主机的情况下,直接查看guest主机内的文内容,也可以直接向img镜像中写入文件和复制文件到外面的物理机,当然其也可以像mount一样,支持挂载操作。

一、libguestfs-tools的安装

由于在rpm源里直接有该包,所以可以直接通过yum进行安装:

1#yum -y install libguestfs-tools

二、linux下的使用

 1[root@localhost file]# virt-df centos.img
 2Filesystem                           1K-blocks       Used  Available  Use%
 3centos.img:/dev/sda1                    495844      31950     438294    7%
 4centos.img:/dev/VolGroup/lv_root      28423176     721504   26257832    3%
 5[root@localhost file]# virt-ls centos.img /
 6.autofsck
 7bin
 8boot
 9dev
10etc
11home
12lib
13lib64
14lost+found
15media
16mnt
17opt
18proc
19root
20sbin
21selinux
22srv
23sys
24tmp
25usr
26va

复制文件操作:

1[root@localhost file]# virt-copy-out -d centos.img  /etc/passwd /tmp
2libguestfs: error: no libvirt domain called 'centos.img': Domain not found: no domain with matching name 'centos.img'

注意这里有报错,报错原因是因为-d参数后面跟的是主机domain,不是镜像文件名。更改为domain后的操作步骤如下:

 1[root@localhost file]# virsh list --all
 2 Id    Name                           State
 3----------------------------------------------------
 4 4     ppd_win7                       running
 5 14    ppd_win2008                    running
 6 -     ppd_centos                     shut off
 7[root@localhost file]# virt-copy-out -d ppd_centos /etc/passwd /tmp/
 8[root@localhost file]# cat /tmp/passwd
 9root:x:0:0:root:/root:/bin/bash
10bin:x:1:1:bin:/bin:/sbin/nologin
11daemon:x:2:2:daemon:/sbin:/sbin/nologin
12adm:x:3:4:adm:/var/adm:/sbin/nologin
13lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
14sync:x:5:0:sync:/sbin:/bin/sync
15shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
16halt:x:7:0:halt:/sbin:/sbin/halt
17mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
18uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
19operator:x:11:0:operator:/root:/sbin/nologin
20games:x:12:100:games:/usr/games:/sbin/nologin
21gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
22ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
23nobody:x:99:99:Nobody:/:/sbin/nologin
24vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
25saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
26postfix:x:89:89::/var/spool/postfix:/sbin/nologin
27sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

查看分区相关信息:

 1[root@localhost file]# virt-filesystems  -d ppd_centos
 2/dev/sda1
 3/dev/VolGroup/lv_root
 4[root@localhost file]# virt-list-filesystems  -d ppd_centos
 5Unknown option: d
 6Usage:
 7     virt-list-filesystems [--options] domname
 8     virt-list-filesystems [--options] disk.img [disk.img ...]
 9[root@localhost file]# virt-list-filesystems  /file/centos.img
10/dev/VolGroup/lv_root
11/dev/sda1
12[root@localhost file]# virt-list-partitions  /file/centos.img
13/dev/sda1
14/dev/sda2

需要注意的是,有的命令后面用的是domain,有的用的是img文件名。

guestmount 分区挂载

 1[root@localhost ~]# guestmount -a /file/centos.qcow2  -m /dev/sda2  --rw /mnt
 2libguestfs: error: mount_options: /dev/sda2 on / (options: ''): mount: unknown filesystem type 'LVM2_member'
 3guestmount: '/dev/sda2' could not be mounted.
 4guestmount: Did you mean to mount one of these filesystems?
 5guestmount:     /dev/sda1 (ext4)
 6guestmount:     /dev/VolGroup/lv_root (ext4)
 7guestmount:     /dev/VolGroup/lv_swap (swap)
 8[root@localhost ~]# guestmount -a /file/centos.qcow2  -m /dev/VolGroup/lv_root  --rw /mnt
 9fuse: mountpoint is not empty
10fuse: if you are sure this is safe, use the 'nonempty' mount option
11libguestfs: error: fuse_mount: /mnt: Resource temporarily unavailable
12[root@localhost ~]# ls /mnt/
13bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
14[root@localhost ~]# umount /mnt/
15[root@localhost ~]# guestmount -a /file/centos.qcow2  -m /dev/VolGroup/lv_root  --rw /mnt
16[root@localhost ~]# ls /mnt/
17bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
18[root@localhost ~]# umount /mnt/

挂载出错时会列出所有的分区信息,然后再做使用正确的分区去挂载。不过也可以在挂载前先使用virt-list-filesystems去查看。使用guestmount时,对raw和qcow2格式的分区都支持。

三、windows系统下的使用

 1[root@localhost opt]# virt-ls -a /file/win7.img c:
 2virt-ls: no operating system was found on this disk
 3If using guestfish '-i' option, remove this option and instead
 4use the commands 'run' followed by 'list-filesystems'.
 5You can then mount filesystems you want by hand using the
 6'mount' or 'mount-ro' command.
 7If using guestmount '-i', remove this option and choose the
 8filesystem(s) you want to see by manually adding '-m' option(s).
 9Use 'virt-filesystems' to see what filesystems are available.
10If using other virt tools, this disk image won't work
11with these tools.  Use the guestfish equivalent commands
12(see the virt tool manual page).
13RHEL 6 notice
14-------------
15libguestfs will return this error for Microsoft Windows guests if the
16separate 'libguestfs-winsupport' package is not installed. If the
17guest is running Microsoft Windows, please try again after installing
18'libguestfs-winsupport'.

需要注意的是,上面的用法中,有两个错误的地方,一处是linux查看C分区,后面不能直接跟C:,而应该换用/ ;第二个错误是由于没有安装libguestfs-winsupport 。该工具也可以看接通过yum安装 。安装完该包后,再进行查看:

 1[root@localhost opt]# virt-ls -a /file/win7.img /
 2$Recycle.Bin
 3Documents and Settings
 4PerfLogs
 5Program Files
 6Program Files (x86)
 7ProgramData
 8Recovery
 9System Volume Information
10Users
11Windows
12pagefile.sys

利用guestmount进行挂载

1[root@localhost ~]# guestmount -a /file/win7.img  -m /dev/sda2  --rw /mnt
2[root@localhost ~]# ls /mnt/
3Documents and Settings  pagefile.sys  PerfLogs  ProgramData  Program Files  Program Files (x86)  Recovery  $Recycle.Bin  System Volume Information  Users  Windows

注:此处的挂载同样需要ntfs-3g软件包的支持。