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
22 # background: upgrading all packages is not recommended because it
23 # doesn't go into the firmware. build new firmware if you want
24 # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
26 if ! (( $
(date -r $f +%s
) + 60*60*24 > $
(date +%s
) )); then
33 if [[ ! $
(opkg list-installed
"$x") ]]; then
47 ssid
="check out gnu.org"
52 passwd
-l root ||
: #already locked fails
54 cat >/usr
/bin
/archlike-pxe-mount
<<'EOFOUTER'
56 # symlinks are collapsed for nfs mount points, so use a bind mount.
57 # tried putting this in /etc/config/fstab,
58 # then doing block mount, it didn't work. This doesn't persist across reboots,
59 # todo: figure that out
61 for d in /run/{arch,parabola}iso/bootmnt; do
62 cat >>/etc/fstab <<EOF
63 /mnt/usb/tftpboot $d none bind 0 0
65 mount | grep $d &>/dev/null || mount $d
67 /etc/init.d/nfsd restart
69 chmod +x
/usr
/bin
/archlike-pxe-mount
72 # changing login shell emits spam on ssh single commands & scp
73 # sed -i 's#/bin/ash$#/bin/bash#' /etc/passwd
74 # https://github.com/openwrt/packages/issues/6137
75 [ "$BASH_VERSION" != "" ] || exec /bin/bash -i
77 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
78 tcpdump openvpn-openssl adblock
84 v
/etc
/init.d
/fstab
enable ||
:
86 # rebooting makes mounting work, but comparing lsmod,
87 # i'm guessing this will too. todo, test it.
88 # 255 == module already loaded
89 for mod
in scsi_mod sd_mod
; do v modprobe
$mod ||
[[ $?
== 255 ]]; done
91 # for archlike pxe. The default settings in the installer expect to find
92 # the NFS at one of these dirs
93 mkdir
-p /run
/archiso
/bootmnt
94 mkdir
-p /run
/parabolaiso
/bootmnt
96 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
97 # that is the case after running this or rebooting.
98 # wiki says safe to do in case of fstab changes:
100 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
101 echo | cedit
/etc
/config
/fstab ||
:
102 cedit
/etc
/config
/fstab
<<'EOF' || { v block umount; v block mount; }
103 config global automount
108 option target /mnt/usb
109 option device /dev/sda1
111 option options rw,async,noatime,nodiratime
116 # ian: disabled because afaik I don't need it, no benefit.
117 # config global autoswap
118 # option from_fstab 1
122 # option device /dev/sda1
128 # exportfs -ra wont cut it when its the same path, but now a bind mount
129 cedit
/etc
/exports
<<EOF || v /etc/init.d/nfsd restart ||:
130 /mnt/usb $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
132 /run/archiso/bootmnt $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
133 /run/parabolaiso/bootmnt $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
137 v
/etc
/init.d
/portmap start
138 v
/etc
/init.d
/nfsd start
139 v
/etc
/init.d
/portmap
enable
140 v
/etc
/init.d
/nfsd
enable
147 ######### uci example:#######
148 # # https://wiki.openwrt.org/doc/uci
149 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
150 # wan="firewall.@zone[$wan_index]"
151 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
153 # v uci set firewall.@forwarding[0].dest=$forward_dest
154 # uci commit firewall
155 # firewall_restart=true
159 wireless_restart
=false
162 if [[ $
(uci get wireless.default_radio
$x.ssid
) != "$ssid" ]]; then
163 v uci
set wireless.default_radio
$x.ssid
="$ssid"
164 wireless_restart
=true
166 if [[ $
(uci get wireless.default_radio
$x.key
) != $key ]]; then
167 v uci
set wireless.default_radio
$x.key
=$key
168 wireless_restart
=true
170 if [[ $
(uci get wireless.default_radio
$x.encryption
) != psk2
]]; then
171 v uci
set wireless.default_radio
$x.encryption
=psk2
172 wireless_restart
=true
174 if [[ $
(uci get wireless.default_radio
$x.disabled
2>/dev
/null
) ]]; then
175 v uci delete wireless.default_radio
$x.disabled
176 wireless_restart
=true
180 if $wireless_restart; then
186 ########## openvpn exampl
187 ########## missing firewall settings for routing lan
189 # v /etc/init.d/openvpn start
190 # v /etc/init.d/openvpn enable
192 # # from https://wiki.openwrt.org/doc/uci/firewall
193 # # todo: not sure if /etc/init.d/network needs restarting.
194 # # I did, and I had to restart the vpn afterwards.
195 # # This maps a uci interface to a real interface which is
196 # # managed outside of uci.
197 # v cedit /etc/config/network <<'EOF' ||:
198 # config interface 'tun0'
199 # option ifname 'tun0'
200 # option proto 'none'
202 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
203 # config openvpn my_client_config
205 # option config /etc/openvpn/client.conf
210 v cedit
/etc
/config
/network
<<EOF || v /etc/init.d/network reload
211 config 'route' 'transmission'
212 option 'interface' 'lan'
213 option 'target' '10.173.0.0'
214 option 'netmask' '255.255.0.0'
215 option 'gateway' '$l.3'
218 firewall_restart
=false
219 v cedit
/etc
/config
/firewall
<<EOF || firewall_restart=true
234 option src_dport 2222
241 option dest_port 2222
257 option src_dport 1196
264 option dest_port 1196
281 option name syncthing
283 option src_dport 22001
289 option dest_port 22001
298 dnsmasq_restart
=false
299 mail_host
=$
(grep -F mail.iankelling.org
/etc
/hosts |
awk '{print $1}')
300 v cedit
/etc
/hosts
<<EOF || dnsmasq_restart=true
309 $l.8 tp faiserver b8.nz
316 # netns creation looks for next free subnet starting at 10.173, but I only
317 # use one, and I would keep this one as the first created.
318 10.173.0.2 transmission
321 # if [[ $mail_host ]]; then
322 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
326 # avoid using the dns servers that my isp tells me about.
327 if [[ $
(uci get dhcp.@dnsmasq
[0].resolvfile
2>/dev
/null
) ]]; then
328 # default is '/tmp/resolv.conf.auto', we switch to the dnsmasq default of
329 # /etc/resolv.conf. not sure why I did this.
330 v uci delete dhcp.@dnsmasq
[0].resolvfile
335 if [[ $
(uci get dhcp.@dnsmasq
[0].domain
) != b8.nz
]]; then
336 v uci
set dhcp.@dnsmasq
[0].domain
=b8.nz
340 if [[ $
(uci get dhcp.@dnsmasq
[0].
local) != /b8.nz
/ ]]; then
341 v uci
set dhcp.@dnsmasq
[0].
local=/b8.nz
/
346 if [[ $
(uci get system.@system
[0].hostname
) != wrt
]]; then
347 v uci
set system.@system
[0].hostname
=wrt
352 if [[ $
(uci get adblock.global.adb_enabled
) != 1 ]]; then
353 v uci
set adblock.global.adb_enabled
=1
355 /etc
/init.d
/adblock restart
357 # https://github.com/openwrt/packages/tree/master/net/adblock/files
358 cat >/etc
/crontabs
/root
<<'EOF'
359 0 06 * * * /etc/init.d/adblock reload
363 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
365 # sometimes /mnt/usb fails, cuz it's just a flash drive,
366 # so make sure we have this dir or else dnsmasq will fail
368 mkdir
-p /mnt
/usb
/tftpboot
369 v cedit
/etc
/dnsmasq.conf
<<EOF || dnsmasq_restart=true
371 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
374 # this says the ip of default gateway and dns server,
375 # but I think they are unneded and default
381 # results from googling around dnsmasq optimizations
382 # about 50k in memory. router has 62 megs.
383 # in a browsing session, I probably won't ever do 5000 lookups
384 # before the ttl expiration or whatever does expiration.
387 # ask all servers, use the one which responds first.
388 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
391 # namebench benchmarks dns servers. google's dns was only
392 # slightly less fast than some others, and I trust it more
393 # to give accurate results, stay relatively fast, and
394 # not do anythin too malicious, so just use that.
395 # download namebench and run it like this:
396 # 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
400 server=2001:4860:4860::8888
401 server=2001:4860:4860::8844
404 # to fixup existin ips, on the client you can do
405 # sudo dhclient -r; sudo dhclient <interface-name>
407 # default dhcp range is 100-150
408 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
409 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
410 # top port, iPXE (PCI 04:00.0) in seabios boot menu
411 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
412 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
413 dhcp-host=10:78:d2:da:29:22,set:htpc,$l.4,htpc
414 dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
415 #dhcp-host=00:c0:ca:27:e9:b2,set:x2w,$l.11,x2w
417 # this is so fai can have an explicit name to use for testing,
418 # or else any random machine which did a pxe boot would get
419 # reformatted. The mac is from doing a virt-install, cancelling it,
420 # and copying the generated mac, so it should be randomish.
421 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.6,demohost
422 #dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.7,faiserver
423 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
424 # this is the ip it picks by default if dhcp fails,
425 # so might as well use it.
426 # hostname is the name it uses according to telnet
427 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
432 # Just leave the tftp server up even if we aren't doing pxe boot.
433 # It has no sensitive info.
435 tftp-root=/mnt/usb/tftpboot
438 if $dnsmasq_restart; then
439 v
/etc
/init.d
/dnsmasq restart
442 if $firewall_restart; then
443 v
/etc
/init.d
/firewall restart
448 if [[ $
(uci get network.lan.ipaddr
) != $l.1 ]]; then
449 v uci
set network.lan.ipaddr
=$l.1
453 if [[ $
(uci get network.lan.netmask
) != $mask ]]; then
454 v uci
set network.lan.netmask
=$mask