本文将介绍下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
    2
    3
    umount /mnt/somepoint/
    qemu-nbd -d /dev/nbd0
    rmmod nbd

参考资料:

  1. 记录一次将虚拟机kernel写坏之后的修复过程
  2. QEMU Disk Network Block Device Server
  3. Mount disk images using losetup
  4. man losetup