minor fixes
[automated-distro-installer] / wrt-setup
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 set -eE -o pipefail
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
20
21 # ssh
22
23 pmirror() {
24 # background: upgrading all packages is not recommended because it
25 # doesn't go into the firmware. build new firmware if you want
26 # lots of upgrades.
27 f=(/tmp/opkg-lists/*)
28 if ! (( $(date -r $f +%s) + 60*60*24 > $(date +%s) )); then
29 opkg update
30 fi
31 }
32
33 pi() {
34 for x in "$@"; do
35 if [[ ! $(opkg list-installed "$x") ]]; then
36 pmirror
37 opkg install "$@"
38 fi
39 done
40 }
41
42 v() {
43 printf "+ %s\n" "$*"
44 "$@"
45 }
46
47 cat >/usr/bin/arch-pxe-mount <<'EOFOUTER'
48 #!/bin/bash
49 # symlinks are collapsed for nfs mount points, so use a bind mount.
50 # tried putting this in /etc/config/fstab,
51 # then doig block mount, it didn't work. This doesn't persist across reboots,
52 # todo: figure that out
53 d=/run/archiso/bootmnt
54 cat > /etc/fstab <<EOF
55 /mnt/usb/tftpboot $d none bind 0 0
56 EOF
57 mount | grep $d &>/dev/null || mount $d
58 /etc/init.d/nfsd restart
59 EOFOUTER
60 chmod +x /usr/bin/arch-pxe-mount
61
62 cat >.profile <<'EOF'
63 # changing login shell emits spam on ssh single commands & scp
64 # sed -i 's#/bin/ash$#/bin/bash#' /etc/passwd
65 #https://dev.openwrt.org/ticket/13852
66 [ "$PS1" = "" ] || {
67 /bin/bash
68 exit
69 }
70 EOF
71 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
72 tcpdump openvpn-openssl
73
74
75
76 sed -ri "s/option[[:space:]]*encryption[[:space:]]*'?none'?/option encryption psk2\n option key pictionary49/" /etc/config/wireless
77 sed -i '/^[[:space:]]*option disabled/d' /etc/config/wireless
78 v wifi
79
80
81 v /etc/init.d/fstab enable ||:
82
83 # rebooting makes mounting work, but comparing lsmod,
84 # i'm guessing this will too. todo, test it.
85 # 255 == module already loaded
86 for mod in scsi_mod sd_mod; do v modprobe $mod || [[ $? == 255 ]]; done
87
88 # for arch pxe. The default settings in the installer expect to find
89 # the NFS at /run/archiso/bootmnt
90 mkdir -p /run/archiso/bootmnt
91
92 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
93 # that is the case after running this or rebooting.
94 # wiki says safe to do in case of fstab changes:
95 cedit /etc/config/fstab <<'EOF' || { v block umount; v block mount; }
96 config global automount
97 option from_fstab 1
98 option anon_mount 1
99
100 config global autoswap
101 option from_fstab 1
102 option anon_swap 1
103
104 config mount
105 option target /mnt/usb
106 option device /dev/sda2
107 option fstype ext4
108 option options rw,async,noatime,nodiratime
109 option enabled 1
110 option enabled_fsck 0
111
112 config swap
113 option device /dev/sda1
114 option enabled 1
115
116 EOF
117
118
119
120 # exportfs -ra wont cut it when its the same path, but now a bind mount
121 cedit /etc/exports <<'EOF' || v /etc/init.d/nfsd restart ||:
122 /mnt/usb 192.168.1.0/255.255.255.0(rw,no_root_squash,insecure,sync,no_subtree_check)
123 # for arch pxe
124 /run/archiso/bootmnt 192.168.1.0/255.255.255.0(rw,no_root_squash,insecure,sync,no_subtree_check)
125 EOF
126
127
128 v /etc/init.d/portmap start
129 v /etc/init.d/nfsd start
130 v /etc/init.d/portmap enable
131 v /etc/init.d/nfsd enable
132
133
134
135
136
137
138 ######### uci example:#######
139 # # https://wiki.openwrt.org/doc/uci
140 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
141 # wan="firewall.@zone[$wan_index]"
142 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
143 # # default is wan
144 # v uci set firewall.@forwarding[0].dest=$forward_dest
145 # uci commit firewall
146 # firewall_restart=true
147 # fi
148
149
150
151 ########## openvpn exampl
152 ########## missing firewall settings for routing lan
153 ########## traffic
154 # v /etc/init.d/openvpn start
155 # v /etc/init.d/openvpn enable
156
157 # # from https://wiki.openwrt.org/doc/uci/firewall
158 # # todo: not sure if /etc/init.d/network needs restarting.
159 # # I did, and I had to restart the vpn afterwards.
160 # # This maps a uci interface to a real interface which is
161 # # managed outside of uci.
162 # v cedit /etc/config/network <<'EOF' ||:
163 # config interface 'tun0'
164 # option ifname 'tun0'
165 # option proto 'none'
166 # EOF
167 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
168 # config openvpn my_client_config
169 # option enabled 1
170 # option config /etc/openvpn/client.conf
171 # EOF
172
173
174 v cedit /etc/config/network <<'EOF' || v /etc/init.d/network reload
175 config 'route' 'transmission'
176 option 'interface' 'lan'
177 option 'target' '10.173.0.0'
178 option 'netmask' '255.255.0.0'
179 option 'gateway' '192.168.1.2'
180 EOF
181
182 v cedit /etc/config/firewall <<'EOF' || firewall_restart=true
183 config redirect
184 option name ssh
185 option src wan
186 option src_dport 22
187 option dest_ip 192.168.1.2
188 option dest lan
189 config rule
190 option src wan
191 option target ACCEPT
192 option dest_port 22
193
194 config redirect
195 option name sshalt
196 option src wan
197 option src_dport 2222
198 option dest_port 22
199 option dest_ip 192.168.1.3
200 option dest lan
201 config rule
202 option src wan
203 option target ACCEPT
204 option dest_port 2222
205
206 config redirect
207 option src wan
208 option src_dport 443
209 option dest lan
210 option dest_ip 192.168.1.2
211 option proto tcp
212 config rule
213 option src wan
214 option target ACCEPT
215 option dest_port 443
216 option proto tcp
217
218 config redirect
219 option src wan
220 option src_dport 80
221 option dest lan
222 option dest_ip 192.168.1.2
223 option proto tcp
224 config rule
225 option src wan
226 option target ACCEPT
227 option dest_port 80
228 option proto tcp
229
230 config redirect
231 option name syncthing
232 option src wan
233 option src_dport 22001
234 option dest_ip 192.168.1.2
235 option dest lan
236 config rule
237 option src wan
238 option target ACCEPT
239 option dest_port 22001
240
241 #### begin rules for nfs ####
242 # https://serverfault.com/questions/377170/which-ports-do-i-need-to-open-in-the-firewall-to-use-nfs
243 # https://wiki.debian.org/SecuringNFS
244 # I had no /etc/default/quota, or any process named quota anything,
245 # so, assumed that was unneeded. seems to work.
246 config redirect
247 option src wan
248 option src_dport 111
249 option dest_ip 192.168.1.2
250 option dest lan
251 config rule
252 option src wan
253 option target ACCEPT
254 option dest_port 111
255 config redirect
256 option src wan
257 option src_dport 2049
258 option dest_ip 192.168.1.2
259 option dest lan
260 config rule
261 option src wan
262 option target ACCEPT
263 option dest_port 2049
264 config redirect
265 option src wan
266 option src_dport 32764
267 option dest_ip 192.168.1.2
268 option dest lan
269 config rule
270 option src wan
271 option target ACCEPT
272 option dest_port 32764
273 config redirect
274 option src wan
275 option src_dport 32765
276 option dest_ip 192.168.1.2
277 option dest lan
278 config rule
279 option src wan
280 option target ACCEPT
281 option dest_port 32765
282 config redirect
283 option src wan
284 option src_dport 32766
285 option dest_ip 192.168.1.2
286 option dest lan
287 config rule
288 option src wan
289 option target ACCEPT
290 option dest_port 32766
291 config redirect
292 option src wan
293 option src_dport 32767
294 option dest_ip 192.168.1.2
295 option dest lan
296 config rule
297 option src wan
298 option target ACCEPT
299 option dest_port 32767
300 config redirect
301 option src wan
302 option src_dport 32768
303 option dest_ip 192.168.1.2
304 option dest lan
305 config rule
306 option src wan
307 option target ACCEPT
308 option dest_port 32768
309 #### end rules for nfs ####
310
311
312 config redirect
313 option name mariadb
314 option src wan
315 option src_dport 3306
316 option dest lan
317 option dest_ip 192.168.1.2
318 option proto tcp
319 config rule
320 option src wan
321 option target ACCEPT
322 option dest_port 3306
323 option proto tcp
324
325
326 EOF
327
328
329
330
331 dnsmasq_restart=false
332 v cedit /etc/hosts <<EOF || dnsmasq_restart=true
333 192.168.1.1 wrt
334 192.168.1.2 treetowl $HOME_DOMAIN faiserver
335 192.168.1.3 frodo
336 192.168.1.4 htpc
337 192.168.1.5 x2
338 192.168.1.6 demohost
339 #192.168.1.7 faiserver
340 192.168.1.8 tp
341 192.168.1.9 n5
342 72.14.176.105 li
343 45.33.9.11 lj
344 138.68.10.24 dopub
345 # netns creation looks for next free subnet starting at 10.173, but I only
346 # use one, and I would keep this one as the first created.
347 10.173.0.2 transmission
348 EOF
349
350
351 # avoid using the dns servers that my isp tells me about.
352 if [[ $(uci get dhcp.@dnsmasq[0].resolvfile) ]]; then
353 # default is '/tmp/resolv.conf.auto', we switch to the dnsmasq default of
354 # /etc/resolv.conf
355 v uci delete dhcp.@dnsmasq[0].resolvfile
356 uci commit dhcp
357 dnsmasq_restart=true
358 fi
359
360
361 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
362
363 # sometimes /mnt/usb fails, cuz it's just a flash drive,
364 # so make sure we have this dir or else dnsmasq will fail
365 # to start.
366 mkdir -p /mnt/usb/tftpboot
367 v cedit /etc/dnsmasq.conf <<'EOF' || dnsmasq_restart=true
368
369 ############ updating dns servers ###################3
370
371
372 # this says the ip of default gateway and dns server,
373 # but I think they are unneded and default
374 #dhcp-option=3,192.168.1.1
375 #dhcp-option=6,192.168.1.1
376
377
378
379 # results from googling around dnsmasq optimizations
380 # about 50k in memory. router has 62 megs.
381 # in a browsing session, I probably won't ever do 5000 lookups
382 # before the ttl expiration or whatever does expiration.
383 cache-size=10000
384
385 # ask all servers, use the one which responds first.
386 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
387 all-servers
388
389 # namebench benchmarks dns servers. google's dns was only
390 # slightly less fast than some others, and I trust it more
391 # to give accurate results, stay relatively fast, and
392 # not do anythin too malicious, so just use that.
393 # download namebench and run it like this:
394 # for x in all regional isp global preferred nearby; do ./namebench.py -s $x -c US -i firefox -m weighted -J 10 -w; echo $x; hr; done
395 # google
396 server=8.8.4.4
397 server=8.8.8.8
398 server=2001:4860:4860::8888
399 server=2001:4860:4860::8844
400
401
402 # to fixup existin ips, on the client you can do
403 # sudo dhclient -r; sudo dhclient <interface-name>
404
405 # default dhcp range is 100-150
406 dhcp-host=f4:6d:04:02:ed:66,set:treetowl,192.168.1.2,treetowl
407 dhcp-host=00:26:18:97:bb:16,set:frodo,192.168.1.3,frodo
408 dhcp-host=10:78:d2:da:29:22,set:htpc,192.168.1.4,htpc
409 dhcp-host=00:1f:16:16:39:24,set:x2,192.168.1.5,x2
410 # this is so fai can have an explicit name to use for testing,
411 # or else any random machine which did a pxe boot would get
412 # reformatted. The mac is from doing a virt-install, cancelling it,
413 # and copying the generated mac, so it should be randomish.
414 dhcp-host=52:54:00:9c:ef:ad,set:demohost,192.168.1.6,demohost
415 #dhcp-host=52:54:00:56:09:f9,set:faiserver,192.168.1.7,faiserver
416 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,192.168.1.8,tp
417 dhcp-host=c4:43:8f:f2:79:1f,set:n5,192.168.1.9,n5
418 # this is the ip it picks by default if dhcp fails,
419 # so might as well use it.
420 # hostname is the name it uses according to telnet
421 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,192.168.1.251,switch9429ca
422
423
424 # template
425 # dhcp-host=,192.168.1.,
426
427 # Just leave the tftp server up even if we aren't doing pxe boot.
428 # It has no sensitive info.
429 enable-tftp=br-lan
430 tftp-root=/mnt/usb/tftpboot
431 EOF
432
433 if $dnsmasq_restart; then
434 v /etc/init.d/dnsmasq restart
435 fi
436
437 if $firewall_restart; then
438 v /etc/init.d/firewall restart
439 fi