V8.00.000.2025.06.17
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 54s

Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
This commit is contained in:
2025-07-30 18:49:39 +02:00
parent 4c804e577f
commit 9d1d6581b5
8 changed files with 278 additions and 105 deletions

View File

@@ -14,9 +14,9 @@ guard_sourcing
#######################################
# Use do_in_target() for:
# simple commands (e.g., dpkg, ln, mkdir, apt, etc.)
# - simple commands (e.g., dpkg, ln, mkdir, apt, etc.).
# Use do_in_target_script() for:
# all shell scripts, redirects, pipes, conditions, loops, or subshells
# - all shell scripts, redirects, pipes, conditions, loops, or subshells.
#######################################
#######################################
@@ -32,34 +32,52 @@ guard_sourcing
# ERR_CHRT_COMMAND: on failure
#######################################
do_in_target() {
declare var_chroot_target="$1"
shift
declare var_chroot_target="$1"; shift
declare -a ary_chroot_command=("$@")
declare -r var_default_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -i var_chroot_rc=0
if (( ${#ary_chroot_command[@]} == 0 )); then
do_log "emergency" "file_only" "Empty command passed to 'do_in_target()'."
do_log "emergency" "file_only" "1080() Empty command passed to 'do_in_target()'."
return "${ERR_CHRT_COMMAND}"
fi
if chroot "${var_chroot_target}" /usr/bin/env -i \
HOME=/root \
PATH=/usr/sbin:/usr/bin:/sbin:/bin \
TERM="${TERM}" \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8 \
"${ary_chroot_command[@]}"
then
do_log "info" "file_only" "Success: chroot '${var_chroot_target}': '${ary_chroot_command[*]}'."
return 0
else
do_log "emergency" "file_only" "Failed: chroot '${var_chroot_target}': '${ary_chroot_command[*]}'."
if ! chroot "${var_chroot_target}" /usr/bin/env -i PATH="${var_default_path}" which "${ary_chroot_command[0]}" &>/dev/null; then
do_log "emergency" "file_only" "1080() Binary: '${ary_chroot_command[0]}' not found in target 'PATH=${var_default_path}'."
do_log "emergency" "file_only" "1080() Command: [chroot ${var_chroot_target} /usr/bin/env -i PATH=${var_default_path} which ${ary_chroot_command[0]} &>/dev/null]."
return "${ERR_CHRT_COMMAND}"
fi
if ! chroot "${var_chroot_target}" /usr/bin/env -i \
HOME="/root" \
PATH="${var_default_path}" \
TERM="${TERM}" \
LANG="C.UTF-8" \
LC_ALL="C.UTF-8" \
"${ary_chroot_command[@]}"
then
var_chroot_rc="${?}"
do_log "emergency" "file_only" "1080() Command: [chroot ${var_chroot_target} /usr/bin/env -i HOME=/root PATH=${var_default_path} TERM=${TERM} LANG=C.UTF-8 LC_ALL=C.UTF-8 ${ary_chroot_command[*]}] failed."
do_log "emergency" "file_only" "1080() Command: [Return code: '${var_chroot_rc}']."
return "${ERR_CHRT_COMMAND}"
else
do_log "info" "file_only" "1080() Command: [chroot ${var_chroot_target} /usr/bin/env -i HOME=/root PATH=${var_default_path} TERM=${TERM} LANG=C.UTF-8 LC_ALL=C.UTF-8 ${ary_chroot_command[*]}] successful."
return 0
fi
}
#######################################
# Execute a full shell script line inside the chroot via bash -c.
# Supports interactive debug shell on error.
# TODO: Supports interactive debug shell on error.
# Globals:
# ERR_CHRT_COMMAND
# TERM
@@ -72,43 +90,44 @@ do_in_target() {
# ERR_CHRT_COMMAND: on failure
#######################################
do_in_target_script() {
declare var_chroot_target="$1"
shift
declare var_chroot_script="$1"
declare var_chroot_target="$1"; shift
declare var_chroot_script="$1"
declare -r var_default_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -i var_chroot_rc=0
if [[ -z "${var_chroot_script}" ]]; then
do_log "emergency" "file_only" "Empty command passed to 'do_in_target_script()'."
do_log "emergency" "file_only" "1080() Empty command passed to 'do_in_target_script()'."
return "${ERR_CHRT_COMMAND}"
fi
# do_log "debug" "file_only" "Evaluating chroot script in '${var_chroot_target}': '${var_chroot_script}'."
if chroot "${var_chroot_target}" /usr/bin/env -i \
HOME=/root \
PATH=/usr/sbin:/usr/bin:/sbin:/bin \
if ! chroot "${var_chroot_target}" /usr/bin/env -i \
HOME="/root" \
PATH="${var_default_path}" \
TERM="${TERM}" \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8 \
LANG="C.UTF-8" \
LC_ALL="C.UTF-8" \
/bin/bash -c "${var_chroot_script}"
then
do_log "info" "file_only" "Success: chroot '${var_chroot_target}': '${var_chroot_script}'."
return 0
else
declare -i var_chroot_rc="${?}"
do_log "emergency" "file_only" "Failure: chroot '${var_chroot_target}': '${var_chroot_script}'."
do_log "info" "file_only" "Return code: '${var_chroot_rc}'."
var_chroot_rc="${?}"
do_log "emergency" "file_only" "1080() Command: [chroot ${var_chroot_target} /usr/bin/env -i HOME=/root PATH=${var_default_path} TERM=${TERM} LANG=C.UTF-8 LC_ALL=C.UTF-8 /bin/bash -c ${var_chroot_script}] failed."
do_log "emergency" "file_only" "1080() Command: [Return code: '${var_chroot_rc}']."
return "${ERR_CHRT_COMMAND}"
# TODO: Test with Dialog Wrapper in interactive mode.
# TODO: Call clean screen first to terminate dialog wrapper !
#if [[ "${DEBUG_INTERACTIVE}" == "true" ]]; then
# do_log "warning" "true" "Launching interactive debug shell in chroot: '${var_chroot_target}'."
# chroot "${var_chroot_target}" /bin/bash -l
#fi
return "${ERR_CHRT_COMMAND}"
else
do_log "info" "file_only" "1080() Command: [chroot ${var_chroot_target} /usr/bin/env -i HOME=/root PATH=${var_default_path} TERM=${TERM} LANG=C.UTF-8 LC_ALL=C.UTF-8 /bin/bash -c ${var_chroot_script}] successful."
return 0
fi
}