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