#!/bin/bash

function error()
{
	echo "makedumpfile: ERROR: crash test: $@"
	exit 1
}

function report()
{
	echo "makedumpfile: crash test: $@"
}

function skip()
{
	echo "makedumpfile: SKIP: crash test: $@"
	exit 0
}

function wait_for_systemd_service()
{
	TIMEOUT=180
	while
		! systemctl is-active kdump-tools &&
		! systemctl is-failed kdump-tools &&
		[ $TIMEOUT -gt 0 ] ; do
			TIMEOUT=$((TIMEOUT - 1))
	done
}

function get_crash_kernel_size()
{
	if kexec --print-ckr-size > /dev/null 2> /dev/null ; then
		kexec --print-ckr-size
	elif [ -e /sys/kernel/kexec_crash_size ] ; then
		cat /sys/kernel/kexec_crash_size
	else
		echo 0
	fi
}

case "$AUTOPKGTEST_REBOOT_MARK" in
  "")
	report "rebooting to have crashkernel on cmdline"
	/tmp/autopkgtest-reboot crashkernel
	;;
  crashkernel)
	report "checking for crashkernel"
	if ! grep crashkernel /proc/cmdline > /dev/null; then
		error "no crashkernel option on cmdline"
	fi
	report "checking crash kernel size"
	crksize=`get_crash_kernel_size`
	if [ $crksize -eq 0 ]; then
		skip "crash kernel size is 0, possibly not enough memory"
	fi
	report "waiting for kdump-tools systemd service"
	wait_for_systemd_service
	if systemctl is-failed kdump-tools; then
		error "kdump-tools systemd service failed"
	fi
	report "checking kdump-config status"
	if ! kdump-config status | grep ": ready to kdump" > /dev/null; then
		error "kdump is not ready"
	fi
	/tmp/autopkgtest-reboot-prepare crash
	echo 1 > /proc/sys/kernel/sysrq
	echo c > /proc/sysrq-trigger
	;;
  crash)
	report "checking for crash file"
	found=false
	for i in `find /var/crash/ -name dump.*`; do
		if file $i | grep 'Kdump compressed dump' > /dev/null; then
			report "Found compressed dump at $i"
			found=true
		else
			error "$i is not a compressed dump"
		fi
	done
	if [ "$found" = "false" ]; then
		error "Found no compressed dumps"
	fi
	;;
esac
