From f5178b08ef41f13069af364ddfbadb57532471e3 Mon Sep 17 00:00:00 2001 From: Tom Whitwell Date: Fri, 17 Feb 2023 11:22:16 +0000 Subject: [PATCH 1/3] Improve portability of make_custom_pi_os - Use `which` to check for `gsed` and use it if available: macos ships with BSD sed which doesn't support `-i` for in-place editing - get the primary group of the current user with `id -gn` instead of hardcoding it to `${USER}` - macos doesn't create a group with the same name as the user by default - Add quotes around a bunch of variables to prevent globbing / word splitting - Use `set -euo pipefail` to make the script fail on errors and undefined variables, rather than silently continuing - Use '-s' flag with curl to prevent it from printing the download progress bar when retrieving version info - Check we can retrieve the filename of the latest Raspbian image before downloading it - Print out the URL of the image we're downloading --- src/dist_generators/dist_example_script | 35 ++++++++++++++--------- src/make_custom_pi_os | 38 ++++++++++++++----------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/dist_generators/dist_example_script b/src/dist_generators/dist_example_script index e2582198..cd48f6b2 100755 --- a/src/dist_generators/dist_example_script +++ b/src/dist_generators/dist_example_script @@ -1,18 +1,25 @@ #!/usr/bin/env bash +set -euo pipefail DEST_FOLDER="$1" -DIST_NAME=$(basename $DEST_FOLDER) +DIST_NAME=$(basename "${DEST_FOLDER}") -pushd "${DEST_FOLDER}" > /dev/null - pushd src/modules > /dev/null - mv example ${DIST_NAME,,} - pushd ${DIST_NAME,,} > /dev/null - DIST_NAME_UPPER=$(echo $DIST_NAME | awk '{print toupper($0)}') - sed -i "s/EXAMPLE_VAR/${DIST_NAME_UPPER}_VAR/g" config start_chroot_script - popd > /dev/null - popd > /dev/null - pushd src > /dev/null - sed -i "s/export DIST_NAME=.*/export DIST_NAME=${DIST_NAME}/g" config - sed -i "s/example/${DIST_NAME,,}/g" config - popd > /dev/null -popd > /dev/null +if which gsed >/dev/null; then # use gnu sed if available (macos) + SED="gsed" +else + SED="sed" +fi + +pushd "${DEST_FOLDER}" >/dev/null || exit 1 + pushd src/modules >/dev/null || exit 1 + mv example "${DIST_NAME,,}" + pushd "${DIST_NAME,,}" >/dev/null || exit 1 + DIST_NAME_UPPER=$(echo "${DIST_NAME}" | awk '{print toupper($0)}') + ${SED} -i "s/EXAMPLE_VAR/${DIST_NAME_UPPER}_VAR/g" config start_chroot_script + popd >/dev/null || exit 1 + popd >/dev/null || exit 1 + pushd src >/dev/null || exit 1 + ${SED} -i "s/export DIST_NAME=.*/export DIST_NAME=${DIST_NAME}/g" config + ${SED} -i "s/example/${DIST_NAME,,}/g" config + popd >/dev/null || exit 1 +popd >/dev/null || exit 1 diff --git a/src/make_custom_pi_os b/src/make_custom_pi_os index 7719c626..ac87498b 100755 --- a/src/make_custom_pi_os +++ b/src/make_custom_pi_os @@ -1,5 +1,6 @@ #!/usr/bin/env bash -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +set -euo pipefail +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$DIR/argparse.bash" || exit 1 argparse "$@" <" '{print $1}')" if [ $? -ne 0 ]; then - echo "error getting date" + echo -e "\nerror getting file name" exit 1 fi - CURRENT_RASPBIAN_FILE=$(curl http://downloads.raspberrypi.org/raspios_lite_armhf/images/${CURRENT_RASPBIAN}/ | grep .xz | head -n 1 | awk -F "href=\"" '{print $2}' | awk -F "\">" '{print $1}') - curl -L -o "${DEST}/src/image/${CURRENT_RASPBIAN_FILE}" https://downloads.raspberrypi.org/raspios_lite_armhf/images/${CURRENT_RASPBIAN}/${CURRENT_RASPBIAN_FILE} + CURRENT_RASPBIAN_URL="https://downloads.raspberrypi.org/raspios_lite_armhf/images/${CURRENT_RASPBIAN}/${CURRENT_RASPBIAN_FILE}" + echo " from ${CURRENT_RASPBIAN_URL}" + curl -L -o "${DEST}/src/image/${CURRENT_RASPBIAN_FILE}" "${CURRENT_RASPBIAN_URL}" fi - From 7405018a7d791aaf5cd7c87c98abfe3b9e3db975 Mon Sep 17 00:00:00 2001 From: Tom Whitwell Date: Fri, 17 Feb 2023 11:38:08 +0000 Subject: [PATCH 2/3] Allow specifying variant in make_custom_pi_os --- src/make_custom_pi_os | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/make_custom_pi_os b/src/make_custom_pi_os index ac87498b..daf45b11 100755 --- a/src/make_custom_pi_os +++ b/src/make_custom_pi_os @@ -8,10 +8,15 @@ argparse "$@" <" '{print $1}')" + CURRENT_RASPBIAN_FILE="$(curl -s "http://downloads.raspberrypi.org/${VARIANT}/images/${CURRENT_RASPBIAN}"/ | grep .xz | head -n 1 | awk -F "href=\"" '{print $2}' | awk -F "\">" '{print $1}')" if [ $? -ne 0 ]; then echo -e "\nerror getting file name" exit 1 fi - CURRENT_RASPBIAN_URL="https://downloads.raspberrypi.org/raspios_lite_armhf/images/${CURRENT_RASPBIAN}/${CURRENT_RASPBIAN_FILE}" + CURRENT_RASPBIAN_URL="https://downloads.raspberrypi.org/${VARIANT}/images/${CURRENT_RASPBIAN}/${CURRENT_RASPBIAN_FILE}" echo " from ${CURRENT_RASPBIAN_URL}" curl -L -o "${DEST}/src/image/${CURRENT_RASPBIAN_FILE}" "${CURRENT_RASPBIAN_URL}" fi From cc05702a98d1d682240809afe4c43d38bc39dcf5 Mon Sep 17 00:00:00 2001 From: Tom Whitwell Date: Fri, 17 Feb 2023 11:47:43 +0000 Subject: [PATCH 3/3] Guard against space / hyphen in $DEST --- src/make_custom_pi_os | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/make_custom_pi_os b/src/make_custom_pi_os index daf45b11..fbb67798 100755 --- a/src/make_custom_pi_os +++ b/src/make_custom_pi_os @@ -14,6 +14,19 @@ parser.add_argument('-v', '--variant', action='store', help='Which variant to use [default: %(default)s]') EOF +case $DEST in + *"-"*) + echo "Error, destination folder cannot contain a dash" + exit 1 + ;; + *" "*) + echo "Error, destination folder cannot contain a space" + exit 1 + ;; + *) + ;; +esac + echo Settings: echo "making dstro in ${DEST}" echo "variant: ${VARIANT}"