Linux核心的最大循環設備是多少?

Linux核心的最大循環設備是多少?

我可以包含循環模組來支援循環文件。 Loop 模組支援 max_loop 選項。我找到了帶有選項循環 max_loop 256 的範例。我的問題是,最大支援的循環設備是多少?我不敢相信,256 是硬限制,創造超過 256 個循環設備是不可能的。

更新:

我在文件中沒有發現任何有趣的內容https://elixir.bootlin.com/linux/v4.0/source/drivers/block/loop.c

但我做了一些實驗,並運行 modprobe max_loops=512 然後我在 /dev/ 目錄中看到完全相同的計數循環區塊檔案安裝為 udev,編號從loop0到loop511

我用 linux 核心 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u2 (2019-11-11) x86_64 做到了

答案1

在內核 3.1 之前,您必須設定固定數量的循環設備。從 3.1 開始,有/dev/loop-control, 和 循環設備根據需要動態分配,而不是固定數量。因此,它不是從不需要的 100 個循環設備(以防萬一)開始,而是從 0 個設備(或可選的最小計數)開始,並且僅在實際需要時創建它們。

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.

非常好的源代碼(drivers/block/loop.c)描述它:

    /*
     * 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.
     */

它也是建議根本不要設定

     * 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.

那麼實際可以使用多少個循環設備呢?此限制是單一主要設備的次要設備的最大數量(因為loop有一個主要設備,區塊 7),其限制為MINORBITS(所以 2 20,剛好超過一百萬)。

我試著強迫一些像這樣的大數字:

truncate -s 1M foobar
i=1
while losetup --show /dev/loop$(($i-1)) foobar
do
    i=$(($i*2))
done

……但最終引發了內核恐慌。 ;-)

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.

這符合 2 20限制。

相關內容