more complete copyright notices
[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 # note, this script gets piped to bash, so cant cd to current dir
20
21 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
22
23 set -eE -o pipefail
24 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
25
26 kgped16=false
27 bond=false
28 fai_action=install
29 fai_reboot_arg=,reboot
30 while [[ $1 == -* ]]; do
31 case $1 in
32 -h|--help)
33 echo "see help from myfai-chboot"
34 exit 0
35 ;;
36 -S)
37 fai_action=sysinfo
38 fai_reboot_arg=
39 shift
40 ;;
41 -i) #inventory
42 fai_action=inventory
43 fai_reboot_arg=
44 shift
45 ;;
46 -k)
47 kgped16=true
48 shift
49 ;;
50 -b)
51 bond=true
52 shift
53 ;;
54 --no-r)
55 fai_reboot_arg=
56 shift
57 ;;
58 esac
59 done
60
61 pre="${0##*/}:"
62 m() { printf "$pre %s\n" "$*"; "$@"; }
63 e() { printf "$pre %s\n" "$*"; }
64 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
65
66 host=$1
67
68
69 rm -f /srv/tftp/fai/pxelinux.cfg/*
70 if [[ ! $1 ]]; then
71 echo "$0: clearing pxe config and exiting"
72 exit 0
73 fi
74
75 # somewhat duplicated in brc hostip()
76 case $host in
77 default) : ;;
78 [0-9:])
79 hostip=$host
80 ;;
81 *)
82 hostip=$(getent ahostsv4 "$host" | awk '{ print $1 }' | head -n1)
83 ;;
84 esac
85
86 if [[ $hostip ]]; then
87
88 # assuming ipv4, or else we might need to deal with multiple addresses
89 # in an ipv4 + ipv6 network.
90 my_ip=$(ip -4 route get $hostip | sed -nr 's,^.*src\s+(\S+).*,\1,p')
91 if [[ ! $my_ip || $my_ip =~ [[:space:]] ]]; then
92 echo "$0: error: failed to get \$my_ip, got: $my_ip"
93 exit 1
94 fi
95 else
96 my_ip=$(ip r show default | sed -r 's/.*via ([^ ]*).*/\1/' | head -n1)
97 fi
98
99 if [[ $host == default ]]; then
100 ip='*'
101 elif [[ $host == [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
102 ip=$host/32
103 else
104 type -t host &>/dev/null || apt-get -y install dnsutils
105 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q' ||:)
106 if [[ ! $ip || $ip =~ [[:space:]] ]]; then
107 echo "$0: error: failed to get \$ip, got: $ip"
108 exit 1
109 fi
110 ip=$ip/32
111 echo "$0: found ip of $host: $ip"
112 fi
113
114 if modprobe nfsd &>/dev/null; then
115 std_arg="-u nfs://faiserver/srv/fai/config"
116 # nfsv4 wont do rw with overlayfs yet
117 # https://lists.uni-koeln.de/pipermail/linux-fai/2017-March/011641.html
118 root_arg="$my_ip:/srv/fai/nfsroot:vers=3"
119 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
120 # I restrict it to one ip as simple but imperfect access control.
121
122 # we may chattr +i /etc/exports if we dun want it modified
123 # for example, if we made these exports more widely available
124 # while doing multiple installs or a recovery.
125 if [[ -w /etc/exports ]]; then
126 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
127 cat >>/etc/exports <<EOF
128 /srv/fai/config $ip(async,ro,no_subtree_check,no_root_squash)
129 /srv/fai/nfsroot $ip(async,ro,no_subtree_check,no_root_squash)
130 EOF
131 exportfs -ra
132 fi
133 systemctl start nfs-server # assumes recent os
134 else
135 std_arg="-u http://faiserver:8080/config.tar.gz"
136 root_arg="live:http://faiserver:8080/squash.img"
137 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
138 <Location />
139 Deny from all
140 Allow from $ip
141 </Location>
142 EOF
143 fi
144
145
146
147 # man page doesn't explain this, but this deletes & thus disables
148 # all chboot systems.
149 m fai-chboot -iv $std_arg default # set it to default to get a val out of it next
150 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
151 default_k_args=$(fai-chboot -L '^default$' | \
152 sed -r "s/^(\S+\s+){3}(.*)/\2/")
153 # example of default_k_args
154 # 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
155
156 # https://wiki.archlinux.org/index.php/Solid_state_drive#Resolving_NCQ_errors
157 # currently on needed on d16 samsung 870 qvo, but better to have this
158 # and not wait for more conditions where its needed.
159 #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)
160 #k_args=(FAI_ACTION=$fai_action libata.force=noncq ifname:bootnet0:08:60:6e:10:f0:fe biosdevname=0 bootdev=bootnet0)
161 k_args=(FAI_ACTION=$fai_action libata.force=noncq)
162 if $kgped16; then
163 k_args+=(console=tty0 console=ttyS0,115200)
164 fi
165
166 for arg in $default_k_args; do
167 case $arg in
168 # default root arg is /srv/fai/nfsroot
169 root=*) k_args+=(root=$root_arg) ;;
170 # note: this works to only dhcp on one interface: ip=eth0:dhcp
171 ip=*)
172 if $bond; then
173 k_args+=("bond=bond0:eth0,eth1:mode=balance-rr ip=bond0:dhcp")
174 else
175 k_args+=($arg)
176 fi
177 ;;
178 *) k_args+=($arg) ;;
179 esac
180 done
181 rm -f /srv/tftp/fai/pxelinux.cfg/*
182 m fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt$fai_reboot_arg $std_arg $kernel "$host"
183
184 # this is needed for autodiscover iso. i'm not sure, it might override
185 # the fai-chboot method of setting this, i'm not sure.
186 echo FAI_ACTION=$fai_action >> /srv/fai/config/class/LAST.var