/
slack_kernel.sh
executable file
·216 lines (199 loc) · 5.29 KB
/
slack_kernel.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
#!/bin/bash
# Script to get, build and install kernels in Slackware Linux
# Loosely based upon the steps specified in http://docs.slackware.com/howtos:slackware_admin:kernelbuilding
# functions
check_version() {
if [ -z "$VERSION" ]; then
echo "No version specified."
echo "Kernel version can be specified using the -k argument."
exit 1
fi
}
init() {
check_version
#ARCH=$(uname -a)
local numjobs
numjobs=$(nproc)
# avoid using full system resources
[ "${numjobs}" -gt 1 ] && numjobs=$(( (numjobs * 3) / 4 ))
NUMJOBS=${NUMJOBS:--j${numjobs}}
# Get base version of kernel, ie, for 3.10.17, base version is 3.10
BASEVER=${VERSION%.*}
# Get really base version, like for 3.10.17, it is 3
BASEBASEVER=${VERSION%%.*}
# Check for something like 4.0
[ "$BASEVER" = "$BASEBASEVER" ] && BASEVER=${VERSION%.*.*}
}
change_dir() {
check_version
local cwd
cwd=$(basename "$(pwd)")
[ ! "$cwd" = "linux-$VERSION" ] && cd "linux-$VERSION"
}
get() {
check_version
# Download the base kernel
wget -Nc "https://www.kernel.org/pub/linux/kernel/v${BASEBASEVER}.x/linux-$BASEVER.tar.xz"
# Download patch
if [ ! "$VERSION" = "$BASEVER" ]; then
wget -Nc "https://www.kernel.org/pub/linux/kernel/v${BASEBASEVER}.x/patch-$VERSION.xz"
fi
}
extract_release() {
# Check if kernel directory is available
if [ -d "linux-$VERSION" ]; then
return 0
fi
if [ -d "linux-$BASEVER" ]; then
return 0
# Else extract the base version
elif [ -f "linux-${BASEVER}.tar.xz" ]; then # || [ -f "linux-$BASEVER.tar.gz" ] || [ -f "linux-$BASEVER.tar.bz2" ]
tar xvf "linux-${BASEVER}.tar.xz" || return 1
else
echo "Unable to extract linux-${BASEVER}.tar.xz"
return 1
fi
}
extract_patch() {
# Need to check if patch for specified version is available
if [ -e "patch-$VERSION.xz" ]; then
# Extract the patch
unxz "patch-$VERSION.xz"
elif [ -f "patch-$VERSION.gz" ]; then
"gunzip patch-$VERSION.gz"
else
return 1
fi
}
apply_patch() {
# Now check and apply patch
if [ -e "patch-$VERSION" ]; then
# Change into the src directory and apply the patch
cd "linux-$BASEVER" && patch -p1 < ../"patch-$VERSION" || exit 1
# Change name of src directory so state can be verified
cd .. && mv -v "linux-$BASEVER" "linux-$VERSION"
fi
# Apply any other patches present
if [ -d "patches/linux-${BASEVER}" ]; then
echo "Applying custom linux-${BASEVER} patches..."
cd "linux-${VERSION}" || exit 1
for file in ../patches/linux-"${BASEVER}"/*; do
patch -p1 < "${file}" || exit 1
done
cd ..
fi
}
extract(){
check_version
extract_release
extract_patch
apply_patch
}
config() {
# change to proper directory
change_dir
# copy the config
local config_arch config_file
[ "$(uname -m)" = x86_64 ] && config_arch=.x64
for file in ../config-custom-${VERSION}${config_arch} \
../config-custom-${BASEVER}*${config_arch} \
../config-generic-${VERSION}${config_arch} \
../config-generic-${BASEVER}*${config_arch}; do
config_file=${file}
done
cp -iv "$config_file" .config || exit 1
# Config handling is manual
make oldconfig
# Custom config too
make menuconfig
}
build() {
# change to proper directory
change_dir
# Build the kernel
make "$NUMJOBS" bzImage
# Build the modules
make "$NUMJOBS" modules
}
install() {
check_version
# change to proper directory
change_dir
echo "Installing kernel ${VERSION}"
# Install the modules
sudo make modules_install || exit 1
# Copy the built kernel and configs
sudo cp -v arch/x86/boot/bzImage "/boot/vmlinuz-custom-$VERSION" || exit 1
sudo cp -v System.map "/boot/System.map-custom-$VERSION" || exit 1
sudo cp -v .config "/boot/config-custom-$VERSION" || exit 1
}
remove() {
check_version
echo "Removing kernel ${VERSION}"
if [ "$(uname -r)" = "${VERSION}" ]; then
echo "You are about to remove the running kernel, are you sure you want to continue? [y/n]"
local choice
read -r -e choice
if [ ! "${choice}" = y ] && [ ! "${choice}" = Y ]; then
return 1
fi
fi
sudo rm -rv "/lib/modules/$VERSION" || exit 1
sudo rm -v "/boot/vmlinuz-custom-$VERSION" || exit 1
sudo rm -v "/boot/System.map-custom-$VERSION" || exit 1
sudo rm -v "/boot/config-custom-$VERSION" || exit 1
}
post_install() {
# Update bootloader
echo 'Updating grub configurartion'
sudo grub-mkconfig -o /boot/grub/grub.cfg
}
clean() {
check_version
# Revert any other patches present
if [ -d "patches/linux-${BASEVER}" ]; then
echo "Reverting custom linux-${BASEVER} patches..."
cd "linux-${VERSION}"
local files=()
for file in ../patches/linux-${BASEVER}/*; do
files+=($file)
done
for ((i=${#files[@]}-1; i>=0; i--)); do
patch -R -p1 < "${files[$i]}" || exit 1
done
cd ..
fi
# change to proper directory
change_dir
# Unpatch and move the directory to linux-basever
patch -R -p1 < ../"patch-$VERSION" || exit 1
cd .. && mv -v "linux-$VERSION" "linux-$BASEVER"
}
display_help() {
cat << EOF
Usage: slack_kernel.sh -k <kernel> <option>
Options-
[get,-g] [clean,-c] [extract,-e]
[config,-m] [build,-b] [install,-i]
[posinstall,-p] [remove,-r] [all(-gembip),-a]
[help,-h]
EOF
}
# process cmd line args
while getopts "gcembiprahk:" opt; do
case "$opt" in
g) get;;
c) clean;;
e) extract;;
m) config;;
b) build;;
i) install;;
p) post_install;;
r) remove;;
a) get; extract; config; build; install; post_install;;
h) display_help ;;
k) VERSION=$OPTARG; init;;
*) ;;
esac
done
#echo "Done"