#!/bin/bash
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

. /lib/kdump/setup-kdump.functions

kdump_get_config || return 1

KERNELVERSION=
FORCE=0
QUIET=0
MKINITRD_EXTRA_PARAMS=

#
# Prints usage.                                                              {{{
function usage()
{
    echo "mkdumprd - Create an initrd for kdump"
    echo ""
    echo "This script uses mkinitrd(8) internally. Options:"
    echo ""
    echo "   -k <version>     Kernel to create the initrd for."
    echo "                    Defaults to the running kernel."
    echo "   -f               Force regeneration even if the configuration"
    echo "                    did not change"
    echo "   -q               quiet (don't print status messages)"
    echo "   -i <params>      pass <params> to mkinitrd"
    echo "   -h               Print this help"
}                                                                          # }}}

#
# Quiet message                                                              {{{
function status_message()
{
    local msg=$1

    if (( ! $QUIET )) ; then
        echo ">>> $msg"
    fi
}                                                                          # }}}

#
# Create a new initrd using mkinitrd                                         {{{
function run_mkinitrd()
{
    #
    # network configuration
    FEATURE_LIST="kdump"
    NETARGS=
    if [ "$KDUMP_NETCONFIG" = "auto" ] ; then
	FEATURE_LIST="$FEATURE_LIST network"
	status_message "Network: auto"
    elif [ -z "$KDUMP_NETCONFIG" ] ; then
	status_message "Network: none"
    else
	interface=$(echo "$KDUMP_NETCONFIG" | cut -d ':' -f 1)
	mode=$(echo "$KDUMP_NETCONFIG" | cut -d ':' -f 2)

	status_message "Network interface: $interface"
	if [ "$mode" = "static" ] ; then
            NETARGS="-I $interface"
            status_message "Network mode: Static IP"
	else
            NETARGS="-D $interface"
            status_message "Network mode: Automatic IP (DHCP)"
	fi
    fi

    INITRD_TMP=`mktemp --tmpdir mkdumprd.XXXXXXXXXX` || exit $?
    MKINITRD_ARGS="-k $KERNEL -i $INITRD_TMP -f '$FEATURE_LIST'"

    #
    # the -B parameter skips bootloader update, which is desirable if the
    # kernel is loaded by kexec, but not if we're using FADUMP
    if [ "$KDUMP_FADUMP" != "yes" ] ; then
	MKINITRD_ARGS="$MKINITRD_ARGS -B"
    fi

    #
    # the -s parameter is only available when the bootsplash package and its
    # mkinitrd script is installed ... so check that first
    if [ -f "/lib/mkinitrd/scripts/setup-splash.sh" ] ; then
	MKINITRD_ARGS="$MKINITRD_ARGS  -s ''"
    fi

    MKINITRD_ARGS="$MKINITRD_ARGS $MKINITRD_EXTRA_PARAMS $NETARGS"

    #
    # if -q is specified, don't print mkinitrd output
    if (( $QUIET )) ; then
	MKINITRD_ARGS="$MKINITRD_ARGS &>/dev/null"
    fi

    status_message "Calling mkinitrd $MKINITRD_ARGS"
    echo "Regenerating kdump initrd ..." >&2
    eval "bash -$- /sbin/mkinitrd $MKINITRD_ARGS"
    ret=$?
    if [ $ret -eq 0 ]; then
	mv "$INITRD_TMP" "$INITRD"
	ret=$?
    fi
    return $ret
}                                                                          # }}}

#
# Create a new initrd using dracut                                           {{{
function run_dracut()
{
    DRACUT_ARGS="--force --hostonly --omit 'plymouth resume usrmount'"
    DRACUT_ARGS="$DRACUT_ARGS --compress='xz -0 --check=crc32'"

    if [ -z "$KERNELVERSION" ]
    then
	KERNELVERSION=$(get_kernel_version "$KERNEL")
    fi

    # add mount points
    kdump_get_mountpoints || return 1
    i=0
    while [ $i -lt ${#kdump_mnt[@]} ]
    do
	if [ -n "${kdump_mnt[i]}" ] ; then
	    DRACUT_ARGS="$DRACUT_ARGS --mount '${kdump_dev[i]} ${kdump_mnt[i]} ${kdump_fstype[i]} ${kdump_opts[i]}'"
	fi
	i=$((i+1))
    done

    # Make resolved variables visible to the dracut module
    kdump_export_targets

    DRACUT_ARGS="$DRACUT_ARGS --add 'kdump' $INITRD $KERNELVERSION"
    echo "Regenerating kdump initrd ..." >&2
    eval "bash -$- dracut $DRACUT_ARGS"
}                                                                          # }}}


# Option parsing                                                             {{{
while getopts "i:hfqk:K:I:" name ; do
    case $name in
        k)  KERNELVERSION=$OPTARG
            ;;

        f)  FORCE=1
            ;;

        h)  usage
            exit 0
            ;;

        q)  QUIET=1
            ;;

        K)  KERNEL=$OPTARG
            ;;

        I)  INITRD=$OPTARG
            ;;

        i)  MKINITRD_EXTRA_PARAMS=$OPTARG
            ;;

        ?)  usage
            exit 1
            ;;
    esac
done
shift $(($OPTIND -1))

                                                                           # }}}

#
# if we don't have specified -K <file>, then get the kernel from the version
# if $KERNELVERSION is non-zero and use the default kernel for kdump
# if $KERNELVERSION is zero.
if [ -z "$KERNEL" ] ; then
    if [ -n "$KERNELVERSION" ] ; then
        KERNEL=/boot/vmlinuz-$KERNELVERSION
        if ! [ -f "$KERNEL" ] ; then
            KERNEL=/boot/vmlinux-$KERNELVERSION
        fi
    else
        output=$(kdumptool find_kernel $find_kernel_args)
        KERNEL=$(echo "$output" | grep ^Kernel | cut -f 2)
        INITRD=$(echo "$output" | grep ^Initrd | cut -f 2)
    fi
fi

#
# If 

if ! [ -f "$KERNEL" ] ; then
    echo "Kernel $KERNEL does not exist."
    exit 1
fi

#
# if we don't have specified -I <file>, then get the kernel from the version
if [ -z "$INITRD" ] ; then
    INITRD=/boot/initrd-$KERNELVERSION

    if ! echo $KERNELVERSION | grep -q kdump ; then
        INITRD=$INITRD-kdump
    fi
fi

#
# check if we need to regenerate it?
if (( ! $FORCE )) ; then
    if [ -f "$INITRD" ] && \
            [ "$KDUMP_CONFIG" -ot "$INITRD" ] && \
            [ /root/.ssh/id_dsa.pub -ot "$INITRD" ] && \
            [ /root/.ssh/id_dsa -ot "$INITRD" ] && \
            [ "$KERNEL" -ot "$INITRD" ] ; then
        status_message "Not regenerating kdump initrd. Use mkdumprd -f to force regeneration."
        exit 0
    fi
fi

if type dracut >/dev/null 2>&1 ; then
    run_dracut
    ret=$?
else
    run_mkinitrd
    ret=$?
fi

exit $ret

# vim: set ts=4 sw=4 et fdm=marker: :collapseFolds=1:
