static usb ethnet addresses
[automated-distro-installer] / myfai-chboot-local
1 #!/bin/bash
2 # This file is part of Ian Kelling's automated-distro-installer
3 # Copyright (C) 2024 Ian Kelling
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19
20 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
21
22 set -x
23
24 set -eE -o pipefail
25 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
26
27 pre="${0##*/}:"
28 m() { printf "$pre %s\n" "$*"; "$@"; }
29 e() { printf "$pre %s\n" "$*"; }
30 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
31
32 usage() {
33 cat <<EOF
34 Usage: call from myfai-chboot, see its help
35
36 # note, this script gets piped to bash, so cant cd to current dir
37
38 -h|--help Print help and exit.
39
40 Note: Uses util-linux getopt option parsing: spaces between args and
41 options, short options can be combined, options before args.
42 EOF
43 exit $1
44 }
45
46
47 kgped16=false
48 bond=false
49 fai_action=install
50 fai_reboot_arg=,reboot
51
52 # ensure we can handle args with spaces or empty.
53 ret=0; getopt -T || ret=$?
54 [[ $ret == 4 ]] || { echo "Install util-linux for enhanced getopt" >&2; exit 1; }
55
56 temp=$(getopt -l help,no-r hSi "$@") || usage 1
57 eval set -- "$temp"
58 while true; do
59 case $1 in
60 -S)
61 fai_action=sysinfo
62 fai_reboot_arg=
63 ;;
64 -i) #inventory
65 fai_action=inventory
66 fai_reboot_arg=
67 ;;
68 -k)
69 kgped16=true
70 ;;
71 -b)
72 bond=true
73 ;;
74 --no-r)
75 fai_reboot_arg=
76 ;;
77 -h|--help) usage ;;
78 --) shift; break ;;
79 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
80 esac
81 shift
82 done
83 read -r host <<<"$@"
84 readonly host
85
86
87 rm -f /srv/tftp/fai/pxelinux.cfg/*
88 if [[ ! $1 ]]; then
89 echo "$0: clearing pxe config and exiting"
90 exit 0
91 fi
92
93 # somewhat duplicated in brc hostip()
94 case $host in
95 default) : ;;
96 [0-9:])
97 hostip=$host
98 ;;
99 *)
100 hostip=$(getent ahostsv4 "$host" | awk '{ print $1 }' | head -n1)
101 ;;
102 esac
103
104 if [[ $hostip ]]; then
105
106 # assuming ipv4, or else we might need to deal with multiple addresses
107 # in an ipv4 + ipv6 network.
108 my_ip=$(ip -4 route get $hostip | sed -nr 's,^.*src\s+(\S+).*,\1,p')
109 if [[ ! $my_ip || $my_ip =~ [[:space:]] ]]; then
110 echo "$0: error: failed to get \$my_ip, got: $my_ip"
111 exit 1
112 fi
113 else
114 my_ip=$(ip r show default | sed -r 's/.*via ([^ ]*).*/\1/' | head -n1)
115 fi
116
117 if [[ $host == default ]]; then
118 ip='*'
119 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
120 ip=$host/32
121 else
122 type -t host &>/dev/null || apt-get -y install dnsutils
123 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q' ||:)
124 if [[ ! $ip || $ip =~ [[:space:]] ]]; then
125 echo "$0: error: failed to get \$ip, got: $ip"
126 exit 1
127 fi
128 ip=$ip/32
129 echo "$0: found ip of $host: $ip"
130 fi
131
132 if modprobe nfsd &>/dev/null; then
133 std_arg="-u nfs://faiserver/srv/fai/config"
134 # nfsv4 wont do rw with overlayfs yet
135 # https://lists.uni-koeln.de/pipermail/linux-fai/2017-March/011641.html
136 root_arg="$my_ip:/srv/fai/nfsroot:vers=3"
137 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
138 # I restrict it to one ip as simple but imperfect access control.
139
140 # we may chattr +i /etc/exports if we dun want it modified
141 # for example, if we made these exports more widely available
142 # while doing multiple installs or a recovery.
143 if [[ -w /etc/exports ]]; then
144 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
145 cat >>/etc/exports <<EOF
146 /srv/fai/config $ip(async,ro,no_subtree_check,no_root_squash)
147 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
148 EOF
149 exportfs -ra
150 fi
151 systemctl start nfs-server # assumes recent os
152 else
153 std_arg="-u http://faiserver:8080/config.tar.gz"
154 root_arg="live:http://faiserver:8080/squash.img"
155 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
156 <Location />
157 Deny from all
158 Allow from $ip
159 </Location>
160 EOF
161 fi
162
163
164
165 # man page doesn't explain this, but this deletes & thus disables
166 # all chboot systems.
167 m fai-chboot -iv $std_arg default # set it to default to get a val out of it next
168 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
169 default_k_args=$(fai-chboot -L '^default$' | \
170 sed -r "s/^(\S+\s+){3}(.*)/\2/")
171 # example of default_k_args
172 # initrd=initrd.img-3.16.0-4-amd64 ip=dhcp root=192.168.1.3:/srv/fai/nfsroot FAI_CONFIG_SRC=nfs://faiserver/srv/fai/config FAI_ACTION=install
173
174 # https://wiki.archlinux.org/index.php/Solid_state_drive#Resolving_NCQ_errors
175 # currently on needed on d16 samsung 870 qvo, but better to have this
176 # and not wait for more conditions where its needed.
177 #k_args=(FAI_ACTION=$fai_action libata.force=noncq ifname:bootnet0:08:60:6e:10:f0:fe ifname:bootnet1:08:60:6e:10:f0:98 bond=bond0:bootnet0,bootnet1:mode=balance-rr)
178 #k_args=(FAI_ACTION=$fai_action libata.force=noncq ifname:bootnet0:08:60:6e:10:f0:fe biosdevname=0 bootdev=bootnet0)
179 k_args=(FAI_ACTION=$fai_action libata.force=noncq)
180 if $kgped16; then
181 k_args+=(console=tty0 console=ttyS0,115200)
182 fi
183
184 for arg in $default_k_args; do
185 case $arg in
186 # default root arg is /srv/fai/nfsroot
187 root=*) k_args+=(root=$root_arg) ;;
188 # note: this works to only dhcp on one interface: ip=eth0:dhcp
189 ip=*)
190 if $bond; then
191 k_args+=("bond=bond0:eth0,eth1:mode=balance-rr ip=bond0:dhcp")
192 else
193 k_args+=($arg)
194 fi
195 ;;
196 *) k_args+=($arg) ;;
197 esac
198 done
199 rm -f /srv/tftp/fai/pxelinux.cfg/*
200 m fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"
201
202 # this is needed for autodiscover iso. i'm not sure, it might override
203 # the fai-chboot method of setting this, i'm not sure.
204 echo FAI_ACTION=$fai_action >> /srv/fai/config/class/LAST.var