Site icon LinuxAndUbuntu

Creating Virtual Disks Using Linux Command Line

Creating Virtual Disks Using Linux Command Line

Creating Virtual Disks Using Linux Command Line

​Linux is indeed a great system with excellent tools at our disposal. There are lots of things that can be achieved using the terminal. One such activity is creating virtual hard drives. Your Linux system should already have the tools required to do this without the need for virtual machine software.  

NOTE: This tutorial only covers creating fixed-size disk images whose partitions can be mounted using Linux. Virtual machine programs like VirtualBox allow you to create dynamically expanding virtual drives that increase in size whenever necessary. If your goal is to create disks for programs such as VirtualBox, you need to use its tools.

Tools Requirements

​The following commands that you will need are:

You may use whatever tool you are comfortable with to achieve the same goal, of course.

Create The Image and Format Partitions

​Creating the image is simple using ‘dd’. All this will do is write zeros to a file of a specified size. For example, let’s create a 1GB image: $ dd if=/dev/zero of=1GB_HDD.img bs=1M count=1024 ​This will take a little time. You may choose a smaller or larger size if you wish.

Once completed, a partition should be created using fdisk. Because there is no partition table, one will be created. This is the DOS partition table. Let’s switch to a GPT table by entering ‘g’ into the prompt to create one. Now, create a partition by entering ‘n’. Accept all of the defaults. The partition created will be in a native Linux format that can be either ext2, ext3, or ext4. Then write the changes to the image by entering ‘w’.

fdisk linux partitioning tool

After the changes are written and fdisk closes, all that is required to do is format the partition running ‘mkfs.ext4’ on the image file itself to create an ext4 partition. It may ask you if you wish to proceed anyway if a GPT partition is found. If so, say yes.

$ mkfs.ext4 1GB_HDD.img
mkfs linux tool

If all went well, you can then proceed to setup a loop device for your image. This requires the use of ‘losetup’ (that is, loop setup). The command we wish to run will assign an available loop device (-f parameter to find one) to the partition on the image, and show the name of said loop device (–show parameter):​ $ sudo losetup -Pf –show 1GB_HDD.img

​If successful, you should be able to access the partition by either using ‘mount’ or through your file manager.  

​Images With Multiple Partitions

​That was how you create virtual drives with a single partition. What about images with two or more partitions? There are a few extra steps necessary, but once you know what to do, it should still be quite simple.​Begin by creating a 4GB image:

$ dd if=/dev/zero of=4GB_HDD.img bs=1M count=4096

​Use fdisk to create three Linux partitions with a GPT partition table. I chose the size of my partitions randomly. Feel free to choose the size of each partition yourself.

​Now we need to run ‘losetup’ to gain access to each partition by assigning loop devices to each one.

$ sudo losetup -Pf --show 4GB_HDD.img 

As before, we wanted to see what loop device was chosen. However, this time, the ‘-P’ parameter was useful in this case because it tells ‘losetup’ to scan the image for any partitions to create loop devices for. When the loop interfaces are created, have a look at ‘lsblk’ to see the created devices.

After that, each partition needs to be formatted before use, so run ‘mkfs’ to create them. Try running ‘mkfs.ext2’ on the first partition to create an ext2 filesystem. Then run ‘mkfs.ext4’ on the other two to create ext4 filesystems on the image. Once they’re formatted, you should be able to mount them via the command line or a file manager.

Finish partitions

​If you are finished with the partitions, simply run ‘losetup’ to remove the loop device you wish.

Conclusion

​To create a virtual drive with partitions on Linux is a very simple process. If you run into any trouble, do let me know in the comment section below this article. I’ll to respond as soon as possible.

Exit mobile version