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
@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