From 32662f463246710ed64ddbfdf9136c299fb9c059 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 19 Aug 2025 04:27:53 +0200 Subject: [PATCH] Fixes. --- .../{apt_get_digested.sh => apt_digested.sh} | 10 +++---- trixie/scripts/lib/expect_n_args.sh | 22 +++++++++++++- .../scripts/lib/get_mountable_device_path.sh | 17 +++++++---- trixie/scripts/lib/path_tmp_timestamped.sh | 2 +- trixie/scripts/lib/trapp.sh | 8 ++++- trixie/scripts/make_writable_installer.sh | 30 +++++++++++-------- 6 files changed, 63 insertions(+), 26 deletions(-) rename trixie/scripts/lib/{apt_get_digested.sh => apt_digested.sh} (96%) mode change 120000 => 100644 trixie/scripts/lib/expect_n_args.sh mode change 120000 => 100644 trixie/scripts/lib/trapp.sh diff --git a/trixie/scripts/lib/apt_get_digested.sh b/trixie/scripts/lib/apt_digested.sh similarity index 96% rename from trixie/scripts/lib/apt_get_digested.sh rename to trixie/scripts/lib/apt_digested.sh index df4baa7..a6abe64 100644 --- a/trixie/scripts/lib/apt_get_digested.sh +++ b/trixie/scripts/lib/apt_digested.sh @@ -2,7 +2,7 @@ # NB: to avoid confusing the IFS handling, ensure none of the lines is empty – # even if ignored! -apt_get_digested_SEDS=$(cat << "DELIMITER" +apt_digested_SEDS=$(cat << "DELIMITER" Get: Get:[[:digit:]]+ (.*) \1 @@ -115,10 +115,10 @@ dpkg warns: removing \1 despite dependency problems: DELIMITER ) -apt_get_digested() { - prefixed_msg_init 'apt_get_digested' +apt_digested() { + prefixed_msg_init 'apt_digested' local APT_GET_EXIT_CODE - local FINISH_LINE='apt_get_digested_FINISH_LINE' + local FINISH_LINE='apt_digested_FINISH_LINE' run_apt() { set +e @@ -158,7 +158,7 @@ apt_get_digested() { local OLD_IFS=${IFS} local IFS IFS=$(printf '\n\r') - for LINE in ${apt_get_digested_SEDS}; do + for LINE in ${apt_digested_SEDS}; do I=$((1 + "$I")) if [ "$I" -eq 1 ]; then PREFIX="${LINE}" diff --git a/trixie/scripts/lib/expect_n_args.sh b/trixie/scripts/lib/expect_n_args.sh deleted file mode 120000 index 7430ba5..0000000 --- a/trixie/scripts/lib/expect_n_args.sh +++ /dev/null @@ -1 +0,0 @@ -../../../bookworm/scripts/lib/expect_n_args.sh \ No newline at end of file diff --git a/trixie/scripts/lib/expect_n_args.sh b/trixie/scripts/lib/expect_n_args.sh new file mode 100644 index 0000000..11a26ea --- /dev/null +++ b/trixie/scripts/lib/expect_n_args.sh @@ -0,0 +1,21 @@ +. lib/abort.sh + +expect_n_args() { + _N_MIN_ARGS="$1" + _N_MAX_ARGS="$2" + _USAGE="$3" + shift 3 + _ABORT_MSG= + if [ "$#" -lt "${_N_MIN_ARGS}" ]; then + _ABORT_MSG="missing arguments (got $# instead of ${_N_MIN_ARGS})." + elif [ "$#" -gt "${_N_MAX_ARGS}" ]; then + shift "${_N_MAX_ARGS}" + _ABORT_MSG="unexpected arguments beyond expected number (${_N_MAX_ARGS}): $*." + fi + if [ -n "${_ABORT_MSG}" ]; then + if [ -n "${_USAGE}" ]; then + _ABORT_MSG="${_ABORT_MSG} Expected arguments: ${_USAGE}" + fi + abort "Aborting due to ${_ABORT_MSG}" + fi +} diff --git a/trixie/scripts/lib/get_mountable_device_path.sh b/trixie/scripts/lib/get_mountable_device_path.sh index 8af2a89..8405944 100644 --- a/trixie/scripts/lib/get_mountable_device_path.sh +++ b/trixie/scripts/lib/get_mountable_device_path.sh @@ -1,11 +1,16 @@ . lib/abort.sh get_mountable_device_path() { - _PATH_DEV="/dev/$1" - if [ ! -b "${_PATH_DEV}" ]; then - abort "No block device at ${_PATH_DEV}." - elif [ $(mount | grep -E "^${_PATH_DEV}" | wc -l) -gt 0 ]; then - abort "${_PATH_DEV} already mounted." + local PATH_DEV + PATH_DEV="/dev/$1" + if [ ! -b "${PATH_DEV}" ]; then + abort "No block device at ${PATH_DEV}." + else + local COUNT + COUNT=$(mount | grep -cE "^${PATH_DEV}") + if [ "${COUNT}" -gt 0 ]; then + abort "${PATH_DEV} already mounted." + fi fi - printf "${_PATH_DEV}" + printf '%s' "${PATH_DEV}" } diff --git a/trixie/scripts/lib/path_tmp_timestamped.sh b/trixie/scripts/lib/path_tmp_timestamped.sh index 7ae63cd..4d1488a 100644 --- a/trixie/scripts/lib/path_tmp_timestamped.sh +++ b/trixie/scripts/lib/path_tmp_timestamped.sh @@ -1,3 +1,3 @@ path_tmp_timestamped () { - printf "/tmp/${1}_$(date +'%s')" + printf '/tmp/%s_%s' "$1" "$(date +'%s')" } diff --git a/trixie/scripts/lib/trapp.sh b/trixie/scripts/lib/trapp.sh deleted file mode 120000 index 23d49a5..0000000 --- a/trixie/scripts/lib/trapp.sh +++ /dev/null @@ -1 +0,0 @@ -../../../bookworm/scripts/lib/trapp.sh \ No newline at end of file diff --git a/trixie/scripts/lib/trapp.sh b/trixie/scripts/lib/trapp.sh new file mode 100644 index 0000000..c36568f --- /dev/null +++ b/trixie/scripts/lib/trapp.sh @@ -0,0 +1,7 @@ +trapp() { + local COMMAND="$*" + if [ -z "${COMMAND}" ]; then + COMMAND='-' + fi + trap "${COMMAND}" EXIT TERM HUP INT +} diff --git a/trixie/scripts/make_writable_installer.sh b/trixie/scripts/make_writable_installer.sh index dc8dedd..6505bdf 100755 --- a/trixie/scripts/make_writable_installer.sh +++ b/trixie/scripts/make_writable_installer.sh @@ -1,7 +1,8 @@ -#!/bin/sh +#!/usr/bin/env dash # based on set -eu -cd $(dirname "$0") +PARENT_DIR=$(dirname "$0") +cd "${PARENT_DIR}" . lib/INSTALLER_VERSION.sh . lib/abort.sh . lib/abort_if_command_unknown.sh @@ -13,40 +14,45 @@ cd $(dirname "$0") . lib/path_tmp_timestamped.sh . lib/trapp.sh -expect_n_args 1 2 'DEVICE (e.g. "sdb") [PATH_FILE_ISO]' $@ +FILENAME_ISO="debian-${INSTALLER_VERSION}-amd64-netinst.iso" +URL_ISO="https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/${FILENAME_ISO}" +PATH_MNT_ISO=/mnt/iso + +expect_n_args 1 2 'DEVICE (e.g. "sdb") [PATH_FILE_ISO]' "$@" abort_if_not_user root abort_if_command_unknown mkfs.vfat abort_if_command_unknown parted abort_if_command_unknown rsync abort_if_command_unknown wget -FILENAME_ISO="debian-${INSTALLER_VERSION}-amd64-netinst.iso" + if [ -z "$2" ]; then + echo "No file path set, so will try to retrieve image online from ${URL_ISO}." abort_if_offline else + echo "With file path set, checking if we can use file at ${PATH_FILE_ISO}." PATH_FILE_ISO=$(realpath "$2") - if [ ! "${FILENAME_ISO}" = $(basename "${PATH_FILE_ISO}") ]; then + BASENAME_FILE_ISO=$(basename "${PATH_FILE_ISO}") + if [ ! "${FILENAME_ISO}" = "${BASENAME_FILE_ISO}" ]; then abort "basename of PATH_FILE_ISO != expected ${FILENAME_ISO}" elif [ ! -f "${PATH_FILE_ISO}" ]; then abort 'no file found at PATH_FILE_ISO' fi fi -PATH_DEV="$(get_mountable_device_path $1)" -PATH_MNT_ISO=/mnt/iso +PATH_DEV=$(get_mountable_device_path "$1") PATH_PROCESSING="$(path_tmp_timestamped make_writable_installer)" -RM_PROCESSING="rm -rf ${PATH_PROCESSING}" echo "Setting up processing directory at ${PATH_PROCESSING} …" +RM_PROCESSING="rm -rf ${PATH_PROCESSING}" mkdir "${PATH_PROCESSING}" trapp "${RM_PROCESSING}" cd "${PATH_PROCESSING}" if [ -z "${PATH_FILE_ISO}" ]; then echo "Retrieving ${FILENAME_ISO} …" - URL_ISO="https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/${FILENAME_ISO}" wget --quiet --output-document "${FILENAME_ISO}" "${URL_ISO}" else echo "Linking to ${PATH_FILE_ISO} …" - ln -s "${PATH_FILE_ISO}" + ln -s "${PATH_FILE_ISO}" . fi echo "Preparing partition/filesystem on ${PATH_DEV} …" @@ -79,9 +85,9 @@ if [ "${RESULT}" != "0" ]; then echo 'RSYNC ERRORS:' cat "${FILENAME_RSYNC_ERRORS}" echo '\nrsync encountered errors, see above – continue? (Y/N)' - read ANSWER + read -r ANSWER FIRST_CHAR=$(echo "${ANSWER}" | cut -c1) - if ! [ "${FIRST_CHAR}" = 'y' -o "${FIRST_CHAR}" = 'Y' ]; then + if ! [ "${FIRST_CHAR}" = 'y' ] || [ "${FIRST_CHAR}" = 'Y' ]; then abort 'as requested' fi fi -- 2.30.2