##// END OF EJS Templates
a
Unknown -
r448:8a0c6dc945ab
parent child
Show More
@@ -1,842 +1,842
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 NEXMON_URL=${NEXMON_URL:=https://github.com/seemoo-lab/nexmon.git}
67 67
68 68 # Build directories
69 69 WORKDIR=$(pwd)
70 70 BASEDIR=${BASEDIR:=${WORKDIR}/images/${RELEASE}}
71 71 BUILDDIR="${BASEDIR}/build"
72 72
73 73 # Chroot directories
74 74 R="${BUILDDIR}/chroot"
75 75 ETC_DIR="${R}/etc"
76 76 LIB_DIR="${R}/lib"
77 77 BOOT_DIR="${R}/boot/firmware"
78 78 KERNEL_DIR="${R}/usr/src/linux"
79 79 WLAN_FIRMWARE_DIR="${LIB_DIR}/firmware/brcm"
80 80 BLUETOOTH_FIRMWARE_DIR="${ETC_DIR}/firmware/bt"
81 81
82 82 # Firmware directory: Blank if download from github
83 83 RPI_FIRMWARE_DIR=${RPI_FIRMWARE_DIR:=""}
84 84
85 85 # General settings
86 86 SET_ARCH=${SET_ARCH:=32}
87 87 HOSTNAME=${HOSTNAME:=rpi${RPI_MODEL}-${RELEASE}}
88 88 PASSWORD=${PASSWORD:=raspberry}
89 89 USER_PASSWORD=${USER_PASSWORD:=raspberry}
90 90 DEFLOCAL=${DEFLOCAL:="en_US.UTF-8"}
91 91 TIMEZONE=${TIMEZONE:="Europe/Berlin"}
92 92 EXPANDROOT=${EXPANDROOT:=true}
93 93
94 94 # Keyboard settings
95 95 XKB_MODEL=${XKB_MODEL:=""}
96 96 XKB_LAYOUT=${XKB_LAYOUT:=""}
97 97 XKB_VARIANT=${XKB_VARIANT:=""}
98 98 XKB_OPTIONS=${XKB_OPTIONS:=""}
99 99
100 100 # Network settings (DHCP)
101 101 ENABLE_DHCP=${ENABLE_DHCP:=true}
102 102
103 103 # Network settings (static)
104 104 NET_ADDRESS=${NET_ADDRESS:=""}
105 105 NET_GATEWAY=${NET_GATEWAY:=""}
106 106 NET_DNS_1=${NET_DNS_1:=""}
107 107 NET_DNS_2=${NET_DNS_2:=""}
108 108 NET_DNS_DOMAINS=${NET_DNS_DOMAINS:=""}
109 109 NET_NTP_1=${NET_NTP_1:=""}
110 110 NET_NTP_2=${NET_NTP_2:=""}
111 111
112 112 # APT settings
113 113 APT_PROXY=${APT_PROXY:=""}
114 114 APT_SERVER=${APT_SERVER:="ftp.debian.org"}
115 115
116 116 # Feature settings
117 117 ENABLE_PRINTK=${ENABLE_PRINTK:=false}
118 118 ENABLE_BLUETOOTH=${ENABLE_BLUETOOTH:=false}
119 119 ENABLE_MINIUART_OVERLAY=${ENABLE_MINIUART_OVERLAY:=false}
120 120 ENABLE_CONSOLE=${ENABLE_CONSOLE:=true}
121 121 ENABLE_I2C=${ENABLE_I2C:=false}
122 122 ENABLE_SPI=${ENABLE_SPI:=false}
123 123 ENABLE_IPV6=${ENABLE_IPV6:=true}
124 124 ENABLE_SSHD=${ENABLE_SSHD:=true}
125 125 ENABLE_NONFREE=${ENABLE_NONFREE:=false}
126 126 ENABLE_WIRELESS=${ENABLE_WIRELESS:=false}
127 127 ENABLE_SOUND=${ENABLE_SOUND:=true}
128 128 ENABLE_DBUS=${ENABLE_DBUS:=true}
129 129 ENABLE_HWRANDOM=${ENABLE_HWRANDOM:=true}
130 130 ENABLE_MINGPU=${ENABLE_MINGPU:=false}
131 131 ENABLE_XORG=${ENABLE_XORG:=false}
132 132 ENABLE_WM=${ENABLE_WM:=""}
133 133 ENABLE_RSYSLOG=${ENABLE_RSYSLOG:=true}
134 134 ENABLE_USER=${ENABLE_USER:=true}
135 135 USER_NAME=${USER_NAME:="pi"}
136 136 ENABLE_ROOT=${ENABLE_ROOT:=false}
137 137 ENABLE_QEMU=${ENABLE_QEMU:=false}
138 138 ENABLE_SYSVINIT=${ENABLE_SYSVINIT:=false}
139 139
140 140 # SSH settings
141 141 SSH_ENABLE_ROOT=${SSH_ENABLE_ROOT:=false}
142 142 SSH_DISABLE_PASSWORD_AUTH=${SSH_DISABLE_PASSWORD_AUTH:=false}
143 143 SSH_LIMIT_USERS=${SSH_LIMIT_USERS:=false}
144 144 SSH_ROOT_PUB_KEY=${SSH_ROOT_PUB_KEY:=""}
145 145 SSH_USER_PUB_KEY=${SSH_USER_PUB_KEY:=""}
146 146
147 147 # Advanced settings
148 148 ENABLE_MINBASE=${ENABLE_MINBASE:=false}
149 149 ENABLE_REDUCE=${ENABLE_REDUCE:=false}
150 150 ENABLE_UBOOT=${ENABLE_UBOOT:=false}
151 151 UBOOTSRC_DIR=${UBOOTSRC_DIR:=""}
152 152 ENABLE_UBOOTUSB=${ENABLE_UBOOTUSB=false}
153 153 ENABLE_FBTURBO=${ENABLE_FBTURBO:=false}
154 154 ENABLE_VIDEOCORE=${ENABLE_VIDEOCORE:=false}
155 155 ENABLE_NEXMON=${ENABLE_NEXMON:="false"}
156 156 VIDEOCORESRC_DIR=${VIDEOCORESRC_DIR:=""}
157 157 FBTURBOSRC_DIR=${FBTURBOSRC_DIR:=""}
158 158 NEXMON_DIR=${NEXMON_DIR:=""}
159 159 ENABLE_HARDNET=${ENABLE_HARDNET:=false}
160 160 ENABLE_IPTABLES=${ENABLE_IPTABLES:=false}
161 161 ENABLE_SPLITFS=${ENABLE_SPLITFS:=false}
162 162 ENABLE_INITRAMFS=${ENABLE_INITRAMFS:=false}
163 163 ENABLE_IFNAMES=${ENABLE_IFNAMES:=true}
164 164 DISABLE_UNDERVOLT_WARNINGS=${DISABLE_UNDERVOLT_WARNINGS:=}
165 165
166 166 # Kernel compilation settings
167 167 BUILD_KERNEL=${BUILD_KERNEL:=true}
168 168 KERNEL_REDUCE=${KERNEL_REDUCE:=false}
169 169 KERNEL_THREADS=${KERNEL_THREADS:=1}
170 170 KERNEL_HEADERS=${KERNEL_HEADERS:=true}
171 171 KERNEL_MENUCONFIG=${KERNEL_MENUCONFIG:=false}
172 172 KERNEL_REMOVESRC=${KERNEL_REMOVESRC:=true}
173 173 KERNEL_OLDDEFCONFIG=${KERNEL_OLDDEFCONFIG:=false}
174 174 KERNEL_CCACHE=${KERNEL_CCACHE:=false}
175 175 KERNEL_ZSWAP=${KERNEL_ZSWAP:=false}
176 176 KERNEL_VIRT=${KERNEL_VIRT:=false}
177 177 KERNEL_BPF=${KERNEL_BPF:=false}
178 178
179 179 # Kernel compilation from source directory settings
180 180 KERNELSRC_DIR=${KERNELSRC_DIR:=""}
181 181 KERNELSRC_CLEAN=${KERNELSRC_CLEAN:=false}
182 182 KERNELSRC_CONFIG=${KERNELSRC_CONFIG:=true}
183 183 KERNELSRC_PREBUILT=${KERNELSRC_PREBUILT:=false}
184 184
185 185 # Reduce disk usage settings
186 186 REDUCE_APT=${REDUCE_APT:=true}
187 187 REDUCE_DOC=${REDUCE_DOC:=true}
188 188 REDUCE_MAN=${REDUCE_MAN:=true}
189 189 REDUCE_VIM=${REDUCE_VIM:=false}
190 190 REDUCE_BASH=${REDUCE_BASH:=false}
191 191 REDUCE_HWDB=${REDUCE_HWDB:=true}
192 192 REDUCE_SSHD=${REDUCE_SSHD:=true}
193 193 REDUCE_LOCALE=${REDUCE_LOCALE:=true}
194 194
195 195 # Encrypted filesystem settings
196 196 ENABLE_CRYPTFS=${ENABLE_CRYPTFS:=false}
197 197 CRYPTFS_PASSWORD=${CRYPTFS_PASSWORD:=""}
198 198 CRYPTFS_MAPPING=${CRYPTFS_MAPPING:="secure"}
199 199 CRYPTFS_CIPHER=${CRYPTFS_CIPHER:="aes-xts-plain64:sha512"}
200 200 CRYPTFS_XTSKEYSIZE=${CRYPTFS_XTSKEYSIZE:=512}
201 201 #Dropbear-initramfs supports unlocking encrypted filesystem via SSH on bootup
202 CRYPTFS_DROPBEAR=${CRYPTFS_DROPBEAR:=true}
202 CRYPTFS_DROPBEAR=${CRYPTFS_DROPBEAR:=false}
203 203 #Provide your own Dropbear Public RSA-OpenSSH Key otherwise it will be generated
204 204 CRYPTFS_DROPBEAR_PUBKEY=${CRYPTFS_DROPBEAR_PUBKEY:=""}
205 205
206 206 # Chroot scripts directory
207 207 CHROOT_SCRIPTS=${CHROOT_SCRIPTS:=""}
208 208
209 209 # Packages required in the chroot build environment
210 210 APT_INCLUDES=${APT_INCLUDES:=""}
211 211 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"
212 212
213 213 #Packages to exclude from chroot build environment
214 214 APT_EXCLUDES=${APT_EXCLUDES:=""}
215 215
216 216 # Packages required for bootstrapping
217 217 REQUIRED_PACKAGES="debootstrap debian-archive-keyring qemu-user-static binfmt-support dosfstools rsync bmap-tools whois git bc psmisc dbus sudo netselect-apt"
218 218 MISSING_PACKAGES=""
219 219
220 220 # Packages installed for c/c++ build environment in chroot (keep empty)
221 221 COMPILER_PACKAGES=""
222 222
223 223 set +x
224 224
225 225 #Check if apt-cacher-ng has port 3142 open and set APT_PROXY
226 226 APT_CACHER_RUNNING=$(lsof -i :3142 | grep apt-cacher-ng | cut -d ' ' -f3 | uniq)
227 227 if [ -n "${APT_CACHER_RUNNING}" ] ; then
228 228 APT_PROXY=http://127.0.0.1:3142/
229 229 fi
230 230
231 231 #netselect-apt does not know buster yet
232 232 if [ "$RELEASE" = "buster" ] ; then
233 233 RLS=testing
234 234 else
235 235 RLS="$RELEASE"
236 236 fi
237 237
238 238 if [ -f "$(pwd)/files/apt/sources.list" ] ; then
239 239 rm "$(pwd)/files/apt/sources.list"
240 240 fi
241 241
242 242 if [ "$ENABLE_NONFREE" = true ] ; then
243 243 netselect-apt --arch "$RELEASE_ARCH" --tests 10 --sources --nonfree --outfile "$(pwd)/files/apt/sources.list" -d "$RLS"
244 244 else
245 245 netselect-apt --arch "$RELEASE_ARCH" --tests 10 --sources --outfile "$(pwd)/files/apt/sources.list" -d "$RLS"
246 246 fi
247 247
248 248 #sed and cut the result string so we can use it as APT_SERVER
249 249 APT_SERVER=$(grep -m 1 http files/apt/sources.list | sed "s|http://| |g" | cut -d ' ' -f 3 | sed 's|/$|''|')
250 250
251 251 #make script easier and more stable to use with convenient setup switch. Just setup SET_ARCH and RPI_MODEL and your good to go!
252 252 if [ -n "$SET_ARCH" ] ; then
253 253 # 64 bit configuration
254 254 if [ "$SET_ARCH" = 64 ] ; then
255 255 # General 64 bit depended settings
256 256 QEMU_BINARY=${QEMU_BINARY:=/usr/bin/qemu-aarch64-static}
257 257 KERNEL_ARCH=${KERNEL_ARCH:=arm64}
258 258 KERNEL_BIN_IMAGE=${KERNEL_BIN_IMAGE:="Image"}
259 259
260 260 # Board specific settings
261 261 if [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then
262 262 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-arm64"
263 263 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcmrpi3_defconfig}
264 264 RELEASE_ARCH=${RELEASE_ARCH:=arm64}
265 265 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel8.img}
266 266 CROSS_COMPILE=${CROSS_COMPILE:=aarch64-linux-gnu-}
267 267 else
268 268 echo "error: Only Raspberry PI 3 and 3B+ support 64 bit"
269 269 exit 1
270 270 fi
271 271 fi
272 272
273 273 # 32 bit configuration
274 274 if [ "$SET_ARCH" = 32 ] ; then
275 275 # General 32 bit dependend settings
276 276 QEMU_BINARY=${QEMU_BINARY:=/usr/bin/qemu-arm-static}
277 277 KERNEL_ARCH=${KERNEL_ARCH:=arm}
278 278 KERNEL_BIN_IMAGE=${KERNEL_BIN_IMAGE:="zImage"}
279 279
280 280 # Hardware specific settings
281 281 if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 1 ] || [ "$RPI_MODEL" = 1P ] ; then
282 282 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-armel"
283 283 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcmrpi_defconfig}
284 284 RELEASE_ARCH=${RELEASE_ARCH:=armel}
285 285 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel.img}
286 286 CROSS_COMPILE=${CROSS_COMPILE:=arm-linux-gnueabi-}
287 287 fi
288 288
289 289 # Hardware specific settings
290 290 if [ "$RPI_MODEL" = 2 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then
291 291 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-armhf"
292 292 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcm2709_defconfig}
293 293 RELEASE_ARCH=${RELEASE_ARCH:=armhf}
294 294 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel7.img}
295 295 CROSS_COMPILE=${CROSS_COMPILE:=arm-linux-gnueabihf-}
296 296 fi
297 297 fi
298 298 #SET_ARCH not set
299 299 else
300 300 echo "error: Please set '32' or '64' as value for SET_ARCH"
301 301 exit 1
302 302 fi
303 303 # Device specific configuration and U-Boot configuration
304 304 case "$RPI_MODEL" in
305 305 0)
306 306 DTB_FILE=${DTB_FILE:=bcm2708-rpi-0-w.dtb}
307 307 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
308 308 ;;
309 309 1)
310 310 DTB_FILE=${DTB_FILE:=bcm2708-rpi-b.dtb}
311 311 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
312 312 ;;
313 313 1P)
314 314 DTB_FILE=${DTB_FILE:=bcm2708-rpi-b-plus.dtb}
315 315 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_defconfig}
316 316 ;;
317 317 2)
318 318 DTB_FILE=${DTB_FILE:=bcm2709-rpi-2-b.dtb}
319 319 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_2_defconfig}
320 320 ;;
321 321 3)
322 322 DTB_FILE=${DTB_FILE:=bcm2710-rpi-3-b.dtb}
323 323 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_3_defconfig}
324 324 ;;
325 325 3P)
326 326 DTB_FILE=${DTB_FILE:=bcm2710-rpi-3-b.dtb}
327 327 UBOOT_CONFIG=${UBOOT_CONFIG:=rpi_3_defconfig}
328 328 ;;
329 329 *)
330 330 echo "error: Raspberry Pi model $RPI_MODEL is not supported!"
331 331 exit 1
332 332 ;;
333 333 esac
334 334
335 335 # Raspberry PI 0,3,3P with Bluetooth and Wifi onboard
336 336 if [ "$RPI_MODEL" = 0 ] || [ "$RPI_MODEL" = 3 ] || [ "$RPI_MODEL" = 3P ] ; then
337 337 # Include bluetooth packages on supported boards
338 338 if [ "$ENABLE_BLUETOOTH" = true ] && [ "$ENABLE_CONSOLE" = false ]; then
339 339 APT_INCLUDES="${APT_INCLUDES},bluetooth,bluez"
340 340 fi
341 341 else # Raspberry PI 1,1P,2 without Wifi and bluetooth onboard
342 342 # Check if the internal wireless interface is not supported by the RPi model
343 343 if [ "$ENABLE_WIRELESS" = true ] || [ "$ENABLE_BLUETOOTH" = true ]; then
344 344 echo "error: The selected Raspberry Pi model has no integrated interface for wireless or bluetooth"
345 345 exit 1
346 346 fi
347 347 fi
348 348
349 349 # Prepare date string for default image file name
350 350 DATE="$(date +%Y-%m-%d)"
351 351 if [ -z "$KERNEL_BRANCH" ] ; then
352 352 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
353 353 else
354 354 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-${KERNEL_BRANCH}-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
355 355 fi
356 356
357 357 # Check if DISABLE_UNDERVOLT_WARNINGS parameter value is supported
358 358 if [ -n "$DISABLE_UNDERVOLT_WARNINGS" ] ; then
359 359 if [ "$DISABLE_UNDERVOLT_WARNINGS" != 1 ] && [ "$DISABLE_UNDERVOLT_WARNINGS" != 2 ] ; then
360 360 echo "error: DISABLE_UNDERVOLT_WARNINGS=${DISABLE_UNDERVOLT_WARNINGS} is not supported"
361 361 exit 1
362 362 fi
363 363 fi
364 364
365 365 # Add cmake to compile videocore sources
366 366 if [ "$ENABLE_VIDEOCORE" = true ] ; then
367 367 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} cmake"
368 368 fi
369 369
370 370 # Add libncurses5 to enable kernel menuconfig
371 371 if [ "$KERNEL_MENUCONFIG" = true ] ; then
372 372 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} libncurses-dev"
373 373 fi
374 374
375 375 # Add ccache compiler cache for (faster) kernel cross (re)compilation
376 376 if [ "$KERNEL_CCACHE" = true ] ; then
377 377 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} ccache"
378 378 fi
379 379
380 380 # Add cryptsetup package to enable filesystem encryption
381 381 if [ "$ENABLE_CRYPTFS" = true ] && [ "$BUILD_KERNEL" = true ] ; then
382 382 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} cryptsetup"
383 383 APT_INCLUDES="${APT_INCLUDES},cryptsetup,busybox,console-setup"
384 384
385 385 #If cryptfs,dropbear and initramfs are enabled include dropbear-initramfs package
386 386 if [ "$CRYPTFS_DROPBEAR" = true ] && [ "$ENABLE_INITRAMFS" = true ]; then
387 387 APT_INCLUDES="${APT_INCLUDES},dropbear-initramfs"
388 388 fi
389 389
390 390 if [ -z "$CRYPTFS_PASSWORD" ] ; then
391 391 echo "error: no password defined (CRYPTFS_PASSWORD)!"
392 392 exit 1
393 393 fi
394 394 ENABLE_INITRAMFS=true
395 395 fi
396 396
397 397 # Add initramfs generation tools
398 398 if [ "$ENABLE_INITRAMFS" = true ] && [ "$BUILD_KERNEL" = true ] ; then
399 399 APT_INCLUDES="${APT_INCLUDES},initramfs-tools"
400 400 fi
401 401
402 402 # Add device-tree-compiler required for building the U-Boot bootloader
403 403 if [ "$ENABLE_UBOOT" = true ] ; then
404 404 APT_INCLUDES="${APT_INCLUDES},device-tree-compiler,bison,flex,bc"
405 405 else
406 406 if [ "$ENABLE_UBOOTUSB" = true ] ; then
407 407 echo "error: Enabling UBOOTUSB requires u-boot to be enabled"
408 408 exit 1
409 409 fi
410 410 fi
411 411
412 412 # Check if root SSH (v2) public key file exists
413 413 if [ -n "$SSH_ROOT_PUB_KEY" ] ; then
414 414 if [ ! -f "$SSH_ROOT_PUB_KEY" ] ; then
415 415 echo "error: '$SSH_ROOT_PUB_KEY' specified SSH public key file not found (SSH_ROOT_PUB_KEY)!"
416 416 exit 1
417 417 fi
418 418 fi
419 419
420 420 # Check if $USER_NAME SSH (v2) public key file exists
421 421 if [ -n "$SSH_USER_PUB_KEY" ] ; then
422 422 if [ ! -f "$SSH_USER_PUB_KEY" ] ; then
423 423 echo "error: '$SSH_USER_PUB_KEY' specified SSH public key file not found (SSH_USER_PUB_KEY)!"
424 424 exit 1
425 425 fi
426 426 fi
427 427
428 428 # Check if all required packages are installed on the build system
429 429 for package in $REQUIRED_PACKAGES ; do
430 430 if [ "$(dpkg-query -W -f='${Status}' "$package")" != "install ok installed" ] ; then
431 431 MISSING_PACKAGES="${MISSING_PACKAGES} $package"
432 432 fi
433 433 done
434 434
435 435 # If there are missing packages ask confirmation for install, or exit
436 436 if [ -n "$MISSING_PACKAGES" ] ; then
437 437 echo "the following packages needed by this script are not installed:"
438 438 echo "$MISSING_PACKAGES"
439 439
440 440 printf "\ndo you want to install the missing packages right now? [y/n] "
441 441 read -r confirm
442 442 [ "$confirm" != "y" ] && exit 1
443 443
444 444 # Make sure all missing required packages are installed
445 445 apt-get -qq -y install "${MISSING_PACKAGES}"
446 446 fi
447 447
448 448 # Check if ./bootstrap.d directory exists
449 449 if [ ! -d "./bootstrap.d/" ] ; then
450 450 echo "error: './bootstrap.d' required directory not found!"
451 451 exit 1
452 452 fi
453 453
454 454 # Check if ./files directory exists
455 455 if [ ! -d "./files/" ] ; then
456 456 echo "error: './files' required directory not found!"
457 457 exit 1
458 458 fi
459 459
460 460 # Check if specified KERNELSRC_DIR directory exists
461 461 if [ -n "$KERNELSRC_DIR" ] && [ ! -d "$KERNELSRC_DIR" ] ; then
462 462 echo "error: '${KERNELSRC_DIR}' specified directory not found (KERNELSRC_DIR)!"
463 463 exit 1
464 464 fi
465 465
466 466 # Check if specified UBOOTSRC_DIR directory exists
467 467 if [ -n "$UBOOTSRC_DIR" ] && [ ! -d "$UBOOTSRC_DIR" ] ; then
468 468 echo "error: '${UBOOTSRC_DIR}' specified directory not found (UBOOTSRC_DIR)!"
469 469 exit 1
470 470 fi
471 471
472 472 # Check if specified VIDEOCORESRC_DIR directory exists
473 473 if [ -n "$VIDEOCORESRC_DIR" ] && [ ! -d "$VIDEOCORESRC_DIR" ] ; then
474 474 echo "error: '${VIDEOCORESRC_DIR}' specified directory not found (VIDEOCORESRC_DIR)!"
475 475 exit 1
476 476 fi
477 477
478 478 # Check if specified FBTURBOSRC_DIR directory exists
479 479 if [ -n "$FBTURBOSRC_DIR" ] && [ ! -d "$FBTURBOSRC_DIR" ] ; then
480 480 echo "error: '${FBTURBOSRC_DIR}' specified directory not found (FBTURBOSRC_DIR)!"
481 481 exit 1
482 482 fi
483 483
484 484 # Check if specified NEXMON_DIR directory exists
485 485 if [ -n "$NEXMON_DIR" ] && [ ! -d "$NEXMON_DIR" ] ; then
486 486 echo "error: '${NEXMON_DIR}' specified directory not found (NEXMON_DIR)!"
487 487 exit 1
488 488 fi
489 489
490 490 # Check if specified CHROOT_SCRIPTS directory exists
491 491 if [ -n "$CHROOT_SCRIPTS" ] && [ ! -d "$CHROOT_SCRIPTS" ] ; then
492 492 echo "error: ${CHROOT_SCRIPTS} specified directory not found (CHROOT_SCRIPTS)!"
493 493 exit 1
494 494 fi
495 495
496 496 # Check if specified device mapping already exists (will be used by cryptsetup)
497 497 if [ -r "/dev/mapping/${CRYPTFS_MAPPING}" ] ; then
498 498 echo "error: mapping /dev/mapping/${CRYPTFS_MAPPING} already exists, not proceeding"
499 499 exit 1
500 500 fi
501 501
502 502 # Don't clobber an old build
503 503 if [ -e "$BUILDDIR" ] ; then
504 504 echo "error: directory ${BUILDDIR} already exists, not proceeding"
505 505 exit 1
506 506 fi
507 507
508 508 # Setup chroot directory
509 509 mkdir -p "${R}"
510 510
511 511 # Check if build directory has enough of free disk space >512MB
512 512 if [ "$(df --output=avail "${BUILDDIR}" | sed "1d")" -le "524288" ] ; then
513 513 echo "error: ${BUILDDIR} not enough space left to generate the output image!"
514 514 exit 1
515 515 fi
516 516
517 517 set -x
518 518
519 519 # Call "cleanup" function on various signals and errors
520 520 trap cleanup 0 1 2 3 6
521 521
522 522 # Add required packages for the minbase installation
523 523 if [ "$ENABLE_MINBASE" = true ] ; then
524 524 APT_INCLUDES="${APT_INCLUDES},vim-tiny,netbase,net-tools,ifupdown"
525 525 fi
526 526
527 527 # Add parted package, required to get partprobe utility
528 528 if [ "$EXPANDROOT" = true ] ; then
529 529 APT_INCLUDES="${APT_INCLUDES},parted"
530 530 fi
531 531
532 532 # Add dbus package, recommended if using systemd
533 533 if [ "$ENABLE_DBUS" = true ] ; then
534 534 APT_INCLUDES="${APT_INCLUDES},dbus"
535 535 fi
536 536
537 537 # Add iptables IPv4/IPv6 package
538 538 if [ "$ENABLE_IPTABLES" = true ] ; then
539 539 APT_INCLUDES="${APT_INCLUDES},iptables,iptables-persistent"
540 540 fi
541 541
542 542 # Add openssh server package
543 543 if [ "$ENABLE_SSHD" = true ] ; then
544 544 APT_INCLUDES="${APT_INCLUDES},openssh-server"
545 545 fi
546 546
547 547 # Add alsa-utils package
548 548 if [ "$ENABLE_SOUND" = true ] ; then
549 549 APT_INCLUDES="${APT_INCLUDES},alsa-utils"
550 550 fi
551 551
552 552 # Add rng-tools package
553 553 if [ "$ENABLE_HWRANDOM" = true ] ; then
554 554 APT_INCLUDES="${APT_INCLUDES},rng-tools"
555 555 fi
556 556
557 557 # Add fbturbo video driver
558 558 if [ "$ENABLE_FBTURBO" = true ] ; then
559 559 # Enable xorg package dependencies
560 560 ENABLE_XORG=true
561 561 fi
562 562
563 563 # Add user defined window manager package
564 564 if [ -n "$ENABLE_WM" ] ; then
565 565 APT_INCLUDES="${APT_INCLUDES},${ENABLE_WM}"
566 566
567 567 # Enable xorg package dependencies
568 568 ENABLE_XORG=true
569 569 fi
570 570
571 571 # Add xorg package
572 572 if [ "$ENABLE_XORG" = true ] ; then
573 573 APT_INCLUDES="${APT_INCLUDES},xorg,dbus-x11"
574 574 fi
575 575
576 576 # Replace selected packages with smaller clones
577 577 if [ "$ENABLE_REDUCE" = true ] ; then
578 578 # Add levee package instead of vim-tiny
579 579 if [ "$REDUCE_VIM" = true ] ; then
580 580 APT_INCLUDES="$(echo ${APT_INCLUDES} | sed "s/vim-tiny/levee/")"
581 581 fi
582 582
583 583 # Add dropbear package instead of openssh-server
584 584 if [ "$REDUCE_SSHD" = true ] ; then
585 585 APT_INCLUDES="$(echo "${APT_INCLUDES}" | sed "s/openssh-server/dropbear/")"
586 586 fi
587 587 fi
588 588
589 589 # Configure systemd-sysv exclude to make halt/reboot/shutdown scripts available
590 590 if [ "$ENABLE_SYSVINIT" = false ] ; then
591 591 APT_EXCLUDES="--exclude=${APT_EXCLUDES},init,systemd-sysv"
592 592 fi
593 593
594 594 # Configure kernel sources if no KERNELSRC_DIR
595 595 if [ "$BUILD_KERNEL" = true ] && [ -z "$KERNELSRC_DIR" ] ; then
596 596 KERNELSRC_CONFIG=true
597 597 fi
598 598
599 599 # Configure reduced kernel
600 600 if [ "$KERNEL_REDUCE" = true ] ; then
601 601 KERNELSRC_CONFIG=false
602 602 fi
603 603
604 604 # Configure qemu compatible kernel
605 605 if [ "$ENABLE_QEMU" = true ] ; then
606 606 DTB_FILE=vexpress-v2p-ca15_a7.dtb
607 607 UBOOT_CONFIG=vexpress_ca15_tc2_defconfig
608 608 KERNEL_DEFCONFIG="vexpress_defconfig"
609 609 if [ "$KERNEL_MENUCONFIG" = false ] ; then
610 610 KERNEL_OLDDEFCONFIG=true
611 611 fi
612 612 fi
613 613
614 614 # Execute bootstrap scripts
615 615 for SCRIPT in bootstrap.d/*.sh; do
616 616 head -n 3 "$SCRIPT"
617 617 . "$SCRIPT"
618 618 done
619 619
620 620 ## Execute custom bootstrap scripts
621 621 if [ -d "custom.d" ] ; then
622 622 for SCRIPT in custom.d/*.sh; do
623 623 . "$SCRIPT"
624 624 done
625 625 fi
626 626
627 627 # Execute custom scripts inside the chroot
628 628 if [ -n "$CHROOT_SCRIPTS" ] && [ -d "$CHROOT_SCRIPTS" ] ; then
629 629 cp -r "${CHROOT_SCRIPTS}" "${R}/chroot_scripts"
630 630 chroot_exec /bin/bash -x <<'EOF'
631 631 for SCRIPT in /chroot_scripts/* ; do
632 632 if [ -f $SCRIPT -a -x $SCRIPT ] ; then
633 633 $SCRIPT
634 634 fi
635 635 done
636 636 EOF
637 637 rm -rf "${R}/chroot_scripts"
638 638 fi
639 639
640 640 # Remove c/c++ build environment from the chroot
641 641 chroot_remove_cc
642 642
643 643 # Generate required machine-id
644 644 MACHINE_ID=$(dbus-uuidgen)
645 645 echo -n "${MACHINE_ID}" > "${R}/var/lib/dbus/machine-id"
646 646 echo -n "${MACHINE_ID}" > "${ETC_DIR}/machine-id"
647 647
648 648 # APT Cleanup
649 649 chroot_exec apt-get -y clean
650 650 chroot_exec apt-get -y autoclean
651 651 chroot_exec apt-get -y autoremove
652 652
653 653 # Unmount mounted filesystems
654 654 umount -l "${R}/proc"
655 655 umount -l "${R}/sys"
656 656
657 657 # Clean up directories
658 658 rm -rf "${R}/run/*"
659 659 rm -rf "${R}/tmp/*"
660 660
661 661 # Clean up files
662 662 rm -f "${ETC_DIR}/ssh/ssh_host_*"
663 663 rm -f "${ETC_DIR}/dropbear/dropbear_*"
664 664 rm -f "${ETC_DIR}/apt/sources.list.save"
665 665 rm -f "${ETC_DIR}/resolvconf/resolv.conf.d/original"
666 666 rm -f "${ETC_DIR}/*-"
667 667 rm -f "${ETC_DIR}/apt/apt.conf.d/10proxy"
668 668 rm -f "${ETC_DIR}/resolv.conf"
669 669 rm -f "${R}/root/.bash_history"
670 670 rm -f "${R}/var/lib/urandom/random-seed"
671 671 rm -f "${R}/initrd.img"
672 672 rm -f "${R}/vmlinuz"
673 673 rm -f "${R}${QEMU_BINARY}"
674 674
675 675 if [ "$ENABLE_QEMU" = true ] ; then
676 676 # Setup QEMU directory
677 677 mkdir "${BASEDIR}/qemu"
678 678
679 679 # Copy kernel image to QEMU directory
680 680 install_readonly "${BOOT_DIR}/${KERNEL_IMAGE}" "${BASEDIR}/qemu/${KERNEL_IMAGE}"
681 681
682 682 # Copy kernel config to QEMU directory
683 683 install_readonly "${R}/boot/config-${KERNEL_VERSION}" "${BASEDIR}/qemu/config-${KERNEL_VERSION}"
684 684
685 685 # Copy kernel dtbs to QEMU directory
686 686 for dtb in "${BOOT_DIR}/"*.dtb ; do
687 687 if [ -f "${dtb}" ] ; then
688 688 install_readonly "${dtb}" "${BASEDIR}/qemu/"
689 689 fi
690 690 done
691 691
692 692 # Copy kernel overlays to QEMU directory
693 693 if [ -d "${BOOT_DIR}/overlays" ] ; then
694 694 # Setup overlays dtbs directory
695 695 mkdir "${BASEDIR}/qemu/overlays"
696 696
697 697 for dtb in "${BOOT_DIR}/overlays/"*.dtb ; do
698 698 if [ -f "${dtb}" ] ; then
699 699 install_readonly "${dtb}" "${BASEDIR}/qemu/overlays/"
700 700 fi
701 701 done
702 702 fi
703 703
704 704 # Copy u-boot files to QEMU directory
705 705 if [ "$ENABLE_UBOOT" = true ] ; then
706 706 if [ -f "${BOOT_DIR}/u-boot.bin" ] ; then
707 707 install_readonly "${BOOT_DIR}/u-boot.bin" "${BASEDIR}/qemu/u-boot.bin"
708 708 fi
709 709 if [ -f "${BOOT_DIR}/uboot.mkimage" ] ; then
710 710 install_readonly "${BOOT_DIR}/uboot.mkimage" "${BASEDIR}/qemu/uboot.mkimage"
711 711 fi
712 712 if [ -f "${BOOT_DIR}/boot.scr" ] ; then
713 713 install_readonly "${BOOT_DIR}/boot.scr" "${BASEDIR}/qemu/boot.scr"
714 714 fi
715 715 fi
716 716
717 717 # Copy initramfs to QEMU directory
718 718 if [ -f "${BOOT_DIR}/initramfs-${KERNEL_VERSION}" ] ; then
719 719 install_readonly "${BOOT_DIR}/initramfs-${KERNEL_VERSION}" "${BASEDIR}/qemu/initramfs-${KERNEL_VERSION}"
720 720 fi
721 721 fi
722 722
723 723 # Calculate size of the chroot directory in KB
724 724 CHROOT_SIZE=$(expr "$(du -s "${R}" | awk '{ print $1 }')")
725 725
726 726 # Calculate the amount of needed 512 Byte sectors
727 727 TABLE_SECTORS=$(expr 1 \* 1024 \* 1024 \/ 512)
728 728 FRMW_SECTORS=$(expr 64 \* 1024 \* 1024 \/ 512)
729 729 ROOT_OFFSET=$(expr "${TABLE_SECTORS}" + "${FRMW_SECTORS}")
730 730
731 731 # The root partition is EXT4
732 732 # This means more space than the actual used space of the chroot is used.
733 733 # As overhead for journaling and reserved blocks 35% are added.
734 734 ROOT_SECTORS=$(expr "$(expr "${CHROOT_SIZE}" + "${CHROOT_SIZE}" \/ 100 \* 35)" \* 1024 \/ 512)
735 735
736 736 # Calculate required image size in 512 Byte sectors
737 737 IMAGE_SECTORS=$(expr "${TABLE_SECTORS}" + "${FRMW_SECTORS}" + "${ROOT_SECTORS}")
738 738
739 739 # Prepare image file
740 740 if [ "$ENABLE_SPLITFS" = true ] ; then
741 741 dd if=/dev/zero of="$IMAGE_NAME-frmw.img" bs=512 count="${TABLE_SECTORS}"
742 742 dd if=/dev/zero of="$IMAGE_NAME-frmw.img" bs=512 count=0 seek="${FRMW_SECTORS}"
743 743 dd if=/dev/zero of="$IMAGE_NAME-root.img" bs=512 count="${TABLE_SECTORS}"
744 744 dd if=/dev/zero of="$IMAGE_NAME-root.img" bs=512 count=0 seek="${ROOT_SECTORS}"
745 745
746 746 # Write firmware/boot partition tables
747 747 sfdisk -q -L -uS -f "$IMAGE_NAME-frmw.img" 2> /dev/null <<EOM
748 748 ${TABLE_SECTORS},${FRMW_SECTORS},c,*
749 749 EOM
750 750
751 751 # Write root partition table
752 752 sfdisk -q -L -uS -f "$IMAGE_NAME-root.img" 2> /dev/null <<EOM
753 753 ${TABLE_SECTORS},${ROOT_SECTORS},83
754 754 EOM
755 755
756 756 # Setup temporary loop devices
757 757 FRMW_LOOP="$(losetup -o 1M --sizelimit 64M -f --show "$IMAGE_NAME"-frmw.img)"
758 758 ROOT_LOOP="$(losetup -o 1M -f --show "$IMAGE_NAME"-root.img)"
759 759 else # ENABLE_SPLITFS=false
760 760 dd if=/dev/zero of="$IMAGE_NAME.img" bs=512 count="${TABLE_SECTORS}"
761 761 dd if=/dev/zero of="$IMAGE_NAME.img" bs=512 count=0 seek="${IMAGE_SECTORS}"
762 762
763 763 # Write partition table
764 764 sfdisk -q -L -uS -f "$IMAGE_NAME.img" 2> /dev/null <<EOM
765 765 ${TABLE_SECTORS},${FRMW_SECTORS},c,*
766 766 ${ROOT_OFFSET},${ROOT_SECTORS},83
767 767 EOM
768 768
769 769 # Setup temporary loop devices
770 770 FRMW_LOOP="$(losetup -o 1M --sizelimit 64M -f --show "$IMAGE_NAME".img)"
771 771 ROOT_LOOP="$(losetup -o 65M -f --show "$IMAGE_NAME".img)"
772 772 fi
773 773
774 774 if [ "$ENABLE_CRYPTFS" = true ] ; then
775 775 # Create dummy ext4 fs
776 776 mkfs.ext4 "$ROOT_LOOP"
777 777
778 778 # Setup password keyfile
779 779 touch .password
780 780 chmod 600 .password
781 781 echo -n ${CRYPTFS_PASSWORD} > .password
782 782
783 783 # Initialize encrypted partition
784 784 echo "YES" | cryptsetup luksFormat "${ROOT_LOOP}" -c "${CRYPTFS_CIPHER}" -s "${CRYPTFS_XTSKEYSIZE}" .password
785 785
786 786 # Open encrypted partition and setup mapping
787 787 cryptsetup luksOpen "${ROOT_LOOP}" -d .password "${CRYPTFS_MAPPING}"
788 788
789 789 # Secure delete password keyfile
790 790 shred -zu .password
791 791
792 792 # Update temporary loop device
793 793 ROOT_LOOP="/dev/mapper/${CRYPTFS_MAPPING}"
794 794
795 795 # Wipe encrypted partition (encryption cipher is used for randomness)
796 796 dd if=/dev/zero of="${ROOT_LOOP}" bs=512 count="$(blockdev --getsz "${ROOT_LOOP}")"
797 797 fi
798 798
799 799 # Build filesystems
800 800 mkfs.vfat "$FRMW_LOOP"
801 801 mkfs.ext4 "$ROOT_LOOP"
802 802
803 803 # Mount the temporary loop devices
804 804 mkdir -p "$BUILDDIR/mount"
805 805 mount "$ROOT_LOOP" "$BUILDDIR/mount"
806 806
807 807 mkdir -p "$BUILDDIR/mount/boot/firmware"
808 808 mount "$FRMW_LOOP" "$BUILDDIR/mount/boot/firmware"
809 809
810 810 # Copy all files from the chroot to the loop device mount point directory
811 811 rsync -a "${R}/" "$BUILDDIR/mount/"
812 812
813 813 # Unmount all temporary loop devices and mount points
814 814 cleanup
815 815
816 816 # Create block map file(s) of image(s)
817 817 if [ "$ENABLE_SPLITFS" = true ] ; then
818 818 # Create block map files for "bmaptool"
819 819 bmaptool create -o "$IMAGE_NAME-frmw.bmap" "$IMAGE_NAME-frmw.img"
820 820 bmaptool create -o "$IMAGE_NAME-root.bmap" "$IMAGE_NAME-root.img"
821 821
822 822 # Image was successfully created
823 823 echo "$IMAGE_NAME-frmw.img ($(expr \( "${TABLE_SECTORS}" + "${FRMW_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
824 824 echo "$IMAGE_NAME-root.img ($(expr \( "${TABLE_SECTORS}" + "${ROOT_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
825 825 else
826 826 # Create block map file for "bmaptool"
827 827 bmaptool create -o "$IMAGE_NAME.bmap" "$IMAGE_NAME.img"
828 828
829 829 # Image was successfully created
830 830 echo "$IMAGE_NAME.img ($(expr \( "${TABLE_SECTORS}" + "${FRMW_SECTORS}" + "${ROOT_SECTORS}" \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
831 831
832 832 # Create qemu qcow2 image
833 833 if [ "$ENABLE_QEMU" = true ] ; then
834 834 QEMU_IMAGE=${QEMU_IMAGE:=${BASEDIR}/qemu/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
835 835 QEMU_SIZE=16G
836 836
837 837 qemu-img convert -f raw -O qcow2 "$IMAGE_NAME".img "$QEMU_IMAGE".qcow2
838 838 qemu-img resize "$QEMU_IMAGE".qcow2 $QEMU_SIZE
839 839
840 840 echo "$QEMU_IMAGE.qcow2 ($QEMU_SIZE)" ": successfully created"
841 841 fi
842 842 fi
General Comments 0
Vous devez vous connecter pour laisser un commentaire. Se connecter maintenant