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.
if [ -n "${ORIG_TARGET}" ] ; then
einfo "Removing loopback mount of file ${ORIG_TARGET}."
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 $?
# 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'
# 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
export TARGET="/dev/mapper/$LOOP_PART" # '/dev/mapper/loop1p1'
if [ -z "$TARGET" ] ; then
try_umount 3 "${MNTPOINT}"/dev
umount "${MNTPOINT}"
kpartx -d "${ORIG_TARGET}" >/dev/null
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