Create a Virtual HardDisk (VHD) Volume Using a File in Linux

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Introduction

Virtual Hard Disk is a disk image file format which represents a virtual hard disk drive, It’s a container file that acts similar to a physical hard drive.

VHD can contains a file system, and you can use it to store and run an operating system, applications, as well as store data.

We will illustrate how to create a virtual hard disk volume using a file in Linux. we will create a VHD volume of size 1GB, and format it with ext4 file system type.

Create an Image to be the Virtual Drive

We can use the following dd command to do this.

So we will create a VHD volume of size 1GB image.

[root@unixcop ~]# dd if=/dev/zero of=/media/unixcop.img bs=1M count=1200
1200+0 records in
1200+0 records out
1258291200 bytes (1.3 GB) copied, 18.8613 s, 66.7 MB/s
[root@unixcop ~]#

Where:

  • if=/dev/zero: input file to provide a character stream for initializing data storage
  • of=unixcop.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

We need to format the ext4 file system type in the VHD image file with the mkfs command.

[root@unixcop ~]# mkfs -t ext4 /media/unixcop.img
mke2fs 1.42.9 (28-Dec-2013)
/media/unixcop.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
76800 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=314572800
10 block groups
32768 blocks per group, 32768 fragments per group
7680 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

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

[root@unixcop ~]#

Also we need to mount to a directory .

To create the mount point and mount the VHD volume, run the following commands.

[root@unixcop ~]# mkdir /mnt/unixcop
[root@unixcop ~]# mount -t auto -o loop /media/unixcop.img /mnt/unixcop/
  • The -o is used to specify options for mounting.
  • the option loop indicates the device node under the /dev/ directory.

To mount the VHD at system boot, add this entry in the /etc/fstab file.

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

You can see its form from the below screenshot.

Finally, You can verify the new VHD filesystem with mount point using the following command:

# df -h

How to remove VHD volume

If you don’t need the VHD volume anymore, use these commands to unmount and remove it.

# umount /mnt/unixcop/
# rm /media/unixcop.img

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
MQ-Jr
MQ-Jr
unixcop Admin

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook