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