如何挂载虚拟硬盘?

在Ubuntu上是否可以挂载虚拟硬盘(VHD、HDD、VDI、VMDK)?如何操作?

1你有在Google上搜索过吗?那里有很多关于在Ubuntu上挂载VMDK、VDI、VHD和raw磁盘镜像文件的指南。 - SirCharlo
2我在谷歌上搜索过,但是没有找到和你的结果一样的东西。 谢谢 :) - Snow Leopard
Ubuntugeek上的VHD链接失败了。 - K7AAY
4个回答

根据这篇维基百科文章:这个维基百科文章所述:

Linux and other Unix-like hosts can mount images created with the raw format type using a loopback device. From a root login (or using sudo), mount a loopback with an offset of 32,256.

mount -o loop,offset=32256 /path/to/image.img /mnt/mountpoint

For other types of qemu images, you can use qemu-nbd

modprobe nbd max_part=16
qemu-nbd -c /dev/nbd0 image.qcow2
partprobe /dev/nbd0
mount /dev/nbd0p1 /mnt/image
通常情况下,您可以将图像从一种格式转换为另一种格式。
raw - (default) the raw format is a plain binary image of the disc 
       image, and is very portable. 
       On filesystems that support sparse files, 
       images in this format only use the 
       space actually used by the data recorded in them.
cloop -     Compressed Loop format, mainly used for reading Knoppix 
       and similar live CD image formats
cow - copy-on-write format, supported for historical reasons only and
       not available to QEMU on Windows
qcow - the old QEMU copy-on-write format, supported for 
       historical reasons and superseded by qcow2
qcow2 - QEMU copy-on-write format with a range of special features, 
       including the ability to take multiple snapshots, smaller 
       images on filesystems that don't support sparse files, 
       optional AES encryption, and optional zlib compression
vmdk - VMware 3 & 4, or 6 image format, for exchanging images 
       with that product
vdi - VirtualBox 1.1 compatible image format, for exchanging 
       images with VirtualBox.

我在搜索时找到了关于(VirtualBox).VDI的解决方案,链接在这个网站上。
modprobe nbd max_part=16
qemu-nbd -c /dev/nbd0 /path/to/some.vdi
mount -o loop /dev/nbd0p1 /mnt
# do stuff
umount /mnt
qemu-nbd -d /dev/nbd0
rmmod nbd
与“Qemu的方式”命令相同。没有边界!

截至今天(2022年11月),这个解决方案仍然有效!也许值得补充的是,所需的分区不一定是nbd0p1。列出所有分区:ls /dev/nbd0*,然后逐个尝试挂载它们(如果得到不想要的结果,请执行umount /mnt/image)。 - Pawel

这是在Ubuntu 16.04上。
使用affuse安装和挂载:
sudo apt-get install afflib-tools
sudo affuse /path/file.vmdk /mnt/vmdk

检查扇区大小:
sudo fdisk -l /mnt/vmdk/file.vmdk.raw

# example

Disk file.vmdk.raw: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000da525

Device       Boot Start      End  Sectors Size Id Type
/mnt/vmdk/file.vmdk.raw1 *     2048 41943039 41940992  20G 83 Linux

将扇区大小和起始扇区相乘。在这个例子中,它将是2048*512。
$ echo 2048*512 | bc
1048576

使用该偏移量安装:
sudo mount -o ro,loop,offset=1048576 /mnt/vmdk/file.raw /mnt/vmdisk

磁盘现在应该已经挂载并可读取在/mnt/vmdisk上。

1太棒了!在Ubuntu 17.10上为我完成了。 - cljk
这在我的16.04.5版本上对我的.vmdk文件不起作用...通过fdisk步骤可以工作,并且我要挂载的虚拟机的主分区也从2048开始,但是mount -o ro,loop,offset=1048576 ./foo.raw /mnt/foo失败,并显示只有root用户可以使用“--options”选项。如果使用sudo,则会显示设置回环设备失败:权限被拒绝 - Theodore Murdock
至少在Ubuntu 22.04上,affuse不支持vmdk文件。 - Daniel Alder

你也可以使用qemu:
对于.vdi文件:
sudo modprobe nbd
sudo qemu-nbd -c /dev/nbd1 ./linux_box/VM/image.vdi

如果它们没有安装,你可以安装它们(在Ubuntu上是这个命令)。
sudo apt install qemu-utils

然后将其安装上去
mount /dev/nbd1p1 /mnt

对于 .vmdk

sudo modprobe nbd
sudo qemu-nbd -r -c /dev/nbd1 ./linux_box/VM/image.vmdk

请注意,我使用选项-r,这是因为VMDK版本3必须是只读的才能够被qemu挂载。
然后我将其挂载。
mount /dev/nbd1p1 /mnt

我使用 nbd1 是因为 nbd0 有时会出现 'mount: special device /dev/nbd0p1 does not exist' 的错误。

对于 .ova 文件

tar -tf image.ova
tar -xvf image.ova

以上将提取.vmdk磁盘,然后挂载它。

对于 vmdkvhd 文件,我只能依靠下面的 kpartx 命令来运气。
sudo kpartx -a -v <image-flat.vmdk>

检查losetup的输出,它应该包含循环设备/dev/loop0;还要检查sudo blkid中的分区/dev/mapper/loop0p1,然后在挂载命令中使用它。
sudo mount -o rw /dev/mapper/loop0p1 /mnt/vmdk

在/mnt/vmdk是你的挂载点,在不存在的情况下,可以使用sudo mkdir /mnt/vmdk命令创建。

在commandlinefu.com上查看源代码(kpartx和mount命令)

卸载使用以下命令:

sudo umount /mnt/vmdk
sudo kpartx -d -v <image-flat.vmdk>

刚刚用“vhd”测试了这种方法,它有效! - N0rbert
刚刚用vmdk进行了测试,但是没有成功。看起来kpartx只支持原始镜像(不带元数据和节省空间的技巧)。这也是有道理的,因为kpartx只是一个对losetup(回环设备)的分区感知包装器,而这些设备只能支持原始文件。 - Daniel Alder