dracut.sh: use getopt to parse arguments
now we can put options and arguments anywhere we like. e.g. $ dracut test.img --forcemaster
parent
9f355169f4
commit
ffa71b4afa
233
dracut.sh
233
dracut.sh
|
@ -24,7 +24,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
# store for logging
|
# store for logging
|
||||||
dracut_args="$@"
|
dracut_args=( "$@" )
|
||||||
|
|
||||||
set -o pipefail
|
set -o pipefail
|
||||||
|
|
||||||
|
@ -36,7 +36,32 @@ usage() {
|
||||||
|
|
||||||
# 80x25 linebreak here ^
|
# 80x25 linebreak here ^
|
||||||
cat << EOF
|
cat << EOF
|
||||||
Usage: $0 [OPTION]... <initramfs> <kernel-version>
|
Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
|
||||||
|
|
||||||
|
Version: $DRACUT_VERSION
|
||||||
|
|
||||||
|
Creates initial ramdisk images for preloading modules
|
||||||
|
|
||||||
|
-h, --help Display all options
|
||||||
|
|
||||||
|
If a [LIST] has multiple arguments, then you have to put these in quotes.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
# dracut --add-drivers "module1 module2" ...
|
||||||
|
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
long_usage() {
|
||||||
|
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
|
||||||
|
if [[ -f $dracutbasedir/dracut-version.sh ]]; then
|
||||||
|
. $dracutbasedir/dracut-version.sh
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 80x25 linebreak here ^
|
||||||
|
cat << EOF
|
||||||
|
Usage: $0 [OPTION]... [<initramfs> [<kernel-version>]]
|
||||||
|
|
||||||
Version: $DRACUT_VERSION
|
Version: $DRACUT_VERSION
|
||||||
|
|
||||||
|
@ -139,8 +164,11 @@ Creates initial ramdisk images for preloading modules
|
||||||
--sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
|
--sshkey [SSHKEY] Add ssh key to initramfs (use with ssh-client module)
|
||||||
|
|
||||||
If [LIST] has multiple arguments, then you have to put these in quotes.
|
If [LIST] has multiple arguments, then you have to put these in quotes.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
# dracut --add-drivers "module1 module2" ...
|
|
||||||
|
# dracut --add-drivers "module1 module2" ...
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,9 +179,10 @@ EOF
|
||||||
# example:
|
# example:
|
||||||
# push stack 1 2 "3 4"
|
# push stack 1 2 "3 4"
|
||||||
push() {
|
push() {
|
||||||
|
local _i
|
||||||
local __stack=$1; shift
|
local __stack=$1; shift
|
||||||
for i in "$@"; do
|
for _i in "$@"; do
|
||||||
eval ${__stack}'[${#'${__stack}'[@]}]="$i"'
|
eval ${__stack}'[${#'${__stack}'[@]}]="$_i"'
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,16 +198,16 @@ push() {
|
||||||
pop() {
|
pop() {
|
||||||
local __stack=$1; shift
|
local __stack=$1; shift
|
||||||
local __resultvar=$1
|
local __resultvar=$1
|
||||||
local myresult;
|
local _value;
|
||||||
# check for empty stack
|
# check for empty stack
|
||||||
eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
|
eval '[[ ${#'${__stack}'[@]} -eq 0 ]] && return 1'
|
||||||
|
|
||||||
eval myresult='${'${__stack}'[${#'${__stack}'[@]}-1]}'
|
eval _value='${'${__stack}'[${#'${__stack}'[@]}-1]}'
|
||||||
|
|
||||||
if [[ "$__resultvar" ]]; then
|
if [[ "$__resultvar" ]]; then
|
||||||
eval $__resultvar="'$myresult'"
|
eval $__resultvar="'$_value'"
|
||||||
else
|
else
|
||||||
echo "$myresult"
|
echo "$_value"
|
||||||
fi
|
fi
|
||||||
eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
|
eval unset ${__stack}'[${#'${__stack}'[@]}-1]'
|
||||||
return 0
|
return 0
|
||||||
|
@ -202,52 +231,105 @@ read_arg() {
|
||||||
fi
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
verbosity_mod_l=0
|
verbosity_mod_l=0
|
||||||
unset kernel
|
unset kernel
|
||||||
unset outfile
|
unset outfile
|
||||||
|
|
||||||
while (($# > 0)); do
|
# Workaround -i, --include taking 2 arguments
|
||||||
case ${1%%=*} in
|
set -- "${@/--include/++include}"
|
||||||
-a|--add) push_arg add_dracutmodules_l "$@" || shift;;
|
|
||||||
--force-add) push_arg force_add_dracutmodules_l "$@" || shift;;
|
# This prevents any long argument ending with "-i"
|
||||||
--add-drivers) push_arg add_drivers_l "$@" || shift;;
|
# -i, like --opt-i but I think we can just prevent that
|
||||||
--omit-drivers) push_arg omit_drivers_l "$@" || shift;;
|
set -- "${@/%-i/++include}"
|
||||||
-m|--modules) push_arg dracutmodules_l "$@" || shift;;
|
|
||||||
-o|--omit) push_arg omit_dracutmodules_l "$@" || shift;;
|
TEMP=$(unset POSIXLY_CORRECT; getopt \
|
||||||
-d|--drivers) push_arg drivers_l "$@" || shift;;
|
-o "a:m:o:d:I:k:c:L:fvqlHhM" \
|
||||||
--filesystems) push_arg filesystems_l "$@" || shift;;
|
--long add: \
|
||||||
-I|--install) push_arg install_items_l "$@" || shift;;
|
--long force-add: \
|
||||||
--fwdir) push_arg fw_dir_l "$@" || shift;;
|
--long add-drivers: \
|
||||||
--libdirs) push_arg libdirs_l "$@" || shift;;
|
--long omit-drivers: \
|
||||||
--fscks) push_arg fscks_l "$@" || shift;;
|
--long modules: \
|
||||||
--add-fstab) push_arg add_fstab_l "$@" || shift;;
|
--long omit: \
|
||||||
--mount) push_arg fstab_lines "$@" || shift;;
|
--long drivers: \
|
||||||
|
--long filesystems: \
|
||||||
|
--long install: \
|
||||||
|
--long fwdir: \
|
||||||
|
--long libdirs: \
|
||||||
|
--long fscks: \
|
||||||
|
--long add-fstab: \
|
||||||
|
--long mount: \
|
||||||
|
--long nofscks: \
|
||||||
|
--long kmoddir: \
|
||||||
|
--long conf: \
|
||||||
|
--long confdir: \
|
||||||
|
--long tmpdir: \
|
||||||
|
--long stdlog: \
|
||||||
|
--long compress: \
|
||||||
|
--long prefix: \
|
||||||
|
--long force \
|
||||||
|
--long kernel-only \
|
||||||
|
--long no-kernel \
|
||||||
|
--long strip \
|
||||||
|
--long nostrip \
|
||||||
|
--long hardlink \
|
||||||
|
--long nohardlink \
|
||||||
|
--long noprefix \
|
||||||
|
--long mdadmconf \
|
||||||
|
--long nomdadmconf \
|
||||||
|
--long lvmconf \
|
||||||
|
--long nolvmconf \
|
||||||
|
--long debug \
|
||||||
|
--long profile \
|
||||||
|
--long sshkey: \
|
||||||
|
--long verbose \
|
||||||
|
--long quiet \
|
||||||
|
--long local \
|
||||||
|
--long hostonly \
|
||||||
|
--long no-hostonly \
|
||||||
|
--long fstab \
|
||||||
|
--long help \
|
||||||
|
--long bzip2 \
|
||||||
|
--long lzma \
|
||||||
|
--long xz \
|
||||||
|
--long no-compress \
|
||||||
|
--long gzip \
|
||||||
|
--long list-modules \
|
||||||
|
--long show-modules \
|
||||||
|
--long keep \
|
||||||
|
--long printsize \
|
||||||
|
-- "$@")
|
||||||
|
|
||||||
|
if (( $? != 0 )); then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
eval set -- "$TEMP"
|
||||||
|
|
||||||
|
while :; do
|
||||||
|
case $1 in
|
||||||
|
-a|--add) push add_dracutmodules_l "$2"; shift;;
|
||||||
|
--force-add) push force_add_dracutmodules_l "$2"; shift;;
|
||||||
|
--add-drivers) push add_drivers_l "$2"; shift;;
|
||||||
|
--omit-drivers) push omit_drivers_l "$2"; shift;;
|
||||||
|
-m|--modules) push dracutmodules_l "$2"; shift;;
|
||||||
|
-o|--omit) push omit_dracutmodules_l "$2"; shift;;
|
||||||
|
-d|--drivers) push drivers_l "$2"; shift;;
|
||||||
|
--filesystems) push filesystems_l "$2"; shift;;
|
||||||
|
-I|--install) push install_items_l "$2"; shift;;
|
||||||
|
--fwdir) push fw_dir_l "$2"; shift;;
|
||||||
|
--libdirs) push libdirs_l "$2"; shift;;
|
||||||
|
--fscks) push fscks_l "$2"; shift;;
|
||||||
|
--add-fstab) push add_fstab_l "$2"; shift;;
|
||||||
|
--mount) push fstab_lines "$2"; shift;;
|
||||||
--nofscks) nofscks_l="yes";;
|
--nofscks) nofscks_l="yes";;
|
||||||
-k|--kmoddir) read_arg drivers_dir_l "$@" || shift;;
|
-k|--kmoddir) drivers_dir_l="$2"; shift;;
|
||||||
-c|--conf) read_arg conffile "$@" || shift;;
|
-c|--conf) conffile="$2"; shift;;
|
||||||
--confdir) read_arg confdir "$@" || shift;;
|
--confdir) confdir="$2"; shift;;
|
||||||
--tmpdir) read_arg tmpdir_l "$@" || shift;;
|
--tmpdir) tmpdir_l="$2"; shift;;
|
||||||
-L|--stdlog) read_arg stdloglvl_l "$@" || shift;;
|
-L|--stdlog) stdloglvl_l="$2"; shift;;
|
||||||
--compress) read_arg compress_l "$@" || shift;;
|
--compress) compress_l="$2"; shift;;
|
||||||
--prefix) read_arg prefix_l "$@" || shift;;
|
--prefix) prefix_l="$2"; shift;;
|
||||||
-f|--force) force=yes;;
|
-f|--force) force=yes;;
|
||||||
--kernel-only) kernel_only="yes"; no_kernel="no";;
|
--kernel-only) kernel_only="yes"; no_kernel="no";;
|
||||||
--no-kernel) kernel_only="no"; no_kernel="yes";;
|
--no-kernel) kernel_only="no"; no_kernel="yes";;
|
||||||
|
@ -262,7 +344,7 @@ while (($# > 0)); do
|
||||||
--nolvmconf) lvmconf_l="no";;
|
--nolvmconf) lvmconf_l="no";;
|
||||||
--debug) debug="yes";;
|
--debug) debug="yes";;
|
||||||
--profile) profile="yes";;
|
--profile) profile="yes";;
|
||||||
--sshkey) read_arg sshkey "$@" || shift;;
|
--sshkey) sshkey="$2"; shift;;
|
||||||
-v|--verbose) ((verbosity_mod_l++));;
|
-v|--verbose) ((verbosity_mod_l++));;
|
||||||
-q|--quiet) ((verbosity_mod_l--));;
|
-q|--quiet) ((verbosity_mod_l--));;
|
||||||
-l|--local)
|
-l|--local)
|
||||||
|
@ -273,48 +355,67 @@ while (($# > 0)); do
|
||||||
-H|--hostonly) hostonly_l="yes" ;;
|
-H|--hostonly) hostonly_l="yes" ;;
|
||||||
--no-hostonly) hostonly_l="no" ;;
|
--no-hostonly) hostonly_l="no" ;;
|
||||||
--fstab) use_fstab_l="yes" ;;
|
--fstab) use_fstab_l="yes" ;;
|
||||||
-h|--help) usage; exit 1 ;;
|
-h|--help) long_usage; exit 1 ;;
|
||||||
-i|--include) push include_src "$2"
|
-i|--include) push include_src "$2"
|
||||||
push include_target "$3"
|
shift;;
|
||||||
shift 2;;
|
|
||||||
--bzip2) compress_l="bzip2";;
|
--bzip2) compress_l="bzip2";;
|
||||||
--lzma) compress_l="lzma";;
|
--lzma) compress_l="lzma";;
|
||||||
--xz) compress_l="xz";;
|
--xz) compress_l="xz";;
|
||||||
--no-compress) _no_compress_l="cat";;
|
--no-compress) _no_compress_l="cat";;
|
||||||
--gzip) compress_l="gzip";;
|
--gzip) compress_l="gzip";;
|
||||||
--list-modules)
|
--list-modules) do_list="yes";;
|
||||||
do_list="yes";
|
|
||||||
;;
|
|
||||||
-M|--show-modules)
|
-M|--show-modules)
|
||||||
show_modules_l="yes"
|
show_modules_l="yes"
|
||||||
;;
|
;;
|
||||||
--keep) keep="yes";;
|
--keep) keep="yes";;
|
||||||
--printsize) printsize="yes";;
|
--printsize) printsize="yes";;
|
||||||
-*) printf "\nUnknown option: %s\n\n" "$1" >&2; usage; exit 1;;
|
|
||||||
|
--) shift; break;;
|
||||||
|
|
||||||
|
*) # should not even reach this point
|
||||||
|
printf "\n!Unknown option: '%s'\n\n" "$1" >&2; usage; exit 1;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
# getopt cannot handle multiple arguments, so just handle "-I,--include"
|
||||||
|
# the old fashioned way
|
||||||
|
|
||||||
|
while (($# > 0)); do
|
||||||
|
case ${1%%=*} in
|
||||||
|
++include) push include_src "$2"
|
||||||
|
push include_target "$3"
|
||||||
|
shift 2;;
|
||||||
*)
|
*)
|
||||||
if ! [[ ${outfile+x} ]]; then
|
if ! [[ ${outfile+x} ]]; then
|
||||||
outfile=$1
|
outfile=$1
|
||||||
elif ! [[ ${kernel+x} ]]; then
|
elif ! [[ ${kernel+x} ]]; then
|
||||||
kernel=$1
|
kernel=$1
|
||||||
else
|
else
|
||||||
echo "Unknown argument: $1"
|
printf "\nUnknown arguments: %s\n\n" "$*" >&2
|
||||||
usage; exit 1;
|
usage; exit 1;
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
if ! [[ $kernel ]]; then
|
if ! [[ $kernel ]]; then
|
||||||
kernel=$(uname -r)
|
kernel=$(uname -r)
|
||||||
fi
|
fi
|
||||||
[[ $outfile ]] || outfile="/boot/initramfs-$kernel.img"
|
|
||||||
|
if ! [[ $outfile ]]; then
|
||||||
|
outfile="/boot/initramfs-$kernel.img"
|
||||||
|
fi
|
||||||
|
|
||||||
for i in /usr/sbin /sbin /usr/bin /bin; do
|
for i in /usr/sbin /sbin /usr/bin /bin; do
|
||||||
rl=$i
|
rl=$i
|
||||||
if [ -L "$i" ]; then
|
if [ -L "$i" ]; then
|
||||||
rl=$(readlink -f $i)
|
rl=$(readlink -f $i)
|
||||||
fi
|
fi
|
||||||
NPATH+=":$rl"
|
if [[ "$NPATH" != "*:$rl*" ]] ; then
|
||||||
|
NPATH+=":$rl"
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
export PATH="${NPATH#:}"
|
export PATH="${NPATH#:}"
|
||||||
unset NPATH
|
unset NPATH
|
||||||
|
@ -536,8 +637,12 @@ done
|
||||||
omit_drivers="${omit_drivers_corrected%|}"
|
omit_drivers="${omit_drivers_corrected%|}"
|
||||||
unset omit_drivers_corrected
|
unset omit_drivers_corrected
|
||||||
|
|
||||||
|
# prepare args for logging
|
||||||
ddebug "Executing $0 $dracut_args"
|
for ((i=0; i < ${#dracut_args[@]}; i++)); do
|
||||||
|
strstr "${dracut_args[$i]}" " " && \
|
||||||
|
dracut_args[$i]="\"${dracut_args[$i]}\""
|
||||||
|
done
|
||||||
|
ddebug "Executing: $0 ${dracut_args[@]}"
|
||||||
|
|
||||||
[[ $do_list = yes ]] && {
|
[[ $do_list = yes ]] && {
|
||||||
for mod in $dracutbasedir/modules.d/*; do
|
for mod in $dracutbasedir/modules.d/*; do
|
||||||
|
@ -573,7 +678,7 @@ if [[ ! -d "$outdir" ]]; then
|
||||||
dfatal "Can't write $outfile: Directory $outdir does not exist."
|
dfatal "Can't write $outfile: Directory $outdir does not exist."
|
||||||
exit 1
|
exit 1
|
||||||
elif [[ ! -w "$outdir" ]]; then
|
elif [[ ! -w "$outdir" ]]; then
|
||||||
dfatal "No permission to write $outdir."
|
dfatal "No permission to write to $outdir."
|
||||||
exit 1
|
exit 1
|
||||||
elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
|
elif [[ -f "$outfile" && ! -w "$outfile" ]]; then
|
||||||
dfatal "No permission to write $outfile."
|
dfatal "No permission to write $outfile."
|
||||||
|
|
Loading…
Reference in New Issue