mirror of
https://github.com/armbian/build.git
synced 2026-11-26 23:29:56 +00:00
It must exist and belong to the building user. Refuse '/', the build directory and its parents, framework directories and special characters. Warn when it comes from the environment instead of the command line. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
358 lines
18 KiB
Bash
358 lines
18 KiB
Bash
#!/usr/bin/env bash
|
|
#
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
#
|
|
# Copyright (c) 2013-2026 Igor Pecovnik, igor@armbian.com
|
|
#
|
|
# This file is a part of the Armbian Build Framework
|
|
# https://github.com/armbian/build/
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
# This is called like this:
|
|
# declare -A -g ARMBIAN_PARSED_CMDLINE_PARAMS=()
|
|
# declare -a -g ARMBIAN_NON_PARAM_ARGS=()
|
|
# parse_cmdline_params "${@}" # which fills the vars above, being global.
|
|
function parse_cmdline_params() {
|
|
declare -A -g ARMBIAN_PARSED_CMDLINE_PARAMS=()
|
|
declare -a -g ARMBIAN_NON_PARAM_ARGS=()
|
|
|
|
# loop over the arguments parse them out
|
|
local arg
|
|
for arg in "${@}"; do
|
|
if [[ "${arg}" == *=* ]]; then # contains an equal sign. it's a param.
|
|
local param_name param_value param_value_desc
|
|
param_name=${arg%%=*}
|
|
param_value=${arg##*=}
|
|
param_value_desc="${param_value:-(empty)}"
|
|
# Sanity check for the param name; it must be a valid bash variable name.
|
|
if [[ "${param_name}" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
|
|
ARMBIAN_PARSED_CMDLINE_PARAMS["${param_name}"]="${param_value}" # For current run.
|
|
ARMBIAN_CLI_RELAUNCH_PARAMS["${param_name}"]="${param_value}" # For relaunch.
|
|
display_alert "Command line: parsed parameter '$param_name' to" "${param_value_desc}" "debug"
|
|
else
|
|
exit_with_error "Invalid cmdline param '${param_name}=${param_value_desc}'"
|
|
fi
|
|
elif [[ "x${arg}x" != "xx" ]]; then # not a param, not empty, store it in the non-param array for later usage
|
|
local non_param_value="${arg}"
|
|
local non_param_value_desc="${non_param_value:-(empty)}"
|
|
display_alert "Command line: storing non-param argument" "${non_param_value_desc}" "debug"
|
|
ARMBIAN_NON_PARAM_ARGS+=("${non_param_value}")
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Determines USERPATCHES_PATH, once, and makes it read-only. It defaults to ${SRC}/userpatches, and can be pointed
|
|
# elsewhere with USERPATCHES_PATH=xx, either as a cmdline param or in the environment. It is frozen afterwards, so that
|
|
# config lookup, patching, artifact hashing and the Docker mount can never disagree about which directory is in use.
|
|
# Call this after the early apply_cmdline_params_to_env, and before anything looks at USERPATCHES_PATH.
|
|
function cli_determine_userpatches_path() {
|
|
declare default_userpatches_path="${SRC}/userpatches"
|
|
declare userpatches_path="${USERPATCHES_PATH:-"${default_userpatches_path}"}"
|
|
|
|
# Relative paths are relative to ${SRC}; compile.sh has already cd'ed there. Lose any trailing slashes too.
|
|
if [[ "${userpatches_path}" != /* ]]; then
|
|
userpatches_path="${SRC}/${userpatches_path}"
|
|
fi
|
|
while [[ "${userpatches_path}" == */ && "${userpatches_path}" != "/" ]]; do
|
|
userpatches_path="${userpatches_path%/}"
|
|
done
|
|
|
|
# The default location is used as-is, and is created later on if missing. Anything else is checked thoroughly:
|
|
if [[ "${userpatches_path}" != "${default_userpatches_path}" ]]; then
|
|
# It has to exist already. A typo would otherwise silently produce a build without any of the user's customizations.
|
|
if [[ ! -d "${userpatches_path}" ]]; then
|
|
exit_with_error "USERPATCHES_PATH does not exist, or is not a directory. Create it first" "${userpatches_path}"
|
|
fi
|
|
|
|
# Absolute, physical and normalized, just like ${SRC} itself is; the checks below and the Docker bind-mount need that.
|
|
userpatches_path="$(realpath "${userpatches_path}")"
|
|
fi
|
|
|
|
if [[ "${userpatches_path}" != "${default_userpatches_path}" ]]; then # still not the default, after resolving it?
|
|
# Templates and a directory skeleton are created in this directory, and the owner of what is in there is reset to
|
|
# the calling user later on (see prepare_host_noninteractive), so refuse the obviously wrong locations.
|
|
if [[ "${userpatches_path}" == "/" || "${SRC}/" == "${userpatches_path}/"* ]]; then
|
|
exit_with_error "USERPATCHES_PATH can't be '/', nor the build directory itself or one of its parents" "${userpatches_path}"
|
|
fi
|
|
|
|
# Inside the build directory is fine (eg: userpatches-myproject), but not in the framework's own directories.
|
|
if [[ "${userpatches_path}/" == "${SRC}/"* ]]; then
|
|
declare inside_src="${userpatches_path#"${SRC}/"}"
|
|
case "${inside_src%%/*}" in
|
|
lib | config | extensions | packages | patch | tools | cache | output | .tmp | .git | .github)
|
|
exit_with_error "USERPATCHES_PATH can't be inside the '${inside_src%%/*}' directory of the build framework" "${userpatches_path}"
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
# The path ends up in Docker --mount specs, sed expressions and re-parsed command lines. Letters (of any language),
|
|
# digits and the likes of ./_-+@ are fine; whitespace and anything special to those is not.
|
|
declare forbidden_chars_regex='[][[:space:][:cntrl:]%&,:;|<>()*?{}!#=$`\\"'"'"']'
|
|
if [[ "${userpatches_path}" =~ ${forbidden_chars_regex} ]]; then
|
|
exit_with_error "USERPATCHES_PATH can't contain whitespace or special characters" "'${userpatches_path}'"
|
|
fi
|
|
|
|
# It must already belong to the user its contents will be chown'ed to; this keeps system and other people's directories out.
|
|
declare owner_uid="${SET_OWNER_TO_UID:-"${EUID}"}"
|
|
if [[ "${owner_uid}" != "0" && -z "$(find "${userpatches_path}" -maxdepth 0 -uid "${owner_uid}")" ]]; then
|
|
exit_with_error "USERPATCHES_PATH must be owned by the user running the build (uid ${owner_uid})" "${userpatches_path}"
|
|
fi
|
|
fi
|
|
|
|
# Keep the cmdline param, if any, in sync with the normalized value. The params are applied again after each config file
|
|
# is sourced, and a different value there would try to change the read-only variable. Also used when relaunching under sudo.
|
|
if [[ -n "${ARMBIAN_PARSED_CMDLINE_PARAMS["USERPATCHES_PATH"]+x}" ]]; then
|
|
ARMBIAN_PARSED_CMDLINE_PARAMS["USERPATCHES_PATH"]="${userpatches_path}"
|
|
ARMBIAN_CLI_RELAUNCH_PARAMS["USERPATCHES_PATH"]="${userpatches_path}"
|
|
fi
|
|
|
|
declare -g -r USERPATCHES_PATH="${userpatches_path}"
|
|
|
|
if [[ "${USERPATCHES_PATH}" != "${default_userpatches_path}" ]]; then
|
|
if [[ -n "${ARMBIAN_PARSED_CMDLINE_PARAMS["USERPATCHES_PATH"]+x}" ]]; then
|
|
display_alert "Using custom userpatches directory" "${USERPATCHES_PATH}" "info"
|
|
else # not asked for on the command line, so make sure it doesn't go unnoticed.
|
|
display_alert "Using custom userpatches directory, from the USERPATCHES_PATH environment variable" "${USERPATCHES_PATH}" "warn"
|
|
fi
|
|
elif [[ -n "${ARMBIAN_HOST_USERPATCHES_PATH:-}" ]]; then # under Docker; see cli_docker_run
|
|
display_alert "Using custom userpatches directory, bind-mounted from the host" "${ARMBIAN_HOST_USERPATCHES_PATH}" "info"
|
|
fi
|
|
}
|
|
|
|
# This can be called early on, or later after having sourced the config. Show what is happening.
|
|
# This is called:
|
|
# apply_cmdline_params_to_env "reason" # reads from global ARMBIAN_PARSED_CMDLINE_PARAMS
|
|
function apply_cmdline_params_to_env() {
|
|
declare -A -g ARMBIAN_PARSED_CMDLINE_PARAMS # Hopefully this has values
|
|
declare __my_reason="${1}"
|
|
shift
|
|
|
|
# Loop over the dictionary and apply the values to the environment.
|
|
for param_name in "${!ARMBIAN_PARSED_CMDLINE_PARAMS[@]}"; do
|
|
local param_value param_value_desc current_env_value
|
|
# get the current value from the environment
|
|
current_env_value="${!param_name}"
|
|
current_env_value_desc="${!param_name-(unset)}"
|
|
current_env_value_desc="${current_env_value_desc:-(empty)}"
|
|
# get the new value from the dictionary
|
|
param_value="${ARMBIAN_PARSED_CMDLINE_PARAMS[${param_name}]}"
|
|
param_value_desc="${param_value:-(empty)}"
|
|
|
|
# Compare, log, and apply.
|
|
if [[ -z "${!param_name+x}" ]] || [[ "${current_env_value}" != "${param_value}" ]]; then
|
|
display_alert "Applying cmdline param" "'$param_name': '${current_env_value_desc}' --> '${param_value_desc}' ${__my_reason}" "cmdline"
|
|
# use `declare -g` to make it global, we're in a function.
|
|
declare -g "${param_name}=${param_value}"
|
|
else
|
|
# rpardini: strategic amount of spacing in log files show the kinda neuroticism that drives me.
|
|
display_alert "Skip cmdline param" "'$param_name': already set to '${param_value_desc}' ${__my_reason}" "info"
|
|
fi
|
|
done
|
|
}
|
|
|
|
function armbian_prepare_cli_command_to_run() {
|
|
local command_id="${1}"
|
|
display_alert "Preparing to run command" "${command_id}" "debug"
|
|
ARMBIAN_COMMAND="${command_id}"
|
|
ARMBIAN_COMMAND_HANDLER="${ARMBIAN_COMMANDS_TO_HANDLERS_DICT[${command_id}]}"
|
|
ARMBIAN_COMMAND_VARS="${ARMBIAN_COMMANDS_TO_VARS_DICT[${command_id}]}"
|
|
# @TODO: actually set the vars...
|
|
|
|
local set_vars_for_command=""
|
|
if [[ "x${ARMBIAN_COMMAND_VARS}x" != "xx" ]]; then
|
|
# Loop over them, expanding...
|
|
for var_piece in ${ARMBIAN_COMMAND_VARS}; do
|
|
local var_decl="declare -g ${var_piece};"
|
|
display_alert "Command handler: setting variable" "${var_decl}" "debug"
|
|
set_vars_for_command+=" ${var_decl}"
|
|
done
|
|
fi
|
|
|
|
local pre_run_function_name="cli_${ARMBIAN_COMMAND_HANDLER}_pre_run"
|
|
local run_function_name="cli_${ARMBIAN_COMMAND_HANDLER}_run"
|
|
|
|
# Reset the functions.
|
|
function armbian_cli_pre_run_command() {
|
|
display_alert "No pre-run function for command" "${ARMBIAN_COMMAND}" "warn"
|
|
}
|
|
function armbian_cli_run_command() {
|
|
display_alert "No run function for command" "${ARMBIAN_COMMAND}" "warn"
|
|
}
|
|
|
|
# Materialize functions to call that specific command.
|
|
if [[ $(type -t "${pre_run_function_name}" || true) == function ]]; then
|
|
eval "$(
|
|
cat <<- EOF
|
|
display_alert "Setting up pre-run function for command" "${ARMBIAN_COMMAND}: ${pre_run_function_name}" "debug"
|
|
function armbian_cli_pre_run_command() {
|
|
# Set the variables defined in ARMBIAN_COMMAND_VARS
|
|
${set_vars_for_command}
|
|
display_alert "Calling pre-run function for command" "${ARMBIAN_COMMAND}: ${pre_run_function_name}" "debug"
|
|
${pre_run_function_name}
|
|
}
|
|
EOF
|
|
)"
|
|
fi
|
|
|
|
if [[ $(type -t "${run_function_name}" || true) == function ]]; then
|
|
eval "$(
|
|
cat <<- EOF
|
|
display_alert "Setting up run function for command" "${ARMBIAN_COMMAND}: ${run_function_name}" "debug"
|
|
function armbian_cli_run_command() {
|
|
# Set the variables defined in ARMBIAN_COMMAND_VARS
|
|
${set_vars_for_command}
|
|
display_alert "Calling run function for command" "${ARMBIAN_COMMAND}: ${run_function_name}" "debug"
|
|
${run_function_name}
|
|
}
|
|
EOF
|
|
)"
|
|
fi
|
|
}
|
|
|
|
function parse_each_cmdline_arg_as_command_param_or_config() {
|
|
local is_command="no" is_config="no" command_handler conf_path conf_sh_path config_file=""
|
|
local argument="${1}"
|
|
|
|
# lookup if it is a command.
|
|
if [[ -n "${ARMBIAN_COMMANDS_TO_HANDLERS_DICT[${argument}]}" ]]; then
|
|
is_command="yes"
|
|
command_handler="${ARMBIAN_COMMANDS_TO_HANDLERS_DICT[${argument}]}"
|
|
display_alert "Found command!" "${argument} is handled by '${command_handler}'" "debug"
|
|
fi
|
|
|
|
# see if we can find config file in userpatches. can be either config-${argument}.conf or config-${argument}.conf.sh
|
|
conf_path="${USERPATCHES_PATH}/config-${argument}.conf"
|
|
conf_sh_path="${USERPATCHES_PATH}/config-${argument}.conf.sh"
|
|
|
|
# early safety net: immediately bomb if we find both forms of config. it's too confusing. choose one.
|
|
if [[ -f ${conf_path} && -f ${conf_sh_path} ]]; then
|
|
exit_with_error "Found both config-${argument}.conf and config-${argument}.conf.sh in userpatches. Please remove one."
|
|
exit 1
|
|
elif [[ -f ${conf_sh_path} ]]; then
|
|
config_file="${conf_sh_path}"
|
|
is_config="yes"
|
|
elif [[ -f ${conf_path} ]]; then
|
|
config_file="${conf_path}"
|
|
is_config="yes"
|
|
fi
|
|
|
|
# Sanity check. If we have both a command and a config, bomb.
|
|
if [[ "${is_command}" == "yes" && "${is_config}" == "yes" ]]; then
|
|
exit_with_error "You cannot have a configuration file named '${config_file}'. '${argument}' is a command name and is reserved for internal Armbian usage. Sorry. Please rename your config file and pass its name it an argument, and I'll use it. PS: You don't need a config file for 'docker' anymore, Docker is all managed by Armbian now."
|
|
elif [[ "${is_config}" == "yes" ]]; then # we have a config only
|
|
display_alert "Adding config file to list" "${config_file}" "debug"
|
|
ARMBIAN_CONFIG_FILES+=("${config_file}") # full path to be sourced
|
|
ARMBIAN_CLI_RELAUNCH_CONFIGS+=("${argument}") # name reference to be relaunched
|
|
elif [[ "${is_command}" == "yes" ]]; then # we have a command, only.
|
|
# sanity check. we can't have more than one command. decide!
|
|
if [[ -n "${ARMBIAN_COMMAND}" ]]; then
|
|
exit_with_error "You cannot specify more than one command. You have '${ARMBIAN_COMMAND}' and '${argument}'. Please decide which one you want to run and pass only that one."
|
|
exit 1
|
|
fi
|
|
ARMBIAN_COMMAND="${argument}" # too early for armbian_prepare_cli_command_to_run "${argument}"
|
|
else
|
|
# We've an unknown argument. Alert now, bomb later.
|
|
ARMBIAN_HAS_UNKNOWN_ARG="yes"
|
|
display_alert "Unknown argument" "${argument}" "err"
|
|
fi
|
|
}
|
|
|
|
# Produce relaunch parameters. Add the running configs, arguments, and command.
|
|
# Declare and use ARMBIAN_CLI_FINAL_RELAUNCH_ARGS as "${ARMBIAN_CLI_FINAL_RELAUNCH_ARGS[@]}"
|
|
# Also ARMBIAN_CLI_FINAL_RELAUNCH_ENVS as "${ARMBIAN_CLI_FINAL_RELAUNCH_ENVS[@]}"
|
|
function produce_relaunch_parameters() {
|
|
declare -g -a ARMBIAN_CLI_FINAL_RELAUNCH_ARGS=()
|
|
declare -g -a ARMBIAN_CLI_FINAL_RELAUNCH_ENVS=()
|
|
|
|
declare hide_repeat_params=()
|
|
|
|
# add the running parameters from ARMBIAN_CLI_RELAUNCH_PARAMS dict
|
|
for param in "${!ARMBIAN_CLI_RELAUNCH_PARAMS[@]}"; do
|
|
ARMBIAN_CLI_FINAL_RELAUNCH_ARGS+=("${param}=${ARMBIAN_CLI_RELAUNCH_PARAMS[${param}]}")
|
|
# If the param is not a key of ARMBIAN_PARSED_CMDLINE_PARAMS (eg was added for re-launching), add it to the hide list
|
|
if [[ -z "${ARMBIAN_PARSED_CMDLINE_PARAMS[${param}]}" ]]; then
|
|
hide_repeat_params+=("${param}")
|
|
fi
|
|
done
|
|
# add the running configs
|
|
for config in "${ARMBIAN_CLI_RELAUNCH_CONFIGS[@]}"; do
|
|
ARMBIAN_CLI_FINAL_RELAUNCH_ARGS+=("${config}")
|
|
done
|
|
# add the command; defaults to the last command, but can be changed by the last pre-run.
|
|
if [[ -n "${ARMBIAN_CLI_RELAUNCH_COMMAND}" ]]; then
|
|
ARMBIAN_CLI_FINAL_RELAUNCH_ARGS+=("${ARMBIAN_CLI_RELAUNCH_COMMAND}")
|
|
else
|
|
ARMBIAN_CLI_FINAL_RELAUNCH_ARGS+=("${ARMBIAN_COMMAND}")
|
|
fi
|
|
|
|
# These two envs are always included.
|
|
ARMBIAN_CLI_FINAL_RELAUNCH_ENVS+=("ARMBIAN_ORIGINAL_BUILD_UUID=${ARMBIAN_BUILD_UUID}")
|
|
ARMBIAN_CLI_FINAL_RELAUNCH_ENVS+=("ARMBIAN_HIDE_REPEAT_PARAMS=${hide_repeat_params[*]}")
|
|
|
|
# Add all values from ARMBIAN_CLI_RELAUNCH_ENVS dict
|
|
for env in "${!ARMBIAN_CLI_RELAUNCH_ENVS[@]}"; do
|
|
ARMBIAN_CLI_FINAL_RELAUNCH_ENVS+=("${env}=${ARMBIAN_CLI_RELAUNCH_ENVS[${env}]}")
|
|
done
|
|
|
|
display_alert "Produced relaunch args:" "ARMBIAN_CLI_FINAL_RELAUNCH_ARGS: ${ARMBIAN_CLI_FINAL_RELAUNCH_ARGS[*]}" "debug"
|
|
display_alert "Produced relaunch envs:" "ARMBIAN_CLI_FINAL_RELAUNCH_ENVS: ${ARMBIAN_CLI_FINAL_RELAUNCH_ENVS[*]}" "debug"
|
|
}
|
|
|
|
function cli_standard_relaunch_docker_or_sudo() {
|
|
display_alert "Gonna relaunch" "EUID: ${EUID} -- PREFER_DOCKER:${PREFER_DOCKER}" "debug"
|
|
if [[ "${EUID}" == "0" ]]; then # we're already root. Either running as real root, or already sudo'ed.
|
|
if [[ "${ARMBIAN_RELAUNCHED}" != "yes" && "${ALLOW_ROOT}" != "yes" ]]; then
|
|
display_alert "PROBLEM: don't run ./compile.sh as root or with sudo" "PROBLEM: don't run ./compile.sh as root or with sudo" "err"
|
|
if [[ -t 0 ]]; then # so... non-interactive builds *can* run as root. It's not supported, you'll get an error, but we'll proceed.
|
|
exit_if_countdown_not_aborted 10 "directly called as root"
|
|
fi
|
|
fi
|
|
display_alert "Already running as root" "great, running '${ARMBIAN_COMMAND}' normally" "debug"
|
|
else # not root.
|
|
# add params when relaunched under docker or sudo
|
|
ARMBIAN_CLI_RELAUNCH_PARAMS+=(["SET_OWNER_TO_UID"]="${EUID}") # Pass the current UID to any further relaunchings
|
|
ARMBIAN_CLI_RELAUNCH_PARAMS+=(["PREFER_DOCKER"]="no") # make sure we don't loop forever when relaunching.
|
|
|
|
# We've a few options.
|
|
# 1) We could check if Docker is working, and do everything under Docker. Users who can use Docker, can "become" root inside a container.
|
|
# 2) We could ask for sudo (which _might_ require a password)...
|
|
# @TODO: GitHub actions can do both. Sudo without password _and_ Docker; should we prefer Docker? Might have unintended consequences...
|
|
|
|
get_docker_info_once # Get Docker info once, and cache it; calling "docker info" is expensive
|
|
|
|
if [[ "${DOCKER_INFO_OK}" == "yes" ]]; then
|
|
if [[ "${PREFER_DOCKER:-yes}" == "yes" ]]; then
|
|
display_alert "not root, but Docker is ready to go" "delegating to Docker" "debug"
|
|
ARMBIAN_CHANGE_COMMAND_TO="docker"
|
|
ARMBIAN_CLI_RELAUNCH_COMMAND="${ARMBIAN_COMMAND}" # add params when relaunched under docker
|
|
return 0
|
|
else
|
|
display_alert "not root, but Docker is ready to go" "but PREFER_DOCKER is set to 'no', so can't use it" "warn"
|
|
fi
|
|
else
|
|
if [[ "${DOCKER_IN_PATH:-no}" == "yes" ]]; then
|
|
if [[ "${PREFER_DOCKER:-yes}" == "no" ]]; then
|
|
: # congrats, don't have it, didn't wanna it.
|
|
else
|
|
display_alert "Docker is installed, but not usable" "can't use Docker; check your Docker config / groups / etc" "warn"
|
|
exit_if_countdown_not_aborted 10 "Docker installed but not usable"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# check if we're on Linux via uname. if not, refuse to do anything.
|
|
if [[ "$(uname)" != "Linux" ]]; then
|
|
display_alert "Not running on Linux; Docker is not available" "refusing to run" "err"
|
|
exit 1
|
|
fi
|
|
|
|
display_alert "This script requires root privileges; Docker is unavailable" "trying to use sudo" "wrn"
|
|
declare -g ARMBIAN_CLI_FINAL_RELAUNCH_ARGS=()
|
|
declare -g ARMBIAN_CLI_FINAL_RELAUNCH_ENVS=()
|
|
produce_relaunch_parameters # produces ARMBIAN_CLI_FINAL_RELAUNCH_ARGS and ARMBIAN_CLI_FINAL_RELAUNCH_ENVS
|
|
# shellcheck disable=SC2093 # re-launching under sudo: replace the current shell, and never return.
|
|
exec sudo --preserve-env "${ARMBIAN_CLI_FINAL_RELAUNCH_ENVS[@]}" bash "${SRC}/compile.sh" "${ARMBIAN_CLI_FINAL_RELAUNCH_ARGS[@]}" # MARK: relaunch done here!
|
|
display_alert "AFTER SUDO!!!" "AFTER SUDO!!!" "warn" # This should _never_ happen
|
|
fi
|
|
}
|