2 # Copyright (C) 2016 Ian Kelling
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.
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.
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.
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
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
34 if [[ ! $
(opkg list-installed
"$x") ]]; then
52 cat >/usr
/bin
/arch-pxe-mount
<<'EOFOUTER'
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
62 mount | grep $d &>/dev/null || mount $d
63 /etc/init.d/nfsd restart
65 chmod +x
/usr
/bin
/arch-pxe-mount
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
73 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
74 tcpdump openvpn-openssl adblock
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
83 v
/etc
/init.d
/fstab
enable ||
:
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
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
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:
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
106 option target /mnt/usb
107 option device /dev/sda1
109 option options rw,async,noatime,nodiratime
114 # ian: disabled because afaik I don't need it, no benefit.
115 # config global autoswap
116 # option from_fstab 1
120 # option device /dev/sda1
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)
130 /run/archiso/bootmnt $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
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
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
150 # v uci set firewall.@forwarding[0].dest=$forward_dest
151 # uci commit firewall
152 # firewall_restart=true
157 ########## openvpn exampl
158 ########## missing firewall settings for routing lan
160 # v /etc/init.d/openvpn start
161 # v /etc/init.d/openvpn enable
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'
173 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
174 # config openvpn my_client_config
176 # option config /etc/openvpn/client.conf
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'
188 v cedit
/etc
/config
/firewall
<<EOF || firewall_restart=true
203 option src_dport 2222
210 option dest_port 2222
226 option src_dport 1196
233 option dest_port 1196
250 option name syncthing
252 option src_dport 22001
258 option dest_port 22001
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
278 $l.8 tp faiserver b8.nz
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
290 # if [[ $mail_host ]]; then
291 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
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
304 if [[ $
(uci get dhcp.@dnsmasq
[0].domain
) != b8.nz
]]; then
305 v uci
set dhcp.@dnsmasq
[0].domain
=b8.nz
309 if [[ $
(uci get dhcp.@dnsmasq
[0].
local) != b8.nz
]]; then
310 v uci
set dhcp.@dnsmasq
[0].
local=/b8.nz
/
315 if [[ $
(uci get system.@system
[0].hostname
) != wrt
]]; then
316 v uci
set system.@system
[0].hostname
=wrt
321 if [[ $
(uci get adblock.global.adb_enabled
) != 1 ]]; then
322 v uci
set adblock.global.adb_enabled
=1
324 /etc
/init.d
/adblock restart
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
332 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
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
337 mkdir
-p /mnt
/usb
/tftpboot
338 v cedit
/etc
/dnsmasq.conf
<<EOF || dnsmasq_restart=true
340 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
343 # this says the ip of default gateway and dns server,
344 # but I think they are unneded and default
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.
356 # ask all servers, use the one which responds first.
357 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
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
369 server=2001:4860:4860::8888
370 server=2001:4860:4860::8844
373 # to fixup existin ips, on the client you can do
374 # sudo dhclient -r; sudo dhclient <interface-name>
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
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
401 # Just leave the tftp server up even if we aren't doing pxe boot.
402 # It has no sensitive info.
404 tftp-root=/mnt/usb/tftpboot
407 if $dnsmasq_restart; then
408 v
/etc
/init.d
/dnsmasq restart
411 if $firewall_restart; then
412 v
/etc
/init.d
/firewall restart