From 33d0e7f675e3f805cbd23dcfd5989e24cf5b0db6 Mon Sep 17 00:00:00 2001 From: Darshaka Pathirana Date: Wed, 25 Jul 2018 12:14:37 +0200 Subject: [PATCH] Improve cleanup of loop devices kpartx (sometimes) does not cleanup the loop devices when deleting the partition devmappings. This is a known bug, see Debian Bug #891077. This is change is a possible workaround for that issue. Now aborting grml-debootstrap is possible without leaving unneded loop devices behind. --- grml-debootstrap | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/grml-debootstrap b/grml-debootstrap index 1c5d8b7..1ce3be0 100755 --- a/grml-debootstrap +++ b/grml-debootstrap @@ -298,7 +298,13 @@ cleanup() { if [ -n "${ORIG_TARGET}" ] ; then einfo "Removing loopback mount of file ${ORIG_TARGET}." - kpartx -d "${ORIG_TARGET}" ; eend $? + kpartx -d "${ORIG_TARGET}" + # Workaround for a bug in kpartx which doesn't clean up properly, + # see Debian Bug #891077 and Github-PR grml/grml-debootstrap#112 + if dmsetup ls | grep -q "^${LOOP_PART} "; then + kpartx -d "/dev/${LOOP_DISK}" >/dev/null + fi + eend $? fi } @@ -1432,6 +1438,7 @@ prepare_vm() { # hopefully this always works as expected LOOP_PART="${DEVINFO##add map }" # 'loop0p1 (254:5): 0 20477 linear 7:0 3' LOOP_PART="${LOOP_PART// */}" # 'loop0p1' + LOOP_DISK="${LOOP_PART%p*}" # 'loop0' export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1' if [ -z "$TARGET" ] ; then @@ -1516,6 +1523,11 @@ fi try_umount 3 "${MNTPOINT}"/dev umount "${MNTPOINT}" kpartx -d "${ORIG_TARGET}" >/dev/null + # Workaround for a bug in kpartx which doesn't clean up properly, + # see Debian Bug #891077 and Github-PR grml/grml-debootstrap#112 + if dmsetup ls | grep -q "^${LOOP_PART} "; then + kpartx -d "/dev/${LOOP_DISK}" >/dev/null + fi } # }}} -- 2.1.4