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