| 1 |
#! /bin/bash
|
| 2 |
|
| 3 |
# $Id$
|
| 4 |
#*********************************************************************
|
| 5 |
#
|
| 6 |
# fai-setup -- set up FAI
|
| 7 |
#
|
| 8 |
# This script is part of FAI (Fully Automatic Installation)
|
| 9 |
# (c) 2000-2007 by Thomas Lange, lange@informatik.uni-koeln.de
|
| 10 |
# Universitaet zu Koeln
|
| 11 |
#
|
| 12 |
#*********************************************************************
|
| 13 |
# This program is free software; you can redistribute it and/or modify
|
| 14 |
# it under the terms of the GNU General Public License as published by
|
| 15 |
# the Free Software Foundation; either version 2 of the License, or
|
| 16 |
# (at your option) any later version.
|
| 17 |
#
|
| 18 |
# This program is distributed in the hope that it will be useful, but
|
| 19 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 20 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 21 |
# General Public License for more details.
|
| 22 |
#
|
| 23 |
# A copy of the GNU General Public License is available as
|
| 24 |
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
|
| 25 |
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
|
| 26 |
# can also obtain it by writing to the Free Software Foundation, Inc.,
|
| 27 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
| 28 |
#*********************************************************************
|
| 29 |
|
| 30 |
PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin
|
| 31 |
|
| 32 |
set -e
|
| 33 |
. /etc/fai/fai.conf
|
| 34 |
. /etc/fai/make-fai-nfsroot.conf
|
| 35 |
|
| 36 |
options=$@ # all options are also passed to make-fai-nfsroot
|
| 37 |
|
| 38 |
# look for option -e (expert mode)
|
| 39 |
case "$options" in
|
| 40 |
*-e*) expert=1 ;;
|
| 41 |
esac
|
| 42 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 43 |
add_export_line() {
|
| 44 |
|
| 45 |
# add a line to /etc/exports
|
| 46 |
|
| 47 |
pattern=$1
|
| 48 |
shift
|
| 49 |
options="$@"
|
| 50 |
|
| 51 |
[ -f /etc/exports ] && grep -q "^$pattern[[:space:]]" /etc/exports && return
|
| 52 |
echo "Adding line to /etc/exports: $pattern $options"
|
| 53 |
echo "$pattern $options" >> /etc/exports
|
| 54 |
}
|
| 55 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 56 |
add_fai_account() {
|
| 57 |
|
| 58 |
if id $LOGUSER 2>/dev/null 1>&2 ; then
|
| 59 |
echo Account \$LOGUSER=$LOGUSER already exists.
|
| 60 |
echo Make sure that all install clients can
|
| 61 |
echo log into this account without a password.
|
| 62 |
return
|
| 63 |
fi
|
| 64 |
|
| 65 |
adduser --system --disabled-password --home /var/log/fai --gecos "FAI account for log files" $LOGUSER
|
| 66 |
# there's a bug when the encrypted password is !, so change it to *
|
| 67 |
perl -pi.bak -e "s#^$LOGUSER:.:(.+):/bin/false#$LOGUSER:*:\1:/bin/bash#" /etc/passwd
|
| 68 |
# get the home dir of a user in a variable; do not exit when set -e is used
|
| 69 |
# loguserhome is unset if $LOGUSER does not exists
|
| 70 |
# this is not a evil hack, it's a very clever piece of code
|
| 71 |
loguserhome=$(eval "cd ~$LOGUSER 2>/dev/null && pwd;true")
|
| 72 |
touch $loguserhome/.account_created_by_fai_package
|
| 73 |
}
|
| 74 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 75 |
setup_fai_account() {
|
| 76 |
|
| 77 |
# most things should be executed as user $LOGUSER, since root may not have write
|
| 78 |
# permissions to $loguserhome (e.g if mount via NFS without no_root_squash)
|
| 79 |
|
| 80 |
set +e
|
| 81 |
loguserhome=$(eval "cd ~$LOGUSER 2>/dev/null && pwd;true")
|
| 82 |
sshdir=$loguserhome/.ssh
|
| 83 |
if [ -z "$loguserhome" ]; then
|
| 84 |
echo "Can't determine home directory for user $LOGUSER."
|
| 85 |
echo "LOGUSER= $LOGUSER loguserhome= $loguserhome"
|
| 86 |
exit 8
|
| 87 |
fi
|
| 88 |
|
| 89 |
if [ "$FAI_LOGPROTO" = "ssh" ]; then
|
| 90 |
# set up ssh on the server
|
| 91 |
mkdir -p -m 700 $loguserhome/.ssh
|
| 92 |
|
| 93 |
#Generating keys for LOGUSER
|
| 94 |
[ -f $sshdir/id_rsa ] || {
|
| 95 |
ssh-keygen -t rsa -N '' -f $sshdir/id_rsa -C "$LOGUSER@$HOSTNAME"
|
| 96 |
cat $sshdir/id_rsa.pub >> $sshdir/authorized_keys
|
| 97 |
}
|
| 98 |
|
| 99 |
[ -f $sshdir/id_dsa ] || {
|
| 100 |
ssh-keygen -t dsa -N '' -f $sshdir/id_dsa -C "$LOGUSER@$HOSTNAME"
|
| 101 |
cat $sshdir/id_dsa.pub >> $sshdir/authorized_keys
|
| 102 |
}
|
| 103 |
|
| 104 |
#Adding servers keys to known_hosts list of LOGUSER.
|
| 105 |
#So that installed clients can ssh $LOGUSER@$HOSTNAME without password
|
| 106 |
if [ ! -f $sshdir/known_hosts ]; then
|
| 107 |
[ -f /etc/ssh/ssh_host_dsa_key.pub ] && DSASERVER=$(sed -e "s/= .*$/=/" /etc/ssh/ssh_host_dsa_key.pub)
|
| 108 |
[ -f /etc/ssh/ssh_host_rsa_key.pub ] && RSASERVER=$(sed -e "s/= .*$/=/" /etc/ssh/ssh_host_rsa_key.pub )
|
| 109 |
# determine all IP addresses, and their host names
|
| 110 |
ips=$(LC_ALL=C ifconfig| perl -ne '/addr:([\d.]+)/ && print"$1\n"'|grep -v 127.0.0.1)
|
| 111 |
for ip in $ips; do
|
| 112 |
hname=$(getent hosts $ip| tr -s ' ' ',')
|
| 113 |
[ -z "$hname" ] && hname=$ip
|
| 114 |
echo "Adding $hname to known_hosts."
|
| 115 |
[ -z "$DSASERVER" ] || echo "$hname $DSASERVER" >> $sshdir/known_hosts
|
| 116 |
[ -z "$RSASERVER" ] || echo "$hname $RSASERVER" >> $sshdir/known_hosts
|
| 117 |
done
|
| 118 |
echo "$sshdir/known_hosts created."
|
| 119 |
else
|
| 120 |
echo "$sshdir/known_hosts remained unchanged."
|
| 121 |
fi
|
| 122 |
|
| 123 |
chmod 0700 $sshdir/authorized_keys
|
| 124 |
echo "$sshdir/authorized_keys created."
|
| 125 |
fi
|
| 126 |
if [ "$FAI_LOGPROTO" = "rsh" -a ! -f $loguserhome/.rhosts ]; then
|
| 127 |
# use .rhosts authentication
|
| 128 |
echo "+@faiclients root" > $loguserhome/.rhosts
|
| 129 |
chmod go-rwx $loguserhome/.rhosts
|
| 130 |
echo "$loguserhome/.rhosts created."
|
| 131 |
fi
|
| 132 |
|
| 133 |
logusergid=$(id -ng $LOGUSER)
|
| 134 |
echo "User account $LOGUSER set up."
|
| 135 |
set -e
|
| 136 |
}
|
| 137 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 138 |
|
| 139 |
if [ -n "$LOGUSER" ]; then
|
| 140 |
add_fai_account
|
| 141 |
setup_fai_account
|
| 142 |
fi
|
| 143 |
|
| 144 |
make-fai-nfsroot $options
|
| 145 |
|
| 146 |
if [ -n "$LOGUSER" ]; then
|
| 147 |
# chown only if cd was successful
|
| 148 |
cd $loguserhome
|
| 149 |
if [ $? -eq 0 ]; then
|
| 150 |
chown $LOGUSER:$logusergid . .rhosts || true
|
| 151 |
[ -d .ssh ] && chown -R $LOGUSER:$logusergid .ssh || true
|
| 152 |
fi
|
| 153 |
mkdir -p $TFTPROOT
|
| 154 |
chown -R $LOGUSER:$logusergid $TFTPROOT || true
|
| 155 |
fi
|
| 156 |
|
| 157 |
if [ -z "$FAI_CONFIGDIR" ]; then
|
| 158 |
echo "FAI_CONFIGDIR not set in /etc/fai/fai.conf."
|
| 159 |
echo "Using /srv/fai/config as default."
|
| 160 |
FAI_CONFIGDIR=/srv/fai/config
|
| 161 |
fi
|
| 162 |
mkdir -p $FAI_CONFIGDIR
|
| 163 |
|
| 164 |
# in expert mode, do not export nfs directories
|
| 165 |
if [ -z "$expert" ]; then
|
| 166 |
test -f /etc/init.d/nfs-kernel-server && nfsserver=nfs-kernel-server
|
| 167 |
test -f /etc/init.d/nfs-user-server && nfsserver=nfs-user-server
|
| 168 |
|
| 169 |
addr=$(LC_ALL=C ifconfig $SERVERINTERFACE | grep -v 127.0.0.1 | perl -ne '/addr:([\d.]+)/ && print"$1\n"'|head -1)
|
| 170 |
mask=$(LC_ALL=C ifconfig $SERVERINTERFACE | grep -v 127.0.0.1 | perl -ne '/Mask:([\d.]+)/ && print"$1\n"'|head -1)
|
| 171 |
add_export_line $FAI_CONFIGDIR "$addr/$mask(async,ro,no_subtree_check)"
|
| 172 |
add_export_line $NFSROOT "$addr/$mask(async,ro,no_subtree_check,no_root_squash)"
|
| 173 |
if [ -z "$nfsserver" ]; then
|
| 174 |
echo "Could not find the type of your nfs server. Maybe"
|
| 175 |
echo "no nfs server is installed. I can't restart it."
|
| 176 |
else
|
| 177 |
/etc/init.d/$nfsserver reload
|
| 178 |
fi
|
| 179 |
fi
|
| 180 |
|
| 181 |
if [ ! -d $FAI_CONFIGDIR/class ]; then
|
| 182 |
echo ""
|
| 183 |
echo " You have no FAI configuration space yet. Copy the simple examples with:"
|
| 184 |
echo " cp -a /usr/share/doc/fai-doc/examples/simple/* $FAI_CONFIGDIR"
|
| 185 |
echo " Then change the configuration files to meet your local needs."
|
| 186 |
echo ""
|
| 187 |
fi
|
| 188 |
echo "FAI setup finished."
|