Virtual HardDisk Volume Creation Using a File in Linux

Today we will see How to create Virtual HardDisk Volume using file in Linux. So first off all we should know what is Virtual HardDisk and why we need it?

What is Virtual Hard Disk?

Virtual HardDisk  is a disk image file format which represents a virtual hard disk drive, which are able to store the complete contents of a physical hard drive. It’s a container file that acts similar to a physical hard drive.

As like a physical hard drive, a Virtual HardDisk can contains a file system, and we can use it to store and run an operating system, applications, as well as data store.

Now I am going to create 2GB Virtual HardDisk using file, After that we need to format this Virtual HardDisk and then we can mount this Virtual HardDisk to use like physical HardDisk. So let’s start step by step these process.

These steps I am go to perform in this tutorial:-

  1. Create a new image to hold Virtual HardDisk volume.
  2. Format the volume.
  3. Mount the volume.
  4. Delete the volume.

Create a new image to hold Virtual HardDisk volume

I am going to use dd command to create Virtual HardDisk here. You can also use fallocate command to create Virtual HardDisk from Linux terminal. Like below:-

[[email protected] ~]# dd if=/dev/zero of=myvhd.img bs=1M count=2400
2400+0 records in
2400+0 records out
2516582400 bytes (2.5 GB) copied, 4.18154 s, 602 MB/s
[[email protected] ~]#


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

Format the volume

Now we need to format this image type volume in ext4 file system mkfs utility. Answer y, when prompted that /root/myvhd.img is not a block special device as shown in the following screenshot.

[[email protected] ~]# mkfs -t ext4 /root/myvhd.img
mke2fs 1.42.9 (28-Dec-2013)
/root/myvhd.img is not a block special device.
Proceed anyway? (y,n) y
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
153824 inodes, 614400 blocks
30720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=629145600
19 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

[[email protected] ~]#

Mount the volume

Now we need to mount this volume, but before mount this volume we need to create mount directory where we can mount this volume. So first of all create the directory and mount this volume using below commands. We are using –o to specify option for mounting here the option loop indicates the device node under the /dev/ directory.

[[email protected] ~]# mkdir /mnt/urclouds-vhd
[[email protected] ~]#
[[email protected] ~]# mount -t auto -o loop /root/myvhd.img /mnt/urclouds-vhd/
[[email protected] ~]#

We can add below line in the /etc/fstab file to mount it at system boot.

/root/myvhd.img  /mnt/urclouds-vhd/  ext4    defaults        0  0


[[email protected] ~]# cat /etc/fstab

# /etc/fstab
# Created by anaconda on Tue Sep 26 12:44:35 2017
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos-root / xfs defaults 0 0
UUID=9b1b6c8c-a702-4654-8b65-3ea79c368a84 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/root/myvhd.img /mnt/urclouds-vhd/ ext4 defaults 0 0
[[email protected] ~]#

Now we can verify our newly created Virtual HardDisk file system with mount point using df –h command like below:-

[[email protected] ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 13G 4.9G 8.1G 38% /
devtmpfs devtmpfs 482M 0 482M 0% /dev
tmpfs tmpfs 497M 88K 497M 1% /dev/shm
tmpfs tmpfs 497M 7.1M 490M 2% /run
tmpfs tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 157M 341M 32% /boot
tmpfs tmpfs 100M 16K 100M 1% /run/user/0
/dev/loop0 ext4 2.3G 7.1M 2.2G 1% /mnt/urclouds-vhd
[[email protected] ~]#

You can see in above df –h command output we have successfully mounted 2.3GB Virtual HardDisk volume.

We can use this volume as like our physical hard disk volume like below:-

[[email protected] ~]# cd /mnt/urclouds-vhd/
[[email protected] urclouds-vhd]# ls -l
total 16
drwx------ 2 root root 16384 Dec 31 18:27 lost+found
[[email protected] urclouds-vhd]# mkdir test
[[email protected] urclouds-vhd]# ls -l
total 20
drwx------ 2 root root 16384 Dec 31 18:27 lost+found
drwxr-xr-x 2 root root 4096 Dec 31 18:40 test
[[email protected] urclouds-vhd]# touch test1
[[email protected] urclouds-vhd]# ls -l
total 20
drwx------ 2 root root 16384 Dec 31 18:27 lost+found
drwxr-xr-x 2 root root 4096 Dec 31 18:40 test
-rw-r--r-- 1 root root 0 Dec 31 18:40 test1
[[email protected] urclouds-vhd]#

Delete the volume

We can run the below commands if we don’t need the Virtual HardDisk volume.

[[email protected] ~]# umount /mnt/urclouds-vhd
[[email protected] ~]#
[[email protected] ~]# ls -l /root/myvhd.img
-rw-r--r-- 1 root root 2516582400 Dec 31 18:43 /root/myvhd.img
[[email protected] ~]# rm -rf /root/myvhd.img
[[email protected] ~]#

We need also remove entry from /etc/fstab file.

That’s all we have successfully created, mounted and deleted Virtual HardDisk volume using file in Linux.

One Comment

  1. Marshall Nagano said:

    Oh mate What would I express? Really… really loved the contentThank you so much!

    June 27, 2019

Leave a Reply

Your email address will not be published. Required fields are marked *