{"id":2570,"date":"2023-03-22T17:03:41","date_gmt":"2023-03-22T11:33:41","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2570"},"modified":"2023-03-22T17:16:16","modified_gmt":"2023-03-22T11:46:16","slug":"how-to-create-and-use-virtual-hard-disk-in-linux","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/how-to-create-and-use-virtual-hard-disk-in-linux\/","title":{"rendered":"How to Create and Use Virtual Hard Disk in Linux"},"content":{"rendered":"\n
Virtual Hard Disk (VHD) is a file that behaves like a physical Hard Disk. Like Hard Disk, it can be mounted to a computer. Like Hard Disk, it can be formatted with any file system. For example, you can have an EXT4 VHD on an NTFS-formatted Hard Disk.<\/p>\n\n\n\n
There are many applications for Virtual Hard Disks. Personally, I use it for backing up my linux device<\/a> in an NTFS partition. As you might already know, unlike ext4, NTFS cannot store all of a file’s metadata (ex – permissions, execution bits, timestamps, etc.). So, I have created a VHD formatted with EXT4 inside an NTFS partition. For some reason, I cannot reformat my NTFS partition with ext4 because I don’t want to lose my data and at the same time, I also want to mount that with my Windows OS. Now, I use this Virtual Hard Disk to backup my Linux device without losing any metadata.<\/p>\n\n\n\n In this article, I will talk about how to create it and how you can mount that manually and automatically during the system boot.<\/p>\n\n\n\n The command Where, So, for example<\/strong>, to create a VHD of size 56 gigs:<\/p>\n\n\n\n Like any other hard disk partition, this file can be formatted. To format it with the ext4 file system, execute the following command:<\/p>\n\n\n\n So, for the above example<\/strong>, to format it with ext4, it will be:<\/p>\n\n\n\n In step 2, your VHD is prepared. Go ahead, and try to mount\/unmount<\/a> it, it will work.<\/p>\n\n\n\n For example, to mount it in the directory Where, Table of Contents<\/h2>\n\n\n\n
\n
\n
Steps to create Virtual Hard Disk<\/h2>\n\n\n\n
Step 1: Use the command dd to create a file<\/h3>\n\n\n\n
dd<\/code> is available in all Linux distros. Generally, dd is used to create Live Boot Media<\/a>. You can also use it to create Virtual Hard Disks.<\/p>\n\n\n\n
$ sudo dd if=\/dev\/zero of=<path_to_virtual_hard_disk> bs=<bytes> count=<count><\/code><\/pre>\n\n\n\n
<path_to_virtual_hard_disk><\/code> is the path to a virtual hard disk;
dd<\/code> reads
<bytes><\/code> at a time and it copies only
<count><\/code> input blocks.<\/p>\n\n\n\n
sudo dd if=\/dev\/zero of=\/mnt\/crucial\/linux_backup.img bs=1G count=56<\/code><\/pre>\n\n\n\n
Step 2: Format the file with the desired file system<\/h3>\n\n\n\n
$ sudo mkfs -t <file_system_type> <virtual hard disk><\/code><\/pre>\n\n\n\n
$ sudo mkfs -t ext4 \/mnt\/crucial\/linux_backup.img<\/code><\/pre>\n\n\n\n
Step 3: Mount the Virtual Hard Disk<\/h3>\n\n\n\n
$ sudo mount -o loop <path_to_virtual_hard_disk> <location_to_mount> <\/code><\/pre>\n\n\n\n
\/mnt\/VHD\/<\/code>, first create the directory, and then mount it using the following command:<\/p>\n\n\n\n
$ sudo mount -t auto -o loop \/mnt\/crucial\/VHD.img \/mnt\/VHD\/<\/code><\/pre>\n\n\n\n
-o<\/code> in the
mount<\/code> command is used to supply all the options you want. You can learn these mount options<\/a> in my fstab article.
loop<\/code> is used to mount the file to the loop device (
\/dev\/loopn<\/code>) where n is the next remaining loop number. You can use
loop=\/dev\/loop0<\/code> to mount at the
loop0<\/code>.<\/p>\n\n\n\n