What is a Loop device in Linux?

When listing mounted drives through the terminal, you should have encountered drive names starting with loop :

Loop devices

If you are an Ubuntu user, you will get a long list of looping devices as shown in the screenshot above.

It’s because of snaps, the universal package management system developed by Canonical. Snap applications are mounted as loop devices.

Now this brings up another set of questions like what is a loop device and why snaps apps are mounted as a disk partition.

Let me shed some light on the subject

Loopback devices: normal files mounted as a filesystem

Linux allows users to create a special block device through which they can map a normal file to a virtual block device.

Sounds too complicated, right? Let me explain to you.

Simply put, a loop device can behave like a virtual filesystem, which is very useful when working with isolated programs like snaps.

So basically you get an isolated filesystem mounted at a specific mount point. By which a developer/power user bundles a bunch of files in one place. It is therefore accessible by an operating system and this behavior is known as loop mounts.

But working with isolated systems using a loop device is one of the many reasons loop devices are used and if you’re interested, here are some other use cases for loop devices.

Reasons to use loop devices

While being a virtual filesystem, the possibilities are endless; Here are some widely known use cases of loop devices:

  1. It can be used to install an operating system on a file system without repartitioning the drive.
  2. A convenient way to set up system images (after mounting them).
  3. Provides permanent data separation.
  4. It can be used for sandboxed applications that contain all the necessary dependencies.

And developers can do wonders when given isolated file systems.

Loop devices can be easily managed via losetup utility. Let me show you how.

Manage Loop Devices

So let’s start by listing the available loop devices.

To list them, simply couple losetup with -a option:

losetup -a
lose one

Unmount the Loop device

The process of disassembling any loop device is quite simple. For this I will use the umount command.

sudo umount /dev/loop9

The loop9 block was a brave browser installed as a snap-in, and you can clearly see it is no longer mounted and cannot be launched.

Remove Loop Device

This is for demonstration purposes only. Don’t go randomly deleting devices in a loop.

Be sure to unmount the loop device before continuing to remove a specific loop device.

Your first step will be to detach files on any loop device using -d option. For the demonstration, I will use loop9:

sudo losetup -d /dev/loop9

And now you can delete the loop9 device by the same old rm command that is used to delete files and directory:

sudo rm /dev/loop9

And loop9 was no longer listed in available loop devices:

remove a loop device

Last words

The guide was meant to cover the basics of loop devices, and I kept it simple enough that even new users could benefit from this guide.

Do you have a point to add? The comments section is yours.

Comments are closed.