Files
init.sh/lib/chroot.sh

52 lines
1.9 KiB
Bash

#!/bin/bash
# ------------------------------------------------------------------------------
# Chroot system functions
# This file is part of the init.sh project
# Copyright (c) 2019-2024 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# If chrooted, we need to bootstrap to a new copy of our directory tree
chroot_bootstrap()
{
if [[ ! -d "$CHROOT_PATH" ]]; then
prnt E "The path given to chroot don't exists."
die 14
fi
if [[ ! -d "$CHROOT_PATH/tmp" ]]; then
prnt E "The target filesystem doesn't seems to be a valid installation."
die 15
fi
local tmpdir=$(mktemp -d "$CHROOT_PATH/tmp/init.sh-XXXX")
local bootstrap_items="conf lib modules repo bash.rc init.sh prepost.d"
if [[ $RESUME == true ]]; then
bootstrap_items="$bootstrap_items $STAGE_FILE"
fi
prnt I "Preparing root change."
cp -av $bootstrap_items "$tmpdir"
prnt I "Changing root and starting a fork of init.sh..."
# on the following line, true allows to correctly exit in case of error since
# errors are managed by the chrooted environment
chroot "$CHROOT_PATH" /bin/bash -c 'CHROOT_DONE=true; "$tmpdir/init.sh" "$@"' || true
# If stage file still exists we copy it to be able to resume later
if [[ -e "$tmpdir/$(basename "$STAGE_FILE")" ]]; then
cp "$tmpdir/$(basename "$STAGE_FILE")" "$STAGE_FILE"
fi
prnt I "Back to host system and clean up."
rm -rf "$tmpdir"
}
export -f chroot_bootstrap
# ------------------------------------------------------------------------------
# EOF