Mount virtual disk files such as VHD under linux

  • 2020-05-30 21:59:09
  • OfStack

1. RAW virtual disk

Virtual disk image files in raw format can be mounted directly under linux.

For example, here you create a file with the dd command and format it as ext4 Format (only 1 partition) and then mount to /mnt Directory.

The following raw.img The disk image file is only one partition, so it is not used offset= To specify the offset. If you have multiple partitions, you can mount them by specifying an offset. You can refer to the details mount The related parameter information of the command.

> dd if=/dev/zero of=raw.img bs=1M count=512
 Record the 512+0  The read 
 Record the 512+0  The write 
536870912 bytes (537 MB, 512 MiB) copied, 0.207045 s, 2.6 GB/s

/home/o [o@o-pc] [10:29]
> mkfs.ext4 -q raw.img 

/home/o [o@o-pc] [10:30]
> sudo mount -o loop raw.img /mnt

/home/o [o@o-pc] [10:30]
> df -h
 The file system      capacity   Has been used   available   Has been used %  The mount point 
tmpfs      3.9G  79M 3.8G  2% /dev/shm
/dev/sda4    30G 6.6G  24G  22% /
tmpfs      3.9G 136K 3.9G  1% /tmp
/dev/sda2    69G  26G  41G  39% /home
tmpfs      794M  36K 794M  1% /run/user/1000
/dev/loop0   488M 780K 452M  1% /mnt

2, VHD/VHDX disk file mount

linux does not directly support mounting VHD disk image files. You can go through vmware vmware-mount Wait for tools to mount. vmware Not provided directly with this tool, but in vmware player and vmware workstation Both are available. But I'm not going to do that here.

qemu-nbd is used to mount the disk image file.

Install qemu a)

First, install 1 qemu-kvm I'm using theta here /mnt0 , the installation command is as follows

sudo dnf install qemu-kvm 

If you are using debian/ubuntu etc., you can use it sudo apt-get install qemu-kvm Install.

archlinux is available sudo pacman -S qemu Install.

b) loads the nbd driver

NBD (Network Block Device) is 网络块设备 The abbreviations. This module can use the disk space of a remote host (as opposed to mounting nfs) as a local block device.

NBD is a kernel module that is already included in most of the Linux distributions, so there is no need to install it again.

Use modprobe to load the nbd driver

/media/o/data [o@o-pc] [11:04]
> sudo modprobe nbd max_part=8

Once loaded, you can use the modinfo command to view the module information

/media/o/data [o@o-pc] [11:05]
> modinfo nbd
filename:    /lib/modules/4.9.6-200.fc25.x86_64/kernel/drivers/block/nbd.ko.xz
license:    GPL
description:  Network Block Device
intree:     Y
vermagic:    4.9.6-200.fc25.x86_64 SMP mod_unload 
signat:     PKCS#7
sig_hashalgo:  md4
parm:      nbds_max:number of network block devices to initialize (default: 16) (int)
parm:      max_part:number of partitions per device (default: 0) (int)

The information above says that the number of network block devices initialized is 16, indicating that it is in /dev/ Create 16 nbd devices.

/media/o/data [o@o-pc] [11:05]
> ls /dev/nbd*
/dev/nbd0 /dev/nbd0p1 /dev/nbd1 /dev/nbd10 /dev/nbd11 /dev/nbd12 /dev/nbd13 /dev/nbd14 /dev/nbd15 /dev/nbd2 /dev/nbd3 /dev/nbd4 /dev/nbd5 /dev/nbd6 /dev/nbd7 /dev/nbd8 /dev/nbd9

c) connect the vhdx file to the nbd device

Used here qemu-nbd To connect (connect with -c parameter, disconnect with -d parameter)

/media/o/data [o@o-pc] [11:05]
> sudo qemu-nbd -c /dev/nbd0 VS2017RC-offline.vhdx 

Use after connection fdisk View 1 for device information.

/media/o/data [o@o-pc] [11:05]
> sudo fdisk -l /dev/nbd0
Disk /dev/nbd0 : 100 GiB . 107374182400  Bytes, 209715200  A sector 
 Unit: sector  / 1 * 512 = 512  byte 
 Sector size ( logic / physical ) : 512  byte  / 512  byte 
I/O  The size of the ( The minimum / The best ) : 512  byte  / 512  byte 
 Disk label type: dos
 Disk identifier: 0xa373e501
 equipment      Start the   The starting point      At the end of     sector    The size of the  Id  type 
/dev/nbd0p1   2048 209711103 209709056 100G 7 HPFS/NTFS/exFAT

In fact, the disk has only one partition, and the partition format is exFAT , the disk size for dynamic growth.

d) mount the partition

Direct use of mount Command to mount nbd0p1 Can be

/media/o/data [o@o-pc] [11:36]
> sudo mount -t exfat -o rw /dev/nbd0p1 /mnt
[sudo] o  Password: 
FUSE exfat 1.0.1

/media/o/data [o@o-pc] [12:05]
> ls /mnt/
'$RECYCLE.BIN' 'System Volume Information' vs2017rc  Installation instructions .txt

Install exFAT support

Because the partition is exFAT Formatted, cannot be mounted directly.

Install 1 fuse-exfat and exfat-utils .

The specific installation process says 1 briefly

Download the two rpm source packages first.


Then install fuse-devel and rpmbuild And unpack src.rpm The package.

sudo dnf install fuse-devel rpmbuild
sudo dnf install scons  #  build exfat-utils Need to be 
rpm -ivh exfat-utils-1.0.1-2.el6.src.rpm exfat-utils-1.0.1-2.el6.src.rpm

Decompression can be completed after the current user's home Under the table of contents rpmbuild Directory, enter the directory under SPECS Directory.

Then use the rpmbuild build offset=0 The package.

sudo dnf install qemu-kvm 

Enter after the build is complete offset=1 Directory (here x86_64 is related to your system architecture), generated by installation offset=0 The package.

sudo dnf install qemu-kvm 

You can install apt directly on ubuntu sudo apt install exfat-utils exfat-fuse

3. Mount other virtual disk files

Other than that, the VHD mount is identical to the VHD mount above, as long as it supports the disk image format.

Related articles: