Virtual Hard Disk (VHD) is a disk image file format which represents a virtual hard disk drive, capable of storing the complete contents of a physical hard drive. It’s a container file that acts similar to a physical hard drive. The disk image replicates an existing hard drive and includes all data and structural features.
Just like a physical hard drive, a VHD can contains a file system, and you can use it to store and run an operating system, applications, as well as store data. One of the typical uses of VHDs in VirtualBox Virtual Machines (VMs) to store operating systems and application, and data.
In this article, we will demonstrate how to create a virtual hard disk volume using a file in Linux. This guide is useful for creating VHDs for testing purposes in your IT environment. For the purpose of this guide, we will create a VHD volume of size 1GB, and format it with EXT4 file system type.
Create a New Image to Hold Virtual Drive Volume
There are number of ways you can do this, but the most easiest way is using the following dd command. In this example, we will be creating a VHD volume of size 1GB image.
$ sudo dd if=/dev/zero of=VHD.img bs=1M count=1200
- if=/dev/zero: input file to provide a character stream for initializing data storage
- of=VHD.img: image file to be created as storage volume
- bs=1M: read and write up to 1M at a time
- count=1200: copy only 1200M (1GB) input blocks
Next, we need to format the EXT4 file system type in the VHD image file with the mkfs utility. Answer
y, when prompted that /media/VHD.img is not a block special device as shown in the following screenshot.
$ sudo mkfs -t ext4 /media/VHD.img
In order to access the VHD volume, we need to mount to a directory (mount point). Run these commands to create the mount point and mount the VHD volume, respectively. The
-o is used to specify options for mounting, here, the option loop indicates the device node under the /dev/ directory.
$ sudo mkdir /mnt/VHD/ $ sudo mount -t auto -o loop /media/VHD.img /mnt/VHD/
Note: The VHD filesystem will only remain mounted until the next reboot, to mount it at system boot, add this entry in the /etc/fstab file.
/media/VHD.img /mnt/VHD/ ext4 defaults 0 0
Now you can verify the newly created VHD filesystem with mount point using the following df command.
$ df -hT
Removing Virtual Drive Volume
If you don’t need the VHD volume anymore, run the following commands to unmount the VHD filesystem, then delete the image file:
$ sudo umount /mnt/VHD/ $ sudo rm /media/VHD.img
Using the same idea, you can also create a swap area/space using a file in Linux.
That’s all! In this guide, we have demonstrated how to create a virtual hard disk volume using a file in Linux. If you have any thoughts to share or questions to ask, reach us via the comment form below.
10 thoughts on “How to Create a Virtual HardDisk Volume Using a File in Linux”
I tried this method, but the mounted drive/container remains read-only… I could not find how to enable write permission.
I’m not sure it’s fair to call this a VHD? since VHD is a file extension specifically used by Microsoft’s VirtualPC and Hyper-V hypervisor. What this creates is a bare image that doesn’t have whatever magic makes a Microsoft VHD work on a Windows machine.
VHD merely stands for “Virtual Hard Disk“, just as VMDK stands for “Virtual Machine Disk“. All of which are nothing but a file inside a partition, on a physical disk, pretending to be a real Hard/Machine disk. We just don’t use the word “pretend disk” in I.T., instead of calling them “virtual disks”.
VHD/VMDK files have metadata containing additional information such as the logical C/H/S layout, bytes per sector, etc.
VHD is a well-known and well-established format. Mention ‘VHD‘ to pretty much any IT professional, and they will assume you are talking about the Connectix/Microsoft format and NOT a generic disk image file.
How to increase the size of this drive ?
If your disk is not containing anything that is required at boot, you could create a new, bigger disk, copy the content across, then unmounted and mount the new one, not deleting the old one until you confirm the new one works. Within Linux there are a number of tools to help you manage hard disks, such as fdisk, gdisk, GParted, sdparm, tune2fs, etc…. you just need to find the right tool for the job. Hope that helps.
Are there any tools other than dd to create a virtual hard disk in Linux?
I store my VHD on a file share because there will be cases where I need to umount from Server 1 and then mount from Server 2. The risk here is if Server 2 ever mounts and writes before the umount from Server 1. Is there a way to fully prevent Server 2 from being able to mount if it’s already mounted by another server?
Is it possible to create the image file on another disk than the drive the command is issued from?
In essence, what I’d like to accomplish is this:
Yes, it should work well as long as the other disk is properly mounted in the file system.