summaryrefslogtreecommitdiff
blob: 2a3b63ad5263d7eccea424028ffd8b1951c95c2b (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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/sbin/openrc-run
# Copyright 1999-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# Set up some defaults.
: "${LOAD_DURING_SHUTDOWN:=yes}"
: "${BOOTPART:=/boot}"
: "${DONT_MOUNT_BOOT:=no}"

depend() {
	need localmount
}

auto_prefix_bootpath() {
	# Only auto-add prefix to relative paths.
	case $1 in
		*/*) echo "$1";;
		*)   echo "${BOOTPART}/$1" ;;
	esac
}

get_genkernel_arch() {
	case $1 in
		x86_64) echo "amd64" ;;
		i[3456]86) echo "x86" ;;
		*) echo "$1" ;;
	esac
}

image_path() {
	# Do no sanity checking if the user has set a value.
	if [ -n "${KNAME}" ]; then
		auto_prefix_bootpath "${KNAME}"
		return
	fi

	local x kver="$(uname -r)" karch="$(uname -m)"
	local gkarch="$(get_genkernel_arch $karch)"
	for x in \
		"bzImage" \
		"vmlinux" \
		"vmlinuz" \
		"kernel" \
		"bzImage-${kver}" \
		"vmlinux-${kver}" \
		"vmlinuz-${kver}" \
		"kernel-genkernel-${karch}-${kver}" \
		"kernel-genkernel-${gkarch}-${kver}" \
		"kernel-${kver}" \
		"kernel-${karch}"; do
		if [ -e "${BOOTPART}/${x}" ]; then
			echo "${BOOTPART}/${x}"
			return
		fi
	done

	return 1
}

initrd_path() {
	# Do no sanity checking if the user has set a value.
	if [ -n "${INITRD}" ]; then
		auto_prefix_bootpath "${INITRD}"
		return 0
	fi

	local x kver="$(uname -r)" karch="$(uname -m)"
	local gkarch="$(get_genkernel_arch $karch)"
	for x in \
		"initrd" \
		"initramfs" \
		"initrd.img-${kver}" \
		"initrd-${kver}.img" \
		"initrd-${kver}" \
		"initramfs-${kver}.img" \
		"initramfs-genkernel-${karch}-${kver}" \
		"initramfs-genkernel-${gkarch}-${kver}"; do
		if [ -e "${BOOTPART}/${x}" ]; then
			echo "${BOOTPART}/${x}"
			return 0
		fi
	done

	return 1
}

mount_boot() {
	[ "${DONT_MOUNT_BOOT}" != "no" ] && return 1
	mountinfo -q "${BOOTPART}" && return 1

	ebegin "Mounting ${BOOTPART}"
	mount "${BOOTPART}"
	eend $?
}

load_image() {
	if [ "${KNAME}" = "-" ]; then
		ebegin "Disabling kexec"
		kexec -u
		eend $?
		return  # eend preserved $? for us.
	fi

	local img initrd="$(initrd_path)" mounted=false kparamopt initrdopt

	if ! img="$(image_path)"; then
		if mount_boot; then
			if img="$(image_path)"; then
				mounted=true
				initrd="$(initrd_path)"
			else
				eerror "No kernel image found in ${BOOTPART}!"
				umount "${BOOTPART}"
				return 1
			fi
		else
			eerror "No kernel image found in ${BOOTPART}!"
			return 1
		fi
	fi

	if [ -z "${ROOTPART}" ]; then
		ROOTPART="$(readlink -f "$(sed -n '/^\/[^ ]* \/ / s,^\([^ ]*\).*,\1,p' /proc/mounts)")"
	fi

	if [ -z "${KPARAM}" ]; then
		kparamopt="--reuse-cmdline"
	fi

	if [ -n "${initrd}" ]; then
		initrdopt="--initrd=${initrd}"
	fi

	local msg
	[ -n "${initrd}" ] && \
		msg=" (with ${initrd})"
	ebegin "Using kernel image ${img}${msg} for kexec"

	kexec ${KEXEC_OPT_ARGS} ${kparamopt} \
		-l "${img}" --append="root=${ROOTPART} ${KPARAM}" ${initrdopt}
	local ret=$?

	${mounted} && umount "${BOOTPART}"
	eend ${ret}
	return ${ret}
}

start() {
	if [ "${LOAD_DURING_SHUTDOWN}" = "yes" ]; then
		local mounted
		if mount_boot; then
			mounted=true
		fi
		if ! image_path >/dev/null; then
			ewarn "Cannot find kernel image!"
			ewarn "Please make sure a valid kernel image is present before reboot."
			return 0
		fi
		if [ -n "${mounted}" ]; then
			ebegin "Unmounting ${BOOTPART}"
			umount "${BOOTPART}"
			eend $?
		fi
		# $? is already set to the previous calls.
		return
	else
		load_image
	fi
}

stop() {
	if ! yesno ${RC_REBOOT}; then
		ebegin "Not rebooting; disabling kexec"
		kexec -u
		eend $?
		return
	fi

	if [ -f /nokexec ]; then
		ebegin "Rebooting; disabling kexec due to /nokexec"
		rm -f /nokexec
		kexec -u
		eend $?
		return
	fi

	if [ "${LOAD_DURING_SHUTDOWN}" = "yes" ]; then
		load_image
	fi
}