summaryrefslogtreecommitdiff
path: root/sys-apps/kexec-tools/files/kexec-auto-load
blob: 62c828fd1c3d35cd84fbed6caee6d656ddc54653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env bash

# Defaults
LAYOUT=compat
BOOTPART=/boot
KNAME=kernel
INITRD=initramfs.img

instkern_state=/var/lib/misc/installkernel
if [[ -s ${instkern_state} ]]; then
	# If we have a log file, set defaults from there.
	IFS=$'\t' read -r -a LastKernArray <<< "$(tail -n1 ${instkern_state})"
	LAYOUT="${LastKernArray[4]}"
	BOOTPART="${LastKernArray[7]}"
	KNAME="${LastKernArray[8]}"
	if [[ "${LastKernArray[9]}" != unknown && ${LAYOUT} != uki ]]; then
		INITRD="${LastKernArray[9]}"
	else
		INITRD=
	fi
fi

if [[ ${LAYOUT} == uki ]]; then
	echo "WARNING: kexec currently does not support UKIs"
	KPARAM=
else
	if [[ -f /etc/kernel/cmdline ]]; then
		KPARAM="$(tr -s "${IFS}" ' ' </etc/kernel/cmdline)"
	elif [[ -f /usr/lib/kernel/cmdline ]]; then
		KPARAM="$(tr -s "${IFS}" ' ' </usr/lib/kernel/cmdline)"
	else
		KPARAM=
	fi
fi


# /etc/kexec.conf overrides installkernel.log
kexec_conf=/etc/kexec.conf
if [[ -f ${kexec_conf} ]]; then
	source ${kexec_conf}
fi

if [[ -z ${DONT_MOUNT_BOOT} ]]; then
	# Borrowed from mount-boot-utils.eclass
	# note that /dev/BOOT is in the Gentoo default /etc/fstab file
	fstabstate=$(awk "!/^[[:blank:]]*#|^\/dev\/BOOT/ && \$2 == \"${BOOTPART}\" \
		{ print 1; exit }" /etc/fstab || die "awk failed")

	if [[ -z ${fstabstate} ]]; then
		echo "Assuming you do not have a separate ${BOOTPART} partition."
	else
		procstate=$(awk "\$2 == \"${BOOTPART}\" { split(\$4, a, \",\"); \
			for (i in a) if (a[i] ~ /^r[ow]\$/) { print a[i]; break }; exit }" \
			/proc/mounts || die "awk failed")

		if [[ -z ${procstate} ]]; then
			echo "ERROR: Your ${BOOTPART} partition is not mounted"
			exit 1
		fi
	fi
fi

if [[ ! -d ${BOOTPART} ]]; then
	echo "ERROR: BOOTPART=${BOOTPART} not found"
	exit 1
fi

KEXEC_ARGS=()

if [[ -f ${BOOTPART}/${KNAME} ]]; then
	KEXEC_ARGS+=( --load "${BOOTPART}/${KNAME}" )
else
	echo "ERROR: KNAME=${KNAME} not found"
	exit 1
fi

if [[ -n ${INITRD} ]]; then
	if [[ -f ${BOOTPART}/${INITRD} ]]; then
		KEXEC_ARGS+=( --initrd "${BOOTPART}/${INITRD}" )
	else
		echo "WARNING: INITRD=${INITRD} not found"
	fi
fi

if [[ -n ${KPARAM} ]]; then
	KEXEC_ARGS+=(  --command-line "${KPARAM}" )
elif [[ ! -f /etc/kernel/cmdline && ! -f /usr/lib/kernel/cmdline ]]; then
	# If it is still empty and we did not intentionally set it empty then reuse.
	KEXEC_OPT_ARGS+=" --reuse-cmdline "
fi

KEXEC_ARGS+=( ${KEXEC_OPT_ARGS} )

echo "Calling kexec with arguments: ${KEXEC_ARGS[@]}"
exec kexec "${KEXEC_ARGS[@]}"