如何mount虚拟机镜像
本文将介绍下mount虚拟机镜像的方法。
losetup
losetup只能mount raw格式的镜像。
To check what is the first usable loop device, run
1 | losetup -f |
After that, use the output of that command to link the disk image to the loop device file (using root privileges):
1 | losetup -P /dev/loopX example.img |
The -P flag searches through the image for partitions, which you need to mount.
After that, create the folder named example and run the command:
1 | mount /dev/loopXpY example |
The disk image should now be mounted in that directory. Depending on the Y variable, the right partition was mounted.
qemu-nbd
Export a QEMU disk image using the NBD protocol.
qemu-nbd可以mount多种格式的虚拟机镜像,因此适用范围比losetup要广!
- Enable NBD on the host
1 | modprobe nbd max_part=8 |
- Connect the QCOW2 as a network block device
1 | qemu-nbd -c /dev/nbd0 /var/lib/vz/images/100/vm-100-disk-1.qcow2 |
- List partitions inside the QCOW2
1 | fdisk /dev/nbd0 -l |
- Mount the partition from the VM
1 | mount /dev/nbd0p1 /mnt/somepoint/ |
- After you’re done, unmount and disconnect
1 | umount /mnt/somepoint/ |
参考资料: