diff options
author | Sven Gothel <[email protected]> | 2021-06-28 06:15:16 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-06-28 06:15:16 +0200 |
commit | f44f5a676b1c621f451550d77ed0ce75d5696dce (patch) | |
tree | 7eec235a00fd8f93e81375d3401991bde015b4ff /imagetool.sh | |
parent | 904d3505c714bafde8e12e44f41c47dfeb2b50fd (diff) |
imagetool.sh: Add '-C --force-cleanup' to force drop nbd devices/connections
Sadly it occurs that NBD mapped and connected devices won't be freed 'sometimes'.
Force cleanup disregards that they are in use...
Diffstat (limited to 'imagetool.sh')
-rwxr-xr-x | imagetool.sh | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/imagetool.sh b/imagetool.sh index 9b359f8..fafbade 100755 --- a/imagetool.sh +++ b/imagetool.sh @@ -19,6 +19,7 @@ Usage: arguments: -h, --help show this help message and exit -c, --cleanup cleanup orphaned device mappings + -C, --force-cleanup forcefully cleanup orphaned or still connected device mappings -m, --mount mount image -u, --umount umount image -i, --image-name path to qcow2 image @@ -49,6 +50,22 @@ nbd_cleanup() { fi } +force_nbd_cleanup() { + DEVS="$(lsblk | grep nbd | grep disk | cut -d" " -f1)" + if [ ! -z "${DEVS}" ]; then + for d in $DEVS; do + if [ ! -z "${d}" ]; then + QDEV="$(ps xa | grep $d | grep -v grep)" + if [ -z "${QDEV}" ]; then + kpartx -d /dev/$d && echo "Unconnected device map removed: /dev/$d" + else + kpartx -d /dev/$d && echo "Connected device map removed (force): /dev/$d" + fi + fi + done + fi +} + # As long as there is at least one more argument, keep looping while [[ $# -gt 0 ]]; do key="$1" @@ -60,6 +77,9 @@ while [[ $# -gt 0 ]]; do -c|--cleanup) nbd_cleanup ;; + -C|--force-cleanup) + force_nbd_cleanup + ;; -m|--mount) MOUNT=1 ;; |