-
Notifications
You must be signed in to change notification settings - Fork 7
/
custom-kernel-install
executable file
·105 lines (89 loc) · 2.64 KB
/
custom-kernel-install
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
#!/bin/bash
set -e
FILE=
KVER=
HAVE_MODULES=
SCRIPT=$(realpath ${BASH_SOURCE[0]})
usage() {
echo "
Usage: $(basename $0) [OPTIONS]
Unpack a kernel and install on this board board
OPTIONS
-c [J|z|zstd] pass flag to tar while unpacking
" 1>&$1;
exit $(($1 - 1));
}
OPT_COMPRESS="zstd"
EXT=""
args=0
while getopts "ch" o; do
case "${o}" in
c) OPT_COMPRESS=${OPTARG}; args=$[$args + 2] ;;
h) usage 1;;
\?) usage 2;;
esac
done
shift $args
case "$OPT_COMPRESS" in
J) EXT="tar.xz"; OPT_COMPRESS=-J;;
z) EXT="tar.gz"; OPT_COMPRESS=-z;;
zstd) EXT="tar.zstd"; OPT_COMPRESS="-I zstd";;
"") EXT=".tar";;
\?) usage 2;;
esac
installkernel=$(which installkernel || true)
kernel_install=$(which kernel-install || true)
if [ $# -gt 0 ]; then
FILE=$1
KVER=$(basename $FILE)
KVER=${KVER/linux-}
KVER=${KVER//${EXT}}
HAVE_MODULES=$(tar -tf $FILE ${OPT_COMPRESS} usr/lib/modules/$KVER | head -n 1)
else
FILE=$SCRIPT
KVER=$(basename $FILE)
KVER=${KVER/linux-}
KVER=${KVER//.sh}
KVER=${KVER//${EXT}}
# check if we have embedded custom kernel
if grep --quiet -e "^__CUSTOM_KERNEL_BELLOW__" ${SCRIPT}; then
echo "Found kernel $KVER embedded in script, extracting..." >&2
HAVE_MODULES=$(sed -e '1,/^__CUSTOM_KERNEL_BELLOW__/d' ${SCRIPT} | \
tar -t ${OPT_COMPRESS} usr/lib/modules/$KVER | head -n 1)
fi
fi
# sanity check if file has that module directory
if [ -z "$HAVE_MODULES" ]; then
echo "wrong file name or kernel doesn't have any modules directory" >&2
exit 1
fi
# remove previous modules
rm -rf /usr/lib/modules/$KVER
if [ ! -z "$kernel_install" ]; then
# remove previous kernel
kernel-install remove $KVER
fi
# install kernel and modules on /usr/lib
if [[ ! "$FILE" = "$SCRIPT" ]]; then
tar -C / -mxf $FILE ${OPT_COMPRESS}
else
sed -e '1,/^__CUSTOM_KERNEL_BELLOW__/d' ${SCRIPT} | \
tar -C / -mx ${OPT_COMPRESS}
fi
# target didn't adhere to the /usr move
if [ "$(realpath /lib/modules)" != "/usr/lib/modules" ]; then
rm -rf /lib/modules/$KVER
mv /usr/lib/modules/$KVER /lib/modules/$KVER
fi
# copy kernel to boot partition, install bootloader entry and possibly
# generate an initrd
if [ ! -z "$installkernel" ]; then
$installkernel $KVER /usr/lib/custom-kernel/{vmlinuz,System.map}
elif [ ! -z "$kernel_install" ]; then
$kernel_install add $KVER /usr/lib/custom-kernel/vmlinuz
else
echo "Warning: neither kernel-install or installkernel available to install a kernel" >&2
echo "Just going to install the kernel, bootloader config is on you" >&2
cp /usr/lib/custom-kernel/vmlinuz /boot/${2:-vmlinuz-test}
fi
echo "Done."