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.
For more details you can Click-Here
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:-
- Create a new image to hold Virtual HardDisk volume.
- Format the volume.
- Mount the volume.
- 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:-
[root@urclouds ~]# 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 [root@urclouds ~]#
Here:
- 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.
[root@urclouds ~]# 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 [root@urclouds ~]#
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.
[root@urclouds ~]# mkdir /mnt/urclouds-vhd [root@urclouds ~]# [root@urclouds ~]# mount -t auto -o loop /root/myvhd.img /mnt/urclouds-vhd/ [root@urclouds ~]#
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
Like:-
[root@urclouds ~]# 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 [root@urclouds ~]#
Verify created Virtual HardDisk Volume file system
Now we can verify our newly created Virtual HardDisk Volume file system with mount point using df –h command like below:-
[root@urclouds ~]# 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 [root@urclouds ~]#
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:-
[root@urclouds ~]# cd /mnt/urclouds-vhd/ [root@urclouds urclouds-vhd]# ls -l total 16 drwx------ 2 root root 16384 Dec 31 18:27 lost+found [root@urclouds urclouds-vhd]# mkdir test [root@urclouds 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 [root@urclouds urclouds-vhd]# touch test1 [root@urclouds 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 [root@urclouds urclouds-vhd]#
Delete the volume
We can run the below commands if we don’t need the Virtual HardDisk volume.
[root@urclouds ~]# umount /mnt/urclouds-vhd [root@urclouds ~]# [root@urclouds ~]# ls -l /root/myvhd.img -rw-r--r-- 1 root root 2516582400 Dec 31 18:43 /root/myvhd.img [root@urclouds ~]# rm -rf /root/myvhd.img [root@urclouds ~]#
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.
You can also check this link:-
Oh mate What would I express? Really… really loved the contentThank you so much!