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