This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
installkernel
228 lines (190 loc) · 5.79 KB
/
installkernel
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
#
# Author: Todor Minchev <[email protected]>
# Modified-by: Miguel Bernal Marin <[email protected]>
#
# Copyright © 2014 Intel Corporation
#
# Kernel installer script - installs kernel, initrd, kernel config
# and system map and creates a bootloader entry for the new kernel
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Arguments:
# $1 - kernel version
# $2 - kernel image file
# $3 - kernel map file
# $4 - default install path (blank if root directory)
KERNEL_VER=$1
KERNEL_IMAGE=$2
KERNEL_MAP=$3
INSTALL_PATH=${4%/}
BOOTLOADER=""
BOOT_MOUNTED=0
declare -a BOOT_OPTIONS
ret=0
validate_install_path ()
{
if [ -n "$1" ]; then
if [ ! -d "$1" ]; then
echo -e "\n *** Invalid installation path *** : $1\n" 1>&2
exit 1
fi
fi
}
validate_kernel_version ()
{
if [ -z "$1" ]; then
echo -e "\n *** Kernel version not found *** \n" 1>&2
exit 1
fi
}
validate_kernel_image ()
{
if [ ! -f "$1" ]; then
echo -e "\n *** Kernel image not found *** \n" 1>&2
echo -e "\n *** Please compile your kernel and modules *** \n" 1>&2
exit 1
fi
}
validate_system_map ()
{
if [ ! -f "$1" ]; then
echo -e "\n *** Kernel map file not found *** \n" 1>&2
echo -e "\n *** Please compile your kernel *** \n" 1>&2
exit 1
fi
}
validate_modules_install ()
{
if [ ! -d "$1" ]; then
echo -e "\n *** Kernel modules not found *** : $1\n" 1>&2
echo -e "\n *** Please run 'make modules_install' first*** \n" 1>&2
exit 1
fi
}
setup_install ()
{
if [[ -f /etc/os-release ]]; then
. /etc/os-release
elif [[ -f /usr/lib/os-release ]]; then
. /usr/lib/os-release
fi
if ! [[ $PRETTY_NAME ]]; then
PRETTY_NAME="Linux $KERNEL_VERSION"
fi
if [[ -f /etc/kernel/cmdline ]]; then
read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
fi
if ! [[ ${BOOT_OPTIONS[*]} ]]; then
read -r -d '' -a line < /proc/cmdline
for i in "${line[@]}"; do
[[ "${i#initrd=*}" != "$i" ]] && continue
[[ "${i#BOOT_IMAGE=*}" != "$i" ]] && continue
BOOT_OPTIONS+=("$i")
done
fi
if ! [[ ${BOOT_OPTIONS[*]} ]]; then
echo "Could not determine the kernel command line parameters." >&2
echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2
exit 1
fi
cp "$KERNEL_IMAGE" "${INSTALL_PATH}/vmlinuz-${KERNEL_VER}" &&
chown root:root "${INSTALL_PATH}/vmlinuz-${KERNEL_VER}" &&
chmod 0644 "${INSTALL_PATH}/vmlinuz-${KERNEL_VER}" || {
echo "Could not copy '$KERNEL_IMAGE to '${INSTALL_PATH}/vmlinuz-${KERNEL_VER}'." >&2
exit 1
}
}
install_efi_kernel () {
LOADER_ENTRY="${INSTALL_PATH}/loader/entries/linux-${KERNEL_VER}.conf"
LOADER_CONF="${INSTALL_PATH}/loader/loader.conf"
setup_install
mkdir -p "${LOADER_ENTRY%/*}" || {
echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
exit 1
}
{
echo "title $PRETTY_NAME"
echo "version ${KERNEL_VER}"
echo "options ${BOOT_OPTIONS[*]}"
echo "linux /vmlinuz-${KERNEL_VER}"
} > "$LOADER_ENTRY" || {
echo "Could not create loader entry '$LOADER_ENTRY'." >&2
exit 1
}
# Add timeout to boot menu if needed
grep -q timeout ${LOADER_CONF} || echo "timeout 15" >> ${LOADER_CONF}
}
install_syslinux_kernel () {
LOADER_CONF="${INSTALL_PATH}/syslinux.cfg"
setup_install
{
echo "#"
echo "# Installed from sources"
echo "LABEL linux-${KERNEL_VER}"
echo " LINUX vmlinuz-${KERNEL_VER}"
echo " APPEND ${BOOT_OPTIONS[*]}"
echo "#"
} >> "${LOADER_CONF}" || {
echo "Cound not create loader entry " >&2
exit 1
}
# Add timeout to boot menu if needed
grep -q -i TIMEOUT ${LOADER_CONF} || echo "TIMEOUT 150" >> ${LOADER_CONF}
grep -q -i "PROMPT" ${LOADER_CONF} || echo "PROMPT 1" >> ${LOADER_CONF}
}
################ main ################
validate_kernel_version ${KERNEL_VER}
validate_kernel_image ${KERNEL_IMAGE}
validate_system_map ${KERNEL_MAP}
validate_install_path ${INSTALL_PATH}
KERNEL_MODULES=${INSTALL_PATH%/*}/usr/lib/modules/${KERNEL_VER}
validate_modules_install ${KERNEL_MODULES}
if [ -d ${INSTALL_PATH}/loader ] ; then
BOOTLOADER="systemd"
fi
if [ -f ${INSTALL_PATH}/syslinux.cfg ] ; then
BOOTLOADER="syslinux"
fi
if [ -z "${BOOTLOADER}" ] ; then
# Mount /boot partition
systemctl start boot.mount
((ret+=$?))
if [ ${ret} -eq 0 ] ; then
BOOT_MOUNTED=1
fi
if [ -d ${INSTALL_PATH}/loader ] ; then
BOOTLOADER="systemd"
fi
if [ -f ${INSTALL_PATH}/syslinux.cfg ] ; then
BOOTLOADER="syslinux"
fi
if [ -z "${BOOTLOADER}" ] ; then
echo "Cound not find boot loader" >&2
exit 1
fi
fi
if [ "${BOOTLOADER}" == "systemd" ] ; then
# Based on systemd kernel-install plugin script
install_efi_kernel
((ret+=$?))
else
install_syslinux_kernel
((ret+=$?))
fi
if [ ${BOOT_MOUNTED} -eq 1 ] ; then
# Unmount /boot partition
systemctl stop boot.mount
((ret+=$?))
fi
exit $ret