#!/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.

CONFIG=/etc/sysconfig/kdump
KERNELVERSION=
FORCE=0
QUIET=0
MKINITRD_EXTRA_PARAMS=

. "$CONFIG"

#
# 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
}                                                                          # }}}


# 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" ] && \
            [ "$CONFIG" -ot "$INITRD" ] && \
            [ /root/.ssh/id_dsa.pub -ot "$INITRD" ] && \
            [ /root/.ssh/id_dsa -ot "$INITRD" ] && \
            [ "$KERNEL" -ot "$INITRD" ] ; then
        status_message "Not regenerating initrd. Use -f to force regeneration."
        exit 0
    fi
fi

#
# network configuration
FEATURE_LIST="kdump"
NETARGS=
if [ "$KDUMP_NETCONFIG" = "auto" ] ; then
    FEATURE_LIST="$FEATURE_LIST network"
    NETARGS="-D default"
    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

MKINITRD_ARGS="-B -k $KERNEL -i $INITRD -f '$FEATURE_LIST'"

#
# 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 "/sbin/mkinitrd $MKINITRD_ARGS"
ret=$?

#
# because the root key of /root is stored into initrd, we make the
# initrd read-only for root
if [ -f "$INITRD" ] ; then
    chmod 600 "$INITRD"
fi

exit $ret

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