Debian Codegraph @

bin/fai-setup

raw symbols SCIP
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # fai-setup -- set up FAI
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # (c) 2000-2026 by Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 #
11 #*********************************************************************
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
21 #
22 # A copy of the GNU General Public License is available as
23 # `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
24 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
25 # can also obtain it by writing to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #*********************************************************************
28
29 PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin
30
31 set -e
32
33 cfdir=/etc/fai
34
35 options=$@ # all options are also passed to fai-make-nfsroot
36
37 while getopts pPvC:efgkKV:B: opt ; do
38 case "$opt" in
39 C) cfdir=$OPTARG ;;
40 v) verbose=1 ; v=-v ;;
41 e) expert=1 ;;
42 esac
43 done
44
45
46 . $cfdir/fai.conf
47 . $cfdir/nfsroot.conf
48
49 : ${FAI_LOGPROTO:=ssh}
50
51 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52 add_export_line() {
53
54 # add a line to /etc/exports
55
56 pattern=$1
57 shift
58 options="$@"
59
60 [ -f /etc/exports ] && grep -q "^$pattern[[:space:]]" /etc/exports && return
61 echo "Adding line to /etc/exports: $pattern $options"
62 echo "$pattern $options" >> /etc/exports
63 }
64 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65 add_fai_account() {
66
67 if id $LOGUSER 2>/dev/null 1>&2 ; then
68 echo Account \$LOGUSER=$LOGUSER already exists.
69 echo Make sure that all install clients can
70 echo log into this account without a password.
71 return
72 fi
73
74 adduser --system --disabled-password --home /var/log/fai/remote-logs --gecos "FAI account for remote log files" --shell /bin/sh $LOGUSER
75 # get the home dir of a user in a variable; do not exit when set -e is used
76 # loguserhome is unset if $LOGUSER does not exists
77 # this is not a evil hack, it's a very clever piece of code
78 loguserhome=$(eval "cd ~$LOGUSER 2>/dev/null && pwd;true")
79 touch $loguserhome/.account_created_by_fai_package
80 }
81 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82 setup_fai_account() {
83
84 # most things should be executed as user $LOGUSER, since root may not have write
85 # permissions to $loguserhome (e.g if mount via NFS without no_root_squash)
86
87 set +e
88 loguserhome=$(eval "cd ~$LOGUSER 2>/dev/null && pwd;true")
89 sshdir=$loguserhome/.ssh
90 if [ -z "$loguserhome" ]; then
91 echo "Can't determine home directory for user $LOGUSER."
92 echo "LOGUSER= $LOGUSER loguserhome= $loguserhome"
93 exit 8
94 fi
95
96 if [ "$FAI_LOGPROTO" = "ssh" ]; then
97 # set up ssh on the server
98 mkdir -p -m 700 $loguserhome/.ssh
99
100 #Generating keys for LOGUSER
101 [ -f $sshdir/id_rsa ] || {
102 ssh-keygen -t rsa -N '' -f $sshdir/id_rsa -C "$LOGUSER@$HOSTNAME"
103 cat $sshdir/id_rsa.pub >> $sshdir/authorized_keys
104 }
105
106 [ -f $sshdir/id_ed25519 ] || {
107 ssh-keygen -t ed25519 -N '' -f $sshdir/id_ed25519 -C "$LOGUSER@$HOSTNAME"
108 cat $sshdir/id_ed25519.pub >> $sshdir/authorized_keys
109 }
110
111 [ -f $sshdir/id_ecdsa ] || {
112 ssh-keygen -t ecdsa -N '' -f $sshdir/id_ecdsa -C "$LOGUSER@$HOSTNAME"
113 cat $sshdir/id_ecdsa.pub >> $sshdir/authorized_keys
114 }
115
116 #Adding servers keys to known_hosts list of LOGUSER.
117 #So that installed clients can ssh $LOGUSER@$HOSTNAME without password
118 if [ ! -f $sshdir/known_hosts ]; then
119 [ -f /etc/ssh/ssh_host_ed25519_key.pub ] && ED25519SERVER=$(sed -e "s/= .*$/=/" /etc/ssh/ssh_host_ed25519_key.pub)
120 [ -f /etc/ssh/ssh_host_rsa_key.pub ] && RSASERVER=$(sed -e "s/= .*$/=/" /etc/ssh/ssh_host_rsa_key.pub )
121 [ -f /etc/ssh/ssh_host_ecdsa_key.pub ] && ECDSASERVER=$(sed -e "s/= .*$/=/" /etc/ssh/ssh_host_ecdsa_key.pub )
122 # determine all IP addresses, and their host names
123 ips=$(ip addr show up| grep -P -o '(?<=inet )[^/]+' | grep -v 127.0.0.1)
124 for ip in $ips; do
125 hname=$(getent hosts $ip| tr -s ' ' ',')
126 : ${hname:=$ip}
127 echo "Adding $hname to known_hosts."
128 [ -z "$ED25519SERVER" ] || echo "$hname $ED25519SERVER" >> $sshdir/known_hosts
129 [ -z "$RSASERVER" ] || echo "$hname $RSASERVER" >> $sshdir/known_hosts
130 [ -z "$ECDSASERVER" ] || echo "$hname $ECDSASERVER" >> $sshdir/known_hosts
131 done
132 echo "$sshdir/known_hosts created."
133 else
134 echo "$sshdir/known_hosts remained unchanged."
135 fi
136
137 chmod 0600 $sshdir/authorized_keys
138 echo "$sshdir/authorized_keys created."
139 fi
140 if [ "$FAI_LOGPROTO" = "rsh" ] && [ ! -f $loguserhome/.rhosts ]; then
141 # use .rhosts authentication
142 echo "+@faiclients root" > $loguserhome/.rhosts
143 chmod go-rwx $loguserhome/.rhosts
144 echo "$loguserhome/.rhosts created."
145 fi
146
147 logusergid=$(id -ng $LOGUSER)
148 echo "User account $LOGUSER set up."
149 set -e
150 }
151 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152
153 { # start huge block for capturing output
154
155 if [ -n "$LOGUSER" ]; then
156 add_fai_account
157 setup_fai_account
158 fi
159
160 cp /dev/null /var/log/fai/variables
161 set +e
162 if ! fai-make-nfsroot $options ; then
163 echo "ERROR when calling fai-make-nfsroot."
164 exit 9
165 fi
166 set -e
167
168 if [ -n "$LOGUSER" ]; then
169 # chown only if cd was successful
170 if cd $loguserhome; then
171 [ -e .rhosts ] && chown $LOGUSER:$logusergid . .rhosts || true
172 [ -d .ssh ] && chown -R $LOGUSER:$logusergid .ssh || true
173 fi
174 mkdir -p $TFTPROOT
175 chown -R $LOGUSER:$logusergid $TFTPROOT || true
176 fai-chboot -o default
177 fi
178
179 if [ -z "$FAI_CONFIGDIR" ]; then
180 echo "FAI_CONFIGDIR not set in /etc/fai/nfsroot.conf."
181 exit 7
182 fi
183 mkdir -p $FAI_CONFIGDIR
184
185 # in expert mode, do not export nfs directories
186 if [ -z "$expert" ]; then
187 # by default use the first interface which is up
188 if [ -z "$SERVERINTERFACE" ]; then
189 opt="up"
190 else
191 opt="dev $SERVERINTERFACE"
192 fi
193 nip=$(ip -br ad show $opt | awk '/UP / {if ($3) print $3}' | wc -l)
194 iprange=$(ip -br ad show $opt | awk '/UP / {if ($3) print $3}' | head -1)
195 myip=${iprange%/*}
196 if [ $nip -gt 1 ]; then
197 echo "Warning. More than one network interface found for FAI server."
198 echo "Using IP address $myip. Please set \$SERVERINTERFACE in"
199 echo "nfsroot.conf if you want to use another interface."
200 fi
201 echo "FAI_CONFIGDIR=$FAI_CONFIGDIR" >> /var/log/fai/variables
202 echo "FAI_CONFIG_SRC=nfs://${myip}$FAI_CONFIGDIR" >> /var/log/fai/variables
203
204 add_export_line $FAI_CONFIGDIR "$iprange(async,ro,no_subtree_check)"
205 add_export_line $NFSROOT "$iprange(async,ro,no_subtree_check,no_root_squash)"
206 systemctl reload-or-restart nfs-kernel-server
207 fi
208
209 if [ ! -d $FAI_CONFIGDIR/class ]; then
210 fai-mk-configspace
211 echo "Please don't forget to fill out the FAI questionnaire after you've finished your project with FAI."
212 echo ""
213 fi
214 echo "FAI setup finished."
215 } |& tee /var/log/fai/fai-setup.log
216 RC=${PIPESTATUS[0]}
217 echo "Log file written to /var/log/fai/fai-setup.log"
218 exit ${RC}