Skip to content

Commit fdcc142

Browse files
committed
[update : ]Adding linting for initcpio scripts
archlinux/archiso@e2032db
1 parent 0c44d97 commit fdcc142

15 files changed

Lines changed: 189 additions & 159 deletions

system/initcpio/archiso_shutdown

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ mkdir /oldrun
55
mount -n --move /oldroot/run /oldrun
66

77
# Unmount all mounts now.
8-
umount $(mount | awk '$3 ~/^\/oldroot/ {print $3}' | sort -r)
8+
umount "$(mount | awk '$3 ~/^\/oldroot/ {print $3}' | sort -r)"
99

1010
# Remove all dm-snapshot devices.
1111
dmsetup remove_all
1212

1313
# Remove all loopback devices.
1414
for _lup in $(grep ^/dev/loop /oldrun/archiso/used_block_devices | tac); do
15-
if ! losetup -d ${_lup} 2> /dev/null; then
16-
umount -d ${_lup}
15+
if ! losetup -d -- "${_lup}" 2> /dev/null; then
16+
umount -d -- "${_lup}"
1717
fi
1818
done
1919

2020
# Unmount the space used to store *.cow.
2121
umount /oldrun/archiso/cowspace
2222

2323
# Unmount boot device if needed (no copytoram=y used)
24-
if [[ ! -d /oldrun/archiso/copytoram ]]; then
25-
if [[ -d /oldrun/archiso/img_dev ]]; then
24+
if [ ! -d /oldrun/archiso/copytoram ]; then
25+
if [ -d /oldrun/archiso/img_dev ]; then
2626
umount /oldrun/archiso/img_dev
2727
else
2828
umount /oldrun/archiso/bootmnt
@@ -35,3 +35,5 @@ case "$1" in
3535
reboot|poweroff|halt) "$1" -f ;;
3636
*) halt -f;;
3737
esac
38+
39+
# vim: set ft=sh:

system/initcpio/hooks/archiso

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/bin/ash
2+
13
# args: source, newroot, mountpoint
24
_mnt_dmsnapshot() {
35
local img="${1}"
@@ -8,46 +10,49 @@ _mnt_dmsnapshot() {
810
local dm_snap_name="${dm_snap_prefix}_${img_name}"
911
local ro_dev ro_dev_size rw_dev
1012

11-
ro_dev=$(losetup --find --show --read-only "${img}")
12-
echo ${ro_dev} >> /run/archiso/used_block_devices
13-
ro_dev_size=$(blockdev --getsz ${ro_dev})
13+
ro_dev="$(losetup --find --show --read-only -- "${img}")"
14+
echo "${ro_dev}" >> /run/archiso/used_block_devices
15+
ro_dev_size="$(blockdev --getsz -- "${ro_dev}")"
1416

15-
if [[ "${cow_persistent}" == "P" ]]; then
16-
if [[ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]]; then
17+
if [ "${cow_persistent}" = "P" ]; then
18+
if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then
1719
msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow', using as persistent."
1820
else
1921
msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as persistent."
2022
truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
2123
fi
2224
else
23-
if [[ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]]; then
25+
if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then
2426
msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' but non-persistent requested, removing."
2527
rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
2628
fi
2729
msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as non-persistent."
2830
truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
2931
fi
3032

31-
rw_dev=$(losetup --find --show "/run/archiso/cowspace/${cow_directory}/${img_name}.cow")
32-
echo ${rw_dev} >> /run/archiso/used_block_devices
33+
rw_dev="$(losetup --find --show "/run/archiso/cowspace/${cow_directory}/${img_name}.cow")"
34+
echo "${rw_dev}" >> /run/archiso/used_block_devices
3335

34-
dmsetup create ${dm_snap_name} --table "0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} ${cow_chunksize}"
36+
dmsetup create "${dm_snap_name}" --table \
37+
"0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} ${cow_chunksize}"
3538

36-
if [[ "${cow_persistent}" != "P" ]]; then
39+
if [ "${cow_persistent}" != "P" ]; then
3740
rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
3841
fi
3942

4043
_mnt_dev "/dev/mapper/${dm_snap_name}" "${newroot}${mnt}" "-w" "defaults"
41-
echo $(readlink -f /dev/mapper/${dm_snap_name}) >> /run/archiso/used_block_devices
44+
readlink -f "/dev/mapper/${dm_snap_name}" >> /run/archiso/used_block_devices
4245
}
4346

4447
# args: source, newroot, mountpoint
4548
_mnt_overlayfs() {
4649
local src="${1}"
4750
local newroot="${2}"
4851
local mnt="${3}"
49-
mkdir -p /run/archiso/cowspace/${cow_directory}/upperdir /run/archiso/cowspace/${cow_directory}/workdir
50-
mount -t overlay -o lowerdir=${src},upperdir=/run/archiso/cowspace/${cow_directory}/upperdir,workdir=/run/archiso/cowspace/${cow_directory}/workdir airootfs "${newroot}${mnt}"
52+
mkdir -p "/run/archiso/cowspace/${cow_directory}/upperdir" "/run/archiso/cowspace/${cow_directory}/workdir"
53+
mount -t overlay -o \
54+
"lowerdir=${src},upperdir=/run/archiso/cowspace/${cow_directory}/upperdir,workdir=/run/archiso/cowspace/${cow_directory}/workdir" \
55+
airootfs "${newroot}${mnt}"
5156
}
5257

5358

@@ -58,17 +63,18 @@ _mnt_sfs() {
5863
local img_fullname="${img##*/}"
5964
local sfs_dev
6065

61-
if [[ "${copytoram}" == "y" ]]; then
66+
# shellcheck disable=SC2154 # defined via initcpio's parse_cmdline()
67+
if [ "${copytoram}" = "y" ]; then
6268
msg -n ":: Copying squashfs image to RAM..."
63-
if ! cp "${img}" "/run/archiso/copytoram/${img_fullname}" ; then
69+
if ! cp -- "${img}" "/run/archiso/copytoram/${img_fullname}" ; then
6470
echo "ERROR: while copy '${img}' to '/run/archiso/copytoram/${img_fullname}'"
6571
launch_interactive_shell
6672
fi
6773
img="/run/archiso/copytoram/${img_fullname}"
6874
msg "done."
6975
fi
70-
sfs_dev=$(losetup --find --show --read-only "${img}")
71-
echo ${sfs_dev} >> /run/archiso/used_block_devices
76+
sfs_dev="$(losetup --find --show --read-only -- "${img}")"
77+
echo "${sfs_dev}" >> /run/archiso/used_block_devices
7278
_mnt_dev "${sfs_dev}" "${mnt}" "-r" "defaults"
7379
}
7480

@@ -102,45 +108,46 @@ _mnt_dev() {
102108

103109
_verify_checksum() {
104110
local _status
105-
cd "/run/archiso/bootmnt/${archisobasedir}/${arch}"
111+
cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
106112
sha512sum -c airootfs.sha512 > /tmp/checksum.log 2>&1
107113
_status=$?
108-
cd "${OLDPWD}"
109-
return ${_status}
114+
cd -- "${OLDPWD}" || exit 1
115+
return "${_status}"
110116
}
111117

112118
_verify_signature() {
113119
local _status
114-
cd "/run/archiso/bootmnt/${archisobasedir}/${arch}"
120+
cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
115121
gpg --homedir /gpg --status-fd 1 --verify airootfs.sfs.sig 2>/dev/null | grep -qE '^\[GNUPG:\] GOODSIG'
116122
_status=$?
117-
cd "${OLDPWD}"
123+
cd -- "${OLDPWD}" || exit 1
118124
return ${_status}
119125
}
120126

121127
run_hook() {
122-
[[ -z "${arch}" ]] && arch="$(uname -m)"
123-
[[ -z "${copytoram_size}" ]] && copytoram_size="75%"
124-
[[ -z "${archisobasedir}" ]] && archisobasedir="arch"
125-
[[ -z "${dm_snap_prefix}" ]] && dm_snap_prefix="arch"
126-
[[ -z "${archisodevice}" ]] && archisodevice="/dev/disk/by-label/${archisolabel}"
127-
[[ -z "${cow_spacesize}" ]] && cow_spacesize="256M"
128-
129-
if [[ -n "${cow_label}" ]]; then
128+
[ -z "${arch}" ] && arch="$(uname -m)"
129+
[ -z "${copytoram_size}" ] && copytoram_size="75%"
130+
[ -z "${archisobasedir}" ] && archisobasedir="arch"
131+
[ -z "${dm_snap_prefix}" ] && dm_snap_prefix="arch"
132+
# shellcheck disable=SC2154 # defined via initcpio's parse_cmdline()
133+
[ -z "${archisodevice}" ] && archisodevice="/dev/disk/by-label/${archisolabel}"
134+
[ -z "${cow_spacesize}" ] && cow_spacesize="256M"
135+
# shellcheck disable=SC2154 # defined via initcpio's parse_cmdline()
136+
if [ -n "${cow_label}" ]; then
130137
cow_device="/dev/disk/by-label/${cow_label}"
131-
[[ -z "${cow_persistent}" ]] && cow_persistent="P"
132-
elif [[ -n "${cow_device}" ]]; then
133-
[[ -z "${cow_persistent}" ]] && cow_persistent="P"
138+
[ -z "${cow_persistent}" ] && cow_persistent="P"
139+
elif [ -n "${cow_device}" ]; then
140+
[ -z "${cow_persistent}" ] && cow_persistent="P"
134141
else
135142
cow_persistent="N"
136143
fi
137144

138-
[[ -z "${cow_flags}" ]] && cow_flags="defaults"
139-
[[ -z "${cow_directory}" ]] && cow_directory="persistent_${archisolabel}/${arch}"
140-
[[ -z "${cow_chunksize}" ]] && cow_chunksize="8"
145+
[ -z "${cow_flags}" ] && cow_flags="defaults"
146+
[ -z "${cow_directory}" ] && cow_directory="persistent_${archisolabel}/${arch}"
147+
[ -z "${cow_chunksize}" ] && cow_chunksize="8"
141148

142149
# set mount handler for archiso
143-
mount_handler="archiso_mount_handler"
150+
export mount_handler="archiso_mount_handler"
144151
}
145152

146153
# This function is called normally from init script, but it can be called
@@ -151,13 +158,14 @@ archiso_mount_handler() {
151158

152159
if ! mountpoint -q "/run/archiso/bootmnt"; then
153160
_mnt_dev "${archisodevice}" "/run/archiso/bootmnt" "-r" "defaults"
154-
if [[ "${copytoram}" != "y" ]]; then
155-
echo $(readlink -f ${archisodevice}) >> /run/archiso/used_block_devices
161+
if [ "${copytoram}" != "y" ]; then
162+
readlink -f "${archisodevice}" >> /run/archiso/used_block_devices
156163
fi
157164
fi
158165

159-
if [[ "${checksum}" == "y" ]]; then
160-
if [[ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sha512" ]]; then
166+
# shellcheck disable=SC2154 # defined via initcpio's parse_cmdline()
167+
if [ "${checksum}" = "y" ]; then
168+
if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sha512" ]; then
161169
msg -n ":: Self-test requested, please wait..."
162170
if _verify_checksum; then
163171
msg "done. Checksum is OK, continue booting."
@@ -172,8 +180,9 @@ archiso_mount_handler() {
172180
fi
173181
fi
174182

175-
if [[ "${verify}" == "y" ]]; then
176-
if [[ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs.sig" ]]; then
183+
# shellcheck disable=SC2154 # defined via initcpio's parse_cmdline()
184+
if [ "${verify}" = "y" ]; then
185+
if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs.sig" ]; then
177186
msg -n ":: Signature verification requested, please wait..."
178187
if _verify_signature; then
179188
msg "done. Signature is OK, continue booting."
@@ -187,33 +196,34 @@ archiso_mount_handler() {
187196
fi
188197
fi
189198

190-
if [[ "${copytoram}" == "y" ]]; then
199+
if [ "${copytoram}" = "y" ]; then
191200
msg ":: Mounting /run/archiso/copytoram (tmpfs) filesystem, size=${copytoram_size}"
192201
mkdir -p /run/archiso/copytoram
193202
mount -t tmpfs -o "size=${copytoram_size}",mode=0755 copytoram /run/archiso/copytoram
194203
fi
195204

196-
if [[ -n "${cow_device}" ]]; then
205+
if [ -n "${cow_device}" ]; then
197206
_mnt_dev "${cow_device}" "/run/archiso/cowspace" "-r" "${cow_flags}"
198-
echo $(readlink -f ${cow_device}) >> /run/archiso/used_block_devices
207+
readlink -f "${cow_device}" >> /run/archiso/used_block_devices
199208
mount -o remount,rw "/run/archiso/cowspace"
200209
else
201210
msg ":: Mounting /run/archiso/cowspace (tmpfs) filesystem, size=${cow_spacesize}..."
202211
mkdir -p /run/archiso/cowspace
203212
mount -t tmpfs -o "size=${cow_spacesize}",mode=0755 cowspace /run/archiso/cowspace
204213
fi
205-
mkdir -p -m 0700 "/run/archiso/cowspace/${cow_directory}"
214+
mkdir -p "/run/archiso/cowspace/${cow_directory}"
215+
chmod 0700 "/run/archiso/cowspace/${cow_directory}"
206216

207217
_mnt_sfs "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs" "/run/archiso/sfs/airootfs"
208-
if [[ -f "/run/archiso/sfs/airootfs/airootfs.img" ]]; then
218+
if [ -f "/run/archiso/sfs/airootfs/airootfs.img" ]; then
209219
_mnt_dmsnapshot "/run/archiso/sfs/airootfs/airootfs.img" "${newroot}" "/"
210220
else
211221
_mnt_overlayfs "/run/archiso/sfs/airootfs" "${newroot}" "/"
212222
fi
213223

214-
if [[ "${copytoram}" == "y" ]]; then
224+
if [ "${copytoram}" = "y" ]; then
215225
umount -d /run/archiso/bootmnt
216226
fi
217227
}
218228

219-
# vim:ft=sh:ts=4:sw=4:et:
229+
# vim: set ft=sh:
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# vim: set ft=sh:
1+
#!/bin/ash
22

33
run_hook () {
4-
[[ -n "${img_label}" ]] && img_dev="/dev/disk/by-label/${img_label}"
5-
[[ -z "${img_flags}" ]] && img_flags="defaults"
6-
if [[ -n "${img_dev}" && -n "${img_loop}" ]]; then
7-
mount_handler="archiso_loop_mount_handler"
4+
# shellcheck disable=SC2154 # defined via initcpio's parse_cmdline()
5+
[ -n "${img_label}" ] && img_dev="/dev/disk/by-label/${img_label}"
6+
[ -z "${img_flags}" ] && img_flags="defaults"
7+
# shellcheck disable=SC2154 # defined via initcpio's parse_cmdline()
8+
if [ -n "${img_dev}" ] && [ -n "${img_loop}" ]; then
9+
export mount_handler="archiso_loop_mount_handler"
810
fi
911
}
1012

@@ -15,21 +17,24 @@ archiso_loop_mount_handler () {
1517

1618
msg ":: Setup a loop device from ${img_loop} located at device ${img_dev}"
1719
_mnt_dev "${img_dev}" "/run/archiso/img_dev" "-r" "${img_flags}"
18-
if [[ "${copytoram}" != "y" ]]; then
19-
echo $(readlink -f ${img_dev}) >> /run/archiso/used_block_devices
20+
# shellcheck disable=SC2154 # defined via initcpio's parse_cmdline()
21+
if [ "${copytoram}" != "y" ]; then
22+
readlink -f "${img_dev}" >> /run/archiso/used_block_devices
2023
fi
2124

2225
if _dev_loop=$(losetup --find --show --read-only "/run/archiso/img_dev/${img_loop}"); then
23-
archisodevice="${_dev_loop}"
26+
export archisodevice="${_dev_loop}"
2427
else
2528
echo "ERROR: Setting loopback device for file '/run/archiso/img_dev/${img_loop}'"
2629
launch_interactive_shell
2730
fi
2831

29-
archiso_mount_handler ${newroot}
32+
archiso_mount_handler "${newroot}"
3033

31-
if [[ "${copytoram}" == "y" ]]; then
32-
losetup -d ${_dev_loop} 2>/dev/null
34+
if [ "${copytoram}" = "y" ]; then
35+
losetup -d "${_dev_loop}" 2>/dev/null
3336
umount /run/archiso/img_dev
3437
fi
3538
}
39+
40+
# vim: set ft=sh:

0 commit comments

Comments
 (0)