##// END OF EJS Templates
Added variables to allow for arm64 and different branches.
Bobberty -
r146:e6b5c93408d5
parent child
Show More
@@ -1,629 +1,640
1 1 #!/bin/sh
2 2
3 3 ########################################################################
4 4 # rpi23-gen-image.sh 2015-2017
5 5 #
6 6 # Advanced Debian "jessie" and "stretch" bootstrap script for RPi2/3
7 7 #
8 8 # This program is free software; you can redistribute it and/or
9 9 # modify it under the terms of the GNU General Public License
10 10 # as published by the Free Software Foundation; either version 2
11 11 # of the License, or (at your option) any later version.
12 12 #
13 13 # Copyright (C) 2015 Jan Wagner <mail@jwagner.eu>
14 14 #
15 15 # Big thanks for patches and enhancements by 10+ github contributors!
16 16 ########################################################################
17 17
18 18 # Are we running as root?
19 19 if [ "$(id -u)" -ne "0" ] ; then
20 20 echo "error: this script must be executed with root privileges!"
21 21 exit 1
22 22 fi
23 23
24 24 # Check if ./functions.sh script exists
25 25 if [ ! -r "./functions.sh" ] ; then
26 26 echo "error: './functions.sh' required script not found!"
27 27 exit 1
28 28 fi
29 29
30 30 # Load utility functions
31 31 . ./functions.sh
32 32
33 33 # Load parameters from configuration template file
34 34 if [ ! -z "$CONFIG_TEMPLATE" ] ; then
35 35 use_template
36 36 fi
37 37
38 38 # Introduce settings
39 39 set -e
40 40 echo -n -e "\n#\n# RPi2/3 Bootstrap Settings\n#\n"
41 41 set -x
42 42
43 43 # Raspberry Pi model configuration
44 44 RPI_MODEL=${RPI_MODEL:=2}
45 45 RPI2_DTB_FILE=${RPI2_DTB_FILE:=bcm2709-rpi-2-b.dtb}
46 46 RPI2_UBOOT_CONFIG=${RPI2_UBOOT_CONFIG:=rpi_2_defconfig}
47 47 RPI3_DTB_FILE=${RPI3_DTB_FILE:=bcm2710-rpi-3-b.dtb}
48 48 RPI3_UBOOT_CONFIG=${RPI3_UBOOT_CONFIG:=rpi_3_32b_defconfig}
49 49
50 50 # Debian release
51 51 RELEASE=${RELEASE:=jessie}
52 52 KERNEL_ARCH=${KERNEL_ARCH:=arm}
53 53 RELEASE_ARCH=${RELEASE_ARCH:=armhf}
54 54 CROSS_COMPILE=${CROSS_COMPILE:=arm-linux-gnueabihf-}
55 55 COLLABORA_KERNEL=${COLLABORA_KERNEL:=3.18.0-trunk-rpi2}
56 56 KERNEL_DEFCONFIG=${KERNEL_DEFCONFIG:=bcm2709_defconfig}
57 57 KERNEL_IMAGE=${KERNEL_IMAGE:=kernel7.img}
58 58 QEMU_BINARY=${QEMU_BINARY:=/usr/bin/qemu-arm-static}
59 KERNEL_BRANCH=${KERNEL_BRANCH:=""}
59 60
60 61 # URLs
61 62 KERNEL_URL=${KERNEL_URL:=https://github.com/raspberrypi/linux}
62 63 FIRMWARE_URL=${FIRMWARE_URL:=https://github.com/raspberrypi/firmware/raw/master/boot}
63 64 WLAN_FIRMWARE_URL=${WLAN_FIRMWARE_URL:=https://github.com/RPi-Distro/firmware-nonfree/raw/master/brcm80211/brcm}
64 65 COLLABORA_URL=${COLLABORA_URL:=https://repositories.collabora.co.uk/debian}
65 66 FBTURBO_URL=${FBTURBO_URL:=https://github.com/ssvb/xf86-video-fbturbo.git}
66 67 UBOOT_URL=${UBOOT_URL:=git://git.denx.de/u-boot.git}
67 68
68 69 # Build directories
69 70 BASEDIR=${BASEDIR:=$(pwd)/images/${RELEASE}}
70 71 BUILDDIR="${BASEDIR}/build"
72
71 73 # Prepare date string for default image file name
72 74 DATE="$(date +%Y-%m-%d)"
73 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-rpi${RPI_MODEL}-${RELEASE}}
75 if [ -z "$KERNEL_BRANCH" ] ; then
76 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-CURRENT-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
77 else
78 IMAGE_NAME=${IMAGE_NAME:=${BASEDIR}/${DATE}-${KERNEL_ARCH}-${KERNEL_BRANCH}-rpi${RPI_MODEL}-${RELEASE}-${RELEASE_ARCH}}
79 fi
74 80
75 81 # Chroot directories
76 82 R="${BUILDDIR}/chroot"
77 83 ETC_DIR="${R}/etc"
78 84 LIB_DIR="${R}/lib"
79 85 BOOT_DIR="${R}/boot/firmware"
80 86 KERNEL_DIR="${R}/usr/src/linux"
81 87 WLAN_FIRMWARE_DIR="${R}/lib/firmware/brcm"
82 88
83 89 # Firmware directory: Blank if download from github
84 90 RPI_FIRMWARE_DIR=${RPI_FIRMWARE_DIR:=""}
85 91
86 92 # General settings
87 93 HOSTNAME=${HOSTNAME:=rpi${RPI_MODEL}-${RELEASE}}
88 94 PASSWORD=${PASSWORD:=raspberry}
89 95 USER_PASSWORD=${USER_PASSWORD:=raspberry}
90 96 DEFLOCAL=${DEFLOCAL:="en_US.UTF-8"}
91 97 TIMEZONE=${TIMEZONE:="Europe/Berlin"}
92 98 EXPANDROOT=${EXPANDROOT:=true}
93 99
94 100 # Keyboard settings
95 101 XKB_MODEL=${XKB_MODEL:=""}
96 102 XKB_LAYOUT=${XKB_LAYOUT:=""}
97 103 XKB_VARIANT=${XKB_VARIANT:=""}
98 104 XKB_OPTIONS=${XKB_OPTIONS:=""}
99 105
100 106 # Network settings (DHCP)
101 107 ENABLE_DHCP=${ENABLE_DHCP:=true}
102 108
103 109 # Network settings (static)
104 110 NET_ADDRESS=${NET_ADDRESS:=""}
105 111 NET_GATEWAY=${NET_GATEWAY:=""}
106 112 NET_DNS_1=${NET_DNS_1:=""}
107 113 NET_DNS_2=${NET_DNS_2:=""}
108 114 NET_DNS_DOMAINS=${NET_DNS_DOMAINS:=""}
109 115 NET_NTP_1=${NET_NTP_1:=""}
110 116 NET_NTP_2=${NET_NTP_2:=""}
111 117
112 118 # APT settings
113 119 APT_PROXY=${APT_PROXY:=""}
114 120 APT_SERVER=${APT_SERVER:="ftp.debian.org"}
115 121
116 122 # Feature settings
117 123 ENABLE_CONSOLE=${ENABLE_CONSOLE:=true}
118 124 ENABLE_I2C=${ENABLE_I2C:=false}
119 125 ENABLE_SPI=${ENABLE_SPI:=false}
120 126 ENABLE_IPV6=${ENABLE_IPV6:=true}
121 127 ENABLE_SSHD=${ENABLE_SSHD:=true}
122 128 ENABLE_NONFREE=${ENABLE_NONFREE:=false}
123 129 ENABLE_WIRELESS=${ENABLE_WIRELESS:=false}
124 130 ENABLE_SOUND=${ENABLE_SOUND:=true}
125 131 ENABLE_DBUS=${ENABLE_DBUS:=true}
126 132 ENABLE_HWRANDOM=${ENABLE_HWRANDOM:=true}
127 133 ENABLE_MINGPU=${ENABLE_MINGPU:=false}
128 134 ENABLE_XORG=${ENABLE_XORG:=false}
129 135 ENABLE_WM=${ENABLE_WM:=""}
130 136 ENABLE_RSYSLOG=${ENABLE_RSYSLOG:=true}
131 137 ENABLE_USER=${ENABLE_USER:=true}
132 138 USER_NAME=${USER_NAME:="pi"}
133 139 ENABLE_ROOT=${ENABLE_ROOT:=false}
134 140
135 141 # SSH settings
136 142 SSH_ENABLE_ROOT=${SSH_ENABLE_ROOT:=false}
137 143 SSH_DISABLE_PASSWORD_AUTH=${SSH_DISABLE_PASSWORD_AUTH:=false}
138 144 SSH_LIMIT_USERS=${SSH_LIMIT_USERS:=false}
139 145 SSH_ROOT_PUB_KEY=${SSH_ROOT_PUB_KEY:=""}
140 146 SSH_USER_PUB_KEY=${SSH_USER_PUB_KEY:=""}
141 147
142 148 # Advanced settings
143 149 ENABLE_MINBASE=${ENABLE_MINBASE:=false}
144 150 ENABLE_REDUCE=${ENABLE_REDUCE:=false}
145 151 ENABLE_UBOOT=${ENABLE_UBOOT:=false}
146 152 UBOOTSRC_DIR=${UBOOTSRC_DIR:=""}
147 153 ENABLE_FBTURBO=${ENABLE_FBTURBO:=false}
148 154 FBTURBOSRC_DIR=${FBTURBOSRC_DIR:=""}
149 155 ENABLE_HARDNET=${ENABLE_HARDNET:=false}
150 156 ENABLE_IPTABLES=${ENABLE_IPTABLES:=false}
151 157 ENABLE_SPLITFS=${ENABLE_SPLITFS:=false}
152 158 ENABLE_INITRAMFS=${ENABLE_INITRAMFS:=false}
153 159 ENABLE_IFNAMES=${ENABLE_IFNAMES:=true}
154 160 DISABLE_UNDERVOLT_WARNINGS=${DISABLE_UNDERVOLT_WARNINGS:=}
155 161
156 162 # Kernel compilation settings
157 163 BUILD_KERNEL=${BUILD_KERNEL:=false}
158 164 KERNEL_REDUCE=${KERNEL_REDUCE:=false}
159 165 KERNEL_THREADS=${KERNEL_THREADS:=1}
160 166 KERNEL_HEADERS=${KERNEL_HEADERS:=true}
161 167 KERNEL_MENUCONFIG=${KERNEL_MENUCONFIG:=false}
162 168 KERNEL_REMOVESRC=${KERNEL_REMOVESRC:=true}
169 KERNEL_BIN_IMAGE=${KERNEL_BIN_IMAGE:="zImage"}
163 170
164 171 # Kernel compilation from source directory settings
165 172 KERNELSRC_DIR=${KERNELSRC_DIR:=""}
166 173 KERNELSRC_CLEAN=${KERNELSRC_CLEAN:=false}
167 174 KERNELSRC_CONFIG=${KERNELSRC_CONFIG:=true}
168 175 KERNELSRC_PREBUILT=${KERNELSRC_PREBUILT:=false}
169 176
170 177 # Reduce disk usage settings
171 178 REDUCE_APT=${REDUCE_APT:=true}
172 179 REDUCE_DOC=${REDUCE_DOC:=true}
173 180 REDUCE_MAN=${REDUCE_MAN:=true}
174 181 REDUCE_VIM=${REDUCE_VIM:=false}
175 182 REDUCE_BASH=${REDUCE_BASH:=false}
176 183 REDUCE_HWDB=${REDUCE_HWDB:=true}
177 184 REDUCE_SSHD=${REDUCE_SSHD:=true}
178 185 REDUCE_LOCALE=${REDUCE_LOCALE:=true}
179 186
180 187 # Encrypted filesystem settings
181 188 ENABLE_CRYPTFS=${ENABLE_CRYPTFS:=false}
182 189 CRYPTFS_PASSWORD=${CRYPTFS_PASSWORD:=""}
183 190 CRYPTFS_MAPPING=${CRYPTFS_MAPPING:="secure"}
184 191 CRYPTFS_CIPHER=${CRYPTFS_CIPHER:="aes-xts-plain64:sha512"}
185 192 CRYPTFS_XTSKEYSIZE=${CRYPTFS_XTSKEYSIZE:=512}
186 193
187 194 # Stop the Crypto Wars
188 195 DISABLE_FBI=${DISABLE_FBI:=false}
189 196
190 197 # Chroot scripts directory
191 198 CHROOT_SCRIPTS=${CHROOT_SCRIPTS:=""}
192 199
193 200 # Packages required in the chroot build environment
194 201 APT_INCLUDES=${APT_INCLUDES:=""}
195 202 APT_INCLUDES="${APT_INCLUDES},apt-transport-https,apt-utils,ca-certificates,debian-archive-keyring,dialog,sudo,systemd,sysvinit-utils"
196 203
197 204 # Packages required for bootstrapping
198 205 REQUIRED_PACKAGES="debootstrap debian-archive-keyring qemu-user-static binfmt-support dosfstools rsync bmap-tools whois git bc psmisc dbus sudo"
199 206 MISSING_PACKAGES=""
200 207
201 208 # Packages installed for c/c++ build environment in chroot (keep empty)
202 209 COMPILER_PACKAGES=""
203 210
204 211 set +x
205 212
206 213 # Set Raspberry Pi model specific configuration
207 214 if [ "$RPI_MODEL" = 2 ] ; then
208 215 DTB_FILE=${RPI2_DTB_FILE}
209 216 UBOOT_CONFIG=${RPI2_UBOOT_CONFIG}
210 217 elif [ "$RPI_MODEL" = 3 ] ; then
211 218 DTB_FILE=${RPI3_DTB_FILE}
212 219 UBOOT_CONFIG=${RPI3_UBOOT_CONFIG}
213 220 BUILD_KERNEL=true
214 221 else
215 222 echo "error: Raspberry Pi model ${RPI_MODEL} is not supported!"
216 223 exit 1
217 224 fi
218 225
219 226 # Check if the internal wireless interface is supported by the RPi model
220 227 if [ "$ENABLE_WIRELESS" = true ] && [ "$RPI_MODEL" != 3 ] ; then
221 228 echo "error: The selected Raspberry Pi model has no internal wireless interface"
222 229 exit 1
223 230 fi
224 231
225 232 # Check if DISABLE_UNDERVOLT_WARNINGS parameter value is supported
226 233 if [ ! -z "$DISABLE_UNDERVOLT_WARNINGS" ] ; then
227 234 if [ "$DISABLE_UNDERVOLT_WARNINGS" != 1 ] && [ "$DISABLE_UNDERVOLT_WARNINGS" != 2 ] ; then
228 235 echo "error: DISABLE_UNDERVOLT_WARNINGS=${DISABLE_UNDERVOLT_WARNINGS} is not supported"
229 236 exit 1
230 237 fi
231 238 fi
232 239
233 240 # Build RPi2/3 Linux kernel if required by Debian release
234 241 if [ "$RELEASE" = "stretch" ] ; then
235 242 BUILD_KERNEL=true
236 243 fi
237 244
238 245 # Add packages required for kernel cross compilation
239 246 if [ "$BUILD_KERNEL" = true ] ; then
247 if [ "$KERNEL_ARCH" = "arm" ] ; then
240 248 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-armhf"
249 else
250 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-arm64"
251 fi
241 252 fi
242 253
243 254 # Add libncurses5 to enable kernel menuconfig
244 255 if [ "$KERNEL_MENUCONFIG" = true ] ; then
245 256 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} libncurses5-dev"
246 257 fi
247 258
248 259 # Stop the Crypto Wars
249 260 if [ "$DISABLE_FBI" = true ] ; then
250 261 ENABLE_CRYPTFS=true
251 262 fi
252 263
253 264 # Add cryptsetup package to enable filesystem encryption
254 265 if [ "$ENABLE_CRYPTFS" = true ] && [ "$BUILD_KERNEL" = true ] ; then
255 266 REQUIRED_PACKAGES="${REQUIRED_PACKAGES} cryptsetup"
256 267 APT_INCLUDES="${APT_INCLUDES},cryptsetup"
257 268
258 269 if [ -z "$CRYPTFS_PASSWORD" ] ; then
259 270 echo "error: no password defined (CRYPTFS_PASSWORD)!"
260 271 exit 1
261 272 fi
262 273 ENABLE_INITRAMFS=true
263 274 fi
264 275
265 276 # Add initramfs generation tools
266 277 if [ "$ENABLE_INITRAMFS" = true ] && [ "$BUILD_KERNEL" = true ] ; then
267 278 APT_INCLUDES="${APT_INCLUDES},initramfs-tools"
268 279 fi
269 280
270 281 # Add device-tree-compiler required for building the U-Boot bootloader
271 282 if [ "$ENABLE_UBOOT" = true ] ; then
272 283 APT_INCLUDES="${APT_INCLUDES},device-tree-compiler"
273 284 fi
274 285
275 286 # Check if root SSH (v2) public key file exists
276 287 if [ ! -z "$SSH_ROOT_PUB_KEY" ] ; then
277 288 if [ ! -f "$SSH_ROOT_PUB_KEY" ] ; then
278 289 echo "error: '$SSH_ROOT_PUB_KEY' specified SSH public key file not found (SSH_ROOT_PUB_KEY)!"
279 290 exit 1
280 291 fi
281 292 fi
282 293
283 294 # Check if $USER_NAME SSH (v2) public key file exists
284 295 if [ ! -z "$SSH_USER_PUB_KEY" ] ; then
285 296 if [ ! -f "$SSH_USER_PUB_KEY" ] ; then
286 297 echo "error: '$SSH_USER_PUB_KEY' specified SSH public key file not found (SSH_USER_PUB_KEY)!"
287 298 exit 1
288 299 fi
289 300 fi
290 301
291 302 # Check if all required packages are installed on the build system
292 303 for package in $REQUIRED_PACKAGES ; do
293 304 if [ "`dpkg-query -W -f='${Status}' $package`" != "install ok installed" ] ; then
294 305 MISSING_PACKAGES="${MISSING_PACKAGES} $package"
295 306 fi
296 307 done
297 308
298 309 # If there are missing packages ask confirmation for install, or exit
299 310 if [ -n "$MISSING_PACKAGES" ] ; then
300 311 echo "the following packages needed by this script are not installed:"
301 312 echo "$MISSING_PACKAGES"
302 313
303 314 echo -n "\ndo you want to install the missing packages right now? [y/n] "
304 315 read confirm
305 316 [ "$confirm" != "y" ] && exit 1
306 317
307 318 # Make sure all missing required packages are installed
308 319 apt-get -qq -y install ${MISSING_PACKAGES}
309 320 fi
310 321
311 322 # Check if ./bootstrap.d directory exists
312 323 if [ ! -d "./bootstrap.d/" ] ; then
313 324 echo "error: './bootstrap.d' required directory not found!"
314 325 exit 1
315 326 fi
316 327
317 328 # Check if ./files directory exists
318 329 if [ ! -d "./files/" ] ; then
319 330 echo "error: './files' required directory not found!"
320 331 exit 1
321 332 fi
322 333
323 334 # Check if specified KERNELSRC_DIR directory exists
324 335 if [ -n "$KERNELSRC_DIR" ] && [ ! -d "$KERNELSRC_DIR" ] ; then
325 336 echo "error: '${KERNELSRC_DIR}' specified directory not found (KERNELSRC_DIR)!"
326 337 exit 1
327 338 fi
328 339
329 340 # Check if specified UBOOTSRC_DIR directory exists
330 341 if [ -n "$UBOOTSRC_DIR" ] && [ ! -d "$UBOOTSRC_DIR" ] ; then
331 342 echo "error: '${UBOOTSRC_DIR}' specified directory not found (UBOOTSRC_DIR)!"
332 343 exit 1
333 344 fi
334 345
335 346 # Check if specified FBTURBOSRC_DIR directory exists
336 347 if [ -n "$FBTURBOSRC_DIR" ] && [ ! -d "$FBTURBOSRC_DIR" ] ; then
337 348 echo "error: '${FBTURBOSRC_DIR}' specified directory not found (FBTURBOSRC_DIR)!"
338 349 exit 1
339 350 fi
340 351
341 352 # Check if specified CHROOT_SCRIPTS directory exists
342 353 if [ -n "$CHROOT_SCRIPTS" ] && [ ! -d "$CHROOT_SCRIPTS" ] ; then
343 354 echo "error: ${CHROOT_SCRIPTS} specified directory not found (CHROOT_SCRIPTS)!"
344 355 exit 1
345 356 fi
346 357
347 358 # Check if specified device mapping already exists (will be used by cryptsetup)
348 359 if [ -r "/dev/mapping/${CRYPTFS_MAPPING}" ] ; then
349 360 echo "error: mapping /dev/mapping/${CRYPTFS_MAPPING} already exists, not proceeding"
350 361 exit 1
351 362 fi
352 363
353 364 # Don't clobber an old build
354 365 if [ -e "$BUILDDIR" ] ; then
355 366 echo "error: directory ${BUILDDIR} already exists, not proceeding"
356 367 exit 1
357 368 fi
358 369
359 370 # Setup chroot directory
360 371 mkdir -p "${R}"
361 372
362 373 # Check if build directory has enough of free disk space >512MB
363 374 if [ "$(df --output=avail ${BUILDDIR} | sed "1d")" -le "524288" ] ; then
364 375 echo "error: ${BUILDDIR} not enough space left to generate the output image!"
365 376 exit 1
366 377 fi
367 378
368 379 set -x
369 380
370 381 # Call "cleanup" function on various signals and errors
371 382 trap cleanup 0 1 2 3 6
372 383
373 384 # Add required packages for the minbase installation
374 385 if [ "$ENABLE_MINBASE" = true ] ; then
375 386 APT_INCLUDES="${APT_INCLUDES},vim-tiny,netbase,net-tools,ifupdown"
376 387 fi
377 388
378 389 # Add required locales packages
379 390 if [ "$DEFLOCAL" != "en_US.UTF-8" ] ; then
380 391 APT_INCLUDES="${APT_INCLUDES},locales,keyboard-configuration,console-setup"
381 392 fi
382 393
383 394 # Add parted package, required to get partprobe utility
384 395 if [ "$EXPANDROOT" = true ] ; then
385 396 APT_INCLUDES="${APT_INCLUDES},parted"
386 397 fi
387 398
388 399 # Add dbus package, recommended if using systemd
389 400 if [ "$ENABLE_DBUS" = true ] ; then
390 401 APT_INCLUDES="${APT_INCLUDES},dbus"
391 402 fi
392 403
393 404 # Add iptables IPv4/IPv6 package
394 405 if [ "$ENABLE_IPTABLES" = true ] ; then
395 406 APT_INCLUDES="${APT_INCLUDES},iptables"
396 407 fi
397 408
398 409 # Add openssh server package
399 410 if [ "$ENABLE_SSHD" = true ] ; then
400 411 APT_INCLUDES="${APT_INCLUDES},openssh-server"
401 412 fi
402 413
403 414 # Add alsa-utils package
404 415 if [ "$ENABLE_SOUND" = true ] ; then
405 416 APT_INCLUDES="${APT_INCLUDES},alsa-utils"
406 417 fi
407 418
408 419 # Add rng-tools package
409 420 if [ "$ENABLE_HWRANDOM" = true ] ; then
410 421 APT_INCLUDES="${APT_INCLUDES},rng-tools"
411 422 fi
412 423
413 424 # Add fbturbo video driver
414 425 if [ "$ENABLE_FBTURBO" = true ] ; then
415 426 # Enable xorg package dependencies
416 427 ENABLE_XORG=true
417 428 fi
418 429
419 430 # Add user defined window manager package
420 431 if [ -n "$ENABLE_WM" ] ; then
421 432 APT_INCLUDES="${APT_INCLUDES},${ENABLE_WM}"
422 433
423 434 # Enable xorg package dependencies
424 435 ENABLE_XORG=true
425 436 fi
426 437
427 438 # Add xorg package
428 439 if [ "$ENABLE_XORG" = true ] ; then
429 440 APT_INCLUDES="${APT_INCLUDES},xorg"
430 441 fi
431 442
432 443 # Replace selected packages with smaller clones
433 444 if [ "$ENABLE_REDUCE" = true ] ; then
434 445 # Add levee package instead of vim-tiny
435 446 if [ "$REDUCE_VIM" = true ] ; then
436 447 APT_INCLUDES="$(echo ${APT_INCLUDES} | sed "s/vim-tiny/levee/")"
437 448 fi
438 449
439 450 # Add dropbear package instead of openssh-server
440 451 if [ "$REDUCE_SSHD" = true ] ; then
441 452 APT_INCLUDES="$(echo ${APT_INCLUDES} | sed "s/openssh-server/dropbear/")"
442 453 fi
443 454 fi
444 455
445 456 # Configure kernel sources if no KERNELSRC_DIR
446 457 if [ "$BUILD_KERNEL" = true ] && [ -z "$KERNELSRC_DIR" ] ; then
447 458 KERNELSRC_CONFIG=true
448 459 fi
449 460
450 461 # Configure reduced kernel
451 462 if [ "$KERNEL_REDUCE" = true ] ; then
452 463 KERNELSRC_CONFIG=false
453 464 fi
454 465
455 466 # Execute bootstrap scripts
456 467 for SCRIPT in bootstrap.d/*.sh; do
457 468 head -n 3 "$SCRIPT"
458 469 . "$SCRIPT"
459 470 done
460 471
461 472 ## Execute custom bootstrap scripts
462 473 if [ -d "custom.d" ] ; then
463 474 for SCRIPT in custom.d/*.sh; do
464 475 . "$SCRIPT"
465 476 done
466 477 fi
467 478
468 479 # Execute custom scripts inside the chroot
469 480 if [ -n "$CHROOT_SCRIPTS" ] && [ -d "$CHROOT_SCRIPTS" ] ; then
470 481 cp -r "${CHROOT_SCRIPTS}" "${R}/chroot_scripts"
471 482 chroot_exec /bin/bash -x <<'EOF'
472 483 for SCRIPT in /chroot_scripts/* ; do
473 484 if [ -f $SCRIPT -a -x $SCRIPT ] ; then
474 485 $SCRIPT
475 486 fi
476 487 done
477 488 EOF
478 489 rm -rf "${R}/chroot_scripts"
479 490 fi
480 491
481 492 # Remove c/c++ build environment from the chroot
482 493 chroot_remove_cc
483 494
484 495 # Remove apt-utils
485 496 if [ "$RELEASE" = "jessie" ] ; then
486 497 chroot_exec apt-get purge -qq -y --force-yes apt-utils
487 498 fi
488 499
489 500 # Generate required machine-id
490 501 MACHINE_ID=$(dbus-uuidgen)
491 502 echo -n "${MACHINE_ID}" > "${R}/var/lib/dbus/machine-id"
492 503 echo -n "${MACHINE_ID}" > "${ETC_DIR}/machine-id"
493 504
494 505 # APT Cleanup
495 506 chroot_exec apt-get -y clean
496 507 chroot_exec apt-get -y autoclean
497 508 chroot_exec apt-get -y autoremove
498 509
499 510 # Unmount mounted filesystems
500 511 umount -l "${R}/proc"
501 512 umount -l "${R}/sys"
502 513
503 514 # Clean up directories
504 515 rm -rf "${R}/run/*"
505 516 rm -rf "${R}/tmp/*"
506 517
507 518 # Clean up files
508 519 rm -f "${ETC_DIR}/ssh/ssh_host_*"
509 520 rm -f "${ETC_DIR}/dropbear/dropbear_*"
510 521 rm -f "${ETC_DIR}/apt/sources.list.save"
511 522 rm -f "${ETC_DIR}/resolvconf/resolv.conf.d/original"
512 523 rm -f "${ETC_DIR}/*-"
513 524 rm -f "${ETC_DIR}/apt/apt.conf.d/10proxy"
514 525 rm -f "${ETC_DIR}/resolv.conf"
515 526 rm -f "${R}/root/.bash_history"
516 527 rm -f "${R}/var/lib/urandom/random-seed"
517 528 rm -f "${R}/initrd.img"
518 529 rm -f "${R}/vmlinuz"
519 530 rm -f "${R}${QEMU_BINARY}"
520 531
521 532 # Calculate size of the chroot directory in KB
522 533 CHROOT_SIZE=$(expr `du -s "${R}" | awk '{ print $1 }'`)
523 534
524 535 # Calculate the amount of needed 512 Byte sectors
525 536 TABLE_SECTORS=$(expr 1 \* 1024 \* 1024 \/ 512)
526 537 FRMW_SECTORS=$(expr 64 \* 1024 \* 1024 \/ 512)
527 538 ROOT_OFFSET=$(expr ${TABLE_SECTORS} + ${FRMW_SECTORS})
528 539
529 540 # The root partition is EXT4
530 541 # This means more space than the actual used space of the chroot is used.
531 542 # As overhead for journaling and reserved blocks 25% are added.
532 543 ROOT_SECTORS=$(expr $(expr ${CHROOT_SIZE} + ${CHROOT_SIZE} \/ 100 \* 25) \* 1024 \/ 512)
533 544
534 545 # Calculate required image size in 512 Byte sectors
535 546 IMAGE_SECTORS=$(expr ${TABLE_SECTORS} + ${FRMW_SECTORS} + ${ROOT_SECTORS})
536 547
537 548 # Prepare image file
538 549 if [ "$ENABLE_SPLITFS" = true ] ; then
539 550 dd if=/dev/zero of="$IMAGE_NAME-frmw.img" bs=512 count=${TABLE_SECTORS}
540 551 dd if=/dev/zero of="$IMAGE_NAME-frmw.img" bs=512 count=0 seek=${FRMW_SECTORS}
541 552 dd if=/dev/zero of="$IMAGE_NAME-root.img" bs=512 count=${TABLE_SECTORS}
542 553 dd if=/dev/zero of="$IMAGE_NAME-root.img" bs=512 count=0 seek=${ROOT_SECTORS}
543 554
544 555 # Write firmware/boot partition tables
545 556 sfdisk -q -L -uS -f "$IMAGE_NAME-frmw.img" 2> /dev/null <<EOM
546 557 ${TABLE_SECTORS},${FRMW_SECTORS},c,*
547 558 EOM
548 559
549 560 # Write root partition table
550 561 sfdisk -q -L -uS -f "$IMAGE_NAME-root.img" 2> /dev/null <<EOM
551 562 ${TABLE_SECTORS},${ROOT_SECTORS},83
552 563 EOM
553 564
554 565 # Setup temporary loop devices
555 566 FRMW_LOOP="$(losetup -o 1M --sizelimit 64M -f --show $IMAGE_NAME-frmw.img)"
556 567 ROOT_LOOP="$(losetup -o 1M -f --show $IMAGE_NAME-root.img)"
557 568 else # ENABLE_SPLITFS=false
558 569 dd if=/dev/zero of="$IMAGE_NAME.img" bs=512 count=${TABLE_SECTORS}
559 570 dd if=/dev/zero of="$IMAGE_NAME.img" bs=512 count=0 seek=${IMAGE_SECTORS}
560 571
561 572 # Write partition table
562 573 sfdisk -q -L -uS -f "$IMAGE_NAME.img" 2> /dev/null <<EOM
563 574 ${TABLE_SECTORS},${FRMW_SECTORS},c,*
564 575 ${ROOT_OFFSET},${ROOT_SECTORS},83
565 576 EOM
566 577
567 578 # Setup temporary loop devices
568 579 FRMW_LOOP="$(losetup -o 1M --sizelimit 64M -f --show $IMAGE_NAME.img)"
569 580 ROOT_LOOP="$(losetup -o 65M -f --show $IMAGE_NAME.img)"
570 581 fi
571 582
572 583 if [ "$ENABLE_CRYPTFS" = true ] ; then
573 584 # Create dummy ext4 fs
574 585 mkfs.ext4 "$ROOT_LOOP"
575 586
576 587 # Setup password keyfile
577 588 touch .password
578 589 chmod 600 .password
579 590 echo -n ${CRYPTFS_PASSWORD} > .password
580 591
581 592 # Initialize encrypted partition
582 593 echo "YES" | cryptsetup luksFormat "${ROOT_LOOP}" -c "${CRYPTFS_CIPHER}" -s "${CRYPTFS_XTSKEYSIZE}" .password
583 594
584 595 # Open encrypted partition and setup mapping
585 596 cryptsetup luksOpen "${ROOT_LOOP}" -d .password "${CRYPTFS_MAPPING}"
586 597
587 598 # Secure delete password keyfile
588 599 shred -zu .password
589 600
590 601 # Update temporary loop device
591 602 ROOT_LOOP="/dev/mapper/${CRYPTFS_MAPPING}"
592 603
593 604 # Wipe encrypted partition (encryption cipher is used for randomness)
594 605 dd if=/dev/zero of="${ROOT_LOOP}" bs=512 count=$(blockdev --getsz "${ROOT_LOOP}")
595 606 fi
596 607
597 608 # Build filesystems
598 609 mkfs.vfat "$FRMW_LOOP"
599 610 mkfs.ext4 "$ROOT_LOOP"
600 611
601 612 # Mount the temporary loop devices
602 613 mkdir -p "$BUILDDIR/mount"
603 614 mount "$ROOT_LOOP" "$BUILDDIR/mount"
604 615
605 616 mkdir -p "$BUILDDIR/mount/boot/firmware"
606 617 mount "$FRMW_LOOP" "$BUILDDIR/mount/boot/firmware"
607 618
608 619 # Copy all files from the chroot to the loop device mount point directory
609 620 rsync -a "${R}/" "$BUILDDIR/mount/"
610 621
611 622 # Unmount all temporary loop devices and mount points
612 623 cleanup
613 624
614 625 # Create block map file(s) of image(s)
615 626 if [ "$ENABLE_SPLITFS" = true ] ; then
616 627 # Create block map files for "bmaptool"
617 628 bmaptool create -o "$IMAGE_NAME-frmw.bmap" "$IMAGE_NAME-frmw.img"
618 629 bmaptool create -o "$IMAGE_NAME-root.bmap" "$IMAGE_NAME-root.img"
619 630
620 631 # Image was successfully created
621 632 echo "$IMAGE_NAME-frmw.img ($(expr \( ${TABLE_SECTORS} + ${FRMW_SECTORS} \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
622 633 echo "$IMAGE_NAME-root.img ($(expr \( ${TABLE_SECTORS} + ${ROOT_SECTORS} \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
623 634 else
624 635 # Create block map file for "bmaptool"
625 636 bmaptool create -o "$IMAGE_NAME.bmap" "$IMAGE_NAME.img"
626 637
627 638 # Image was successfully created
628 639 echo "$IMAGE_NAME.img ($(expr \( ${TABLE_SECTORS} + ${FRMW_SECTORS} + ${ROOT_SECTORS} \) \* 512 \/ 1024 \/ 1024)M)" ": successfully created"
629 640 fi
General Comments 0
Vous devez vous connecter pour laisser un commentaire. Se connecter maintenant