Skip to content

Commit

Permalink
feat(_sh): assure proper installation of a command shell
Browse files Browse the repository at this point in the history
If one of the supported shells is not in the add modules list,
use the realpath of /bin/sh, if it passes check_module. Otherwise,
use one of the supported shells. Fail if none of them passes.

This scheme assures that implicitly installed command shells pass
through their module-setup.sh with all their dependencies.

Completes fix to #1530 augmenting #1534.
  • Loading branch information
FGrose committed Jun 1, 2023
1 parent c08ae40 commit cb03a14
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
8 changes: 5 additions & 3 deletions dracut-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -999,11 +999,13 @@ check_module() {
[[ " $mods_to_load " == *\ $_mod\ * ]] && return 0
[[ " $mods_checked_as_dep " == *\ $_mod\ * ]] && return 1

# This should never happen, but...
[[ -d $_moddir ]] || return 1

[[ $2 ]] || mods_checked_as_dep+=" $_mod "

[[ -d $_moddir ]] || {
dwarn "Module '$_mod' cannot be installed without a source directory, '$_moddir'."
return 1
}

if [[ " $omit_dracutmodules " == *\ $_mod\ * ]]; then
ddebug "Module '$_mod' will not be installed, because it's in the list to be omitted!"
return 1
Expand Down
33 changes: 33 additions & 0 deletions modules.d/00_sh/module-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
# This file is part of dracut.
# SPDX-License-Identifier: GPL-2.0-or-later

# Prerequisite check(s) for module.
check() {

local shells _shell _s

# Assure a command shell.
[[ " $add_dracutmodules $force_add_dracutmodules " == @(*\ dash\ *|*\ bash\ *|*\ mksh\ *|*\ busybox\ *) ]] || {
shells='dash bash mksh busybox'
_shell=$(realpath -e /bin/sh)
_shell=${_shell##*/}
{ [[ "$_shell" ]] && check_module "$_shell"; } || {
for _s in $_shell ${shells/$_shell/}; do
check_module "$_s" || continue
# We only want to return 255 since this is a meta module.
return 255
done
dfatal "One of '$shells' must be made available."
exit 1
}
}
return 1
}

# Module dependency requirements.
depends() {

return 0

}
13 changes: 13 additions & 0 deletions modules.d/00bash/module-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@ depends() {
# Install the required file(s) and directories for the module in the initramfs.
install() {

local version ver0 ver1

inst /bin/bash

# local - (available since bash-4.4 2016-09-15) automates the
# restoration of local xtrace & other set options.
IFS=' ' read -r -a version <<< "$(command "$initdir"/bin/bash --version)"
IFS=. read -r ver0 ver1 _ <<< "${version[3]}"
if ((${ver0}${ver1} < 44)); then
dfatal "Installed Bash ${version[3]} ${version[4]}.
At least Bash 4.4 is required for proper xtrace logging
when Bash is the initramfs command shell."
exit 1
fi

# Prefer bash as default shell if no other shell is preferred.
[[ -L $initdir/bin/sh ]] || ln -sf bash "${initdir}/bin/sh"

Expand Down
1 change: 1 addition & 0 deletions modules.d/80test-root/module-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ install() {
inst_binary "${dracutbasedir}/dracut-util" "/usr/bin/dracut-util"
ln -s dracut-util "${initdir}/usr/bin/dracut-getarg"
ln -s dracut-util "${initdir}/usr/bin/dracut-getargs"
ln -s dash /bin/sh

inst_multiple -o plymouth
}
5 changes: 0 additions & 5 deletions modules.d/99base/module-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ install() {
ln -s dracut-util "${initdir}/usr/bin/dracut-getarg"
ln -s dracut-util "${initdir}/usr/bin/dracut-getargs"

if [ ! -e "${initdir}/bin/sh" ]; then
inst_multiple bash
(ln -s bash "${initdir}/bin/sh" || :)
fi

# add common users in /etc/passwd, it will be used by nfs/ssh currently
# use password for hostonly images to facilitate secure sulogin in emergency console
[[ $hostonly ]] && pwshadow='x'
Expand Down

0 comments on commit cb03a14

Please sign in to comment.