网站建设平台排名,查看网站建站时间,东莞正规网页设计培训学费,wordpress邮箱注册功能文章目录 简介guestfishlibguestfs项目 例子原理代码libguestfs架构参考 简介
guestfish
Guestfish 是libguestfs项目中的一个工具软件#xff0c;提供修改虚机镜像内部配置的功能。它不需要把虚机镜像挂接到本地#xff0c;而是为你提供一个shell接口#xff0c;你可以查… 文章目录 简介guestfishlibguestfs项目 例子原理代码libguestfs架构参考 简介
guestfish
Guestfish 是libguestfs项目中的一个工具软件提供修改虚机镜像内部配置的功能。它不需要把虚机镜像挂接到本地而是为你提供一个shell接口你可以查看、编辑和删除镜像内的文件。
Guestfish提供了结构化的libguestfs API访问可以通过shell脚本、命令行或交互方式访问。它使用libguestfs并公开了guestfs API的所有功能。Libguestfs是一个用于访问和修改磁盘映像和虚拟机的库。
libguestfs项目
从官网的描述看libguestfs项目是一些用来操作虚拟机镜像的工具和库的集合从描述看在镜像管理方面很强大。
例子
一个简单查看镜像文件系统的例子
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 shellfs help
Add disk images to examine using the ‘-a’ or ‘-d’ options, or the ‘add’
command.
Or create a new disk image using ‘-N’, or the ‘alloc’ or ‘sparse’ commands.
Once you have done this, use the ‘run’ command.For more information about a command, use ‘help cmd’.To read the manual, type ‘man’.fs add linux.qcow2
fs run100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
fs list-filesystems
/dev/sda1: vfat
/dev/sda2: ext4
/dev/sda3: swap
fs 可以看出通过guestfish可以列出镜像里面的文件系统。
原理
在运行guestfish查看镜像的过程中后台ps查看下qemu进程可以找到这样一个进程
/usr/bin/qemu-system-x86_64 -global virtio-blk-pci.scsioff -no-user-config -nodefaults -display none -machine accelkvm:tcg -cpu max,la57off -m 1280 -no-reboot -rtc driftfixslew -no-hpet -global kvm-pit.lost_tick_policydiscard -kernel /var/tmp/.guestfs-1000/appliance.d/kernel -initrd /var/tmp/.guestfs-1000/appliance.d/initrd -object rng-random,filename/dev/urandom,idrng0 -device virtio-rng-pci,rngrng0 -device virtio-scsi-pci,idscsi -drive file/home/czw/vm/img/linux.qcow2,cachewriteback,idhd0,ifnone -device scsi-hd,drivehd0 -drive file/var/tmp/.guestfs-1000/appliance.d/root,snapshoton,idappliance,cacheunsafe,ifnone,formatraw -device scsi-hd,driveappliance -device virtio-serial-pci -serial stdio -chardev socket,path/run/user/1000/libguestfsdU5W8O/guestfsd.sock,idchannel0 -device virtserialport,chardevchannel0,nameorg.libguestfs.channel.0 -append panic1 consolettyS0 eddoff udevtimeout6000 udev.event-timeout6000 no_timer_check printk.time1 cgroup_disablememory usbcore.nousb cryptomgr.notests tscreliable 8250.nr_uarts1 rootUUID3c1ee3f6-e2da-41b9-ac29-ca925bafb519 selinux0 quiet TERMxterm-256color
这个进程让guestfish有了能够操作镜像的能力分析这个qemu进程的参数
指定虚机kernel为guestfish自己的指定initrd为guestfish自己的指定镜像文件为所操作的镜像文件关联一个本地socket文件/run/user/1000/libguestfsdU5W8O/guestfsd.sock
进一步查看这个本地socket文件谁在使用 lsof /run/user/1000/libguestfsdU5W8O/guestfsd.sock
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
guestfish 18535 czw 6u unix 0x00000000649fd22e 0t0 192221 /run/user/1000/libguestfsdU5W8O/guestfsd.sock typeSTREAM (CONNECTED)
至此可以大致猜测出guestfish的实现原理
使用自己定制的内核启动虚拟机并将待操作的磁盘文件分配给虚拟机这里称该虚拟机为guest通过本地socket文件guestfish进程可以与guest中的某个程序通信从而通过guest中的程序对磁盘文件进行操作当guestfish退出的时候自动销毁guest
代码
为了验证上面实验的猜想可以直接下载源代码guestfish是libguestfs项目的一部分直接clone整个项目
git clone https://github.com/libguestfs/libguestfscd libguestfsgit submodule update --initautoreconf -i./configure CFLAGS-fPICmake如果是debian/ubutnu的环境参考https://manpages.ubuntu.com/manpages/noble/ja/man1/guestfs-building.1.html安装编译依赖。 编译完在fish/.lib文件夹下就是gestfish可执行程序他的代码路径为fish/fish.c 简单看下代码核心是调用libguestfs库提供的guestfs_开头的函数详细的流程直接看代码就好了。
libguestfs架构
直接展示官方的架构图 大致思路就是libguestfs通过rpc与qemu虚机内部的guestfsd通信将对镜像的操作转换为通过guestfsd间接操作镜像。
参考
guestfish - the guest filesystem shell libguestfs guestfs-internals - architecture and internals of libguestfs