Accessing raw filesystem image partitions without the need to specify the offset and size manually

# udisksctl
sudo pacman -S udisks2
man 1 udisksctl

# losetup
sudo pacman -S util-linux
man 8 losetup

udisksctl

Root permissions are not required

udisksctl loop-setup --read-only --file <raw-image>.img
udisksctl loop-setup -rf <raw-file>.img

Mapped file raw-image.img as /dev/loop0.

Image file location is specified with an option

  • loop-setup is an argument, because udisksctl has other competences as well
  • --read-only or -r prevents accidental damage, can be omitted
  • --file or -f takes a raw filesystem image file location

A first unused partitioned loop device file location the image was associated with is printed automatically

losetup

Requires root permissions

sudo losetup --show --partscan --read-only --find <raw-image>.img
sudo losetup --show -Prf <raw-image>.img

/dev/loop0

Raw image file is specified with an argument

  • --show print the loop device, used with -f
  • --partscan or -P creates a partitioned loop device
  • --read-only or -r prevents accidental damage, can be omitted
  • --find or -f find first unused device

Control is more granular as most actions are not invoked automatically, but rather explicitly

Probing and listing

udisksctl

Provide information about a specific loop device

udisksctl info -b /dev/loop0
  • --block-device or -b specifies the loop device to probe

A an uninspiring output snippet, truncated

/org/freedesktop/UDisks2/block_devices/loop0:
  org.freedesktop.UDisks2.Block:
    ...
    PreferredDevice:            /dev/loop0
    ReadOnly:                   false
    Size:                       1845493760
    Symlinks:
    UserspaceMountOptions:
  org.freedesktop.UDisks2.Loop:
    Autoclear:          false
    BackingFile:        raw-image.img
    SetupByUID:         0
  org.freedesktop.UDisks2.PartitionTable:
    Partitions:         /org/freedesktop/UDisks2/block_devices/loop0p1
                        /org/freedesktop/UDisks2/block_devices/loop0p2
    Type:               dos

Caution: may make you dizzy

udisksctl dump

losetup

Provide information about a specific loop device

losetup -l /dev/loop0
  • --list or -l lists info about a specified loop device
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE   DIO LOG-SEC
/dev/loop0         0      0         0  1 image-A.img   0     512

Provide information about all used loop devices

losetup -l
  • --list or -l lists info about all used loop devices without speficiyng one
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE   DIO LOG-SEC
/dev/loop0         0      0         0  1 image-A.img   0     512
/dev/loop1         0      0         1  0 image-B.img   0     512
  • --json or -J formats the output as JSON, used with -l
{
  "loopdevices": [
    {
      "name": "/dev/loop0",
      "sizelimit": 0,
      "offset": 0,
      "autoclear": false,
      "ro": false,
      "back-file": "raw-image.img",
      "dio": false,
      "log-sec": 512
    }
  ]
}
  • --raw outputs the data without extra whitespaces
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop0 0 0 1 0 image-A.img 0 512
/dev/loop1 0 1 0 0 image-B.img 0 512
  • --all or -a lists all used loop devices (but why?)
/dev/loop0: []: (image-A.img)
/dev/loop1: []: (image-B.img)

Removal

Sets the autoclear flag - the device will be released instantly when not needed, or i.e. after unmounting

udisksctl

udisksctl loop-delete -b /dev/loop0

The device is specified with an option

  • loop-delete is an argument for a sub-command
  • --block-device or -b specifies the loop device to flag

losetup

sudo losetup -d /dev/loop0
  • --detach or -d specifies the loop device to flag
sudo losetup -D
  • --detach-all or -D detaches all used devices

Conclusion

After trying both udisksctl and losetup for handling loop devices to manipulate the raw filesystem images, I personally like losetup much more. It is a tool spcialised exactly for this job and mostly nothing else, following the UNIX philosophy. Udisksctl was designed for a little different role and covers broader use cases, one of which is also handling loop devices.

I did find only single legitimate reason to use udisksctl for this task - it was already installed on my system, because i.e. mintstick and gnome-control-center depend on it.

If I did want to keep my system lean, I would tried to use packages already installed to do the job, intead of installing new ones. But hey, the packages that have brought udisks2 in (depend on it) are already rather large UI tools, so speaking about efficiency here is not really cutting it.

Both mentioned tools can do the job pretty well and they differ only slightly in the syntax. Where they differ mostly is how they output the data. Losetup provides multiple output formatting options. It is even including JSON which definitely feels much more modern.

Building a Dockerfile with losetup's raw output definitely feels faster when I do not need to spend too much time parsing complex text outputs and rather spend time building the actual thing. Altought, I could not think of any example using a web technology in conjunction with losetup to utilize JSON output formatting,

Please let me know about any real use cases of using JSON output here, I am genuinely interested. Maybe it could be a glipse to another development perspective.