dirinstall fixes/improvements, other minor changes
[automated-distro-installer] / wrt-setup-local
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
22 pmirror() {
23 # background: upgrading all packages is not recommended because it
24 # doesn't go into the firmware. build new firmware if you want
25 # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
26 f=(/tmp/opkg-lists/* /var/opkg-lists/*)
27 if ! (( $(date -r $f +%s) + 60*60*24 > $(date +%s) )); then
28 opkg update
29 fi
30 }
31
32 pi() {
33 for x in "$@"; do
34 if [[ ! $(opkg list-installed "$x") ]]; then
35 pmirror
36 opkg install "$@"
37 fi
38 done
39 }
40
41 v() {
42 printf "+ %s\n" "$*"
43 "$@"
44 }
45
46 ### network config
47 ###
48 l=10.0.0
49 lan=10.0.0.0
50 mask=255.255.0.0
51
52 cat >/usr/bin/arch-pxe-mount <<'EOFOUTER'
53 #!/bin/bash
54 # symlinks are collapsed for nfs mount points, so use a bind mount.
55 # tried putting this in /etc/config/fstab,
56 # then doig block mount, it didn't work. This doesn't persist across reboots,
57 # todo: figure that out
58 d=/run/archiso/bootmnt
59 cat > /etc/fstab <<EOF
60 /mnt/usb/tftpboot $d none bind 0 0
61 EOF
62 mount | grep $d &>/dev/null || mount $d
63 /etc/init.d/nfsd restart
64 EOFOUTER
65 chmod +x /usr/bin/arch-pxe-mount
66
67 cat >.profile <<'EOF'
68 # changing login shell emits spam on ssh single commands & scp
69 # sed -i 's#/bin/ash$#/bin/bash#' /etc/passwd
70 # https://github.com/openwrt/packages/issues/6137
71 [ "$BASH_VERSION" != "" ] || exec /bin/bash -i
72 EOF
73 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
74 tcpdump openvpn-openssl adblock
75
76
77
78 sed -ri "s/option[[:space:]]+encryption[[:space:]].*/option encryption psk2/;s/option[[:space:]]+key.*/option key pictionary49/" /etc/config/wireless
79 sed -i '/^[[:space:]]*option disabled/d' /etc/config/wireless
80 v wifi
81
82
83 v /etc/init.d/fstab enable ||:
84
85 # rebooting makes mounting work, but comparing lsmod,
86 # i'm guessing this will too. todo, test it.
87 # 255 == module already loaded
88 for mod in scsi_mod sd_mod; do v modprobe $mod || [[ $? == 255 ]]; done
89
90 # for arch pxe. The default settings in the installer expect to find
91 # the NFS at /run/archiso/bootmnt
92 mkdir -p /run/archiso/bootmnt
93
94 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
95 # that is the case after running this or rebooting.
96 # wiki says safe to do in case of fstab changes:
97
98 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
99 echo | cedit /etc/config/fstab ||:
100 cedit /etc/config/fstab <<'EOF' || { v block umount; v block mount; }
101 config global automount
102 option from_fstab 1
103 option anon_mount 1
104
105 config mount
106 option target /mnt/usb
107 option device /dev/sda1
108 option fstype ext4
109 option options rw,async,noatime,nodiratime
110 option enabled 1
111 EOF
112
113
114 # ian: disabled because afaik I don't need it, no benefit.
115 # config global autoswap
116 # option from_fstab 1
117 # option anon_swap 1
118
119 # config swap
120 # option device /dev/sda1
121 # option enabled 1
122
123
124
125
126 # exportfs -ra wont cut it when its the same path, but now a bind mount
127 cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
128 /mnt/usb $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
129 # for arch pxe
130 /run/archiso/bootmnt $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
131 EOF
132
133
134 v /etc/init.d/portmap start
135 v /etc/init.d/nfsd start
136 v /etc/init.d/portmap enable
137 v /etc/init.d/nfsd enable
138
139
140
141
142
143
144 ######### uci example:#######
145 # # https://wiki.openwrt.org/doc/uci
146 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
147 # wan="firewall.@zone[$wan_index]"
148 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
149 # # default is wan
150 # v uci set firewall.@forwarding[0].dest=$forward_dest
151 # uci commit firewall
152 # firewall_restart=true
153 # fi
154
155
156
157 ########## openvpn exampl
158 ########## missing firewall settings for routing lan
159 ########## traffic
160 # v /etc/init.d/openvpn start
161 # v /etc/init.d/openvpn enable
162
163 # # from https://wiki.openwrt.org/doc/uci/firewall
164 # # todo: not sure if /etc/init.d/network needs restarting.
165 # # I did, and I had to restart the vpn afterwards.
166 # # This maps a uci interface to a real interface which is
167 # # managed outside of uci.
168 # v cedit /etc/config/network <<'EOF' ||:
169 # config interface 'tun0'
170 # option ifname 'tun0'
171 # option proto 'none'
172 # EOF
173 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
174 # config openvpn my_client_config
175 # option enabled 1
176 # option config /etc/openvpn/client.conf
177 # EOF
178
179
180 v cedit /etc/config/network <<EOF || v /etc/init.d/network reload
181 config 'route' 'transmission'
182 option 'interface' 'lan'
183 option 'target' '10.173.0.0'
184 option 'netmask' '255.255.0.0'
185 option 'gateway' '$l.3'
186 EOF
187
188 v cedit /etc/config/firewall <<EOF || firewall_restart=true
189 config redirect
190 option name ssh
191 option src wan
192 option src_dport 22
193 option dest_ip $l.8
194 option dest lan
195 config rule
196 option src wan
197 option target ACCEPT
198 option dest_port 22
199
200 config redirect
201 option name sshalt
202 option src wan
203 option src_dport 2222
204 option dest_port 22
205 option dest_ip $l.3
206 option dest lan
207 config rule
208 option src wan
209 option target ACCEPT
210 option dest_port 2222
211
212 config redirect
213 option src wan
214 option src_dport 443
215 option dest lan
216 option dest_ip $l.8
217 option proto tcp
218 config rule
219 option src wan
220 option target ACCEPT
221 option dest_port 443
222 option proto tcp
223
224 config redirect
225 option src wan
226 option src_dport 1196
227 option dest lan
228 option dest_ip $l.8
229 option proto udp
230 config rule
231 option src wan
232 option target ACCEPT
233 option dest_port 1196
234 option proto udp
235
236
237 config redirect
238 option src wan
239 option src_dport 80
240 option dest lan
241 option dest_ip $l.8
242 option proto tcp
243 config rule
244 option src wan
245 option target ACCEPT
246 option dest_port 80
247 option proto tcp
248
249 config redirect
250 option name syncthing
251 option src wan
252 option src_dport 22001
253 option dest_ip $l.8
254 option dest lan
255 config rule
256 option src wan
257 option target ACCEPT
258 option dest_port 22001
259
260
261
262 EOF
263
264
265
266
267 dnsmasq_restart=false
268 mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
269 v cedit /etc/hosts <<EOF || dnsmasq_restart=true
270 127.0.1.1 wrt
271 $l.1 wrt
272 $l.2 kd
273 $l.3 frodo
274 $l.4 htpc
275 $l.5 x2
276 $l.6 demohost
277 #$l.7 faiserver
278 $l.8 tp faiserver b8.nz
279 $l.9 n5
280 $l.10 so
281 $l.12 fz
282 72.14.176.105 li
283 45.33.9.11 lj
284 138.68.10.24 dopub
285 # netns creation looks for next free subnet starting at 10.173, but I only
286 # use one, and I would keep this one as the first created.
287 10.173.0.2 transmission
288 EOF
289
290 # if [[ $mail_host ]]; then
291 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
292 # fi
293
294
295 # avoid using the dns servers that my isp tells me about.
296 if [[ $(uci get dhcp.@dnsmasq[0].resolvfile 2>/dev/null) ]]; then
297 # default is '/tmp/resolv.conf.auto', we switch to the dnsmasq default of
298 # /etc/resolv.conf. not sure why I did this.
299 v uci delete dhcp.@dnsmasq[0].resolvfile
300 uci commit dhcp
301 dnsmasq_restart=true
302 fi
303
304 if [[ $(uci get dhcp.@dnsmasq[0].domain) != b8.nz ]]; then
305 v uci set dhcp.@dnsmasq[0].domain=b8.nz
306 uci commit dhcp
307 dnsmasq_restart=true
308 fi
309 if [[ $(uci get dhcp.@dnsmasq[0].local) != b8.nz ]]; then
310 v uci set dhcp.@dnsmasq[0].local=/b8.nz/
311 uci commit dhcp
312 dnsmasq_restart=true
313 fi
314
315 if [[ $(uci get system.@system[0].hostname) != wrt ]]; then
316 v uci set system.@system[0].hostname=wrt
317 uci commit system
318 fi
319
320
321 if [[ $(uci get adblock.global.adb_enabled) != 1 ]]; then
322 v uci set adblock.global.adb_enabled=1
323 uci commit adblock
324 /etc/init.d/adblock restart
325 fi
326 # https://github.com/openwrt/packages/tree/master/net/adblock/files
327 cat >/etc/crontabs/root <<'EOF'
328 0 06 * * * /etc/init.d/adblock reload
329 EOF
330
331
332 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
333
334 # sometimes /mnt/usb fails, cuz it's just a flash drive,
335 # so make sure we have this dir or else dnsmasq will fail
336 # to start.
337 mkdir -p /mnt/usb/tftpboot
338 v cedit /etc/dnsmasq.conf <<EOF || dnsmasq_restart=true
339
340 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
341 stop-dns-rebind
342
343 # this says the ip of default gateway and dns server,
344 # but I think they are unneded and default
345 #dhcp-option=3,$l.1
346 #dhcp-option=6,$l.1
347
348
349
350 # results from googling around dnsmasq optimizations
351 # about 50k in memory. router has 62 megs.
352 # in a browsing session, I probably won't ever do 5000 lookups
353 # before the ttl expiration or whatever does expiration.
354 cache-size=10000
355
356 # ask all servers, use the one which responds first.
357 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
358 all-servers
359
360 # namebench benchmarks dns servers. google's dns was only
361 # slightly less fast than some others, and I trust it more
362 # to give accurate results, stay relatively fast, and
363 # not do anythin too malicious, so just use that.
364 # download namebench and run it like this:
365 # 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
366 # google
367 server=8.8.4.4
368 server=8.8.8.8
369 server=2001:4860:4860::8888
370 server=2001:4860:4860::8844
371
372
373 # to fixup existin ips, on the client you can do
374 # sudo dhclient -r; sudo dhclient <interface-name>
375
376 # default dhcp range is 100-150
377 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
378 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
379 # top port, iPXE (PCI 04:00.0) in seabios boot menu
380 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
381 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
382 dhcp-host=10:78:d2:da:29:22,set:htpc,$l.4,htpc
383 dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
384 #dhcp-host=00:c0:ca:27:e9:b2,set:x2w,$l.11,x2w
385 #wireless interface
386 # this is so fai can have an explicit name to use for testing,
387 # or else any random machine which did a pxe boot would get
388 # reformatted. The mac is from doing a virt-install, cancelling it,
389 # and copying the generated mac, so it should be randomish.
390 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.6,demohost
391 #dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.7,faiserver
392 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
393 # this is the ip it picks by default if dhcp fails,
394 # so might as well use it.
395 # hostname is the name it uses according to telnet
396 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
397
398 # template
399 # dhcp-host=,$l.,
400
401 # Just leave the tftp server up even if we aren't doing pxe boot.
402 # It has no sensitive info.
403 enable-tftp=br-lan
404 tftp-root=/mnt/usb/tftpboot
405 EOF
406
407 if $dnsmasq_restart; then
408 v /etc/init.d/dnsmasq restart
409 fi
410
411 if $firewall_restart; then
412 v /etc/init.d/firewall restart
413 fi