#!/bin/sh
### BEGIN INIT INFO
# Provides:		kdump
# Required-Start:	$local_fs
# Required-Stop:	$local_fs
# Should-Start:
# Should-Stop:
# Default-Start:	0 1 2 3 4 5
# Default-Stop:		6
# Short-Description: Load crashkernel image with kexec
# Description:
### END INIT INFO

PATH=/usr/sbin:/usr/bin:/sbin:/bin

. /lib/lsb/init-functions

test -r /etc/default/kexec && . /etc/default/kexec

test -z "$KERNEL_IMAGE" && KERNEL_IMAGE="/boot/vmlinuz"
test -z "$INITRD" && INITRD="/initrd.img"

do_start () {
	test -x /sbin/kexec || exit 0

	# We have to be booted with crashkernel= to work. See
	# /boot/grub/menu.lst
	grep -q crashkernel= /proc/cmdline || exit 0

	# Remove crashkernel, vga, video and splash options
	APPEND=""
	for x in `cat /proc/cmdline`; do
		case $x in
			splash*|vga*|video*|crashkernel*)
				;;
			*)
				APPEND="$APPEND $x"
				;;
		esac
	done

	# Append kdump_needed for initramfs to know what to do, and add
	# maxcpus=1 to keep things sane.
	APPEND="$APPEND kdump_needed maxcpus=1 irqpoll elevator=deadline sysrq=1"

	# --elf32-core-headers is needed for 32-bit systems (ok
	# for 64-bit ones too).
	log_action_begin_msg "Loading crashkernel"
	kexec -p "$KERNEL_IMAGE" --initrd="$INITRD" --append="$APPEND" \
		--elf32-core-headers
	log_action_end_msg $?
}

case "$1" in
  start)
	do_start
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop)
	# No-op
	;;
  *)
	echo "Usage: $0 start|stop" >&2
	exit 3
	;;
esac
exit 0
