Bashify mkinitrd-dracut.sh, introduce read_arg.

Hack up argument processing in dracut and mkinitrd-dracut.sh to use
read_arg to flexibly process arguments.
master
Victor Lowther 2010-08-14 14:23:26 -05:00 committed by Harald Hoyer
parent 644c5241d2
commit 5bc545ed79
2 changed files with 81 additions and 97 deletions

50
dracut
View File

@ -89,23 +89,41 @@ Creates initial ramdisk images for preloading modules
"
}

# 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.
read_arg() {
# $1 = arg name
# $2 = arg value
# $3 = arg parameter
local rematch='^[^=]*=(.*)$'
if [[ $2 =~ $rematch ]]; then
read "$1" <<< "${BASH_REMATCH[1]}"
else
read "$1" <<< "$3"
# There is no way to shift our callers args, so
# return 1 to indicate they should do it instead.
return 1
fi
}

while (($# > 0)); do
case $1 in
case ${1%%=*} in
-f|--force) force=yes;;
-m|--modules) dracutmodules_l="$dracutmodules_l $2"; shift;;
-o|--omit) omit_dracutmodules_l="$omit_dracutmodules_l $2"; shift;;
-a|--add) add_dracutmodules_l="$add_dracutmodules_l $2"; shift;;
-d|--drivers) drivers_l="$drivers_l $2"; shift;;
--add-drivers) add_drivers_l="$add_drivers_l $2"; shift;;
--filesystems) filesystems_l="$filesystems_l $2"; shift;;
-k|--kmoddir) drivers_dir_l="$2"; shift;;
--fwdir) fw_dir_l="$fw_dir_l:$2"; shift;;
-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;;
-k|--kmoddir) read_arg drivers_dir_l "$@" || shift;;
--fwdir) read_arg fw_dir_l "$@" || shift;;
--kernel-only) kernel_only="yes"; no_kernel="no";;
--no-kernel) kernel_only="no"; no_kernel="yes";;
--ignore-kernel-modules) kernel_only="no"; no_kernel="yes"
ignore_kmodules="yes"
omit_dracutmodules_l+=\ kernel-modules
;;
--ignore-kernel-modules) kernel_only="no"; no_kernel="yes"
ignore_kmodules="yes"
omit_dracutmodules_l+=\ kernel-modules
;;
--strip) do_strip_l="yes";;
--nostrip) do_strip_l="no";;
--mdadmconf) mdadmconf_l="yes";;
@ -115,13 +133,13 @@ while (($# > 0)); do
-h|--help) usage; exit 1 ;;
--debug) debug="yes";;
-v|--verbose) beverbose="yes";;
-c|--conf) conffile="$2"; shift;;
--confdir) confdir="$2"; shift;;
-c|--conf) read_arg conffile "$@" || shift;;
--confdir) read_arg confdir "$@" || shift;;
-l|--local) allowlocal="yes" ;;
-H|--hostonly) hostonly_l="yes" ;;
--fstab) use_fstab_l="yes" ;;
-i|--include) include_src="$2"; include_target="$3"; shift 2;;
-I|--install) install_items="$2"; shift;;
-I|--install) read_arg install_items "$@" || shift;;
--gzip) [[ $compress != cat ]] && compress="gzip -9";;
--bzip2) [[$compress != cat ]] && compress="bzip2 -9";;
--xz) [[ $compress != cat ]] && compress="xz -9";;

View File

@ -16,74 +16,50 @@ usage () {
exit 1
}

# 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.
read_arg() {
# $1 = arg name
# $2 = arg value
# $3 = arg parameter
local rematch='^[^=]*=(.*)$'
if [[ $2 =~ $rematch ]]; then
read "$1" <<< "${BASH_REMATCH[1]}"
elif [[ $3 != -* ]]; then
# Only read next arg if it not an arg itself.
read "$1" <<< "$3"
# There is no way to shift our callers args, so
# return 1 to indicate they should do it instead.
return 1
fi
}

while [ $# -gt 0 ]; do
case $1 in
--with-usb*)
if [ "$1" != "${1##--with-usb=}" ]; then
usbmodule=${1##--with-usb=}
else
usbmodule="usb-storage"
fi
basicmodules="$basicmodules $usbmodule"
unset usbmodule
;;
--with-avail*)
if [ "$1" != "${1##--with-avail=}" ]; then
modname=${1##--with-avail=}
else
modname=$2
shift
fi

basicmodules="$basicmodules $modname"
;;
--with*)
if [ "$1" != "${1##--with=}" ]; then
modname=${1##--with=}
else
modname=$2
shift
fi

basicmodules="$basicmodules $modname"
;;
while (($# > 0)); do
case ${1%%=*} in
--with-usb) read_arg usbmodule "$@" || shift
basicmodules="$basicmodules ${usbmodule:-usb-storage}"
unset usbmodule;;
--with-avail) read_arg modname "$@" || shift
basicmodules="$basicmodules $modname";;
--with) read_arg modname "$@" || shift
basicmodules="$basicmodules $modname";;
--version)
echo "mkinitrd: dracut compatibility wrapper"
exit 0
;;
-v|--verbose)
dracut_args="${dracut_args} -v"
;;
-f)
dracut_args="${dracut_args} -f"
;;
--preload*)
if [ "$1" != "${1##--preload=}" ]; then
modname=${1##--preload=}
else
modname=$2
shift
fi
basicmodules="$basicmodules $modname"
;;
--image-version)
img_vers=yes
;;
--rootfs*)
if [ "$1" != "${1##--rootfs=}" ]; then
rootfs="${1##--rootfs=}"
else
rootfs="$2"
shift
fi
dracut_args="${dracut_args} --filesystems $rootfs"
;;
--builtin*) ;;
exit 0;;
-v|--verbose) dracut_args="${dracut_args} -v";;
-f) dracut_args="${dracut_args} -f";;
--preload) read_args modname "$@" || shift
basicmodules="$basicmodules $modname";;
--image-version) img_vers=yes;;
--rootfs) read_args rootfs "$@" || shift
dracut_args="${dracut_args} --filesystems $rootfs";;
--nocompress) dracut_args="$dracut_args --no-compress";;
--help) usage -n;;
--builtin) ;;
--without*) ;;
--without-usb) ;;
--fstab*) ;;
--nocompress) dracut_args="$dracut_args --no-compress";;
--ifneeded) ;;
--omit-scsi-modules) ;;
--omit-ide-modules) ;;
@ -103,32 +79,22 @@ while [ $# -gt 0 ]; do
--looppath*) ;;
--dsdt*) ;;
--bootchart) ;;
--help)
usage -n
;;
*)
if [ -z "$target" ]; then
target=$1
elif [ -z "$kernel" ]; then
kernel=$1
*) if [[ ! $target ]]; then
target=$1
elif [[ ! $kernel ]]; then
kernel=$1
else
usage
fi
;;
usage
fi;;
esac

shift
done

if [ -z "$target" -o -z "$kernel" ]; then
usage
[[ $target && $kernel ]] || usage
[[ $img_vers ]] && target="$target-$kernel"
fi

if [ -n "$img_vers" ]; then
target="$target-$kernel"
fi

if [ -n "$basicmodules" ]; then
if [[ $basicmodules ]]; then
dracut -H $dracut_args --add-drivers "$basicmodules" "$target" "$kernel"
else
dracut -H $dracut_args "$target" "$kernel"