Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am using losetup -a to see my loop devices, but it has a length limit (which is 62 characters). So I can't know for sure which loop device I really want.

Ex:

[root@r7byach]# losetup -a 
/dev/loop0: [fc03]:33030504 (/storage5_vol1/6/ABCD116476-linux-ubuntu-10_04-64b-base-07150/*)
/dev/loop1: [0015]:4933 (/storage4/5/ABCD12345-linux-centos-5-64b-base-86836/hdc)

I can get the output on /dev/loop1 using

losetup -a|grep diskPath |cut -f1 -d:|cut -f3 -d/

But I can't get the device on loop0, since the name is not fully qualified.

How can I get the loop device regardless of this size limit?

share|improve this question
add comment

2 Answers

With recent kernels (≥ 2.6.37), see Petr Uzel's answer. With older kernels, this length limit is inherent.

Strace shows that losetup -a obtains the name through the LOOP_GET_STATUS ioctl, which calls loop_get_status_old, which gets its data from a struct loop_info. The name field in that kernel data structure is limited to LO_NAME_SIZE = 64 bytes. (There's also a loop_get_status64 and struct loop_info64, but they have the same limitation.) So this limitation is intrinsic, you can't do any better.

Anyway, that data structure shows the original name of the underlying device or file, but you can rename it (or even remove it), and this won't be reflected in the output from losetup.

The output of losetup shows the filesystem identifier (st_dev) and inode (st_ino) of the file, e.g., [fc03]:33030504. You can find the block device for the filesystem by looking it up in /proc/partitions (line beginning with 252 3). Then look up that block device's mount point in /proc/mounts. Then run find /mount/point -inum 33030504 to locate the file. Yes, it's convoluted, and no, there's no faster way (well, there's debugfs for some filesystem types).

share|improve this answer
    
Thanks for the answer and excellent explanation. As I was doing losetup -f before doing losetup -a on my script. I changed losetup -f to losetup -fv and parsed the output to get the loop device. That solved my problem –  Diego Dias Mar 16 '11 at 23:00
    
@Diego: Indeed, if you can remember the association between the loop device and the underlying file from losetup -fv /path/to/file, that's much better. –  Gilles Mar 16 '11 at 23:05
add comment

Since kernel 2.6.37, you can look up the name of underlying file (without the length limitation) via /sys/block/loopX/loop/backing_file. The losetup command supports this method since util-linux 2.19.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.