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