Shellcheck cleanup for initrd scripts

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Co-authored-by: Kash Pande <kash@tripleback.net>
Co-authored-by: Matthew Thode <mthode@mthode.org>
Signed-off-by: Kash Pande <kash@tripleback.net>
Signed-off-by: Matthew Thode <mthode@mthode.org>
Closes #7214
This commit is contained in:
Kash Pande 2018-02-21 20:54:54 -05:00 committed by Brian Behlendorf
parent 7280d58197
commit 41532e5a29
6 changed files with 95 additions and 98 deletions

View File

@ -3,9 +3,9 @@
. /lib/dracut-zfs-lib.sh . /lib/dracut-zfs-lib.sh
_do_zpool_export() { _do_zpool_export() {
local ret=0 ret=0
local errs="" errs=""
local final="${1}" final="${1}"
info "ZFS: Exporting ZFS storage pools..." info "ZFS: Exporting ZFS storage pools..."
errs=$(export_all -F 2>&1) errs=$(export_all -F 2>&1)

View File

@ -13,7 +13,7 @@ esac
GENERATOR_FILE=/run/systemd/generator/sysroot.mount GENERATOR_FILE=/run/systemd/generator/sysroot.mount
GENERATOR_EXTENSION=/run/systemd/generator/sysroot.mount.d/zfs-enhancement.conf GENERATOR_EXTENSION=/run/systemd/generator/sysroot.mount.d/zfs-enhancement.conf
if [ -e "$GENERATOR_FILE" -a -e "$GENERATOR_EXTENSION" ] ; then if [ -e "$GENERATOR_FILE" ] && [ -e "$GENERATOR_EXTENSION" ] ; then
# If the ZFS sysroot.mount flag exists, the initial RAM disk configured # If the ZFS sysroot.mount flag exists, the initial RAM disk configured
# it to mount ZFS on root. In that case, we bail early. This flag # it to mount ZFS on root. In that case, we bail early. This flag
# file gets created by the zfs-generator program upon successful run. # file gets created by the zfs-generator program upon successful run.
@ -57,9 +57,9 @@ ZFS_POOL="${ZFS_DATASET%%/*}"
if import_pool "${ZFS_POOL}" ; then if import_pool "${ZFS_POOL}" ; then
# Load keys if we can or if we need to # Load keys if we can or if we need to
if [ $(zpool list -H -o feature@encryption $(echo "${ZFS_POOL}" | awk -F\/ '{print $1}')) == 'active' ]; then if [ $(zpool list -H -o feature@encryption $(echo "${ZFS_POOL}" | awk -F\/ '{print $1}')) = 'active' ]; then
# if the root dataset has encryption enabled # if the root dataset has encryption enabled
ENCRYPTIONROOT=$(zfs get -H -o value encryptionroot ${ZFS_DATASET}) ENCRYPTIONROOT="$(zfs get -H -o value encryptionroot "${ZFS_DATASET}")"
if ! [ "${ENCRYPTIONROOT}" = "-" ]; then if ! [ "${ENCRYPTIONROOT}" = "-" ]; then
# decrypt them # decrypt them
ask_for_password \ ask_for_password \

View File

@ -3,16 +3,16 @@
. /lib/dracut-lib.sh . /lib/dracut-lib.sh
# Let the command line override our host id. # Let the command line override our host id.
spl_hostid=`getarg spl_hostid=` spl_hostid=$(getarg spl_hostid=)
if [ -n "${spl_hostid}" ] ; then if [ -n "${spl_hostid}" ] ; then
info "ZFS: Using hostid from command line: ${spl_hostid}" info "ZFS: Using hostid from command line: ${spl_hostid}"
AA=`echo ${spl_hostid} | cut -b 1,2` AA=$(echo "${spl_hostid}" | cut -b 1,2)
BB=`echo ${spl_hostid} | cut -b 3,4` BB=$(echo "${spl_hostid}" | cut -b 3,4)
CC=`echo ${spl_hostid} | cut -b 5,6` CC=$(echo "${spl_hostid}" | cut -b 5,6)
DD=`echo ${spl_hostid} | cut -b 7,8` DD=$(echo "${spl_hostid}" | cut -b 7,8)
printf "\x${DD}\x${CC}\x${BB}\x${AA}" >/etc/hostid printf "\\x%s\\x%s\\x%s\\x%s" "${DD}" "${CC}" "${BB}" "${AA}" >/etc/hostid
elif [ -f "/etc/hostid" ] ; then elif [ -f "/etc/hostid" ] ; then
info "ZFS: Using hostid from /etc/hostid: `hostid`" info "ZFS: Using hostid from /etc/hostid: $(hostid)"
else else
warn "ZFS: No hostid found on kernel command line or /etc/hostid." warn "ZFS: No hostid found on kernel command line or /etc/hostid."
warn "ZFS: Pools may not import correctly." warn "ZFS: Pools may not import correctly."

View File

@ -5,16 +5,16 @@ command -v getargbool >/dev/null || {
# Compatibility with older Dracut versions. # Compatibility with older Dracut versions.
# With apologies to the Dracut developers. # With apologies to the Dracut developers.
getargbool() { getargbool() {
local _b if ! [ -z "$_b" ]; then
unset _b unset _b
local _default fi
_default="$1"; shift _default="$1"; shift
_b=$(getarg "$@") _b=$(getarg "$@")
[ $? -ne 0 -a -z "$_b" ] && _b="$_default" [ $? -ne 0 ] && [ -z "$_b" ] && _b="$_default"
if [ -n "$_b" ]; then if [ -n "$_b" ]; then
[ $_b = "0" ] && return 1 [ "$_b" = "0" ] && return 1
[ $_b = "no" ] && return 1 [ "$_b" = "no" ] && return 1
[ $_b = "off" ] && return 1 [ "$_b" = "off" ] && return 1
fi fi
return 0 return 0
} }
@ -26,82 +26,82 @@ NEWLINE="
ZPOOL_IMPORT_OPTS="" ZPOOL_IMPORT_OPTS=""
if getargbool 0 zfs_force -y zfs.force -y zfsforce ; then if getargbool 0 zfs_force -y zfs.force -y zfsforce ; then
warn "ZFS: Will force-import pools if necessary." warn "ZFS: Will force-import pools if necessary."
ZPOOL_IMPORT_OPTS="${ZPOOL_IMPORT_OPTS} -f" ZPOOL_IMPORT_OPTS="${ZPOOL_IMPORT_OPTS} -f"
fi fi
# find_bootfs # find_bootfs
# returns the first dataset with the bootfs attribute. # returns the first dataset with the bootfs attribute.
find_bootfs() { find_bootfs() {
IFS="${NEWLINE}" IFS="${NEWLINE}"
for dataset in $(zpool list -H -o bootfs); do for dataset in $(zpool list -H -o bootfs); do
case "${dataset}" in case "${dataset}" in
"" | "-") "" | "-")
continue continue
;; ;;
"no pools available") "no pools available")
IFS="${OLDIFS}" IFS="${OLDIFS}"
return 1 return 1
;; ;;
*) *)
IFS="${OLDIFS}" IFS="${OLDIFS}"
echo "${dataset}" echo "${dataset}"
return 0 return 0
;; ;;
esac esac
done done
IFS="${OLDIFS}" IFS="${OLDIFS}"
return 1 return 1
} }
# import_pool POOL # import_pool POOL
# imports the given zfs pool if it isn't imported already. # imports the given zfs pool if it isn't imported already.
import_pool() { import_pool() {
local pool="${1}" pool="${1}"
if ! zpool list -H "${pool}" 2>&1 > /dev/null ; then if ! zpool list -H "${pool}" > /dev/null 2>&1; then
info "ZFS: Importing pool ${pool}..." info "ZFS: Importing pool ${pool}..."
if ! zpool import -N ${ZPOOL_IMPORT_OPTS} "${pool}" ; then if ! zpool import -N ${ZPOOL_IMPORT_OPTS} "${pool}" ; then
warn "ZFS: Unable to import pool ${pool}" warn "ZFS: Unable to import pool ${pool}"
return 1 return 1
fi fi
fi fi
return 0 return 0
} }
# mount_dataset DATASET # mount_dataset DATASET
# mounts the given zfs dataset. # mounts the given zfs dataset.
mount_dataset() { mount_dataset() {
local dataset="${1}" dataset="${1}"
local mountpoint="$(zfs get -H -o value mountpoint "${dataset}")" mountpoint="$(zfs get -H -o value mountpoint "${dataset}")"
# We need zfsutil for non-legacy mounts and not for legacy mounts. # We need zfsutil for non-legacy mounts and not for legacy mounts.
if [ "${mountpoint}" = "legacy" ] ; then if [ "${mountpoint}" = "legacy" ] ; then
mount -t zfs "${dataset}" "${NEWROOT}" mount -t zfs "${dataset}" "${NEWROOT}"
else else
mount -o zfsutil -t zfs "${dataset}" "${NEWROOT}" mount -o zfsutil -t zfs "${dataset}" "${NEWROOT}"
fi fi
return $? return $?
} }
# export_all OPTS # export_all OPTS
# exports all imported zfs pools. # exports all imported zfs pools.
export_all() { export_all() {
local opts="${@}" opts="${@}"
local ret=0 ret=0
IFS="${NEWLINE}" IFS="${NEWLINE}"
for pool in `zpool list -H -o name` ; do for pool in $(zpool list -H -o name) ; do
if zpool list -H "${pool}" 2>&1 > /dev/null ; then if zpool list -H "${pool}" > /dev/null 2>&1; then
zpool export "${pool}" ${opts} || ret=$? zpool export "${pool}" ${opts} || ret=$?
fi fi
done done
IFS="${OLDIFS}" IFS="${OLDIFS}"
return ${ret} return ${ret}
} }
# ask_for_password # ask_for_password
@ -124,12 +124,9 @@ export_all() {
# Turn off input echo before tty command is executed and turn on after. # Turn off input echo before tty command is executed and turn on after.
# It's useful when password is read from stdin. # It's useful when password is read from stdin.
ask_for_password() { ask_for_password() {
local cmd; local prompt; local tries=3 ply_tries=3
local ply_cmd; local ply_prompt; local ply_tries=3 tty_tries=3
local tty_cmd; local tty_prompt; local tty_tries=3 while [ "$#" -gt 0 ]; do
local ret
while [ $# -gt 0 ]; do
case "$1" in case "$1" in
--cmd) ply_cmd="$2"; tty_cmd="$2"; shift;; --cmd) ply_cmd="$2"; tty_cmd="$2"; shift;;
--ply-cmd) ply_cmd="$2"; shift;; --ply-cmd) ply_cmd="$2"; shift;;
@ -147,9 +144,9 @@ ask_for_password() {
{ flock -s 9; { flock -s 9;
# Prompt for password with plymouth, if installed and running. # Prompt for password with plymouth, if installed and running.
if type plymouth >/dev/null 2>&1 && plymouth --ping 2>/dev/null; then if whereis plymouth >/dev/null 2>&1 && plymouth --ping 2>/dev/null; then
plymouth ask-for-password \ plymouth ask-for-password \
--prompt "$ply_prompt" --number-of-tries=$ply_tries \ --prompt "$ply_prompt" --number-of-tries="$ply_tries" \
--command="$ply_cmd" --command="$ply_cmd"
ret=$? ret=$?
else else
@ -158,17 +155,17 @@ ask_for_password() {
stty -echo stty -echo
fi fi
local i=1 i=1
while [ $i -le $tty_tries ]; do while [ "$i" -le "$tty_tries" ]; do
[ -n "$tty_prompt" ] && \ [ -n "$tty_prompt" ] && \
printf "$tty_prompt [$i/$tty_tries]:" >&2 printf "%s [%i/%i]:" "$tty_prompt" "$i" "$tty_tries" >&2
eval "$tty_cmd" && ret=0 && break eval "$tty_cmd" && ret=0 && break
ret=$? ret=$?
i=$(($i+1)) i=$((i+1))
[ -n "$tty_prompt" ] && printf '\n' >&2 [ -n "$tty_prompt" ] && printf '\n' >&2
done done
unset i
[ "$tty_echo_off" = yes ] && stty $stty_orig [ "$tty_echo_off" = yes ] && stty "$stty_orig"
fi fi
} 9>/.console_lock } 9>/.console_lock

View File

@ -1,47 +1,47 @@
#!/bin/bash #!/bin/sh
# only run this on systemd systems, we handle the decrypt in mount-zfs.sh in the mount hook otherwise # only run this on systemd systems, we handle the decrypt in mount-zfs.sh in the mount hook otherwise
[[ -e /bin/systemctl ]] || return 0 [ -e /bin/systemctl ] || return 0
# This script only gets executed on systemd systems, see mount-zfs.sh for non-systemd systems # This script only gets executed on systemd systems, see mount-zfs.sh for non-systemd systems
# import the libs now that we know the pool imported # import the libs now that we know the pool imported
[ -f /lib/dracut-lib.sh ] && dracutlib=/lib/dracut-lib.sh [ -f /lib/dracut-lib.sh ] && dracutlib=/lib/dracut-lib.sh
[ -f /usr/lib/dracut/modules.d/99base/dracut-lib.sh ] && dracutlib=/usr/lib/dracut/modules.d/99base/dracut-lib.sh [ -f /usr/lib/dracut/modules.d/99base/dracut-lib.sh ] && dracutlib=/usr/lib/dracut/modules.d/99base/dracut-lib.sh
# shellcheck source=./lib-zfs.sh.in
. "$dracutlib" . "$dracutlib"
# load the kernel command line vars # load the kernel command line vars
[ -z "$root" ] && root=$(getarg root=) [ -z "$root" ] && root="$(getarg root=)"
# If root is not ZFS= or zfs: or rootfstype is not zfs then we are not supposed to handle it. # If root is not ZFS= or zfs: or rootfstype is not zfs then we are not supposed to handle it.
[ "${root##zfs:}" = "${root}" -a "${root##ZFS=}" = "${root}" -a "$rootfstype" != "zfs" ] && exit 0 [ "${root##zfs:}" = "${root}" ] && [ "${root##ZFS=}" = "${root}" ] && [ "$rootfstype" != "zfs" ] && exit 0
# There is a race between the zpool import and the pre-mount hooks, so we wait for a pool to be imported # There is a race between the zpool import and the pre-mount hooks, so we wait for a pool to be imported
while true; do while true; do
zpool list -H | grep -q -v '^$' && break zpool list -H | grep -q -v '^$' && break
[[ $(systemctl is-failed zfs-import-cache.service) == 'failed' ]] && exit 1 [ "$(systemctl is-failed zfs-import-cache.service)" = 'failed' ] && exit 1
[[ $(systemctl is-failed zfs-import-scan.service) == 'failed' ]] && exit 1 [ "$(systemctl is-failed zfs-import-scan.service)" = 'failed' ] && exit 1
sleep 0.1s sleep 0.1s
done done
# run this after import as zfs-import-cache/scan service is confirmed good # run this after import as zfs-import-cache/scan service is confirmed good
if [[ "${root}" = "zfs:AUTO" ]] ; then if [ "${root}" = "zfs:AUTO" ] ; then
root=$(zpool list -H -o bootfs | awk '$1 != "-" {print; exit}') root="$(zpool list -H -o bootfs | awk '$1 != "-" {print; exit}')"
else else
root="${root##zfs:}" root="${root##zfs:}"
root="${root##ZFS=}" root="${root##ZFS=}"
fi fi
# if pool encryption is active and the zfs command understands '-o encryption' # if pool encryption is active and the zfs command understands '-o encryption'
if [[ $(zpool list -H -o feature@encryption $(echo "${root}" | awk -F\/ '{print $1}')) == 'active' ]]; then if [ "$(zpool list -H -o feature@encryption $(echo "${root}" | awk -F\/ '{print $1}'))" = 'active' ]; then
# if the root dataset has encryption enabled # if the root dataset has encryption enabled
ENCRYPTIONROOT=$(zfs get -H -o value encryptionroot ${ZFS_DATASET}) ENCRYPTIONROOT=$(zfs get -H -o value encryptionroot "${root}")
if ! [ "${ENCRYPTIONROOT}" = "-" ]; then if ! [ "${ENCRYPTIONROOT}" = "-" ]; then
# decrypt them # decrypt them
TRY_COUNT=5 TRY_COUNT=5
while [ $TRY_COUNT != 0 ]; do while [ $TRY_COUNT -gt 0 ]; do
zfs load-key "${ENCRYPTIONROOT}" <<< $(systemd-ask-password "Encrypted ZFS password for ${root}: ") systemd-ask-password "Encrypted ZFS password for ${root}" --no-tty | zfs load-key "${ENCRYPTIONROOT}" && break
[[ $? == 0 ]] && break TRY_COUNT=$((TRY_COUNT - 1))
((TRY_COUNT-=1))
done done
fi fi
fi fi