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.

About the author
Sagar Sharma

Sagar Sharma

A software engineer who loves to write about his experience with Linux. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.