diff options
author | Daniel Baumann <daniel.baumann@open-infrastructure.net> | 2019-04-12 19:22:40 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@open-infrastructure.net> | 2019-04-13 04:05:15 +0000 |
commit | 2b5d28ac59346523440d2b3977ea8b69d316d53f (patch) | |
tree | 6cacad0c5e7273d2ccc1b2f7614bd75df3059af5 | |
parent | Unmount bind mounts in reverse order to allow nested bind mounts. (diff) | |
download | compute-tools-2b5d28ac59346523440d2b3977ea8b69d316d53f.tar.xz compute-tools-2b5d28ac59346523440d2b3977ea8b69d316d53f.zip |
Using trap function to unmount bind mounts etc. on exit with or without error.
Signed-off-by: Daniel Baumann <daniel.baumann@open-infrastructure.net>
-rwxr-xr-x | share/scripts/debconf | 228 |
1 files changed, 123 insertions, 105 deletions
diff --git a/share/scripts/debconf b/share/scripts/debconf index 35b5969..b4b5e18 100755 --- a/share/scripts/debconf +++ b/share/scripts/debconf @@ -126,6 +126,124 @@ then exit 1 fi +Start () +{ + # Mounting rw bind mounts + if [ -n "${BIND}" ] + then + BINDS="$(echo ${BIND} | sed -e 's|;| |g')" + + for ENTRY in ${BINDS} + do + SOURCE="$(echo ${ENTRY} | awk -F: '{ print $1 }')" + TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" + + mkdir -p "${SOURCE}" + mkdir -p "${MACHINES}/${NAME}/${TARGET}" + + mount -o bind "${SOURCE}" "${MACHINES}/${NAME}/${TARGET}" + done + fi + + # Mounting ro bind mounts + if [ -n "${BIND_RO}" ] + then + BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g')" + + for ENTRY in ${BINDS_RO} + do + SOURCE="$(echo ${ENTRY} | awk -F: '{ print $1 }')" + TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" + + mkdir -p "${SOURCE}" + mkdir -p "${MACHINES}/${NAME}/${TARGET}" + + mount -o rbind "${SOURCE}" "${MACHINES}/${NAME}/${TARGET}" + done + fi + + # Mounting overlay mounts + if [ -n "${CNT_OVERLAY}" ] + then + CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" + + for CNT_OVERLAY in ${CNT_OVERLAYS} + do + DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" + DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" + DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" + DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" + + for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + do + mkdir -p "${DIRECTORY}" + done + + mount -t overlay overlay-${NAME} -olowerdir="${DIRECTORY_LOWER}",upperdir="${DIRECTORY_UPPER}",workdir="${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + done + fi + + # Trap function + trap 'Stop' EXIT HUP INT QUIT TERM +} + +Stop () +{ + # Unmounting overlay mounts + if [ -n "${CNT_OVERLAY}" ] + then + CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" + + for CNT_OVERLAY in ${CNT_OVERLAYS} + do + DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" + DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" + DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" + DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" + + umount -f "${DIRECTORY_MERGED}" > /dev/null 2>&1 || true + + for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + do + rmdir --ignore-fail-on-non-empty --parents ${DIRECTORY} > /dev/null 2>&1 || true + done + done + fi + + # Unmounting ro bind mounts + if [ -n "${BIND_RO}" ] + then + # unmount in reverse order to allow nested bind mounts + BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g' | awk '{ for (i=NF; i>=1; i--) printf "%s ", $i; print ""}')" + + for ENTRY in ${BINDS_RO} + do + TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" + + umount -f "${MACHINES}/${NAME}/${TARGET}" > /dev/null 2>&1 || true + done + fi + + # Unmounting rw bind mounts + if [ -n "${BIND}" ] + then + # unmount in reverse order to allow nested bind mounts + BINDS="$(echo ${BIND} | sed -e 's|;| |g' | awk '{ for (i=NF; i>=1; i--) printf "%s ", $i; print ""}')" + + for ENTRY in ${BINDS} + do + TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" + + umount -f "${MACHINES}/${NAME}/${TARGET}" > /dev/null 2>&1 || true + done + fi + + # Unmounting pseudo-filesystems + umount -f "${DIRECTORY}/dev/pts" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}/proc" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}/sys" > /dev/null 2>&1 || true +} + Chroot () { CHROOT="${1}" @@ -187,9 +305,9 @@ Cleanup_system () rm -f "${DIRECTORY}/usr/sbin/policy-rc.d" # Unmount pseudo-filesystems - umount "${DIRECTORY}/dev/pts" - umount "${DIRECTORY}/proc" - umount "${DIRECTORY}/sys" + umount -f "${DIRECTORY}/dev/pts" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}/proc" > /dev/null 2>&1 || true + umount -f "${DIRECTORY}/sys" > /dev/null 2>&1 || true } Debconf () @@ -1011,60 +1129,7 @@ Cleanup_system "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}" mkdir -p "${MACHINES}" cp -a "${CACHE}/${DISTRIBUTION}_${ARCHITECTURE}" "${MACHINES}/${NAME}" -# Mounting rw bind mounts -if [ -n "${BIND}" ] -then - BINDS="$(echo ${BIND} | sed -e 's|;| |g')" - - for ENTRY in ${BINDS} - do - SOURCE="$(echo ${ENTRY} | awk -F: '{ print $1 }')" - TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" - - mkdir -p "${SOURCE}" - mkdir -p "${MACHINES}/${NAME}/${TARGET}" - - mount -o bind "${SOURCE}" "${MACHINES}/${NAME}/${TARGET}" - done -fi - -# Mounting ro bind mounts -if [ -n "${BIND_RO}" ] -then - BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g')" - - for ENTRY in ${BINDS_RO} - do - SOURCE="$(echo ${ENTRY} | awk -F: '{ print $1 }')" - TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" - - mkdir -p "${SOURCE}" - mkdir -p "${MACHINES}/${NAME}/${TARGET}" - - mount -o rbind "${SOURCE}" "${MACHINES}/${NAME}/${TARGET}" - done -fi - -# Mounting overlay mounts -if [ -n "${CNT_OVERLAY}" ] -then - CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" - - for CNT_OVERLAY in ${CNT_OVERLAYS} - do - DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" - DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" - DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" - DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" - - for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" - do - mkdir -p "${DIRECTORY}" - done - - mount -t overlay overlay-${NAME} -olowerdir="${DIRECTORY_LOWER}",upperdir="${DIRECTORY_UPPER}",workdir="${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" - done -fi +Start Configure_system "${MACHINES}/${NAME}" Configure_network "${MACHINES}/${NAME}" @@ -1073,54 +1138,7 @@ Cleanup_system "${MACHINES}/${NAME}" Commands "${MACHINES}/${NAME}" -# Unmounting overlay mounts -if [ -n "${CNT_OVERLAY}" ] -then - CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" - - for CNT_OVERLAY in ${CNT_OVERLAYS} - do - DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" - DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" - DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" - DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" - - umount -f "${DIRECTORY_MERGED}" - - for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" - do - rmdir --ignore-fail-on-non-empty --parents ${DIRECTORY} > /dev/null 2>&1 || true - done - done -fi - -# Unmounting ro bind mounts -if [ -n "${BIND_RO}" ] -then - # unmount in reverse order to allow nested bind mounts - BINDS_RO="$(echo ${BIND_RO} | sed -e 's|;| |g' | awk '{ for (i=NF; i>=1; i--) printf "%s ", $i; print ""}')" - - for ENTRY in ${BINDS_RO} - do - TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" - - umount "${MACHINES}/${NAME}/${TARGET}" - done -fi - -# Unmounting rw bind mounts -if [ -n "${BIND}" ] -then - # unmount in reverse order to allow nested bind mounts - BINDS="$(echo ${BIND} | sed -e 's|;| |g' | awk '{ for (i=NF; i>=1; i--) printf "%s ", $i; print ""}')" - - for ENTRY in ${BINDS} - do - TARGET="$(echo ${ENTRY} | awk -F: '{ print $2 }')" - - umount "${MACHINES}/${NAME}/${TARGET}" - done -fi +Stop # remove debconf temporary files rm --preserve-root --one-file-system -rf "${DEBCONF_TMPDIR}" |