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