create virtual hard disk in linux

How to Create Virtual Hard Disk Volume from File in Linux

A Virtual hard disk (VHD) is a disk image file format that represents a virtual hard disk drive. It can store the entire contents of a physical hard drive. It can replicate most behavior of a physical hard drive, including the data and structures of one. It contains a file system so it can be used to store operating system, data and applications. In this article, we will learn how to create virtual hard disk volume from file in Linux. It is useful in IT environment. For our purpose, we will create a VHD of 1GB size with EXT4 file system.

How to Create Virtual Hard Disk Volume from File in Linux

Here are the steps to create VHD Volume. There are several tools to help you create virtual drives but we will use dd command for our purpose. Here is the command to create virtual disk of 1GB size.

$ sudo dd if=/dev/zero of=VHD.img bs=1M count=1200

In the above command,

  • if=/dev/zero: input file to provide character stream for initializing data storage
  • of=VHD.img: image file to be created as storage volume
  • bs=1M: read & write up to 1M at a time
  • count=1200: copy 1200M (1GB) input blocks

Next we will use mkfs tool to format the VHD image to EXT4 format. Enter y when prompted that /media/VHD.img is not a block device as shown in screenshot.

$ sudo mkfs -t ext4 /media/VHD.img

Next, in order to access VHD image, we need to mount it to a specific directory (mount point). Run the following commands to create mount point and mount VHD volume.

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

In the above command, -o option is used to specify mounting options. The -o option indicates that the device is under /dev/ directory. The above command will mount the VHD image but only until the next system reboot. If you want to permanently mount it, then you need to edit /etc/fstab file. Open it in text editor.

$ vi /etc/fstab

Add the following line to it.

/media/VHD.img  /mnt/VHD/  ext4    defaults        0  0

Now verify that the VHD has been mounted with the following command.

$ df -hT

The above command’s output will display all mount points in your system, including the one for VHD at /mnt/VHD.

Remove Virtual Drive Volume

If you don’t need the virtual drive anymore, you can unmount it with the following command.

$ sudo umount /mnt/VHD/

Please note, the above command only unmounts the VHD image but does not remove the file. If you want to remove the file, you will need to use rm command for it.

$ sudo rm /media/VHD.img

In this article, we have learnt how to create virtual hard disk volume in Linux. You can use these steps on almost every Linux distribution.

A Virtual Hard Disk is just a file or part of your physical disk that behaves as another separate disk in your system, once it is mounted. So it has the characteristics similar to that of a physical drive – it has a file system and formatting. It can be used to store data as well as applications. It needs to be mounted before it is used and can be unmounted when not needed.

Also read:

How to Downgrade Software in Ubuntu
How to Downgrade RHEL/CentOS to Previous Minor Release
How to Mount Windows Partition in Ubuntu
Top Hex File Editors in Linux
How to Setup Two Factor Authentication in SSH

Leave a Reply

Your email address will not be published.