##// END OF EJS Templates
t
Unknown -
r408:fb8ed5eebd29
parent child
Show More
@@ -1,406 +1,406
1 1 #
2 2 # Build and Setup RPi2/3 Kernel
3 3 #
4 4
5 5 # Load utility functions
6 6 . ./functions.sh
7 7
8 8 # Fetch and build latest raspberry kernel
9 9 if [ "$BUILD_KERNEL" = true ] ; then
10 10 # Setup source directory
11 11 mkdir -p "${KERNEL_DIR}"
12 12
13 13 # Copy existing kernel sources into chroot directory
14 14 if [ -n "$KERNELSRC_DIR" ] && [ -d "$KERNELSRC_DIR" ] ; then
15 15 # Copy kernel sources and include hidden files
16 16 cp -r "${KERNELSRC_DIR}/". "${KERNEL_DIR}"
17 17
18 18 # Clean the kernel sources
19 19 if [ "$KERNELSRC_CLEAN" = true ] && [ "$KERNELSRC_PREBUILT" = false ] ; then
20 20 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" mrproper
21 21 fi
22 22 else # KERNELSRC_DIR=""
23 23 # Create temporary directory for kernel sources
24 24 temp_dir=$(as_nobody mktemp -d)
25 25
26 26 # Fetch current RPi2/3 kernel sources
27 27 if [ -z "${KERNEL_BRANCH}" ] ; then
28 28 as_nobody -H git -C "${temp_dir}" clone --depth=1 "${KERNEL_URL}" linux
29 29 else
30 30 as_nobody -H git -C "${temp_dir}" clone --depth=1 --branch "${KERNEL_BRANCH}" "${KERNEL_URL}" linux
31 31 fi
32 32
33 33 # Copy downloaded kernel sources
34 34 cp -r "${temp_dir}/linux/"* "${KERNEL_DIR}"
35 35
36 36 # Remove temporary directory for kernel sources
37 37 rm -fr "${temp_dir}"
38 38
39 39 # Set permissions of the kernel sources
40 40 chown -R root:root "${R}/usr/src"
41 41 fi
42 42
43 43 # Calculate optimal number of kernel building threads
44 44 if [ "$KERNEL_THREADS" = "1" ] && [ -r /proc/cpuinfo ] ; then
45 45 KERNEL_THREADS=$(grep -c processor /proc/cpuinfo)
46 46 fi
47 47
48 48 # Configure and build kernel
49 49 if [ "$KERNELSRC_PREBUILT" = false ] ; then
50 50 # Remove device, network and filesystem drivers from kernel configuration
51 51 if [ "$KERNEL_REDUCE" = true ] ; then
52 52 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" "${KERNEL_DEFCONFIG}"
53 53 sed -i\
54 54 -e "s/\(^CONFIG_SND.*\=\).*/\1n/"\
55 55 -e "s/\(^CONFIG_SOUND.*\=\).*/\1n/"\
56 56 -e "s/\(^CONFIG_AC97.*\=\).*/\1n/"\
57 57 -e "s/\(^CONFIG_VIDEO_.*\=\).*/\1n/"\
58 58 -e "s/\(^CONFIG_MEDIA_TUNER.*\=\).*/\1n/"\
59 59 -e "s/\(^CONFIG_DVB.*\=\)[ym]/\1n/"\
60 60 -e "s/\(^CONFIG_REISERFS.*\=\).*/\1n/"\
61 61 -e "s/\(^CONFIG_JFS.*\=\).*/\1n/"\
62 62 -e "s/\(^CONFIG_XFS.*\=\).*/\1n/"\
63 63 -e "s/\(^CONFIG_GFS2.*\=\).*/\1n/"\
64 64 -e "s/\(^CONFIG_OCFS2.*\=\).*/\1n/"\
65 65 -e "s/\(^CONFIG_BTRFS.*\=\).*/\1n/"\
66 66 -e "s/\(^CONFIG_HFS.*\=\).*/\1n/"\
67 67 -e "s/\(^CONFIG_JFFS2.*\=\)[ym]/\1n/"\
68 68 -e "s/\(^CONFIG_UBIFS.*\=\).*/\1n/"\
69 69 -e "s/\(^CONFIG_SQUASHFS.*\=\)[ym]/\1n/"\
70 70 -e "s/\(^CONFIG_W1.*\=\)[ym]/\1n/"\
71 71 -e "s/\(^CONFIG_HAMRADIO.*\=\).*/\1n/"\
72 72 -e "s/\(^CONFIG_CAN.*\=\).*/\1n/"\
73 73 -e "s/\(^CONFIG_IRDA.*\=\).*/\1n/"\
74 74 -e "s/\(^CONFIG_BT_.*\=\).*/\1n/"\
75 75 -e "s/\(^CONFIG_WIMAX.*\=\)[ym]/\1n/"\
76 76 -e "s/\(^CONFIG_6LOWPAN.*\=\).*/\1n/"\
77 77 -e "s/\(^CONFIG_IEEE802154.*\=\).*/\1n/"\
78 78 -e "s/\(^CONFIG_NFC.*\=\).*/\1n/"\
79 79 -e "s/\(^CONFIG_FB_TFT=.*\=\).*/\1n/"\
80 80 -e "s/\(^CONFIG_TOUCHSCREEN.*\=\).*/\1n/"\
81 81 -e "s/\(^CONFIG_USB_GSPCA_.*\=\).*/\1n/"\
82 82 -e "s/\(^CONFIG_DRM.*\=\).*/\1n/"\
83 "${KERNEL_DIR}"/.config
83 "${KERNEL_DIR}/.config"
84 84 fi
85 85
86 86 if [ "$KERNELSRC_CONFIG" = true ] ; then
87 87 # Load default raspberry kernel configuration
88 88 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" "${KERNEL_DEFCONFIG}"
89 89 #Switch to KERNELSRC_DIR
90 90 cd "${KERNEL_DIR}"
91 91
92 92 # GPL v2.0
93 93 #https://github.com/sakaki-/bcmrpi3-kernel-bis/blob/master/conform_config.sh
94 94 if [ "$KERNEL_ZSWAP" = true ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then
95 95 # enable ZSWAP support for better performance during large builds etc.
96 96 # requires activation via kernel parameter or sysfs
97 97 # see e.g. https://askubuntu.com/a/472227 for a summary of ZSWAP (vs ZRAM etc.)
98 98 # and e.g. https://wiki.archlinux.org/index.php/zswap for parameters etc.
99 99 set_kernel_config ZPOOL y
100 100 set_kernel_config ZSWAP y
101 101 set_kernel_config ZBUD y
102 102 set_kernel_config Z3FOLD y
103 103 set_kernel_config ZSMALLOC y
104 104 set_kernel_config PGTABLE_MAPPING y
105 105 fi
106 106
107 107 if [ "$KERNEL_VIRT" = true ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then
108 108 # enable basic KVM support; see e.g.
109 109 # https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=210546&start=25#p1300453
110 110 set_kernel_config VIRTUALIZATION y
111 111 set_kernel_config KVM y
112 112 set_kernel_config VHOST_NET m
113 113 set_kernel_config VHOST_CROSS_ENDIAN_LEGACY y
114 114 fi
115 115 #See https://github.com/raspberrypi/linux/issues/2177#issuecomment-354647406
116 116 # Netfilter kernel support
117 117 if [ "$KERNEL_NF" = true ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then
118 118 set_kernel_config CONFIG_NETFILTER_XTABLES m
119 119 set_kernel_config CONFIG_NF_DUP_NETDEV m
120 120 set_kernel_config CONFIG_NF_NAT_SIP m
121 121 set_kernel_config CONFIG_NF_TABLES_ARP m
122 122 set_kernel_config CONFIG_NF_TABLES_BRIDGE m
123 123 set_kernel_config NF_TABLES m
124 124 set_kernel_config CONFIG_BRIDGE_EBT_BROUTE m
125 125 set_kernel_config CONFIG_BRIDGE_EBT_T_FILTER m
126 126 set_kernel_config CONFIG_BRIDGE_NF_EBTABLES m
127 127 set_kernel_config CONFIG_IP6_NF_IPTABLES m
128 128 set_kernel_config CONFIG_IP6_NF_MATCH_AH m
129 129 set_kernel_config CONFIG_IP6_NF_MATCH_EUI64 m
130 130 set_kernel_config CONFIG_IP6_NF_NAT m
131 131 set_kernel_config CONFIG_IP6_NF_TARGET_MASQUERADE m
132 132 set_kernel_config CONFIG_IP6_NF_TARGET_NPT m
133 133 set_kernel_config CONFIG_IP_SET_BITMAP_IPMAC m
134 134 set_kernel_config CONFIG_IP_SET_BITMAP_PORT m
135 135 set_kernel_config CONFIG_IP_SET_HASH_IP m
136 136 set_kernel_config CONFIG_IP_SET_HASH_IPMARK m
137 137 set_kernel_config CONFIG_IP_SET_HASH_IPPORT m
138 138 set_kernel_config CONFIG_IP_SET_HASH_IPPORTIP m
139 139 set_kernel_config CONFIG_IP_SET_HASH_IPPORTNET m
140 140 set_kernel_config CONFIG_IP_SET_HASH_MAC m
141 141 set_kernel_config CONFIG_IP_SET_HASH_NET m
142 142 set_kernel_config CONFIG_IP_SET_HASH_NETIFACE m
143 143 set_kernel_config CONFIG_IP_SET_HASH_NETNET m
144 144 set_kernel_config CONFIG_IP_SET_HASH_NETPORT m
145 145 set_kernel_config CONFIG_IP_SET_HASH_NETPORTNET m
146 146 set_kernel_config CONFIG_IP_SET_LIST_SET m
147 147 set_kernel_config CONFIG_NFT_BRIDGE_META m
148 148 set_kernel_config CONFIG_NFT_BRIDGE_REJECT m
149 149 set_kernel_config CONFIG_NFT_CHAIN_NAT_IPV4 m
150 150 set_kernel_config CONFIG_NFT_CHAIN_NAT_IPV6 m
151 151 set_kernel_config CONFIG_NFT_CHAIN_ROUTE_IPV4 m
152 152 set_kernel_config CONFIG_NFT_CHAIN_ROUTE_IPV6 m
153 153 set_kernel_config CONFIG_NFT_COMPAT m
154 154 set_kernel_config CONFIG_NFT_COUNTER m
155 155 set_kernel_config CONFIG_NFT_CT m
156 156 set_kernel_config CONFIG_NFT_DUP_IPV4 m
157 157 set_kernel_config CONFIG_NFT_DUP_IPV6 m
158 158 set_kernel_config CONFIG_NFT_DUP_NETDEV m
159 159 set_kernel_config CONFIG_NFT_EXTHDR m
160 160 set_kernel_config CONFIG_NFT_FWD_NETDEV m
161 161 set_kernel_config CONFIG_NFT_HASH m
162 162 set_kernel_config CONFIG_NFT_LIMIT m
163 163 set_kernel_config CONFIG_NFT_LOG m
164 164 set_kernel_config CONFIG_NFT_MASQ m
165 165 set_kernel_config CONFIG_NFT_MASQ_IPV4 m
166 166 set_kernel_config CONFIG_NFT_MASQ_IPV6 m
167 167 set_kernel_config CONFIG_NFT_META m
168 168 set_kernel_config CONFIG_NFT_NAT m
169 169 set_kernel_config CONFIG_NFT_NUMGEN m
170 170 set_kernel_config CONFIG_NFT_QUEUE m
171 171 set_kernel_config CONFIG_NFT_QUOTA m
172 172 set_kernel_config CONFIG_NFT_REDIR m
173 173 set_kernel_config CONFIG_NFT_REDIR_IPV4 m
174 174 set_kernel_config CONFIG_NFT_REDIR_IPV6 m
175 175 set_kernel_config CONFIG_NFT_REJECT m
176 176 set_kernel_config CONFIG_NFT_REJECT_INET m
177 177 set_kernel_config CONFIG_NFT_REJECT_IPV4 m
178 178 set_kernel_config CONFIG_NFT_REJECT_IPV6 m
179 179 set_kernel_config CONFIG_NFT_SET_HASH m
180 180 set_kernel_config CONFIG_NFT_SET_RBTREE m
181 181 set_kernel_config CONFIG_NF_CONNTRACK_IPV4 m
182 182 set_kernel_config CONFIG_NF_CONNTRACK_IPV6 m
183 183 set_kernel_config CONFIG_NF_DEFRAG_IPV4 m
184 184 set_kernel_config CONFIG_NF_DEFRAG_IPV6 m
185 185 set_kernel_config CONFIG_NF_DUP_IPV4 m
186 186 set_kernel_config CONFIG_NF_DUP_IPV6 m
187 187 set_kernel_config CONFIG_NF_LOG_BRIDGE m
188 188 set_kernel_config CONFIG_NF_LOG_IPV4 m
189 189 set_kernel_config CONFIG_NF_LOG_IPV6 m
190 190 set_kernel_config CONFIG_NF_NAT_IPV4 m
191 191 set_kernel_config CONFIG_NF_NAT_IPV6 m
192 192 set_kernel_config CONFIG_NF_NAT_MASQUERADE_IPV4 m
193 193 set_kernel_config CONFIG_NF_NAT_MASQUERADE_IPV6 m
194 194 set_kernel_config CONFIG_NF_NAT_PPTP m
195 195 set_kernel_config CONFIG_NF_NAT_PROTO_GRE m
196 196 set_kernel_config CONFIG_NF_NAT_REDIRECT m
197 197 set_kernel_config CONFIG_NF_NAT_SNMP_BASIC m
198 198 set_kernel_config CONFIG_NF_NAT_TFTP m
199 199 set_kernel_config CONFIG_NF_REJECT_IPV4 m
200 200 set_kernel_config CONFIG_NF_REJECT_IPV6 m
201 201 set_kernel_config CONFIG_NF_TABLES_INET m
202 202 set_kernel_config CONFIG_NF_TABLES_IPV4 m
203 203 set_kernel_config CONFIG_NF_TABLES_IPV6 m
204 204 set_kernel_config CONFIG_NF_TABLES_NETDEV m
205 205 set_kernel_config NETFILTER_XTABLES m
206 206 fi
207 207
208 208 #https://groups.google.com/forum/#!topic/linux.gentoo.user/_2aSc_ztGpA
209 209 #https://github.com/torvalds/linux/blob/master/init/Kconfig#L848
210 210 # Enables BPF syscall for systemd-journald
211 211 if [ "$KERNEL_BPF" = true ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then
212 212 set_kernel_config CONFIG_BPF_SYSCALL y
213 213 set_kernel_config CONFIG_CGROUP_BPF y
214 214 fi
215 215
216 216 #Revert to previous directory
217 cd "${R}"
217 cd "${WORKDIR}"
218 218
219 219 # Set kernel configuration parameters to enable qemu emulation
220 220 if [ "$ENABLE_QEMU" = true ] ; then
221 221 echo "CONFIG_FHANDLE=y" >> "${KERNEL_DIR}"/.config
222 222 echo "CONFIG_LBDAF=y" >> "${KERNEL_DIR}"/.config
223 223
224 224 if [ "$ENABLE_CRYPTFS" = true ] ; then
225 225 {
226 226 echo "CONFIG_EMBEDDED=y"
227 227 echo "CONFIG_EXPERT=y"
228 228 echo "CONFIG_DAX=y"
229 229 echo "CONFIG_MD=y"
230 230 echo "CONFIG_BLK_DEV_MD=y"
231 231 echo "CONFIG_MD_AUTODETECT=y"
232 232 echo "CONFIG_BLK_DEV_DM=y"
233 233 echo "CONFIG_BLK_DEV_DM_BUILTIN=y"
234 234 echo "CONFIG_DM_CRYPT=y"
235 235 echo "CONFIG_CRYPTO_BLKCIPHER=y"
236 236 echo "CONFIG_CRYPTO_CBC=y"
237 237 echo "CONFIG_CRYPTO_XTS=y"
238 238 echo "CONFIG_CRYPTO_SHA512=y"
239 239 echo "CONFIG_CRYPTO_MANAGER=y"
240 240 } >> "${KERNEL_DIR}/.config"
241 241 fi
242 242 fi
243 243
244 244 # Copy custom kernel configuration file
245 245 if [ -n "$KERNELSRC_USRCONFIG" ] ; then
246 246 cp "$KERNELSRC_USRCONFIG" "${KERNEL_DIR}"/.config
247 247 fi
248 248
249 249 # Set kernel configuration parameters to their default values
250 250 if [ "$KERNEL_OLDDEFCONFIG" = true ] ; then
251 251 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" olddefconfig
252 252 fi
253 253
254 254 # Start menu-driven kernel configuration (interactive)
255 255 if [ "$KERNEL_MENUCONFIG" = true ] ; then
256 256 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" menuconfig
257 257 fi
258 258 fi
259 259
260 260 # Use ccache to cross compile the kernel
261 261 if [ "$KERNEL_CCACHE" = true ] ; then
262 262 cc="ccache ${CROSS_COMPILE}gcc"
263 263 else
264 264 cc="${CROSS_COMPILE}gcc"
265 265 fi
266 266
267 267 # Cross compile kernel and dtbs
268 268 make -C "${KERNEL_DIR}" -j"${KERNEL_THREADS}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" CC="${cc}" "${KERNEL_BIN_IMAGE}" dtbs
269 269
270 270 # Cross compile kernel modules
271 271 if grep -q "CONFIG_MODULES=y" "${KERNEL_DIR}/.config" ; then
272 272 make -C "${KERNEL_DIR}" -j"${KERNEL_THREADS}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" CC="${cc}" modules
273 273 fi
274 274 fi
275 275
276 276 # Check if kernel compilation was successful
277 277 if [ ! -r "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/${KERNEL_BIN_IMAGE}" ] ; then
278 278 echo "error: kernel compilation failed! (kernel image not found)"
279 279 cleanup
280 280 exit 1
281 281 fi
282 282
283 283 # Install kernel modules
284 284 if [ "$ENABLE_REDUCE" = true ] ; then
285 285 if grep -q "CONFIG_MODULES=y" "${KERNEL_DIR}/.config" ; then
286 286 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=../../.. modules_install
287 287 fi
288 288 else
289 289 if grep -q "CONFIG_MODULES=y" "${KERNEL_DIR}/.config" ; then
290 290 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_MOD_PATH=../../.. modules_install
291 291 fi
292 292
293 293 # Install kernel firmware
294 294 if grep -q "^firmware_install:" "${KERNEL_DIR}/Makefile" ; then
295 295 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_FW_PATH=../../../lib firmware_install
296 296 fi
297 297 fi
298 298
299 299 # Install kernel headers
300 300 if [ "$KERNEL_HEADERS" = true ] && [ "$KERNEL_REDUCE" = false ] ; then
301 301 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" INSTALL_HDR_PATH=../.. headers_install
302 302 fi
303 303 # make tar.gz kernel package - missing os bzw. modules
304 304 #** ** ** WARNING ** ** **
305 305 #Your architecture did not define any architecture-dependent files
306 306 #to be placed into the tarball. Please add those to ./scripts/package/buildtar .
307 307 # make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" CC="${cc}" targz-pkg
308 308
309 309 # Prepare boot (firmware) directory
310 310 mkdir "${BOOT_DIR}"
311 311
312 312 # Get kernel release version
313 313 KERNEL_VERSION=$(cat "${KERNEL_DIR}/include/config/kernel.release")
314 314
315 315 # Copy kernel configuration file to the boot directory
316 316 install_readonly "${KERNEL_DIR}/.config" "${R}/boot/config-${KERNEL_VERSION}"
317 317
318 318 # Prepare device tree directory
319 319 mkdir "${BOOT_DIR}/overlays"
320 320
321 321 # Ensure the proper .dtb is located
322 322 if [ "$KERNEL_ARCH" = "arm" ] ; then
323 323 for dtb in "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/"*.dtb ; do
324 324 if [ -f "${dtb}" ] ; then
325 325 install_readonly "${dtb}" "${BOOT_DIR}/"
326 326 fi
327 327 done
328 328 else
329 329 for dtb in "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/broadcom/"*.dtb ; do
330 330 if [ -f "${dtb}" ] ; then
331 331 install_readonly "${dtb}" "${BOOT_DIR}/"
332 332 fi
333 333 done
334 334 fi
335 335
336 336 # Copy compiled dtb device tree files
337 337 if [ -d "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays" ] ; then
338 338 for dtb in "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays/"*.dtb ; do
339 339 if [ -f "${dtb}" ] ; then
340 340 install_readonly "${dtb}" "${BOOT_DIR}/overlays/"
341 341 fi
342 342 done
343 343
344 344 if [ -f "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays/README" ] ; then
345 345 install_readonly "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/dts/overlays/README" "${BOOT_DIR}/overlays/README"
346 346 fi
347 347 fi
348 348
349 349 if [ "$ENABLE_UBOOT" = false ] ; then
350 350 # Convert and copy kernel image to the boot directory
351 351 "${KERNEL_DIR}/scripts/mkknlimg" "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/${KERNEL_BIN_IMAGE}" "${BOOT_DIR}/${KERNEL_IMAGE}"
352 352 else
353 353 # Copy kernel image to the boot directory
354 354 install_readonly "${KERNEL_DIR}/arch/${KERNEL_ARCH}/boot/${KERNEL_BIN_IMAGE}" "${BOOT_DIR}/${KERNEL_IMAGE}"
355 355 fi
356 356
357 357 # Remove kernel sources
358 358 if [ "$KERNEL_REMOVESRC" = true ] ; then
359 359 rm -fr "${KERNEL_DIR}"
360 360 else
361 361 # Prepare compiled kernel modules
362 362 if grep -q "CONFIG_MODULES=y" "${KERNEL_DIR}/.config" ; then
363 363 if grep -q "^modules_prepare:" "${KERNEL_DIR}/Makefile" ; then
364 364 make -C "${KERNEL_DIR}" ARCH="${KERNEL_ARCH}" CROSS_COMPILE="${CROSS_COMPILE}" modules_prepare
365 365 fi
366 366
367 367 # Create symlinks for kernel modules
368 368 chroot_exec ln -sf /usr/src/linux "/lib/modules/${KERNEL_VERSION}/build"
369 369 chroot_exec ln -sf /usr/src/linux "/lib/modules/${KERNEL_VERSION}/source"
370 370 fi
371 371 fi
372 372
373 373 else # BUILD_KERNEL=false
374 374 # echo Install precompiled kernel...
375 375 # echo error: not implemented
376 376 if [ "$KERNEL_ARCH" = arm64 ] && { [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; } ; then
377 377 # Create temporary directory for dl
378 378 temp_dir=$(as_nobody mktemp -d)
379 379
380 380 # Fetch kernel dl
381 381 as_nobody wget -O "${temp_dir}"/kernel.tar.xz -c "$RPI3_64_KERNEL_URL"
382 382 #extract download
383 383 tar -xJf "${temp_dir}"/kernel.tar.xz -C "${temp_dir}"
384 384
385 385 #move extracted kernel to /boot/firmware
386 386 mkdir "${R}/boot/firmware"
387 387 cp "${temp_dir}"/boot/* "${R}"/boot/firmware/
388 388 cp -r "${temp_dir}"/lib/* "${R}"/lib/
389 389
390 390 # Remove temporary directory for kernel sources
391 391 rm -fr "${temp_dir}"
392 392 # Set permissions of the kernel sources
393 393 chown -R root:root "${R}/boot/firmware"
394 394 chown -R root:root "${R}/lib/modules"
395 395 #Create cmdline.txt for 15-rpi-config.sh
396 396 touch "${BOOT_DIR}/cmdline.txt"
397 397 fi
398 398
399 399 # Check if kernel installation was successful
400 400 KERNEL="$(ls -1 "${R}"/boot/firmware/kernel* | sort | tail -n 1)"
401 401 if [ -z "$KERNEL" ] ; then
402 402 echo "error: kernel installation failed! (/boot/kernel* not found)"
403 403 cleanup
404 404 exit 1
405 405 fi
406 406 fi
@@ -1,821 +1,822
1 1 #!/bin/sh
2 2 ########################################################################
3 3 # rpi23-gen-image.sh 2015-2017
4 4 #
5 5 # Advanced Debian "stretch" and "buster" bootstrap script for RPi2/3
6 6 #
7 7 # This program is free software; you can redistribute it and/or
8 8 # modify it under the terms of the GNU General Public License
9 9 # as published by the Free Software Foundation; either version 2
10 10 # of the License, or (at your option) any later version.
11 11 #
12 12 # Copyright (C) 2015 Jan Wagner <mail@jwagner.eu>
13 13 #
14 14 # Big thanks for patches and enhancements by 20+ github contributors!
15 15 ########################################################################
16 16
17 17 # Are we running as root?
18 18 if [ "$(id -u)" -ne "0" ] ; then
19 19 echo "error: this script must be executed with root privileges!"
20 20 exit 1
21 21 fi
22 22
23 23 # Check if ./functions.sh script exists
24 24 if [ ! -r "./functions.sh" ] ; then
25 25 echo "error: './functions.sh' required script not found!"
26 26 exit 1
27 27 fi
28 28
29 29 # Load utility functions
30 30 . ./functions.sh
31 31
32 32 # Load parameters from configuration template file
33 33 if [ -n "$CONFIG_TEMPLATE" ] ; then
34 34 use_template
35 35 fi
36 36
37 37 # Introduce settings
38 38 set -e
39 39 echo -n -e "\n#\n# RPi2/3 Bootstrap Settings\n#\n"
40 40 set -x
41 41
42 42 # Raspberry Pi model configuration
43 43 RPI_MODEL=${RPI_MODEL:=2}
44 44
45 45 # Debian release
46 46 RELEASE=${RELEASE:=buster}
47 47
48 48 #Kernel Branch
49 49 KERNEL_BRANCH=${KERNEL_BRANCH:=""}
50 50
51 51 # URLs
52 52 KERNEL_URL=${KERNEL_URL:=https://github.com/raspberrypi/linux}
53 53 FIRMWARE_URL=${FIRMWARE_URL:=https://github.com/raspberrypi/firmware/raw/master/boot}
54 54 WLAN_FIRMWARE_URL=${WLAN_FIRMWARE_URL:=https://github.com/RPi-Distro/firmware-nonfree/raw/master/brcm}
55 55 COLLABORA_URL=${COLLABORA_URL:=https://repositories.collabora.co.uk/debian}
56 56 FBTURBO_URL=${FBTURBO_URL:=https://github.com/ssvb/xf86-video-fbturbo.git}
57 57 UBOOT_URL=${UBOOT_URL:=https://git.denx.de/u-boot.git}
58 58 VIDEOCORE_URL=${VIDEOCORE_URL:=https://github.com/raspberrypi/userland}
59 59 #BIS= Kernel has KVM and zswap enabled
60 60 RPI3_64_BIS_KERNEL_URL=${RPI3_64_BIS_KERNEL_URL:=https://github.com/sakaki-/bcmrpi3-kernel-bis/releases/download/4.14.80.20181113/bcmrpi3-kernel-bis-4.14.80.20181113.tar.xz}
61 61 #default bcmrpi3_defconfig target kernel
62 62 RPI3_64_DEF_KERNEL_URL=${RPI3_64_DEF_KERNEL_URL:=https://github.com/sakaki-/bcmrpi3-kernel/releases/download/4.14.80.20181113/bcmrpi3-kernel-4.14.80.20181113.tar.xz}
63 63 #enhanced kernel
64 64 RPI3_64_KERNEL_URL=${RPI3_64_KERNEL_URL:=$RPI3_64_BIS_KERNEL_URL}
65 65 BLUETOOTH_URL=${BLUETOOTH_URL:=https://github.com/RPi-Distro/pi-bluetooth.git}
66 66
67 67 # Build directories
68 BASEDIR=${BASEDIR:=$(pwd)/images/${RELEASE}}
68 WORKDIR=$(pwd)
69 BASEDIR=${BASEDIR:=${WORKDIR}/images/${RELEASE}}
69 70 BUILDDIR="${BASEDIR}/build"
70 71
71 72 # Prepare date string for default image file name
72 73 DATE="$(date +%Y-%m-%d)"
73 74 if [ -z "$KERNEL_BRANCH" ] ; then
74 75 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
75 76 else
76 77 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-${KERNEL_BRANCH}-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
77 78 fi
78 79
79 80 # Chroot directories
80 81 R="${BUILDDIR}/chroot"
81 82 ETC_DIR="${R}/etc"
82 83 LIB_DIR="${R}/lib"
83 84 BOOT_DIR="${R}/boot/firmware"
84 85 KERNEL_DIR="${R}/usr/src/linux"
85 86 WLAN_FIRMWARE_DIR="${LIB_DIR}/firmware/brcm"
86 87
87 88 # Firmware directory: Blank if download from github
88 89 RPI_FIRMWARE_DIR=${RPI_FIRMWARE_DIR:=""}
89 90
90 91 # General settings
91 92 SET_ARCH=${SET_ARCH:=32}
92 93 HOSTNAME=${HOSTNAME:=rpi${RPI_MODEL}-${RELEASE}}
93 94 PASSWORD=${PASSWORD:=raspberry}
94 95 USER_PASSWORD=${USER_PASSWORD:=raspberry}
95 96 DEFLOCAL=${DEFLOCAL:="en_US.UTF-8"}
96 97 TIMEZONE=${TIMEZONE:="Europe/Berlin"}
97 98 EXPANDROOT=${EXPANDROOT:=true}
98 99
99 100 # Keyboard settings
100 101 XKB_MODEL=${XKB_MODEL:=""}
101 102 XKB_LAYOUT=${XKB_LAYOUT:=""}
102 103 XKB_VARIANT=${XKB_VARIANT:=""}
103 104 XKB_OPTIONS=${XKB_OPTIONS:=""}
104 105
105 106 # Network settings (DHCP)
106 107 ENABLE_DHCP=${ENABLE_DHCP:=true}
107 108
108 109 # Network settings (static)
109 110 NET_ADDRESS=${NET_ADDRESS:=""}
110 111 NET_GATEWAY=${NET_GATEWAY:=""}
111 112 NET_DNS_1=${NET_DNS_1:=""}
112 113 NET_DNS_2=${NET_DNS_2:=""}
113 114 NET_DNS_DOMAINS=${NET_DNS_DOMAINS:=""}
114 115 NET_NTP_1=${NET_NTP_1:=""}
115 116 NET_NTP_2=${NET_NTP_2:=""}
116 117
117 118 # APT settings
118 119 APT_PROXY=${APT_PROXY:=""}
119 120 APT_SERVER=${APT_SERVER:="ftp.debian.org"}
120 121
121 122 # Feature settings
122 123 ENABLE_CONSOLE=${ENABLE_CONSOLE:=true}
123 124 ENABLE_I2C=${ENABLE_I2C:=false}
124 125 ENABLE_SPI=${ENABLE_SPI:=false}
125 126 ENABLE_IPV6=${ENABLE_IPV6:=true}
126 127 ENABLE_SSHD=${ENABLE_SSHD:=true}
127 128 ENABLE_NONFREE=${ENABLE_NONFREE:=false}
128 129 ENABLE_WIRELESS=${ENABLE_WIRELESS:=false}
129 130 ENABLE_SOUND=${ENABLE_SOUND:=true}
130 131 ENABLE_DBUS=${ENABLE_DBUS:=true}
131 132 ENABLE_HWRANDOM=${ENABLE_HWRANDOM:=true}
132 133 ENABLE_MINGPU=${ENABLE_MINGPU:=false}
133 134 ENABLE_XORG=${ENABLE_XORG:=false}
134 135 ENABLE_WM=${ENABLE_WM:=""}
135 136 ENABLE_RSYSLOG=${ENABLE_RSYSLOG:=true}
136 137 ENABLE_USER=${ENABLE_USER:=true}
137 138 USER_NAME=${USER_NAME:="pi"}
138 139 ENABLE_ROOT=${ENABLE_ROOT:=false}
139 140 ENABLE_QEMU=${ENABLE_QEMU:=false}
140 141 ENABLE_SYSVINIT=${ENABLE_SYSVINIT:=false}
141 142
142 143 # SSH settings
143 144 SSH_ENABLE_ROOT=${SSH_ENABLE_ROOT:=false}
144 145 SSH_DISABLE_PASSWORD_AUTH=${SSH_DISABLE_PASSWORD_AUTH:=false}
145 146 SSH_LIMIT_USERS=${SSH_LIMIT_USERS:=false}
146 147 SSH_ROOT_PUB_KEY=${SSH_ROOT_PUB_KEY:=""}
147 148 SSH_USER_PUB_KEY=${SSH_USER_PUB_KEY:=""}
148 149
149 150 # Advanced settings
150 151 ENABLE_MINBASE=${ENABLE_MINBASE:=false}
151 152 ENABLE_REDUCE=${ENABLE_REDUCE:=false}
152 153 ENABLE_UBOOT=${ENABLE_UBOOT:=false}
153 154 UBOOTSRC_DIR=${UBOOTSRC_DIR:=""}
154 155 ENABLE_UBOOTUSB=${ENABLE_UBOOTUSB=false}
155 156 ENABLE_FBTURBO=${ENABLE_FBTURBO:=false}
156 157 ENABLE_VIDEOCORE=${ENABLE_VIDEOCORE:=true}
157 158 VIDEOCORESRC_DIR=${VIDEOCORESRC_DIR:=""}
158 159 FBTURBOSRC_DIR=${FBTURBOSRC_DIR:=""}
159 160 ENABLE_HARDNET=${ENABLE_HARDNET:=false}
160 161 ENABLE_IPTABLES=${ENABLE_IPTABLES:=false}
161 162 ENABLE_SPLITFS=${ENABLE_SPLITFS:=false}
162 163 ENABLE_INITRAMFS=${ENABLE_INITRAMFS:=false}
163 164 ENABLE_IFNAMES=${ENABLE_IFNAMES:=true}
164 165 DISABLE_UNDERVOLT_WARNINGS=${DISABLE_UNDERVOLT_WARNINGS:=}
165 166
166 167 # Kernel compilation settings
167 168 BUILD_KERNEL=${BUILD_KERNEL:=true}
168 169 KERNEL_REDUCE=${KERNEL_REDUCE:=false}
169 170 KERNEL_THREADS=${KERNEL_THREADS:=1}
170 171 KERNEL_HEADERS=${KERNEL_HEADERS:=true}
171 172 KERNEL_MENUCONFIG=${KERNEL_MENUCONFIG:=false}
172 173 KERNEL_REMOVESRC=${KERNEL_REMOVESRC:=true}
173 174 KERNEL_OLDDEFCONFIG=${KERNEL_OLDDEFCONFIG:=false}
174 175 KERNEL_CCACHE=${KERNEL_CCACHE:=false}
175 176 KERNEL_ZSWAP=${KERNEL_ZSWAP:=false}
176 177 KERNEL_VIRT=${KERNEL_VIRT:=false}
177 178 KERNEL_BPF=${KERNEL_BPF:=true}
178 179
179 180 # Kernel compilation from source directory settings
180 181 KERNELSRC_DIR=${KERNELSRC_DIR:=""}
181 182 KERNELSRC_CLEAN=${KERNELSRC_CLEAN:=false}
182 183 KERNELSRC_CONFIG=${KERNELSRC_CONFIG:=true}
183 184 KERNELSRC_PREBUILT=${KERNELSRC_PREBUILT:=false}
184 185
185 186 # Reduce disk usage settings
186 187 REDUCE_APT=${REDUCE_APT:=true}
187 188 REDUCE_DOC=${REDUCE_DOC:=true}
188 189 REDUCE_MAN=${REDUCE_MAN:=true}
189 190 REDUCE_VIM=${REDUCE_VIM:=false}
190 191 REDUCE_BASH=${REDUCE_BASH:=false}
191 192 REDUCE_HWDB=${REDUCE_HWDB:=true}
192 193 REDUCE_SSHD=${REDUCE_SSHD:=true}
193 194 REDUCE_LOCALE=${REDUCE_LOCALE:=true}
194 195
195 196 # Encrypted filesystem settings
196 197 ENABLE_CRYPTFS=${ENABLE_CRYPTFS:=false}
197 198 CRYPTFS_PASSWORD=${CRYPTFS_PASSWORD:=""}
198 199 CRYPTFS_MAPPING=${CRYPTFS_MAPPING:="secure"}
199 200 CRYPTFS_CIPHER=${CRYPTFS_CIPHER:="aes-xts-plain64:sha512"}
200 201 CRYPTFS_XTSKEYSIZE=${CRYPTFS_XTSKEYSIZE:=512}
201 202
202 203 # Chroot scripts directory
203 204 CHROOT_SCRIPTS=${CHROOT_SCRIPTS:=""}
204 205
205 206 # Packages required in the chroot build environment
206 207 APT_INCLUDES=${APT_INCLUDES:=""}
207 208 APT_INCLUDES="${APT_INCLUDES},apt-transport-https,apt-utils,ca-certificates,debian-archive-keyring,dialog,sudo,systemd,sysvinit-utils,locales,keyboard-configuration,console-setup"
208 209
209 210 #Packages to exclude from chroot build environment
210 211 APT_EXCLUDES=${APT_EXCLUDES:=""}
211 212
212 213 # Packages required for bootstrapping
213 214 REQUIRED_PACKAGES="debootstrap debian-archive-keyring qemu-user-static binfmt-support dosfstools rsync bmap-tools whois git bc psmisc dbus sudo netselect-apt"
214 215 MISSING_PACKAGES=""
215 216
216 217 # Packages installed for c/c++ build environment in chroot (keep empty)
217 218 COMPILER_PACKAGES=""
218 219
219 220 #If init and systemd-sysv are wanted e.g. halt/reboot/shutdown scripts
220 221 if [ "$ENABLE_SYSVINIT" = false ] ; then
221 222 APT_EXCLUDES="--exclude=${APT_EXCLUDES},init,systemd-sysv"
222 223 fi
223 224
224 225 #Check if apt-cacher-ng has its default port open on and set APT_PROXY
225 226 if [ -n "$(lsof -i :3142)" ] ; then
226 227 HTTP_PROXY=http://127.0.0.1:3142/
227 228 fi
228 229
229 230 #netselect-apt does not know buster yet
230 231 if [ "$RELEASE" = "buster" ] ; then
231 232 RLS=testing
232 233 else
233 234 RLS="$RELEASE"
234 235 fi
235 236
236 237 if [ -f "$(pwd)/files/apt/sources.list" ] ; then
237 238 rm "$(pwd)/files/apt/sources.list"
238 239 fi
239 240
240 241 if [ "$ENABLE_NONFREE" = true ] ; then
241 242 netselect-apt --arch "$RELEASE_ARCH" --tests 10 --sources --nonfree --outfile "$(pwd)/files/apt/sources.list" -d "$RLS"
242 243 else
243 244 netselect-apt --arch "$RELEASE_ARCH" --tests 10 --sources --outfile "$(pwd)/files/apt/sources.list" -d "$RLS"
244 245 fi
245 246
246 247 #sed and cut the result string so we can use it as APT_SERVER
247 248 APT_SERVER=$(grep -m 1 http files/apt/sources.list | sed "s|http://| |g" | cut -d ' ' -f 3 | sed 's|/$|''|')
248 249
249 250 #make script easier and more stable to use with convenient setup switch. Just setup SET_ARCH and RPI_MODEL and your good to go!
250 251 if [ -n "$SET_ARCH" ] ; then
251 252 echo "Setting Architecture specific settings"
252 253 ##################################
253 254 # 64 bit config
254 255 ##################################
255 256 if [ "$SET_ARCH" = 64 ] ; then
256 257 # General 64 bit depended settings
257 258 QEMU_BINARY=${QEMU_BINARY:=/usr/bin/qemu-aarch64-static}
258 259 KERNEL_ARCH=${KERNEL_ARCH:=arm64}
259 260 KERNEL_BIN_IMAGE=${KERNEL_BIN_IMAGE:="Image"}
260 261
261 262 # Board specific settings
262 263 if [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then
263 264 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-arm64"
264 265 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcmrpi3_defconfig}
265 266 RELEASE_ARCH=${RELEASE_ARCH:=arm64}
266 267 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel8.img}
267 268 CROSS_COMPILE=${CROSS_COMPILE:=aarch64-linux-gnu-}
268 269 else
269 270 echo "error: Only Raspberry PI 3 and 3B+ support 64 bit"
270 271 exit 1
271 272 fi
272 273 fi
273 274
274 275 # 32 bit configuration
275 276 if [ "$SET_ARCH" = 32 ] ; then
276 277 # General 32 bit dependend settings
277 278 QEMU_BINARY=${QEMU_BINARY:=/usr/bin/qemu-arm-static}
278 279 KERNEL_ARCH=${KERNEL_ARCH:=arm}
279 280 KERNEL_BIN_IMAGE=${KERNEL_BIN_IMAGE:="zImage"}
280 281
281 282 # Hardware specific settings
282 283 if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 1 ] || [ "$RPI_MODEL" = 1P ] ; then
283 284 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-armel"
284 285 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcmrpi_defconfig}
285 286 RELEASE_ARCH=${RELEASE_ARCH:=armel}
286 287 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel.img}
287 288 CROSS_COMPILE=${CROSS_COMPILE:=arm-linux-gnueabi-}
288 289 fi
289 290
290 291 # Hardware specific settings
291 292 if [ "$RPI_MODEL" = 2 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then
292 293 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-armhf"
293 294 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcm2709_defconfig}
294 295 RELEASE_ARCH=${RELEASE_ARCH:=armhf}
295 296 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel7.img}
296 297 CROSS_COMPILE=${CROSS_COMPILE:=arm-linux-gnueabihf-}
297 298 fi
298 299 fi
299 300 #SET_ARCH not set
300 301 else
301 302 echo "error: Please set '32' or '64' as value for SET_ARCH"
302 303 exit 1
303 304 fi
304 305 # Device specific configuration and U-Boot configuration
305 306 case "$RPI_MODEL" in
306 307 0)
307 308 DTB_FILE=${DTB_FILE:=bcm2708-rpi-0-w.dtb}
308 309 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
309 310 ;;
310 311 1)
311 312 DTB_FILE=${DTB_FILE:=bcm2708-rpi-b.dtb}
312 313 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
313 314 ;;
314 315 1P)
315 316 DTB_FILE=${DTB_FILE:=bcm2708-rpi-b-plus.dtb}
316 317 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
317 318 ;;
318 319 2)
319 320 DTB_FILE=${DTB_FILE:=bcm2709-rpi-2-b.dtb}
320 321 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_2_defconfig}
321 322 ;;
322 323 3)
323 324 DTB_FILE=${DTB_FILE:=bcm2710-rpi-3-b.dtb}
324 325 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_3_defconfig}
325 326 ;;
326 327 3P)
327 328 DTB_FILE=${DTB_FILE:=bcm2710-rpi-3-b.dtb}
328 329 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_3_defconfig}
329 330 ;;
330 331 *)
331 332 echo "error: Raspberry Pi model $RPI_MODEL is not supported!"
332 333 exit 1
333 334 ;;
334 335 esac
335 336
336 337 #DEBUG off
337 338 set +x
338 339
339 340 # Check if the internal wireless interface is supported by the RPi model
340 341 if [ "$ENABLE_WIRELESS" = true ] ; then
341 342 if [ "$RPI_MODEL" = 1 ] || [ "$RPI_MODEL" = 1P ] || [ "$RPI_MODEL" = 2 ] ; then
342 343 echo "error: The selected Raspberry Pi model has no internal wireless interface"
343 344 exit 1
344 345 fi
345 346 fi
346 347
347 348 # Check if DISABLE_UNDERVOLT_WARNINGS parameter value is supported
348 349 if [ -n "$DISABLE_UNDERVOLT_WARNINGS" ] ; then
349 350 if [ "$DISABLE_UNDERVOLT_WARNINGS" != 1 ] && [ "$DISABLE_UNDERVOLT_WARNINGS" != 2 ] ; then
350 351 echo "error: DISABLE_UNDERVOLT_WARNINGS=${DISABLE_UNDERVOLT_WARNINGS} is not supported"
351 352 exit 1
352 353 fi
353 354 fi
354 355
355 356 # Add cmake to compile videocore sources
356 357 if [ "$ENABLE_VIDEOCORE" = true ] ; then
357 358 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} cmake"
358 359 fi
359 360
360 361 # Add libncurses5 to enable kernel menuconfig
361 362 if [ "$KERNEL_MENUCONFIG" = true ] ; then
362 363 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} libncurses-dev"
363 364 fi
364 365
365 366 # Add ccache compiler cache for (faster) kernel cross (re)compilation
366 367 if [ "$KERNEL_CCACHE" = true ] ; then
367 368 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} ccache"
368 369 fi
369 370
370 371 # Add cryptsetup package to enable filesystem encryption
371 372 if [ "$ENABLE_CRYPTFS" = true ] && [ "$BUILD_KERNEL" = true ] ; then
372 373 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} cryptsetup"
373 374 APT_INCLUDES="${APT_INCLUDES},cryptsetup,busybox,console-setup"
374 375
375 376 if [ -z "$CRYPTFS_PASSWORD" ] ; then
376 377 echo "error: no password defined (CRYPTFS_PASSWORD)!"
377 378 exit 1
378 379 fi
379 380 ENABLE_INITRAMFS=true
380 381 fi
381 382
382 383 # Add initramfs generation tools
383 384 if [ "$ENABLE_INITRAMFS" = true ] && [ "$BUILD_KERNEL" = true ] ; then
384 385 APT_INCLUDES="${APT_INCLUDES},initramfs-tools"
385 386 fi
386 387
387 388 # Add device-tree-compiler required for building the U-Boot bootloader
388 389 if [ "$ENABLE_UBOOT" = true ] ; then
389 390 APT_INCLUDES="${APT_INCLUDES},device-tree-compiler,bison,flex,bc"
390 391 else
391 392 if [ "$ENABLE_UBOOTUSB" = true ] ; then
392 393 echo "error: Enabling UBOOTUSB requires u-boot to be enabled"
393 394 exit 1
394 395 fi
395 396 fi
396 397
397 398 # Check if root SSH (v2) public key file exists
398 399 if [ -n "$SSH_ROOT_PUB_KEY" ] ; then
399 400 if [ ! -f "$SSH_ROOT_PUB_KEY" ] ; then
400 401 echo "error: '$SSH_ROOT_PUB_KEY' specified SSH public key file not found (SSH_ROOT_PUB_KEY)!"
401 402 exit 1
402 403 fi
403 404 fi
404 405
405 406 # Check if $USER_NAME SSH (v2) public key file exists
406 407 if [ -n "$SSH_USER_PUB_KEY" ] ; then
407 408 if [ ! -f "$SSH_USER_PUB_KEY" ] ; then
408 409 echo "error: '$SSH_USER_PUB_KEY' specified SSH public key file not found (SSH_USER_PUB_KEY)!"
409 410 exit 1
410 411 fi
411 412 fi
412 413
413 414 # Check if all required packages are installed on the build system
414 415 for package in $REQUIRED_PACKAGES ; do
415 416 if [ "$(dpkg-query -W -f='${Status}' "$package")" != "install ok installed" ] ; then
416 417 MISSING_PACKAGES="${MISSING_PACKAGES} $package"
417 418 fi
418 419 done
419 420
420 421 # If there are missing packages ask confirmation for install, or exit
421 422 if [ -n "$MISSING_PACKAGES" ] ; then
422 423 echo "the following packages needed by this script are not installed:"
423 424 echo "$MISSING_PACKAGES"
424 425
425 426 printf "\ndo you want to install the missing packages right now? [y/n] "
426 427 read -r confirm
427 428 [ "$confirm" != "y" ] && exit 1
428 429
429 430 # Make sure all missing required packages are installed
430 431 apt-get -qq -y install "${MISSING_PACKAGES}"
431 432 fi
432 433
433 434 # Check if ./bootstrap.d directory exists
434 435 if [ ! -d "./bootstrap.d/" ] ; then
435 436 echo "error: './bootstrap.d' required directory not found!"
436 437 exit 1
437 438 fi
438 439
439 440 # Check if ./files directory exists
440 441 if [ ! -d "./files/" ] ; then
441 442 echo "error: './files' required directory not found!"
442 443 exit 1
443 444 fi
444 445
445 446 # Check if specified KERNELSRC_DIR directory exists
446 447 if [ -n "$KERNELSRC_DIR" ] && [ ! -d "$KERNELSRC_DIR" ] ; then
447 448 echo "error: '${KERNELSRC_DIR}' specified directory not found (KERNELSRC_DIR)!"
448 449 exit 1
449 450 fi
450 451
451 452 # Check if specified UBOOTSRC_DIR directory exists
452 453 if [ -n "$UBOOTSRC_DIR" ] && [ ! -d "$UBOOTSRC_DIR" ] ; then
453 454 echo "error: '${UBOOTSRC_DIR}' specified directory not found (UBOOTSRC_DIR)!"
454 455 exit 1
455 456 fi
456 457
457 458 # Check if specified VIDEOCORESRC_DIR directory exists
458 459 if [ -n "$VIDEOCORESRC_DIR" ] && [ ! -d "$VIDEOCORESRC_DIR" ] ; then
459 460 echo "error: '${VIDEOCORESRC_DIR}' specified directory not found (VIDEOCORESRC_DIR)!"
460 461 exit 1
461 462 fi
462 463
463 464 # Check if specified FBTURBOSRC_DIR directory exists
464 465 if [ -n "$FBTURBOSRC_DIR" ] && [ ! -d "$FBTURBOSRC_DIR" ] ; then
465 466 echo "error: '${FBTURBOSRC_DIR}' specified directory not found (FBTURBOSRC_DIR)!"
466 467 exit 1
467 468 fi
468 469
469 470 # Check if specified CHROOT_SCRIPTS directory exists
470 471 if [ -n "$CHROOT_SCRIPTS" ] && [ ! -d "$CHROOT_SCRIPTS" ] ; then
471 472 echo "error: ${CHROOT_SCRIPTS} specified directory not found (CHROOT_SCRIPTS)!"
472 473 exit 1
473 474 fi
474 475
475 476 # Check if specified device mapping already exists (will be used by cryptsetup)
476 477 if [ -r "/dev/mapping/${CRYPTFS_MAPPING}" ] ; then
477 478 echo "error: mapping /dev/mapping/${CRYPTFS_MAPPING} already exists, not proceeding"
478 479 exit 1
479 480 fi
480 481
481 482 # Don't clobber an old build
482 483 if [ -e "$BUILDDIR" ] ; then
483 484 echo "error: directory ${BUILDDIR} already exists, not proceeding"
484 485 exit 1
485 486 fi
486 487
487 488 # Setup chroot directory
488 489 mkdir -p "${R}"
489 490
490 491 # Check if build directory has enough of free disk space >512MB
491 492 if [ "$(df --output=avail "${BUILDDIR}" | sed "1d")" -le "524288" ] ; then
492 493 echo "error: ${BUILDDIR} not enough space left to generate the output image!"
493 494 exit 1
494 495 fi
495 496
496 497 set -x
497 498
498 499 # Call "cleanup" function on various signals and errors
499 500 trap cleanup 0 1 2 3 6
500 501
501 502 # Add required packages for the minbase installation
502 503 if [ "$ENABLE_MINBASE" = true ] ; then
503 504 APT_INCLUDES="${APT_INCLUDES},vim-tiny,netbase,net-tools,ifupdown"
504 505 fi
505 506
506 507 # Add parted package, required to get partprobe utility
507 508 if [ "$EXPANDROOT" = true ] ; then
508 509 APT_INCLUDES="${APT_INCLUDES},parted"
509 510 fi
510 511
511 512 # Add dbus package, recommended if using systemd
512 513 if [ "$ENABLE_DBUS" = true ] ; then
513 514 APT_INCLUDES="${APT_INCLUDES},dbus"
514 515 fi
515 516
516 517 # Add iptables IPv4/IPv6 package
517 518 if [ "$ENABLE_IPTABLES" = true ] ; then
518 519 APT_INCLUDES="${APT_INCLUDES},iptables,iptables-persistent"
519 520 fi
520 521
521 522 # Add openssh server package
522 523 if [ "$ENABLE_SSHD" = true ] ; then
523 524 APT_INCLUDES="${APT_INCLUDES},openssh-server"
524 525 fi
525 526
526 527 # Add alsa-utils package
527 528 if [ "$ENABLE_SOUND" = true ] ; then
528 529 APT_INCLUDES="${APT_INCLUDES},alsa-utils"
529 530 fi
530 531
531 532 # Add rng-tools package
532 533 if [ "$ENABLE_HWRANDOM" = true ] ; then
533 534 APT_INCLUDES="${APT_INCLUDES},rng-tools"
534 535 fi
535 536
536 537 # Add fbturbo video driver
537 538 if [ "$ENABLE_FBTURBO" = true ] ; then
538 539 # Enable xorg package dependencies
539 540 ENABLE_XORG=true
540 541 fi
541 542
542 543 # Add user defined window manager package
543 544 if [ -n "$ENABLE_WM" ] ; then
544 545 APT_INCLUDES="${APT_INCLUDES},${ENABLE_WM}"
545 546
546 547 # Enable xorg package dependencies
547 548 ENABLE_XORG=true
548 549 fi
549 550
550 551 # Add xorg package
551 552 if [ "$ENABLE_XORG" = true ] ; then
552 553 APT_INCLUDES="${APT_INCLUDES},xorg,dbus-x11"
553 554 fi
554 555
555 556 # Replace selected packages with smaller clones
556 557 if [ "$ENABLE_REDUCE" = true ] ; then
557 558 # Add levee package instead of vim-tiny
558 559 if [ "$REDUCE_VIM" = true ] ; then
559 560 APT_INCLUDES="$(echo ${APT_INCLUDES} | sed "s/vim-tiny/levee/")"
560 561 fi
561 562
562 563 # Add dropbear package instead of openssh-server
563 564 if [ "$REDUCE_SSHD" = true ] ; then
564 565 APT_INCLUDES="$(echo "${APT_INCLUDES}" | sed "s/openssh-server/dropbear/")"
565 566 fi
566 567 fi
567 568
568 569 # Configure systemd-sysv exclude to make halt/reboot/shutdown scripts available
569 570 if [ "$ENABLE_SYSVINIT" = false ] ; then
570 571 APT_EXCLUDES="--exclude=${APT_EXCLUDES},init,systemd-sysv"
571 572 fi
572 573
573 574 # Configure kernel sources if no KERNELSRC_DIR
574 575 if [ "$BUILD_KERNEL" = true ] && [ -z "$KERNELSRC_DIR" ] ; then
575 576 KERNELSRC_CONFIG=true
576 577 fi
577 578
578 579 # Configure reduced kernel
579 580 if [ "$KERNEL_REDUCE" = true ] ; then
580 581 KERNELSRC_CONFIG=false
581 582 fi
582 583
583 584 # Configure qemu compatible kernel
584 585 if [ "$ENABLE_QEMU" = true ] ; then
585 586 DTB_FILE=vexpress-v2p-ca15_a7.dtb
586 587 UBOOT_CONFIG=vexpress_ca15_tc2_defconfig
587 588 KERNEL_DEFCONFIG="vexpress_defconfig"
588 589 if [ "$KERNEL_MENUCONFIG" = false ] ; then
589 590 KERNEL_OLDDEFCONFIG=true
590 591 fi
591 592 fi
592 593
593 594 # Execute bootstrap scripts
594 595 for SCRIPT in bootstrap.d/*.sh; do
595 596 head -n 3 "$SCRIPT"
596 597 . "$SCRIPT"
597 598 done
598 599
599 600 ## Execute custom bootstrap scripts
600 601 if [ -d "custom.d" ] ; then
601 602 for SCRIPT in custom.d/*.sh; do
602 603 . "$SCRIPT"
603 604 done
604 605 fi
605 606
606 607 # Execute custom scripts inside the chroot
607 608 if [ -n "$CHROOT_SCRIPTS" ] && [ -d "$CHROOT_SCRIPTS" ] ; then
608 609 cp -r "${CHROOT_SCRIPTS}" "${R}/chroot_scripts"
609 610 chroot_exec /bin/bash -x <<'EOF'
610 611 for SCRIPT in /chroot_scripts/* ; do
611 612 if [ -f $SCRIPT -a -x $SCRIPT ] ; then
612 613 $SCRIPT
613 614 fi
614 615 done
615 616 EOF
616 617 rm -rf "${R}/chroot_scripts"
617 618 fi
618 619
619 620 # Remove c/c++ build environment from the chroot
620 621 chroot_remove_cc
621 622
622 623 # Generate required machine-id
623 624 MACHINE_ID=$(dbus-uuidgen)
624 625 echo -n "${MACHINE_ID}" > "${R}/var/lib/dbus/machine-id"
625 626 echo -n "${MACHINE_ID}" > "${ETC_DIR}/machine-id"
626 627
627 628 # APT Cleanup
628 629 chroot_exec apt-get -y clean
629 630 chroot_exec apt-get -y autoclean
630 631 chroot_exec apt-get -y autoremove
631 632
632 633 # Unmount mounted filesystems
633 634 umount -l "${R}/proc"
634 635 umount -l "${R}/sys"
635 636
636 637 # Clean up directories
637 638 rm -rf "${R}/run/*"
638 639 rm -rf "${R}/tmp/*"
639 640
640 641 # Clean up files
641 642 rm -f "${ETC_DIR}/ssh/ssh_host_*"
642 643 rm -f "${ETC_DIR}/dropbear/dropbear_*"
643 644 rm -f "${ETC_DIR}/apt/sources.list.save"
644 645 rm -f "${ETC_DIR}/resolvconf/resolv.conf.d/original"
645 646 rm -f "${ETC_DIR}/*-"
646 647 rm -f "${ETC_DIR}/apt/apt.conf.d/10proxy"
647 648 rm -f "${ETC_DIR}/resolv.conf"
648 649 rm -f "${R}/root/.bash_history"
649 650 rm -f "${R}/var/lib/urandom/random-seed"
650 651 rm -f "${R}/initrd.img"
651 652 rm -f "${R}/vmlinuz"
652 653 rm -f "${R}${QEMU_BINARY}"
653 654
654 655 if [ "$ENABLE_QEMU" = true ] ; then
655 656 # Setup QEMU directory
656 657 mkdir "${BASEDIR}/qemu"
657 658
658 659 # Copy kernel image to QEMU directory
659 660 install_readonly "${BOOT_DIR}/${KERNEL_IMAGE}" "${BASEDIR}/qemu/${KERNEL_IMAGE}"
660 661
661 662 # Copy kernel config to QEMU directory
662 663 install_readonly "${R}/boot/config-${KERNEL_VERSION}" "${BASEDIR}/qemu/config-${KERNEL_VERSION}"
663 664
664 665 # Copy kernel dtbs to QEMU directory
665 666 for dtb in "${BOOT_DIR}/"*.dtb ; do
666 667 if [ -f "${dtb}" ] ; then
667 668 install_readonly "${dtb}" "${BASEDIR}/qemu/"
668 669 fi
669 670 done
670 671
671 672 # Copy kernel overlays to QEMU directory
672 673 if [ -d "${BOOT_DIR}/overlays" ] ; then
673 674 # Setup overlays dtbs directory
674 675 mkdir "${BASEDIR}/qemu/overlays"
675 676
676 677 for dtb in "${BOOT_DIR}/overlays/"*.dtb ; do
677 678 if [ -f "${dtb}" ] ; then
678 679 install_readonly "${dtb}" "${BASEDIR}/qemu/overlays/"
679 680 fi
680 681 done
681 682 fi
682 683
683 684 # Copy u-boot files to QEMU directory
684 685 if [ "$ENABLE_UBOOT" = true ] ; then
685 686 if [ -f "${BOOT_DIR}/u-boot.bin" ] ; then
686 687 install_readonly "${BOOT_DIR}/u-boot.bin" "${BASEDIR}/qemu/u-boot.bin"
687 688 fi
688 689 if [ -f "${BOOT_DIR}/uboot.mkimage" ] ; then
689 690 install_readonly "${BOOT_DIR}/uboot.mkimage" "${BASEDIR}/qemu/uboot.mkimage"
690 691 fi
691 692 if [ -f "${BOOT_DIR}/boot.scr" ] ; then
692 693 install_readonly "${BOOT_DIR}/boot.scr" "${BASEDIR}/qemu/boot.scr"
693 694 fi
694 695 fi
695 696
696 697 # Copy initramfs to QEMU directory
697 698 if [ -f "${BOOT_DIR}/initramfs-${KERNEL_VERSION}" ] ; then
698 699 install_readonly "${BOOT_DIR}/initramfs-${KERNEL_VERSION}" "${BASEDIR}/qemu/initramfs-${KERNEL_VERSION}"
699 700 fi
700 701 fi
701 702
702 703 # Calculate size of the chroot directory in KB
703 704 CHROOT_SIZE=$(expr "$(du -s "${R}" | awk '{ print $1 }')")
704 705
705 706 # Calculate the amount of needed 512 Byte sectors
706 707 TABLE_SECTORS=$(expr 1 \* 1024 \* 1024 \/ 512)
707 708 FRMW_SECTORS=$(expr 64 \* 1024 \* 1024 \/ 512)
708 709 ROOT_OFFSET=$(expr "${TABLE_SECTORS}" + "${FRMW_SECTORS}")
709 710
710 711 # The root partition is EXT4
711 712 # This means more space than the actual used space of the chroot is used.
712 713 # As overhead for journaling and reserved blocks 35% are added.
713 714 ROOT_SECTORS=$(expr "$(expr "${CHROOT_SIZE}" + "${CHROOT_SIZE}" \/ 100 \* 35)" \* 1024 \/ 512)
714 715
715 716 # Calculate required image size in 512 Byte sectors
716 717 IMAGE_SECTORS=$(expr "${TABLE_SECTORS}" + "${FRMW_SECTORS}" + "${ROOT_SECTORS}")
717 718
718 719 # Prepare image file
719 720 if [ "$ENABLE_SPLITFS" = true ] ; then
720 721 dd if=/dev/zero of="$IMAGE_NAME-frmw.img" bs=512 count="${TABLE_SECTORS}"
721 722 dd if=/dev/zero of="$IMAGE_NAME-frmw.img" bs=512 count=0 seek="${FRMW_SECTORS}"
722 723 dd if=/dev/zero of="$IMAGE_NAME-root.img" bs=512 count="${TABLE_SECTORS}"
723 724 dd if=/dev/zero of="$IMAGE_NAME-root.img" bs=512 count=0 seek="${ROOT_SECTORS}"
724 725
725 726 # Write firmware/boot partition tables
726 727 sfdisk -q -L -uS -f "$IMAGE_NAME-frmw.img" 2> /dev/null <<EOM
727 728 ${TABLE_SECTORS},${FRMW_SECTORS},c,*
728 729 EOM
729 730
730 731 # Write root partition table
731 732 sfdisk -q -L -uS -f "$IMAGE_NAME-root.img" 2> /dev/null <<EOM
732 733 ${TABLE_SECTORS},${ROOT_SECTORS},83
733 734 EOM
734 735
735 736 # Setup temporary loop devices
736 737 FRMW_LOOP="$(losetup -o 1M --sizelimit 64M -f --show "$IMAGE_NAME"-frmw.img)"
737 738 ROOT_LOOP="$(losetup -o 1M -f --show "$IMAGE_NAME"-root.img)"
738 739 else # ENABLE_SPLITFS=false
739 740 dd if=/dev/zero of="$IMAGE_NAME.img" bs=512 count="${TABLE_SECTORS}"
740 741 dd if=/dev/zero of="$IMAGE_NAME.img" bs=512 count=0 seek="${IMAGE_SECTORS}"
741 742
742 743 # Write partition table
743 744 sfdisk -q -L -uS -f "$IMAGE_NAME.img" 2> /dev/null <<EOM
744 745 ${TABLE_SECTORS},${FRMW_SECTORS},c,*
745 746 ${ROOT_OFFSET},${ROOT_SECTORS},83
746 747 EOM
747 748
748 749 # Setup temporary loop devices
749 750 FRMW_LOOP="$(losetup -o 1M --sizelimit 64M -f --show "$IMAGE_NAME".img)"
750 751 ROOT_LOOP="$(losetup -o 65M -f --show "$IMAGE_NAME".img)"
751 752 fi
752 753
753 754 if [ "$ENABLE_CRYPTFS" = true ] ; then
754 755 # Create dummy ext4 fs
755 756 mkfs.ext4 "$ROOT_LOOP"
756 757
757 758 # Setup password keyfile
758 759 touch .password
759 760 chmod 600 .password
760 761 echo -n ${CRYPTFS_PASSWORD} > .password
761 762
762 763 # Initialize encrypted partition
763 764 echo "YES" | cryptsetup luksFormat "${ROOT_LOOP}" -c "${CRYPTFS_CIPHER}" -s "${CRYPTFS_XTSKEYSIZE}" .password
764 765
765 766 # Open encrypted partition and setup mapping
766 767 cryptsetup luksOpen "${ROOT_LOOP}" -d .password "${CRYPTFS_MAPPING}"
767 768
768 769 # Secure delete password keyfile
769 770 shred -zu .password
770 771
771 772 # Update temporary loop device
772 773 ROOT_LOOP="/dev/mapper/${CRYPTFS_MAPPING}"
773 774
774 775 # Wipe encrypted partition (encryption cipher is used for randomness)
775 776 dd if=/dev/zero of="${ROOT_LOOP}" bs=512 count="$(blockdev --getsz "${ROOT_LOOP}")"
776 777 fi
777 778
778 779 # Build filesystems
779 780 mkfs.vfat "$FRMW_LOOP"
780 781 mkfs.ext4 "$ROOT_LOOP"
781 782
782 783 # Mount the temporary loop devices
783 784 mkdir -p "$BUILDDIR/mount"
784 785 mount "$ROOT_LOOP" "$BUILDDIR/mount"
785 786
786 787 mkdir -p "$BUILDDIR/mount/boot/firmware"
787 788 mount "$FRMW_LOOP" "$BUILDDIR/mount/boot/firmware"
788 789
789 790 # Copy all files from the chroot to the loop device mount point directory
790 791 rsync -a "${R}/" "$BUILDDIR/mount/"
791 792
792 793 # Unmount all temporary loop devices and mount points
793 794 cleanup
794 795
795 796 # Create block map file(s) of image(s)
796 797 if [ "$ENABLE_SPLITFS" = true ] ; then
797 798 # Create block map files for "bmaptool"
798 799 bmaptool create -o "$IMAGE_NAME-frmw.bmap" "$IMAGE_NAME-frmw.img"
799 800 bmaptool create -o "$IMAGE_NAME-root.bmap" "$IMAGE_NAME-root.img"
800 801
801 802 # Image was successfully created
802 803 echo "$IMAGE_NAME-frmw.img ($(expr \( "${TABLE_SECTORS}" + "${FRMW_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
803 804 echo "$IMAGE_NAME-root.img ($(expr \( "${TABLE_SECTORS}" + "${ROOT_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
804 805 else
805 806 # Create block map file for "bmaptool"
806 807 bmaptool create -o "$IMAGE_NAME.bmap" "$IMAGE_NAME.img"
807 808
808 809 # Image was successfully created
809 810 echo "$IMAGE_NAME.img ($(expr \( "${TABLE_SECTORS}" + "${FRMW_SECTORS}" + "${ROOT_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
810 811
811 812 # Create qemu qcow2 image
812 813 if [ "$ENABLE_QEMU" = true ] ; then
813 814 QEMU_IMAGE=${QEMU_IMAGE:=${BASEDIR}/qemu/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
814 815 QEMU_SIZE=16G
815 816
816 817 qemu-img convert -f raw -O qcow2 "$IMAGE_NAME".img "$QEMU_IMAGE".qcow2
817 818 qemu-img resize "$QEMU_IMAGE".qcow2 $QEMU_SIZE
818 819
819 820 echo "$QEMU_IMAGE.qcow2 ($QEMU_SIZE)" ": successfully created"
820 821 fi
821 822 fi
General Comments 0
Vous devez vous connecter pour laisser un commentaire. Se connecter maintenant