diff --git a/init.sh b/init.sh index 6e50dfd..a9b0aa4 100755 --- a/init.sh +++ b/init.sh @@ -36,7 +36,7 @@ export LC_ALL=C export LANG=C # Version of init -export VERSION="0.99.13" +export VERSION="0.99.14" # Store script's path (realpath -s resolve symlinks if init.sh is a symlink) export MYPATH=$(dirname $(realpath -s $0)) diff --git a/lib/chroot.sh b/lib/chroot.sh new file mode 100644 index 0000000..f2e89fa --- /dev/null +++ b/lib/chroot.sh @@ -0,0 +1,46 @@ +# ------------------------------------------------------------------------------ +# Chroot system functions +# This file is part of the init.sh project +# Copyright (c) 2019-2021 Geoffray Levasseur +# ------------------------------------------------------------------------------ +# 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..." + chroot $CHROOT_PATH /bin/bash -c 'CHROOT_DONE=true; $tmpdir/init.sh $@' + + # 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 +} + + diff --git a/lib/zzz_main_fct.sh b/lib/command_line.sh similarity index 63% rename from lib/zzz_main_fct.sh rename to lib/command_line.sh index ae1e573..6ebb90e 100644 --- a/lib/zzz_main_fct.sh +++ b/lib/command_line.sh @@ -142,133 +142,6 @@ read_commandline() export -f read_commandline -# ------------------------------------------------------------------------------ -# 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..." - chroot $CHROOT_PATH /bin/bash -c 'CHROOT_DONE=true; $tmpdir/init.sh $@' - - # 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 -} - -# ------------------------------------------------------------------------------ -# Automatically load system specific configuration if file exist in the -# following order: -# 1) auto/arch.conf.sh -# 2) auto/distro.conf.sh -# 3) auto/distro-arch.conf.sh -# 4) auto/distro-version.conf.sh -# 5) auto/distro-codename.conf.sh (if sys_code defined) -# 6) auto/distro-version-arch.conf.sh -# 7) auto/distro-codename-arch.conf.sh (if sys_code defined) -load_autoconf() -{ - local prefix="$MYPATH/conf/auto" - - if [[ -e $prefix/$SYS_ARCH.conf.sh ]]; then - . $prefix/$SYS_ARCH.conf.sh - fi - if [[ -e $prefix/$SYS_DIST.conf.sh ]]; then - . $prefix/$SYS_DIST.conf.sh - fi - if [[ -e $prefix/$SYS_DIST-$SYS_ARCH.conf.sh ]]; then - . $prefix/$SYS_DIST-$SYS_ARCH.conf.sh - fi - if [[ -e $prefix/$SYS_DIST-$SYS_VER.conf.sh ]]; then - . $prefix/$SYS_DIST-$SYS_VER.conf.sh - fi - if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE.conf.sh ]]; then - . $prefix/$SYS_DIST-$SYS_CODE.conf.sh - fi - if [[ -e $prefix/$SYS_DIST-$SYS_VER-$SYS_ARCH.conf.sh ]]; then - . $prefix/$SYS_DIST-$SYS_VER-$SYS_ARCH.conf.sh - fi - if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh ]]; then - . $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh - fi - - unset prefix -} -export -f load_autoconf - - -# ------------------------------------------------------------------------------ -# Load configuration with the following priorities: -# 1) Those given on command line, if any -# 2) /conf/.conf (Hostname based and specific) -# 3) /conf/init.conf.sh (Generic default) -load_configuration() -{ - if [[ -n $CONFFILES ]]; then - local f= - for f in $CONFFILES; do - prnt I "Loading $f manually specified." - if [[ -s $f ]]; then - . $f - else - prnt E "The $f file doesn't exists or is empty." - die 6 --force - fi - done - unset f - else - prnt I "Loading configuration..." - if [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then - prnt I "A specific configuration will be used." - . $MYPATH/conf/$HOSTNAME.conf.sh - else - if [[ -e $MYPATH/conf/init.conf.sh ]]; then - prnt I "A generic configuration will be used." - . $MYPATH/conf/init.conf.sh - else - prnt E "No configuration found, impossible to continue." - die 6 --force - fi - fi - fi -} -export -f load_configuration - - -# ------------------------------------------------------------------------------ -# Load pre and post actions for package manager -load_prepost_actions() -{ - local prepost= - for prepost in $MYPATH/prepost.d/*.sh; do - prnt I "Loading prepost actions in $prepost ..." - . $prepost - done - unset prepost -} -export -f load_prepost_actions - - # ------------------------------------------------------------------------------ # Do deeper command line analysis to detect unconsistancies process_commandline_and_vars() diff --git a/lib/loaders.sh b/lib/loaders.sh new file mode 100644 index 0000000..0ab96b0 --- /dev/null +++ b/lib/loaders.sh @@ -0,0 +1,104 @@ +# ------------------------------------------------------------------------------ +# Loaders for conf and prepost functions +# This file is part of the init.sh project +# Copyright (c) 2019-2021 Geoffray Levasseur +# ------------------------------------------------------------------------------ +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# https://opensource.org/licenses/BSD-3-Clause +# ------------------------------------------------------------------------------ + + +# ------------------------------------------------------------------------------ +# Automatically load system specific configuration if file exist in the +# following order: +# 1) auto/arch.conf.sh +# 2) auto/distro.conf.sh +# 3) auto/distro-arch.conf.sh +# 4) auto/distro-version.conf.sh +# 5) auto/distro-codename.conf.sh (if sys_code defined) +# 6) auto/distro-version-arch.conf.sh +# 7) auto/distro-codename-arch.conf.sh (if sys_code defined) +load_autoconf() +{ + local prefix="$MYPATH/conf/auto" + + if [[ -e $prefix/$SYS_ARCH.conf.sh ]]; then + . $prefix/$SYS_ARCH.conf.sh + fi + if [[ -e $prefix/$SYS_DIST.conf.sh ]]; then + . $prefix/$SYS_DIST.conf.sh + fi + if [[ -e $prefix/$SYS_DIST-$SYS_ARCH.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_ARCH.conf.sh + fi + if [[ -e $prefix/$SYS_DIST-$SYS_VER.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_VER.conf.sh + fi + if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_CODE.conf.sh + fi + if [[ -e $prefix/$SYS_DIST-$SYS_VER-$SYS_ARCH.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_VER-$SYS_ARCH.conf.sh + fi + if [[ -n $SYS_CODE && -e $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh ]]; then + . $prefix/$SYS_DIST-$SYS_CODE-$SYS_ARCH.conf.sh + fi + + unset prefix +} +export -f load_autoconf + + +# ------------------------------------------------------------------------------ +# Load configuration with the following priorities: +# 1) Those given on command line, if any +# 2) /conf/.conf (Hostname based and specific) +# 3) /conf/init.conf.sh (Generic default) +load_configuration() +{ + if [[ -n $CONFFILES ]]; then + local f= + for f in $CONFFILES; do + prnt I "Loading $f manually specified." + if [[ -s $f ]]; then + . $f + else + prnt E "The $f file doesn't exists or is empty." + die 6 --force + fi + done + unset f + else + prnt I "Loading configuration..." + if [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then + prnt I "A specific configuration will be used." + . $MYPATH/conf/$HOSTNAME.conf.sh + else + if [[ -e $MYPATH/conf/init.conf.sh ]]; then + prnt I "A generic configuration will be used." + . $MYPATH/conf/init.conf.sh + else + prnt E "No configuration found, impossible to continue." + die 6 --force + fi + fi + fi +} +export -f load_configuration + + +# ------------------------------------------------------------------------------ +# Load pre and post actions for package manager +load_prepost_actions() +{ + local prepost= + for prepost in $MYPATH/prepost.d/*.sh; do + prnt I "Loading prepost actions in $prepost ..." + . $prepost + done + unset prepost +} +export -f load_prepost_actions + +# EOF