-
Notifications
You must be signed in to change notification settings - Fork 0
/
k3rn3l.sh
executable file
·300 lines (232 loc) · 7.39 KB
/
k3rn3l.sh
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env bash
# constants
readonly SCRIPT_VERSION="1.2.0"
# global stuff - those can be overridden via ENV vars
GRUB_CMD=grub-mkconfig
KERNEL_SOURCE_DIRECTORY="/usr/src/"
verbose=
quiet=
# default log output
# deactivated when quiet=1
function l {
[[ -n "$quiet" ]] || echo -e "$*"
}
# log if verbose=1
function v {
[[ -n "$verbose" ]] && l "$*"
}
# log errors
# errors go to STDERR and always log, even if quiet=1
function e {
(>&2 echo -e "\\e[31m$*\\e[0m")
}
function die {
e "$@"
exit 1
}
# check if string $1 begins with string $2
function beginsWith {
case "$1" in
"$2"*) true ;;
*) false ;;
esac
}
function requirements() {
v 'Checking script requirements...'
[[ -e '/etc/gentoo-release' ]] || die 'This script supports only gentoo'
[[ "$EUID" -eq 0 ]] || die 'This script needs to be run as root'
( $GRUB_CMD --version &> /dev/null ) || die "$GRUB_CMD not found"
}
function usage {
cat <<EOF
k3rn3l <command> [options]
Gentoo linux kernel management.
available commands:
clean
Cleanup older kernel versions.
You should run emerge --depclean first, though it is not required.
This deletes all kernel files for kernels older than the current one, so
ensure that the current kernel boots correctly.
These occurrences are cleaned:
- kernel source directories in /usr/src/
- compiled kernel in /boot/
- kernel config and system.map in /boot/
recompile
Recompile the current kernel.
Recompiles the current kernel from source and updates the boot loader.
This is useful when you changed the kernel configuration.
update
Update to the newest kernel version
Update to the newest installed kernel version. The kernel compilation
tries to use old kernel config as far as possible, asking for changed or
new kernel configuration options (make oldconfig)
available options:
-p, --pretend Do a dry-run, don't actually do anything harmful
-v, --verbose Verbose output
-q, --quiet Less output
-h, --help Show this help screen
--version Show the script version
EOF
exit 0
}
function parseArgs {
while [[ $# -gt 0 ]]; do
case $1 in
-p|--pretend)
dryRun="echo"
;;
-s|--skip-requirements)
skipRequirements=1
;;
-v|--verbose)
verbose=1
;;
-q|--quiet)
quiet=1
;;
-h|--help)
usage
;;
--version)
echo "$SCRIPT_VERSION"
exit 0
;;
*)
if beginsWith "$1" "-"; then
die "Unknown option '$1'"
fi
if [[ -z "$cmd" ]]; then
cmd="$1"
else
die "argument is not allowed here: $1"
fi
;;
esac
shift
done
}
function detectCurrentKernel {
local runningKernel eselectKernel
runningKernel=$(uname -r)
eselectKernel=$(eselect kernel list | grep '\*$' | awk '{print $2}')
[[ "$eselectKernel" == "linux-$runningKernel" ]] \
|| die "Please reboot to the current kernel before issuing clean"
echo "$eselectKernel"
}
function mountBoot {
if ! findmnt /boot &> /dev/null ; then
l "/boot not mounted, mounting..."
mount /boot || die "Failed to mount /boot"
fi
}
function clean {
local currentKernel currentVersion removalList unmergeCmd
removalList=()
currentKernel=$(detectCurrentKernel)
currentVersion=$(echo "$currentKernel" | cut -d '-' -f 1 --complement)
l "Current kernel: $currentKernel"
l "\\nUnmerging old kernel slots..."
unmergeCmd="emerge --depclean gentoo-sources -a"
if [[ -n "$dryRun" ]]; then
unmergeCmd="$unmergeCmd -p"
fi
$unmergeCmd || die 'unmerging failed!'
l "\\nCleaning kernel sources..."
for directory in "$KERNEL_SOURCE_DIRECTORY"* ; do
if [[ "$directory" == "$KERNEL_SOURCE_DIRECTORY$currentKernel" \
|| "$directory" == "${KERNEL_SOURCE_DIRECTORY}linux" ]]; then
l "Skipping current kernel directory \\e[32m$directory\\e[0m"
continue;
fi
l "Slating for removal: \\e[31m$directory\\e[0m"
removalList+=("$directory")
done
l "\\nCleaning /boot/ ..."
mountBoot
for bootFile in /boot/vmlinuz-* /boot/config-* /boot/System.map-* ; do
if [[ "$(basename "$bootFile" | cut -d '-' -f 1 --complement)" \
== "$currentVersion" ]]; then
l "Skipping current kernel file \\e[32m$bootFile\\e[0m"
continue
fi
l "Slating for removal: \\e[31m$bootFile\\e[0m"
removalList+=("$bootFile")
done
l ""
if [[ ${#removalList[@]} -eq 0 ]]; then
l "Nothing to delete"
exit 0
fi
if [[ -z "$dryRun" ]]; then
# shellcheck disable=SC2145
v "About to execute: rm -rf ${removalList[@]}"
read -r -p "Are you sure? [y/N] " response
if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
l 'User aborted'
exit 0
fi
fi
l "Deleting..."
$dryRun rm -rf "${removalList[@]}"
l 'Updating grub config...'
$dryRun $GRUB_CMD -o /boot/grub/grub.cfg
}
function recompile {
cd "${KERNEL_SOURCE_DIRECTORY}linux" || die "Unable to change to kernel dir"
mountBoot
l 'Starting kernel compilation...'
if ! ( make -j "$(nproc)" && make modules_install && make install ) ; then
die 'Kernel compilation failed'
fi
l 'Updating grub config...'
$dryRun $GRUB_CMD -o /boot/grub/grub.cfg
}
function update {
local oldKernel newKernel
l 'Updating kernel...'
v 'Determining kernel versions...'
oldKernel=$(eselect kernel list | grep '\*' | awk '{print $2}')
newKernel=$(eselect kernel list | tail -n 1 | awk '{print $2}')
if [[ -z "$oldKernel" ]]; then
die 'Could not determine kernel versions'
fi
[[ "$oldKernel" != "$newKernel" ]] || die 'Only one kernel version installed'
l "Switching from $oldKernel to $newKernel"
( $dryRun eselect kernel set "$newKernel" ) \
|| die "Switching to new kernel failed"
cd "${KERNEL_SOURCE_DIRECTORY}linux" || die "Unable to change to kernel dir"
v 'Copying old config...'
[[ -e "../$oldKernel/.config" ]] \
|| die "Error: No .config present for $oldKernel"
cp "../$oldKernel/.config" ./
mountBoot
l 'Starting kernel compilation...'
if ! ( make oldconfig && make -j "$(nproc)" && $dryRun make modules_install && $dryRun make install ) ; then
die 'Kernel compilation failed'
fi
l 'Rebuilding kernel modules...'
( $dryRun emerge -q @module-rebuild ) || die 'module-rebuild failed'
l 'Updating grub config...'
$dryRun $GRUB_CMD -o /boot/grub/grub.cfg
}
function main {
local dryRun skipRequirements cmd
parseArgs "$@"
[[ $skipRequirements ]] || requirements
v "Executing command '$cmd'"
case "$cmd" in
clean|recompile|update)
$cmd
;;
help)
usage
;;
*)
die "Unknown command given: '$cmd'"
;;
esac
exit 0
}
if [[ "$0" != *bats* ]]; then
main "$@"
fi