如何挂载虚拟硬盘?


Answers:


16

根据这篇文章

Linux和其他类似Unix的主机可以使用环回设备挂载以原始格式类型创建的映像。通过root登录(或使用sudo),安装偏移量为32,256的环回。

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

对于其他类型的qemu图像,可以使用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的方式”命令相同。无国界!


6

这是在Ubuntu 16.04上

作为根:

使用仿制品安装和安装。

apt-get install afflib-tools

affuse /path/file.vmdk /mnt/vmdk

检查扇区大小

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

将扇区大小乘以startector。例如它将是2048 * 512

echo 2048*512 | bc
1048576

使用该偏移量挂载

mount -o ro,loop,offset=1048576 /mnt/vmdk/file.raw /mnt/vmdisk

现在应该在/ mnt / vmdisk上挂载磁盘并可读


1
大!!!是在Ubuntu 17.10上为我完成的操作
cljk

在16.04.5上,这对我不起作用,因为我的.vmdk ...通过fdisk步骤工作,而我要挂载的VM的主分区也从2048开始,但mount -o ro,loop,offset=1048576 ./foo.raw /mnt/foo失败only root can use "--options" option。使用sudo,则失败failed to setup loop device: Permission denied
西奥多·默多克

3

您也可以使用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:特殊设备/ dev / nbd0p1不存在”

对于.ova

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

上面将提取.vmdk磁盘,然后将其挂载。


2

对于vmdkvhd文件,仅使用以下kpartx命令我很幸运:

sudo kpartx -a -v <image-flat.vmdk>

检查输出是否为losetup,它应该包含回路设备/dev/loop0; 还检查sudo blkid分区/dev/mapper/loop0p1,然后在mount命令中使用它:

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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.