[arch-projects] [MKINITCPIO][PATCH 1/4] Allow mkinitcpio to generate all preset
Add options -a and --all which ask to mkinitcpio to generate all presets registered in /etc/mkinitcpio.d. This is usefull, by example, when you update mkinitcpio.conf and you want to rebuild all initramfs for your kernels. Signed-off-by: Sébastien Luttringer <seblu@seblu.net> --- bash-completion | 2 +- man/mkinitcpio.8.txt | 4 ++ mkinitcpio | 106 +++++++++++++++++++++++++++++---------------------- 3 files changed, 65 insertions(+), 47 deletions(-) diff --git a/bash-completion b/bash-completion index 0108863..b9dc888 100644 --- a/bash-completion +++ b/bash-completion @@ -62,7 +62,7 @@ _files_from_dirs() { _mkinitcpio() { local action cur prev opts - opts=(-A --addhooks -c --config -g --generate -H --hookhelp -h --help -k --kernel + opts=(-A --addhooks -a --all -c --config -g --generate -H --hookhelp -h --help -k --kernel -L --listhooks -M --automods -n --nocolor -p --preset -r --moduleroot -S --skiphooks -s --save -t --builddir -V --version -v --verbose -z --compress) diff --git a/man/mkinitcpio.8.txt b/man/mkinitcpio.8.txt index 2b0f524..cd7dd42 100644 --- a/man/mkinitcpio.8.txt +++ b/man/mkinitcpio.8.txt @@ -29,6 +29,10 @@ Options after all other hooks from the config file. Multiple hooks should be comma-separated. This option can be specified multiple times. +*-a, \--all*:: + Build all initramfs image(s) according to all specified presets in + /etc/mkinitcpio.d. See '-p' option for more details about presets. + *-c, \--config* 'config':: Use 'config' file to generate the ramdisk. Default: /etc/mkinitcpio.conf diff --git a/mkinitcpio b/mkinitcpio index 9802fd5..e932602 100755 --- a/mkinitcpio +++ b/mkinitcpio @@ -5,7 +5,7 @@ declare -r version=%VERSION% -shopt -s extglob +shopt -s extglob nullglob ### globals within mkinitcpio, but not intended to be used by hooks @@ -39,6 +39,7 @@ usage: ${0##*/} [options] Options: -A, --addhooks <hooks> Add specified hooks, comma separated, to image + -a, --all Build all presets in $_d_presets -c, --config <config> Use alternate config file. (default: /etc/mkinitcpio.conf) -g, --generate <path> Generate cpio image and write to specified path -H, --hookhelp <hookname> Display help for given hook and exit @@ -237,57 +238,65 @@ build_image() { } process_preset() { - local preset=$1 preset_image= preset_options= + local preset= preset_image= preset_options= ret=0 local -a preset_mkopts preset_cmd - # allow path to preset file, else resolve it in $_d_presets - if [[ $preset != */* ]]; then - printf -v preset '%s/%s.preset' "$_d_presets" "$preset" - fi + for preset; do + # here we need to use subshell to be sure sourcing files doesn't taint + # our shell context accross call + ( + # allow path to preset file, else resolve it in $_d_presets + if [[ $preset != */* ]]; then + printf -v preset '%s/%s.preset' "$_d_presets" "$preset" + fi - . "$preset" || die "Preset not found: \`%s'" "$preset" + . "$preset" || die "Preset not found: \`%s'" "$preset" - # Use -m and -v options specified earlier - (( _optquiet )) || preset_mkopts+=(-v) - (( _optcolor )) || preset_mkopts+=(-n) + # Use -m and -v options specified earlier + (( _optquiet )) || preset_mkopts+=(-v) + (( _optcolor )) || preset_mkopts+=(-n) - ret=0 - for p in "${PRESETS[@]}"; do - msg "Building image from preset: '$p'" - preset_cmd=("${preset_mkopts[@]}") + for p in "${PRESETS[@]}"; do + msg "Building image from preset '$p' in '$preset'" + preset_cmd=("${preset_mkopts[@]}") - preset_kver=${p}_kver - if [[ ${!preset_kver:-$ALL_kver} ]]; then - preset_cmd+=(-k "${!preset_kver:-$ALL_kver}") - else - warning "No kernel version specified. Skipping image \`%s'" "$p" - continue - fi + preset_kver=${p}_kver + if [[ ${!preset_kver:-$ALL_kver} ]]; then + preset_cmd+=(-k "${!preset_kver:-$ALL_kver}") + else + warning "No kernel version specified. Skipping image \`%s'" "$p" + continue + fi - preset_config=${p}_config - if [[ ${!preset_config:-$ALL_config} ]]; then - preset_cmd+=(-c "${!preset_config:-$ALL_config}") - else - warning "No configuration file specified. Skipping image \`%s'" "$p" - continue - fi + preset_config=${p}_config + if [[ ${!preset_config:-$ALL_config} ]]; then + preset_cmd+=(-c "${!preset_config:-$ALL_config}") + else + warning "No configuration file specified. Skipping image \`%s'" "$p" + continue + fi - preset_image=${p}_image - if [[ ${!preset_image} ]]; then - preset_cmd+=(-g "${!preset_image}") - else - warning "No image file specified. Skipping image \`%s'" "$p" - continue - fi + preset_image=${p}_image + if [[ ${!preset_image} ]]; then + preset_cmd+=(-g "${!preset_image}") + else + warning "No image file specified. Skipping image \`%s'" "$p" + continue + fi - preset_options=${p}_options - if [[ ${!preset_options} ]]; then - preset_cmd+=(${!preset_options}) # intentional word splitting - fi + preset_options=${p}_options + if [[ ${!preset_options} ]]; then + preset_cmd+=(${!preset_options}) # intentional word splitting + fi + + msg2 "${preset_cmd[*]}" + "$0" "${preset_cmd[@]}" + (( $? )) && ret=1 + done - msg2 "${preset_cmd[*]}" - "$0" "${preset_cmd[@]}" - (( $? )) && ret=1 + exit $ret + ) + (( $? )) && ret=1 done exit $ret @@ -339,8 +348,8 @@ install_modules() { trap 'cleanup 130' INT trap 'cleanup 143' TERM -_opt_short='A:c:g:H:hk:nLMp:r:S:st:Vvz:' -_opt_long=('add:' 'addhooks:' 'config:' 'generate:' 'hookhelp:' 'help' +_opt_short='aA:c:g:H:hk:nLMp:r:S:st:Vvz:' +_opt_long=('add:' 'addhooks:' 'all' 'config:' 'generate:' 'hookhelp:' 'help' 'kernel:' 'listhooks' 'automods' 'moduleroot:' 'nocolor' 'preset:' 'skiphooks:' 'save' 'builddir:' 'version' 'verbose' 'compress:') @@ -356,6 +365,11 @@ while :; do IFS=, read -r -a add <<< "$1" _optaddhooks+=("${add[@]}") unset add ;; + -a|--all) + _optpreset=("$_d_presets"/*.preset) + if ! (( ${#_optpreset} )); then + die "No preset files available in \`%s'." "$_d_presets" + fi ;; -c|--config) shift _f_config=$1 ;; @@ -378,7 +392,7 @@ while :; do cleanup 0 ;; -p|--preset) shift - _optpreset=$1 ;; + _optpreset=("$1") ;; -n|--nocolor) _optcolor=0 ;; -v|--verbose) @@ -424,7 +438,7 @@ fi [[ -e /dev/fd ]] || die "/dev must be mounted!" # use preset $_optpreset (exits after processing) -[[ $_optpreset ]] && process_preset "$_optpreset" +(( ${#_optpreset} )) && process_preset "${_optpreset[@]}" KERNELVERSION=$(resolve_kernver "$_optkver") || cleanup 1 _d_kmoduledir=$_optmoduleroot/lib/modules/$KERNELVERSION -- Sébastien "Seblu" Luttringer
Signed-off-by: Sébastien Luttringer <seblu@seblu.net> --- mkinitcpio | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mkinitcpio b/mkinitcpio index e932602..fb07001 100755 --- a/mkinitcpio +++ b/mkinitcpio @@ -40,7 +40,7 @@ usage: ${0##*/} [options] Options: -A, --addhooks <hooks> Add specified hooks, comma separated, to image -a, --all Build all presets in $_d_presets - -c, --config <config> Use alternate config file. (default: /etc/mkinitcpio.conf) + -c, --config <config> Use alternate config file. (default: $_f_config) -g, --generate <path> Generate cpio image and write to specified path -H, --hookhelp <hookname> Display help for given hook and exit -h, --help Display this message and exit @@ -48,7 +48,7 @@ usage: ${0##*/} [options] -L, --listhooks List all available hooks -M, --automods Display modules found via autodetection -n, --nocolor Disable colorized output messages - -p, --preset <file> Build specified preset from /etc/mkinitcpio.d + -p, --preset <file> Build specified preset from $_d_presets -r, --moduleroot <dir> Root directory for modules (default: /) -S, --skiphooks <hooks> Skip specified hooks, comma-separated, during build -s, --save Save build directory. (default: no) -- Sébastien "Seblu" Luttringer
On Mon, Feb 04, 2013 at 06:44:00PM +0100, Sébastien Luttringer wrote:
Signed-off-by: Sébastien Luttringer <seblu@seblu.net> --- mkinitcpio | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/mkinitcpio b/mkinitcpio index e932602..fb07001 100755 --- a/mkinitcpio +++ b/mkinitcpio @@ -40,7 +40,7 @@ usage: ${0##*/} [options] Options: -A, --addhooks <hooks> Add specified hooks, comma separated, to image -a, --all Build all presets in $_d_presets - -c, --config <config> Use alternate config file. (default: /etc/mkinitcpio.conf) + -c, --config <config> Use alternate config file. (default: $_f_config)
NAK. This only has an effect on in-tree git builds, and I expect people hacking on mkinitcpio to be smart enough to figure this out. Worse, it makes user options masquerade as defaults if you were to do something like this: $ mkinitcpio -c /dev/null -h | grep -- --config -c, --config <config> Use alternate config file. (default: /dev/null)
-g, --generate <path> Generate cpio image and write to specified path -H, --hookhelp <hookname> Display help for given hook and exit -h, --help Display this message and exit @@ -48,7 +48,7 @@ usage: ${0##*/} [options] -L, --listhooks List all available hooks -M, --automods Display modules found via autodetection -n, --nocolor Disable colorized output messages - -p, --preset <file> Build specified preset from /etc/mkinitcpio.d + -p, --preset <file> Build specified preset from $_d_presets
Same here. Note that both of these cases are going to be a simple relative path (either "mkinitcpio.conf" or "mkinitcpio.d") which isn't very useful if you happen to invoke mkinitcpio from anywhere else besides the same directory in which it resides. Please, let's just skip this.
-r, --moduleroot <dir> Root directory for modules (default: /) -S, --skiphooks <hooks> Skip specified hooks, comma-separated, during build -s, --save Save build directory. (default: no) -- Sébastien "Seblu" Luttringer
Add .disable to example file in mkinitcpio.d to avoid this file to be automatically builded by -p and -a option of mkinitpcio Signed-off-by: Sébastien Luttringer <seblu@seblu.net> --- mkinitcpio.d/example.preset | 25 ------------------------- mkinitcpio.d/example.preset.disable | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 25 deletions(-) delete mode 100644 mkinitcpio.d/example.preset create mode 100644 mkinitcpio.d/example.preset.disable diff --git a/mkinitcpio.d/example.preset b/mkinitcpio.d/example.preset deleted file mode 100644 index a0479d2..0000000 --- a/mkinitcpio.d/example.preset +++ /dev/null @@ -1,25 +0,0 @@ -# Example mkinitcpio preset file - -# preset names -PRESETS=('default' 'fallback') - -# ALL_kver is used if presetname_kver is not set -# The kver can be specified as an explicit kernel version or -# as the path to an kernel image. -ALL_kver='/boot/vmlinuz-linux' -ALL_config='/etc/mkinitcpio.conf' - -# presetname_kver - the kernel version (omit if ALL_kver should be used) -# presetname_config - the configuration file (omit if ALL_config should be used) -# presetname_image - the filename of generated image -# presetname_options - any extra options - -#default_kver="3.0-ARCH" -#default_config="/etc/mkinitcpio.conf" -default_image="/tmp/initramfs-linux.img" -default_options="" - -#fallback_kver="3.0-ARCH" -#fallback_config="/etc/mkinitcpio.conf" -fallback_image="/tmp/initramfs-linux-fallback.img" -fallback_options="-S autodetect" diff --git a/mkinitcpio.d/example.preset.disable b/mkinitcpio.d/example.preset.disable new file mode 100644 index 0000000..a0479d2 --- /dev/null +++ b/mkinitcpio.d/example.preset.disable @@ -0,0 +1,25 @@ +# Example mkinitcpio preset file + +# preset names +PRESETS=('default' 'fallback') + +# ALL_kver is used if presetname_kver is not set +# The kver can be specified as an explicit kernel version or +# as the path to an kernel image. +ALL_kver='/boot/vmlinuz-linux' +ALL_config='/etc/mkinitcpio.conf' + +# presetname_kver - the kernel version (omit if ALL_kver should be used) +# presetname_config - the configuration file (omit if ALL_config should be used) +# presetname_image - the filename of generated image +# presetname_options - any extra options + +#default_kver="3.0-ARCH" +#default_config="/etc/mkinitcpio.conf" +default_image="/tmp/initramfs-linux.img" +default_options="" + +#fallback_kver="3.0-ARCH" +#fallback_config="/etc/mkinitcpio.conf" +fallback_image="/tmp/initramfs-linux-fallback.img" +fallback_options="-S autodetect" -- Sébastien "Seblu" Luttringer
On Mon, Feb 04, 2013 at 06:44:01PM +0100, Sébastien Luttringer wrote:
Add .disable to example file in mkinitcpio.d to avoid this file to be automatically builded by -p and -a option of mkinitpcio
Signed-off-by: Sébastien Luttringer <seblu@seblu.net> --- mkinitcpio.d/example.preset | 25 ------------------------- mkinitcpio.d/example.preset.disable | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 25 deletions(-) delete mode 100644 mkinitcpio.d/example.preset create mode 100644 mkinitcpio.d/example.preset.disable
You can use git format-patch -M to make this a lot cleaner, as git will just detect the rename. I'm almost tempted to say that we should just ship this as documentation in /usr/share/mkinitcpio or embed it in a manpage somewhere. Undecided, but I dislike the name mangling.
diff --git a/mkinitcpio.d/example.preset b/mkinitcpio.d/example.preset deleted file mode 100644 index a0479d2..0000000 --- a/mkinitcpio.d/example.preset +++ /dev/null @@ -1,25 +0,0 @@ -# Example mkinitcpio preset file - -# preset names -PRESETS=('default' 'fallback') - -# ALL_kver is used if presetname_kver is not set -# The kver can be specified as an explicit kernel version or -# as the path to an kernel image. -ALL_kver='/boot/vmlinuz-linux' -ALL_config='/etc/mkinitcpio.conf' - -# presetname_kver - the kernel version (omit if ALL_kver should be used) -# presetname_config - the configuration file (omit if ALL_config should be used) -# presetname_image - the filename of generated image -# presetname_options - any extra options - -#default_kver="3.0-ARCH" -#default_config="/etc/mkinitcpio.conf" -default_image="/tmp/initramfs-linux.img" -default_options="" - -#fallback_kver="3.0-ARCH" -#fallback_config="/etc/mkinitcpio.conf" -fallback_image="/tmp/initramfs-linux-fallback.img" -fallback_options="-S autodetect" diff --git a/mkinitcpio.d/example.preset.disable b/mkinitcpio.d/example.preset.disable new file mode 100644 index 0000000..a0479d2 --- /dev/null +++ b/mkinitcpio.d/example.preset.disable @@ -0,0 +1,25 @@ +# Example mkinitcpio preset file + +# preset names +PRESETS=('default' 'fallback') + +# ALL_kver is used if presetname_kver is not set +# The kver can be specified as an explicit kernel version or +# as the path to an kernel image. +ALL_kver='/boot/vmlinuz-linux' +ALL_config='/etc/mkinitcpio.conf' + +# presetname_kver - the kernel version (omit if ALL_kver should be used) +# presetname_config - the configuration file (omit if ALL_config should be used) +# presetname_image - the filename of generated image +# presetname_options - any extra options + +#default_kver="3.0-ARCH" +#default_config="/etc/mkinitcpio.conf" +default_image="/tmp/initramfs-linux.img" +default_options="" + +#fallback_kver="3.0-ARCH" +#fallback_config="/etc/mkinitcpio.conf" +fallback_image="/tmp/initramfs-linux-fallback.img" +fallback_options="-S autodetect" -- Sébastien "Seblu" Luttringer
Colors initialization was called after arg parsing. This disallow error messages and options called directly from arg parser to use colors. By example, call `mkinitcpio -k toto' or `mkinitcpio -L'. This patch initialize colors when terminal is able to support it but disable it if users don't wants it (with -n). Signed-off-by: Sébastien Luttringer <seblu@seblu.net> --- functions | 4 ++++ mkinitcpio | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/functions b/functions index 4df91bc..d58a71a 100644 --- a/functions +++ b/functions @@ -693,4 +693,8 @@ try_enable_color() { fi } +disable_colors() { + unset _color_none _color_bold _color_blue _color_green _color_red _color_yellow +} + # vim: set ft=sh ts=4 sw=4 et: diff --git a/mkinitcpio b/mkinitcpio index fb07001..dca3a06 100755 --- a/mkinitcpio +++ b/mkinitcpio @@ -357,6 +357,10 @@ parseopts "$_opt_short" "${_opt_long[@]}" -- "$@" || exit 1 set -- "${OPTRET[@]}" unset _opt_short _opt_long OPTRET +if [[ -t 1 ]]; then + try_enable_color +fi + while :; do case $1 in # --add remains for backwards compat @@ -394,7 +398,8 @@ while :; do shift _optpreset=("$1") ;; -n|--nocolor) - _optcolor=0 ;; + _optcolor=0 + disable_colors ;; -v|--verbose) _optquiet=0 ;; -S|--skiphooks) @@ -427,10 +432,6 @@ while :; do shift done -if [[ -t 1 ]] && (( _optcolor )); then - try_enable_color -fi - # insist that /proc and /dev be mounted (important for chroots) # NOTE: avoid using mountpoint for this -- look for the paths that we actually # use in mkinitcpio. Avoids issues like FS#26344. -- Sébastien "Seblu" Luttringer
On Mon, Feb 04, 2013 at 06:44:02PM +0100, Sébastien Luttringer wrote:
Colors initialization was called after arg parsing. This disallow error messages and options called directly from arg parser to use colors. By example, call `mkinitcpio -k toto' or `mkinitcpio -L'.
This patch initialize colors when terminal is able to support it but disable it if users don't wants it (with -n).
Signed-off-by: Sébastien Luttringer <seblu@seblu.net> --- functions | 4 ++++ mkinitcpio | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/functions b/functions index 4df91bc..d58a71a 100644 --- a/functions +++ b/functions @@ -693,4 +693,8 @@ try_enable_color() { fi }
+disable_colors() { + unset _color_none _color_bold _color_blue _color_green _color_red _color_yellow
You can inline this and simply call: unset "${!_color_@}" The joy of having some semblance of namespacing for variables...
+} + # vim: set ft=sh ts=4 sw=4 et: diff --git a/mkinitcpio b/mkinitcpio index fb07001..dca3a06 100755 --- a/mkinitcpio +++ b/mkinitcpio @@ -357,6 +357,10 @@ parseopts "$_opt_short" "${_opt_long[@]}" -- "$@" || exit 1 set -- "${OPTRET[@]}" unset _opt_short _opt_long OPTRET
+if [[ -t 1 ]]; then + try_enable_color +fi + while :; do case $1 in # --add remains for backwards compat @@ -394,7 +398,8 @@ while :; do shift _optpreset=("$1") ;; -n|--nocolor) - _optcolor=0 ;; + _optcolor=0 + disable_colors ;; -v|--verbose) _optquiet=0 ;; -S|--skiphooks) @@ -427,10 +432,6 @@ while :; do shift done
-if [[ -t 1 ]] && (( _optcolor )); then - try_enable_color -fi - # insist that /proc and /dev be mounted (important for chroots) # NOTE: avoid using mountpoint for this -- look for the paths that we actually # use in mkinitcpio. Avoids issues like FS#26344. -- Sébastien "Seblu" Luttringer
On Mon, Feb 04, 2013 at 06:43:59PM +0100, Sébastien Luttringer wrote:
Add options -a and --all which ask to mkinitcpio to generate all presets registered in /etc/mkinitcpio.d.
This is usefull, by example, when you update mkinitcpio.conf and you want to rebuild all initramfs for your kernels.
Signed-off-by: Sébastien Luttringer <seblu@seblu.net> ---
Yay! I love code reviews! This patch is a mess, but I like the idea. Seems to me that it could be far cleaner if you did something in main() like: if (( ${#_optpreset[*]} )); then map process_preset "${_optpreset[@]}" exit fi Just fix process_preset so that it doesn't ever exit, only return $ret.
bash-completion | 2 +- man/mkinitcpio.8.txt | 4 ++ mkinitcpio | 106 +++++++++++++++++++++++++++++---------------------- 3 files changed, 65 insertions(+), 47 deletions(-)
diff --git a/bash-completion b/bash-completion index 0108863..b9dc888 100644 --- a/bash-completion +++ b/bash-completion @@ -62,7 +62,7 @@ _files_from_dirs() {
_mkinitcpio() { local action cur prev opts - opts=(-A --addhooks -c --config -g --generate -H --hookhelp -h --help -k --kernel + opts=(-A --addhooks -a --all -c --config -g --generate -H --hookhelp -h --help -k --kernel
Would prefer using -P, --allpresets over -a. Think "lots of -p". --all is especially vague, since presets are a feature and not the centerpiece of mkinitcpio.
-L --listhooks -M --automods -n --nocolor -p --preset -r --moduleroot -S --skiphooks -s --save -t --builddir -V --version -v --verbose -z --compress)
diff --git a/man/mkinitcpio.8.txt b/man/mkinitcpio.8.txt index 2b0f524..cd7dd42 100644 --- a/man/mkinitcpio.8.txt +++ b/man/mkinitcpio.8.txt @@ -29,6 +29,10 @@ Options after all other hooks from the config file. Multiple hooks should be comma-separated. This option can be specified multiple times.
+*-a, \--all*:: + Build all initramfs image(s) according to all specified presets in
Just "images". We're definitely interested in the plural here since we expect multiple presets, and therefore multiple images.
+ /etc/mkinitcpio.d. See '-p' option for more details about presets.
See _the_ '-p' option
+ *-c, \--config* 'config':: Use 'config' file to generate the ramdisk. Default: /etc/mkinitcpio.conf
diff --git a/mkinitcpio b/mkinitcpio index 9802fd5..e932602 100755 --- a/mkinitcpio +++ b/mkinitcpio @@ -5,7 +5,7 @@
declare -r version=%VERSION%
-shopt -s extglob +shopt -s extglob nullglob
NAK. nullglob should never be enabled globally. Find out where you need it and find a way to scope it or work around it. I suspect I've located this, but I'll defer to you, the author.
### globals within mkinitcpio, but not intended to be used by hooks
@@ -39,6 +39,7 @@ usage: ${0##*/} [options]
Options: -A, --addhooks <hooks> Add specified hooks, comma separated, to image + -a, --all Build all presets in $_d_presets -c, --config <config> Use alternate config file. (default: /etc/mkinitcpio.conf) -g, --generate <path> Generate cpio image and write to specified path -H, --hookhelp <hookname> Display help for given hook and exit @@ -237,57 +238,65 @@ build_image() { }
process_preset() { - local preset=$1 preset_image= preset_options= + local preset= preset_image= preset_options= ret=0 local -a preset_mkopts preset_cmd
- # allow path to preset file, else resolve it in $_d_presets - if [[ $preset != */* ]]; then - printf -v preset '%s/%s.preset' "$_d_presets" "$preset" - fi + for preset; do + # here we need to use subshell to be sure sourcing files doesn't taint + # our shell context accross call + ( + # allow path to preset file, else resolve it in $_d_presets + if [[ $preset != */* ]]; then + printf -v preset '%s/%s.preset' "$_d_presets" "$preset" + fi
- . "$preset" || die "Preset not found: \`%s'" "$preset" + . "$preset" || die "Preset not found: \`%s'" "$preset"
- # Use -m and -v options specified earlier - (( _optquiet )) || preset_mkopts+=(-v) - (( _optcolor )) || preset_mkopts+=(-n) + # Use -m and -v options specified earlier + (( _optquiet )) || preset_mkopts+=(-v) + (( _optcolor )) || preset_mkopts+=(-n)
- ret=0 - for p in "${PRESETS[@]}"; do - msg "Building image from preset: '$p'" - preset_cmd=("${preset_mkopts[@]}") + for p in "${PRESETS[@]}"; do + msg "Building image from preset '$p' in '$preset'" + preset_cmd=("${preset_mkopts[@]}")
- preset_kver=${p}_kver - if [[ ${!preset_kver:-$ALL_kver} ]]; then - preset_cmd+=(-k "${!preset_kver:-$ALL_kver}") - else - warning "No kernel version specified. Skipping image \`%s'" "$p" - continue - fi + preset_kver=${p}_kver + if [[ ${!preset_kver:-$ALL_kver} ]]; then + preset_cmd+=(-k "${!preset_kver:-$ALL_kver}") + else + warning "No kernel version specified. Skipping image \`%s'" "$p" + continue + fi
- preset_config=${p}_config - if [[ ${!preset_config:-$ALL_config} ]]; then - preset_cmd+=(-c "${!preset_config:-$ALL_config}") - else - warning "No configuration file specified. Skipping image \`%s'" "$p" - continue - fi + preset_config=${p}_config + if [[ ${!preset_config:-$ALL_config} ]]; then + preset_cmd+=(-c "${!preset_config:-$ALL_config}") + else + warning "No configuration file specified. Skipping image \`%s'" "$p" + continue + fi
- preset_image=${p}_image - if [[ ${!preset_image} ]]; then - preset_cmd+=(-g "${!preset_image}") - else - warning "No image file specified. Skipping image \`%s'" "$p" - continue - fi + preset_image=${p}_image + if [[ ${!preset_image} ]]; then + preset_cmd+=(-g "${!preset_image}") + else + warning "No image file specified. Skipping image \`%s'" "$p" + continue + fi
- preset_options=${p}_options - if [[ ${!preset_options} ]]; then - preset_cmd+=(${!preset_options}) # intentional word splitting - fi + preset_options=${p}_options + if [[ ${!preset_options} ]]; then + preset_cmd+=(${!preset_options}) # intentional word splitting + fi + + msg2 "${preset_cmd[*]}" + "$0" "${preset_cmd[@]}" + (( $? )) && ret=1 + done
- msg2 "${preset_cmd[*]}" - "$0" "${preset_cmd[@]}" - (( $? )) && ret=1 + exit $ret + ) + (( $? )) && ret=1 done
exit $ret @@ -339,8 +348,8 @@ install_modules() { trap 'cleanup 130' INT trap 'cleanup 143' TERM
-_opt_short='A:c:g:H:hk:nLMp:r:S:st:Vvz:' -_opt_long=('add:' 'addhooks:' 'config:' 'generate:' 'hookhelp:' 'help' +_opt_short='aA:c:g:H:hk:nLMp:r:S:st:Vvz:' +_opt_long=('add:' 'addhooks:' 'all' 'config:' 'generate:' 'hookhelp:' 'help' 'kernel:' 'listhooks' 'automods' 'moduleroot:' 'nocolor' 'preset:' 'skiphooks:' 'save' 'builddir:' 'version' 'verbose' 'compress:')
@@ -356,6 +365,11 @@ while :; do IFS=, read -r -a add <<< "$1" _optaddhooks+=("${add[@]}") unset add ;; + -a|--all) + _optpreset=("$_d_presets"/*.preset)
This variable is treated as an array now, but the global def is still a string. Please fix. It leads to your hacky workaround which I'll point out below.
+ if ! (( ${#_optpreset} )); then
style nit, please fix: if (( expr == 0 )); then But rather, I suspect this is where you wanted nullglob. Just check for existance: if [[ ! -e ${_optpreset[0]} ]]; then die ....; fi
+ die "No preset files available in \`%s'." "$_d_presets" + fi ;; -c|--config) shift _f_config=$1 ;; @@ -378,7 +392,7 @@ while :; do cleanup 0 ;; -p|--preset) shift - _optpreset=$1 ;; + _optpreset=("$1") ;; -n|--nocolor) _optcolor=0 ;; -v|--verbose) @@ -424,7 +438,7 @@ fi [[ -e /dev/fd ]] || die "/dev must be mounted!"
# use preset $_optpreset (exits after processing) -[[ $_optpreset ]] && process_preset "$_optpreset" +(( ${#_optpreset} )) && process_preset "${_optpreset[@]}"
You surely wanted to use (( ${#_optpreset[*]} )) here, but you can't, since you didn't alter the definition of _optpreset earlier. $ v= $ echo ${#v[*]} 1 $ v=() $ echo ${#v[*]} 0
KERNELVERSION=$(resolve_kernver "$_optkver") || cleanup 1 _d_kmoduledir=$_optmoduleroot/lib/modules/$KERNELVERSION -- Sébastien "Seblu" Luttringer
As a logical extension, allow the -p option to be specified multiple times in order to process several presets at once. --- This is more along the lines of what I had in mind. Only lightly tested. bash-completion | 2 +- man/mkinitcpio.8.txt | 8 ++++++-- mkinitcpio | 28 +++++++++++++++++++--------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/bash-completion b/bash-completion index 559b2f3..e7c1db0 100644 --- a/bash-completion +++ b/bash-completion @@ -63,7 +63,7 @@ _files_from_dirs() { _mkinitcpio() { local action cur prev opts opts=(-A --addhooks -c --config -g --generate -H --hookhelp -h --help -k --kernel - -L --listhooks -M --automods -n --nocolor -p --preset -r --moduleroot + -L --listhooks -M --automods -n --nocolor -P --allpresets -p --preset -r --moduleroot -S --skiphooks -s --save -t --builddir -V --version -v --verbose -z --compress) _get_comp_words_by_ref cur prev diff --git a/man/mkinitcpio.8.txt b/man/mkinitcpio.8.txt index 2b0f524..56ac571 100644 --- a/man/mkinitcpio.8.txt +++ b/man/mkinitcpio.8.txt @@ -57,10 +57,14 @@ Options *-n, \--nocolor*:: Disable color output. +*-P, \--allpresets*:: + Process all presets contained in '/etc/mkinitcpio.d'. See the '-p' option for + more detail about presets. + *-p, \--preset* 'preset':: Build initramfs image(s) according to specified 'preset'. This may be a file in /etc/mkinitcpio.d (without the .preset extension) or a full, absolute path to a - file. + file. This option may be specified multiple times to process multiple presets. *-r, \--moduleroot* 'root':: Specifies the root directory to find modules in, defaulting to '/'. @@ -254,7 +258,7 @@ Files Default configuration file for mkinitcpio. '/etc/mkinitcpio.d':: - Folder containing mkinitcpio presets. + Directory containing mkinitcpio presets. '/usr/lib/initcpio/install':: '/lib/initcpio/install':: diff --git a/mkinitcpio b/mkinitcpio index 9802fd5..b19b5be 100755 --- a/mkinitcpio +++ b/mkinitcpio @@ -17,11 +17,11 @@ _d_install="$PWD/install:/usr/lib/initcpio/install:/lib/initcpio/install" _d_presets=mkinitcpio.d # options and runtime data -_optmoduleroot= _optkver= _optgenimg= _optpreset= +_optmoduleroot= _optkver= _optgenimg= _optcompress= _optshowautomods=0 _optsavetree=0 _optshowmods=0 _optquiet=1 _optcolor=1 -_optskiphooks=() _optaddhooks=() _hooks=() +_optskiphooks=() _optaddhooks=() _hooks=() _optpreset=() declare -A _runhooks _addedmodules _modpaths _autodetect_cache # export a sane PATH @@ -245,7 +245,10 @@ process_preset() { printf -v preset '%s/%s.preset' "$_d_presets" "$preset" fi - . "$preset" || die "Preset not found: \`%s'" "$preset" + if ! . "$preset"; then + error "Preset not found: \`%s'" "$preset" + return 1 + fi # Use -m and -v options specified earlier (( _optquiet )) || preset_mkopts+=(-v) @@ -253,7 +256,7 @@ process_preset() { ret=0 for p in "${PRESETS[@]}"; do - msg "Building image from preset: '$p'" + msg "Building image from preset: $preset: '$p'" preset_cmd=("${preset_mkopts[@]}") preset_kver=${p}_kver @@ -290,7 +293,7 @@ process_preset() { (( $? )) && ret=1 done - exit $ret + return $ret } install_modules() { @@ -339,9 +342,9 @@ install_modules() { trap 'cleanup 130' INT trap 'cleanup 143' TERM -_opt_short='A:c:g:H:hk:nLMp:r:S:st:Vvz:' +_opt_short='A:c:g:H:hk:nLMPp:r:S:st:Vvz:' _opt_long=('add:' 'addhooks:' 'config:' 'generate:' 'hookhelp:' 'help' - 'kernel:' 'listhooks' 'automods' 'moduleroot:' 'nocolor' + 'kernel:' 'listhooks' 'automods' 'moduleroot:' 'nocolor' 'allpresets' 'preset:' 'skiphooks:' 'save' 'builddir:' 'version' 'verbose' 'compress:') parseopts "$_opt_short" "${_opt_long[@]}" -- "$@" || exit 1 @@ -378,7 +381,7 @@ while :; do cleanup 0 ;; -p|--preset) shift - _optpreset=$1 ;; + _optpreset+=("$1") ;; -n|--nocolor) _optcolor=0 ;; -v|--verbose) @@ -397,6 +400,10 @@ while :; do exit 0 ;; -M|--automods) _optshowautomods=1 ;; + -P|--allpresets) + _optpreset=("$_d_presets"/*.preset) + [[ -e ${_optpreset[0]} ]] || die "No presets found in $_d_presets" + ;; -t|--builddir) shift export TMPDIR=$1 ;; @@ -424,7 +431,10 @@ fi [[ -e /dev/fd ]] || die "/dev must be mounted!" # use preset $_optpreset (exits after processing) -[[ $_optpreset ]] && process_preset "$_optpreset" +if (( ${#_optpreset[*]} )); then + map process_preset "${_optpreset[@]}" + exit +fi KERNELVERSION=$(resolve_kernver "$_optkver") || cleanup 1 _d_kmoduledir=$_optmoduleroot/lib/modules/$KERNELVERSION -- 1.8.1.2
On Mon, Feb 4, 2013 at 11:58 PM, Dave Reisner <dreisner@archlinux.org> wrote:
As a logical extension, allow the -p option to be specified multiple times in order to process several presets at once. --- This is more along the lines of what I had in mind. Only lightly tested.
bash-completion | 2 +- man/mkinitcpio.8.txt | 8 ++++++-- mkinitcpio | 28 +++++++++++++++++++--------- 3 files changed, 26 insertions(+), 12 deletions(-)
...
# use preset $_optpreset (exits after processing) -[[ $_optpreset ]] && process_preset "$_optpreset" +if (( ${#_optpreset[*]} )); then + map process_preset "${_optpreset[@]}" + exit +fi
This is smaller but you don't have a clean bash context between preset files call. In my original patch I use a subshell to avoid this and this is why I doesn't use map function[1]. By example, the first preset file define ALL_options and a second file not. Without different context, second files execution will inherits of this var and result from 2 call with -p options. Cheers, [1] To be honnest, I doesn't knew that function exists :p -- Sébastien "Seblu" Luttringer https://www.seblu.net GPG: 0x2072D77A
On Tue, Feb 05, 2013 at 01:56:33AM +0100, Sébastien Luttringer wrote:
On Mon, Feb 4, 2013 at 11:58 PM, Dave Reisner <dreisner@archlinux.org> wrote:
As a logical extension, allow the -p option to be specified multiple times in order to process several presets at once. --- This is more along the lines of what I had in mind. Only lightly tested.
bash-completion | 2 +- man/mkinitcpio.8.txt | 8 ++++++-- mkinitcpio | 28 +++++++++++++++++++--------- 3 files changed, 26 insertions(+), 12 deletions(-)
...
# use preset $_optpreset (exits after processing) -[[ $_optpreset ]] && process_preset "$_optpreset" +if (( ${#_optpreset[*]} )); then + map process_preset "${_optpreset[@]}" + exit +fi
This is smaller but you don't have a clean bash context between preset files call. In my original patch I use a subshell to avoid this and this is why I doesn't use map function[1].
Fair point, but that's easily worked around by turning the entire process_preset function into a subshell, or simply scoping the ALL_* variables after sourcing the preset file.
By example, the first preset file define ALL_options and a second file not. Without different context, second files execution will inherits of this var and result from 2 call with -p options.
Cheers,
[1] To be honnest, I doesn't knew that function exists :p
-- Sébastien "Seblu" Luttringer https://www.seblu.net GPG: 0x2072D77A
participants (3)
-
Dave Reisner
-
Dave Reisner
-
Sébastien Luttringer