From 043262b5f71e3030ad5553597b0e9696f6ff56b2 Mon Sep 17 00:00:00 2001 From: David Runge Date: Sat, 11 Jul 2020 18:13:20 +0200 Subject: Adding linting for initcpio scripts archiso/initcpio/install/*: Setting bash shebang for all scripts and making them comform with shellcheck. archiso/initcpio/{hooks,script}/*: Setting ash shebang for all scripts and making them comform with shellcheck (for dash, as shellcheck has no ash specific ruleset). Essentially the ash based scripts should be POSIX compliant as much as possible to have an easier time writing, debugging and maintaining them. Ensuring that variables are not treated as options and introducing variable quoting. .gitlab-ci.yml: Integrating shellcheck for initcpio scripts. Closes #32 --- hooks/archiso | 110 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 60 insertions(+), 50 deletions(-) (limited to 'hooks/archiso') diff --git a/hooks/archiso b/hooks/archiso index 5c56561..44abb88 100644 --- a/hooks/archiso +++ b/hooks/archiso @@ -1,3 +1,5 @@ +#!/bin/ash + # args: source, newroot, mountpoint _mnt_dmsnapshot() { local img="${1}" @@ -8,19 +10,19 @@ _mnt_dmsnapshot() { local dm_snap_name="${dm_snap_prefix}_${img_name}" local ro_dev ro_dev_size rw_dev - ro_dev=$(losetup --find --show --read-only "${img}") - echo ${ro_dev} >> /run/archiso/used_block_devices - ro_dev_size=$(blockdev --getsz ${ro_dev}) + ro_dev="$(losetup --find --show --read-only -- "${img}")" + echo "${ro_dev}" >> /run/archiso/used_block_devices + ro_dev_size="$(blockdev --getsz -- "${ro_dev}")" - if [[ "${cow_persistent}" == "P" ]]; then - if [[ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]]; then + if [ "${cow_persistent}" = "P" ]; then + if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow', using as persistent." else msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as persistent." truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" fi else - if [[ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]]; then + if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' but non-persistent requested, removing." rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" fi @@ -28,17 +30,18 @@ _mnt_dmsnapshot() { truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" fi - rw_dev=$(losetup --find --show "/run/archiso/cowspace/${cow_directory}/${img_name}.cow") - echo ${rw_dev} >> /run/archiso/used_block_devices + rw_dev="$(losetup --find --show "/run/archiso/cowspace/${cow_directory}/${img_name}.cow")" + echo "${rw_dev}" >> /run/archiso/used_block_devices - dmsetup create ${dm_snap_name} --table "0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} ${cow_chunksize}" + dmsetup create "${dm_snap_name}" --table \ + "0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} ${cow_chunksize}" - if [[ "${cow_persistent}" != "P" ]]; then + if [ "${cow_persistent}" != "P" ]; then rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" fi _mnt_dev "/dev/mapper/${dm_snap_name}" "${newroot}${mnt}" "-w" "defaults" - echo $(readlink -f /dev/mapper/${dm_snap_name}) >> /run/archiso/used_block_devices + readlink -f "/dev/mapper/${dm_snap_name}" >> /run/archiso/used_block_devices } # args: source, newroot, mountpoint @@ -46,8 +49,10 @@ _mnt_overlayfs() { local src="${1}" local newroot="${2}" local mnt="${3}" - mkdir -p /run/archiso/cowspace/${cow_directory}/upperdir /run/archiso/cowspace/${cow_directory}/workdir - mount -t overlay -o lowerdir=${src},upperdir=/run/archiso/cowspace/${cow_directory}/upperdir,workdir=/run/archiso/cowspace/${cow_directory}/workdir airootfs "${newroot}${mnt}" + mkdir -p "/run/archiso/cowspace/${cow_directory}/upperdir" "/run/archiso/cowspace/${cow_directory}/workdir" + mount -t overlay -o \ + "lowerdir=${src},upperdir=/run/archiso/cowspace/${cow_directory}/upperdir,workdir=/run/archiso/cowspace/${cow_directory}/workdir" \ + airootfs "${newroot}${mnt}" } @@ -58,17 +63,18 @@ _mnt_sfs() { local img_fullname="${img##*/}" local sfs_dev - if [[ "${copytoram}" == "y" ]]; then + # shellcheck disable=SC2154 # defined via initcpio's parse_cmdline() + if [ "${copytoram}" = "y" ]; then msg -n ":: Copying squashfs image to RAM..." - if ! cp "${img}" "/run/archiso/copytoram/${img_fullname}" ; then + if ! cp -- "${img}" "/run/archiso/copytoram/${img_fullname}" ; then echo "ERROR: while copy '${img}' to '/run/archiso/copytoram/${img_fullname}'" launch_interactive_shell fi img="/run/archiso/copytoram/${img_fullname}" msg "done." fi - sfs_dev=$(losetup --find --show --read-only "${img}") - echo ${sfs_dev} >> /run/archiso/used_block_devices + sfs_dev="$(losetup --find --show --read-only -- "${img}")" + echo "${sfs_dev}" >> /run/archiso/used_block_devices _mnt_dev "${sfs_dev}" "${mnt}" "-r" "defaults" } @@ -102,45 +108,46 @@ _mnt_dev() { _verify_checksum() { local _status - cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" + cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1 sha512sum -c airootfs.sha512 > /tmp/checksum.log 2>&1 _status=$? - cd "${OLDPWD}" - return ${_status} + cd -- "${OLDPWD}" || exit 1 + return "${_status}" } _verify_signature() { local _status - cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" + cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1 gpg --homedir /gpg --status-fd 1 --verify airootfs.sfs.sig 2>/dev/null | grep -qE '^\[GNUPG:\] GOODSIG' _status=$? - cd "${OLDPWD}" + cd -- "${OLDPWD}" || exit 1 return ${_status} } run_hook() { - [[ -z "${arch}" ]] && arch="$(uname -m)" - [[ -z "${copytoram_size}" ]] && copytoram_size="75%" - [[ -z "${archisobasedir}" ]] && archisobasedir="arch" - [[ -z "${dm_snap_prefix}" ]] && dm_snap_prefix="arch" - [[ -z "${archisodevice}" ]] && archisodevice="/dev/disk/by-label/${archisolabel}" - [[ -z "${cow_spacesize}" ]] && cow_spacesize="256M" - - if [[ -n "${cow_label}" ]]; then + [ -z "${arch}" ] && arch="$(uname -m)" + [ -z "${copytoram_size}" ] && copytoram_size="75%" + [ -z "${archisobasedir}" ] && archisobasedir="arch" + [ -z "${dm_snap_prefix}" ] && dm_snap_prefix="arch" + # shellcheck disable=SC2154 # defined via initcpio's parse_cmdline() + [ -z "${archisodevice}" ] && archisodevice="/dev/disk/by-label/${archisolabel}" + [ -z "${cow_spacesize}" ] && cow_spacesize="256M" + # shellcheck disable=SC2154 # defined via initcpio's parse_cmdline() + if [ -n "${cow_label}" ]; then cow_device="/dev/disk/by-label/${cow_label}" - [[ -z "${cow_persistent}" ]] && cow_persistent="P" - elif [[ -n "${cow_device}" ]]; then - [[ -z "${cow_persistent}" ]] && cow_persistent="P" + [ -z "${cow_persistent}" ] && cow_persistent="P" + elif [ -n "${cow_device}" ]; then + [ -z "${cow_persistent}" ] && cow_persistent="P" else cow_persistent="N" fi - [[ -z "${cow_flags}" ]] && cow_flags="defaults" - [[ -z "${cow_directory}" ]] && cow_directory="persistent_${archisolabel}/${arch}" - [[ -z "${cow_chunksize}" ]] && cow_chunksize="8" + [ -z "${cow_flags}" ] && cow_flags="defaults" + [ -z "${cow_directory}" ] && cow_directory="persistent_${archisolabel}/${arch}" + [ -z "${cow_chunksize}" ] && cow_chunksize="8" # set mount handler for archiso - mount_handler="archiso_mount_handler" + export mount_handler="archiso_mount_handler" } # This function is called normally from init script, but it can be called @@ -151,13 +158,14 @@ archiso_mount_handler() { if ! mountpoint -q "/run/archiso/bootmnt"; then _mnt_dev "${archisodevice}" "/run/archiso/bootmnt" "-r" "defaults" - if [[ "${copytoram}" != "y" ]]; then - echo $(readlink -f ${archisodevice}) >> /run/archiso/used_block_devices + if [ "${copytoram}" != "y" ]; then + readlink -f "${archisodevice}" >> /run/archiso/used_block_devices fi fi - if [[ "${checksum}" == "y" ]]; then - if [[ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sha512" ]]; then + # shellcheck disable=SC2154 # defined via initcpio's parse_cmdline() + if [ "${checksum}" = "y" ]; then + if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sha512" ]; then msg -n ":: Self-test requested, please wait..." if _verify_checksum; then msg "done. Checksum is OK, continue booting." @@ -172,8 +180,9 @@ archiso_mount_handler() { fi fi - if [[ "${verify}" == "y" ]]; then - if [[ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs.sig" ]]; then + # shellcheck disable=SC2154 # defined via initcpio's parse_cmdline() + if [ "${verify}" = "y" ]; then + if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs.sig" ]; then msg -n ":: Signature verification requested, please wait..." if _verify_signature; then msg "done. Signature is OK, continue booting." @@ -187,33 +196,34 @@ archiso_mount_handler() { fi fi - if [[ "${copytoram}" == "y" ]]; then + if [ "${copytoram}" = "y" ]; then msg ":: Mounting /run/archiso/copytoram (tmpfs) filesystem, size=${copytoram_size}" mkdir -p /run/archiso/copytoram mount -t tmpfs -o "size=${copytoram_size}",mode=0755 copytoram /run/archiso/copytoram fi - if [[ -n "${cow_device}" ]]; then + if [ -n "${cow_device}" ]; then _mnt_dev "${cow_device}" "/run/archiso/cowspace" "-r" "${cow_flags}" - echo $(readlink -f ${cow_device}) >> /run/archiso/used_block_devices + readlink -f "${cow_device}" >> /run/archiso/used_block_devices mount -o remount,rw "/run/archiso/cowspace" else msg ":: Mounting /run/archiso/cowspace (tmpfs) filesystem, size=${cow_spacesize}..." mkdir -p /run/archiso/cowspace mount -t tmpfs -o "size=${cow_spacesize}",mode=0755 cowspace /run/archiso/cowspace fi - mkdir -p -m 0700 "/run/archiso/cowspace/${cow_directory}" + mkdir -p "/run/archiso/cowspace/${cow_directory}" + chmod 0700 "/run/archiso/cowspace/${cow_directory}" _mnt_sfs "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs" "/run/archiso/sfs/airootfs" - if [[ -f "/run/archiso/sfs/airootfs/airootfs.img" ]]; then + if [ -f "/run/archiso/sfs/airootfs/airootfs.img" ]; then _mnt_dmsnapshot "/run/archiso/sfs/airootfs/airootfs.img" "${newroot}" "/" else _mnt_overlayfs "/run/archiso/sfs/airootfs" "${newroot}" "/" fi - if [[ "${copytoram}" == "y" ]]; then + if [ "${copytoram}" = "y" ]; then umount -d /run/archiso/bootmnt fi } -# vim:ft=sh:ts=4:sw=4:et: +# vim: set ft=sh: -- cgit v1.2.3-70-g09d2