How to Create a Virtual Hard Disk Volume Using a File in Linux

Create a virtual hard disk volume using a file in Linux and start storing the components of your system even without bringing the physical drive into the scene. Read the following tutorial and learn how you can do it in minutes.

When we talk about Virtual Hard Disk (VHD), we’re looking at an image file format to help users in many situations. However, its prime capability is storing the elements that rest within your system’s general or physical storage. It is nothing but a container in the virtual form that acts pretty similar to how a physical hard drive operates. 

If you’re new to the concept of a virtual driver, wondering what’s it for is something obvious. Let me tell you, the best answer for creating a virtual hard disk volume using a file in Linux can be a lot. 

Several users choose this method to add swap space to their computers without creating a new physical hard-drive partition. Since VHDs act similar to a web-page file on the Windows OS, you can seamlessly craft a virtual hard disk for building a portable virtual solution to back up your files.

In the following article, I’ll walk you through the step-by-step on creating virtual hard drives for your Linux system.

Step-by-Step Guide to Create a Virtual HardDisk Volume Using a File in Linux

With the concept of virtual disks discussed, it is the perfect time to uncover the steps you need to follow to create a VHD volume using a file in Linux.

Step 1: Creating a New Image for Holding the Virtual Drive Volume

To create a virtual hard disk volume using a file in Linux, the first thing that you’ll need to do is build an image file and employ the same to hold the virtual volume. Now, there are several ways to conclude the job. However, I’ll help you learn using the dd command to ensure you’ve got the easiest way out.

Depending on the volume you want to allocate, alter the values around the dd command. Suppose you’re after crafting a virtual hard drive that volumes to 1 GB, launch the Terminal by using the “Ctrl+Alt+T” key and invoke the following command:

Creating a New Image for Holding the Virtual Drive Volume


 • if=/dev/zero: Thus input file is meant to supply a specific character stream, especially for initializing the data storage in your Linux system.

 • of=MVHD.img: It reflects the focus image file, the one that will get created as your system’s volume for storage.

 • bs=1M/2M: The bs flag or the bs= option indicates the drive’s write and read speed. In this case, the value is 1M/2M.

 • count=(value)1200: 1200M (or 1 GB) input refers to the virtual hard disk volume. You can desirably choose this value as per your needs.

Step 2: Formatting the EXT4 File

Once you’re done creating the required image file for holding the virtual driver, the next thing to do is format the existing EXT4 file that sits within the created image. The concerned system contains specific files, and it is crucial to get rid of them before moving further.

Doing this is pretty simple. Employ the mkfs command alongside the -t flag, and you’re good to go.

Run the following command:

Formatting the EXT4 File

You’ll then get prompted to verify your decision—input “Y” and press the “Enter key” on your keyboard. 

Step 3: Creating a Mount Point

Next up is accessing the created MVHD volume. To do that, mount a directory or, in other words, build a mount point.  

Invoke the mkdir and mount command inside the Terminal in the following ways:

$ sudo mkdir /mnt/MVHD/
$ sudo mount -t auto -o loop /media/MVHD.img /mnt/MVHD/
creating a mount point

Remember, the -o flag helps specify options corresponding to the mounting scene like I’ve used loop here. If you don’t know, the loop option indicates the situation when the concerned device node is under the directory: /dev/.

Mounting the Created Image File at System Boot

By default, the created filesystem remains mounted until you decide to reboot your system. So, in case you don’t want it to happen and rather mount the file at the very system boot, all that needs to be done is add the following entry within the file that looks something like this:

/media/MVHD.img /mnt/MVHD/ ext4  defaults 

Step 4: Verifying the Created Virtual System

Verifying whether or not you’ve successfully created the virtual system is crucial. For that, invoke the df command together with the -hT flag

$ df -hT
Create a Virtual Hard Disk Volume Using a File in Linux

That’s how you create a virtual hard disk volume using a file in Linux.

Removing the Virtual Drive from your System

Don’t need the virtual drive anymore? Well, the piece of good news is that you can seamlessly get rid of it in minutes. Yes, you read that right. Removing a virtual drive takes less time than creating one.

Start by unmounting the MVHD filesystem and deleting the created image file. Launch the Terminal using the “Ctrl+Alt+T” key and then run the following commands:

$ sudo unmount /mnt/MVHD/
$ sudo rm /media/MVHD.img
Remove a Virtual Hard Disk Volume in Linux

With that, you know how to create a virtual hard disk volume using a file in Linux. Although the process is simple, it can be troublesome when the appropriate approach is unknown. To help you with that, I’ve presented the most user-friendly guide ever. For queries, feel free to leave a comment below.

If this guide helped you, please share it.

Leave a Reply
Related Posts