From: Petr Tesarik <ptesarik@suse.cz>
Date: Sun Aug 30 23:18:46 2015 +0200
Subject: Improve systemd integration
References: FATE#319020
Patch-mainline: v0.8.16
Git-commit: 4d72c589ca9de7696e662f10f86d1739438ed6e1

Add a unit file for systemd. To avoid divergence between systemd
and sysv scripts, the ExecStart and ExecStop actions are moved to
individual scripts under /lib/kdump and used by both implementations.

As a side effect, rc.kdump.functions is no longer used.

Signed-off-by: Petr Tesarik <ptesarik@suse.com>

---
 init/CMakeLists.txt     |   14 +-
 init/boot.kdump         |   55 ++++++--
 init/kdump.service      |   12 +
 init/load.sh            |  294 +++++++++++++++++++++++++++++++++++++++++++
 init/mkdumprd           |    8 -
 init/rc.kdump.functions |  326 ------------------------------------------------
 init/unload.sh          |   24 +++
 7 files changed, 384 insertions(+), 349 deletions(-)

--- a/init/CMakeLists.txt
+++ b/init/CMakeLists.txt
@@ -30,6 +30,17 @@ INSTALL(
 
 INSTALL(
     FILES
+        ${CMAKE_CURRENT_SOURCE_DIR}/kdump.service
+    DESTINATION
+        /usr/lib/systemd/system
+    PERMISSIONS
+        OWNER_READ OWNER_WRITE
+        GROUP_READ
+        WORLD_READ
+)
+
+INSTALL(
+    FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/setup-mkdumprd.sh
         ${CMAKE_CURRENT_SOURCE_DIR}/setup-kdumpfs.sh
         ${CMAKE_CURRENT_SOURCE_DIR}/setup-kdump.sh
@@ -56,9 +67,10 @@ INSTALL(
 
 INSTALL(
     FILES
-        ${CMAKE_CURRENT_SOURCE_DIR}/rc.kdump.functions
         ${CMAKE_CURRENT_SOURCE_DIR}/setup-kdump.functions
         ${CMAKE_CURRENT_SOURCE_DIR}/save_dump.sh
+        ${CMAKE_CURRENT_SOURCE_DIR}/load.sh
+        ${CMAKE_CURRENT_SOURCE_DIR}/unload.sh
     DESTINATION
         /lib/kdump
     PERMISSIONS
--- a/init/boot.kdump
+++ b/init/boot.kdump
@@ -25,30 +25,56 @@
 # Description:    This script loads the kdump kernel on startup.
 ### END INIT INFO
 
+KDUMPTOOL=/usr/sbin/kdumptool
+LOAD_KDUMP=/lib/kdump/load.sh
+UNLOAD_KDUMP=/lib/kdump/unload.sh
+
 . /etc/rc.status
-. /lib/kdump/rc.kdump.functions
+
+#
+# Checks if currently a kdump kernel is loaded.
+#
+# Returns: 0 (true) if a kdump kernel is loaded,
+#          1 (false) if the kdump kernel is not loaded and
+#          -1 (false) if the feature is not implemented (too old kernel)
+function kdump_kernel_loaded()
+{
+    eval $($KDUMPTOOL dump_config)
+    if [ "$KDUMP_FADUMP" = "yes" ] ; then
+	if [ -r /sys/kernel/fadump_registered ] ; then
+	    if [ $(cat /sys/kernel/fadump_registered) = "1" ] ; then
+		return 0
+	    else
+		return 1
+	    fi
+	else
+	    return -1
+	fi
+    elif [ -r /sys/kernel/kexec_crash_loaded ] ; then
+        if [ $(cat /sys/kernel/kexec_crash_loaded) = "1" ] ; then
+            return 0
+        else
+            return 1
+        fi
+    else
+        return -1
+    fi
+}
 
 case "$1" in
   start|restart|force-reload)
-        load_kdump
+	echo -n "Loading kdump"
+        $LOAD_KDUMP
+	rc_status
         ;;
   stop)
         if [ "$RUNLEVEL" != "" ]; then
             echo -n "Not unloading kdump during runlevel changes"
             rc_status -s
         else
-            kdump_kernel_loaded
-            if [ $? -ne 1 ] ; then
+            if kdump_kernel_loaded ; then
                 echo -n "Unloading kdump"
-		if fadump_enabled; then
-		    # The kernel fails with EINVAL if unregistered already
-		    # (see bnc#814780)
-		    if [ "$(cat $FADUMP_REGISTERED)" != "0" ]; then
-			echo 0 > $FADUMP_REGISTERED
-		    fi
-		else
-                    $KEXEC -p -u
-		fi
+		$UNLOAD_KDUMP
             fi
             rc_status -v
         fi
@@ -71,7 +97,8 @@ case "$1" in
         ;;
   try-restart)
         if kdump_kernel_loaded ; then
-            $0 start
+	    echo "Loading kdump"
+            $LOAD_KDUMP
         else
             rc_reset
         fi
--- /dev/null
+++ b/init/kdump.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Load kdump kernel on startup
+After=local-fs.target
+
+[Service]
+Type=oneshot
+ExecStart=/lib/kdump/load.sh
+ExecStop=/lib/kdump/unload.sh
+RemainAfterExit=true
+
+[Install]
+WantedBy=multi-user.target
--- /dev/null
+++ b/init/load.sh
@@ -0,0 +1,294 @@
+#! /bin/bash
+#
+#  Copyright Petr Tesarik <ptesarik@suse.com>
+#  Load the kdump kernel specified in /etc/sysconfig/kdump
+
+KDUMPTOOL=/usr/sbin/kdumptool
+KEXEC=/sbin/kexec
+EFI_SYSTAB=/sys/firmware/efi/systab
+FADUMP_ENABLED=/sys/kernel/fadump_enabled
+FADUMP_REGISTERED=/sys/kernel/fadump_registered
+
+#
+# Remove an option from the kernel command line
+function remove_from_commandline()
+{
+    local option="$1"
+
+    awk 'BEGIN { ORS="" }
+    {
+        while(length()) {
+            sub(/^[[:space:]]+/,"");
+            pstart=match($0,/("[^"]*"?|[^"[:space:]])+/);
+            plength=RLENGTH;
+            param=substr($0,pstart,plength);
+            raw=param;
+            gsub(/"/,"",raw);
+            if (raw !~ /^('"$option"')(=|$)/)
+                print param " ";
+            $0=substr($0,pstart+plength);
+        }
+        print "\n";
+    }'
+}
+
+#
+# Get the name of kernel parameter to limit CPUs
+# Linux 2.6.34+ has nr_cpus, older versions must use maxcpus
+function cpus_param()
+{
+    # nr_cpus does not work properly on ppc/ppc64/ppc64le
+    case $(uname -m) in
+        ppc*)
+	    echo maxcpus
+	    return 0
+	    ;;
+    esac
+
+    local version=$(get_kernel_version "$1")
+    version="${version%%-*}"
+    local verhex=$(IFS=. ; set -- $version ; printf "0x%02x%02x%02x" $1 $2 $3)
+    if [ $(( $verhex )) -ge $(( 0x020622 )) ] ; then
+	echo nr_cpus
+    else
+	echo maxcpus
+    fi
+}
+
+#
+# Builds the kdump command line from KDUMP_COMMANDLINE.
+function build_kdump_commandline()
+{
+    local kdump_kernel="$1"
+    local commandline="$KDUMP_COMMANDLINE"
+
+    if [ -z "$commandline" ] ; then
+        local nr_cpus=$(cpus_param "$kdump_kernel")
+        commandline=$(
+            remove_from_commandline \
+                'root|resume|crashkernel|splash|mem|BOOT_IMAGE|showopts|zfcp\.allow_lun_scan|hugepages|acpi_no_memhotplug|cgroup_disable|unknown_nmi_panic' \
+                < /proc/cmdline)
+        # Use deadline for saving the memory footprint
+        commandline="$commandline elevator=deadline sysrq=yes reset_devices acpi_no_memhotplug cgroup_disable=memory"
+        commandline="$commandline irqpoll ${nr_cpus}=${KDUMP_CPUS:-1}"
+        commandline="$commandline root=kdump"
+        case $(uname -i) in
+        i?86|x86_64)
+            local boot_apicid=$(
+                awk -F':[ \t]' '/^initial apicid[ \t]*:/ {print $2; exit}' \
+                    /proc/cpuinfo)
+            test -n "$boot_apicid" && \
+		commandline="$commandline disable_cpu_apicid=$boot_apicid"
+            commandline=$(echo "$commandline" |
+                remove_from_commandline 'unknown_nmi_panic')
+            ;;
+	s390*)
+	    commandline="$commandline zfcp.allow_lun_scan=0"
+	    ;;
+        esac
+
+        # Xen does not include the kernel release in VMCOREINFO_XEN
+        # so we have to pass it via the command line
+        if [[ $(uname -r) =~ -xen$ ]] ; then
+            kernelrelease=$(uname -r)
+            commandline="$commandline kernelversion=$kernelrelease"
+        fi
+
+	if [ -f /sys/firmware/efi/systab ] ; then
+	    local acpi_addr
+	    if grep -q '^ACPI20=' /sys/firmware/efi/systab ; then
+		acpi_addr=$(awk -F'=' '/^ACPI20=/ {print $2}' "$EFI_SYSTAB")
+	    else
+		acpi_addr=$(awk -F'=' '/^ACPI=/ {print $2}' "$EFI_SYSTAB")
+	    fi
+	    commandline="$commandline noefi acpi_rsdp=$acpi_addr"
+	fi
+    fi
+
+    commandline="$commandline $KDUMP_COMMANDLINE_APPEND"
+
+    # Add panic=1 unless there is a panic option already
+    local cmdline_nopanic
+    cmdline_nopanic=$(echo "$commandline" | remove_from_commandline 'panic')
+    if [ "$commandline" = "$cmdline_nopanic" ] ; then
+	commandline="$commandline panic=1"
+    fi
+
+    echo "$commandline"
+}
+
+#
+# Builds the kexec options from KEXEC_OPTIONS
+# Parameters: 1) kernel
+function build_kexec_options()
+{
+    local kdump_kernel="$1"
+    local options="$KEXEC_OPTIONS"
+
+    # remove `--args-linux' for x86 type kernel files here
+    if [ $($KDUMPTOOL identify_kernel -t "$kdump_kernel") = "x86" ] ; then
+        options=$(echo "$options" | sed -e 's/--args-linux//g')
+    fi
+
+    # add --noio on ia64
+    if [ $(uname -i) = "ia64" ] ; then
+        options="$options --noio"
+    fi
+
+    echo "$options"
+}
+
+#
+# Load kdump using kexec
+function load_kdump_kexec()
+{
+    local result
+
+    if [ ! -f "$kdump_initrd" ] ; then
+        echo "No kdump initial ramdisk found. Tried to locate $kdump_initrd."
+	return 6
+    fi
+
+    local kdump_commandline=$(build_kdump_commandline "$kdump_kernel")
+    local kexec_options=$(build_kexec_options "$kdump_kernel")
+
+    KEXEC_CALL="$KEXEC -p $kdump_kernel --append=\"$kdump_commandline\""
+    KEXEC_CALL="$KEXEC_CALL --initrd=$kdump_initrd $kexec_options"
+
+    if [ $((${KDUMP_VERBOSE:-0} & 4)) -gt 0 ] ; then
+        echo "Loading kdump kernel: $KEXEC_CALL"
+    fi
+
+    local output=$(eval "$KEXEC_CALL" 2>&1)
+    if [ $? -eq 0 ] ; then
+	result=0
+    else
+	result=1
+    fi
+
+    # print stderr in any case to show warnings that normally
+    # would be supressed (bnc#374185)
+    echo -n "$output"
+
+    if [ $((${KDUMP_VERBOSE:-0} & 1)) -gt 0 ] ; then
+        if [ $result -eq 0 ] ; then
+            logger -i -t kdump \
+		"Loaded kdump kernel: $KEXEC_CALL, Result: $output"
+        else
+            logger -i -t kdump \
+                "FAILED to load kdump kernel: $KEXEC_CALL, Result: $output"
+        fi
+    fi
+
+    return $result
+}
+
+#
+# Checks if fadump is enabled
+#
+# Returns: 0 (true) if fadump is enabled
+#          1 (false) if fadump is not availabled or disabled
+function fadump_enabled()
+{
+    test -f "$FADUMP_ENABLED" && test $(cat "$FADUMP_ENABLED") = "1"
+}
+
+#
+# Update the bootloader options for fadump
+function fadump_bootloader()
+{
+    case $(uname -m) in
+	ppc*) ;;
+	*) return 0 ;;
+    esac
+
+    local newstate="$1"
+
+    # check if the old configuration is still valid
+    boot_opts=$(kdump-bootloader.pl --get)
+    nofadump_opts=$(echo "$boot_opts" | remove_from_commandline 'fadump')
+    old_opts=$($KDUMPTOOL -F /dev/null -C <(echo "$boot_opts") \
+		dump_config --usage dump --format kernel --nodefault)
+    if [ "$newstate" = on ] ; then
+	curr_opts=$($KDUMPTOOL dump_config --usage dump --format kernel --nodefault)
+	if [ "$old_opts" != "$curr_opts" -o \
+            "$boot_opts" = "$nofadump_opts" ] ; then
+	    kdump-bootloader.pl --update fadump=on "$curr_opts"
+	fi
+    elif [ "$boot_opts" != "$nofadump_opts" ] ; then
+	kdump-bootloader.pl --update
+    fi
+}
+
+#
+# Update fadump configuration
+function load_kdump_fadump()
+{
+    fadump_bootloader on
+
+    if ! fadump_enabled ; then
+        echo "fadump is not enabled in the kernel."
+	return 5
+    fi
+
+    # The kernel fails with EINVAL if registered already
+    # (see bnc#814780)
+    if [ $(cat "$FADUMP_REGISTERED") != "1" ] ; then
+	echo 1 > "$FADUMP_REGISTERED"
+    fi
+
+    return 0
+}
+
+#
+# Find the desired kernel and initrd
+function find_kernel()
+{
+    local output=$($KDUMPTOOL find_kernel)
+    test $? -eq 0 || return 1
+
+    kdump_kernel=$(echo "$output" | grep ^Kernel | cut -f 2)
+    kdump_initrd=$(echo "$output" | grep ^Initrd | cut -f 2)
+
+    return 0
+}
+
+#
+# Rebuild the kdump initramfs if necessary
+function rebuild_kdumprd()
+{
+    local output=$(mkdumprd -K "$kdump_kernel" -I "$kdump_initrd" 2>&1)
+    if [ $? -ne 0 ] ; then
+	echo "$output"
+	return 1
+    fi
+
+    return 0
+}
+
+############################################################
+# MAIN PROGRAM STARTS HERE
+#
+
+eval $($KDUMPTOOL dump_config)
+
+if [ $((${KDUMP_VERBOSE:-0} & 16)) -gt 0 ] ; then
+    KDUMPTOOL="$KDUMPTOOL -D"
+fi
+
+find_kernel || exit 6
+rebuild_kdumprd || exit 1
+
+if [ "$KDUMP_FADUMP" = "yes" ] ; then
+    load_kdump_fadump
+else
+    fadump_bootloader off
+    load_kdump_kexec
+fi
+result=$?
+
+if [ $result = 0 ] ; then
+    echo 1 > /proc/sys/kernel/panic_on_oops
+fi
+
+exit $result
--- a/init/mkdumprd
+++ b/init/mkdumprd
@@ -15,7 +15,6 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
-. /lib/kdump/rc.kdump.functions
 . /lib/kdump/setup-kdump.functions
 
 kdump_get_config || return 1
@@ -230,13 +229,6 @@ if (( ! $FORCE )) ; then
     fi
 fi
 
-# Update the bootloader if necessary
-if [ "$KDUMP_FADUMP" != "yes" ]; then
-    fadump_bootloader off
-else
-    fadump_bootloader on
-fi
-
 if type dracut >/dev/null 2>&1 ; then
     run_dracut
     ret=$?
--- a/init/rc.kdump.functions
+++ /dev/null
@@ -1,326 +0,0 @@
-#!/bin/sh
-#
-#  Copyright 2005 Red Hat, Inc.
-#  Author:  Jeff Moyer <jmoyer@redhat.com>
-#  Modifications for SUSE from Chris Mason <mason@suse.com>
-#                              Takashi Iwai <tiwai@suse.de>
-#                              Bernhard Walle <bwalle@suse.de>
-#
-# ----------------------------------------------------------------------
-# rc.kdump.functions by Petr Tesarik
-
-KDUMP_CONFIG=/etc/sysconfig/kdump
-KEXEC=/sbin/kexec
-FADUMP_ENABLED="/sys/kernel/fadump_enabled"
-FADUMP_REGISTERED="/sys/kernel/fadump_registered"
-
-#
-# Checks if fadump is enabled
-#
-# Returns: 0 (true) if fadump is enabled
-#          1 (false) if fadump is not availabled or disabled
-function fadump_enabled()
-{
-    test -f $FADUMP_ENABLED && test "$(cat $FADUMP_ENABLED)" = "1"
-}
-
-#
-# Checks if currently a kdump kernel is loaded.
-#
-# Returns: 0 (true) if a kdump kernel is loaded,
-#          1 (false) if the kdump kernel is not loaded and
-#          -1 (false) if the feature is not implemented (too old kernel)
-function kdump_kernel_loaded()
-{
-    if [ -f $FADUMP_REGISTERED ]; then
-	if [ "$(cat $FADUMP_REGISTERED)" = "1" ]; then
-	    return 0
-	else
-	    return 1
-	fi
-    fi
-
-    if [ -r /sys/kernel/kexec_crash_loaded ]; then
-        if [ "$(cat /sys/kernel/kexec_crash_loaded)" = "1" ]; then
-            return 0
-        else
-            return 1
-        fi
-    else
-        return -1
-    fi
-}
-
-#
-# Remove an option from the kernel command line
-function remove_from_commandline()
-{
-    local option="$1"
-
-    awk 'BEGIN { ORS="" }
-    {
-        while(length()) {
-            sub(/^[[:space:]]+/,"");
-            pstart=match($0,/("[^"]*"?|[^"[:space:]])+/);
-            plength=RLENGTH;
-            param=substr($0,pstart,plength);
-            raw=param;
-            gsub(/"/,"",raw);
-            if (raw !~ /^('"$option"')(=|$)/)
-                print param " ";
-            $0=substr($0,pstart+plength);
-        }
-        print "\n";
-    }'
-}
-
-#
-# Get the name of kernel parameter to limit CPUs
-# Linux 2.6.34+ has nr_cpus, older versions must use maxcpus
-function cpus_param()
-{
-    # nr_cpus does not work properly on ppc/ppc64/ppc64le
-    case "$(uname -m)" in
-        ppc*)
-	    echo maxcpus
-	    return 0
-	    ;;
-    esac
-
-    local version=$(get_kernel_version "$1")
-    version="${version%%-*}"
-    local verhex=$(IFS=. ; set -- $version ; printf "0x%02x%02x%02x" $1 $2 $3)
-    if [ $(( $verhex )) -ge $(( 0x020622 )) ] ; then
-	echo nr_cpus
-    else
-	echo maxcpus
-    fi
-}
-
-#
-# Builds the kdump command line from KDUMP_COMMANDLINE.
-function build_kdump_commandline()
-{
-    local kdump_kernel="$1"
-    local commandline="$KDUMP_COMMANDLINE"
-
-    if [ -z "$commandline" ]; then
-        local nr_cpus=$(cpus_param "$kdump_kernel")
-        commandline=$(
-            remove_from_commandline \
-                'root|resume|crashkernel|splash|mem|BOOT_IMAGE|showopts|zfcp\.allow_lun_scan|hugepages|acpi_no_memhotplug|cgroup_disable' \
-                < /proc/cmdline)
-        # Use deadline for saving the memory footprint
-        commandline="$commandline elevator=deadline sysrq=yes reset_devices acpi_no_memhotplug cgroup_disable=memory"
-        commandline="$commandline irqpoll ${nr_cpus}=${KDUMP_CPUS:-1}"
-        commandline="$commandline root=kdump"
-        local arch=$(uname -i)
-        case "$arch" in
-        i?86|x86_64)
-            local boot_apicid=$(
-                awk -F':[ \t]' '/^initial apicid[ \t]*:/ {print $2; exit}' \
-                    /proc/cpuinfo)
-            test -n "$boot_apicid" &&
-            commandline="$commandline disable_cpu_apicid=$boot_apicid"
-	    ;;
-	s390*)
-	    commandline="$commandline zfcp.allow_lun_scan=0"
-	    ;;
-        esac
-
-        # Xen does not include the kernel release in VMCOREINFO_XEN
-        # so we have to pass it via the command line
-        if [[ "$(uname -r)" =~ -xen$ ]] ; then
-            kernelrelease=$(uname -r)
-            commandline="$commandline kernelversion=$kernelrelease"
-        fi
-
-	if [ -f /sys/firmware/efi/systab ]
-	then
-		local acpi_addr
-		if grep -q '^ACPI20=' /sys/firmware/efi/systab
-		then
-			acpi_addr=$(awk -F'=' '/^ACPI20=/ {print $2}' /sys/firmware/efi/systab)
-		else
-			acpi_addr=$(awk -F'=' '/^ACPI=/ {print $2}' /sys/firmware/efi/systab)
-		fi
-		commandline="$commandline noefi acpi_rsdp=$acpi_addr"
-	fi
-
-	case `uname -i` in
-	i?86|x86_64)
-            commandline=$(echo "$commandline" |
-                remove_from_commandline 'unknown_nmi_panic')
-            ;;
-	esac
-    fi
-
-    commandline="$commandline $KDUMP_COMMANDLINE_APPEND"
-
-    # Add panic=1 unless there is a panic option already
-    local cmdline_nopanic
-    cmdline_nopanic=$(echo "$commandline" | remove_from_commandline panic)
-    if [ "$commandline" = "$cmdline_nopanic" ]
-    then
-	commandline="$commandline panic=1"
-    fi
-
-    echo "$commandline"
-}
-
-#
-# Builds the kexec options from KEXEC_OPTIONS
-# Parameters: 1) kernel
-function build_kexec_options()
-{
-    local kdump_kernel=$1
-    local options=$KEXEC_OPTIONS
-
-    # remove `--args-linux' for x86 type kernel files here
-    if [ "$(kdumptool identify_kernel -t "$kdump_kernel")" = "x86" ] ; then
-        options=$(echo $options | sed -e 's/--args-linux//g')
-    fi
-
-    # add --noio on ia64
-    if [ "$(uname -i)" = "ia64" ] ; then
-        options="$options --noio"
-    fi
-
-    echo "$options"
-}
-
-# Load kdump using kexec
-function load_kdump_kexec()
-{
-    if [ ! -f $kdump_initrd ]; then
-        echo -n ": No kdump initial ramdisk found."
-        echo "Tried to locate ${kdump_initrd}"
-        rc_status -s
-        rc_failed 6
-        rc_exit
-    fi
-
-    local kdump_commandline=$(build_kdump_commandline "$kdump_kernel")
-    local kexec_options=$(build_kexec_options "$kdump_kernel")
-
-    KEXEC_CALL="$KEXEC -p $kdump_kernel --append=\"$kdump_commandline\""
-    KEXEC_CALL="$KEXEC_CALL --initrd=$kdump_initrd $kexec_options"
-
-    result=$(eval "$KEXEC_CALL" 2>&1)
-    rc_check
-
-    # print stderr in any case to show warnings that normally
-    # would be supressed (bnc#374185)
-    echo "$result"
-
-    if rc_status ; then
-        echo 1 > /proc/sys/kernel/panic_on_oops
-    fi
-
-    if [ $(($KDUMP_VERBOSE & 4)) -gt 0 ] ; then
-        echo "Loading kdump kernel: $KEXEC_CALL"
-    fi
-    if [ $(($KDUMP_VERBOSE & 1)) -gt 0 ] ; then
-        if rc_status ; then
-            logger -i -t kdump "Loaded kdump kernel: $KEXEC_CALL, Result: $result"
-        else
-            logger -i -t kdump \
-                "FAILED to load kdump kernel: $KEXEC_CALL, Result: $result"
-        fi
-    fi
-
-}
-
-# Update the bootloader options for fadump
-function fadump_bootloader()
-{
-    case $(uname -m) in
-	ppc*) ;;
-	*) return 0 ;;
-    esac
-
-    local newstate="$1"
-
-    # check if the old configuration is still valid
-    boot_opts=$(kdump-bootloader.pl --get)
-    nofadump_opts=$(echo "$boot_opts" | remove_from_commandline 'fadump')
-    old_opts=$(kdumptool -F /dev/null -C <(echo "$boot_opts") \
-		dump_config --usage dump --format kernel --nodefault)
-    if [ "$newstate" = on ]; then
-	curr_opts=$(kdumptool dump_config --usage dump --format kernel --nodefault)
-	if [ "$old_opts" != "$curr_opts" -o \
-            "$boot_opts" = "$nofadump_opts" ]; then
-	    kdump-bootloader.pl --update fadump=on "$curr_opts"
-	fi
-    elif [ "$boot_opts" != "$nofadump_opts" ]; then
-	kdump-bootloader.pl --update
-    fi
-}
-
-# Update fadump configuration
-function load_kdump_fadump()
-{
-    fadump_bootloader on
-
-    if ! fadump_enabled; then
-        echo ": fadump is not enabled in the kernel."
-        rc_status -s
-        rc_failed 6
-        rc_exit
-    fi
-
-    # The kernel fails with EINVAL if registered already
-    # (see bnc#814780)
-    if [ "$(cat $FADUMP_REGISTERED)" != "1" ]; then
-	echo 1 > $FADUMP_REGISTERED
-    fi
-    echo 1 > /proc/sys/kernel/panic_on_oops
-    rc_reset
-}
-
-#
-# Load the kdump kerel specified in /etc/sysconfig/kdump
-function load_kdump()
-{
-    echo "Loading kdump"
-
-    if [ -f ${KDUMP_CONFIG} ] ; then
-        . ${KDUMP_CONFIG}
-    fi
-
-    local find_kernel_args=
-    if [ $(($KDUMP_VERBOSE & 16)) -gt 0 ] ; then
-        find_kernel_args="-D"
-    fi
-
-    local output=$(kdumptool $find_kernel_args find_kernel)
-    if [ $? -ne 0 ] ; then
-        rc_status -s
-        rc_failed 6
-        rc_exit
-    fi
-
-    kdump_kernel=$(echo "$output" | grep ^Kernel | cut -f 2)
-    kdump_initrd=$(echo "$output" | grep ^Initrd | cut -f 2)
-
-    # rebuild the kdump initramfs if necessary, but not at boot
-    if [ "$RUNLEVEL" != "B" ]; then
-        output=$(mkdumprd -K "$kdump_kernel" -I "$kdump_initrd" 2>&1)
-        if [ $? -ne 0 ] ; then
-	    echo "$output"
-	    rc_failed
-	    rc_status -v
-	    rc_exit
-        fi
-    fi
-
-    if [ "$KDUMP_FADUMP" = "yes" ]; then
-	load_kdump_fadump
-    else
-	load_kdump_kexec
-    fi
-
-    rc_status -v
-}
-
-# vim: set ts=4 sw=4 et:
--- /dev/null
+++ b/init/unload.sh
@@ -0,0 +1,24 @@
+#! /bin/bash
+#
+#  Copyright 2015 Petr Tesarik <ptesarik@suse.com>
+#  Unload the kdump kernel
+
+KDUMPTOOL=/usr/sbin/kdumptool
+KEXEC=/sbin/kexec
+FADUMP_REGISTERED=/sys/kernel/fadump_registered
+
+eval $($KDUMPTOOL dump_config)
+
+if [ "$KDUMP_FADUMP" = "yes" ]; then
+    # The kernel fails with EINVAL if unregistered already
+    # (see bnc#814780)
+    if [ $(cat "$FADUMP_REGISTERED") != "0" ] ; then
+	echo 0 > "$FADUMP_REGISTERED"
+    fi
+else
+    $KEXEC -p -u
+fi
+
+test $? -eq 0 || exit 1
+
+exit 0
