Skip to content

Instantly share code, notes, and snippets.

@waja
Last active September 30, 2022 12:37
Show Gist options
  • Save waja/c780d4217fd695e4f55f to your computer and use it in GitHub Desktop.
Save waja/c780d4217fd695e4f55f to your computer and use it in GitHub Desktop.
Remove unused loop devices
#!/bin/sh
for LOOPS in $(losetup -a | awk -F':' {'print $1'} | awk -F'/' {'print $3'}); do
for LOOPPART in $(ls /dev/mapper/${LOOPS}*| awk -F'/' {'print $4'}); do
dmsetup remove ${LOOPPART};
done;
losetup -d /dev/${LOOPS};
done
@tripulse
Copy link

Got a bunch of errors while executing the script through (/bin/sh). I copy-pasted from the terminal output.

$ curl -o - "https://gist.githubusercontent.com/waja/c780d4217fd695e4f55f/raw/4f2a575adf0bd8c91875139a873409bd01cdb15c/free_unused_loop_dev.sh" | sudo sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   239  100   239    0     0    534      0 --:--:-- --:--:-- --:--:--   534
ls: cannot access '/dev/mapper/loop19*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop17*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop8*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop15*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop6*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop13*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop4*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop11*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop18*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop9*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop16*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop7*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop14*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop5*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop12*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.
ls: cannot access '/dev/mapper/loop10*': No such file or directory
losetup: option '--d' is ambiguous; possibilities: '--detach' '--detach-all' '--direct-io'
Try 'losetup --help' for more information.

@donadelden
Copy link

The same errors of @tripulse also in my Ubuntu 20.04

@captn3m0
Copy link

captn3m0 commented Jan 20, 2021

sudo losetup --detach-all works well

@waja
Copy link
Author

waja commented Jan 20, 2021

Seriously? You guys have an error message and there is written Try 'losetup --help' for more information. but it's too hard to look into this?

$ /sbin/losetup --help | grep detach
 -d, --detach <loopdev>...     detach one or more devices
 -D, --detach-all              detach all used devices

Indeed, the command from @captn3m0 works, but I replaces the losetup --d by losetup -d which can be easily spotted in the --help output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment