@@ -1,300 +1,301 | |||
|
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 | source ./functions.sh |
|
19 | 19 | |
|
20 | 20 | set -e |
|
21 | 21 | set -x |
|
22 | 22 | |
|
23 | 23 | # Debian release |
|
24 | 24 | RELEASE=${RELEASE:=jessie} |
|
25 | 25 | KERNEL=${KERNEL:=3.18.0-trunk-rpi2} |
|
26 | 26 | |
|
27 | 27 | # Build settings |
|
28 | 28 | BASEDIR=$(pwd)/images/${RELEASE} |
|
29 | 29 | BUILDDIR=${BASEDIR}/build |
|
30 | 30 | |
|
31 | 31 | # General settings |
|
32 | 32 | HOSTNAME=${HOSTNAME:=rpi2-${RELEASE}} |
|
33 | 33 | PASSWORD=${PASSWORD:=raspberry} |
|
34 | 34 | DEFLOCAL=${DEFLOCAL:="en_US.UTF-8"} |
|
35 | 35 | TIMEZONE=${TIMEZONE:="Europe/Berlin"} |
|
36 | 36 | XKBMODEL=${XKBMODEL:=""} |
|
37 | 37 | XKBLAYOUT=${XKBLAYOUT:=""} |
|
38 | 38 | XKBVARIANT=${XKBVARIANT:=""} |
|
39 | 39 | XKBOPTIONS=${XKBOPTIONS:=""} |
|
40 | 40 | EXPANDROOT=${EXPANDROOT:=true} |
|
41 | 41 | |
|
42 | 42 | # Network settings |
|
43 | 43 | ENABLE_DHCP=${ENABLE_DHCP:=true} |
|
44 | 44 | # NET_* settings are ignored when ENABLE_DHCP=true |
|
45 | 45 | # NET_ADDRESS is an IPv4 or IPv6 address and its prefix, separated by "/" |
|
46 | 46 | NET_ADDRESS=${NET_ADDRESS:=""} |
|
47 | 47 | NET_GATEWAY=${NET_GATEWAY:=""} |
|
48 | 48 | NET_DNS_1=${NET_DNS_1:=""} |
|
49 | 49 | NET_DNS_2=${NET_DNS_2:=""} |
|
50 | 50 | NET_DNS_DOMAINS=${NET_DNS_DOMAINS:=""} |
|
51 | 51 | NET_NTP_1=${NET_NTP_1:=""} |
|
52 | 52 | NET_NTP_2=${NET_NTP_2:=""} |
|
53 | 53 | |
|
54 | 54 | # APT settings |
|
55 | 55 | APT_PROXY=${APT_PROXY:=""} |
|
56 | 56 | APT_SERVER=${APT_SERVER:="ftp.debian.org"} |
|
57 | 57 | |
|
58 | 58 | # Feature settings |
|
59 | 59 | ENABLE_CONSOLE=${ENABLE_CONSOLE:=true} |
|
60 | 60 | ENABLE_IPV6=${ENABLE_IPV6:=true} |
|
61 | 61 | ENABLE_SSHD=${ENABLE_SSHD:=true} |
|
62 | 62 | ENABLE_SOUND=${ENABLE_SOUND:=true} |
|
63 | 63 | ENABLE_DBUS=${ENABLE_DBUS:=true} |
|
64 | 64 | ENABLE_HWRANDOM=${ENABLE_HWRANDOM:=true} |
|
65 | 65 | ENABLE_MINGPU=${ENABLE_MINGPU:=false} |
|
66 | 66 | ENABLE_XORG=${ENABLE_XORG:=false} |
|
67 | 67 | ENABLE_WM=${ENABLE_WM:=""} |
|
68 | 68 | ENABLE_RSYSLOG=${ENABLE_RSYSLOG:=true} |
|
69 | 69 | ENABLE_USER=${ENABLE_USER:=true} |
|
70 | 70 | ENABLE_ROOT=${ENABLE_ROOT:=false} |
|
71 | 71 | ENABLE_ROOT_SSH=${ENABLE_ROOT_SSH:=false} |
|
72 | 72 | |
|
73 | 73 | # Advanced settings |
|
74 | 74 | ENABLE_MINBASE=${ENABLE_MINBASE:=false} |
|
75 | 75 | ENABLE_UBOOT=${ENABLE_UBOOT:=false} |
|
76 | 76 | ENABLE_FBTURBO=${ENABLE_FBTURBO:=false} |
|
77 | 77 | ENABLE_HARDNET=${ENABLE_HARDNET:=false} |
|
78 | 78 | ENABLE_IPTABLES=${ENABLE_IPTABLES:=false} |
|
79 | 79 | |
|
80 | 80 | # Kernel compilation settings |
|
81 | 81 | BUILD_KERNEL=${BUILD_KERNEL:=false} |
|
82 | 82 | |
|
83 | 83 | # Image chroot path |
|
84 | 84 | R=${BUILDDIR}/chroot |
|
85 | 85 | CHROOT_SCRIPTS=${CHROOT_SCRIPTS:=""} |
|
86 | 86 | |
|
87 | 87 | # Packages required for bootstrapping |
|
88 | 88 | REQUIRED_PACKAGES="debootstrap debian-archive-keyring qemu-user-static binfmt-support dosfstools rsync bmap-tools whois git-core" |
|
89 | 89 | |
|
90 | 90 | # Missing packages that need to be installed |
|
91 | 91 | MISSING_PACKAGES="" |
|
92 | 92 | |
|
93 | 93 | # Packages required in the chroot build environment |
|
94 | 94 | APT_INCLUDES=${APT_INCLUDES:=""} |
|
95 | 95 | APT_INCLUDES="${APT_INCLUDES},apt-transport-https,ca-certificates,debian-archive-keyring,dialog,sudo" |
|
96 | 96 | |
|
97 | 97 | set +x |
|
98 | 98 | |
|
99 | 99 | # Are we running as root? |
|
100 | 100 | if [ "$(id -u)" -ne "0" ] ; then |
|
101 | 101 | echo "this script must be executed with root privileges" |
|
102 | 102 | exit 1 |
|
103 | 103 | fi |
|
104 | 104 | |
|
105 | 105 | # Add packages required for kernel cross compilation |
|
106 | 106 | if [ "$BUILD_KERNEL" = true ] ; then |
|
107 | 107 | REQUIRED_PACKAGES="${REQUIRED_PACKAGES} crossbuild-essential-armhf" |
|
108 | 108 | fi |
|
109 | 109 | |
|
110 | 110 | # Check if all required packages are installed |
|
111 | 111 | for package in $REQUIRED_PACKAGES ; do |
|
112 | 112 | if [ "`dpkg-query -W -f='${Status}' $package`" != "install ok installed" ] ; then |
|
113 | 113 | MISSING_PACKAGES="$MISSING_PACKAGES $package" |
|
114 | 114 | fi |
|
115 | 115 | done |
|
116 | 116 | |
|
117 | 117 | # Ask if missing packages should get installed right now |
|
118 | 118 | if [ -n "$MISSING_PACKAGES" ] ; then |
|
119 | 119 | echo "the following packages needed by this script are not installed:" |
|
120 | 120 | echo "$MISSING_PACKAGES" |
|
121 | 121 | |
|
122 | 122 | echo -n "\ndo you want to install the missing packages right now? [y/n] " |
|
123 | 123 | read confirm |
|
124 | 124 | if [ "$confirm" != "y" ] ; then |
|
125 | 125 | exit 1 |
|
126 | 126 | fi |
|
127 | 127 | fi |
|
128 | 128 | |
|
129 | 129 | # Make sure all required packages are installed |
|
130 | 130 | apt-get -qq -y install ${REQUIRED_PACKAGES} |
|
131 | 131 | |
|
132 | 132 | # Don't clobber an old build |
|
133 | 133 | if [ -e "$BUILDDIR" ]; then |
|
134 | 134 | echo "directory $BUILDDIR already exists, not proceeding" |
|
135 | 135 | exit 1 |
|
136 | 136 | fi |
|
137 | 137 | |
|
138 | 138 | set -x |
|
139 | 139 | |
|
140 | 140 | # Call "cleanup" function on various signals and errors |
|
141 | 141 | trap cleanup 0 1 2 3 6 |
|
142 | 142 | |
|
143 | 143 | # Set up chroot directory |
|
144 | 144 | mkdir -p $R |
|
145 | 145 | |
|
146 | 146 | # Add required packages for the minbase installation |
|
147 | 147 | if [ "$ENABLE_MINBASE" = true ] ; then |
|
148 | 148 | APT_INCLUDES="${APT_INCLUDES},vim-tiny,netbase,net-tools" |
|
149 | 149 | else |
|
150 | 150 | APT_INCLUDES="${APT_INCLUDES},locales,keyboard-configuration,console-setup" |
|
151 | 151 | fi |
|
152 | 152 | |
|
153 | 153 | # Add parted package, required to get partprobe utility |
|
154 | 154 | if [ "$EXPANDROOT" = true ] ; then |
|
155 | 155 | APT_INCLUDES="${APT_INCLUDES},parted" |
|
156 | 156 | fi |
|
157 | 157 | |
|
158 | 158 | # Add dbus package, recommended if using systemd |
|
159 | 159 | if [ "$ENABLE_DBUS" = true ] ; then |
|
160 | 160 | APT_INCLUDES="${APT_INCLUDES},dbus" |
|
161 | 161 | fi |
|
162 | 162 | |
|
163 | 163 | # Add iptables IPv4/IPv6 package |
|
164 | 164 | if [ "$ENABLE_IPTABLES" = true ] ; then |
|
165 | 165 | APT_INCLUDES="${APT_INCLUDES},iptables" |
|
166 | 166 | fi |
|
167 | 167 | |
|
168 | 168 | # Add openssh server package |
|
169 | 169 | if [ "$ENABLE_SSHD" = true ] ; then |
|
170 | 170 | APT_INCLUDES="${APT_INCLUDES},openssh-server" |
|
171 | 171 | fi |
|
172 | 172 | |
|
173 | 173 | # Add alsa-utils package |
|
174 | 174 | if [ "$ENABLE_SOUND" = true ] ; then |
|
175 | 175 | APT_INCLUDES="${APT_INCLUDES},alsa-utils" |
|
176 | 176 | fi |
|
177 | 177 | |
|
178 | 178 | # Add rng-tools package |
|
179 | 179 | if [ "$ENABLE_HWRANDOM" = true ] ; then |
|
180 | 180 | APT_INCLUDES="${APT_INCLUDES},rng-tools" |
|
181 | 181 | fi |
|
182 | 182 | |
|
183 | 183 | if [ "$ENABLE_USER" = true ]; then |
|
184 | 184 | APT_INCLUDES="${APT_INCLUDES},sudo" |
|
185 | 185 | fi |
|
186 | 186 | |
|
187 | 187 | # Add fbturbo video driver |
|
188 | 188 | if [ "$ENABLE_FBTURBO" = true ] ; then |
|
189 | 189 | # Enable xorg package dependencies |
|
190 | 190 | ENABLE_XORG=true |
|
191 | 191 | fi |
|
192 | 192 | |
|
193 | 193 | # Add user defined window manager package |
|
194 | 194 | if [ -n "$ENABLE_WM" ] ; then |
|
195 | 195 | APT_INCLUDES="${APT_INCLUDES},${ENABLE_WM}" |
|
196 | 196 | |
|
197 | 197 | # Enable xorg package dependencies |
|
198 | 198 | ENABLE_XORG=true |
|
199 | 199 | fi |
|
200 | 200 | |
|
201 | 201 | # Add xorg package |
|
202 | 202 | if [ "$ENABLE_XORG" = true ] ; then |
|
203 | 203 | APT_INCLUDES="${APT_INCLUDES},xorg" |
|
204 | 204 | fi |
|
205 | 205 | |
|
206 | 206 | ## Main bootstrap |
|
207 | 207 | for i in bootstrap.d/*.sh; do |
|
208 | 208 | . $i |
|
209 | 209 | done |
|
210 | 210 | |
|
211 | 211 | for i in custom.d/*.sh; do |
|
212 | 212 | . $i |
|
213 | 213 | done |
|
214 | 214 | |
|
215 | 215 | # Invoke custom scripts |
|
216 | 216 | if [ -n "${CHROOT_SCRIPTS}" ]; then |
|
217 | 217 | cp -r "${CHROOT_SCRIPTS}" "${R}/chroot_scripts" |
|
218 | 218 | LANG=C chroot $R bash -c 'for SCRIPT in /chroot_scripts/*; do if [ -f $SCRIPT -a -x $SCRIPT ]; then $SCRIPT; fi done;' |
|
219 | 219 | rm -rf "${R}/chroot_scripts" |
|
220 | 220 | fi |
|
221 | 221 | |
|
222 | 222 | ## Cleanup |
|
223 | 223 | chroot_exec apt-get -y clean |
|
224 | 224 | chroot_exec apt-get -y autoclean |
|
225 | 225 | chroot_exec apt-get -y autoremove |
|
226 | 226 | |
|
227 | 227 | # Unmount mounted filesystems |
|
228 | 228 | umount -l $R/proc |
|
229 | 229 | umount -l $R/sys |
|
230 | 230 | |
|
231 | 231 | # Clean up files |
|
232 | 232 | rm -f $R/etc/apt/sources.list.save |
|
233 | 233 | rm -f $R/etc/resolvconf/resolv.conf.d/original |
|
234 | 234 | rm -rf $R/run |
|
235 | 235 | mkdir -p $R/run |
|
236 | 236 | rm -f $R/etc/*- |
|
237 | 237 | rm -f $R/root/.bash_history |
|
238 | 238 | rm -rf $R/tmp/* |
|
239 | 239 | rm -f $R/var/lib/urandom/random-seed |
|
240 | 240 | [ -L $R/var/lib/dbus/machine-id ] || rm -f $R/var/lib/dbus/machine-id |
|
241 | 241 | rm -f $R/etc/machine-id |
|
242 | 242 | rm -fr $R/etc/apt/apt.conf.d/10proxy |
|
243 | rm -f $R/etc/resolv.conf | |
|
243 | 244 | |
|
244 | 245 | # Calculate size of the chroot directory in KB |
|
245 | 246 | CHROOT_SIZE=$(expr `du -s $R | awk '{ print $1 }'`) |
|
246 | 247 | |
|
247 | 248 | # Calculate the amount of needed 512 Byte sectors |
|
248 | 249 | TABLE_SECTORS=$(expr 1 \* 1024 \* 1024 \/ 512) |
|
249 | 250 | BOOT_SECTORS=$(expr 64 \* 1024 \* 1024 \/ 512) |
|
250 | 251 | ROOT_OFFSET=$(expr ${TABLE_SECTORS} + ${BOOT_SECTORS}) |
|
251 | 252 | |
|
252 | 253 | # The root partition is EXT4 |
|
253 | 254 | # This means more space than the actual used space of the chroot is used. |
|
254 | 255 | # As overhead for journaling and reserved blocks 20% are added. |
|
255 | 256 | ROOT_SECTORS=$(expr $(expr ${CHROOT_SIZE} + ${CHROOT_SIZE} \/ 100 \* 20) \* 1024 \/ 512) |
|
256 | 257 | |
|
257 | 258 | # Calculate required image size in 512 Byte sectors |
|
258 | 259 | IMAGE_SECTORS=$(expr ${TABLE_SECTORS} + ${BOOT_SECTORS} + ${ROOT_SECTORS}) |
|
259 | 260 | |
|
260 | 261 | # Prepare date string for image file name |
|
261 | 262 | DATE="$(date +%Y-%m-%d)" |
|
262 | 263 | |
|
263 | 264 | # Prepare image file |
|
264 | 265 | dd if=/dev/zero of="$BASEDIR/${DATE}-debian-${RELEASE}.img" bs=512 count=${TABLE_SECTORS} |
|
265 | 266 | dd if=/dev/zero of="$BASEDIR/${DATE}-debian-${RELEASE}.img" bs=512 count=0 seek=${IMAGE_SECTORS} |
|
266 | 267 | |
|
267 | 268 | # Write partition table |
|
268 | 269 | sfdisk -q -f "$BASEDIR/${DATE}-debian-${RELEASE}.img" <<EOM |
|
269 | 270 | unit: sectors |
|
270 | 271 | |
|
271 | 272 | 1 : start= ${TABLE_SECTORS}, size= ${BOOT_SECTORS}, Id= c, bootable |
|
272 | 273 | 2 : start= ${ROOT_OFFSET}, size= ${ROOT_SECTORS}, Id=83 |
|
273 | 274 | 3 : start= 0, size= 0, Id= 0 |
|
274 | 275 | 4 : start= 0, size= 0, Id= 0 |
|
275 | 276 | EOM |
|
276 | 277 | |
|
277 | 278 | # Set up temporary loop devices and build filesystems |
|
278 | 279 | VFAT_LOOP="$(losetup -o 1M --sizelimit 64M -f --show $BASEDIR/${DATE}-debian-${RELEASE}.img)" |
|
279 | 280 | EXT4_LOOP="$(losetup -o 65M -f --show $BASEDIR/${DATE}-debian-${RELEASE}.img)" |
|
280 | 281 | mkfs.vfat "$VFAT_LOOP" |
|
281 | 282 | mkfs.ext4 "$EXT4_LOOP" |
|
282 | 283 | |
|
283 | 284 | # Mount the temporary loop devices |
|
284 | 285 | mkdir -p "$BUILDDIR/mount" |
|
285 | 286 | mount "$EXT4_LOOP" "$BUILDDIR/mount" |
|
286 | 287 | |
|
287 | 288 | mkdir -p "$BUILDDIR/mount/boot/firmware" |
|
288 | 289 | mount "$VFAT_LOOP" "$BUILDDIR/mount/boot/firmware" |
|
289 | 290 | |
|
290 | 291 | # Copy all files from the chroot to the loop device mount point directory |
|
291 | 292 | rsync -a "$R/" "$BUILDDIR/mount/" |
|
292 | 293 | |
|
293 | 294 | # Unmount all temporary loop devices and mount points |
|
294 | 295 | cleanup |
|
295 | 296 | |
|
296 | 297 | # (optinal) create block map file for "bmaptool" |
|
297 | 298 | bmaptool create -o "$BASEDIR/${DATE}-debian-${RELEASE}.bmap" "$BASEDIR/${DATE}-debian-${RELEASE}.img" |
|
298 | 299 | |
|
299 | 300 | # Image was successfully created |
|
300 | 301 | echo "$BASEDIR/${DATE}-debian-${RELEASE}.img (${IMAGE_SIZE})" ": successfully created" |
General Comments 0
Vous devez vous connecter pour laisser un commentaire.
Se connecter maintenant