##// END OF EJS Templates
Removed ENABLE_SYSTEMD(=false)
Jan Wagner -
r14:f33a610b0e30
parent child
Show More
@@ -1,767 +1,756
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 cleanup (){
19 19 set +x
20 20 set +e
21 21 echo "removing temporary mount points ..."
22 22 umount -l $R/proc 2> /dev/null
23 23 umount -l $R/sys 2> /dev/null
24 24 umount -l $R/dev/pts 2> /dev/null
25 25 umount "$BUILDDIR/mount/boot/firmware" 2> /dev/null
26 26 umount "$BUILDDIR/mount" 2> /dev/null
27 27 losetup -d "$EXT4_LOOP" 2> /dev/null
28 28 losetup -d "$VFAT_LOOP" 2> /dev/null
29 29 trap - 0 1 2 3 6
30 30 }
31 31
32 32 set -e
33 33 set -x
34 34
35 35 RELEASE=${RELEASE:=jessie}
36 36
37 37 # Build settings
38 38 BASEDIR=./images/${RELEASE}
39 39 BUILDDIR=${BASEDIR}/build
40 40
41 41 # General settings
42 42 HOSTNAME=${HOSTNAME:=rpi2-${RELEASE}}
43 43 PASSWORD=${PASSWORD:=raspberry}
44 44 DEFLOCAL=${DEFLOCAL:="en_US.UTF-8"}
45 45 TIMEZONE=${TIMEZONE:="Europe/Berlin"}
46 46
47 47 # APT settings
48 48 APT_PROXY=${APT_PROXY:=""}
49 49 APT_SERVER=${APT_SERVER:="ftp.debian.org"}
50 50
51 51 # Feature settings
52 52 ENABLE_CONSOLE=${ENABLE_CONSOLE:=true}
53 53 ENABLE_IPV6=${ENABLE_IPV6:=true}
54 54 ENABLE_SSHD=${ENABLE_SSHD:=true}
55 55 ENABLE_SOUND=${ENABLE_SOUND:=true}
56 ENABLE_SYSTEMD=${ENABLE_SYSTEMD:=true}
57 56 ENABLE_DBUS=${ENABLE_DBUS:=true}
58 57 ENABLE_HWRANDOM=${ENABLE_HWRANDOM:=true}
59 58 ENABLE_MINGPU=${ENABLE_MINGPU:=false}
60 59 ENABLE_XORG=${ENABLE_XORG:=false}
61 60 ENABLE_FLUXBOX=${ENABLE_FLUXBOX:=false}
62 61
63 62 # Advanced settings
64 63 ENABLE_UBOOT=${ENABLE_UBOOT:=false}
65 64 ENABLE_HARDNET=${ENABLE_HARDNET:=false}
66 65 ENABLE_IPTABLES=${ENABLE_IPTABLES:=false}
67 66
68 67 # Image chroot path
69 68 R=${BUILDDIR}/chroot
70 69
71 70 # Packages required for bootstrapping
72 71 REQUIRED_PACKAGES="debootstrap debian-archive-keyring qemu-user-static dosfstools rsync bmap-tools whois git-core"
73 72
74 73 # Missing packages that need to be installed
75 74 MISSING_PACKAGES=""
76 75
77 76 # Packages required in the chroot build enviroment
78 APT_INCLUDES="apt-transport-https,ca-certificates,debian-archive-keyring,dialog,locales,apt-utils,vim-tiny"
77 APT_INCLUDES="apt-transport-https,ca-certificates,debian-archive-keyring,dialog,locales"
79 78
80 79 set +x
81 80
82 81 # Are we running as root?
83 82 if [ "$(id -u)" -ne "0" ] ; then
84 83 echo "this script must be executed with root privileges"
85 84 exit 1
86 85 fi
87 86
88 87 # Check if all required packages are installed
89 88 for package in $REQUIRED_PACKAGES ; do
90 89 if [ "`dpkg-query -W -f='${Status}' $package`" != "install ok installed" ] ; then
91 90 MISSING_PACKAGES="$MISSING_PACKAGES $package"
92 91 fi
93 92 done
94 93
95 94 # Ask if missing packages should get installed right now
96 95 if [ -n "$MISSING_PACKAGES" ] ; then
97 96 echo "the following packages needed by this script are not installed:"
98 97 echo "$MISSING_PACKAGES"
99 98
100 99 echo -n "\ndo you want to install the missing packages right now? [y/n] "
101 100 read confirm
102 101 if [ "$confirm" != "y" ] ; then
103 102 exit 1
104 103 fi
105 104 fi
106 105
107 106 # Make sure all required packages are installed
108 107 apt-get -qq -y install ${REQUIRED_PACKAGES}
109 108
110 109 # Don't clobber an old build
111 110 if [ -e "$BUILDDIR" ]; then
112 111 echo "directory $BUILDDIR already exists, not proceeding"
113 112 exit 1
114 113 fi
115 114
116 115 set -x
117 116
118 117 # Call "cleanup" function on various signals and errors
119 118 trap cleanup 0 1 2 3 6
120 119
121 120 # Set up chroot directory
122 121 mkdir -p $R
123 122
124 # Use traditional SystemV init instead of systemd services
125 if [ "$ENABLE_SYSTEMD" = false ] ; then
126 APT_INCLUDES="${APT_INCLUDES},sysvinit-core"
127 fi
128
129 123 # Add dbus package, recommended if using systemd
130 124 if [ "$ENABLE_DBUS" = true ] ; then
131 125 APT_INCLUDES="${APT_INCLUDES},dbus"
132 126 fi
133 127
134 128 # Add openssh server package
135 129 if [ "$ENABLE_SSHD" = true ] ; then
136 130 APT_INCLUDES="${APT_INCLUDES},openssh-server"
137 131 fi
138 132
139 133 # Add rng-tools package
140 134 if [ "$ENABLE_HWRANDOM" = true ] ; then
141 135 APT_INCLUDES="${APT_INCLUDES},rng-tools"
142 136 fi
143 137
144 138 # Add xorg package
145 139 if [ "$ENABLE_XORG" = true ] ; then
146 140 APT_INCLUDES="${APT_INCLUDES},xorg"
147 141 fi
148 142
149 143 # Add fluxbox package with eterm
150 144 if [ "$ENABLE_FLUXBOX" = true ] ; then
151 145 APT_INCLUDES="${APT_INCLUDES},fluxbox,eterm"
152 146 fi
153 147
154 148 if [ -z "$APT_PROXY" ] ; then
155 149 APT_PROXY="http://"
156 150 fi
157 151
158 152 # Base debootstrap (unpack only)
159 153 debootstrap --arch=armhf --foreign --include=${APT_INCLUDES} $RELEASE $R ${APT_PROXY}${APT_SERVER}/debian
160 154 cp /usr/bin/qemu-arm-static $R/usr/bin
161 155
162 # Remove systemd related packages from list of packages to be bootstrapped
163 if [ "$ENABLE_SYSTEMD" = false ] ; then
164 chroot $R sed -i -e 's/systemd systemd-sysv //g' /debootstrap/required
165 fi
166
167 156 # Copy debian-archive-keyring.pgp
168 157 chroot $R mkdir -p /usr/share/keyrings
169 158 cp /usr/share/keyrings/debian-archive-keyring.gpg $R/usr/share/keyrings/debian-archive-keyring.gpg
170 159
171 160 # Complete the bootstrapping proccess
172 161 chroot $R /debootstrap/debootstrap --second-stage
173 162
174 163 # Mount required filesystems
175 164 mount -t proc none $R/proc
176 165 mount -t sysfs none $R/sys
177 166 mount --bind /dev/pts $R/dev/pts
178 167
179 168 # Use proxy inside chroot
180 169 if [ -z "$APT_PROXY" ] ; then
181 170 echo "Acquire::http::Proxy \"$APT_PROXY\"" >> $R/etc/apt/apt.conf.d/10proxy
182 171 fi
183 172
184 173 # Pin package flash-kernel to repositories.collabora.co.uk
185 174 cat <<EOM >$R/etc/apt/preferences.d/flash-kernel
186 175 Package: flash-kernel
187 176 Pin: origin repositories.collabora.co.uk
188 177 Pin-Priority: 1000
189 178 EOM
190 179
191 180 # Set up timezone
192 181 echo ${TIMEZONE} >$R/etc/timezone
193 182 LANG=C chroot $R dpkg-reconfigure -f noninteractive tzdata
194 183
195 184 # Set up default locales to "en_US.UTF-8" default
196 185 LANG=C chroot $R sed -i '/${DEFLOCAL}/s/^#//' /etc/locale.gen
197 186 LANG=C chroot $R locale-gen ${DEFLOCAL}
198 187
199 188 # Upgrade collabora package index and install collabora keyring
200 189 echo "deb https://repositories.collabora.co.uk/debian ${RELEASE} rpi2" >$R/etc/apt/sources.list
201 190 LANG=C chroot $R apt-get -qq -y update
202 191 LANG=C chroot $R apt-get -qq -y --force-yes install collabora-obs-archive-keyring
203 192
204 193 # Set up initial sources.list
205 194 cat <<EOM >$R/etc/apt/sources.list
206 195 deb http://${APT_SERVER}/debian ${RELEASE} main contrib
207 196 #deb-src http://${APT_SERVER}/debian ${RELEASE} main contrib
208 197
209 198 deb http://${APT_SERVER}/debian/ ${RELEASE}-updates main contrib
210 199 #deb-src http://${APT_SERVER}/debian/ ${RELEASE}-updates main contrib
211 200
212 201 deb http://security.debian.org/ ${RELEASE}/updates main contrib
213 202 #deb-src http://security.debian.org/ ${RELEASE}/updates main contrib
214 203
215 204 deb https://repositories.collabora.co.uk/debian ${RELEASE} rpi2
216 205 EOM
217 206
218 207 # Upgrade package index and update all installed packages and changed dependencies
219 208 LANG=C chroot $R apt-get -qq -y update
220 209 LANG=C chroot $R apt-get -qq -y -u dist-upgrade
221 210
222 211 # Kernel installation
223 212 # Install flash-kernel last so it doesn't try (and fail) to detect the platform in the chroot
224 213 LANG=C chroot $R apt-get -qq -y --no-install-recommends install linux-image-3.18.0-trunk-rpi2
225 214 LANG=C chroot $R apt-get -qq -y install flash-kernel
226 215
227 216 VMLINUZ="$(ls -1 $R/boot/vmlinuz-* | sort | tail -n 1)"
228 217 [ -z "$VMLINUZ" ] && exit 1
229 218 mkdir -p $R/boot/firmware
230 219
231 220 # required boot binaries from raspberry/firmware github (commit: "kernel: Bump to 3.18.10")
232 221 wget -q -O $R/boot/firmware/bootcode.bin https://github.com/raspberrypi/firmware/raw/cd355a9dd4f1f4de2e79b0c8e102840885cdf1de/boot/bootcode.bin
233 222 wget -q -O $R/boot/firmware/fixup_cd.dat https://github.com/raspberrypi/firmware/raw/cd355a9dd4f1f4de2e79b0c8e102840885cdf1de/boot/fixup_cd.dat
234 223 wget -q -O $R/boot/firmware/fixup.dat https://github.com/raspberrypi/firmware/raw/cd355a9dd4f1f4de2e79b0c8e102840885cdf1de/boot/fixup.dat
235 224 wget -q -O $R/boot/firmware/fixup_x.dat https://github.com/raspberrypi/firmware/raw/cd355a9dd4f1f4de2e79b0c8e102840885cdf1de/boot/fixup_x.dat
236 225 wget -q -O $R/boot/firmware/start_cd.elf https://github.com/raspberrypi/firmware/raw/cd355a9dd4f1f4de2e79b0c8e102840885cdf1de/boot/start_cd.elf
237 226 wget -q -O $R/boot/firmware/start.elf https://github.com/raspberrypi/firmware/raw/cd355a9dd4f1f4de2e79b0c8e102840885cdf1de/boot/start.elf
238 227 wget -q -O $R/boot/firmware/start_x.elf https://github.com/raspberrypi/firmware/raw/cd355a9dd4f1f4de2e79b0c8e102840885cdf1de/boot/start_x.elf
239 228 cp $VMLINUZ $R/boot/firmware/kernel7.img
240 229
241 230 # Set up hosts
242 231 echo ${HOSTNAME} >$R/etc/hostname
243 232 cat <<EOM >$R/etc/hosts
244 233 127.0.0.1 localhost
245 234 127.0.1.1 ${HOSTNAME}
246 235 EOM
247 236
248 237 if [ "$ENABLE_IPV6" = true ] ; then
249 238 cat <<EOM >>$R/etc/hosts
250 239
251 240 ::1 localhost ip6-localhost ip6-loopback
252 241 ff02::1 ip6-allnodes
253 242 ff02::2 ip6-allrouters
254 243 EOM
255 244 fi
256 245
257 246 # Generate crypt(3) password string
258 247 ENCRYPTED_PASSWORD=`mkpasswd -m sha-512 ${PASSWORD}`
259 248
260 249 # Set up default user
261 250 LANG=C chroot $R adduser --gecos "Raspberry PI user" --add_extra_groups --disabled-password pi
262 251 LANG=C chroot $R usermod -a -G sudo -p "${ENCRYPTED_PASSWORD}" pi
263 252
264 253 # Set up root password
265 254 LANG=C chroot $R usermod -p "${ENCRYPTED_PASSWORD}" root
266 255
267 256 # Set up interfaces
268 257 cat <<EOM >$R/etc/network/interfaces
269 258 # interfaces(5) file used by ifup(8) and ifdown(8)
270 259 # Include files from /etc/network/interfaces.d:
271 260 source-directory /etc/network/interfaces.d
272 261
273 262 # The loopback network interface
274 263 auto lo
275 264 iface lo inet loopback
276 265
277 266 # The primary network interface
278 267 allow-hotplug eth0
279 268 iface eth0 inet dhcp
280 269 EOM
281 270
282 271 # Set up firmware boot cmdline
283 272 CMDLINE="dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootfstype=ext4 rootflags=commit=100,data=writeback elevator=deadline rootwait net.ifnames=1 console=tty1"
284 273
285 274 # Set up serial console support (if requested)
286 275 if [ "$ENABLE_CONSOLE" = true ] ; then
287 276 CMDLINE="${CMDLINE} console=ttyAMA0,115200 kgdboc=ttyAMA0,115200"
288 277 fi
289 278
290 279 # Set up ipv6 support (if requested)
291 280 if [ "$ENABLE_IPV6" = false ] ; then
292 281 CMDLINE="${CMDLINE} ipv6.disable=1"
293 282 fi
294 283
295 284 echo "${CMDLINE}" >$R/boot/firmware/cmdline.txt
296 285
297 286 # Set up firmware config
298 287 cat <<EOM >$R/boot/firmware/config.txt
299 288 # For more options and information see
300 289 # http://www.raspberrypi.org/documentation/configuration/config-txt.md
301 290 # Some settings may impact device functionality. See link above for details
302 291
303 292 # uncomment if you get no picture on HDMI for a default "safe" mode
304 293 #hdmi_safe=1
305 294
306 295 # uncomment this if your display has a black border of unused pixels visible
307 296 # and your display can output without overscan
308 297 #disable_overscan=1
309 298
310 299 # uncomment the following to adjust overscan. Use positive numbers if console
311 300 # goes off screen, and negative if there is too much border
312 301 #overscan_left=16
313 302 #overscan_right=16
314 303 #overscan_top=16
315 304 #overscan_bottom=16
316 305
317 306 # uncomment to force a console size. By default it will be display's size minus
318 307 # overscan.
319 308 #framebuffer_width=1280
320 309 #framebuffer_height=720
321 310
322 311 # uncomment if hdmi display is not detected and composite is being output
323 312 #hdmi_force_hotplug=1
324 313
325 314 # uncomment to force a specific HDMI mode (this will force VGA)
326 315 #hdmi_group=1
327 316 #hdmi_mode=1
328 317
329 318 # uncomment to force a HDMI mode rather than DVI. This can make audio work in
330 319 # DMT (computer monitor) modes
331 320 #hdmi_drive=2
332 321
333 322 # uncomment to increase signal to HDMI, if you have interference, blanking, or
334 323 # no display
335 324 #config_hdmi_boost=4
336 325
337 326 # uncomment for composite PAL
338 327 #sdtv_mode=2
339 328
340 329 # uncomment to overclock the arm. 700 MHz is the default.
341 330 #arm_freq=800
342 331 EOM
343 332
344 333 # Set smallest possible GPU memory allocation size: 16MB (no X)
345 334 if [ "$ENABLE_MINGPU" = true ] ; then
346 335 echo "gpu_mem=16" >>$R/boot/firmware/config.txt
347 336 fi
348 337
349 338 # Create symlinks
350 339 ln -sf firmware/config.txt $R/boot/config.txt
351 340 ln -sf firmware/cmdline.txt $R/boot/cmdline.txt
352 341
353 342 # Prepare modules-load.d directory
354 343 mkdir -p $R/lib/modules-load.d/
355 344
356 345 # Load random module on boot
357 346 if [ "$ENABLE_HWRANDOM" = true ] ; then
358 347 cat <<EOM >$R/lib/modules-load.d/rpi2.conf
359 348 bcm2708_rng
360 349 EOM
361 350 fi
362 351
363 352 # Prepare modprobe.d directory
364 353 mkdir -p $R/etc/modprobe.d/
365 354
366 355 # Blacklist sound modules
367 356 cat <<EOM >$R/etc/modprobe.d/raspi-blacklist.conf
368 357 blacklist snd_soc_core
369 358 blacklist snd_pcm
370 359 blacklist snd_pcm_dmaengine
371 360 blacklist snd_timer
372 361 blacklist snd_compress
373 362 blacklist snd_soc_pcm512x_i2c
374 363 blacklist snd_soc_pcm512x
375 364 blacklist snd_soc_tas5713
376 365 blacklist snd_soc_wm8804
377 366 EOM
378 367
379 368 # Create default fstab
380 369 cat <<EOM >$R/etc/fstab
381 370 /dev/mmcblk0p2 / ext4 noatime,nodiratime,errors=remount-ro,discard,data=writeback,commit=100 0 1
382 371 /dev/mmcblk0p1 /boot/firmware vfat defaults,noatime,nodiratime 0 2
383 372 EOM
384 373
385 374 # Avoid swapping and increase cache sizes
386 375 cat <<EOM >>$R/etc/sysctl.d/99-sysctl.conf
387 376
388 377 # Avoid swapping and increase cache sizes
389 378 vm.swappiness=1
390 379 vm.dirty_background_ratio=20
391 380 vm.dirty_ratio=40
392 381 vm.dirty_writeback_centisecs=500
393 382 vm.dirty_expire_centisecs=6000
394 383 EOM
395 384
396 385 # Enable network stack hardening
397 386 if [ "$ENABLE_HARDNET" = true ] ; then
398 387 cat <<EOM >>$R/etc/sysctl.d/99-sysctl.conf
399 388
400 389 # Enable network stack hardening
401 390 net.ipv4.tcp_timestamps=0
402 391 net.ipv4.tcp_syncookies=1
403 392 net.ipv4.conf.all.rp_filter=1
404 393 net.ipv4.conf.all.accept_redirects=0
405 394 net.ipv4.conf.all.send_redirects=0
406 395 net.ipv4.conf.all.accept_source_route=0
407 396 net.ipv4.conf.default.rp_filter=1
408 397 net.ipv4.conf.default.accept_redirects=0
409 398 net.ipv4.conf.default.send_redirects=0
410 399 net.ipv4.conf.default.accept_source_route=0
411 400 net.ipv4.conf.lo.accept_redirects=0
412 401 net.ipv4.conf.lo.send_redirects=0
413 402 net.ipv4.conf.lo.accept_source_route=0
414 403 net.ipv4.conf.eth0.accept_redirects=0
415 404 net.ipv4.conf.eth0.send_redirects=0
416 405 net.ipv4.conf.eth0.accept_source_route=0
417 406 net.ipv4.icmp_echo_ignore_broadcasts=1
418 407 net.ipv4.icmp_ignore_bogus_error_responses=1
419 408
420 409 net.ipv6.conf.all.accept_redirects=0
421 410 net.ipv6.conf.all.accept_source_route=0
422 411 net.ipv6.conf.all.router_solicitations=0
423 412 net.ipv6.conf.all.accept_ra_rtr_pref=0
424 413 net.ipv6.conf.all.accept_ra_pinfo=0
425 414 net.ipv6.conf.all.accept_ra_defrtr=0
426 415 net.ipv6.conf.all.autoconf=0
427 416 net.ipv6.conf.all.dad_transmits=0
428 417 net.ipv6.conf.all.max_addresses=1
429 418
430 419 net.ipv6.conf.default.accept_redirects=0
431 420 net.ipv6.conf.default.accept_source_route=0
432 421 net.ipv6.conf.default.router_solicitations=0
433 422 net.ipv6.conf.default.accept_ra_rtr_pref=0
434 423 net.ipv6.conf.default.accept_ra_pinfo=0
435 424 net.ipv6.conf.default.accept_ra_defrtr=0
436 425 net.ipv6.conf.default.autoconf=0
437 426 net.ipv6.conf.default.dad_transmits=0
438 427 net.ipv6.conf.default.max_addresses=1
439 428
440 429 net.ipv6.conf.lo.accept_redirects=0
441 430 net.ipv6.conf.lo.accept_source_route=0
442 431 net.ipv6.conf.lo.router_solicitations=0
443 432 net.ipv6.conf.lo.accept_ra_rtr_pref=0
444 433 net.ipv6.conf.lo.accept_ra_pinfo=0
445 434 net.ipv6.conf.lo.accept_ra_defrtr=0
446 435 net.ipv6.conf.lo.autoconf=0
447 436 net.ipv6.conf.lo.dad_transmits=0
448 437 net.ipv6.conf.lo.max_addresses=1
449 438
450 439 net.ipv6.conf.eth0.accept_redirects=0
451 440 net.ipv6.conf.eth0.accept_source_route=0
452 441 net.ipv6.conf.eth0.router_solicitations=0
453 442 net.ipv6.conf.eth0.accept_ra_rtr_pref=0
454 443 net.ipv6.conf.eth0.accept_ra_pinfo=0
455 444 net.ipv6.conf.eth0.accept_ra_defrtr=0
456 445 net.ipv6.conf.eth0.autoconf=0
457 446 net.ipv6.conf.eth0.dad_transmits=0
458 447 net.ipv6.conf.eth0.max_addresses=1
459 448 EOM
460 449
461 450 # Enable resolver warnings about spoofed addresses
462 451 cat <<EOM >>$R/etc/host.conf
463 452 spoof warn
464 453 EOM
465 454 fi
466 455
467 456 # Regenerate openssh server host keys
468 457 if [ "$ENABLE_SSHD" = true ] ; then
469 458 rm -fr $R/etc/ssh/ssh_host_*
470 459 LANG=C chroot $R dpkg-reconfigure openssh-server
471 460 fi
472 461
473 462 # Enable serial console systemd style
474 463 if [ "$ENABLE_CONSOLE" = true ] ; then
475 464 LANG=C chroot $R systemctl enable serial-getty\@ttyAMA0.service
476 465 fi
477 466
478 467 # Enable firewall based on iptables started by systemd service
479 468 if [ "$ENABLE_IPTABLES" = true ] ; then
480 469 # Create iptables configuration directory
481 470 mkdir -p "$R/etc/iptables"
482 471
483 472 # Create iptables systemd service
484 473 cat <<EOM >$R/etc/systemd/system/iptables.service
485 474 [Unit]
486 475 Description=Packet Filtering Framework
487 476 DefaultDependencies=no
488 477 After=systemd-sysctl.service
489 478 Before=sysinit.target
490 479 [Service]
491 480 Type=oneshot
492 481 ExecStart=/sbin/iptables-restore /etc/iptables/iptables.rules
493 482 ExecReload=/sbin/iptables-restore /etc/iptables/iptables.rules
494 483 ExecStop=/etc/iptables/flush-iptables.sh
495 484 RemainAfterExit=yes
496 485 [Install]
497 486 WantedBy=multi-user.target
498 487 EOM
499 488
500 489 # Create flush-table script called by iptables service
501 490 cat <<EOM >$R/etc/iptables/flush-iptables.sh
502 491 #!/bin/sh
503 492 iptables -F
504 493 iptables -X
505 494 iptables -t nat -F
506 495 iptables -t nat -X
507 496 iptables -t mangle -F
508 497 iptables -t mangle -X
509 498 iptables -P INPUT ACCEPT
510 499 iptables -P FORWARD ACCEPT
511 500 iptables -P OUTPUT ACCEPT
512 501 EOM
513 502
514 503 # Create iptables rule file
515 504 cat <<EOM >$R/etc/iptables/iptables.rules
516 505 *filter
517 506 :INPUT DROP [0:0]
518 507 :FORWARD DROP [0:0]
519 508 :OUTPUT ACCEPT [0:0]
520 509 :TCP - [0:0]
521 510 :UDP - [0:0]
522 511 :SSH - [0:0]
523 512
524 513 # Rate limit ping requests
525 514 -A INPUT -p icmp --icmp-type echo-request -m limit --limit 30/min --limit-burst 8 -j ACCEPT
526 515 -A INPUT -p icmp --icmp-type echo-request -j DROP
527 516
528 517 # Accept established connections
529 518 -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
530 519
531 520 # Accept all traffic on loopback interface
532 521 -A INPUT -i lo -j ACCEPT
533 522
534 523 # Drop packets declared invalid
535 524 -A INPUT -m conntrack --ctstate INVALID -j DROP
536 525
537 526 # SSH rate limiting
538 527 -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -j SSH
539 528 -A SSH -m recent --name sshbf --rttl --rcheck --hitcount 3 --seconds 10 -j DROP
540 529 -A SSH -m recent --name sshbf --rttl --rcheck --hitcount 20 --seconds 1800 -j DROP
541 530 -A SSH -m recent --name sshbf --set -j ACCEPT
542 531
543 532 # Send TCP and UDP connections to their respective rules chain
544 533 -A INPUT -p udp -m conntrack --ctstate NEW -j UDP
545 534 -A INPUT -p tcp --syn -m conntrack --ctstate NEW -j TCP
546 535
547 536 # Reject dropped packets with a RFC compliant responce
548 537 -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
549 538 -A INPUT -p tcp -j REJECT --reject-with tcp-rst
550 539 -A INPUT -j REJECT --reject-with icmp-proto-unreachable
551 540
552 541 ## TCP PORT RULES
553 542 # -A TCP -p tcp -j LOG
554 543
555 544 ## UDP PORT RULES
556 545 # -A UDP -p udp -j LOG
557 546
558 547 COMMIT
559 548 EOM
560 549
561 550 # Reload systemd configuration and enable iptables service
562 551 LANG=C chroot $R systemctl daemon-reload
563 552 LANG=C chroot $R systemctl enable iptables.service
564 553
565 554 if [ "$ENABLE_IPV6" = true ] ; then
566 555 # Create ip6tables systemd service
567 556 cat <<EOM >$R/etc/systemd/system/ip6tables.service
568 557 [Unit]
569 558 Description=Packet Filtering Framework
570 559 DefaultDependencies=no
571 560 After=systemd-sysctl.service
572 561 Before=sysinit.target
573 562 [Service]
574 563 Type=oneshot
575 564 ExecStart=/sbin/ip6tables-restore /etc/iptables/ip6tables.rules
576 565 ExecReload=/sbin/ip6tables-restore /etc/iptables/ip6tables.rules
577 566 ExecStop=/etc/iptables/flush-ip6tables.sh
578 567 RemainAfterExit=yes
579 568 [Install]
580 569 WantedBy=multi-user.target
581 570 EOM
582 571
583 572 # Create ip6tables file
584 573 cat <<EOM >$R/etc/iptables/flush-ip6tables.sh
585 574 #!/bin/sh
586 575 ip6tables -F
587 576 ip6tables -X
588 577 ip6tables -Z
589 578 for table in $(</proc/net/ip6_tables_names)
590 579 do
591 580 ip6tables -t \$table -F
592 581 ip6tables -t \$table -X
593 582 ip6tables -t \$table -Z
594 583 done
595 584 ip6tables -P INPUT ACCEPT
596 585 ip6tables -P OUTPUT ACCEPT
597 586 ip6tables -P FORWARD ACCEPT
598 587 EOM
599 588
600 589 # Create ip6tables rule file
601 590 cat <<EOM >$R/etc/iptables/ip6tables.rules
602 591 *filter
603 592 :INPUT DROP [0:0]
604 593 :FORWARD DROP [0:0]
605 594 :OUTPUT ACCEPT [0:0]
606 595 :TCP - [0:0]
607 596 :UDP - [0:0]
608 597 :SSH - [0:0]
609 598
610 599 # Drop packets with RH0 headers
611 600 -A INPUT -m rt --rt-type 0 -j DROP
612 601 -A OUTPUT -m rt --rt-type 0 -j DROP
613 602 -A FORWARD -m rt --rt-type 0 -j DROP
614 603
615 604 # Rate limit ping requests
616 605 -A INPUT -p icmpv6 --icmpv6-type echo-request -m limit --limit 30/min --limit-burst 8 -j ACCEPT
617 606 -A INPUT -p icmpv6 --icmpv6-type echo-request -j DROP
618 607
619 608 # Accept established connections
620 609 -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
621 610
622 611 # Accept all traffic on loopback interface
623 612 -A INPUT -i lo -j ACCEPT
624 613
625 614 # Drop packets declared invalid
626 615 -A INPUT -m conntrack --ctstate INVALID -j DROP
627 616
628 617 # SSH rate limiting
629 618 -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -j SSH
630 619 -A SSH -m recent --name sshbf --rttl --rcheck --hitcount 3 --seconds 10 -j DROP
631 620 -A SSH -m recent --name sshbf --rttl --rcheck --hitcount 20 --seconds 1800 -j DROP
632 621 -A SSH -m recent --name sshbf --set -j ACCEPT
633 622
634 623 # Send TCP and UDP connections to their respective rules chain
635 624 -A INPUT -p udp -m conntrack --ctstate NEW -j UDP
636 625 -A INPUT -p tcp --syn -m conntrack --ctstate NEW -j TCP
637 626
638 627 # Reject dropped packets with a RFC compliant responce
639 628 -A INPUT -p udp -j REJECT --reject-with icmp6-adm-prohibited
640 629 -A INPUT -p tcp -j REJECT --reject-with icmp6-adm-prohibited
641 630 -A INPUT -j REJECT --reject-with icmp6-adm-prohibited
642 631
643 632 ## TCP PORT RULES
644 633 # -A TCP -p tcp -j LOG
645 634
646 635 ## UDP PORT RULES
647 636 # -A UDP -p udp -j LOG
648 637
649 638 COMMIT
650 639 EOM
651 640
652 641 # Reload systemd configuration and enable iptables service
653 642 LANG=C chroot $R systemctl daemon-reload
654 643 LANG=C chroot $R systemctl enable ip6tables.service
655 644
656 645 fi
657 646 fi
658 647
659 648 if [ "$ENABLE_UBOOT" = true ] ; then
660 649 # Fetch u-boot github
661 650 git -C $R/tmp clone git://git.denx.de/u-boot.git
662 651
663 652 # Install minimal gcc/g++ build environment and build u-boot inside chroot
664 653 LANG=C chroot $R apt-get install -qq -y --force-yes --no-install-recommends linux-compiler-gcc-4.9-arm g++ make bc
665 654 LANG=C chroot $R make -C /tmp/u-boot/ rpi_2_defconfig all
666 655
667 656 # Copy compiled bootloader binary and set config.txt to load it
668 657 cp $R/tmp/u-boot/u-boot.bin $R/boot/firmware/
669 658 printf "\n# boot u-boot kernel\nkernel=u-boot.bin\n" >> $R/boot/firmware/config.txt
670 659
671 660 # Set u-boot command file
672 661 cat <<EOM >$R/boot/firmware/uboot.mkimage
673 662 # Tell Linux that it is booting on a Raspberry Pi2
674 663 setenv machid 0x00000c42
675 664
676 665 # Set the kernel boot command line
677 666 setenv bootargs "earlyprintk ${CMDLINE}"
678 667
679 668 # Save these changes to u-boot's environment
680 669 saveenv
681 670
682 671 # Load the existing Linux kernel into RAM
683 672 fatload mmc 0:1 \${kernel_addr_r} kernel7.img
684 673
685 674 # Boot the kernel we have just loaded
686 675 bootz \${kernel_addr_r}
687 676 EOM
688 677
689 678 # Generate u-boot image from command file
690 679 LANG=C chroot $R mkimage -A arm -O linux -T script -C none -a 0x00000000 -e 0x00000000 -n "RPi2 Boot Script" -d /boot/firmware/uboot.mkimage /boot/firmware/boot.scr
691 680
692 681 # Remove gcc/c++ build enviroment
693 682 LANG=C chroot $R apt-get purge -y bc binutils cpp cpp-4.9 g++ g++-4.9 gcc gcc-4.9 libasan1 libatomic1 libc-dev-bin libc6-dev libcloog-isl4 libgcc-4.9-dev libgomp1 libisl10 libmpc3 libmpfr4 libstdc++-4.9-dev libubsan0 linux-compiler-gcc-4.9-arm linux-libc-dev make
694 683 fi
695 684
696 685 # Clean cached downloads
697 686 LANG=C chroot $R apt-get -y clean
698 687 LANG=C chroot $R apt-get -y autoclean
699 688 LANG=C chroot $R apt-get -y autoremove
700 689
701 690 # Unmount mounted filesystems
702 691 umount -l $R/proc
703 692 umount -l $R/sys
704 693
705 694 # Clean up files
706 695 rm -f $R/etc/apt/sources.list.save
707 696 rm -f $R/etc/resolvconf/resolv.conf.d/original
708 697 rm -rf $R/run
709 698 mkdir -p $R/run
710 699 rm -f $R/etc/*-
711 700 rm -f $R/root/.bash_history
712 701 rm -rf $R/tmp/*
713 702 rm -f $R/var/lib/urandom/random-seed
714 703 [ -L $R/var/lib/dbus/machine-id ] || rm -f $R/var/lib/dbus/machine-id
715 704 rm -f $R/etc/machine-id
716 705 rm -fr $R/etc/apt/apt.conf.d/10proxy
717 706
718 707 # Calculate size of the chroot directory
719 708 CHROOT_SIZE=$(expr `du -s $R | awk '{ print $1 }'` / 1024)
720 709
721 710 # Calculate required image size
722 711 IMAGE_SIZE=`expr $(expr ${CHROOT_SIZE} / 1024 + 1) \* 1024`
723 712
724 713 # Calculate number of sectors for the partition
725 714 IMAGE_SECTORS=`expr $(expr ${IMAGE_SIZE} \* 1048576) / 512 - 133120`
726 715
727 716 # Prepare date string for image file name
728 717 DATE="$(date +%Y-%m-%d)"
729 718
730 719 # Prepare image file
731 720 dd if=/dev/zero of="$BASEDIR/${DATE}-debian-${RELEASE}.img" bs=1M count=1
732 721 dd if=/dev/zero of="$BASEDIR/${DATE}-debian-${RELEASE}.img" bs=1M count=0 seek=${IMAGE_SIZE}
733 722
734 723 # Write partition table
735 724 sfdisk -q -L -f "$BASEDIR/${DATE}-debian-${RELEASE}.img" <<EOM
736 725 unit: sectors
737 726
738 727 1 : start= 2048, size= 131072, Id= c, bootable
739 728 2 : start= 133120, size= ${IMAGE_SECTORS}, Id=83
740 729 3 : start= 0, size= 0, Id= 0
741 730 4 : start= 0, size= 0, Id= 0
742 731 EOM
743 732
744 733 # Set up temporary loop devices and build filesystems
745 734 VFAT_LOOP="$(losetup -o 1M --sizelimit 64M -f --show $BASEDIR/${DATE}-debian-${RELEASE}.img)"
746 735 EXT4_LOOP="$(losetup -o 65M --sizelimit `expr ${IMAGE_SIZE} - 64`M -f --show $BASEDIR/${DATE}-debian-${RELEASE}.img)"
747 736 mkfs.vfat "$VFAT_LOOP"
748 737 mkfs.ext4 "$EXT4_LOOP"
749 738
750 739 # Mount the temporary loop devices
751 740 mkdir -p "$BUILDDIR/mount"
752 741 mount "$EXT4_LOOP" "$BUILDDIR/mount"
753 742
754 743 mkdir -p "$BUILDDIR/mount/boot/firmware"
755 744 mount "$VFAT_LOOP" "$BUILDDIR/mount/boot/firmware"
756 745
757 746 # Copy all files from the chroot to the loop device mount point directory
758 747 rsync -a "$R/" "$BUILDDIR/mount/"
759 748
760 749 # Unmount all temporary loop devices and mount points
761 750 cleanup
762 751
763 752 # (optinal) create block map file for "bmaptool"
764 753 bmaptool create -o "$BASEDIR/${DATE}-debian-${RELEASE}.bmap" "$BASEDIR/${DATE}-debian-${RELEASE}.img"
765 754
766 755 # Image was successfully created
767 756 echo "$BASEDIR/${DATE}-debian-${RELEASE}.img (${IMAGE_SIZE})" ": successfully created"
General Comments 0
Vous devez vous connecter pour laisser un commentaire. Se connecter maintenant