lxd: Clean up mountpoints and temp files on failure
Attempt to unmount rootfs mountpoints and clean up temporary files even
if the build is unsuccessful. If unmounting fails, then cleanup is
skipped to avoid deleting host system files.
BUG=none
TEST=kokoro
Change-Id: I586d3f2afaaf39f11e1680f1123864f0a8cdc202
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/containers/cros-container-guest-tools/+/3379639
Reviewed-by: David Munro <davidmunro@google.com>
Reviewed-by: Fergus Dall <sidereal@google.com>
Tested-by: James Ye <jamesye@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: James Ye <jamesye@google.com>
diff --git a/lxd/build_debian_container.sh b/lxd/build_debian_container.sh
index a78cb7a..2f158d8 100755
--- a/lxd/build_debian_container.sh
+++ b/lxd/build_debian_container.sh
@@ -8,6 +8,32 @@
LXD="/snap/bin/lxd"
LXC="/snap/bin/lxc"
+cleanup() {
+ local tempdir="$1"
+ local rootfs="$2"
+
+ unmount_all "${rootfs}" || true
+ # Unmounting may fail because paths were not mounted.
+ # Cleanup is skipped if any mounted paths remain in the rootfs.
+ if grep -F -q "${rootfs}" /proc/self/mounts; then
+ echo "Failed to unmount filesystems, skipping cleanup of ${tempdir}."
+ exit 1
+ fi
+
+ rm -rf "${tempdir}"
+}
+
+unmount_all() {
+ local rootfs="$1"
+
+ umount "${rootfs}/tmp"
+ umount "${rootfs}/run"
+ umount "${rootfs}/proc"
+ umount "${rootfs}/dev"
+ umount "${rootfs}/etc/resolv.conf"
+ umount "${rootfs}/opt/google/cros-containers"
+}
+
build_containers() {
local arch=$1
local src_root=$2
@@ -16,10 +42,21 @@
local release=$5
local base_image="images:debian/${release}/${arch}"
- local tempdir="$(mktemp -d)"
- ${LXC} image export "${base_image}" "${tempdir}/image"
+ local tempdir
+ tempdir="$(mktemp -d)"
local rootfs="${tempdir}/rootfs"
+ trap "cleanup \"${tempdir}\" \"${rootfs}\"" EXIT
+
+ # Make dummy sommelier paths for update-alternatives.
+ local dummy_path="${tempdir}/cros-containers"
+ mkdir -p "${dummy_path}"/{bin,lib}
+ touch "${dummy_path}"/bin/sommelier
+ touch "${dummy_path}"/lib/swrast_dri.so
+ touch "${dummy_path}"/lib/virtio_gpu_dri.so
+
+ ${LXC} image export "${base_image}" "${tempdir}/image"
+
unsquashfs -d "${rootfs}" "${tempdir}/image.root"
chmod 0755 "${rootfs}"
@@ -32,9 +69,6 @@
"${results_dir}" \
"${apt_dir}"
done
-
- rm -rf "${tempdir}"
-
}
build_and_export() {
@@ -47,7 +81,7 @@
local apt_dir=$7
mkdir -p "${rootfs}/opt/google/cros-containers"
- mount --bind /tmp/cros-containers "${rootfs}/opt/google/cros-containers"
+ mount --bind "${dummy_path}" "${rootfs}/opt/google/cros-containers"
mount --bind /run/resolvconf/resolv.conf "${rootfs}/etc/resolv.conf"
mount --bind /dev "${rootfs}/dev"
mount -t proc none "${rootfs}/proc"
@@ -78,12 +112,7 @@
"${arch}"
fi
- umount "${rootfs}/tmp"
- umount "${rootfs}/run"
- umount "${rootfs}/proc"
- umount "${rootfs}/dev"
- umount "${rootfs}/etc/resolv.conf"
- umount "${rootfs}/opt/google/cros-containers"
+ unmount_all "${rootfs}"
rm -rf "${rootfs}/opt/google"
# Repack into 2 tarballs for distribution via simplestreams.
@@ -143,13 +172,6 @@
return 1
fi
- # Make dummy sommelier paths for update-alternatives.
- local dummy_path="/tmp/cros-containers"
- mkdir -p "${dummy_path}"/{bin,lib}
- touch "${dummy_path}"/bin/sommelier
- touch "${dummy_path}"/lib/swrast_dri.so
- touch "${dummy_path}"/lib/virtio_gpu_dri.so
-
build_containers "${arch}" \
"${src_root}" \
"${results_dir}" \