How to use loop devices

I have a feeling that loop devices are now used less frequently, but these are still relevant and useful.

Display first found loop device.

$ losetup --find
/dev/loop0

Attach file using first found loop device.

$ sudo losetup --find --nooverlap loop.fs

Attach file using defined loop device with enabled direct IO.

$ sudo losetup --direct-io=on loop1 second_loop.fs

Attach file using defined loop device in read-only mode.

$ sudo losetup --read-only loop2 third_loop.fs

Display loop devices.

$ losetup --list
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                   DIO
/dev/loop2         0      0         0  1 /home/milosz/third_loop.fs    0
/dev/loop1         0      0         0  0 /home/milosz/second_loop.fs   1
/dev/loop0         0      0         0  0 /home/milosz/loop.fs          0

Display loop devices using JSON format.

$ losetup --list --json
{
   "loopdevices": [
      {"name": "/dev/loop2", "sizelimit": "0", "offset": "0", "autoclear": "0", "ro": "1", "back-file": "/home/milosz/third_loop.fs", "dio": "0"},
      {"name": "/dev/loop1", "sizelimit": "0", "offset": "0", "autoclear": "0", "ro": "0", "back-file": "/home/milosz/second_loop.fs", "dio": "1"},
      {"name": "/dev/loop0", "sizelimit": "0", "offset": "0", "autoclear": "0", "ro": "0", "back-file": "/home/milosz/loop.fs", "dio": "0"}
   ]
}

Display defined loop device.

$ losetup --list /dev/loop0
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE            DIO
/dev/loop0         0      0         0  0 /home/milosz/loop.fs   0

Display loop device associated with defined file.

$ losetup --list --associated=loop.fs 
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE            DIO
/dev/loop0         0      0         0  0 /home/milosz/loop.fs   0

Detach defined loop device.

$ sudo losetup --detach /dev/loop1

Detach all loop devices.

$ sudo losetup --detach-all
Reread size of the file attached to the loop device.
$ sudo losetup --set-capacity /dev/loop1

Additional information

You can create empty file with defined size using dd utility.

$ dd if=/dev/zero of=loop.fs bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0,0143954 s, 728 MB/s

Single file can contain multiple filesystems by specifing size and offset parameters.

$ sudo losetup --find loop.fs --offset 0 --sizelimit 1MiB loop.fs
$ sudo losetup --find loop.fs --offset 1MiB  --sizelimit 3MiB loop.fs
$ losetup --list 
NAME       SIZELIMIT  OFFSET AUTOCLEAR RO BACK-FILE            DIO
/dev/loop1   3145728 1048576         0  0 /home/milosz/loop.fs   0
/dev/loop0   1048576       0         0  0 /home/milosz/loop.fs   0

You need to force kernel to scan the partition table if you want to use it on loop device.

$ sudo losetup --partscan loop1 second_loop.fs
$ sudo parted /dev/loop1 unit MiB print
Model: Loopback device (loopback)
Disk /dev/loop1: 10,0MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start    End      Size     File system  Name  Flags
 1      1,00MiB  9,98MiB  8,98MiB

$ ls /dev/loop1*
/dev/loop1  /dev/loop1p1
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.

Gdansk, Poland https://sleeplessbeastie.eu