guestfs-访问和修改虚拟机磁盘的强大工具

(文章来自作者维护的社区微信公众号【虚拟化云计算】)
目前有两个微信群《kvm虚拟化》和《openstack》,扫描二维码点击“云-交流”,进群交流提问
简介
libguestfs 是Redhat开源的一组工具集,主要用来访问和修改虚拟机的磁盘。其功能非常强大,我们常用的查看虚拟机磁盘使用率、P2V、V2V、备份克隆虚拟机、格式化重置虚拟机磁盘大小等功能libguestfs都能提供。甚至定制操作系统、操作windows虚拟机注册表这样的功能它也包含其中。
 
libguestfs时使用C语言开发的。
 
使用libguestfs时,可以使用virt-df这样的一系列命令行工具,也可以在程序中调用libguestfs实现的API库函数。它还包含两个shell交互工具。
 
使用举例1. 两个shell : guestfish和virt-rescue
guestfish - the guest filesystem shell,It uses libguestfs and exposes all of the functionality of the guestfs API
例如:
guestfish -a /tmp/test.img --ro <<_EOF_
run
list-filesystems
_EOF_
 
virt-rescue - the rescue shell and some simple recovery tools which you can use to examine or rescue a virtual machine or disk image.
例如:
# virt-rescue -a /tmp/test.img
Could not open option rom 'sgabios.bin': No such file or directory
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 4.4.0-64-generic (buildd@lgw01-56) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) ) #85-Ubuntu SMP Mon Feb 20 11:50:30 UTC 2017 (Ubuntu 4.4.0-64.85-generic 4.4.44)
[    0.000000] Command line: panic=1 console=ttyS0 udevtimeout=6000 udev.event-timeout=6000 no_timer_check acpi=off printk.time=1 cgroup_disable=memory root=/dev/sdb selinux=0 TERM=linux guestfs_rescue=1
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
... ...
 
[    0.809809] intel_rapl: no valid rapl domains found in package 0
/init: 86: /init: cannot create /sys/block/{h,s,ub,v}d*/queue/scheduler: Directory nonexistent
[    1.223216] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x2283c44026a, max_idle_ns: 440795260713 ns
mdadm: No arrays found in config file or automatically
  lvmetad is not active yet, using direct activation during sysinit
/init: 129: /init: ldmtool: not found
------------------------------------------------------------
Welcome to virt-rescue, the libguestfs rescue shell.
Note: The contents of / are the rescue appliance.
You have to mount the guest's partitions under /sysroot
before you can examine them.
groups: cannot find name for group ID 0
><rescue>
 
 
使用举例2. 其他命令行工具
virt-inspector — 显示一个虚拟机的操作系统信息
virt-builder — 快速创建并定制一个虚拟机
virt-v2v — 把VMware, Xen, Hyper-V 等其他hypervisors,上的虚拟机迁移到KVM
virt-p2v — 把物理机上的操作系统迁移到KVM
hivexsh、hivexml、hivexget — windows注册表hive文件的操作工具
supermin — 创建一个supermin appliances,我们在使用LXC或docker时经常使用的工具
例如监控虚拟机磁盘状态,修改虚拟机内部文件等更多工具在libguestfs的网站可以查看到。
# virt-df -d ubuntu
Filesystem                           1K-blocks       Used  Available  Use%
ubuntu:/dev/sda                          10475520     235820   10239700    3%
#
 
使用举例3. API
支持的API类型有:C/C++, Erlang, Golang, Java, Lua, OCaml, Perl, Python, Ruby.
举例:
#test.py
import guestfs
g = guestfs.GuestFS(python_return_dict=True)
g.add_drive_opts("/tmp/test.img", readonly=1)
g.launch()
roots = g.inspect_os()
for root in roots:
    print "Root device: %s" % root
    mps = g.inspect_get_mountpoints(root)
    print "MPS: %s" % mps
 
# python test.py
Root device: /dev/sda1
MPS: {'/data': '/dev/vdb', '/': '/dev/sda1'}
#
 
工作原理
libguestfs进程使用qemu运行一个appliance作为它的子进程。
appliance使用supermin和host的kernel制作而成。
使用qemu-img制作目标磁盘的增量盘给appliance使用。
appliance内部运行guestfsd守护进程。
libguestfs进程和appliance内部的guestfsd通过socket和host进行通信,实现具体的功能。
 
qemu启动appliance过程如下:
qemu-img create -f qcow2 -o backing_file=rbd:rbd/test.img:mon_host=10.20.1.5\:6789\;10.20.1.6\:6789\;10.20.1.7\:6789:auth_supported=none,backing_fmt=raw /tmp/libguestfsXRFM98/overlay1
#使用目标虚拟机的磁盘创建一个增量盘
 
/usr/bin/qemu-system-x86_64 -global virtio-blk-pci.scsi=off -nodefconfig -enable-fips -nodefaults -display none -machine accel=kvm:tcg -cpu host -m 500 -no-reboot -rtc driftfix=slew -no-hpet -global kvm-pit.lost_tick_policy=discard -kernel /var/tmp/.guestfs-0/appliance.d/kernel -initrd /var/tmp/.guestfs-0/appliance.d/initrd -device virtio-scsi-pci,id=scsi -drive file=/tmp/libguestfsXRFM98/overlay1,cache=unsafe,format=qcow2,id=hd0,if=none -device scsi-hd,drive=hd0 -drive file=/var/tmp/.guestfs-0/appliance.d/root,snapshot=on,id=appliance,cache=unsafe,if=none -device scsi-hd,drive=appliance -device virtio-serial-pci -serial stdio -device sga -chardev socket,path=/tmp/libguestfsXRFM98/guestfsd.sock,id=channel0 -device virtserialport,chardev=channel0,name=org.libguestfs.channel.0 -append panic=1 console=ttyS0 udevtimeout=6000 udev.event-timeout=6000 no_timer_check acpi=off printk.time=1 cgroup_disable=memory root=/dev/sdb selinux=0 TERM=linux
#使用创建好的增量盘启动qemu实例
 
更多信息参考官网:
 

====================================================================

关注微信公众号【虚拟化云计算】,阅读更多虚拟化云计算知识,纯技术干货更新不停。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值