#!/bin/bash
#
# Copyright IBM Corp. 2021
#
# s390-tools is free software; you can redistribute it and/or modify
# it under the terms of the MIT license. See LICENSE for details.
#

set -u

DRACUT_OMIT_MODULES=(
	nfs network network-manager
	dbus dbus-broker dbus-daemon plymouth
	znet dash resume ifcfg earlykdump
	kdumpcore i18n lvm mdraid memstrack
	systemd-timedated systemd-timesyncd
	systemd-networkd systemd-network-manager systemd-coredump)

function create_dumpfs()
{
	[ "$dryrun" -eq 1 ] && { echo "/dev/null"; return 0; }
	mkfs.ext4 -F "$device" >/dev/null || { echo "Couldn't create filesystem on $1." >&2; return 1; }
	local disk_path=/dev/disk/by-uuid/$(blkid -o value -s UUID "$device")
	for i in $(seq 0 30)
	do
		[ -e "$disk_path" ] && break;
		sleep 1
	done

	[ -e "$disk_path" ] || { echo "Filesystem device link missing." >&2; return 1; }
	echo "$disk_path"
}

function get_kernel_version()
{
	local version=$(uname -r)

	echo $version
}

function get_kernel_image()
{
	local version=$1

	if [ -e "/boot/image-$version" ]; then
		# SLES kernel image
		echo "/boot/image-$version"
	else
		# Fedora/RHEL kernel image
		echo "/boot/vmlinuz-$version"
	fi
}

#
# dracut is the standard tool for building initramfs images
# on Fedora, RHEL, and SLES.
#
function create_initrd()
{
	local disk=$1
	local kver=$2
	local initrd=$3
	local omit_modules=$(echo ${DRACUT_OMIT_MODULES[@]})

	command -v dracut >/dev/null ||
		{ echo "Please install dracut." >&2; return 1; }

	dracut -v --hostonly --no-hostonly-cmdline --hostonly-i18n \
	       -o "$omit_modules" --add ngdump \
	       --mount "$disk /ngdump ext4 defaults" \
	       --kver "$kver" -f "$initrd" 2>&1
}

[ $# -gt 1 ] || { echo "Usage: $0 <dump partition device> <dryrun>" >&2; exit 1; }

dasd=
device=$(readlink -f $1)
dryrun=$2
type=$(lsblk -dno type "$device")
[ "$type" != "part" ] && { echo "$device with type $type is not a partition." >&2; exit 1; }

case "${device##*/}" in
	dasd*)
		dasd=$(dasdview -i "$1"|sed -n 's/busid\s*:\s*\([0-9a-fA-F.]\)/dasd=\1/p')
		;;
	nvme*)
		;;
	*)
		echo "Unsupported device $device." >&2
		exit 1
		;;
esac

disk_path=$(create_dumpfs $device)
kernel_version=$(get_kernel_version)
kernel=$(get_kernel_image "$kernel_version")
[ -e "$kernel" ] || { echo "Couldn't find kernel image." >&2; exit 1; }

initrd=$(mktemp)
create_initrd "$disk_path" "$kernel_version" "$initrd"
[ $? -eq 0 ] || { echo "Failed to create initramfs image" >&2; exit 1; }

#
# zipl expects this script to output three variables to stdout:
#  * kernel: path to bzImage file of Linux kernel
#  * initrd: path to dumper initramfs image
#  * cmdline: command-line for dumper's kernel
#
# The output order is irrelevant.
#

echo "kernel=$kernel"
echo "initrd=$initrd"
echo "cmdline=reset_devices cgroup_disable=memory nokaslr numa=off irqpoll nr_cpus=1 $dasd"

exit 0
