From 0d4af3928119b4b749f14e0f3308962e7e553b6a Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Sun, 2 Feb 2020 15:59:30 +0100 Subject: [PATCH] arch-create-system: posix cleanup Signed-off-by: Ralf Zerres --- usr/bin/arch-create-system | 111 +++++++++++++------------------------ 1 file changed, 39 insertions(+), 72 deletions(-) diff --git a/usr/bin/arch-create-system b/usr/bin/arch-create-system index ce6a7c1..913af8a 100755 --- a/usr/bin/arch-create-system +++ b/usr/bin/arch-create-system @@ -20,6 +20,7 @@ # ------------------------------------------------------------------------- progname="${0##*/}" +prog="$0" version="0.1.0" # global variables @@ -132,7 +133,7 @@ create_btrfs () { --data single \ --metadata single \ /dev/disk/by-partlabel/$LABEL" - cmd=`echo $cmd | tr -s "[:blank:]"` + cmd=`echo $cmd | tr -s \"[:blank:]\"` if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run: ${GREEN}'%s'${NO_COLOR}\n" \ "$cmd" @@ -243,7 +244,7 @@ create_fs_structure () { printf "${MAGENTA}Would mount target root at ${GREEN}'%s'${NO_COLOR}\n" \ "$SUBVOL" else - printf "${BLUE} - mount target root${NO_COLOR\n" + printf "${BLUE} - mount target root${NO_COLOR}\n" mount_target /dev/disk/by-partlabel/${PREFIX}-${LABEL} $MOUNT_POINT btrfs $SUBVOL if [ $? -gt 0 ]; then die "Can't mount root filesystem ${PREFIX}-${LABEL}" @@ -305,6 +306,8 @@ create_swapfs () { local PREFIX=$1 local MOUNT_POINT=$2 + local RamSize + local SwapSize local cmd printf "${BLUE}create_swapfs${NO_COLOR}\n" @@ -327,7 +330,8 @@ create_swapfs () { fi # prepare the swapfile (size: 2x RAM) - RamSize=`cat /proc/meminfo | gawk 'NR==1 {print $2}'` + cmd='gawk '/^MemTotal:/{print $2}' /proc/meminfo' + RamSize=$(eval $cmd) SwapSize=$(expr 2 '*' $RamSize) SwapFile=$MOUNT_POINT/swap/swapfile @@ -368,7 +372,7 @@ create_fat () { -F 32 \ -n $LABEL \ /dev/disk/by-partlabel/$LABEL" - cmd=`echo $cmd | tr -s "[:blank:]"` + cmd="echo $cmd | tr -s \"[:blank:]\"" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else @@ -395,7 +399,7 @@ create_vfat () { cmd="mkfs -t vfat \ -n $PREFIX-$LABEL \ /dev/disk/by-partlabel/$LABEL" - cmd=`echo $cmd | tr -s "[:blank:]"` + cmd="echo $cmd | tr -s \"[:blank:]\"" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else @@ -407,7 +411,7 @@ create_vfat () { die () { error "$@" if [ $dryrun -eq 1 ]; then - printf "${RED}Error${GREEN}'%s'${NO_COLOR}\n" "$@" + printf "${RED}Error: ${GREEN}'%s'${NO_COLOR}\n" "$@" fi exit 1 } @@ -416,74 +420,35 @@ error () { printf "\n==> ERROR: %s\n" "$@" } >&2 -get_config () { - local config_json=${configfile:-$config_json} - local config=${1} - - local cmd - - if [ $verbose -ge 1 ]; then - printf "${BLUE}get_config ...${NO_COLOR}\n" - fi - - if test ! -r ${config_json}; then - if [ $verbose -ge 1 ]; then - printf "${RED}Error:${MAGENTA} config file ${GREEN}'%s'${MAGENTA} can't be opend!${NO_COLOR}\n" \ - "config_json" - fi - exit 1 - fi - - cmd="jq -r --monochrome-output --ascii-output ' .ConfigEnvironment[] \ - | select(.ConfigName == \"${name}\") \ - | .ConfigName ' \ - ${config_json}" - #cmd="jq --monochrome-output --ascii-output ' .ConfigEnvironment[] \ - # | {Id: .ConfigId, Name: .ConfigName} ' \ - # ${config_json}" - - config_name=$(eval $cmd) - #config_name=$(echo $config_name | sed -e 's/\n//g') - - if [ $verbose -ge 2 ]; then - printf "${MAGENTA}Config environment id: ${GREEN}'%s'${NO_COLOR}\n" \ - "$config_name" - fi -} - get_config_name () { local config_json=${configfile:-$config_json} - local name=${1} + local config_file=${1} + local cmd if [ $verbose -ge 1 ]; then printf "${BLUE}get_config_name ...${NO_COLOR}\n" fi - if test ! -r ${config_json}; then + if [ ! -r ${config_json} ] ; then if [ $verbose -ge 1 ]; then - printf "${RED}Error:${MAGENTA} config file ${GREEN}'%s'${MAGENTA} can't be opend!${NO_COLOR}\n" \ + printf "${RED}Error:${MAGENTA} config file ${GREEN}'%s'${MAGENTA} can\'t be opend!${NO_COLOR}\n" \ "config_json" fi return 1 fi cmd="jq -r --monochrome-output --ascii-output ' .ConfigEnvironment[] \ - | select(.ConfigName == \"${name}\") \ + | select( .ConfigName == \"${config_file}\" ) \ | .ConfigName ' \ ${config_json}" - config_name=$(eval $cmd) - config_name=$(echo $config_name | sed -e 's/\n//g') - if [ ${#config_name} = 0 ]; then - if [ $verbose -ge 2 ]; then - printf "${MAGENTA}Config environment name: ${GREEN}'%s'${MAGENTA} not found!${NO_COLOR}\n" \ - "$name" - fi - return 1 - fi + + config_name=$(eval $cmd) + #config_name=$(echo $config_name | sed -e 's/\n//g') + if [ $verbose -ge 2 ]; then - printf "${MAGENTA}Config environment name: ${GREEN}'%s'${MAGENTA} exists.${NO_COLOR}\n" \ + printf "${MAGENTA}Config environment id: ${GREEN}'%s'${NO_COLOR}\n" \ "$config_name" fi } @@ -636,8 +601,7 @@ install_target_packages () { eval "$cmd" fi - install_script=`command -v $progname` - cmd="cp $install_script $MOUNT_POINT/usr/bin/" + cmd="cp $prog $MOUNT_POINT/usr/bin/" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would run ${GREEN}'%s'${NO_COLOR}\n" "$cmd" else @@ -716,7 +680,7 @@ mount_target () { ;; esac - cmd=`echo $cmd | tr -s "[:blank:]"` + cmd="echo $cmd | tr -s \"[:blank:]\"" if [ $dryrun -eq 1 ]; then printf "${MAGENTA}Would mount ${GREEN}'%s'${MAGENTA} of ${GREEN}'%s'${MAGENTA} at ${GREEN}'%s'${NO_COLOR}\n" \ "$FS_SUBVOL" "$DEV_NAME" "$MOUNT_POINT" @@ -924,7 +888,7 @@ parse_params () { break ;; -*) - printf "WARN: Unknown option (ignored): $1" >&2 + printf "WARN: Unknown option \(ignored\): $1" >&2 die "Unknown option" ;; *) @@ -964,7 +928,7 @@ prepare_bootloader () { > /boot/loader/loader.conf printf '%s\n' \ - "title Arch Linux (Mainline)" \ + "title Arch Linux \(Mainline\)" \ "linux /vmlinuz-linux" \ "initrd /intel-ucode.img" \ "initrd /initramfs-linux.img" \ @@ -992,8 +956,8 @@ prepare_locale () { prepare_mount_units () { printf '%s\n' \ "[Unit]" \ - "Description=root-Snapshots (btrfs)" \ - "Documentation=man:systemd.mount(5)" \ + "Description=root-Snapshots \(btrfs\)" \ + "Documentation=man:systemd.mount\(5\)" \ "DefaultDependencies=yes" \ "Before=local-fs.target" \ "" \ @@ -1010,8 +974,8 @@ prepare_mount_units () { printf '%s\n' \ "[Unit]" \ - "Description=Caching (btrfs)" \ - "Documentation=man:systemd.mount(5)" \ + "Description=Caching \(btrfs\)" \ + "Documentation=man:systemd.mount\(5\)" \ "DefaultDependencies=yes" \ "Before=local-fs.target" \ "" \ @@ -1028,8 +992,8 @@ prepare_mount_units () { printf '%s\n' \ "[Unit]" \ - "Description=data (btrfs)" \ - "Documentation=man:systemd.mount(5)" \ + "Description=data \(btrfs\)" \ + "Documentation=man:systemd.mount\(5\)" \ "DefaultDependencies=yes" \ "Before=local-fs.target" \ "" \ @@ -1046,8 +1010,8 @@ prepare_mount_units () { printf '%s\n' \ "[Unit]" \ - "Description=Home (btrfs)" \ - "Documentation=man:systemd.mount(5)" \ + "Description=Home \(btrfs\)" \ + "Documentation=man:systemd.mount\(5\)" \ "DefaultDependencies=yes" \ "Before=local-fs.target" \ "" \ @@ -1064,8 +1028,8 @@ prepare_mount_units () { printf '%s\n' \ "[Unit]" \ - "Description=Logging (btrfs)" \ - "Documentation=man:systemd.mount(5)" \ + "Description=Logging \(btrfs\)" \ + "Documentation=man:systemd.mount\(5\)" \ "DefaultDependencies=yes" \ "Before=local-fs.target" \ "" \ @@ -1153,6 +1117,8 @@ prepare_target_filesystems () { } show_config () { + local options + if [ $verbose -ge 1 ]; then printf "${BLUE}$progname (runtime arguments) ...${NO_COLOR}\n" i=0 @@ -1173,7 +1139,7 @@ show_config () { fi options="verbose_level=$verbose" - if [ $dryrun -ge 1 ]; then options="${options}dryrun=true"; fi + if [ $dryrun -ge 1 ]; then options="${options} dryrun=true"; fi if [ $color -ge 1 ]; then options="${options} color=true"; fi printf "Options: '%s'\n\n" "${options}" @@ -1205,7 +1171,8 @@ usage () { ### # Main ### -# can't be ported to dash (ERR is not supported) + +# can't be ported to dash \(ERR is not supported\) #trap 'traperror ${LINENO} $? "$BASH_COMMAND" $BASH_LINENO "${FUNCNAME[@]}"' ERR trap trapkill TERM INT