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
50 ######### uci example:#######
51 # # https://wiki.openwrt.org/doc/uci
52 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
53 # wan="firewall.@zone[$wan_index]"
54 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
56 # v uci set firewall.@forwarding[0].dest=$forward_dest
58 # firewall_restart=true
60 ####### end uci example #####
65 local service
="${key%%.*}"
66 restart_var
=${service}_restart
67 if [[ ! ${!restart_var} ]]; then
68 eval $restart_var=false
70 if [[ $
(uci get
"$key") != "$val" ]]; then
71 v uci
set "$key"="$val"
73 eval $restart_var=true
80 ssid
="check out gnu.org"
89 passwd
-l root ||
: #already locked fails
91 sed -ibak '/^root:/d' /etc
/shadow
92 # /root/router created by manually running passwd then copying the resulting
93 # line. We have no mkpasswd on wrt/librecmc, then we scp it in.
94 cat /root
/router
>>/etc
/shadow
95 # otherwise, serial console gets root login with no password
96 uset system.@system
[0].ttylogin
1
99 cat >/usr
/bin
/archlike-pxe-mount
<<'EOFOUTER'
101 # symlinks are collapsed for nfs mount points, so use a bind mount.
102 # tried putting this in /etc/config/fstab,
103 # then doing block mount, it didn't work. This doesn't persist across reboots,
104 # todo: figure that out
106 for d in /run/{arch,parabola}iso/bootmnt; do
107 cat >>/etc/fstab <<EOF
108 /mnt/usb/tftpboot $d none bind 0 0
110 mount | grep $d &>/dev/null || mount $d
112 /etc/init.d/nfsd restart
114 chmod +x
/usr
/bin
/archlike-pxe-mount
116 sed -i '/^root:/s,/bin/ash$,/bin/bash,' /etc
/passwd
117 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
118 tcpdump openvpn-openssl adblock libusb-compat
/root
/relay_1.0
-1_mips_24kc.ipk
124 v
/etc
/init.d
/fstab
enable ||
:
126 # rebooting makes mounting work, but comparing lsmod,
127 # i'm guessing this will too. todo, test it.
128 # 255 == module already loaded
129 for mod
in scsi_mod sd_mod
; do v modprobe
$mod ||
[[ $?
== 255 ]]; done
131 # for archlike pxe. The default settings in the installer expect to find
132 # the NFS at one of these dirs
133 mkdir
-p /run
/archiso
/bootmnt
134 mkdir
-p /run
/parabolaiso
/bootmnt
136 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
137 # that is the case after running this or rebooting.
138 # wiki says safe to do in case of fstab changes:
140 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
141 echo | cedit
/etc
/config
/fstab ||
:
142 cedit
/etc
/config
/fstab
<<EOF || { v block umount; v block mount; }
143 config global automount
148 # /overlay is an / overlay mount for installing extra packages, etc.
149 # https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
150 option target /mnt/usb
151 # option target /overlay
152 option device /dev/sda1
154 option options rw,async,noatime,nodiratime
159 # ian: disabled because afaik I don't need it, no benefit.
160 # config global autoswap
161 # option from_fstab 1
165 # option device /dev/sda1
171 # exportfs -ra wont cut it when its the same path, but now a bind mount
172 # todo: restart nfs when nfs is enabled?
173 #cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
174 cedit
/etc
/exports
<<EOF ||:
175 /mnt/usb $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
177 /run/archiso/bootmnt $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
178 /run/parabolaiso/bootmnt $lan/$netmask(rw,no_root_squash,insecure,sync,no_subtree_check)
182 # todo: enable nfs when we need it only.
183 # v /etc/init.d/portmap start
184 # v /etc/init.d/nfsd start
185 # v /etc/init.d/portmap enable
186 # v /etc/init.d/nfsd enable
194 uset dropbear.@dropbear
[0].PasswordAuth
0
195 uset dropbear.@dropbear
[0].RootPasswordAuth
0
196 uset dropbear.@dropbear
[0].Port
2220
198 wireless_restart
=false
201 uset wireless.default_radio
$x.ssid
"$ssid"
202 uset wireless.default_radio
$x.key
$key
203 uset wireless.default_radio
$x.encryption psk2
204 if [[ $
(uci get wireless.radio
$x.disabled
2>/dev
/null
) ]]; then
205 v uci delete wireless.radio
$x.disabled
206 wireless_restart
=true
210 if $wireless_restart; then
215 ########## openvpn exampl
216 ########## missing firewall settings for routing lan
218 # v /etc/init.d/openvpn start
219 # v /etc/init.d/openvpn enable
221 # # from https://wiki.openwrt.org/doc/uci/firewall
222 # # todo: not sure if /etc/init.d/network needs restarting.
223 # # I did, and I had to restart the vpn afterwards.
224 # # This maps a uci interface to a real interface which is
225 # # managed outside of uci.
226 # v cedit /etc/config/network <<'EOF' ||:
227 # config interface 'tun0'
228 # option ifname 'tun0'
229 # option proto 'none'
231 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
232 # config openvpn my_client_config
234 # option config /etc/openvpn/client.conf
239 v cedit
/etc
/config
/network
<<EOF || v /etc/init.d/network reload
240 config 'route' 'transmission'
241 option 'interface' 'lan'
242 option 'target' '10.173.0.0'
243 option 'netmask' '255.255.0.0'
244 option 'gateway' '$l.3'
247 firewall_restart
=false
248 v cedit
/etc
/config
/firewall
<<EOF || firewall_restart=true
265 option src_dport 2222
272 option dest_port 2222
277 option dest_port 2220
294 option src_dport 1196
301 option dest_port 1196
318 option name syncthing
320 option src_dport 22001
326 option dest_port 22001
335 dnsmasq_restart
=false
336 v cedit
/etc
/hosts
<<EOF || dnsmasq_restart=true
345 $l.8 tp b8.nz faiserver
349 # netns creation looks for next free subnet starting at 10.173, but I only
350 # use one, and I would keep this one as the first created.
351 10.173.0.2 transmission
354 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
355 # if [[ $mail_host ]]; then
356 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
360 # avoid using the dns servers that my isp tells me about.
361 if [[ $
(uci get dhcp.@dnsmasq
[0].resolvfile
2>/dev
/null
) ]]; then
362 # default is '/tmp/resolv.conf.auto', we switch to the dnsmasq default of
363 # /etc/resolv.conf. not sure why I did this.
364 v uci delete dhcp.@dnsmasq
[0].resolvfile
369 uset dhcp.@dnsmasq
[0].domain b8.nz
370 uset dhcp.@dnsmasq
[0].
local /b8.nz
/
371 uset system.@system
[0].hostname wrt
373 if [[ $
(uci get adblock.global.adb_enabled
) != 1 ]]; then
374 v uci
set adblock.global.adb_enabled
=1
376 /etc
/init.d
/adblock restart
378 # https://github.com/openwrt/packages/tree/master/net/adblock/files
379 cat >/etc
/crontabs
/root
<<'EOF'
380 0 06 * * * /etc/init.d/adblock reload
384 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
386 # sometimes /mnt/usb fails, cuz it's just a flash drive,
387 # so make sure we have this dir or else dnsmasq will fail
389 mkdir
-p /mnt
/usb
/tftpboot
390 v cedit
/etc
/dnsmasq.conf
<<EOF || dnsmasq_restart=true
392 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
395 # this says the ip of default gateway and dns server,
396 # but I think they are unneded and default
402 # results from googling around dnsmasq optimizations
403 # about 50k in memory. router has 62 megs.
404 # in a browsing session, I probably won't ever do 5000 lookups
405 # before the ttl expiration or whatever does expiration.
408 # ask all servers, use the one which responds first.
409 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
412 # namebench benchmarks dns servers. google's dns was only
413 # slightly less fast than some others, and I trust it more
414 # to give accurate results, stay relatively fast, and
415 # not do anythin too malicious, so just use that.
416 # download namebench and run it like this:
417 # 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
421 server=2001:4860:4860::8888
422 server=2001:4860:4860::8844
425 # to fixup existin ips, on the client you can do
426 # sudo dhclient -r; sudo dhclient <interface-name>
428 # default dhcp range is 100-150
429 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
430 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
431 # top port, iPXE (PCI 04:00.0) in seabios boot menu
432 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
433 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
434 dhcp-host=10:78:d2:da:29:22,set:htpc,$l.4,htpc
435 dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
436 # This is so fai can have an explicit name to use for testing,
437 # or else any random machine which did a pxe boot would get
438 # reformatted. The mac is from doing a virt-install, cancelling it,
439 # and copying the generated mac, so it should be randomish.
440 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.6,demohost
441 dhcp-host=00:1f:16:14:01:d8,set:tp,$l.7,x3
442 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
445 dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
447 # This is the ip it picks by default if dhcp fails,
448 # so might as well use it.
449 # hostname is the name it uses according to telnet
450 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
455 # Just leave the tftp server up even if we aren't doing pxe boot.
456 # It has no sensitive info.
458 tftp-root=/mnt/usb/tftpboot
461 if $dnsmasq_restart; then
462 v
/etc
/init.d
/dnsmasq restart
465 if $firewall_restart; then
466 v
/etc
/init.d
/firewall restart
469 uset network.lan.ipaddr
$l.1
470 uset network.lan.netmask
$mask
472 if $network_restart; then
475 if $dropbear_restart; then
476 v
/etc
/init.d
/dropbear restart