Creating Virtual Block Devices

When playing with filesystems or setting up virtual machines, you may want to create virtual block devices (files that act similar to hard drives). Here I will explain the two ways to create such devices and the pros/cons of each

Normal Way

This is the normal way to create a block device and will create an 8 GiB pre-allocated device:
dd if=/dev/zero of=/path/to/dir/filename.img bs=1M count=8192
You may want to change the block size (bs), and change the count in order to change the capacity of the device.
The count x bs = capacity, so reducing the block size would reduce your capacity if you did not adjust the count accordingly.


  • Better performance than with the "sparse" method
  • Can't run out of space before the underlying device is full (dedicated).


  • This eats up your disk capacity very quickly. E.g. your disk is "full" after creating lots of these empty devices, and the majority of them may never reach half capacity.
  • Slow to create (has to write the capacity's worth in 0's to the physical drive)

Sparse Image

Create a "sparse" image with the following example command which creates a 100GB device
dd if=/dev/zero of=/path/to/dir/filename.img bs=1k count=1 seek=100M
The 100M is NOT meant to be 100GB
Running an ls -alh will clearly show the file as being 100G in size, but running a df -h on / shows that it is not used.


  • Almost instantaneous creation
  • Only data written to the image actually takes up space on your physical drive. Thus, you can oversell your physical drive.


  • Poorer performance when writing to.
  • May not be able to write to the device before it's capacity is reached because the underlying device has been filled.

No comments:

Post a Comment