# ------------------------------------------------------------------------------ # Main program 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 # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Read command line and set appropriate vars. Some basic checks can trigger # errors with immediate exit. read_commandline() { syntax_error() { prnt E "Erreur d'analyse de la ligne de commande, vérifiez vos paramètres." die 1 --force } # Processing command line options local want_module=false local want_logfile=false local want_conffile=false local want_chroot=false local opt= for opt in $@; do case $opt in "-h"|"--help") disp_help exit 0 ;; "-v"|"--version") show_version exit 0 ;; "-m"|"--module") local want_module=true ;; "-c"|"--check-only") export CHECK_ONLY=true ;; "-j"|"--jump") export JUMP=true ;; "-k"|"--keep-going") export KEEPGOING=true ;; "-r"|"--resume") if [[ -s $STAGE_FILE ]]; then export RESUME=true else prnt E "Le fichier d'état n'existe pas ou est vide !" prnt E "Sans ce fichier, la reprise n'est pas possible." die 1 --force fi ;; "-R"|"--no-root-check") export NO_ROOT_CHECK=true ;; "-D"|"--no-deps") export NO_DEPS=true ;; "-P"|"--no-proxy") export NO_PROXY=true ;; "-l"|"--logfile") local want_logfile=true ;; "-f"|"--file") local want_conffile=true ;; "-s"|"--shell") export RUN_SHELL=true ;; "--chroot") local want_chroot=true ;; "--cron") export CRON_MODE=true ;; *) if [[ $want_module == true ]]; then [[ $want_logfile == true ]] && synthax_error [[ $want_conffile == true ]] && synthax_error [[ $want_chroot == true ]] && synthax_error if [[ -z $MANUAL_MODULE_LIST ]]; then export MANUAL_MODULE_LIST=$opt want_module=false else prnt E "Une liste de module à déjà été fournie !" prnt E "La ligne de commande ne tolère qu'un seul paramètre --module." die 1 --force fi elif [[ $want_logfile == true ]]; then [[ $want_module == true ]] && synthax_error [[ $want_conffile == true ]] && synthax_error [[ $want_chroot == true ]] && synthax_error if [[ -z $NEW_LOGFILE ]]; then export NEW_LOGFILE=$opt want_logfile=false else prnt E "Il n'est pas possible de spécifier plusieurs fichiers de log." die 1 --force fi elif [[ $want_conffile == true ]]; then [[ $want_module == true ]] && synthax_error [[ $want_logfile == true ]] && synthax_error [[ $want_chroot == true ]] && synthax_error export CONFFILES="$CONFFILES $opt" want_logfile=false elif [[ $want_chroot == true ]]; then [[ $want_module == true ]] && synthax_error [[ $want_logfile == true ]] && synthax_error [[ $want_conffile == true ]] && synthax_error if [[ -z $CHROOT_PATH ]]; then export CHROOT_PATH=$opt want_chroot=false else prnt E "Un chemin pour chroot à déjà été fournis." die 1 --force fi else prnt E "Paramètre \"$opt\" non géré." die 1 --force fi ;; esac done unset opt # If those var are true at that point, something is wrong if [[ $want_logfile == true ]] || [[ $want_module == true ]] || [[ $want_conffile == true ]] || [[ $want_chroot == true ]]; then syntax_error fi unset want_conffile want_logfile want_module } 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 "Préparation du changement de racine." cp -av $bootstrap_items $tmpdir prnt I "Changement de racine et démarrage d'un fork d'init.sh..." chroot $CHROOT_PATH /bin/bash -c 'CHROOT_DONE=true; $tmpdir/init.sh $@' prnt I "Retours au système hote et nettoyage." 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 "Chargement de $f spécifié manuellement." if [[ -s $f ]]; then . $f else prnt E "Le fichier $f n'existe pas ou est vide." die 6 --force fi done unset f else prnt I "Chargement de la configuration..." if [[ -e $MYPATH/conf/$HOSTNAME.conf.sh ]]; then prnt I "Une configuration spécifique sera utilisé." . $MYPATH/conf/$HOSTNAME.conf.sh else if [[ -e $MYPATH/conf/init.conf.sh ]]; then prnt I "Une configuration générique sera utilisé." . $MYPATH/conf/init.conf.sh else prnt E "Aucune configuration trouvée, impossible de continuer." die 6 --force fi fi fi } export -f load_configuration # ------------------------------------------------------------------------------ # Do deeper command line analysis to detect unconsistancies process_commandline_and_vars() { # Check unconsistant parameters if [[ $CHECK_ONLY == true ]]; then if [[ $JUMP == true ]]; then prnt E "Les options --check-only et --jump s'excluent mutuellement !" die 1 --force fi if [[ $KEEPGOING == true ]]; then prnt E "Les options --keep-going et --check-only sont incompatible !" die 1 --force fi fi if [[ $RESUME == true ]]; then if [[ $CHECK_ONLY == true ]]; then prnt E "La reprise n'a pas de sens avec --check-only." die 1 --force fi if [[ $MANUAL_MODULE_LIST ]]; then prnt E "Le mode reprise ne fonctionne pas avec une liste de modules passé manuellement." die 1 --force fi fi if [[ $CRON_MODE == true ]]; then if [[ $CHECK_ONLY == true || $JUMP == true ]]; then prnt E "Des paramètres sont incompatibles avec le mode cron." die 15 --force fi fi # Configure module list if [[ -n $MANUAL_MODULE_LIST ]]; then prnt W "Une liste de modules manuelle sera utilisé." export MODULE_LIST=$(echo $MANUAL_MODULE_LIST | sed "s/,/ /g") fi # Check for module list existance and basic syntax if [[ -n $MODULE_LIST ]]; then if [[ $(echo $MODULE_LIST | grep '-') ]]; then prnt E "Le tiret est interdit dans les noms de module." die 5 fi else prnt E "Aucun module à exécuter !" die 5 fi } export -f process_commandline_and_vars # EOF