How to create & manage loop devices(virtual block) on Linux

When you run the lsblk -l or df -h command to list out the available block/storage devices, you may find the /dev/loop filesystem, especially if you are using the Snap package manager, and it’s quite common to find in Ubuntu distributions.

If you are keen on knowing what loop devices are and how to mount and unmount them from the system, then this article will be enough for you.

because it will cover all the necessary aspects of loop devices, like how to create a loop device, loop filesystem, mounting, and unmounting steps.

What is a loop device or virtual block filesystem?

Over physical storage, a loop device is a virtual block of storage on the host mounted as the /dev/loop directory, and it is mounted as a regular block device.

The virtual block of storage can be formatted depending on the device’s filesystem, which can be Ext4, Ext3, Ext2, or other major filesystems.

Generally, you can think of a loop device as an additional partition on the host machine, similar to swap memory. Obviously, you may think, why use a loop device? The reason is simple: you can share or mount the loop device at any time to access a file by mounting the loop drive.

In the next section, you will see how to create and manage loop devices using losetup.

What is losetup?

A losetup utility is responsible for creating a new loop device, and if you just run “losetup” without any arguments, it will list out all the loop devices that are available on your system.

$ losetup
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                        DIO LOG-SEC
/dev/loop1         0      0         1  1 /var/lib/snapd/snaps/gnome-3-28-1804_161.snap      0     512
************************** TRIMMED OUTPUT ****************************
/dev/loop0         0      0         1  1 /var/lib/snapd/snaps/snapd_14978.snap              0     512
************************** TRIMMED OUTPUT ****************************
/dev/loop3         0      0         1  1 /var/lib/snapd/snaps/authy_7.snap                  0     512

Moreover, losetup is part of util-linux so you do not need to install it to use it.

How to create loop device

The first and most important step in creating a loop device is to create a virtual block from your drive space using the dd command. 

You can follow the below command to create 4GB of virtual blocks. 

$ dd if=/dev/zero of=[BLOCK-NAME] bs=1M count=4096
  • if=/dev/zero:- create a block of 0’s to hold data
  • of=[BLOCK-NAME]: specify block name
  • bs=1M: copy block size of 1M
  • count=4096: block size will copy until it’s reach 4096M.

By using the above command, I have created a block device named “TO_BLOCK” with a size of 4 GB.

$ dd if=/dev/zero of=TO_BLOCK bs=1M count=4096

4096+0 records in
4096+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 9.99422 s, 430 MB/s

After that use losetup command to create a loop filesystem.

To create a loop device, type the following code and make sure to replace TO_BLOCK with an actual block name.

$ sudo losetup -f TO_BLOCK

It is better to use -f or –find flags to find available loop devices to prevent errors like “losetup: TO_BLOCK: failed to set up loop device: Device or resource busy”.

The above command will not print any message on a successful attempt, then how do I know that a loop device has been created? It is simple. Just run the below code, which will show the loop device name along with the path of the block device. 

$ losetup -a     
**************** Trimmed Output *****************************
/dev/loop7: []: (/home/trendoceans/TO_BLOCK)
/dev/loop3: []: (/var/lib/snapd/snaps/authy_7.snap)
**************** Trimmed Output *****************************

How to mount loop devices

Before mounting the loop device, first, you need to format the loop device in any corresponding format. I’m going to format my loop device with ext4 filesystem.

$ sudo mkfs.ext4 /dev/loop7 

How to Determine the File System Type in Linux (Ext2, Ext3, or Ext4)?

Next, you need to create a mount point where you want to mount the loop device. I think it’s better to mount the loop device in the /media directory. 

After that, you can run mount command to mount the loop device at the mount point. 

$ sudo mkdir /media/loop7
$ sudo mount /dev/loop7 /media/loop7  

How to unmount and detach loop device

To unmount a loop device is pretty straightforward. You just need to use the umount command and pass the mount location or loop filesystem name.

$ sudo umount /media/loop7
$ sudo umount /dev/loop7

Detach and remove block file

Prior to detaching the loop device, you need to unmount the respective loop device. After that, run the following command to detach and delete the block file.

$ losetup -d /dev/loop7
$ rm ~/TO_BLOCK 

If you don’t want to remove the block file, then don’t pass the rm command. And later, you can use the same block file to create a loop device.

Wrap up

That’s all you need to create and manage loop devices (virtual blocks) on Linux. In this article, you have learned how to use the losetup utility to create and manage loop devices.

You can learn more about losetup by reading the online manual or typing “man losetup” in your system terminal.

Leave a Reply