Find out which loop device is behind a dev mapper block device

I'm using cryptsetup with loopback devices. I'm looking for a way, given a device mapping, to identify which loopback device is used by this device.

i.e. get /dev/loop1 as a result of an operation on /dev/mapper/some_mapping

How can this be done?


Solution 1:

It's an ancient topic, I know, but this answer may hopefully be useful for future generations of script programmers.

All devices can be displayed with

$ losetup -a

/dev/loop0: [2065]:25 (/mnt/live/memory/data/slax/01-core.sb)
/dev/loop1: [2065]:26 (/mnt/live/memory/data/slax/02-xorg.sb)
/dev/loop2: [2065]:27 (/mnt/live/memory/data/slax/03-kdeps.sb)
(...)

Display a single device (let's say one's interested in 02-xorg.sb) with

$ losetup -j /mnt/live/memory/data/slax/02-xorg.sb

/dev/loop1: [2065]:26 (/mnt/live/memory/data/slax/02-xorg.sb)

Now, the device name /dev/loop1 is in the first field/column (if we separate rows by spaces). To extract the device name, awk can be used. Only remember to remove ':' with substitute

$ losetup -j /mnt/live/memory/data/slax/02-xorg.sb | awk '{sub(/:/,"",$1); print $1}'

/dev/loop1