# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
+# store for logging
+dracut_args="$@"
usage() {
# 80x25 linebreak here ^
--nolvmconf Do not include local /etc/lvm/lvm.conf
-h, --help This message
--debug Output debug information of the build process
- -v, --verbose Verbose output during the build process
+ -L, --stdlog [0-6] Specify logging level (to standard error)
+ 0 - suppress any messages
+ 1 - only fatal errors
+ 2 - all errors
+ 3 - warnings
+ 4 - info (default)
+ 5 - debug info (here starts lots of output)
+ 6 - trace info (and even more)
+ -v, --verbose Verbose output during the build process (info level)
+ -q, --quiet Display only errors and fatal errors during build
-c, --conf [FILE] Specify configuration file to use.
Default: /etc/dracut.conf
--confdir [DIR] Specify configuration directory to use *.conf files
-i, --include [SOURCE] [TARGET]
Include the files in the SOURCE directory into the
Target directory in the final initramfs.
+ If SOURCE is a file, it will be installed to TARGET
+ in the final initramfs.
-I, --install [LIST] Install the space separated list of files into the
initramfs.
--gzip Compress the generated initramfs using gzip.
EOF
}
+# function push()
+# push values to a stack
+# $1 = stack variable
+# $2.. values
+# example:
+# push stack 1 2 "3 4"
+push() {
+ local __stack=$1; shift
+ for i in "$@"; do
+ eval ${__stack}'[${#'${__stack}'[@]}]="$i"'
+ done
+}
+
+# function pop()
+# pops the last value from a stack
+# assigns value to second argument variable
+# or echo to stdout, if no second argument
+# $1 = stack variable
+# $2 = optional variable to store the value
+# example:
+# pop stack val
+# val=$(pop stack)
+pop() {
+ local __stack=$1; shift
+ local __resultvar=$1
+ local myresult;
+ # check for empty stack
+ eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
+
+ eval myresult='${'${__stack}'[${#'${__stack}'[@]}-1]}'
+
+ if [[ "$__resultvar" ]]; then
+ eval $__resultvar="'$myresult'"
+ else
+ echo "$myresult"
+ fi
+ eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
+ return 0
+}
+
# Little helper function for reading args from the commandline.
# it automatically handles -a b and -a=b variants, and returns 1 if
# we need to shift $3.
fi
}
+# Little helper function for reading args from the commandline to a stack.
+# it automatically handles -a b and -a=b variants, and returns 1 if
+# we need to shift $3.
+push_arg() {
+ # $1 = arg name
+ # $2 = arg value
+ # $3 = arg parameter
+ local rematch='^[^=]*=(.*)$'
+ if [[ $2 =~ $rematch ]]; then
+ push "$1" "${BASH_REMATCH[1]}"
+ else
+ push "$1" "$3"
+ # There is no way to shift our callers args, so
+ # return 1 to indicate they should do it instead.
+ return 1
+ fi
+}
+
+kernel="unset"
+
while (($# > 0)); do
case ${1%%=*} in
- -m|--modules) read_arg dracutmodules_l "$@" || shift;;
- -o|--omit) read_arg omit_dracutmodules_l "$@" || shift;;
- -a|--add) read_arg add_dracutmodules_l "$@" || shift;;
- -d|--drivers) read_arg drivers_l "$@" || shift;;
- --add-drivers) read_arg add_drivers_l "$@" || shift;;
- --filesystems) read_arg filesystems_l "$@" || shift;;
+ -a|--add) push_arg add_dracutmodules_l "$@" || shift;;
+ --add-drivers) push_arg add_drivers_l "$@" || shift;;
+ -m|--modules) push_arg dracutmodules_l "$@" || shift;;
+ -o|--omit) push_arg omit_dracutmodules_l "$@" || shift;;
+ -d|--drivers) push_arg drivers_l "$@" || shift;;
+ --filesystems) push_arg filesystems_l "$@" || shift;;
+ -I|--install) push_arg install_items "$@" || shift;;
+ --fwdir) push_arg fw_dir_l "$@" || shift;;
-k|--kmoddir) read_arg drivers_dir_l "$@" || shift;;
-c|--conf) read_arg conffile "$@" || shift;;
--confdir) read_arg confdir "$@" || shift;;
- -I|--install) read_arg install_items "$@" || shift;;
- --fwdir) read_arg fw_dir_l "$@" || shift;;
+ -L|--stdlog) read_arg stdloglvl_l "$@" || shift;;
-f|--force) force=yes;;
--kernel-only) kernel_only="yes"; no_kernel="no";;
--no-kernel) kernel_only="no"; no_kernel="yes";;
--nolvmconf) lvmconf_l="no";;
--debug) debug="yes";;
-v|--verbose) beverbose="yes";;
+ -q|--quiet) bequiet="yes";;
-l|--local) allowlocal="yes" ;;
-H|--hostonly) hostonly_l="yes" ;;
--fstab) use_fstab_l="yes" ;;
-h|--help) usage; exit 1 ;;
- -i|--include) include_src="$2"; include_target="$3"; shift 2;;
+ -i|--include) push include_src "$2"; push include_target "$3"; shift 2;;
--bzip2) [[ $compress != cat ]] && compress="bzip2 -9";;
- --lzma) [[ $compress != cat ]] && compress="lzma -9";;
+ --lzma) [[ $compress != cat ]] && compress="lzma -9";;
+ --xz) [[ $compress != cat ]] && compress="xz -9";;
--no-compress) compress="cat";;
--gzip) if [[ $compress != cat ]]; then
type pigz > /dev/null 2>&1 && compress="pigz -9" || \
do_list="yes";
;;
-*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
- *) break ;;
+ *)
+ if ! [[ $outfile ]]; then
+ outfile=$1
+ elif [[ $kernel = "unset" ]]; then
+ kernel=$1
+ else
+ usage; exit 1;
+ fi
+ ;;
esac
shift
done
+if ! [[ $kernel ]] || [[ $kernel = "unset" ]]; then
+ kernel=$(uname -r)
+fi
+[[ $outfile ]] || outfile="/boot/initramfs-$kernel.img"
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH
set -x
}
+# For compatibility with -v behaviour. Set to info level.
+[[ $beverbose ]] && (( $stdloglvl_l < 4 )) && stdloglvl_l=4
+
+# When quiet, display only errors and fatal errors.
+[[ $bequiet ]] && (( $stdloglvl_l > 2 )) && stdloglvl_l=2
+
[[ $dracutbasedir ]] || dracutbasedir=/usr/share/dracut
[[ $allowlocal && -f "$(readlink -f ${0%/*})/dracut-functions" ]] && \
fi
# these optins add to the stuff in the config file
-[[ $add_dracutmodules_l ]] && add_dracutmodules+=" $add_dracutmodules_l"
-[[ $add_drivers_l ]] && add_drivers+=" $add_drivers_l"
+if [[ ${#add_dracutmodules_l[@]} ]]; then
+ while pop add_dracutmodules_l val; do
+ add_dracutmodules+=" $val "
+ done
+fi
+
+if [[ ${#add_drivers_l[@]} ]]; then
+ while pop add_drivers_l val; do
+ add_drivers+=" $val "
+ done
+fi
# these options override the stuff in the config file
-[[ $dracutmodules_l ]] && dracutmodules=$dracutmodules_l
-[[ $omit_dracutmodules_l ]] && omit_dracutmodules=$omit_dracutmodules_l
-[[ $drivers_l ]] && drivers=$drivers_l
-[[ $filesystems_l ]] && filesystems=$filesystems_l
+if [[ ${#dracutmodules_l[@]} ]]; then
+ dracutmodules=''
+ while pop dracutmodules_l val; do
+ dracutmodules+="$val "
+ done
+fi
+
+if [[ ${#omit_dracutmodules_l[@]} ]]; then
+ omit_dracutmodules=''
+ while pop omit_dracutmodules_l val; do
+ omit_dracutmodules+="$val "
+ done
+fi
+
+if [[ ${#drivers_l[@]} ]]; then
+ drivers=''
+ while pop drivers_l val; do
+ drivers+="$val "
+ done
+fi
+
+if [[ ${#filesystems_l[@]} ]]; then
+ filesystems=''
+ while pop filesystems_l val; do
+ filesystems+="$val "
+ done
+fi
+
+if [[ ${#fw_dir_l[@]} ]]; then
+ fw_dir=''
+ while pop fw_dir_l val; do
+ fw_dir+="$val "
+ done
+fi
+
+[[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
[[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
-[[ $fw_dir_l ]] && fw_dir=$fw_dir_l
[[ $do_strip_l ]] && do_strip=$do_strip_l
[[ $hostonly_l ]] && hostonly=$hostonly_l
[[ $use_fstab_l ]] && use_fstab=$use_fstab_l
if [[ -f $dracutbasedir/dracut-functions ]]; then
. $dracutbasedir/dracut-functions
else
- derror "Cannot find $dracutbasedir/dracut-functions."
- derror "Are you running from a git checkout?"
- derror "Try passing -l as an argument to $0"
+ echo "Cannot find $dracutbasedir/dracut-functions." >&2
+ echo "Are you running from a git checkout?" >&2
+ echo "Try passing -l as an argument to $0" >&2
exit 1
fi
dracutfunctions=$dracutbasedir/dracut-functions
export dracutfunctions
+dinfo "Executing $0 $dracut_args"
+
[[ $do_list = yes ]] && {
for mod in $dracutbasedir/modules.d/*; do
[[ -d $mod ]] || continue;
- [[ -e $mod/install || -e $mod/installkernel ]] || continue;
+ [[ -e $mod/install || -e $mod/installkernel || \
+ -e $mod/module-setup.sh ]] || continue
echo ${mod##*/??}
done
exit 0
""|auto) dracutmodules="all" ;;
esac
-[[ $2 ]] && kernel=$2 || kernel=$(uname -r)
-[[ $1 ]] && outfile=$1 || outfile="/boot/initramfs-$kernel.img"
abs_outfile=$(readlink -f "$outfile") && outfile="$abs_outfile"
srcmods="/lib/modules/$kernel/"
chmod 755 "$initdir"
export initdir hookdirs dracutbasedir dracutmodules drivers \
- fw_dir drivers_dir debug beverbose no_kernel kernel_only \
+ fw_dir drivers_dir debug no_kernel kernel_only \
add_drivers mdadmconf lvmconf filesystems \
- use_fstab libdir usrlibdir
+ use_fstab libdir usrlibdir \
+ stdloglvl sysloglvl fileloglvl kmsgloglvl logfile
if [[ $kernel_only != yes ]]; then
# Create some directory structure first
exit 1
fi
-if [[ $include_src && $include_target ]]; then
- mkdir -p "$initdir$include_target"
- cp -a -t "$initdir$include_target" "$include_src"/*
-fi
+while pop include_src src && pop include_target tgt; do
+ if [[ $src && $tgt ]]; then
+ if [[ -f $src ]]; then
+ inst $src $tgt
+ else
+ mkdir -p "${initdir}/${tgt}"
+ cp -a -t "${initdir}/${tgt}" "$src"/*
+ fi
+ fi
+done
-for item in $install_items; do
- dracut_install "$item"
+while pop install_items items; do
+ for item in $items; do
+ dracut_install "$item"
+ done
done
unset item
derror "dracut: creation of $outfile failed"
exit 1
fi
-
-[[ $beverbose = yes ]] && ls -lh "$outfile"
+dinfo "Wrote $outfile"
+dinfo $(ls -l "$outfile")
exit 0