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