-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkinstall.sh
executable file
·402 lines (362 loc) · 8.38 KB
/
kinstall.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/bin/sh
# program to install a kernel
# Peter Hyman, pete@peterhyman.com
# No warranties, and free to use and copy
# utility functions
# will use getopt to determine build criteria
# and then order the options properly to produce
# kernel.
# set -x # for debugging
# save current directory
pushd $PWD
# return to original dir if needed
# exit with error code set
fini() {
popd
exit $1
}
error() {
echo "ERROR: $1"
fini 1
}
[ $UID -ne 0 ] && error "Must run as root to install a kernel."
# set constants for system and do some error checks
NICE=$(which nice 2>/dev/null)
[ -n $NICE ] && NICE=$(basename $NICE)
MAKE=$(which make 2>/dev/null)
[ $? -eq 1 ] && error "No Make command found...Exiting"
MAKE=$(basename $MAKE)
CCOMP=$(which gcc 2>/dev/null)
[ $? -eq 1 ] && error "No C compiler found...Exiting"
CCOMP=$(basename $CCOMP)
CXXCOMP=$(which g++ 2>/dev/null)
[ $? -eq 1 ] && error "No CPP compiler found...Exiting"
CXXCOMP=$(basename $CXXCOMP)
CCACHE=$(which ccache 2>/dev/null)
MAKEOPTS=-j8
MAKECMD="$NICE $MAKE"
MKINITRD=$(which mkinitrd 2>/dev/null)
[ $? -eq 1 ] && error "No Mkinitrd command found...Exiting"
MKINITRD=$(basename $MKINITRD)
# Set boot and EFI dirs
BOOTDIR="/boot"
EFIDIR="/boot/efi/EFI/Slackware"
# do some final kernel checks
[ ! -f .config ] && error "No .config file found. Run make oldconfig."
[ ! -f Makefile ] && error "SERIOUS ERROR: No Makefile file found."
[ ! -x $(which depmod) ] && error "depmod program not found. Using sudo?"
usage() {
echo "$(basename $0) usage
$(basename $0) [options]
none - do everything from make olddefconfig to mkinitrd
-k - use make kernelversion
-K - use make kernelrelease
-m - make all
-ma - make all
-mo - make oldconfig
-mO - make olddefconfig
-mr - make mrproper
-mc - make clean
-mv - make vmlinuz
-mb - make bzimage
-mm - make modules
-mM - make modules_install
-mh - make headers_install
-mi - make initrd
-C - perform files copy to boot
-E - perform copy files to EFIDIR
-L - symlink /usr/src to REALPWD
-h - show this help and exit"
return
}
# get options, taken from util-linux doc
TEMP=$(getopt -o 'kKm::CELh' -- "$@")
if [ $? -ne 0 ]; then
usage
fini 1
fi
# break up TEMP into positional parameters
eval set -- "$TEMP"
unset TEMP
getkv() {
FV=$(make kernelversion | tail -n1) || error "cannot fetch kernel version."
echo "Kernel Version is $FV"
# not used currently
# KV=`echo $FV | cut -d . -f 1`
# PL=`echo $FV | cut -d . -f 2`
# SL=`echo $FV | cut -d . -f 3`
}
getkr() {
FV=$(make kernelrelease | tail -n1) || error "cannot fetch kernel release."
echo "Kernel Release Version is $FV"
# not used currently
# KV=`echo $FV | cut -d . -f 1`
# PL=`echo $FV | cut -d . -f 2`
# SL=`echo $FV | cut -d . -f 3`
}
setupvars() {
# Set some directories and detect any links
# getkv or getkr must be called prior to get kernel version value $FV
[ -z "$FV" ] && getkv
REALPWDIR=`realpath .`
USRDIR="/usr"
USRSRCDIR="$USRDIR/src"
FVDIR="$USRSRCDIR/linux-$FV"
[ "$PWD" != "$REALPWDIR" ] && cd "$REALPWDIR"
}
makemrp() {
echo "performing make mrproper"
make mrproper || error "make mrproper failed."
}
makec() {
echo "performing make clean"
make clean
}
makeoc() {
echo "performing make oldconfig"
make oldconfig || error "make oldconfig failed."
}
makeodc() {
echo "performing make olddefconfig"
make olddefconfig || error "make olddefconfig failed."
}
makev() {
echo "performing make vmlinux"
if [ ! -n $CCACHE ]; then
$MAKECMD $MAKEOPTS vmlinux || error "make vmlinux failed."
else
$MAKECMD $MAKEOPTS CC="$CCACHE $CCOMP" CXX="$CCACHE $CXXCOMP" vmlinux || error "make vmlinux failed."
fi
}
makebz() {
echo "performing make bzimage"
if [ ! -n $CCACHE ]; then
$MAKECMD $MAKEOPTS bzImage || error "make bzimage failed."
else
$MAKECMD $MAKEOPTS CC="$CCACHE $CCOMP" CXX="$CCACHE $CXXCOMP" bzImage || error "make bzimage failed."
fi
}
makem() {
echo "performing make modules"
if [ ! -n $CCACHE ]; then
$MAKECMD $MAKEOPTS modules || error "make modules failed."
else
$MAKECMD $MAKEOPTS CC="$CCACHE $CCOMP" CXX="$CCACHE $CXXCOMP" modules || error "make modules failed."
fi
}
makemi() {
echo "performing make modules_install"
make modules_install || error "make modules_install failed."
}
makehi() {
echo "performing make headers_install"
make headers_install INSTALL_HDR_PATH="$USRDIR" || error "installing headers failed."
}
makeird() {
echo "Performing mkinitrd"
$MKINITRD -F /etc/mkinitrd.conf -k $FV -o /boot/initrd-$FV.gz
echo "Be sure to adjust and run lilo, or edit grub.cfg"
}
makeall() {
echo "performing make all"
makev
makebz
makem
makemi
makehi
makeird
}
copy2boot() {
echo "Copying core files to /boot"
cp -v .config /boot/config-$FV || error "copying config to boot"
cp -v System.map /boot/System.map-$FV || error "copying System.map to boot"
cp -v arch/x86/boot/bzImage /boot/vmlinuz-$FV || error "copying vmlinux to boot"
}
copy2efi() {
echo "Saving initrd and vmlunz in efi"
if [ -r "$EFIDIR"/initrd.gz ]; then
mv -v "$EFIDIR"/initrd.gz "$EFIDIR"/initrd-lastgood.gz
fi
if [ -r "$EFIDIR"/vmlinuz ]; then
mv -v "$EFIDIR"/vmlinuz "$EFIDIR"/vmlinuz-lastgood
fi
echo "Copying files to efi"
cp -v "$BOOTDIR"/initrd-$FV.gz "$EFIDIR"/initrd.gz
cp -v "$BOOTDIR"/vmlinuz-$FV "$EFIDIR"/vmlinuz
}
dolinks() {
# setup /usr/src directory as required
echo "Linking /usr/src/linux to $REALPWDIR"
if [ ! -e "$FVDIR" ]; then
# kernel source is somewhere else!
# link /usr/src/linux-$FV to REALPWDIR
ln -vfs "$REALPWDIR" "$FVDIR"
fi
# remove and recreate linux link to PWD
ln -vnfs "$FVDIR" /usr/src/linux
}
# set all variables to no
MAKEKV=no
MAKEKR=no
MAKEMRP=no
MAKEC=no
MAKEA=no
MAKEOC=no
MAKEODC=no
MAKEV=no
MAKEBZ=no
MAKEM=no
MAKEMI=no
MAKEHI=no
MAKEIRD=no
COPYB=no
COPYE=no
LINKSRC=no
# if last option is not "--" then go through all cases
if [ "$1" != "--" ]; then
while true; do
case "$1" in
'-k') # get kernel version
MAKEKV=yes
MAKEKR=no
MAKEA=no
shift
continue
;;
'-K') # get kernel release verion
MAKEKV=no
MAKEKR=yes
MAKEA=no
shift
continue
;;
'-m') # make all or make with specific options
case "$2" in
'' | 'a' )
MAKEA=yes
;;
'o') # oldconfig
MAKEODC=no
MAKEOC=yes
MAKEA=no
;;
'O') # olddefconfig
MAKEODC=yes
MAKEOC=no
MAKEA=no
;;
'r') # mrproper
MAKEMRP=yes
MAKEC=no
MAKEKV=no
MAKEKR=no
MAKEODC=no
MAKEOC=no
MAKEA=no
;;
'c') # clean
MAKEMRP=no
MAKEC=yes
MAKEKV=no
MAKEKR=no
MAKEODC=no
MAKEOC=no
MAKEA=no
;;
'v') # vmlinux
MAKEV=yes
MAKEA=no
;;
'b') # bzImage
MAKEBZ=yes
MAKEA=no
;;
'm') # modules
MAKEM=yes
MAKEA=no
;;
'M') # modules_install
MAKEMI=yes
MAKEA=no
;;
'h') # headers_install
MAKEHI=yes
MAKEA=no
;;
'i') # initrd
MAKEIRD=yes
MAKEA=no
;;
*) # oops
echo "Invalid Make Option $2"
usage
fini 1
;;
esac
shift 2
continue
;;
'-C') # copy to /boot
COPYB=yes
MAKEA=no
shift
continue
;;
'-E') # copy to efi partition
COPYE=yes
MAKEA=no
shift
continue
;;
'-L') # link /usr/src/linux to kernel dir
LINKSRC=yes
MAKEA=no
shift
continue
;;
'-h') # show help
usage
fini 0
;;
'--') # last option
shift
break
;;
*) # should never get here
echo "Unknown option $1"
usage
fini 1
;;
esac
done
elif [ "$1" == "--" ]; then # no arguments, set defaults
MAKEA=yes
MAKEODC=yes
MAKEKV=yes
shift
fi # if arguments
# it's still possible non options are present. We then have to stop as well
if [ "$1" ]; then
echout="$@"
error "Extraneous arguments: $echout"
fi
# now process everything in order optionally
# need kernel version first all the time
[ $MAKEKV == yes ] && getkv
[ $MAKEKR == yes ] && getkr
# if kernel version not set, it will be in setupvars
setupvars
[ $MAKEMRP == yes ] && makemrp
[ $MAKEODC == yes ] && makeodc
[ $MAKEOC == yes ] && makeoc
[ $MAKEC == yes ] && makec
[ $MAKEV == yes -o $MAKEA == yes ] && makev
[ $MAKEBZ == yes -o $MAKEA == yes ] && makebz
[ $MAKEM == yes -o $MAKEA == yes ] && makem
[ $MAKEMI == yes -o $MAKEA == yes ] && makemi
[ $MAKEHI == yes -o $MAKEA == yes ] && makehi
[ $MAKEIRD == yes -o $MAKEA == yes ] && makeird
[ $COPYB == yes -o $MAKEA == yes ] && copy2boot
[ $COPYE == yes -o $MAKEA == yes ] && copy2efi
[ $LINKSRC == yes -o $MAKEA == yes ] && dolinks
fini 0