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