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