712866
From f2c5c5c961a91765640f381ec37af085dc91312b Mon Sep 17 00:00:00 2001
712866
From: Hari Bathini <hbathini@linux.vnet.ibm.com>
712866
Date: Thu, 16 Jan 2014 12:11:27 +0530
712866
Subject: [PATCH] Dracut: Add a new argument "--rebuild"
712866
712866
Add "rebuild" option to dracut to  append the current  arguments
712866
to those with  which the input initramfs image was  built.  This
712866
option helps in incrementally building initramfs for testing.
712866
712866
    Usage: dracut [output_file] --rebuild input_file
712866
712866
If optional output file  is not provided, input file provided to
712866
rebuild will be used as output file.
712866
712866
This patch alters  the creation of the initramfs image by adding
712866
the file "/tmp/params.txt" to the image. Command line parameters
712866
excluding "--rebuild",  input  &  output image names and "kernel
712866
version" are stored in this file.  In case "--rebuild" parameter
712866
is specified, "/tmp/params.txt" file, if present in input image,
712866
is read and its contents "prepend"ed to the current command line
712866
parameters, that is if such a file is already present. Also,  it
712866
stores the  cumulative parameters to the file "/tmp/params.txt",
712866
in the  new image. This patch  has been tested successfully on a
712866
PowerBox with f19. It does not alter the behaviour of any of the
712866
existing  options.
712866
712866
Signed-off-by: Manik Bajpai <manibajp@linux.vnet.ibm.com>
712866
Signed-off-by: Hari Bathini <hbathini@linux.vnet.ibm.com>
712866
712866
[Edited-by: Harald Hoyer]
712866
Simplified the cpio extraction process by using 'lsinitrd'.
712866
712866
(cherry picked from commit 659dc319d950999f8d191a81fdc4d3114e9213de)
712866
---
712866
 dracut.sh | 342 ++++++++++++++++++++++++++++++++++++++------------------------
712866
 1 file changed, 210 insertions(+), 132 deletions(-)
712866
712866
diff --git a/dracut.sh b/dracut.sh
5c6c2a
index 8e5e86f7..eff096e5 100755
712866
--- a/dracut.sh
712866
+++ b/dracut.sh
712866
@@ -70,6 +70,7 @@ Creates initial ramdisk images for preloading modules
712866
   --kver [VERSION]      Set kernel version to [VERSION].
712866
   -f, --force           Overwrite existing initramfs file.
712866
   -a, --add [LIST]      Add a space-separated list of dracut modules.
712866
+  --rebuild         Append arguments to those of existing image and rebuild
712866
   -m, --modules [LIST]  Specify a space-separated list of dracut modules to
712866
                          call when building the initramfs. Modules are located
712866
                          in /usr/lib/dracut/modules.d.
712866
@@ -283,132 +284,213 @@ dropindirs_sort()
712866
     }
712866
 }
712866
 
712866
+rearrange_params()
712866
+{
712866
+    # Workaround -i, --include taking 2 arguments
712866
+    set -- "${@/--include/++include}"
712866
+
712866
+    # This prevents any long argument ending with "-i"
712866
+    # -i, like --opt-i but I think we can just prevent that
712866
+    set -- "${@/%-i/++include}"
712866
+
712866
+    TEMP=$(unset POSIXLY_CORRECT; getopt \
712866
+        -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
712866
+        --long kver: \
712866
+        --long add: \
712866
+        --long force-add: \
712866
+        --long add-drivers: \
712866
+        --long omit-drivers: \
712866
+        --long modules: \
712866
+        --long omit: \
712866
+        --long drivers: \
712866
+        --long filesystems: \
712866
+        --long install: \
712866
+        --long fwdir: \
712866
+        --long libdirs: \
712866
+        --long fscks: \
712866
+        --long add-fstab: \
712866
+        --long mount: \
712866
+        --long device: \
712866
+        --long add-device: \
712866
+        --long nofscks: \
712866
+        --long ro-mnt \
712866
+        --long kmoddir: \
712866
+        --long conf: \
712866
+        --long confdir: \
712866
+        --long tmpdir: \
712866
+        --long stdlog: \
712866
+        --long compress: \
712866
+        --long prefix: \
712866
+        --long rebuild: \
712866
+        --long force \
712866
+        --long kernel-only \
712866
+        --long no-kernel \
712866
+        --long print-cmdline \
712866
+        --long kernel-cmdline: \
712866
+        --long strip \
712866
+        --long nostrip \
712866
+        --long prelink \
712866
+        --long noprelink \
712866
+        --long hardlink \
712866
+        --long nohardlink \
712866
+        --long noprefix \
712866
+        --long mdadmconf \
712866
+        --long nomdadmconf \
712866
+        --long lvmconf \
712866
+        --long nolvmconf \
712866
+        --long debug \
712866
+        --long profile \
712866
+        --long sshkey: \
712866
+        --long logfile: \
712866
+        --long verbose \
712866
+        --long quiet \
712866
+        --long local \
712866
+        --long hostonly \
712866
+        --long host-only \
712866
+        --long no-hostonly \
712866
+        --long no-host-only \
712866
+        --long hostonly-cmdline \
712866
+        --long no-hostonly-cmdline \
712866
+        --long persistent-policy: \
712866
+        --long fstab \
712866
+        --long help \
712866
+        --long bzip2 \
712866
+        --long lzma \
712866
+        --long xz \
712866
+        --long lzo \
712866
+        --long lz4 \
712866
+        --long no-compress \
712866
+        --long gzip \
712866
+        --long list-modules \
712866
+        --long show-modules \
712866
+        --long keep \
712866
+        --long printsize \
712866
+        --long regenerate-all \
712866
+        --long noimageifnotneeded \
712866
+        --long early-microcode \
712866
+        --long no-early-microcode \
712866
+        -- "$@")
712866
+
712866
+    if (( $? != 0 )); then
712866
+        usage
712866
+        exit 1
712866
+    fi
712866
+}
712866
+
712866
 verbosity_mod_l=0
712866
 unset kernel
712866
 unset outfile
712866
 
712866
-# Workaround -i, --include taking 2 arguments
712866
-set -- "${@/--include/++include}"
712866
-
712866
-# This prevents any long argument ending with "-i"
712866
-# -i, like --opt-i but I think we can just prevent that
712866
-set -- "${@/%-i/++include}"
712866
-
712866
-TEMP=$(unset POSIXLY_CORRECT; getopt \
712866
-    -o "a:m:o:d:I:k:c:L:fvqlHhMN" \
712866
-    --long kver: \
712866
-    --long add: \
712866
-    --long force-add: \
712866
-    --long add-drivers: \
712866
-    --long omit-drivers: \
712866
-    --long modules: \
712866
-    --long omit: \
712866
-    --long drivers: \
712866
-    --long filesystems: \
712866
-    --long install: \
712866
-    --long fwdir: \
712866
-    --long libdirs: \
712866
-    --long fscks: \
712866
-    --long add-fstab: \
712866
-    --long mount: \
712866
-    --long device: \
712866
-    --long add-device: \
712866
-    --long nofscks: \
712866
-    --long ro-mnt \
712866
-    --long kmoddir: \
712866
-    --long conf: \
712866
-    --long confdir: \
712866
-    --long tmpdir: \
712866
-    --long stdlog: \
712866
-    --long compress: \
712866
-    --long prefix: \
712866
-    --long force \
712866
-    --long kernel-only \
712866
-    --long no-kernel \
712866
-    --long print-cmdline \
712866
-    --long kernel-cmdline: \
712866
-    --long strip \
712866
-    --long nostrip \
712866
-    --long prelink \
712866
-    --long noprelink \
712866
-    --long hardlink \
712866
-    --long nohardlink \
712866
-    --long noprefix \
712866
-    --long mdadmconf \
712866
-    --long nomdadmconf \
712866
-    --long lvmconf \
712866
-    --long nolvmconf \
712866
-    --long debug \
712866
-    --long profile \
712866
-    --long sshkey: \
712866
-    --long logfile: \
712866
-    --long verbose \
712866
-    --long quiet \
712866
-    --long local \
712866
-    --long hostonly \
712866
-    --long host-only \
712866
-    --long no-hostonly \
712866
-    --long no-host-only \
712866
-    --long hostonly-cmdline \
712866
-    --long no-hostonly-cmdline \
712866
-    --long persistent-policy: \
712866
-    --long fstab \
712866
-    --long help \
712866
-    --long bzip2 \
712866
-    --long lzma \
712866
-    --long xz \
712866
-    --long lzo \
712866
-    --long lz4 \
712866
-    --long no-compress \
712866
-    --long gzip \
712866
-    --long list-modules \
712866
-    --long show-modules \
712866
-    --long keep \
712866
-    --long printsize \
712866
-    --long regenerate-all \
712866
-    --long noimageifnotneeded \
712866
-    --long early-microcode \
712866
-    --long no-early-microcode \
712866
-    -- "$@")
712866
-
712866
-if (( $? != 0 )); then
712866
-    usage
712866
-    exit 1
712866
+rearrange_params "$@"
712866
+eval set -- "$TEMP"
712866
+
712866
+# parse command line args to check if '--rebuild' option is present
712866
+unset append_args_l
712866
+unset rebuild_file
712866
+while :
712866
+do
712866
+	if [ "$1" == "--" ]; then
712866
+	    shift; break
712866
+	fi
712866
+	if [ "$1" == "--rebuild" ]; then
712866
+	    append_args_l="yes"
712866
+            rebuild_file=$2
712866
+            if [ ! -e $rebuild_file ]; then
712866
+                echo "Image file '$rebuild_file', for rebuild, does not exist!"
712866
+                exit 1
712866
+            fi
712866
+            abs_rebuild_file=$(readlink -f "$rebuild_file") && rebuild_file="$abs_rebuild_file"
712866
+	    shift; continue
712866
+	fi
712866
+	shift
712866
+done
712866
+
712866
+# get output file name and kernel version from command line arguments
712866
+while (($# > 0)); do
712866
+    case ${1%%=*} in
712866
+        ++include)
712866
+            shift 2;;
712866
+        *)
712866
+            if ! [[ ${outfile+x} ]]; then
712866
+                outfile=$1
712866
+            elif ! [[ ${kernel+x} ]]; then
712866
+                kernel=$1
712866
+            else
712866
+                printf "\nUnknown arguments: %s\n\n" "$*" >&2
712866
+                usage; exit 1;
712866
+            fi
712866
+            ;;
712866
+    esac
712866
+    shift
712866
+done
712866
+
712866
+# extract input image file provided with rebuild option to get previous parameters, if any
712866
+if [[ $append_args_l == "yes" ]]; then
712866
+    unset rebuild_param
712866
+
712866
+    # determine resultant file
712866
+    if ! [[ $outfile ]]; then
712866
+        outfile=$rebuild_file
712866
+    fi
712866
+
712866
+    if ! rebuild_param=$(lsinitrd $rebuild_file '*lib/dracut/build-parameter.txt'); then
712866
+        echo "Image '$rebuild_file' has no rebuild information stored"
712866
+        exit 1
712866
+    fi
712866
+
712866
+    # prepend previous parameters to current command line args
712866
+    if [[ $rebuild_param ]]; then
712866
+        TEMP="$rebuild_param $TEMP"
712866
+        eval set -- "$TEMP"
712866
+        rearrange_params "$@"
712866
+    fi
712866
+
712866
+    # clean the temporarily used scratch-pad directory
712866
+    rm -rf $scratch_dir
712866
 fi
712866
 
712866
+unset PARMS_TO_STORE
712866
+PARMS_TO_STORE=""
712866
+
712866
 eval set -- "$TEMP"
712866
 
712866
 while :; do
712866
+    if [ $1 != "--" ] && [ $1 != "--rebuild" ]; then
712866
+        PARMS_TO_STORE+=" $1";
712866
+    fi
712866
     case $1 in
712866
-        --kver)        kernel="$2"; shift;;
712866
-        -a|--add)      push add_dracutmodules_l  "$2"; shift;;
712866
-        --force-add)   push force_add_dracutmodules_l  "$2"; shift;;
712866
-        --add-drivers) push add_drivers_l        "$2"; shift;;
712866
-        --omit-drivers)
712866
-                       push omit_drivers_l      "$2"; shift;;
712866
-        -m|--modules)  push dracutmodules_l      "$2"; shift;;
712866
-        -o|--omit)     push omit_dracutmodules_l "$2"; shift;;
712866
-        -d|--drivers)  push drivers_l            "$2"; shift;;
712866
-        --filesystems) push filesystems_l        "$2"; shift;;
712866
-        -I|--install)  push install_items_l      "$2"; shift;;
712866
-        --fwdir)       push fw_dir_l             "$2"; shift;;
712866
-        --libdirs)     push libdirs_l            "$2"; shift;;
712866
-        --fscks)       push fscks_l              "$2"; shift;;
712866
-        --add-fstab)   push add_fstab_l          "$2"; shift;;
712866
-        --mount)       push fstab_lines          "$2"; shift;;
712866
+        --kver)        kernel="$2";                    PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        -a|--add)      push add_dracutmodules_l  "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --force-add)   push force_add_dracutmodules_l  "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --add-drivers) push add_drivers_l        "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --omit-drivers) push omit_drivers_l      "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        -m|--modules)  push dracutmodules_l      "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        -o|--omit)     push omit_dracutmodules_l "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        -d|--drivers)  push drivers_l            "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --filesystems) push filesystems_l        "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        -I|--install)  push install_items_l      "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --fwdir)       push fw_dir_l             "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --libdirs)     push libdirs_l            "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --fscks)       push fscks_l              "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --add-fstab)   push add_fstab_l          "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --mount)       push fstab_lines          "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
         --add-device|--device)
712866
-                       push add_device_l         "$2"; shift;;
712866
-        --kernel-cmdline)
712866
-                       push kernel_cmdline_l  "$2"; shift;;
712866
+                       push add_device_l         "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --kernel-cmdline) push kernel_cmdline_l  "$2"; PARMS_TO_STORE+=" '$2'"; shift;;
712866
         --nofscks)     nofscks_l="yes";;
712866
         --ro-mnt)      ro_mnt_l="yes";;
712866
-        -k|--kmoddir)  drivers_dir_l="$2"; shift;;
712866
-        -c|--conf)     conffile="$2"; shift;;
712866
-        --confdir)     confdir="$2"; shift;;
712866
-        --tmpdir)      tmpdir_l="$2"; shift;;
712866
-        -L|--stdlog)   stdloglvl_l="$2"; shift;;
712866
-        --compress)    compress_l="$2"; shift;;
712866
-        --prefix)      prefix_l="$2"; shift;;
712866
+        -k|--kmoddir)  drivers_dir_l="$2";             PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        -c|--conf)     conffile="$2";                  PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --confdir)     confdir="$2";                   PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --tmpdir)      tmpdir_l="$2";                  PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        -L|--stdlog)   stdloglvl_l="$2";               PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --compress)    compress_l="$2";                PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --prefix)      prefix_l="$2";                  PARMS_TO_STORE+=" '$2'"; shift;;
712866
+        --rebuild)     if [ $rebuild_file == $outfile ]; then
712866
+                           force=yes
712866
+                       fi
712866
+                       shift
712866
+                       ;;
712866
         -f|--force)    force=yes;;
712866
         --kernel-only) kernel_only="yes"; no_kernel="no";;
712866
         --no-kernel)   kernel_only="no"; no_kernel="yes";;
712866
@@ -431,7 +513,7 @@ while :; do
712866
         --nolvmconf)   lvmconf_l="no";;
712866
         --debug)       debug="yes";;
712866
         --profile)     profile="yes";;
712866
-        --sshkey)      sshkey="$2"; shift;;
712866
+        --sshkey)      sshkey="$2";                    PARMS_TO_STORE+=" '$2'"; shift;;
712866
         --logfile)     logfile_l="$2"; shift;;
712866
         -v|--verbose)  ((verbosity_mod_l++));;
712866
         -q|--quiet)    ((verbosity_mod_l--));;
712866
@@ -449,11 +531,10 @@ while :; do
712866
         --no-hostonly-cmdline)
712866
                        hostonly_cmdline_l="no" ;;
712866
         --persistent-policy)
712866
-                       persistent_policy_l="$2"; shift;;
712866
+                       persistent_policy_l="$2";       PARMS_TO_STORE+=" '$2'"; shift;;
712866
         --fstab)       use_fstab_l="yes" ;;
712866
         -h|--help)     long_usage; exit 1 ;;
712866
-        -i|--include)
712866
-                       push include_src "$2"
712866
+        -i|--include)  push include_src "$2";          PARMS_TO_STORE+=" '$2'";
712866
                        shift;;
712866
         --bzip2)       compress_l="bzip2";;
712866
         --lzma)        compress_l="lzma";;
712866
@@ -483,21 +564,12 @@ done
712866
 # the old fashioned way
712866
 
712866
 while (($# > 0)); do
712866
-    case ${1%%=*} in
712866
-        ++include) push include_src "$2"
712866
-                       push include_target "$3"
712866
-                       shift 2;;
712866
-        *)
712866
-            if ! [[ ${outfile+x} ]]; then
712866
-                outfile=$1
712866
-            elif ! [[ ${kernel+x} ]]; then
712866
-                kernel=$1
712866
-            else
712866
-                printf "\nUnknown arguments: %s\n\n" "$*" >&2
712866
-                usage; exit 1;
712866
-            fi
712866
-            ;;
712866
-    esac
712866
+    if [ ${1%%=*} == "++include" ]; then
712866
+        push include_src "$2"
712866
+        push include_target "$3"
712866
+        PARMS_TO_STORE+=" --include '$2' '$3'"
712866
+        shift 2
712866
+    fi
712866
     shift
712866
 done
712866
 
712866
@@ -1405,6 +1477,12 @@ if [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]]; then
712866
     done
712866
 fi
712866
 
712866
+dinfo "*** Store current command line parameters ***"
712866
+if ! ( echo $PARMS_TO_STORE > $initdir/lib/dracut/build-parameter.txt ); then
712866
+    dfatal "Could not store the current command line parameters"
712866
+    exit 1
712866
+fi
712866
+
712866
 rm -f -- "$outfile"
712866
 dinfo "*** Creating image file ***"
712866