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