From 8b218188c6f780f71bec0cdc333c262402ae9408 Mon Sep 17 00:00:00 2001 From: david Date: Sat, 5 Dec 2020 17:36:51 +0000 Subject: [PATCH] Initial commit --- _do_self_checks.sh | 116 +++++++++++++++++++++++++++++++++++++++++++++ do_chroot.sh | 14 ++++++ do_umount.sh | 20 ++++++++ 3 files changed, 150 insertions(+) create mode 100755 _do_self_checks.sh create mode 100755 do_chroot.sh create mode 100644 do_umount.sh diff --git a/_do_self_checks.sh b/_do_self_checks.sh new file mode 100755 index 0000000..6c6245a --- /dev/null +++ b/_do_self_checks.sh @@ -0,0 +1,116 @@ +#!/bin/bash +set -Eeuo pipefail + +# Used to check environment variables have been set. +# In use it is called by do_chroot.sh and do_umount.sh +# +# Works safely, but not expected to be run independently. +# It mounts the FILE_SYSTEM_DIR specified in ARM_root.sh. +# If necessary, you can run do_umount.sh to unmount it. + +# Define chroot mounts, without a leading /. +# Needed by do_chroot.sh. +# Used as $i in: +# mount --bind /$i ./$i +# umount ./$i +export CHROOT_MOUNT_SET="dev dev/pts proc sys" + +# +# Check environment variables, then mount the filesystem. +# +main(){ + local CONFIG_FILE="${1}" + + check_config_file_is_specified "$CONFIG_FILE" + check_config_file_exists "$CONFIG_FILE" + check_config_file_is_executable "$CONFIG_FILE" + + . $CONFIG_FILE + + check_for_unset_variables "$CONFIG_FILE" + mount_filesystem_if_not_mounted "$CONFIG_FILE" +} + +check_config_file_is_specified() { + local CONFIG_FILE="${1}" + + if [ -z "$CONFIG_FILE" ]; then + echo "Error: CONFIG_FILE not specified." + exit 1 + fi +} + +check_config_file_exists() { + local CONFIG_FILE="${1}" + + if [ ! -f "$CONFIG_FILE" ]; then + cat <<-! + Error: $CONFIG_FILE is missing. + Expecting $CONFIG_FILE to set FILE_SYSTEM_DIR and POOL_PREFIX, + perhaps something like: + export FILE_SYSTEM_DIR=/space/amd64/devuan/beowulf/tasmotizer + + # On our zfs, /space/amd64/... is the mount of rpool/amd64/... + # RPOOL_ROOT is specified to convert from one to the other. + export POOL_PREFIX=/space + export RPOOL_ROOT=rpool + ! + exit 1 + fi +} + +check_config_file_is_executable() { + local CONFIG_FILE="${1}" + + if [ ! -x "$CONFIG_FILE" ]; then + echo "Error: $CONFIG_FILE is not executable." + exit 2 + fi +} + +check_for_unset_variables() { + local CONFIG_FILE="${1}" + + # Check for unset variables. Allow unset variables here. + set +u; + if [ -z "$FILE_SYSTEM_DIR" ]; then + echo "Error: FILE_SYSTEM_DIR was not set in $CONFIG_FILE." + exit 3 + fi; + set -u + + # Check for unset variables. Allow unset variables here. + set +u; + if [ -z "$POOL_PREFIX" ]; then + echo "Error: POOL_PREFIX was not set in $CONFIG_FILE." + exit 4 + fi; + set -u + + # Check for unset variables. Allow unset variables here. + set +u; + if [ -z "$RPOOL_ROOT" ]; then + echo "Error: RPOOL_ROOT was not set in $CONFIG_FILE." + exit 5 + fi; + set -u + +} + +mount_filesystem_if_not_mounted() { + local CONFIG_FILE="${1}" + + check_for_unset_variables $CONFIG_FILE + + # On our zfs, /space/amd64/... is the mount of rpool/amd64/... + # Get the pool name (change POOL_PREFIX for RPOOL_ROOT). + local RPOOL=${FILE_SYSTEM_DIR/${POOL_PREFIX}/${RPOOL_ROOT}} + + # Mount if not mounted + if [ -z "$(grep "${RPOOL} " /proc/mounts)" ]; then + sudo zfs mount $RPOOL + fi +} + +# Entry point. +main ./ARM_root.sh diff --git a/do_chroot.sh b/do_chroot.sh new file mode 100755 index 0000000..dbc746e --- /dev/null +++ b/do_chroot.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -Eeuo pipefail + +# Check and set environment, and mount file system. +. ./_do_self_checks.sh + +cd $FILE_SYSTEM_DIR +if [ ! -z "$CHROOT_MOUNT_SET" ]; then + for i in $CHROOT_MOUNT_SET; do + sudo mount --bind /$i ./$i + done +fi +sudo /usr/sbin/chroot . /bin/bash + diff --git a/do_umount.sh b/do_umount.sh new file mode 100644 index 0000000..029c308 --- /dev/null +++ b/do_umount.sh @@ -0,0 +1,20 @@ +# Deliberately not executable. Run as: +# bash ./do_umount + +# Check the environment and mount the file system if it is not mounted (to be +# sure it makes sense to umount it, rather than be umounting something else). +. ./_do_self_checks.sh + +( + # If the file system had been mounted before _do_self_checks.sh, above, + # the umounts might fail for being busy, so allow errors. + set +Ee + + cd $FILE_SYSTEM_DIR + for i in $CHROOT_MOUNT_SET; do + sudo umount --lazy ./$i + done 2>/dev/null + exit 0 +) + +sudo umount $FILE_SYSTEM_DIR