{"id":1343,"date":"2019-01-07T12:17:37","date_gmt":"2019-01-07T06:47:37","guid":{"rendered":"http:\/\/jaipurhosting.com\/blog\/?p=1343"},"modified":"2019-04-29T15:17:36","modified_gmt":"2019-04-29T09:47:36","slug":"how-to-create-a-virtual-harddisk-volume-using-a-file-in-linux","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/how-to-create-a-virtual-harddisk-volume-using-a-file-in-linux\/","title":{"rendered":"How to Create a Virtual HardDisk Volume Using a File in Linux"},"content":{"rendered":"\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Description <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Virtual Hard Disk (VHD) is a disk image file format which represents a virtual hard disk drive, capable of storing the complete contents of a physical hard drive. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s a container file that acts similar to a physical hard drive. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The disk image replicates an existing hard drive and includes all data and structural features.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Just like a physical hard drive, a VHD can contains a file system, and you can use it to store and run an operating system, applications, as well as store data. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the typical uses of VHDs in VirtualBox Virtual Machines (VMs) to store operating systems and application, and data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will demonstrate how to create a virtual hard disk volume using a file in Linux. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This guide is useful for creating VHDs for testing purposes in your IT environment. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the purpose of this guide, we will create a VHD volume of size 1GB, and format it with EXT4 file system type.<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Create a New Image to Hold Virtual Drive Volume<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are number of ways you can do this, but the most easiest way is using the following dd command. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we will be creating a VHD volume of size 1GB image.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>sudo dd if=\/dev\/zero of=VHD.img bs=1M count=1200<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>if=\/dev\/zero: input file to provide a character stream for initializing data storage<\/li><li>of=VHD.img: image file to be created as storage volume<\/li><li>bs=1M: read and write up to 1M at a time<\/li><li>count=1200: copy only 1200M (1GB) input blocks<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we need to format the EXT4 file system type in the VHD image file with the mkfs utility. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Answer y, when prompted that \/media\/VHD.img is not a block special device as shown in the following screenshot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>sudo mkfs -t ext4 \/media\/VHD.img<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to access the VHD volume, we need to mount to a directory (mount point). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to access the VHD volume, we need to mount to a directory (mount point). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run these commands to create the mount point and mount the VHD volume, respectively. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The -o is used to specify options for mounting, here, the option loop indicates the device node under the \/dev\/ directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>sudo mkdir \/mnt\/VHD\/<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>sudo mount -t auto -o loop \/media\/VHD.img \/mnt\/VHD\/<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note: The VHD filesystem will only remain mounted until the next reboot, to mount it at system boot, add this entry in the \/etc\/fstab file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>\/media\/VHD.img  \/mnt\/VHD\/  ext4    defaults        0  0<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can verify the newly created VHD filesystem with mount point using the following df command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>df -hT<\/strong><\/em><\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Removing Virtual Drive Volume<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you don\u2019t need the VHD volume anymore, run the following commands to unmount the VHD filesystem, then delete the image file:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>sudo umount \/mnt\/VHD\/<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>sudo rm \/media\/VHD.img<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using the same idea, you can also <a href=\"http:\/\/jaipurhosting.com\/blog\/how-to-create-a-linux-swap-file\/\">create a swap area\/space using a file in Linux.<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We hope you\u2019ve found this useful!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description Virtual Hard Disk (VHD) is a disk image file format which represents a virtual hard disk drive, capable of storing the complete contents of a physical hard drive. It\u2019s a container file that acts similar to a physical hard drive. The disk image replicates an existing hard drive and includes all data and structural [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1344,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-categorized"],"_links":{"self":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1343"}],"version-history":[{"count":3,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1343\/revisions"}],"predecessor-version":[{"id":1352,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1343\/revisions\/1352"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1344"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}