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