Before kernel 3.1, you had to set a fixed number of loop devices. Since 3.1 there is /dev/loop-control
, and loop devices are allocated dynamically as needed, rather than a fixed number. So rather than having a hundred loop devices you never needed (just in case), it starts out with 0 devices (or an optional min count) and only creates them when actually required.
From man 4 loop
:
/dev/loop-control
Since Linux 3.1, the kernel provides the /dev/loop-control device,
which permits an application to dynamically find a free device, and to
add and remove loop devices from the system.
The very fine source code (drivers/block/loop.c
) describes it:
/*
* If max_loop is specified, create that many devices upfront.
* This also becomes a hard limit. If max_loop is not specified,
* create CONFIG_BLK_DEV_LOOP_MIN_COUNT loop devices at module
* init time. Loop devices can be requested on-demand with the
* /dev/loop-control interface, or be instantiated by accessing
* a 'dead' device node.
*/
It also recommends not setting it at all:
* Note: Global-for-all-devices, set-only-at-init, read-only module
* parameteters like 'max_loop' and 'max_part' make things needlessly
* complicated, are too static, inflexible and may surprise
* userspace tools. Parameters like this in general should be avoided.
How many loop devices can realistically be used then? The limit is the maximum number of minor devices for a single major device (since loop
has a single major, block 7), which is limited by MINORBITS
(so 220, just over a million).
I tried to force some large numbers like this:
truncate -s 1M foobar
i=1
while losetup --show /dev/loop$(($i-1)) foobar
do
i=$(($i*2))
done
...but it triggered a kernel panic in the end. ;-)
sysfs: cannot create duplicate filename '/devices/virtual/bdi/7:1048575'
kobject_add_internal failed for 7:1048575 with -EEXIST, don't try to register things with the same name in the same directory.
This matches the 220 limit.