What is a Loop device in Linux?

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

list drives in ubuntu
Loop devices

If you are an Ubuntu user, then you’ll get a long list of loop devices as shown in the screenshot above.

It is because of snaps, the universal package management system developed by Canonical. The snap applications are mounted as loop devices.

Now, this raises another set of questions such as what is a loop device and why snaps applications are mounted as a disk partition.

Let me shed some light on the topic

Loop devices: Regular Files that are mounted as File System

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

Seems too complicated right? Let me break it down for you.

In simple terms, a loop device can behave as a virtual file system which is quite helpful while working with isolated programs such as snaps.

So basically you get an isolated file system mounted at a specific mounting point. By which a developer/advanced user packs a bunch of files in one place. So it can be accessed by an operating system and that behavior is known as loop mounts.

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

Reasons for using loop Devices

While being a virtual file system, there are endless possibilities; here are some widely known use cases of loop devices:

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

And the developers can do wonders when given isolated file systems.

The loop devices can be easily managed through losetup utility. Let me show you how.

Manage loop devices

So let’s start with listing available loop devices.

To list them, all you need to do is pair losetup with -a option:

losetup -a
losetup a

Unmount Loop device

The process for unmounting any loop device is pretty straightforward. For that, I’ll be using umount command.

sudo umount /dev/loop9
lsblk

The loop9 block was brave browser installed as snap, and you can clearly see, it is no longer mounted and can’t be launched.

Delete Loop device

This is for demonstration purposes only. Don’t go and randomly delete loop devices.

Make sure to unmount the loop device before you proceed further in deleting a specific loop device.

Your first step will be detaching files to any loop device using -d option. For demonstration, I’ll be using loop9:

sudo losetup -d /dev/loop9

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

sudo rm /dev/loop9

And loop9 was no longer listed in available loop devices:

delete loop device

Final Words

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

Have a point to add? The comment section is all yours.

Similar Posts

  • Good article but I think you shouldn’t mention to rm a /dev/loop* file, as they are not created by a third party app. They are USED by snap to map a real file to a loop file, deleting the loop file can lead to no loop file at some point, and snaps (or other apps) not functioning correctly.