Skip to content

Commit

Permalink
fix(base): shellcheck for modules.d/99base
Browse files Browse the repository at this point in the history
Also remove some functions, which are not used and broken anyway.
  • Loading branch information
haraldh committed Mar 26, 2021
1 parent cbef7cf commit 2fabaaa
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 219 deletions.
Empty file added modules.d/99base/.shchkdir
Empty file.
247 changes: 122 additions & 125 deletions modules.d/99base/dracut-lib.sh

Large diffs are not rendered by default.

108 changes: 43 additions & 65 deletions modules.d/99base/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH

# mount some important things
[ ! -d /proc/self ] \
&& mount -t proc -o nosuid,noexec,nodev proc /proc > /dev/null

if [ "$?" != "0" ]; then
echo "Cannot mount proc on /proc! Compile the kernel with CONFIG_PROC_FS!"
exit 1
if [ ! -d /proc/self ]; then
if ! mount -t proc -o nosuid,noexec,nodev proc /proc > /dev/null; then
echo "Cannot mount proc on /proc! Compile the kernel with CONFIG_PROC_FS!"
exit 1
fi
fi

[ ! -d /sys/kernel ] \
&& mount -t sysfs -o nosuid,noexec,nodev sysfs /sys > /dev/null

if [ "$?" != "0" ]; then
echo "Cannot mount sysfs on /sys! Compile the kernel with CONFIG_SYSFS!"
exit 1
if [ ! -d /sys/kernel ]; then
if ! mount -t sysfs -o nosuid,noexec,nodev sysfs /sys > /dev/null; then
echo "Cannot mount sysfs on /sys! Compile the kernel with CONFIG_SYSFS!"
exit 1
fi
fi

RD_DEBUG=""
Expand Down Expand Up @@ -77,35 +75,28 @@ fi

if command -v kmod > /dev/null 2> /dev/null; then
kmod static-nodes --format=tmpfiles 2> /dev/null \
| while read type file mode a a a majmin || [ -n "$type" ]; do
| while read -r type file mode _ _ _ majmin || [ -n "$type" ]; do
type=${type%\!}
case $type in
d)
mkdir -m $mode -p $file
mkdir -m "$mode" -p "$file"
;;
c)
mknod -m $mode $file $type ${majmin%:*} ${majmin#*:}
mknod -m "$mode" "$file" "$type" "${majmin%:*}" "${majmin#*:}"
;;
esac
done
fi

trap "emergency_shell Signal caught!" 0

export UDEVVERSION=$(udevadm --version)
if [ $UDEVVERSION -gt 166 ]; then
# newer versions of udev use /run/udev/rules.d
export UDEVRULESD=/run/udev/rules.d
[ -d /run/udev ] || mkdir -p -m 0755 /run/udev
[ -d $UDEVRULESD ] || mkdir -p -m 0755 $UDEVRULESD
else
mkdir -m 0755 -p /dev/.udev /dev/.udev/rules.d
export UDEVRULESD=/dev/.udev/rules.d
fi
export UDEVRULESD=/run/udev/rules.d
[ -d /run/udev ] || mkdir -p -m 0755 /run/udev
[ -d "$UDEVRULESD" ] || mkdir -p -m 0755 "$UDEVRULESD"

if [ "$RD_DEBUG" = "yes" ]; then
mkfifo /run/initramfs/loginit.pipe
loginit $DRACUT_QUIET < /run/initramfs/loginit.pipe > /dev/console 2>&1 &
loginit "$DRACUT_QUIET" < /run/initramfs/loginit.pipe > /dev/console 2>&1 &
exec > /run/initramfs/loginit.pipe 2>&1
else
exec 0<> /dev/console 1<> /dev/console 2<> /dev/console
Expand All @@ -118,7 +109,7 @@ source_conf /etc/conf.d

if getarg "rd.cmdline=ask"; then
echo "Enter additional kernel command line parameter (end with ctrl-d or .)"
while read -p "> " line || [ -n "$line" ]; do
while read -r -p "> " line || [ -n "$line" ]; do
[ "$line" = "." ] && break
echo "$line" >> /etc/cmdline.d/99-cmdline-ask.conf
done
Expand Down Expand Up @@ -150,14 +141,10 @@ getargbool 0 rd.udev.info -d -y rdudevinfo && UDEV_LOG=info
getargbool 0 rd.udev.debug -d -y rdudevdebug && UDEV_LOG=debug

# start up udev and trigger cold plugs
UDEV_LOG=$UDEV_LOG $systemdutildir/systemd-udevd --daemon --resolve-names=never
UDEV_LOG=$UDEV_LOG "$systemdutildir"/systemd-udevd --daemon --resolve-names=never

UDEV_QUEUE_EMPTY="udevadm settle --timeout=0"

if [ $UDEVVERSION -lt 140 ]; then
UDEV_QUEUE_EMPTY="udevadm settle --timeout=1"
fi

udevproperty "hookdir=$hookdir"

make_trace_mem "hook pre-trigger" '1:shortmem' '2+:mem' '3+:slab'
Expand Down Expand Up @@ -186,21 +173,23 @@ while :; do

check_finished && break

if [ -f $hookdir/initqueue/work ]; then
rm -f -- $hookdir/initqueue/work
if [ -f "$hookdir"/initqueue/work ]; then
rm -f -- "$hookdir"/initqueue/work
fi

for job in $hookdir/initqueue/*.sh; do
for job in "$hookdir"/initqueue/*.sh; do
[ -e "$job" ] || break
job=$job . $job
# shellcheck disable=SC2097 disable=SC1090 disable=SC2098
job=$job . "$job"
check_finished && break 2
done

$UDEV_QUEUE_EMPTY > /dev/null 2>&1 || continue

for job in $hookdir/initqueue/settled/*.sh; do
for job in "$hookdir"/initqueue/settled/*.sh; do
[ -e "$job" ] || break
job=$job . $job
# shellcheck disable=SC2097 disable=SC1090 disable=SC2098
job=$job . "$job"
check_finished && break 2
done

Expand All @@ -210,11 +199,12 @@ while :; do
sleep 0.5

if [ $main_loop -gt $((2 * RDRETRY / 3)) ]; then
for job in $hookdir/initqueue/timeout/*.sh; do
for job in "$hookdir"/initqueue/timeout/*.sh; do
[ -e "$job" ] || break
job=$job . $job
# shellcheck disable=SC2097 disable=SC1090 disable=SC2098
job=$job . "$job"
udevadm settle --timeout=0 > /dev/null 2>&1 || main_loop=0
[ -f $hookdir/initqueue/work ] && main_loop=0
[ -f "$hookdir"/initqueue/work ] && main_loop=0
done
fi

Expand Down Expand Up @@ -245,7 +235,8 @@ while :; do
usable_root "$NEWROOT" && break
umount "$NEWROOT"
fi
for f in $hookdir/mount/*.sh; do
for f in "$hookdir"/mount/*.sh; do
# shellcheck disable=SC1090
[ -f "$f" ] && . "$f"
if ismounted "$NEWROOT"; then
usable_root "$NEWROOT" && break
Expand All @@ -265,7 +256,7 @@ done

{
printf "Mounted root filesystem "
while read dev mp rest || [ -n "$dev" ]; do [ "$mp" = "$NEWROOT" ] && echo $dev; done < /proc/mounts
while read -r dev mp _ || [ -n "$dev" ]; do [ "$mp" = "$NEWROOT" ] && echo "$dev"; done < /proc/mounts
} | vinfo

# pre pivot scripts are sourced just before we doing cleanup and switch over
Expand Down Expand Up @@ -303,21 +294,8 @@ done
emergency_shell
}

if [ $UDEVVERSION -lt 168 ]; then
# stop udev queue before killing it
udevadm control --stop-exec-queue

HARD=""
while pidof udevd > /dev/null 2>&1; do
for pid in $(pidof udevd); do
kill $HARD $pid > /dev/null 2>&1
done
HARD="-9"
done
else
udevadm control --exit
udevadm info --cleanup-db
fi
udevadm control --exit
udevadm info --cleanup-db

debug_off # Turn off debugging for this section

Expand Down Expand Up @@ -345,23 +323,23 @@ done
rm -f -- /tmp/export.orig

initargs=""
read CLINE < /proc/cmdline
read -r CLINE < /proc/cmdline
if getarg init= > /dev/null; then
ignoreargs="console BOOT_IMAGE"
# only pass arguments after init= to the init
CLINE=${CLINE#*init=}
set -- $CLINE
set -- "$CLINE"
shift # clear out the rest of the "init=" arg
for x in "$@"; do
for s in $ignoreargs; do
[ "${x%%=*}" = $s ] && continue 2
[ "${x%%=*}" = "$s" ] && continue 2
done
initargs="$initargs $x"
done
unset CLINE
else
debug_off # Turn off debugging for this section
set -- $CLINE
set -- "$CLINE"
for x in "$@"; do
case "$x" in
[0-9] | s | S | single | emergency | auto)
Expand Down Expand Up @@ -399,17 +377,17 @@ if [ -f /etc/capsdrop ]; then
. /etc/capsdrop
info "Calling $INIT with capabilities $CAPS_INIT_DROP dropped."
unset RD_DEBUG
exec $CAPSH --drop="$CAPS_INIT_DROP" -- \
exec "$CAPSH" --drop="$CAPS_INIT_DROP" -- \
-c "exec switch_root \"$NEWROOT\" \"$INIT\" $initargs" \
|| {
warn "Command:"
warn capsh --drop=$CAPS_INIT_DROP -- -c exec switch_root "$NEWROOT" "$INIT" $initargs
warn capsh --drop="$CAPS_INIT_DROP" -- -c exec switch_root "$NEWROOT" "$INIT" "$initargs"
warn "failed."
emergency_shell
}
else
unset RD_DEBUG
exec $SWITCH_ROOT "$NEWROOT" "$INIT" $initargs || {
exec "$SWITCH_ROOT" "$NEWROOT" "$INIT" "$initargs" || {
warn "Something went very badly wrong in the initramfs. Please "
warn "file a bug against dracut."
emergency_shell
Expand Down
5 changes: 3 additions & 2 deletions modules.d/99base/initqueue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ fi
exe=$1
shift

[ -x "$exe" ] || exe=$(command -v $exe)
[ -x "$exe" ] || exe=$(command -v "$exe")
if [ -z "$exe" ]; then
echo "Invalid command"
exit 1
fi

{
# shellcheck disable=SC2016
[ -n "$onetime" ] && echo '[ -e "$job" ] && rm -f -- "$job"'
[ -n "$env" ] && echo "$env"
echo "$exe" "$@"
} > "/tmp/$$-${job}.sh"

mv -f "/tmp/$$-${job}.sh" "$hookdir/initqueue${qname}/${job}.sh"
[ -z "$qname" ] && >> $hookdir/initqueue/work
[ -z "$qname" ] && : >> "$hookdir"/initqueue/work
exit 0
5 changes: 3 additions & 2 deletions modules.d/99base/loginit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ set +x

QUIET=$1

printf -- "$$" > /run/initramfs/loginit.pid
printf "%s" "$$" > /run/initramfs/loginit.pid

# shellcheck disable=SC2015
[ -e /dev/kmsg ] && exec 5> /dev/kmsg || exec 5> /dev/null
exec 6> /run/initramfs/init.log

while read line || [ -n "$line" ]; do
while read -r line || [ -n "$line" ]; do
if [ "$line" = "DRACUT_LOG_END" ]; then
rm -f -- /run/initramfs/loginit.pipe
exit 0
Expand Down
38 changes: 20 additions & 18 deletions modules.d/99base/module-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ depends() {
install() {
inst_multiple mount mknod mkdir sleep chroot chown \
sed ls flock cp mv dmesg rm ln rmmod mkfifo umount readlink setsid \
modprobe
modprobe chmod

inst_multiple -o findmnt less kmod dracut-getargs

Expand Down Expand Up @@ -55,7 +55,7 @@ install() {
if ! dracut_module_included "systemd"; then
inst_multiple switch_root || dfatal "Failed to install switch_root"
inst_hook cmdline 10 "$moddir/parse-root-opts.sh"
inst_multiple -o $systemdutildir/systemd-timestamp
inst_multiple -o "$systemdutildir"/systemd-timestamp
fi

if [[ $realinitpath ]]; then
Expand All @@ -69,12 +69,13 @@ install() {
echo ro >> "${initdir}/etc/cmdline.d/base.conf"
fi

[ -e "${initdir}/usr/lib" ] || mkdir -m 0755 -p ${initdir}/usr/lib
[ -e "${initdir}/usr/lib" ] || mkdir -m 0755 -p "${initdir}"/usr/lib

local VERSION=""
local PRETTY_NAME=""
# Derive an os-release file from the host, if it exists
if [[ -e $dracutsysrootdir/etc/os-release ]]; then
# shellcheck disable=SC1090
. "$dracutsysrootdir"/etc/os-release
grep -hE -ve '^VERSION=' -ve '^PRETTY_NAME' "$dracutsysrootdir"/etc/os-release > "${initdir}"/usr/lib/initrd-release
[[ -n ${VERSION} ]] && VERSION+=" "
Expand All @@ -83,26 +84,26 @@ install() {
# Fall back to synthesizing one, since dracut is presently used
# on non-systemd systems as well.
{
echo NAME=dracut
echo ID=dracut
echo VERSION_ID=\"$DRACUT_VERSION\"
echo ANSI_COLOR='"0;34"'
} > ${initdir}/usr/lib/initrd-release
echo "NAME=dracut"
echo "ID=dracut"
echo "VERSION_ID=\"$DRACUT_VERSION\""
echo 'ANSI_COLOR="0;34"'
} > "${initdir}"/usr/lib/initrd-release
fi
VERSION+="dracut-$DRACUT_VERSION"
PRETTY_NAME+="dracut-$DRACUT_VERSION (Initramfs)"
{
echo VERSION=\"$VERSION\"
echo PRETTY_NAME=\"$PRETTY_NAME\"
echo "VERSION=\"$VERSION\""
echo "PRETTY_NAME=\"$PRETTY_NAME\""
# This addition is relatively new, intended to allow software
# to easily detect the dracut version if need be without
# having it mixed in with the real underlying OS version.
echo DRACUT_VERSION=\"${DRACUT_VERSION}\"
echo "DRACUT_VERSION=\"${DRACUT_VERSION}\""
} >> "$initdir"/usr/lib/initrd-release
echo "dracut-$DRACUT_VERSION" > "$initdir/lib/dracut/dracut-$DRACUT_VERSION"
ln -sf ../usr/lib/initrd-release $initdir/etc/initrd-release
ln -sf initrd-release $initdir/usr/lib/os-release
ln -sf initrd-release $initdir/etc/os-release
ln -sf ../usr/lib/initrd-release "$initdir"/etc/initrd-release
ln -sf initrd-release "$initdir"/usr/lib/os-release
ln -sf initrd-release "$initdir"/etc/os-release

## save host_devs which we need bring up
if [[ $hostonly_cmdline == "yes" ]]; then
Expand All @@ -112,10 +113,11 @@ install() {
if [[ -f $initdir/lib/dracut/need-initqueue ]] || ! dracut_module_included "systemd"; then
(
if dracut_module_included "systemd"; then
DRACUT_SYSTEMD=1
export DRACUT_SYSTEMD=1
fi
PREFIX="$initdir"
export PREFIX="$initdir"

# shellcheck source=dracut-lib.sh
. "$moddir/dracut-lib.sh"

for _dev in "${host_devs[@]}"; do
Expand All @@ -130,10 +132,10 @@ install() {
[[ $_dev == "$_dev2" ]] && continue 2
done

_pdev=$(get_persistent_dev $_dev)
_pdev=$(get_persistent_dev "$_dev")

case "$_pdev" in
/dev/?*) wait_for_dev $_pdev ;;
/dev/?*) wait_for_dev "$_pdev" ;;
*) ;;
esac
done
Expand Down
1 change: 1 addition & 0 deletions modules.d/99base/parse-root-opts.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/sh

# shellcheck disable=SC2034
root=$(getarg root=)

rflags="$(getarg rootflags=)"
Expand Down
Loading

0 comments on commit 2fabaaa

Please sign in to comment.