# functions used only by dracut and dracut modules
# Copyright 2005-2009 Red Hat, Inc. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
if [[ "$EUID" = "0" ]]; then
export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,xattr,links -dfr"
export DRACUT_CP="cp --reflink=auto --sparse=auto --preserve=mode,timestamps,links -dfr"
# Check whether $1 is a function.
[[ "$(type -t "$1")" = "function" ]]
if ! [[ $dracutbasedir ]]; then
dracutbasedir=${BASH_SOURCE[0]%/*}
[[ $dracutbasedir = dracut-functions* ]] && dracutbasedir="."
[[ $dracutbasedir ]] || dracutbasedir="."
dracutbasedir="$(readlink -f $dracutbasedir)"
if ! is_func dinfo >/dev/null 2>&1; then
. "$dracutbasedir/dracut-logger.sh"
if ! [[ $initdir ]]; then
if ! [[ -d $initdir ]]; then
srcmods="/lib/modules/$kernel/"
if ! command -v kmod &>/dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
[[ $DRACUT_FIRMWARE_PATH ]] || export DRACUT_FIRMWARE_PATH="/lib/firmware/updates:/lib/firmware:/lib/firmware/$kernel"
# export standard hookdirs
hookdirs="cmdline pre-udev pre-trigger netroot "
hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
hookdirs+="pre-mount pre-pivot cleanup mount "
hookdirs+="emergency shutdown-emergency pre-shutdown shutdown "
DRACUT_LDD=${DRACUT_LDD:-ldd}
DRACUT_TESTBIN=${DRACUT_TESTBIN:-/bin/sh}
DRACUT_LDCONFIG=${DRACUT_LDCONFIG:-ldconfig}
. $dracutbasedir/dracut-functions.sh
if ! [[ $libdirs ]] ; then
if [[ "$(ldd /bin/sh)" == */lib64/* ]] &>/dev/null \
[[ -d /usr/lib64 ]] && libdirs+=" /usr/lib64"
[[ -d /usr/lib ]] && libdirs+=" /usr/lib"
libdirs+=" $(ldconfig_paths)"
# helper function for check() in module-setup.sh
# to check for required installed binaries
# issues a standardized warning message
local _module_name="${moddir##*/}"
if [[ "$1" = "-m" ]]; then
if ! find_binary "$cmd" &>/dev/null; then
dinfo "dracut module '${_module_name#[0-9][0-9]}' will not be installed, because command '$cmd' could not be found!"
local _module_name="${moddir##*/}"
if [[ "$1" = "-m" ]]; then
if find_binary "$cmd" &>/dev/null; then
if (( $_ret != 0 )); then
dinfo "$_module_name: Could not find any command of '$@'!"
dracut_need_initqueue() {
>"$initdir/lib/dracut/need-initqueue"
dracut_module_included() {
[[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
dracut_no_switch_root() {
>"$initdir/lib/dracut/no-switch-root"
echo ${dracutbasedir}/modules.d/??${1} | { read a b; echo "$a"; }
if ! [[ $DRACUT_INSTALL ]]; then
DRACUT_INSTALL=$(find_binary dracut-install)
if ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/dracut-install ]]; then
DRACUT_INSTALL=$dracutbasedir/dracut-install
elif ! [[ $DRACUT_INSTALL ]] && [[ -x $dracutbasedir/install/dracut-install ]]; then
DRACUT_INSTALL=$dracutbasedir/install/dracut-install
if ! [[ -x $DRACUT_INSTALL ]]; then
dfatal "dracut-install not found!"
if [[ $hostonly == "-h" ]]; then
if ! [[ $DRACUT_KERNEL_MODALIASES ]] || ! [[ -f "$DRACUT_KERNEL_MODALIASES" ]]; then
export DRACUT_KERNEL_MODALIASES="${DRACUT_TMPDIR}/modaliases"
$DRACUT_INSTALL ${srcmods:+--kerneldir "$srcmods"} --modalias > "$DRACUT_KERNEL_MODALIASES"
[[ $DRACUT_RESOLVE_LAZY ]] || export DRACUT_RESOLVE_DEPS=1
[[ -e ${initdir}/"$1" ]] && return 0 # already there
$DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@"
(($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} -d "$@" || :
if [[ "$1" == "-H" ]]; then
[[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
$DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
(($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
if [[ "$1" == "-H" ]]; then
[[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
[[ -e $1 ]] || return 1 # no source
$DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@"
(($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${_hostonly_install:+-H} "$@" || :
if [[ "$1" == "-H" ]]; then
[[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
$DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
(($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
$DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
(($_ret != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} -a ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
[[ $no_kernel = yes ]] && return
[[ $i == "--silent" ]] && _silent=1
${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@"
(($? != 0)) && (($_silent == 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${hostonly:+-H} ${omit_drivers:+-N "$omit_drivers"} ${srcmods:+--kerneldir "$srcmods"} -m "$@" || :
if [[ "$1" == "-H" ]]; then
[[ -e ${initdir}/"${2:-$1}" ]] && return 0 # already there
[[ -e $1 ]] || return 1 # no source
$DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@"
(($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} ${_hostonly_install:+-H} "$@" || :
$DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
(($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
$DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@"
(($? != 0)) && derror FAILED: $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$@" || :
local _helper="/run/dracut/fsck/fsck_help_$1.txt"
$DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" $_helper
(($? != 0)) && derror $DRACUT_INSTALL ${initdir:+-D "$initdir"} ${loginstall:+-L "$loginstall"} ${DRACUT_RESOLVE_DEPS:+-l} ${DRACUT_FIPS_MODE:+-f} "$2" $_helper || :
# Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
# If hosotnly mode is set to "strict", hostonly restrictions will still
# be applied, else will ignore hostonly mode and try to install all
if [[ $hostonly_mode = "strict" ]]; then
echo "$i" >> "$initdir/lib/dracut/hostonly-files"
# find symlinks linked to given library file
# Function searches for symlinks by stripping version numbers appended to
# library filename, checks if it points to the same target and finally
# prints the list of symlinks to stdout.
# rev_lib_symlinks libfoo.so.8.1
# output: libfoo.so.8 libfoo.so
# (Only if libfoo.so.8 and libfoo.so exists on host system.)
local fn="$1" orig="$(readlink -f "$1")" links=''
[[ ${fn} == *.so.* ]] || return 1
until [[ ${fn##*.} == so ]]; do
[[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
# attempt to install any programs specified in a udev rule
for _prog in $(sed -nr 's/.*PROGRAM==?"([^ "]+).*/\1/p' "$1"); do
if [ -x ${udevdir}/$_prog ]; then
elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
_bin=$(find_binary "$_prog") || {
dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
[[ $_bin ]] && inst_binary "$_bin"
for _prog in $(sed -nr 's/.*RUN[+=]=?"([^ "]+).*/\1/p' "$1"); do
if [ -x ${udevdir}/$_prog ]; then
elif [[ "${_prog/\$env\{/}" == "$_prog" ]] && [[ "${_prog}" != "/sbin/initqueue" ]]; then
_bin=$(find_binary "$_prog") || {
dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
[[ $_bin ]] && inst_binary "$_bin"
for _prog in $(sed -nr 's/.*IMPORT\{program\}==?"([^ "]+).*/\1/p' "$1"); do
if [ -x ${udevdir}/$_prog ]; then
elif [[ "${_prog/\$env\{/}" == "$_prog" ]]; then
_bin=$(find_binary "$_prog") || {
dinfo "Skipping program $_prog using in udev rule ${1##*/} as it cannot be found"
[[ $_bin ]] && dracut_install "$_bin"
# attempt to install any programs specified in a udev rule
inst_rule_group_owner() {
for i in $(sed -nr 's/.*OWNER=?"([^ "]+).*/\1/p' "$1"); do
if ! grep -Eq "^$i:" "$initdir/etc/passwd" 2>/dev/null; then
grep -E "^$i:" /etc/passwd 2>/dev/null >> "$initdir/etc/passwd"
for i in $(sed -nr 's/.*GROUP=?"([^ "]+).*/\1/p' "$1"); do
if ! grep -Eq "^$i:" "$initdir/etc/group" 2>/dev/null; then
grep -E "^$i:" /etc/group 2>/dev/null >> "$initdir/etc/group"
if grep -q -F initqueue "$1"; then
# udev rules always get installed in the same place, so
# create a function to install them to make life simpler.
local _target=/etc/udev/rules.d _rule _found
inst_dir "${udevdir}/rules.d"
if [ "${_rule#/}" = "$_rule" ]; then
for r in ${udevdir}/rules.d ${hostonly:+/etc/udev/rules.d}; do
[[ -e $r/$_rule ]] || continue
inst_rule_programs "$_found"
inst_rule_group_owner "$_found"
inst_rule_initqueue "$_found"
for r in '' $dracutbasedir/rules.d/; do
# skip rules without an absolute path
[[ "${r}$_rule" != /* ]] && continue
[[ -f ${r}$_rule ]] || continue
inst_rule_programs "$_found"
inst_rule_group_owner "$_found"
inst_rule_initqueue "$_found"
inst_simple "$_found" "$_target/${_found##*/}"
[[ $_found ]] || dinfo "Skipping udev rule: $_rule"
local _target=/etc/udev/rules.d _rule _found
inst_dir "${udevdir}/rules.d"
for _rule in ${udevdir}/rules.d/$1 ${dracutbasedir}/rules.d/$1 ; do
[[ -e $_rule ]] || continue
inst_rule_programs "$_rule"
inst_rule_group_owner "$_rule"
inst_rule_initqueue "$_rule"
if [[ -n ${hostonly} ]] ; then
for _rule in ${_target}/$1 ; do
[[ -f $_rule ]] || continue
inst_rule_programs "$_rule"
inst_rule_group_owner "$_rule"
inst_rule_initqueue "$_rule"
[[ $_found ]] || dinfo "Skipping udev rule: $_rule"
# make sure that library links are correct and up to date
for f in "$dracutsysrootdir"/etc/ld.so.conf "$dracutsysrootdir"/etc/ld.so.conf.d/*; do
[[ -f $f ]] && inst_simple "${f#$dracutsysrootdir}"
if ! ldconfig -r "$initdir" -f /etc/ld.so.conf; then
if [[ $EUID == 0 ]]; then
derror "ldconfig exited ungracefully"
derror "ldconfig might need uid=0 (root) for chroot()"
[ -z "$UDEVVERSION" ] && export UDEVVERSION=$(udevadm --version | { read v _ ; echo $v ; })
f="${initdir}/etc/udev/rules.d/$f"
while read line || [ -n "$line" ]; do
if [ "${line%%IMPORT PATH_ID}" != "$line" ]; then
if [ $UDEVVERSION -ge 174 ]; then
printf '%sIMPORT{builtin}="path_id"\n' "${line%%IMPORT PATH_ID}"
printf '%sIMPORT{program}="path_id %%p"\n' "${line%%IMPORT PATH_ID}"
elif [ "${line%%IMPORT BLKID}" != "$line" ]; then
if [ $UDEVVERSION -ge 176 ]; then
printf '%sIMPORT{builtin}="blkid"\n' "${line%%IMPORT BLKID}"
printf '%sIMPORT{program}="/sbin/blkid -o udev -p $tempnode"\n' "${line%%IMPORT BLKID}"
done < "${f}" > "${f}.new"
# install function specialized for hooks
# $1 = type of hook, $2 = hook priority (lower runs first), $3 = hook
# All hooks should be POSIX/SuS compliant, they will be sourced by init.
dfatal "Cannot install a hook ($3) that does not exist."
dfatal "Aborting initrd creation."
elif ! [[ "$hookdirs" == *$1* ]]; then
dfatal "No such hook type $1. Aborting initrd creation."