Mount

losetup --list 的輸出是如何排序的?

  • September 2, 2020

當我執行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

它顯然不是按設備名稱排序的,也不是按備份文件的名稱排序的。順序是隨意的嗎?

losetupgetdents64()從讀取/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輸出,我想沒有人會費心以任何有意義的方式對其進行排序。

引用自:https://unix.stackexchange.com/questions/607533