2
# 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.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

4

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.

Caleb
  • 70,105
Petr Uzel
  • 7,257
3

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.