various fixes, improvements, shellcheck
[automated-distro-installer] / fai / config / distro-install-common / ethusb-static
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23 # usage $0 [-c] [off]
24 # off: Turn off static ip.
25 # -c config only, don't tell networkmanager to change anything
26 # -f force interface reup
27
28 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
29 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
30 set -eE -o pipefail
31 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
32
33 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
34
35 m() { printf "%s\n" "$*"; "$@"; }
36
37 ## begin arg parsing ##
38
39 force=false
40 conf_only=false
41 comment='# iank file id: ethusb-dhcp-v1'
42 off=false
43 while [[ $1 ]]; do
44 case $1 in
45 -c)
46 conf_only=true
47 ;;
48 -f)
49 force=true
50 ;;
51 off)
52 off=true
53 comment='# iank file id: ethusb-static-v1'
54 ;;
55 *)
56 echo "$0: error unexpected argument: $1" >&2
57 exit 1
58 ;;
59 esac
60 shift
61 done
62
63 ## end arg parsing ##
64
65
66 shopt -s nullglob
67
68 # we already configured the interface once, afterwards, comment and
69 # uncomment to enable/disable. This makes it so we don't depend on /p
70 # being mounted.
71
72 conf=/etc/NetworkManager/system-connections/ethusb-static.nmconnection
73 if ! $force && [[ -s $conf ]] && grep -qFx "$comment" $conf; then
74 # we already ran successfully in the past to set things this way, so
75 # do nothing.
76 exit 0
77 fi
78
79
80 if [[ $(dig +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]] \
81 && ip n show 10.2.0.1 | grep . &>/dev/null; then
82 # we are at_home=true
83
84 while read -r ip_suf host mac; do
85 if [[ $mac != usb ]]; then
86 continue
87 fi
88 if [[ $host == ${HOSTNAME}c ]]; then
89
90 net_info="address1=10.2.0.$ip_suf/16,10.2.0.1
91 dns=8.8.8.4;8.8.8.8;"
92
93 break
94 fi
95 done </p/c/host-info
96
97 if [[ ! $ip_suf ]]; then
98 echo "$0: error: failed to find ${HOSTNAME}c ip suffix in /p/c/host-info"
99 exit 1
100 fi
101 else
102 ip=$(getent ahostsv4 $HOSTNAME.office.fsf.org | awk '{ print $1 }' | head -n1)
103 net_info="address1=$ip/24,192.168.0.1
104 dns=192.168.0.10;192.168.0.25;"
105 fi
106
107 wiredx=
108
109 # device that has an eth0, but we aren't using it because it is
110 # broken. We could just hardcode a mac comparison with `cat
111 # /sys/class/net/eth0/address` but this is cooler.
112 if [[ -e /sys/class/net/eth0 ]]; then
113 bus_info=$(ethtool -i eth0 | awk '$1 == "bus-info:" { print $2 }')
114 if [[ $bus_info != usb* ]]; then
115 wiredx=2
116 fi
117 fi
118
119 ethx=$(( wiredx - 1 ))
120
121
122
123 uuid=$(nmcli con show "Wired connection $wiredx" 2>/dev/null | awk '$1 == "connection.uuid:" {print $2}' ||:)
124 if [[ ! $uuid ]]; then
125 # just a uuid that nm generated for me at some point
126 uuid=0da4c614-6a3c-3ad2-8d4b-c6eebe0814c3
127 fi
128
129
130 # This template is the result of running, for example
131 # nmcli con mod "Wired connection 1" \
132 # ipv4.addresses "10.2.0.23/24" \
133 # ipv4.gateway "10.2.0.1" \
134 # ipv4.dns "8.8.8.4,8.8.8.8"
135
136 # which creates a fille named "Wired connection 1.nmconnection",
137 # below. I see no reason to keep the same file name, or a bunch of
138 # setting that seem irrelevant, and empty sections don't seem to do
139 # anything according to the man page.
140
141 # [connection]
142 # id=Wired connection 2
143 # uuid=b0fb7694-dfe6-31a1-81fa-7c17b61515a7
144 # type=ethernet
145 # interface-name=eth1
146 # timestamp=1715728264
147
148 # [ethernet]
149
150 # [ipv4]
151 # address1=10.2.0.23/16,10.2.0.1
152 # dns=8.8.8.4;8.8.8.8;
153 # method=manual
154
155 # [ipv6]
156 # addr-gen-mode=stable-privacy
157 # method=auto
158
159 # [proxy]
160
161 {
162 cat <<EOF
163 [connection]
164 id=Wired connection $wiredx
165 uuid=$uuid
166 type=ethernet
167 interface-name=eth$ethx
168
169 [ipv4]
170 EOF
171 if $off; then
172 cat <<'EOF'
173 method=auto
174 EOF
175 else
176 cat <<EOF
177 $net_info
178 method=manual
179 EOF
180 fi
181 } | install -T -m0600 /dev/stdin $conf
182
183 if ! $conf_only; then
184 state=$(nmcli con show $uuid 2>/dev/null | awk '$1 == "GENERAL.STATE:" {print $2}' ||:)
185
186 reup=false
187 if [[ $state == activated ]]; then
188 reup=true
189 fi
190
191 m nmcli con reload
192
193 if $reup; then
194 m nmcli con down $uuid
195 m nmcli con up $uuid
196 fi
197 fi
198
199 if ! grep -F "$comment" $conf; then
200 printf "%s\n" "$comment" >>$conf
201 fi