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-setupis an argument, because udisksctl has other competences as well--read-onlyor-rprevents accidental damage, can be omitted--fileor-ftakes 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
--showprint the loop device, used with-f--partscanor-Pcreates a partitioned loop device--read-onlyor-rprevents accidental damage, can be omitted--findor-ffind 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-deviceor-bspecifies 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
--listor-llists 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
--listor-llists 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
--jsonor-Jformats 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
}
]
}
--rawoutputs 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
--allor-alists 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-deleteis an argument for a sub-command--block-deviceor-bspecifies the loop device to flag
losetup#
sudo losetup -d /dev/loop0
--detachor-dspecifies the loop device to flag
sudo losetup -D
--detach-allor-Ddetaches 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.
Links#
- https://en.wikipedia.org/wiki/Loop_device
- https://wiki.archlinux.org/index.php/udisks#Mount_loop_devices
- https://github.com/karelzak/util-linux
- https://github.com/storaged-project/udisks
- https://stackoverflow.com/questions/5881134/cannot-delete-device-dev-loop0
- https://unix.stackexchange.com/questions/520286/why-is-udisksctl-loop-setup-so-slow