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

View File

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

shift shift
done done


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


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

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