| 1 |
#!/bin/bash
|
| 2 |
# $Id$
|
| 3 |
#*********************************************************************
|
| 4 |
#
|
| 5 |
# fai -- main installation script executed after booting
|
| 6 |
#
|
| 7 |
# This script is part of FAI (Fully Automatic Installation)
|
| 8 |
# (c) 1999-2009 by Thomas Lange, lange@informatik.uni-koeln.de
|
| 9 |
# Universitaet zu Koeln
|
| 10 |
# (c) 2001-2005 by Henning Glawe, glaweh@physik.fu-berlin.de
|
| 11 |
# Freie Universitaet Berlin
|
| 12 |
#
|
| 13 |
#*********************************************************************
|
| 14 |
# This program is free software; you can redistribute it and/or modify
|
| 15 |
# it under the terms of the GNU General Public License as published by
|
| 16 |
# the Free Software Foundation; either version 2 of the License, or
|
| 17 |
# (at your option) any later version.
|
| 18 |
#
|
| 19 |
# This program is distributed in the hope that it will be useful, but
|
| 20 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 21 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 22 |
# General Public License for more details.
|
| 23 |
#
|
| 24 |
# A copy of the GNU General Public License is available as
|
| 25 |
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
|
| 26 |
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
|
| 27 |
# can also obtain it by writing to the Free Software Foundation, Inc.,
|
| 28 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
| 29 |
#*********************************************************************
|
| 30 |
|
| 31 |
#set -xv # for full debugging
|
| 32 |
|
| 33 |
export PATH=/usr/local/sbin:/usr/local/bin:/usr/lib/fai:/usr/sbin:/usr/bin:/sbin:/bin
|
| 34 |
# some variables
|
| 35 |
export FAI_VERSION=FAIVERSIONSTRING
|
| 36 |
stamp=/var/run/fai/FAI_INSTALLATION_IN_PROGRESS
|
| 37 |
export romountopt="-o async,noatime,nolock,ro,actimeo=1800"
|
| 38 |
|
| 39 |
export STOP_ON_ERROR=700
|
| 40 |
export faimond=0
|
| 41 |
export renewclass=0
|
| 42 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 43 |
fai_init() {
|
| 44 |
|
| 45 |
set -a # now export all variables
|
| 46 |
set -o pipefail
|
| 47 |
|
| 48 |
umask 022
|
| 49 |
mkdir -p /var/run/fai
|
| 50 |
if [ ! -d "$FAI_ETC_DIR" ]; then
|
| 51 |
echo "$FAI_ETC_DIR is not a directory"
|
| 52 |
exit 6
|
| 53 |
fi
|
| 54 |
[ -f $FAI_ETC_DIR/fai.conf ] && . $FAI_ETC_DIR/fai.conf
|
| 55 |
[ -n "$cspace" ] && FAI_CONFIG_SRC=$cspace
|
| 56 |
unset cspace
|
| 57 |
|
| 58 |
if [ -f /etc/RUNNING_FROM_FAICD ]; then # we are booting from fai cd
|
| 59 |
umount /initrd
|
| 60 |
romountopt=
|
| 61 |
FAI_DEBMIRROR="--bind /media/mirror"
|
| 62 |
MNTPOINT=/media/mirror
|
| 63 |
FAI_CONFIG_SRC="file://$FAI" # on a fai-cd the config space is already available
|
| 64 |
fi
|
| 65 |
|
| 66 |
# read subroutine definitions
|
| 67 |
. /usr/lib/fai/subroutines
|
| 68 |
|
| 69 |
[ -f "$stamp" ] && {
|
| 70 |
echo -n "$0 already running or was aborted before. PID: "
|
| 71 |
cat $stamp
|
| 72 |
echo "You may remove $stamp and try again."
|
| 73 |
exit 1
|
| 74 |
}
|
| 75 |
|
| 76 |
DEBIAN_FRONTEND=noninteractive
|
| 77 |
# local disks are mounted to $FAI_ROOT
|
| 78 |
if [ -z "$FAI_ROOT" ] ; then
|
| 79 |
[ $do_init_tasks -eq 1 ] && FAI_ROOT=/target || FAI_ROOT=/
|
| 80 |
fi
|
| 81 |
# executed command in the environment of the new system
|
| 82 |
ROOTCMD="chroot $FAI_ROOT"
|
| 83 |
# no chroot needed
|
| 84 |
[ "$FAI_ROOT" = '/' ] && ROOTCMD=
|
| 85 |
target=$FAI_ROOT
|
| 86 |
|
| 87 |
if [ $do_init_tasks -eq 1 ]; then
|
| 88 |
trap 'echo "Now rebooting";faireboot' INT QUIT
|
| 89 |
else
|
| 90 |
trap "echo 'Aborted';rm -f $stamp" INT QUIT
|
| 91 |
fi
|
| 92 |
|
| 93 |
if [ $do_init_tasks -eq 1 ]; then
|
| 94 |
eval_cmdline
|
| 95 |
mount -t sysfs sysfs /sys
|
| 96 |
# we really need to start udev
|
| 97 |
[ -x /etc/init.d/udev ] && /etc/init.d/udev start
|
| 98 |
mkdir -p /var/run/network /dev/shm/network # when using initrd kernels
|
| 99 |
ifup lo
|
| 100 |
[ -x /sbin/portmap ] && /sbin/portmap
|
| 101 |
mount -t devpts devpts /dev/pts
|
| 102 |
cat /proc/kmsg >/dev/tty4 &
|
| 103 |
fi
|
| 104 |
|
| 105 |
# since HOSTNAME may change define classes now, so we can call hooks before fai-class is called
|
| 106 |
[ -z "$classes" ] && classes="DEFAULT $(uname -s | tr a-z A-Z) $HOSTNAME LAST"
|
| 107 |
|
| 108 |
prcopyleft
|
| 109 |
|
| 110 |
[ $do_init_tasks -eq 1 ] && save_dmesg
|
| 111 |
}
|
| 112 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 113 |
usage() {
|
| 114 |
cat <<-EOF
|
| 115 |
fai $FAI_VERSION. Copyright (C) 1999-2009 Thomas Lange
|
| 116 |
Usage: $0 [options] [action]
|
| 117 |
|
| 118 |
Options:
|
| 119 |
-v|--verbose display more information during the update
|
| 120 |
-h|--help display this help message
|
| 121 |
-N|--new renew list of classes
|
| 122 |
-c|--class comma separated list of classes
|
| 123 |
-C|--cfdir CFDIR Use CFDIR for reading the config files
|
| 124 |
-s|--cspace CSDIR URI of the configuration space
|
| 125 |
-u|--hostname HNAME set hostname to be used
|
| 126 |
|
| 127 |
EOF
|
| 128 |
exit 0
|
| 129 |
}
|
| 130 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 131 |
fstart() {
|
| 132 |
|
| 133 |
# these tasks can define variables, that are needed later
|
| 134 |
[ -n "$etc_message" ] && echo ""
|
| 135 |
echo "$etc_message"
|
| 136 |
[ $do_init_tasks -eq 1 ] || echo "Using configuration files from $FAI_ETC_DIR"
|
| 137 |
unset etc_message
|
| 138 |
task confdir
|
| 139 |
# if the config space is a local directory, reset $FAI
|
| 140 |
local method=$(expr match "$FAI_CONFIG_SRC" '\([^+]*\).*://')
|
| 141 |
if [ $method = "file" ]; then
|
| 142 |
local localpath=$(expr match "$FAI_CONFIG_SRC" '.*://\(/.*\)')
|
| 143 |
export FAI=$localpath
|
| 144 |
fi
|
| 145 |
task setup
|
| 146 |
task defclass
|
| 147 |
unset cmdlineclasses renewclass
|
| 148 |
[ $do_init_tasks -eq 1 ] && set_disk_info
|
| 149 |
task defvar
|
| 150 |
[ $do_init_tasks -eq 1 ] && load_keymap_consolechars
|
| 151 |
}
|
| 152 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 153 |
# Main routine
|
| 154 |
|
| 155 |
# Parse commandline options
|
| 156 |
TEMP=$(getopt -o s:u:Nhvc:C: --long cspace:,hostname:,new,help,verbose,class:,cfdir: -n "$0" -- "$@")
|
| 157 |
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
|
| 158 |
# Note the quotes around `$TEMP': they are essential!
|
| 159 |
eval set -- "$TEMP"
|
| 160 |
unset TEMP
|
| 161 |
|
| 162 |
while true ; do
|
| 163 |
case "$1" in
|
| 164 |
-h|--help)
|
| 165 |
shift
|
| 166 |
usage
|
| 167 |
;;
|
| 168 |
-v|--verbose)
|
| 169 |
shift
|
| 170 |
export verbose=1
|
| 171 |
;;
|
| 172 |
-N|--new)
|
| 173 |
shift
|
| 174 |
renewclass=1
|
| 175 |
;;
|
| 176 |
-C|--cfdir)
|
| 177 |
shift
|
| 178 |
cfdir=$1
|
| 179 |
shift
|
| 180 |
;;
|
| 181 |
-c|--class)
|
| 182 |
|
| 183 |
if [ $renewclass -eq 1 ]; then
|
| 184 |
echo "You can't use -c|--classes and -N|--new at the same time."
|
| 185 |
exit 9
|
| 186 |
fi
|
| 187 |
shift
|
| 188 |
export cmdlineclasses=$1
|
| 189 |
shift
|
| 190 |
cmdlineclasses=${cmdlineclasses//,/ }
|
| 191 |
;;
|
| 192 |
-s|--cspace)
|
| 193 |
shift
|
| 194 |
cspace=$1
|
| 195 |
shift
|
| 196 |
;;
|
| 197 |
-u|--hostname)
|
| 198 |
shift
|
| 199 |
export newhostname=$1
|
| 200 |
shift
|
| 201 |
;;
|
| 202 |
--)
|
| 203 |
shift
|
| 204 |
break
|
| 205 |
;;
|
| 206 |
*)
|
| 207 |
echo "$0: command line parsing error ! $@"
|
| 208 |
exit 1
|
| 209 |
;;
|
| 210 |
esac
|
| 211 |
done
|
| 212 |
|
| 213 |
# use FAI_ETC_DIR from environment variable
|
| 214 |
if [ -n "$FAI_ETC_DIR" -a -z "$cfdir" ]; then
|
| 215 |
# print this message later so it gets into the log files
|
| 216 |
etc_message="Using environment variable \$FAI_ETC_DIR."
|
| 217 |
fi
|
| 218 |
[ -n "$cfdir" ] && FAI_ETC_DIR=$cfdir
|
| 219 |
unset cfdir
|
| 220 |
: ${FAI_ETC_DIR:=/etc/fai}
|
| 221 |
FAI_ETC_DIR=$(readlink -f $FAI_ETC_DIR) # canonicalize path
|
| 222 |
export FAI_ETC_DIR
|
| 223 |
|
| 224 |
# override FAI_ACTION later if a command line argument is given
|
| 225 |
[ "$1" ] && export action=$1
|
| 226 |
[ "$2" ] && export FAI_ROOT=$2 # only used for dirinstall
|
| 227 |
|
| 228 |
if [ X$action = Xdirinstall ]; then
|
| 229 |
if [ -z "$FAI_ROOT" ]; then
|
| 230 |
echo "Please specify a target directory. Aborted"
|
| 231 |
exit 3
|
| 232 |
fi
|
| 233 |
if [ $renewclass -eq 0 -a -z "$cmdlineclasses" ]; then
|
| 234 |
echo "Please use -c or -N. Aborted"
|
| 235 |
exit 4
|
| 236 |
fi
|
| 237 |
|
| 238 |
# two lines taken from task_dirinstall
|
| 239 |
mkdir -p $FAI_ROOT
|
| 240 |
FAI_ROOT=$(cd $FAI_ROOT;pwd)
|
| 241 |
|
| 242 |
# check if target directory is mounted with bad options
|
| 243 |
fs=$(df $FAI_ROOT | tail -1 | awk '{print $6}')
|
| 244 |
if mount | grep "on $fs " | awk '{print $6}' | egrep -q "nosuid|nodev"; then
|
| 245 |
echo "Target directory is mounted using nosuid or nodev. Aborting"
|
| 246 |
exit 5
|
| 247 |
fi
|
| 248 |
unset fs
|
| 249 |
|
| 250 |
if [ ! -e $FAI_ETC_DIR/make-fai-nfsroot.conf ]; then
|
| 251 |
echo "$FAI_ETC_DIR/make-fai-nfsroot.conf not found."
|
| 252 |
echo "You may want to install the package fai-server"
|
| 253 |
exit 7
|
| 254 |
fi
|
| 255 |
export NFSROOT=$(source $FAI_ETC_DIR/make-fai-nfsroot.conf; echo $NFSROOT)
|
| 256 |
export FAI_DEBOOTSTRAP=$(source $FAI_ETC_DIR/make-fai-nfsroot.conf; echo $FAI_DEBOOTSTRAP)
|
| 257 |
export FAI_DEBOOTSTRAP_OPTS=$(source $FAI_ETC_DIR/make-fai-nfsroot.conf; echo $FAI_DEBOOTSTRAP_OPTS)
|
| 258 |
|
| 259 |
fi
|
| 260 |
|
| 261 |
if [ $(id -u) != "0" ]; then
|
| 262 |
echo "Run this program as root."
|
| 263 |
exit 1
|
| 264 |
fi
|
| 265 |
|
| 266 |
# exit if we do not run from nfsroot and no parameter is given
|
| 267 |
if [ ! -f /.THIS_IS_THE_FAI_NFSROOT -a "X$1" = "X" ]; then
|
| 268 |
echo "Please give more parameters if not run from the nfsroot."
|
| 269 |
exit 2
|
| 270 |
fi
|
| 271 |
|
| 272 |
# are we called as an init substitute ?
|
| 273 |
export do_init_tasks=0
|
| 274 |
[ "$0" = "/etc/init.d/rcS" ] && do_init_tasks=1
|
| 275 |
if [ $do_init_tasks -eq 1 ]; then
|
| 276 |
hostname $HOSTNAME
|
| 277 |
renewclass=1 # always renew class list when installing
|
| 278 |
mkdir -p /var/lib/discover /var/discover /etc/sysconfig
|
| 279 |
fi
|
| 280 |
|
| 281 |
[ -f /proc/version ] || mount -n -t proc proc /proc # ubuntu initrd does not mount /proc
|
| 282 |
export start_seconds=$(cut -d . -f 1 /proc/uptime)
|
| 283 |
|
| 284 |
[ -n "$newhostname" ] && export HOSTNAME=$newhostname
|
| 285 |
|
| 286 |
if [ $do_init_tasks -eq 1 ]; then
|
| 287 |
# we are running an initial installation
|
| 288 |
export LOGDIR=/tmp/fai
|
| 289 |
mkdir -p $LOGDIR
|
| 290 |
else
|
| 291 |
export fai_rundate=$(date +'%Y%m%d_%H%M%S')
|
| 292 |
export LOGDIR=/var/log/fai/$HOSTNAME/$action-$fai_rundate
|
| 293 |
mkdir -p $LOGDIR
|
| 294 |
ln -snf $action-$fai_rundate $LOGDIR/../last-$action
|
| 295 |
ln -snf $action-$fai_rundate $LOGDIR/../last
|
| 296 |
fi
|
| 297 |
chown root $LOGDIR
|
| 298 |
chgrp adm $LOGDIR
|
| 299 |
chmod 0750 $LOGDIR
|
| 300 |
|
| 301 |
fai_init
|
| 302 |
[ -n "$newhostname" ] && echo "Hostname set to $HOSTNAME" | tee -a $LOGDIR/fai.log
|
| 303 |
unset newhostname
|
| 304 |
|
| 305 |
# remove this crap, use old thing
|
| 306 |
mkfifo $LOGDIR/logfifo
|
| 307 |
tee -a $LOGDIR/fai.log < $LOGDIR/logfifo &
|
| 308 |
# in bash &> redirect stdout and stderr to file
|
| 309 |
fstart &> $LOGDIR/logfifo
|
| 310 |
rm $LOGDIR/logfifo
|
| 311 |
sleep 1 # wait for tee to complete. One second should be ok
|
| 312 |
# old code
|
| 313 |
# {
|
| 314 |
# # a bash group command with { does not work on sparc
|
| 315 |
# task confdir
|
| 316 |
# task setup
|
| 317 |
# task defclass
|
| 318 |
# task defvar
|
| 319 |
# load_keymap_consolechars
|
| 320 |
# set_disk_info
|
| 321 |
# } > >( tee -a $LOGDIR/fai.log ) 2>&1
|
| 322 |
|
| 323 |
[ "$action" ] && export FAI_ACTION=$action
|
| 324 |
unset action
|
| 325 |
task action 2>&1 | tee -a $LOGDIR/fai.log
|
| 326 |
|
| 327 |
[ -L "/var/run/fai/current_config" ] && rm -f "/var/run/fai/current_config"
|
| 328 |
|
| 329 |
echo "End of $0"
|