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