File: fai

package info (click to toggle)
fai 6.5.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,080 kB
  • sloc: sh: 6,721; perl: 5,625; makefile: 138
file content (398 lines) | stat: -rwxr-xr-x 12,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/bin/bash

#*********************************************************************
#
# fai -- main installation script executed after booting
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 1999-2026 by Thomas Lange, lange@cs.uni-koeln.de
# Universitaet zu Koeln
# (c) 2001-2005 by Henning Glawe, glaweh@physik.fu-berlin.de
# Freie Universitaet Berlin
#
#*********************************************************************
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#*********************************************************************

#set -xv # for full debugging

export PATH=/usr/local/sbin:/usr/local/bin:/usr/lib/fai:/usr/sbin:/usr/bin:/sbin:/bin
# some variables
export FAI_VERSION="FAIVERSIONSTRING"
stamp=/var/run/fai/FAI_INSTALLATION_IN_PROGRESS
export romountopt=${romountopt:-"-o async,noatime,nolock,ro,actimeo=1800"}

[ -n "$STOP_ON_ERROR" ] || export STOP_ON_ERROR=700
export faimond=0
export renewclass=0
export task_error=0 # tasks can set this variable to indicate an error
final_exit_code=0

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fdie() {

    local e=$1   # first parameter is the exit code
    shift

    echo "$*" >&2 # print error message
    exit $e
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mirrorvars() {

    # set variables if dir exists (on USB, CD)
    local dir=$1

    if [ -d "$1" ]; then
            romountopt=
            FAI_DEBMIRROR="--bind $1"
            MNTPOINT=/media/mirror
            return 0
    fi
    return 1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fai_init() {

    set -a # now export all variables
    set -o pipefail

    umask 022
    . /usr/lib/fai/subroutines

    mkdir -p /var/run/fai
    echo 0 > $LOGDIR/task_error
    [ -f $FAI_ETC_DIR/fai.conf ] && . $FAI_ETC_DIR/fai.conf
    : ${FAI:=/var/lib/fai/config} # default value
    : ${MNTPOINT:=/media/mirror}  # default value
    : ${FAI_LOGPROTO:=ssh}        # default value
    [ -n "$cspace" ] && FAI_CONFIG_SRC=$cspace
    unset cspace

    [ -f "$stamp" ] && {
       echo -n "$0 already running or was aborted before. PID: " >&2
       cat $stamp >&2
       fdie 1 "You may remove $stamp and try again." >&2
    }

    DEBIAN_FRONTEND=noninteractive
    # local disks are mounted to $FAI_ROOT
    if [ -z "$FAI_ROOT" ] ; then
      inside_nfsroot && FAI_ROOT=/target || FAI_ROOT=/
    fi
    # ROOTCMD will execute a command in the environment of the new system
    # if ROOTCMD was set from outside, do not touch it
    if [ -z "$ROOTCMD" ]; then
        # check if unshare can be used (e.g. not possible in some container environments)
        if unshare --fork --kill-child --mount-proc chroot / ls >/dev/null 2>&1 ; then
            ROOTCMD="unshare --fork --kill-child --mount-proc chroot $FAI_ROOT"
        else
            ROOTCMD="chroot $FAI_ROOT"
        fi
    fi
    # no chroot needed
    [ "$FAI_ROOT" = '/' ] && ROOTCMD=
    target=$FAI_ROOT
    AINSL_TARGET=$FAI_ROOT
    FAI_RAMDISKS="$target/var/lib/dpkg"

    if inside_nfsroot; then
        trap 'echo "Now rebooting";faireboot' INT QUIT
    else
        trap "echo 'Aborted';rm -f $stamp" INT QUIT
    fi

    if inside_nfsroot; then
        eval_cmdline
        define_fai_flags
        rm -f /run/nologin /etc/motd
        mkdir -p /var/run/sshd /var/run/network # when using initrd kernels
        ifup lo
        [ -x /sbin/portmap ] && /sbin/portmap
	if [ -x /sbin/rpcbind ]; then
	    pgrep rpcbind >/dev/null || /sbin/rpcbind
	fi
        pgrep rpc.statd > /dev/null || rpc.statd # idmapd needs this

        cat /proc/kmsg >/dev/tty4 &

        # fix IP address lifetime
	ip -4 -br a | awk '/UP / {if ($3) print $3 " " $1}' | \
        while read addr iface; do
            ip -4 addr change "$addr" dev "$iface" valid_lft forever preferred_lft forever
        done

        # enable EFI variables
        if [ -d /sys/firmware/efi ]; then
           mount -t efivarfs none /sys/firmware/efi/efivars
        fi

        # start secure shell daemon for remote access
        [ "$flag_sshd" ] && [ -x /usr/sbin/sshd ] && /usr/sbin/sshd
	save_dmesg
    fi

    # since HOSTNAME may change define classes now, so we can call hooks before fai-class is called
    [ -z "$classes" ] && classes="DEFAULT $(uname -s | tr a-z A-Z) $HOSTNAME LAST"

    prcopyleft
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
inside_nfsroot() {

    # check if fai is using the nfsroot as root filesystem
    if [ $do_init_tasks -eq 1 ];then
       return 0
    else
        return 1
    fi
}
export -f inside_nfsroot

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
usage() {
    cat <<-EOF
        fai $FAI_VERSION. Copyright (C) 1999-2026 Thomas Lange
        Usage: $0 [options] [action]

        Options:
           -v|--verbose         display more information during the update
           -h|--help            display this help message
           -N|--new             renew list of classes
           -c|--class           comma separated list of classes
           -C|--cfdir CFDIR     Use CFDIR for  reading the config files
           -s|--cspace CSDIR    URI of the configuration space
           -u|--hostname HNAME  set hostname to be used

EOF
    exit 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fstart() {

    # these tasks can define variables, that are needed later
    [ -n "$etc_message" ] && echo ""
    echo "$etc_message"
    inside_nfsroot || echo "Using configuration files from $FAI_ETC_DIR"
    unset etc_message

    mount-fai-data
    # use mirror on partition MY-DATA or on ISO
    mirrorvars /media/data/mirror ||
        mirrorvars /media/mirror
    task confdir
    # if the config space is a local directory, reset $FAI
    local method=$(expr match "$FAI_CONFIG_SRC" '\([^+]*\).*://')
    if [ $method = "file" ]; then
        export FAI=$(expr match "$FAI_CONFIG_SRC" '.*://\(/.*\)')
    fi
    unset method
    task setup
    task defclass
    unset renewclass
    inside_nfsroot && set_disk_info
    task defvar
    inside_nfsroot && load_keymap_consolechars
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Main routine

# Parse commandline options
TEMP=$(getopt -o s:u:Nhvc:C: --long cspace:,hostname:,new,help,verbose,class:,cfdir: -n "$0" -- "$@")
if [ $? != 0 ] ; then fdie 6 "Wrong option. Terminating." >&2 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
unset TEMP

while true ; do
    case "$1" in
        -h|--help)
            usage ;;
        -v|--verbose)
            export verbose=1
            shift ;;
        -N|--new)
            renewclass=1
            shift ;;
        -C|--cfdir)
            cfdir=$2
            shift 2 ;;
        -c|--class)
            export cmdlineclasses=$2
            cmdlineclasses=${cmdlineclasses//,/ }
            export classes=$cmdlineclasses
            shift 2 ;;
        -s|--cspace)
            cspace=$2
            shift 2 ;;
        -u|--hostname)
            export newhostname=$2
            shift 2 ;;
        --)
            shift
            break ;;
         *)
            fdie 1 "$0: command line parsing error ! $*" >&2 ;;
    esac
done

# use FAI_ETC_DIR from environment variable
if [ -n "$FAI_ETC_DIR" ] &&  [ -z "$cfdir" ]; then
    # print this message later so it gets into the log files
    etc_message="Using environment variable \$FAI_ETC_DIR."
fi
[ -n "$cfdir" ] && FAI_ETC_DIR=$cfdir
unset cfdir
: ${FAI_ETC_DIR:=/etc/fai}
FAI_ETC_DIR=$(readlink -f $FAI_ETC_DIR) # canonicalize path
export FAI_ETC_DIR

# override FAI_ACTION later if a command line argument is given
[ "$1" ] && export FAI_ACTION=$1
[ "$2" ] && export FAI_ROOT=$2 # only used for dirinstall

if [ $(id -u) != "0" ]; then
    fdie 1 "Run this program as root." >&2
fi

export FAI_RUNDIR=$PWD
export start_seconds=$(cut -d . -f 1 /proc/uptime)

if [ X$FAI_ACTION = Xdirinstall ]; then
    if [ -z "$FAI_ROOT" ]; then
        fdie 3 "Please specify a target directory. Aborted" >&2
    fi
    if [ $renewclass -eq 0 ] && [ -z "$cmdlineclasses" ]; then
        fdie 4 "Please use -c or -N. Aborted" >&2
    fi

    # two lines taken from task_dirinstall
    mkdir -p $FAI_ROOT
    FAI_ROOT=$(cd $FAI_ROOT;pwd)

    # check if target directory is mounted with bad options
    fs=$(df -P $FAI_ROOT | tail -1 | awk '{print $6}')
    if mount | grep "on $fs " |  awk '{print $6}' | grep -E -q "nosuid|nodev"; then
        fdie 5 "Target directory is mounted using nosuid or nodev. Aborting" >&2
    fi
    unset fs

    if [ ! -e $FAI_ETC_DIR/nfsroot.conf ]; then
        echo "$FAI_ETC_DIR/nfsroot.conf not found." >&2
        fdie 7 "You may want to install the package fai-server" >&2
    fi
    export NFSROOT=$(source $FAI_ETC_DIR/nfsroot.conf; echo $NFSROOT)
    export FAI_DEBOOTSTRAP=$(source $FAI_ETC_DIR/nfsroot.conf; echo $FAI_DEBOOTSTRAP)
    export FAI_DEBOOTSTRAP_OPTS=$(source $FAI_ETC_DIR/nfsroot.conf; echo $FAI_DEBOOTSTRAP_OPTS)

fi

# exit if we do not run from nfsroot and no parameter is given
if [ ! -f /.THIS_IS_THE_FAI_NFSROOT ] && [ "X$1" = "X" ]; then
    fdie 2 "Please give more parameters if not run from the nfsroot." >&2
fi


# set variable so we can use the funcion inside_nfsroot
export do_init_tasks=0
[ -f /.THIS_IS_THE_FAI_NFSROOT ] && do_init_tasks=1

# Do we run inside the nfsroot?
if inside_nfsroot; then

    # if hostname was set on the kernel command line (mostly when booting from CD)
    for word in $(< /proc/cmdline) ; do
        case $word in
            hostname=*)
            HOSTNAME=${word#*hostname=}
            ;;
        esac
    done
    unset word
    if [ -n "$HOSTNAME" ]; then
	echo $HOSTNAME > /proc/sys/kernel/hostname
	export HOSTNAME
    fi

    # if the host name is still not set, use the IP for setting the hostname
    read hname < /proc/sys/kernel/hostname
    if [ -z "$HOSTNAME" -o $HOSTNAME = '(none)' -o -z "$hname" ]; then
	_nic=$(ip route | awk '/^default/ {print $5}'|head -1)
	myip=$(ip -4 -br ad sh $_nic | awk '{print $3}')
	myip=${myip%/*}
	myip=${myip//./-}
	if [ -n "$myip" ]; then
	    export HOSTNAME="ip-$myip"
	else
	    export HOSTNAME=debian
	fi
	echo $HOSTNAME > /proc/sys/kernel/hostname
	echo "Setting the host name: $HOSTNAME"
    fi
    if [ ! -f /etc/hostname ]; then
        echo $HOSTNAME > /etc/hostname
    fi
    unset hname _nic myip

    renewclass=1 # always renew class list when installing
fi

[ -n "$newhostname" ] && export HOSTNAME=$newhostname

if inside_nfsroot; then
    # we are running an initial installation
    export LOGDIR=/tmp/fai
    mkdir -p $LOGDIR
else
    # this is for dirinstall, softupdate, ..
    export fai_rundate=$(date +'%Y%m%d_%H%M%S')
    export LOGDIR=/var/log/fai/$HOSTNAME/$FAI_ACTION-$fai_rundate
    if [ -d $LOGDIR ]; then
	fdie 13 "$LOGDIR already exists. Maybe another process is running. Aborting"
    fi
    mkdir -p $LOGDIR
    ln -snf $FAI_ACTION-$fai_rundate $LOGDIR/../last-$FAI_ACTION
    ln -snf $FAI_ACTION-$fai_rundate $LOGDIR/../last
fi
chown root:adm $LOGDIR
chmod 0750 $LOGDIR

fai_init

if ! inside_nfsroot; then
    echo "Starting FAI execution - $fai_rundate" | tee -a $LOGDIR/fai.log
fi
[ -n "$newhostname" ] && echo "Hostname set to $HOSTNAME" | tee -a $LOGDIR/fai.log
unset newhostname

fstart > >( tee -a $LOGDIR/fai.log )  2>&1

task action |& tee -a $LOGDIR/fai.log
if [ ${PIPESTATUS[0]} -gt 0 ]; then
    final_exit_code=1
fi
if [ -f $LOGDIR/task_error ]; then
    task_error=$(< $LOGDIR/task_error)
    if [ $task_error -gt 0 ]; then
	final_exit_code=1
    fi
fi


[ -L "/var/run/fai/current_config" ] && rm -f "/var/run/fai/current_config"

exit $final_exit_code