`losetup --list` 的输出是如何排序的?

`losetup --list` 的输出是如何排序的?

当我运行losetup --list(或只是losetup)来获取系统上的环回设备列表时,输出似乎没有任何有意义的顺序:

0 $  losetup --list
NAME        SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                                   DIO LOG-SEC
/dev/loop1          0      0         1  1 /var/lib/snapd/snaps/core18_1885.snap                         0     512
/dev/loop29         0      0         1  1 /var/lib/snapd/snaps/core18_1880.snap                         0     512
/dev/loop19         0      0         1  1 /var/lib/snapd/snaps/gtk-common-themes_1506.snap              0     512
/dev/loop27         0      0         1  1 /var/lib/snapd/snaps/gnome-characters_539.snap                0     512
/dev/loop17         0      0         1  1 /var/lib/snapd/snaps/gnome-characters_550.snap                0     512
/dev/loop8          0      0         1  1 /var/lib/snapd/snaps/gtk-common-themes_1502.snap              0     512
/dev/loop25         0      0         1  1 /var/lib/snapd/snaps/kde-frameworks-5-qt-5-14-core18_4.snap   0     512
/dev/loop15         0      0         1  1 /var/lib/snapd/snaps/glimpse-editor_134.snap                  0     512
/dev/loop6          0      0         1  1 /var/lib/snapd/snaps/gnome-calculator_730.snap                0     512
/dev/loop23         0      0         1  1 /var/lib/snapd/snaps/gnome-logs_93.snap                       0     512
/dev/loop13         0      0         1  1 /var/lib/snapd/snaps/wormhole_112.snap                        0     512
/dev/loop4          0      0         1  1 /var/lib/snapd/snaps/canonical-livepatch_94.snap              0     512

它显然不是按设备名称排序的,也不是按支持文件的名称排序的。顺序是任意的吗?

答案1

losetupgetdents64()from /sys/block,所以它是该目录中条目的顺序。

如果你得到相同的顺序ls -f(不排序),那么就是这样:

# ls -f /sys/block | grep loop
loop1
loop6
loop4
loop2
loop0
loop7
loop5
loop3

losetup进行比较:

# losetup
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE  DIO LOG-SEC
/dev/loop1         0      0         0  0 /dev/shm/b   0     512
/dev/loop6         0      0         0  0 /dev/shm/c   0     512
/dev/loop4         0      0         0  0 /dev/shm/a   0     512
/dev/loop2         0      0         0  0 /dev/shm/c   0     512
/dev/loop0         0      0         0  0 /dev/shm/a   0     512
/dev/loop7         0      0         0  0 /dev/shm/d   0     512
/dev/loop5         0      0         0  0 /dev/shm/b   0     512
/dev/loop3         0      0         0  0 /dev/shm/d   0     512

联机帮助页getdents没有提到任何有关顺序的内容,但是readdir说:

   The order in which filenames are read by successive calls to readdir()
   depends on the filesystem implementation; it is unlikely that the names
   will be sorted in any fashion.

在排序列表(ls等)中输出文件的程序通常会自行对其进行排序,并且当您使用诸如 之类的东西时echo *,shell 会为您进行排序。

对于losetup输出,我想没有人愿意以任何有意义的方式对其进行排序。

相关内容