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