chroot-shell/_do_self_checks.sh

117 lines
2.7 KiB
Bash
Executable File

#!/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