在Linux宿主机上直接读写虚拟机磁盘文件

发布时间 2023-08-15 19:20:52作者: aij

主要是使用 guestmount 命令来挂载 KVM 磁盘镜像并从中提取文件,以下是具体操作步骤:

1、宿主机安装 libguestfs 工具集:

sudo apt-get install libguestfs-tools

sudo yum install libguestfs-tools
2、使用 guestmount 命令挂载 KVM 磁盘镜像:

sudo guestmount -a /path/to/kvm-image-file.qcow2 -m /dev/sda1 /mnt
在此命令中,/path/to/kvm-image-file.qcow2 是您要挂载的磁盘镜像文件的路径; /dev/sda1 是磁盘上想要挂载的分区设备名称,可以通过 fdisk -l kvm-image-file.qcow2 命令来查看。 /mnt 是用于挂载磁盘镜像内容的目录。

sudo guestmount -a /path/to/kvm-image-file.qcow2 -i --rw /mnt

自动检测文件系统并进行挂载

3、挂载成功后,您可以访问 /mnt 目录并将其中的数据复制到任何其他目录中:

sudo cp -r /mnt/path/to/file /path/to/destination
在此命令中,/mnt/path/to/file 是在虚拟机磁盘镜像中的文件路径;/path/to/destination 是要将文件复制到的目标路径。

复制完成后,确保安全卸载磁盘镜像并卸载挂载目录:

sudo umount /mnt
现在,您应该可以通过 /path/to/destination 目录中找到从 KVM 磁盘镜像提取出来的文件。