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 if [[ $1 == -2 ]]; then
27 # background: upgrading all packages is not recommended because it
28 # doesn't go into the firmware. build new firmware if you want
29 # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
31 if ! (( $
(date -r $f +%s
) + 60*60*24 > $
(date +%s
) )); then
38 if [[ ! $
(opkg list-installed
"$x") ]]; then
52 ssid
="check out gnu.org"
61 passwd
-l root ||
: #already locked fails
63 cat >/usr
/bin
/archlike-pxe-mount
<<'EOFOUTER'
65 # symlinks are collapsed for nfs mount points, so use a bind mount.
66 # tried putting this in /etc/config/fstab,
67 # then doing block mount, it didn't work. This doesn't persist across reboots,
68 # todo: figure that out
70 for d in /run/{arch,parabola}iso/bootmnt; do
71 cat >>/etc/fstab <<EOF
72 /mnt/usb/tftpboot $d none bind 0 0
74 mount | grep $d &>/dev/null || mount $d
76 /etc/init.d/nfsd restart
78 chmod +x
/usr
/bin
/archlike-pxe-mount
80 sed -i '/^root:/s,/bin/ash$,/bin/bash,' /etc
/passwd
81 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
82 tcpdump openvpn-openssl adblock libusb-compat
/root
/relay_1.0
-1_mips_24kc.ipk
88 v
/etc
/init.d
/fstab
enable ||
:
90 # rebooting makes mounting work, but comparing lsmod,
91 # i'm guessing this will too. todo, test it.
92 # 255 == module already loaded
93 for mod
in scsi_mod sd_mod
; do v modprobe
$mod ||
[[ $?
== 255 ]]; done
95 # for archlike pxe. The default settings in the installer expect to find
96 # the NFS at one of these dirs
97 mkdir
-p /run
/archiso
/bootmnt
98 mkdir
-p /run
/parabolaiso
/bootmnt
100 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
101 # that is the case after running this or rebooting.
102 # wiki says safe to do in case of fstab changes:
104 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
105 echo | cedit
/etc
/config
/fstab ||
:
106 cedit
/etc
/config
/fstab
<<EOF || { v block umount; v block mount; }
107 config global automount
112 # /overlay is an / overlay mount for installing extra packages, etc.
113 # https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
114 option target /mnt/usb
115 # option target /overlay
116 option device /dev/sda1
118 option options rw,async,noatime,nodiratime
123 # ian: disabled because afaik I don't need it, no benefit.
124 # config global autoswap
125 # option from_fstab 1
129 # option device /dev/sda1
135 # exportfs -ra wont cut it when its the same path, but now a bind mount
136 # todo: restart nfs when nfs is enabled?
137 #cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
138 cedit
/etc
/exports
<<EOF ||:
139 /mnt/usb $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
141 /run/archiso/bootmnt $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
142 /run/parabolaiso/bootmnt $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
146 # todo: enable nfs when we need it only.
147 # v /etc/init.d/portmap start
148 # v /etc/init.d/nfsd start
149 # v /etc/init.d/portmap enable
150 # v /etc/init.d/nfsd enable
157 ######### uci example:#######
158 # # https://wiki.openwrt.org/doc/uci
159 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
160 # wan="firewall.@zone[$wan_index]"
161 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
163 # v uci set firewall.@forwarding[0].dest=$forward_dest
164 # uci commit firewall
165 # firewall_restart=true
172 local service
="${key%%.*}"
173 restart_var
=${service}_restart
174 if [[ ! ${!restart_var} ]]; then
175 eval $restart_var=false
177 if [[ $
(uci get
"$key") != "$val" ]]; then
178 v uci
set "$key"="$val"
180 eval $restart_var=true
184 uset dropbear.@dropbear
[0].PasswordAuth
0
185 uset dropbear.@dropbear
[0].RootPasswordAuth
0
186 uset dropbear.@dropbear
[0].Port
2220
188 wireless_restart
=false
191 uset wireless.default_radio
$x.ssid
"$ssid"
192 uset wireless.default_radio
$x.key
$key
193 uset wireless.default_radio
$x.encryption psk2
194 if [[ $
(uci get wireless.radio
$x.disabled
2>/dev
/null
) ]]; then
195 v uci delete wireless.radio
$x.disabled
196 wireless_restart
=true
200 if $wireless_restart; then
205 ########## openvpn exampl
206 ########## missing firewall settings for routing lan
208 # v /etc/init.d/openvpn start
209 # v /etc/init.d/openvpn enable
211 # # from https://wiki.openwrt.org/doc/uci/firewall
212 # # todo: not sure if /etc/init.d/network needs restarting.
213 # # I did, and I had to restart the vpn afterwards.
214 # # This maps a uci interface to a real interface which is
215 # # managed outside of uci.
216 # v cedit /etc/config/network <<'EOF' ||:
217 # config interface 'tun0'
218 # option ifname 'tun0'
219 # option proto 'none'
221 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
222 # config openvpn my_client_config
224 # option config /etc/openvpn/client.conf
229 v cedit
/etc
/config
/network
<<EOF || v /etc/init.d/network reload
230 config 'route' 'transmission'
231 option 'interface' 'lan'
232 option 'target' '10.173.0.0'
233 option 'netmask' '255.255.0.0'
234 option 'gateway' '$l.3'
237 firewall_restart
=false
238 v cedit
/etc
/config
/firewall
<<EOF || firewall_restart=true
253 option src_dport 2222
260 option dest_port 2222
265 option dest_port 2220
282 option src_dport 1196
289 option dest_port 1196
306 option name syncthing
308 option src_dport 22001
314 option dest_port 22001
323 dnsmasq_restart
=false
324 v cedit
/etc
/hosts
<<EOF || dnsmasq_restart=true
333 $l.8 tp faiserver b8.nz
336 # netns creation looks for next free subnet starting at 10.173, but I only
337 # use one, and I would keep this one as the first created.
338 10.173.0.2 transmission
341 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
342 # if [[ $mail_host ]]; then
343 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
347 # avoid using the dns servers that my isp tells me about.
348 if [[ $
(uci get dhcp.@dnsmasq
[0].resolvfile
2>/dev
/null
) ]]; then
349 # default is '/tmp/resolv.conf.auto', we switch to the dnsmasq default of
350 # /etc/resolv.conf. not sure why I did this.
351 v uci delete dhcp.@dnsmasq
[0].resolvfile
356 uset dhcp.@dnsmasq
[0].domain b8.nz
357 uset dhcp.@dnsmasq
[0].
local /b8.nz
/
358 uset system.@system
[0].hostname wrt
360 if [[ $
(uci get adblock.global.adb_enabled
) != 1 ]]; then
361 v uci
set adblock.global.adb_enabled
=1
363 /etc
/init.d
/adblock restart
365 # https://github.com/openwrt/packages/tree/master/net/adblock/files
366 cat >/etc
/crontabs
/root
<<'EOF'
367 0 06 * * * /etc/init.d/adblock reload
371 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
373 # sometimes /mnt/usb fails, cuz it's just a flash drive,
374 # so make sure we have this dir or else dnsmasq will fail
376 mkdir
-p /mnt
/usb
/tftpboot
377 v cedit
/etc
/dnsmasq.conf
<<EOF || dnsmasq_restart=true
379 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
382 # this says the ip of default gateway and dns server,
383 # but I think they are unneded and default
389 # results from googling around dnsmasq optimizations
390 # about 50k in memory. router has 62 megs.
391 # in a browsing session, I probably won't ever do 5000 lookups
392 # before the ttl expiration or whatever does expiration.
395 # ask all servers, use the one which responds first.
396 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
399 # namebench benchmarks dns servers. google's dns was only
400 # slightly less fast than some others, and I trust it more
401 # to give accurate results, stay relatively fast, and
402 # not do anythin too malicious, so just use that.
403 # download namebench and run it like this:
404 # 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
408 server=2001:4860:4860::8888
409 server=2001:4860:4860::8844
412 # to fixup existin ips, on the client you can do
413 # sudo dhclient -r; sudo dhclient <interface-name>
415 # default dhcp range is 100-150
416 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
417 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
418 # top port, iPXE (PCI 04:00.0) in seabios boot menu
419 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
420 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
421 dhcp-host=10:78:d2:da:29:22,set:htpc,$l.4,htpc
422 dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
423 # This is so fai can have an explicit name to use for testing,
424 # or else any random machine which did a pxe boot would get
425 # reformatted. The mac is from doing a virt-install, cancelling it,
426 # and copying the generated mac, so it should be randomish.
427 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.6,demohost
428 dhcp-host=00:1f:16:14:01:d8,set:tp,$l.7,x3
429 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
432 dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
434 # This is the ip it picks by default if dhcp fails,
435 # so might as well use it.
436 # hostname is the name it uses according to telnet
437 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
442 # Just leave the tftp server up even if we aren't doing pxe boot.
443 # It has no sensitive info.
445 tftp-root=/mnt/usb/tftpboot
448 if $dnsmasq_restart; then
449 v
/etc
/init.d
/dnsmasq restart
452 if $firewall_restart; then
453 v
/etc
/init.d
/firewall restart
456 uset network.lan.ipaddr
$l.1
457 uset network.lan.netmask
$mask
459 if $network_restart; then
462 if $dropbear_restart; then
463 v
/etc
/init.d
/dropbear restart