improved code quality, few bug fixes

This commit is contained in:
Geoffray Levasseur
2023-08-02 11:36:01 +02:00
parent e16ce485f9
commit cd35f52509
29 changed files with 174 additions and 168 deletions

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Error management functions
# This file is part of the init.sh project
@@ -7,7 +8,7 @@
# The complete license agreement can be obtained at:
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Exit with error
@@ -29,7 +30,7 @@ function die()
unset errorcode
# Put the trigger back (only executed with --keepgoing)
trap "error ${LINENO}" ERR
trap 'error ${LINENO}' ERR
}
export -f die
@@ -73,8 +74,8 @@ trap "error ${LINENO}; backtrace; err_exit" ERR
function err_exit
{
if [[ $KEEPGOING != true ]]; then
if [[ -f $tmpfile ]]; then
rm -f $tmpfile
if [[ -f "$tmpfile" ]]; then
rm -f "$tmpfile"
fi
exit 255
fi
@@ -94,7 +95,7 @@ function backtrace
printf '%15s() %s:%d\n' \
"$func" "${BASH_SOURCE[$i]}" "${BASH_LINENO[ (( $i - 1)) ]}"
fi
let i++ || true
(( i++ )) || true
done
unset func i
echo "=============================="
@@ -120,7 +121,7 @@ noerror()
fi
echo $?
trap "error ${LINENO}" ERR
trap 'error ${LINENO}' ERR
set -o errexit
}
export -f noerror

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Chroot system functions
# This file is part of the init.sh project
@@ -13,36 +14,36 @@
# If chrooted, we need to bootstrap to a new copy of our directory tree
chroot_bootstrap()
{
if [[ ! -d $CHROOT_PATH ]]; then
if [[ ! -d "$CHROOT_PATH" ]]; then
prnt E "The path given to chroot don't exists."
die 14
fi
if [[ ! -d $CHROOT_PATH/tmp ]]; then
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 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
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
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
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
rm -rf "$tmpdir"
}
# EOF

View File

@@ -1,3 +1,4 @@
#!/bin/env bash
# ------------------------------------------------------------------------------
# Main program functions
# This file is part of the init.sh project
@@ -14,39 +15,46 @@
# errors with immediate exit.
read_commandline()
{
syntax_error()
{
prnt E "Error while analysing command line parameters."
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
local params=''
params=$(getopt -n init.sh -o hvm:cjkrRDoPl:f:s \
--long help,version,module:,check-only,jump,keep-going,resume,no-root-check,no-deps,offline,no-proxy,logfile:,file:,shell,chroot,cron \
-- "$@")
eval set -- "$params"
while true; do
case $1 in
"-h"|"--help")
disp_help
shift
exit 0
;;
"-v"|"--version")
show_version
shift
exit 0
;;
"-m"|"--module")
local want_module=true
if [[ -z $MANUAL_MODULE_LIST ]]; then
export MANUAL_MODULE_LIST=$2
else
prnt E "A module list have already been given!"
prnt E "Commande line only tolerate one --module parameter."
die 1 --force
fi
shift 2
;;
"-c"|"--check-only")
export CHECK_ONLY=true
shift
;;
"-j"|"--jump")
export JUMP=true
shift
;;
"-k"|"--keep-going")
export KEEPGOING=true
shift
;;
"-r"|"--resume")
if [[ -s $STAGE_FILE ]]; then
@@ -56,91 +64,68 @@ read_commandline()
prnt E "Without it, resuming is impossible."
die 17 --force
fi
shift
;;
"-R"|"--no-root-check")
export NO_ROOT_CHECK=true
shift
;;
"-D"|"--no-deps")
export NO_DEPS=true
shift
;;
"-o"|"--offline")
export OFFLINE=true
shift
;;
"-P"|"--no-proxy")
export NO_PROXY=true
shift
;;
"-l"|"--logfile")
local want_logfile=true
if [[ -z $NEW_LOGFILE ]]; then
export NEW_LOGFILE=$2
else
prnt E "Impossible to specify several log files."
die 1 --force
fi
shift 2
;;
"-f"|"--file")
local want_conffile=true
export CONFFILES="$CONFFILES $opt"
shift 2
;;
"-s"|"--shell")
export RUN_SHELL=true
shift
;;
"--chroot")
local want_chroot=true
if [[ -z $CHROOT_PATH ]]; then
export CHROOT_PATH=$2
else
prnt E "A chroot path have already been given."
die 1 --force
fi
shift 2
;;
"--cron")
export CRON_MODE=true
shift
;;
--)
shift
break
;;
*)
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 "A module list have already been given!"
prnt E "Commande line only tolerate one --module parameter."
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 "Impossible to specify several log files."
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 "A chroot path have already been given."
die 1 --force
fi
else
prnt E "Unknow parameter \"$opt\"."
die 1 --force
fi
if [[ -n $1 ]]; then
prnt E "Unknow parameter \"$1\" !"
die 1
fi
break
;;
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 want_chroot
}
export -f read_commandline
@@ -180,11 +165,12 @@ process_commandline_and_vars()
# Configure module list
if [[ -n $MANUAL_MODULE_LIST ]]; then
prnt W "A manual module list will be used."
export MODULE_LIST=$(echo $MANUAL_MODULE_LIST | sed "s/,/ /g")
prnt W "A manual module list will be used:"
export MODULE_LIST=${MANUAL_MODULE_LIST//,/ /g}
prnt m " * $MODULE_LIST"
fi
# Check for module list existance and basic syntax
# Check for module list exis<tance and basic syntax
if [[ -n $MODULE_LIST ]]; then
for mod in $MODULE_LIST; do
if [[ $mod =~ ['-!@#$%\&*=+'] ]]; then

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Disks and partitions manipulation function
# This file is part of the init.sh project
@@ -82,7 +83,7 @@ mkparts()
local tmpfile=$(mktemp sfd.XXXX)
if [[ -n $1 ]]; then
# For each given size we make a partition
for $part in $@; do
for part in $@; do
# If size is zero we interpret it as all available space
if [[ $part == 0 ]]; then
echo ",,L" >> $tmpfile

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Some display functions and defines color codes
# This file is part of the init.sh project

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# File manipulation function
# This file is part of the init.sh project
@@ -34,7 +35,7 @@ backup_dist()
if [[ -L ${file} ]]; then
# With symbolik links we call again backup_dist to treat target
prnt I "Following the symbolic link $file to do a proper backup..."
backup_dist $(readlink -f ${file})
backup_dist $(readlink -f "${file}")
elif [[ -f ${file} ]]; then
prnt I "Creating a backup of ${file} on $tmstmp..."
cp -av $file ${file}.dist.${tmstmp}
@@ -134,7 +135,7 @@ install_file()
prnt E "install_file(): At least two arguments are required."
die 11
fi
if [[ $(echo $@ | grep "\*\|\?") ]]; then
if [[ -n $(echo $@ | grep "\*\|\?") ]]; then
prnt E "install_file(): Wildcards are not authorized."
die 7
fi
@@ -215,7 +216,7 @@ is_dir_empty()
return 0
fi
nbfiles=$(ls -a1 $dir | egrep -v '^.$|^..$' | wc -l)
nbfiles=$(ls -a1 $dir | grep -Evc '^.$|^..$')
if [[ $nbfiles -eq 0 ]]; then
return 0
fi
@@ -240,7 +241,7 @@ patch_file()
# Create a sub-process, to avoid bash environment pollution
(
local varlist= pattern=
local varlist='' pattern=''
if [[ $# -eq 0 ]] ; then
pattern="-e s/<\(.*\)>/\$\1\$\1/g"
else
@@ -249,7 +250,6 @@ patch_file()
if ! declare -p $var >/dev/null 2>&1 ; then
local $var=$(eval echo \$$var)
fi
export $var
pattern="$pattern -e s/@$var@/\$$var/g"
varlist=$varlist\$$var
done

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Loaders for conf and prepost functions
# This file is part of the init.sh project

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Network functions
# This file is part of the init.sh project

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Package manager integration
# This file is part of the init.sh project

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Services manipulation functions
# This file is part of the init.sh project

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Base support function
# This file is part of the init.sh project

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Various utilitary functions
# This file is part of the init.sh project
@@ -25,7 +26,7 @@ function_exists() {
die 11 --force
fi
if [[ $(LC_ALL=C type -t $1 | grep function) ]]; then
if [[ -n $(LC_ALL=C type -t $1 | grep function) ]]; then
return 0
else
return 1
@@ -42,7 +43,7 @@ get_mod_name()
prnt E "get_mod_name(): Bad number of parameters."
die 11 --force
fi
echo $(basename $1 | cut -f 1 -d '.')
basename $1 | cut -f 1 -d '.'
}
export -f get_mod_name
@@ -52,7 +53,7 @@ export -f get_mod_name
trim()
{
local string="$@"
echo "$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'<<<"${string}")"
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'<<<"${string}"
unset string
}
export -f trim

View File

@@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Version determination function
# This file is part of the init.sh project
@@ -37,7 +38,7 @@ get_os_version()
unset maj min
fi
# Return values on stdout
# Return values on stdout (awk used to retreave primary codename when using testing or unstable)
echo ${ID,,} ${VERSION_ID} $(echo ${VERSION_CODENAME,,} | awk '{print $1}')