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