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:

  • ‘dd’ for creating the file. You can also bximage (part of the Bochs PC Emulator) if you wish.
  • ‘fdisk’ for creating partitions, or which ever partition program you like.
  • ‘mkfs’ for formatting the partitions.
  • ‘losetup’ for setting up the loop devices for each partition.

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.

create image and format partitions

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
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
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

losetup virtual hard disk space

​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 
losetup 4gb hdd

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.

lsblk linux tool

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.

losetup linux partitioning tool


​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.


MassiveGRID Banner
4 Comments Text
  • I am trying to understand vdisks as opposed to just partitions. I know I can create partitions, but I would like to create virtual disks.
    If I can create two vdisks, will Linux actually see the second vdisk as a disk or a partition?

    I am looking at this vdisk option since I have installed Ubuntu with zfs, and Ubuntu wants to use the entire disk. I use less than 30 gigs for my Linux needs. Ergo, I want to only allocate 50 gigs of a 120gig ssd to Ubuntu zfs, as my file system needs are less than 30gigs, leaving 20 gigs for snapshots.

    I do have an external disk storage, and I pull in files for my needs, and when I have used them, I choose to either delete them or write the updates back to the external disk.

    Zfs looks to be a more versatile file system and limiting it to one vdisk , mirrored on a second drive, would be just a great solution.

  • Hi: I do have a question concerning this post, I am new to linux and want to learn more. I learning how to do stuff using command line, in the first line about creating the drive you have written “dd if=/dev/zero of=1GB_HDD.img bs=1M count=1024”. I would like to make a 20GB drive, do I use the same numbers except for the 1GB and replace it with 20GB. Also what exactly do the individual commands mean,
    dd if=/dev/zero

    • James, it is not sufficient to change 1GB, because that is just the name of the img file.
      Any time you want to know how to use a command, you can look it up on Ubuntu manpages. For “dd”, it is:

      To answer your question:
      dd = data description utility
      if = input file (in this case a very specific file on your computer that is only zeros)
      of = output file (you pick the name and add .img)
      bs = “byte size”, or the number of bytes processed in one action (default is 512 bytes, OP chose 1M = 1Mebibyte = 1024 bytes); please note that a mebibyte is different from a megabyte. You can google the conversion if you want to understand this better.
      count = the number of times the action is taken

      To get the full size of the .img made by this command, you multiply the “bs” by “count”. in OP’s case, 1024 bytes (bs) * 1024 (count) = 1,048,576 bytes = 1024^2 bytes = 1 Gibibyte.

      In your case, you want a 20GB .img. Let’s use OP’s byte size of 1M (1024 bytes) and calculate how many times we need to do our file action. First convert your desired .img size using the google conversion 20GB into gibibytes (answer = 18.6265 G). Round off the number so it’s nice (19G). Now convert gibibytes into mebibytes using google again (answer = 19456 M) (alternately, there are 1024 mebibytes in a gibibyte, so you can just multiply 19 * 1024 to get mebibytes). If your desired .img size is 19456 mebibytes (20GB), and you process 1 mebibyte in an action, you need to do your action 19456 times (19456 / 1 = 19456). So your count = 19456.

      Your final command looks like this:
      dd if=/dev/zero of=any_name_will_do.img bs=1M count=19456

      Hope this helps!

  • Leave a Reply

    Your email address will not be published. Required fields are marked *