-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbb-get-rcn-kernel-source.sh
executable file
·206 lines (161 loc) · 5.65 KB
/
bb-get-rcn-kernel-source.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
#!/bin/sh
#
# This script downloads, patches and prepares the kernel sources for BeagleBone
# (Black) kernels distributed by http://rcn-ee.net in order to enable you to
# compile kernel modules right on the BeagleBone: After installing the kernel
# sources with this script, you should be able to build kernel modules via
# their respective makefile.
#
# Note that this will only work for those kernels for which rcn-ee has a
# kernel-headers package as well – This should be the case for most older
# and some newer kernels, but there was a time where the kernel-headers package
# was missing from the distributions. The script will notify you if you want
# to run it for a kernel for which no kernel-headers package is available.
#
# By default, the script will prepare the sources for the currently running
# kernel. However, you can also specify a different kernel version as the
# first script argument.
#
# Oh, and also ensure that you have all the necessary dependencies installed,
# since the script doesn't check: gcc, make and all the usual suspects for
# building kernel-related things.
#
# The script tries to determine your linux distribution automatically, but
# if that fails or you want to manually specify one, edit the DIST variable
# at the top of the script.
#
# I've only tested this on Ubuntu, but it should probably also work with
# Debian.
#
DIST=""
BASE_URL="http://rcn-ee.net/deb"
OFFICIAL_KERNEL_BASE_URL="https://www.kernel.org/pub/linux/kernel"
KVER=$(uname -r)
MAIN_KVER=$(echo ${KVER} | sed -nE 's/^(([0-9]+\.?)+).*/\1/gp' | sed 's/.0//g')
DDIR=$(mktemp -d)
clean_up () {
rm -rf "${DDIR}"
}
notif () {
echo "\033[1;34m${1}\033[0m${2}"
}
fail () {
echo "\033[1;31m${1}\033[0m${2}"
clean_up
exit 0
}
checks () {
if [ -d "/usr/src/linux-${KVER}" ]; then
notif "directory /usr/src/linux-${KVER} already exists. nothing to do!"
exit 0
fi
if ! [ $(id -u) = 0 ]; then
fail "You need to be root to run this (or use sudo)."
fi
}
determine_dist () {
#lsb_release: is not installed by default when using debian's debootstrap script.
if [ -z "${DIST}" ]; then
has_lsb_release=$(which lsb_release 2>/dev/null)
if [ "${has_lsb_release}" ] ; then
release=$(lsb_release -cs)
dpkg_arch=$(dpkg --print-architecture)
DIST="${release}-${dpkg_arch}"
fi
fi
if [ -z "${DIST}" ]; then
fail "failed to determine linux distro: either install lsb-release (apt-get install lsb-release) or specify distribution manually at the top of this script, as \$DIST."
fi
RURL="${BASE_URL}/${DIST}/v${KVER}"
notif "using linux distribution: " "${DIST}"
}
get_rcn_kernel_header_package () {
notif "getting file listing from ${RURL}/..."
wget --directory-prefix="${DDIR}" "${RURL}/"
KPATCH=$(cat ${DDIR}/index.html | grep "patch" | sed -nE 's/ *<a href=\"([^\"]+)\".*/\1/p')
HDR_DEB=$(cat ${DDIR}/index.html | grep "linux-headers" | sed -nE 's/ *<a href=\"([^\"]+)\".*/\1/p')
if [ -n "${HDR_DEB}" ]; then
notif "getting kernel headers from ${RURL}/${HDR_DEB}..."
wget --directory-prefix="${DDIR}" "${RURL}/${HDR_DEB}"
else
fail "no kernel header package found for ${KVER}, try another kernel."
fi
if [ -n "${KPATCH}" ]; then
notif "getting kernel patch from ${RURL}/${KPATCH}..."
wget --directory-prefix="${DDIR}" "${RURL}/${KPATCH}"
else
fail "no kernel patch found for ${KVER}, try another kernel."
fi
}
unpack_rcn_kernel_header_package () {
notif "unpacking ${HDR_DEB}"
notif
dpkg -x "${DDIR}/${HDR_DEB}" "${DDIR}/kheaders"
KCONFIG="${DDIR}/kheaders/usr/src/linux-headers-${KVER}/.config"
SYMVERS="${DDIR}/kheaders/usr/src/linux-headers-${KVER}/Module.symvers"
if [ ! -f "${KCONFIG}" ]; then
fail "kernel headers package doesn't include .config, try another kernel."
fi
if [ ! -f "${SYMVERS}" ]; then
fail "kernel headers package doesn't include Module.symvers, try another kernel."
fi
}
get_official_kernel_source () {
if [ -n "$(echo ${MAIN_KVER} | sed -nE 's/(3\.)+.*/\1/p')" ]; then
MAIN_KERNEL_URL="${OFFICIAL_KERNEL_BASE_URL}/v3.x/linux-${MAIN_KVER}.tar.xz"
else
MAIN_KERNEL_URL="${OFFICIAL_KERNEL_BASE_URL}/v$(echo ${MAIN_KVER} | sed -nE 's/([0-9]+\.[0-9]+).*/\1/p')/linux-${MAIN_KVER}.tar.xz"
fi
notif "getting official kernel source from ${MAIN_KERNEL_URL}..."
wget --directory-prefix="${DDIR}" "${MAIN_KERNEL_URL}"
notif "unpacking official kernel source..."
sleep 1
tar xvfJ "${DDIR}/linux-${MAIN_KVER}.tar.xz" --directory "${DDIR}"
}
patch_kernel_source () {
ZCAT="zcat"
has_gzcat=$(which gzcat 2>/dev/null)
if [ "${has_gzcat}" ] ; then
ZCAT="${has_gzcat}"
fi
notif "applying patch ${KPATCH} to linux-${MAIN_KVER}..."
sleep 1
$ZCAT "${DDIR}/${KPATCH}" | patch -d "${DDIR}/linux-${MAIN_KVER}" -p1
}
install_kernel_source () {
notif "moving kernel source to /usr/src/linux-${KVER}..."
mkdir -p /usr/src/
rm -rf "/usr/src/linux-${KVER}"
mv "${DDIR}/linux-${MAIN_KVER}" "/usr/src/linux-${KVER}"
mv "${SYMVERS}" "/usr/src/linux-${KVER}/Module.symvers"
mv "${KCONFIG}" "/usr/src/linux-${KVER}/.config"
mkdir -p "/lib/modules/${KVER}"
rm -f "/lib/modules/${KVER}/build"
ln -s "/usr/src/linux-${KVER}" "/lib/modules/${KVER}/build"
}
prepare_kernel_source () {
notif "preparing kernel source at /usr/src/linux-${KVER}..."
CURPWD=$PWD
cd "/usr/src/linux-${KVER}"
make oldconfig
make prepare
make scripts
cd "${CURPWD}"
}
if [ ! -z ${1} ]; then
KVER="${1}"
fi
notif "installing kernel sources for ${KVER}..."
notif
checks
determine_dist
get_rcn_kernel_header_package
unpack_rcn_kernel_header_package
get_official_kernel_source
patch_kernel_source
install_kernel_source
prepare_kernel_source
clean_up
notif "done: kernel sources for ${KVER} are now installed."
notif "you should be able to compile kernel modules."
exit 0