File: kexec-load.init.d

package info (click to toggle)
kexec-tools 1%3A2.0.20-2.1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 2,828 kB
  • sloc: ansic: 27,201; sh: 3,486; asm: 2,625; cpp: 1,752; makefile: 930
file content (129 lines) | stat: -rw-r--r-- 3,323 bytes parent folder | download | duplicates (3)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#! /bin/sh
### BEGIN INIT INFO
# Provides:		kexec-load
# Required-Start:	$local_fs $remote_fs kexec
# Required-Stop:	$local_fs $remote_fs kexec
# Should-Stop:		autofs
# Default-Start:	2 3 4 5
# Default-Stop:		6
# Short-Description: Load kernel image with kexec
# Description:
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NOKEXECFILE=/no-kexec-reboot

. /lib/lsb/init-functions

test -r /etc/default/kexec && . /etc/default/kexec
if [ -d /etc/default/kexec.d ] ; then
	for snippet in $(run-parts --list /etc/default/kexec.d) ; do
		. "$snippet"
	done
fi

process_grub_entry() {
	initrd_image=
	while read command args; do
		if [ "$command" = "linux" ]; then
			echo "$args" | while read kernel append; do
			echo KERNEL_IMAGE=\"${prefix}${kernel}\"
			echo APPEND=\"${append}\"
			done
		elif [ "$command" = "initrd" ]; then
			initrd_image=${prefix}${args}
		fi
	done
	echo INITRD=\"$initrd_image\"
}

get_grub_kernel() {
	test -f /boot/grub/grub.cfg || return
	local prefix
	mountpoint -q /boot && prefix=/boot || prefix=
	data=$(cat /boot/grub/grub.cfg)

	default=$(echo "$data" | awk '/set default/ {print $2}' | cut -d'"' -f2 | tail -1)
	if [ "$default" = '${saved_entry}' ]; then 
		default=$(sed -ne 's/^saved_entry=//p' /boot/grub/grubenv)
	fi
	if [ -z "$default" ]; then
		default=0
	fi
	start_offset=$((default + 1))
	end_offset=$((default + 2))

	# grub entries start with "menuentry" commands.  Get the line 
	# numbers that surround the first entry
	offsets=$(echo "$data" | grep -n '^[[:space:]]*menuentry[[:space:]]' | cut -d: -f1)
	begin=$(echo "$offsets" | tail -n+$start_offset | head -n1)
	end=$(echo "$offsets" | tail -n+$end_offset | head -n1)

	# If this is the last entry, we need to read to the end of the file
	# or to the end of boot entry section
	if [ -z "$end" ]; then
		numlines=$(echo "$data" | tail --lines=+$begin | grep -n "^### END" | head -1 | cut -d: -f1)
		end=$((begin + numlines - 1))
	fi

	length=$((end - begin))
	entry=$(echo "$data" | tail -n+$begin | head -n$length)
	eval $(echo "$entry" | process_grub_entry)
}

do_stop () {
	test "$LOAD_KEXEC" = "true" || exit 0
	test -x /sbin/kexec || exit 0
	test "x`cat /sys/kernel/kexec_loaded`y" = "x1y" && exit 0

	if [ -f $NOKEXECFILE ]
	then
		/bin/rm -f $NOKEXECFILE
		exit 0
	fi

	test "$USE_GRUB_CONFIG" = "true" && get_grub_kernel

	REAL_APPEND="$APPEND"

	test -z "$REAL_APPEND" && REAL_APPEND="`cat /proc/cmdline`"
	log_action_begin_msg "Loading new kernel image into memory"
	if [ -z "$INITRD" ]
	then
		/sbin/kexec -l "$KERNEL_IMAGE" --append="$REAL_APPEND"
	else
		/sbin/kexec -l "$KERNEL_IMAGE" --initrd="$INITRD" --append="$REAL_APPEND"
	fi
	log_action_end_msg $?
}

case "$1" in
  start)
	# No-op
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop)
	# If running systemd, we want kexec reboot only if current
	# command is reboot. If not running systemd, check if
	# runlevel has been changed to 6 which indicates we are
	# rebooting
	if [ -d /run/systemd/system ]; then
		systemctl list-jobs systemd-kexec.service | grep -q systemd-kexec.service
		if [ $? -ne 0 ]; then
			exit 0
		fi
	else if [ -x /sbin/runlevel -a "$(runlevel | awk '{ print $2 }')" != "6" ]; then 
			exit 0
		fi
	fi
	do_stop
	;;
  *)
	echo "Usage: $0 start|stop" >&2
	exit 3
	;;
esac
exit 0