Where to start First steps track · 15 stages
Linux Labs

Loop device — a file as a disk

5 min Ubuntu 24.04 · util-linux 2.39

A loop device shows a regular file to the system as a disk: you can partition, format and mount it.

Cheat sheet

8 commands Printable version
  • truncate -s 512M disk.img a 512 MiB file
  • sudo losetup -f --show disk.img attach it: /dev/loop0
  • sudo losetup -fP --show disk.img with partitions: loop0p1…
  • losetup -l what is attached to what
  • losetup -j disk.img which device a file is on
  • sudo losetup -d /dev/loop0 detach
  • sudo mount -o loop image.iso /mnt mount an image directly
  • lsblk -e 7 list disks without loops

Overview

A loop device is a block device /dev/loopN backed by a regular file. Whatever is written to the device ends up in the file. You treat it like a disk: partition it (fdisk-parted), create a filesystem (mkfs), mount it (mount).

text
student@ubuntu:~$ truncate -s 512M disk.img
student@ubuntu:~$ sudo losetup -f --show disk.img
/dev/loop0

What it is for:

  • practice partitioning and mounting without touching real disks — this is how the trainer's disk exercises are built;
  • open an image: an ISO, a partition image, a filesystem backup;
  • snap packages on Ubuntu: every snap is mounted through a loop, which is why lsblk often lists many loopN.

The attachment lasts until a reboot or losetup -d. The kernel shows partitions on a loop device only if it was attached with -P: /dev/loop0p1, /dev/loop0p2.

Recipes

4

A practice disk with a partition

bash
truncate -s 512M disk.img
DEV=$(sudo losetup -fP --show disk.img)    # /dev/loop0
sudo parted -s "$DEV" mklabel gpt mkpart data ext4 1MiB 100%
sudo mkfs.ext4 -L data "${DEV}p1"
sudo mount "${DEV}p1" /mnt

A filesystem image without partitions

bash
truncate -s 256M fs.img
mkfs.ext4 -q fs.img                  # the filesystem right in the file
sudo mount fs.img /mnt               # mount picks a free loop itself
findmnt /mnt                         # SOURCE is /dev/loopN

Clean up afterwards

bash
sudo umount /mnt
losetup -j disk.img          # /dev/loop0: []: (…/disk.img)
sudo losetup -d /dev/loop0
rm disk.img

Attach an image at boot

text
/srv/images/data.img  /srv/data  ext4  loop,nofail  0  2

An /etc/fstab line with the loop option — mount attaches the file at boot.

Troubleshooting

5

failed to set up loop device: Permission denied

losetup: disk.img: failed to set up loop device: Permission denied — only root can attach devices. Run it with sudo.

There is no /dev/loop0p1

The device was attached without -P, so the kernel did not read the table. Re-read it with sudo partprobe /dev/loop0, or detach and attach again with -fP.

losetup: cannot find an unused loop device

No free devices are left, usually because of forgotten attachments. Check losetup -l and detach the extra ones with losetup -d.

The file is deleted but the space did not come back

The file is still attached to a loop device. Find it in losetup -l (it is marked (deleted)) and detach it — the space is freed.

losetup -d did not detach the device

The device is mounted. losetup -d only marks it (the AUTOCLEAR column in losetup -l turns to 1), and it is detached after umount. A second losetup -d answers detach failed: No such device or address — the device is already gone.

Danger zone

1

One file, two devices

losetup -f does not check whether the file is already attached. Two devices on one file, each mounted, corrupt the filesystem. Check losetup -j file before attaching.

See also

  • mount — mount a device or an image.
  • lsblk — loop devices and their partitions as a tree.
  • fdisk-parted — partition a loop device like a disk.
  • mkfs — create a filesystem on a device or right in a file.
  • blkid — the UUID and label of the filesystem in an image.
  • qemu-img — virtual machine disk images.
Loading…