libguestfs是一個(gè)用來訪問和修改虛擬機(jī)磁盤鏡像的一個(gè)工具集
libguestfs 是一組 Linux 下的 C 語言的 API 耘斩,用來訪問虛擬機(jī)的磁盤映像文件管行。該工具包內(nèi)包含的工具有virt-cat业舍、virt-df赎懦、virt-ls起胰、virt-copy-in久又、virt-copy-out、virt-edit待错、guestfs籽孙、guestmount烈评、virt-list-filesystems火俄、virt-list-partitions等工具,具體用法也可以參看官網(wǎng)讲冠。該工具可以在不啟動(dòng)KVM guest主機(jī)的情況下瓜客,直接查看guest主機(jī)內(nèi)的文內(nèi)容,也可以直接向img鏡像中寫入文件和復(fù)制文件到外面的物理機(jī),當(dāng)然其也可以像mount一樣谱仪,支持掛載操作玻熙。
安裝Libguestfs
yum install libguestfs-tools
實(shí)例
如何利用Libguestfs修復(fù)受損虛擬機(jī)?
作為VMware虛擬化環(huán)境管理員疯攒,你肯定遇到過虛擬機(jī)無法啟動(dòng)的情況嗦随。實(shí)施排錯(cuò)時(shí),你需要對(duì)虛擬機(jī)的內(nèi)部進(jìn)行檢查敬尺。而Libguestfs Linux工具集可以在這種情況下為你提供幫助枚尼。Libguestfs允許在虛擬機(jī)上掛載任何類型的文件系統(tǒng),以便修復(fù)啟動(dòng)故障砂吞。
利用Libguestfs找出損壞的虛擬機(jī)文件
使用Libguestfs署恍,首先需要使用Libvirt。Libvirt是一個(gè)管理接口蜻直,可以和KVM盯质、Xen和其他一些基于Liunx的虛擬機(jī)相互連接。Libguestfs的功能更加強(qiáng)大概而,可以打開Windows虛擬機(jī)上的文件呼巷。這意味著除了能夠使用libguestfs,你還需要一些關(guān)于虛擬機(jī)操作系統(tǒng)內(nèi)部架構(gòu)的知識(shí)到腥。但是首先你需要將虛擬機(jī)遷移到libguestfs可用的環(huán)境當(dāng)中朵逝,也就是Linux環(huán)境。
1. 使用guestfish操作虛擬機(jī)
完成虛擬機(jī)磁盤鏡像文件的復(fù)制之后乡范,可以在libguestfs中使用guestfish這樣的工具將其打開配名,這樣就可以直接在vmdk文件上進(jìn)行操作了。使用
guestfish -rw -a /path/to/windows.vmdk
命令來在虛擬機(jī)中創(chuàng)建一個(gè)連接到文件系統(tǒng)的交互式shell晋辆。在新出現(xiàn)的窗口中渠脉,你可以使用特定的命令來操作虛擬機(jī)文件。
第一個(gè)任務(wù)就是找到可用的文件系統(tǒng):
\><fs> run
\><fs> list-filesystems
/dev/sda1: ntfs
/dev/sda2: ntfs
當(dāng)你使用guestfish shell找到可用文件系統(tǒng)類型之后瓶佳,就可以進(jìn)行掛載了芋膘。使用命令
mount /dev/sda2 /
來掛載二個(gè)分區(qū)的內(nèi)容——在Linux中是/dev/sda2——到guestfish根目錄下。在guestfish中霸饲,你不能像在其他shell環(huán)境中一樣操作目錄为朋。掛載的分區(qū)就是根目錄,你不能使用cd命令來切換目錄厚脉,這意味著所有的路徑必須是完全限定路徑返十,從根目錄開始遭垛。
在guestfish shell當(dāng)中可以使用像vi、ls妇斤、cat、more、download這樣的命令,來查看和下載文件以及目錄,輸入help可以看到完整的命令信息坊饶。
在完整所有操作之后可以使用exit來關(guān)閉guestfish shell。
2. virt-rescue提供了直接訪問方式
在libguestfs工具中殴蓬,還有許多其他可用的工具匿级。
virt-rescue命令——是使用正常的Linux文件系統(tǒng)工具來實(shí)現(xiàn)ad-hoc變化的最佳命令——可以在安全shell中啟動(dòng)虛擬機(jī)。你可以在虛擬機(jī)中操作這些文件染厅,就像對(duì)掛載的文件系統(tǒng)上進(jìn)行操作一樣根蟹,其提供了對(duì)虛擬機(jī)內(nèi)容更加直接的訪問方式。
為了在虛擬機(jī)上使用virt-rescue命令進(jìn)行操作糟秘,使用
virt-rescure 虛擬機(jī)名
來打開virt-rescue shell简逮,之后就進(jìn)入了
><rescue>
模式。
如果需要在虛擬機(jī)中掛載系統(tǒng)文件尿赚,可以使用
fdisk -l
/dev/sda
命令來查看虛擬機(jī)的分區(qū)情況散庶,之后再使用命令
mount /dev/sda1 /sysroot
掛載你想要訪問的分區(qū)。這樣你就可以通過更改來修復(fù)虛擬機(jī)的內(nèi)容了凌净。
虛擬機(jī)修復(fù)之后悲龟,關(guān)閉virt-rescue shell,將虛擬機(jī)重新移動(dòng)到ESXi服務(wù)器的數(shù)據(jù)存儲(chǔ)當(dāng)中冰寻。虛擬機(jī)返回原位置之后须教,如果之前的操作一切順利,你就應(yīng)該可以訪問它了斩芭。
其他命令:
virt-df
查看磁盤占用率
[root@localhost file]# virt-df centos.img
Filesystem 1K-blocks Used Available Use%
centos.img:/dev/sda1 495844 31950 438294 7%
centos.img:/dev/VolGroup/lv_root 28423176 721504 26257832 3%
virt-ls
查看目錄下文件
[root@localhost file]# virt-ls centos.img /
.autofsck
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
sbin
selinux
srv
sys
tmp
usr
virt-copy-out
將鏡像里的文件拷貝出來
virt-copy-out -d 虛擬機(jī)名 /etc/passwd /tmp/
virt-filesystems
virt-list-filesystems
virt-list-partitions
查看分區(qū)相關(guān)信息
[root@localhost file]# virt-filesystems -d 虛擬機(jī)名
/dev/sda1
/dev/VolGroup/lv_root
[root@localhost file]# virt-list-filesystems /file/centos.img
/dev/VolGroup/lv_root
/dev/sda1
[root@localhost file]# virt-list-partitions /file/centos.img
/dev/sda1
/dev/sda2
guestmount
分區(qū)掛載
[root@localhost ~]# guestmount -a /file/centos.qcow2 -m /dev/sda2 --rw /mnt
libguestfs: error: mount_options: /dev/sda2 on / (options: ''): mount: unknown filesystem type 'LVM2_member'
guestmount: '/dev/sda2' could not be mounted.
guestmount: Did you mean to mount one of these filesystems?
guestmount: /dev/sda1 (ext4)
guestmount: /dev/VolGroup/lv_root (ext4)
guestmount: /dev/VolGroup/lv_swap (swap)
[root@localhost ~]# guestmount -a /file/centos.qcow2 -m /dev/VolGroup/lv_root --rw /mnt
fuse: mountpoint is not empty
fuse: if you are sure this is safe, use the 'nonempty' mount option
libguestfs: error: fuse_mount: /mnt: Resource temporarily unavailable
[root@localhost ~]# ls /mnt/
bin boot dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@localhost ~]# umount /mnt/
[root@localhost ~]# guestmount -a /file/centos.qcow2 -m /dev/VolGroup/lv_root --rw /mnt
[root@localhost ~]# ls /mnt/
bin boot dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@localhost ~]# umount /mnt/
使用libguestfs訪問windows系統(tǒng)
[root@localhost opt]# virt-ls -a /file/win7.img c:
virt-ls: no operating system was found on this disk
If using guestfish '-i' option, remove this option and instead
use the commands 'run' followed by 'list-filesystems'.
You can then mount filesystems you want by hand using the
'mount' or 'mount-ro' command.
If using guestmount '-i', remove this option and choose the
filesystem(s) you want to see by manually adding '-m' option(s).
Use 'virt-filesystems' to see what filesystems are available.
If using other virt tools, this disk image won't work
with these tools. Use the guestfish equivalent commands
(see the virt tool manual page).
RHEL 6 notice
-------------
libguestfs will return this error for Microsoft Windows guests if the
separate 'libguestfs-winsupport' package is not installed. If the
guest is running Microsoft Windows, please try again after installing
'libguestfs-winsupport'.
需要注意的是轻腺,上面的用法中,有兩個(gè)錯(cuò)誤的地方划乖,一處是linux查看C分區(qū)贬养,后面不能直接跟C:,而應(yīng)該換用/ 琴庵;第二個(gè)錯(cuò)誤是由于沒有安裝libguestfs-winsupport 误算。該工具也可以看接通過yum安裝 。安裝完該包后迷殿,再進(jìn)行查看:
[root@localhost opt]# virt-ls -a /file/win7.img /
$Recycle.Bin
Documents and Settings
PerfLogs
Program Files
Program Files (x86)
ProgramData
Recovery
System Volume Information
Users
Windows
pagefile.sys
利用guestmount進(jìn)行掛載
[root@localhost ~]# guestmount -a /file/win7.img -m /dev/sda2 --rw /mnt
[root@localhost ~]# ls /mnt/
Documents and Settings pagefile.sys PerfLogs ProgramData Program Files