`losetup --remove` is confusing, and misuse silently fails

Benno Schulenberg <[email protected]> Fri, 10 Oct 2025 11:41:08 +0200
Newsgroups org.kernel.vger.util-linux
Message-ID <[email protected]>
After setting up a loop device with:

   # ./losetup loop4 some.img

I wanted to see what happens when trying to "remove" it
(since the man page says it is "not recommended"):

   # ./losetup -R loop4

There is no feedback, but it failed:

   # echo $?
   1

When a command fails, shouldn't it print an error message?


Then I tried detaching and removing at the same time:

   # ./losetup -R -d loop4

No feedback, but it didn't do anything:

   # ./losetup loop4
   /dev/loop4: [66306]:5939273 (/home/ben/Sources/util-linux/some.img)

Trying a different order of the options:

   # ./losetup -d -R loop4
   losetup: /dev/-R: detach failed: No such file or directory

Huh?  Why does it try to interpret an option as a loop device name?

   # ./losetup -d loop4 -R
   /home/ben/Sources/util-linux/.libs/losetup: option requires an argument -- 'R'
   Try 'losetup --help' for more information.

   # ./losetup -d loop4 -R loop4

Oh.  Surprisingly, this last invocation worked.

   # ls -l /dev/loop? | grep loop4
   # lsblk -a | grep loop4


However, the loop4 device is not really gone, because one can attach
a file to it again:

   # ./losetup --show loop4 some.img
   /dev/loop4
   # ./losetup -a
   /dev/loop4: [66306]:5939273 (/home/ben/Sources/util-linux/some.img)

When the device is really gone, attaching doesn't work:

   # rm /dev/loop3
   # ./losetup loop3 some.img
   losetup: /dev/loop3: failed to set up loop device: No such file or directory
   losetup: device node /dev/loop3 (7:3) is lost. You may use mknod(1) to 
recover it.

In my opinion --remove is not a good name for the option, as it doesn't
actually remove anything.  Also, its meaning is too similar to "detach":
when the computer asks me to "Remove the USB stick", I detach this stick
from the USB port.

What --remove accomplishes is that it prevents the specified loop device
from getting listed.  So in my opinion a better name for the option would
be: --unlist.

But what is the purpose of --unlist?  Why would one want it?  What harm
is there in listing empty, fallow loop devices?

   # ./lsblk -a | grep loop
   loop0         7:0    0     0B  0 loop
   loop1         7:1    0     0B  0 loop
   loop2         7:2    0     0B  0 loop
   loop3         7:3    0     0B  0 loop
   loop4         7:4    0   400M  0 loop
   loop5         7:5    0     0B  0 loop
   loop6         7:6    0     0B  0 loop
   loop7         7:7    0     0B  0 loop


Benno
OpenPGP_signature.asc (application/pgp-signature, 840 B)
-----BEGIN PGP SIGNATURE-----

wsF5BAABCAAjFiEEFo5vQpe/16ea/USWUUu+Lrjhlh8FAmjo1LUFAwAAAAAACgkQUUu+Lrjhlh84
sg//Y1f0LZLuwKeXn9dPxZLW8xKJRIAirMGcdRfEqjatTzqLoYmWiEdsoV2z+3zL9cVgy03plKTq
NHVgR5LuAMH79xsIvzfmjTXNqPJJyzB3VH58oy8ZLO00cB4B1SUpU76lvZqp+jdpu4W2BSsWdIS9
lpkutBZhwp7U3h0Cf3RUkh++n9hhDi03gEMOWHq9EwTptUUtka4/14DjsK8kJBRr7rIDwgfug0H6
Y5gEGonjtS/bm0FaA33aQ9A57njTdvzxOKsbiPSjWG3TtCWD4RjkOhuuRgcHSux9s76qHw2foju5
Wt/WAghfcJ0CsEMK84c2VDEHVsoU9y1ZKv5Pp0aTzJtRstz+EyhYeH0gag8b4Z2yKPwlOaJxdB2s
KzguWwNsZYJkT+sx2BgMLKD2zDwzsCVMsbQgrD6pVJ3pI9OuZI6N72KyxC90XhlGnVlTgm7bx/I4
930JLPmGfBU1y1nYC+EZlJjY5kUra16PTbWAHGZyS3bZSiOo11goC6rSmAFTWmt8ihsOjCSZPnOZ
IFFpbEAu8607Rs1dVmqNnvdVEAYGS5Pkg8RXNbn8WKnYVDBXh8NKduF5ss16/7tEijOifVLAyWS1
iNCEPwY63tLHlhP/M/Rc6fOzZYEbvOZq5OkbIV5eDEYLtRRsO4HgiW+i/7kknrUAnHUub3zisXnI
nXc=
=eyS4
-----END PGP SIGNATURE-----