revert chost, broke for localhost
[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 EOF
242
243
244
245
246 dnsmasq_restart=false
247 v cedit /etc/hosts <<EOF || dnsmasq_restart=true
248 192.168.1.1 wrt
249 192.168.1.2 treetowl mail.iankelling.org $HOME_DOMAIN
250 192.168.1.3 frodo faiserver
251 192.168.1.4 htpc
252 192.168.1.5 x2
253 192.168.1.6 demohost
254 #192.168.1.7 faiserver
255 192.168.1.8 tp
256 192.168.1.9 n5
257 72.14.176.105 li
258 45.33.9.11 lj
259 138.68.10.24 dopub
260 # netns creation looks for next free subnet starting at 10.173, but I only
261 # use one, and I would keep this one as the first created.
262 10.173.0.2 transmission
263 EOF
264
265
266 # avoid using the dns servers that my isp tells me about.
267 if [[ $(uci get dhcp.@dnsmasq[0].resolvfile) ]]; then
268 # default is '/tmp/resolv.conf.auto', we switch to the dnsmasq default of
269 # /etc/resolv.conf
270 v uci delete dhcp.@dnsmasq[0].resolvfile
271 uci commit dhcp
272 dnsmasq_restart=true
273 fi
274
275
276 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
277
278 # sometimes /mnt/usb fails, cuz it's just a flash drive,
279 # so make sure we have this dir or else dnsmasq will fail
280 # to start.
281 mkdir -p /mnt/usb/tftpboot
282 v cedit /etc/dnsmasq.conf <<'EOF' || dnsmasq_restart=true
283
284 ############ updating dns servers ###################3
285
286
287 # this says the ip of default gateway and dns server,
288 # but I think they are unneded and default
289 #dhcp-option=3,192.168.1.1
290 #dhcp-option=6,192.168.1.1
291
292
293
294 # results from googling around dnsmasq optimizations
295 # about 50k in memory. router has 62 megs.
296 # in a browsing session, I probably won't ever do 5000 lookups
297 # before the ttl expiration or whatever does expiration.
298 cache-size=10000
299
300 # ask all servers, use the one which responds first.
301 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
302 all-servers
303
304 # namebench benchmarks dns servers. google's dns was only
305 # slightly less fast than some others, and I trust it more
306 # to give accurate results, stay relatively fast, and
307 # not do anythin too malicious, so just use that.
308 # download namebench and run it like this:
309 # 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
310 # google
311 server=8.8.4.4
312 server=8.8.8.8
313 server=2001:4860:4860::8888
314 server=2001:4860:4860::8844
315
316
317 # to fixup existin ips, on the client you can do
318 # sudo dhclient -r; sudo dhclient <interface-name>
319
320 # default dhcp range is 100-150
321 dhcp-host=f4:6d:04:02:ed:66,set:treetowl,192.168.1.2,treetowl
322 dhcp-host=00:26:18:97:bb:16,set:frodo,192.168.1.3,frodo
323 dhcp-host=10:78:d2:da:29:22,set:htpc,192.168.1.4,htpc
324 dhcp-host=00:1f:16:16:39:24,set:x2,192.168.1.5,x2
325 # this is so fai can have an explicit name to use for testing,
326 # or else any random machine which did a pxe boot would get
327 # reformatted. The mac is from doing a virt-install, cancelling it,
328 # and copying the generated mac, so it should be randomish.
329 dhcp-host=52:54:00:9c:ef:ad,set:demohost,192.168.1.6,demohost
330 #dhcp-host=52:54:00:56:09:f9,set:faiserver,192.168.1.7,faiserver
331 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,192.168.1.8,tp
332 dhcp-host=c4:43:8f:f2:79:1f,set:n5,192.168.1.9,n5
333 # this is the ip it picks by default if dhcp fails,
334 # so might as well use it.
335 # hostname is the name it uses according to telnet
336 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,192.168.1.251,switch9429ca
337
338
339 # template
340 # dhcp-host=,192.168.1.,
341
342 # Just leave the tftp server up even if we aren't doing pxe boot.
343 # It has no sensitive info.
344 enable-tftp=br-lan
345 tftp-root=/mnt/usb/tftpboot
346 EOF
347
348 if $dnsmasq_restart; then
349 v /etc/init.d/dnsmasq restart
350 fi
351
352 if $firewall_restart; then
353 v /etc/init.d/firewall restart
354 fi