How to Create and Use Virtual Hard Disk in Linux

March 22, 2023

Virtual Hard Disk (VHD) is a file that behaves like a physical Hard Disk. Like Hard Disk, it can be mounted to a computer. Like Hard Disk, it can be formatted with any file system. For example, you can have an EXT4 VHD on an NTFS-formatted Hard Disk.

There are many applications for Virtual Hard Disks. Personally, I use it for backing up my linux device in an NTFS partition. As you might already know, unlike ext4, NTFS cannot store all of a file's metadata (ex - permissions, execution bits, timestamps, etc.). So, I have created a VHD formatted with EXT4 inside an NTFS partition. For some reason, I cannot reformat my NTFS partition with ext4 because I don't want to lose my data and at the same time, I also want to mount that with my Windows OS. Now, I use this Virtual Hard Disk to backup my Linux device without losing any metadata.

In this article, I will talk about how to create it and how you can mount that manually and automatically during the system boot.

Steps to create Virtual Hard Disk

Step 1: Use the command dd to create a file

The command dd is available in all Linux distros. Generally, dd is used to create Live Boot Media. You can also use it to create Virtual Hard Disks.

$ sudo dd if=/dev/zero of=<path_to_virtual_hard_disk> bs=<bytes> count=<count>

Where, <path_to_virtual_hard_disk> is the path to a virtual hard disk; dd reads <bytes> at a time and it copies only <count> input blocks.

So, for example, to create a VHD of size 56 gigs:

sudo dd if=/dev/zero of=/mnt/crucial/linux_backup.img bs=1G count=56

Step 2: Format the file with the desired file system

Like any other hard disk partition, this file can be formatted. To format it with the ext4 file system, execute the following command:

$ sudo mkfs -t <file_system_type> <virtual hard disk>

So, for the above example, to format it with ext4, it will be:

$ sudo mkfs -t ext4 /mnt/crucial/linux_backup.img

Step 3: Mount the Virtual Hard Disk

In step 2, your VHD is prepared. Go ahead, and try to mount/unmount it, it will work.

$ sudo mount -o loop <path_to_virtual_hard_disk> <location_to_mount> 

For example, to mount it in the directory /mnt/VHD/, first create the directory, and then mount it using the following command:

$ sudo mount -t auto -o loop /mnt/crucial/VHD.img /mnt/VHD/

Where, -o in the mount command is used to supply all the options you want. You can learn these mount options in my fstab article. loop is used to mount the file to the loop device (/dev/loopn) where n is the next remaining loop number. You can use loop=/dev/loop0 to mount at the loop0.

Now, you can check whether the virtual hard disk is mounted or not using the lsblk command:

$ lsblk --list        
NAME      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0       7:0    0    56G  0 loop /mnt/VHD
...

To mount the VHD automatically during the boot, create a fstab entry. For that open the fstab file using your favorite text editor.

$ sudo nvim /etc/fstab

And add the following line at the end:

<path_to_virtual_hard_disk>    <location_to_mount> <file_system_type>  <mount_options> 0   0

For the above examples, this fstab line will look like the following:

/mnt/crucial/linux_backup.img    /mnt/VHD    ext4    defaults,nofail 0   0

Conclusion

That's all folks. This was the basic introduction to virtual hard disk and how to use it. If you have any queries or suggestions put them in the comment section below.