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