3

When I run losetup --list (or just losetup) to get a list of the loopback devices on my system, the output doesn't seem to be in any meaningful order:

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 

It's clearly not sorted by the name of the device, nor the name of the backing file. Is the order just arbitrary?

1 Answer 1

2

losetup reads getdents64() from /sys/block, so it's the order of entries in that directory.

If you get the same order with ls -f (do not sort), then that's it:

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

And losetup to compare:

# 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 

The getdents manpage mentions nothing regarding order, but readdir says:

 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. 

Programs that output files in a sorted list (ls etc.) usually sort it themselves, and when you use things like echo *, the shell is doing the sorting for you.

For losetup output, I guess no one bothered to sort it in any meaningful way.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.