5

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?

Dave M
  • 4,494
  • 21
  • 30
  • 30
miluz
  • 161
  • 1
  • 5
  • Have you already tried `losetup -a`? – ott-- Aug 12 '13 at 11:05
  • losetup -a shows all loop devices. The purpose of this is an unmount script that by the mount point name unmaps the dev mapper device, and frees the loopback device as well. There may be several mounts at a given time, therefore there's a need to know which loop device is behind which dev mapper device. – miluz Aug 12 '13 at 12:25
  • what about getting the major/minor numbers from `dmsetup ls`, and then just checking in the `/dev/` tree for the loopdevice with corresponding numbers? – Petter H Aug 12 '13 at 12:27
  • dmsetup ls prints the major,minor of the device hosting the image files, not the loop devices major,minor. – miluz Aug 12 '13 at 13:16
  • Isn't your mount point name not the same as the /dev/mapper and the image name? My script works like this, I call it `crim m im1` to mount or `crim u im1` to umount. It even checks if the image is Luks. – ott-- Aug 12 '13 at 15:39
  • By using naming conventions the script works around this problem. I wonder if there is a more robust solution. – miluz Aug 13 '13 at 09:06

3 Answers3

4

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
Matthias Braun
  • 205
  • 1
  • 8
cieply
  • 41
  • 3
0

cryptsetup status some_mapping prints out a device entry, so I think that will work for your case (not sure about the more general /dev/mapper question when not using cryptsetup).

ws_e_c421
  • 101
0

losetup shows a device number when used as root, or when you are in group disk (an ioctl is needed on the loop device).

The numbers you see in one of the other answers (ie. 2065) can be decomposed by dividing and modding by 256, ie. 2065 / 256 = 8, which is a scsi device, and 2065 % 256 = 17, which is /dev/sdb1.

Xennex81
  • 121
  • 3