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.
# losetup /dev/loop0 /tmp/tmpuUwgbn
# losetup -a
# losetup -f
/dev/loop1
# losetup /dev/loop0
/dev/loop0: [0806]:33654 (/tmp/tmpuUwgbn)
# ls -la /tmp/tmpuUwgbn
-rw------- 1 root root 0 2011-08-24 15:49 /tmp/tmpuUwgbn
# losetup -a
#

Why doesn't losetup -a list /dev/loop0 when it clearly knows about it? The man page doesn't give any hints, and google doesn't help.

Note: I found the answer by trial and error while writing this question, but I'm posting it anyway in the hopes it might help someone. See my answer below.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Loop devices with backing file less than 512B are not listed in /proc/partitions, which losetup uses to look up loop devices. I believe the reason why it is not listed there is that by creating such loop device, you get block device which is smaller than its blocksize (512B in case of loop device).

So while losetup and kernel allows you to create loop device which is smaller than 512B, you can not use it anyways (cannot read or write to it, blocksize --getsize returns 0). The only thing you can do with such device is - as far as I understand - to grow the backing file and call losetup -c /dev/loopX - if the backing file >=512B, the loop device now appears in /proc/partitions and $(losetup -a).

The ability to losetup -c is IMHO the reason why the kernel supports creating loopdevice with <511B backing file instead of returning an error.

share|improve this answer

It's because losetup -a for some reason does not list loop devices whose associated files are smaller than 512 bytes:

# losetup /dev/loop1 `mktemp`
# losetup -a
# losetup -d /dev/loop1
# t=`mktemp`; truncate --size 1 $t; losetup /dev/loop1 $t
# losetup -a
# losetup -d /dev/loop1
# t=`mktemp`; truncate --size 511 $t; losetup /dev/loop1 $t
# losetup -a
# losetup -d /dev/loop1
# t=`mktemp`; truncate --size 512 $t; losetup /dev/loop1 $t                    
# losetup -a
/dev/loop1: [0806]:33683 (/tmp/tmp.M0F7bovkxK)

Why? I wish I knew. I feel the man page ought to mention this somewhere.

share|improve this answer

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.