Debian Codegraph @

bin/fai-make-nfsroot

raw symbols SCIP
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # fai-make-nfsroot -- create nfsroot directory and add additional packages
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 # (c) 2004 by Henning Glawe, [email protected]
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 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32 usage() {
33
34 cat <<-EOF
35 Copyright (C) 1999-2026 Thomas Lange
36
37 Usage: fai-make-nfsroot [OPTIONS]
38 Create an NFSROOT for FAI.
39 Read the man pages pages fai-make-nfsroot(8).
40 EOF
41 exit 0
42 }
43 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44 die() {
45
46 local e=$1 # first parameter is the exit code
47 shift
48
49 echo "ERROR: $*"
50 echo "Log file written to /var/log/fai/fai-make-nfsroot.log"
51 exit $e
52 }
53 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54 check_nfsroot() {
55
56 local lname
57 set +e
58 # simple test, to see if important thing are available inside the nfsroot
59 if [ ! -x $NFSROOT/usr/bin/dracut ]; then
60 die 1 "dracut was not installed into the nfsroot."
61 fi
62
63 echo "FAI packages and related packages inside the nfsroot:"
64 $ROOTCMD dpkg-query -W -f='${Package;-18} ${Version}\n' fai-client fai-nfsroot fai-setup-storage dracut-network dracut dracut-live dracut-squash 2>/dev/null
65
66 # check if all important packages are installed
67 n=$($ROOTCMD dpkg-query -l fai-client fai-nfsroot fai-setup-storage dracut-network dracut 2>/dev/null|grep -c -E ^ii)
68 if [ $n -ne 5 ]; then
69 echo "ERROR: Some essential packages are missing."
70 bad_exit
71 fi
72
73 lname=$(readlink $NFSROOT/sbin/init)
74 if ! [[ "$lname" =~ "lib/systemd/systemd" ]]; then
75 echo "ERROR: It seems that you did not installed systemd into the nfsroot."
76 echo "Make sure this old config line does NOT appear in /etc/fai/NFSROOT:"
77 echo "sysvinit-core systemd-sysv-"
78 bad_exit
79 fi
80
81 local files=$(ls $NFSROOT/boot/initrd* 2>/dev/null)
82 [ -z "$files" ] && die 1 "No initrd installed."
83 grep -E -q "^ERROR: |^E: Sub-process |^dpkg: error processing |^dpkg: dependency problems" /var/log/fai/fai-make-nfsroot.log && bad_exit
84 return 0
85 }
86 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87
88 PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin
89
90 merror="properly"
91 exitcode=0
92 sshpreserve=0
93 adjust=0
94 generic=0
95 force=0
96 usexz=1
97 full=1
98
99 # option e currently does nothing
100 while getopts aghervc:C:B:fkKpPUzsN opt ; do
101 case "$opt" in
102 a) adjust=1 ;;
103 g) generic=1 ;;
104 z) usexz=0 ;;
105 c) classes=$OPTARG
106 classes=${classes//,/ } ;;
107 C) cfdir=$OPTARG ;;
108 B) basetgz=$OPTARG
109 if [ ! -r "$basetgz" ] ; then
110 die 7 "Specified $basetgz can not be read."
111 fi
112 ;;
113 v) verbose=1 ; v=-v ;;
114 U) die 1 "Option -U is not supported any more." ;;
115 r) die 1 "Option -r is not supported any more." ;;
116 f) force=1 ;;
117 k) kinstall=1 ;;
118 K) kremove=1;;
119 N) nonfree=NONFREE ;;
120 h) usage ;;
121 e) expert=1 ;; # a dummy option, that only fai-setup uses
122 p) sshpreserve=1 ;;
123 P) sshpreserve=2 ;;
124 s) full=0 ;; # do only install a smaller list of packages
125 ?) exit 5 ;; # error in option parsing
126 esac
127 done
128
129 [ $(id -u) != "0" ] && die 9 "Run this program as root."
130
131 set -e
132 shopt -s nullglob
133
134 # use FAI_ETC_DIR from environment variable
135 if [ -n "$FAI_ETC_DIR" ] && [ -z "$cfdir" ]; then
136 echo "Using environment variable \$FAI_ETC_DIR."
137 fi
138 [ -z "$cfdir" ] && cfdir=${FAI_ETC_DIR:=/etc/fai}
139 cfdir=$(readlink -f $cfdir) # canonicalize path
140 [ ! -d "$cfdir" ] && die 6 "$cfdir is not a directory"
141
142 [ X$verbose = X1 ] && echo "Using configuration files from $cfdir"
143 . $cfdir/fai.conf
144 : ${FAI:=/var/lib/fai/config} # default value
145 : ${MNTPOINT:=/media/mirror} # default value
146
147 # read config file for this tool
148 [ -f "$cfdir/nfsroot.conf" ] || die 8 "Can't read $cfdir/nfsroot.conf"
149 . $cfdir/nfsroot.conf
150
151 # IMO this may be removed, since all information should be included into sources.list
152 [ -z "$NFSROOT" ] && die 1 "\$NFSROOT is not set. Please check your settings in $cfdir/nfsroot.conf."
153 [ -z "$TFTPROOT" ] && die 1 "\$TFTPROOT is not set. Please check your settings in $cfdir/nfsroot.conf."
154 [ ! -d "$cfdir/apt" ] && die 1 "$cfdir/apt/ does not exists. Can't continue."
155
156 deldir=$NFSROOT
157
158 set +e
159 if [ -z "$ROOTCMD" ]; then
160 # check if unshare can be used (e.g. not possible in some container environments)
161 if unshare --fork --kill-child --mount-proc chroot / ls >/dev/null 2>&1 ; then
162 ROOTCMD="unshare --fork --kill-child --mount-proc chroot $NFSROOT"
163 else
164 ROOTCMD="chroot $NFSROOT"
165 fi
166 fi
167 set -e
168 export DEBIAN_FRONTEND=noninteractive
169
170 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171 adjust_nfsroot() {
172
173 # add install server specific data (like network parameters) into the nfsroot
174 # a generic nfsroot must already be available
175 trap '' EXIT
176 add_etc_hosts_entries
177 set_root_pw
178 call_verbose setup_ssh
179 exit 0
180 }
181 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
182 bad_exit() {
183
184 merror="with errors"
185 exitcode=1
186 echo "An error occured during fai-make-nfsroot."
187 echo "Please fix the error or try fai-make-nfsroot -v"
188 }
189 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
190 call_verbose() {
191
192 if [ X$verbose = X1 ]; then
193 "$@"
194 return $?
195 else
196 "$@" > /dev/null
197 return $?
198 fi
199 }
200 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
201 setclasses() {
202
203 local iarch distro c
204 # Export all the information we have about the system
205 iarch=$($ROOTCMD dpkg --print-architecture|tr /a-z/ /A-Z/)
206 distro=$(. $NFSROOT/etc/os-release; echo ${ID} ${ID}_${VERSION_ID} | tr '[:lower:].' '[:upper:]_')
207
208 # do we want to install the full list of packages?
209 if [ $full = 1 ]; then
210 c=FULL
211 fi
212 export classes="NFSROOT $c $iarch $distro $nonfree $classes"
213 if [ X$verbose = X1 ] ; then
214 echo Classes are set to $classes
215 fi
216 }
217 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218 setup_ssh() {
219
220 # nothing to do if no ssh is available in nfsroot
221 [ -f $NFSROOT/usr/bin/ssh ] || return 0
222
223 # enable root login
224 sed -i -e '/PermitRootLogin/d' $NFSROOT/etc/ssh/sshd_config
225 ainsl $NFSROOT/etc/ssh/sshd_config 'PermitRootLogin yes'
226
227 if [ $sshpreserve -ge 1 ]; then
228 tar -C $NFSROOT -xf $tmptar
229 rm $tmptar
230 return 0
231 fi
232
233 mkdir -p -m 700 $NFSROOT/root/.ssh
234 if [ -n "$LOGUSER" ] ; then
235 loguserhome=$(eval "cd ~$LOGUSER 2>/dev/null && pwd;true")
236 [ -f $loguserhome/.ssh/known_hosts ] && cp $loguserhome/.ssh/known_hosts $NFSROOT/root/.ssh/known_hosts
237 [ -d $loguserhome/.ssh ] && {
238 [ -f $loguserhome/.ssh/id_ed25519 ] &&
239 cp -p $loguserhome/.ssh/id_ed25519* $NFSROOT/root/.ssh/
240 [ -f $loguserhome/.ssh/id_rsa ] &&
241 cp -p $loguserhome/.ssh/id_rsa* $NFSROOT/root/.ssh/
242 }
243 fi
244
245 if [ -f "$SSH_IDENTITY" ]; then
246 install -m0644 $SSH_IDENTITY $NFSROOT/root/.ssh/authorized_keys
247 echo You can log into install clients without password using $SSH_IDENTITY
248 fi
249
250 # seting StrictHostKeyChecking to no inside the nfsroot would enable
251 # easy login to the install server even if the host key is not known
252
253 }
254 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
255 set_root_pw() {
256
257 [ $generic = 1 ] && return # do nothing here if we generate a generic nfsroot
258 # set root pw inside the nfsroot
259 $ROOTCMD usermod -p "$FAI_ROOTPW" root
260 return # do not copy fai files at all
261 }
262 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
263 mount_special_into_nfsroot() {
264
265 mkdir -p $NFSROOT/dev/pts
266 mount -t proc /proc $NFSROOT/proc
267 mount -t sysfs /sys $NFSROOT/sys
268 mount -t devpts devpts $NFSROOT/dev/pts
269 }
270 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
271 call_debootstrap() {
272
273 [ -z "$FAI_DEBOOTSTRAP" ] && die 4 "\$FAI_DEBOOTSTRAP not defined."
274
275 # check if NFSROOT directory is mounted with bad options
276 fs=$(df -P $NFSROOT | tail -1 | awk '{print $6}')
277 if mount | grep "on $fs " | awk '{print $6}' | grep -E -q "nosuid|nodev"; then
278 die 1 "NFSROOT directory $NFSROOT is mounted using nosuid or nodev. Aborting"
279 fi
280 local dversion=$(dpkg-query -Wf '${Version}\n' debootstrap)
281 echo "Creating base system using debootstrap version $dversion"
282
283 echo "Calling debootstrap $FAI_DEBOOTSTRAP_OPTS $1 $NFSROOT $2"
284 LC_ALL=C call_verbose debootstrap $FAI_DEBOOTSTRAP_OPTS $1 $NFSROOT $2
285 if [ $? -ne 0 ]; then
286 echo "ERROR: debootstrap did not complete successfully."
287 echo "This is mostly caused by a broken mirror."
288 echo "Please check your mirror or use an official mirror."
289 [ X$verbose = X1 ] || echo "Call fai-make-nfsroot -v for better debugging."
290 exit 10
291 fi
292
293 if [ ! -f $NFSROOT/usr/bin/apt-get ]; then
294 echo "No apt-get executable available inside the NFSROOT."
295 echo "Maybe debootstrap did not finish successfully. Aborting."
296 [ X$verbose = X1 ] || echo "Call fai-make-nfsroot -v for better debugging."
297 exit 11
298 fi
299 }
300 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
301 add_etc_hosts_entries() {
302
303 # some default entries
304 ainsl -as $NFSROOT/etc/hosts "127.0.0.1 localhost"
305 ainsl $NFSROOT/etc/hosts "# Following entries are specific to your environment"
306 ainsl $v $NFSROOT/etc/hosts "$NFSROOT_ETC_HOSTS"
307
308 # add entries for all network devices
309 local ips=$(ip -br ad show up| awk '/UP / {if ($3) print $3}' |sed -e 's#\/[0-9]\+##g')
310
311 local line
312 for eth in $ips; do
313 line=$(getent hosts $eth || true)
314 if [ -n "$line" ] ; then
315 ainsl $v $NFSROOT/etc/hosts "$line"
316 else
317 echo "Warning: no hostname for $eth found, not adding to /etc/hosts."
318 fi
319 done
320
321 if [ -f /etc/resolv.conf ]; then
322 cp -Lp $v /etc/resolv.conf $NFSROOT/etc/resolv.conf-installserver
323 cp -Lp $v /etc/resolv.conf $NFSROOT/etc/resolv.conf # this is needed during fai-make-nfsroot
324 fi
325 }
326 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
327 create_base() {
328
329 command -v debootstrap >&/dev/null || die 1 "Can't find debootstrap command. Aborting."
330 call_debootstrap $FAI_DEBOOTSTRAP
331 $ROOTCMD apt-get clean
332 rm -f $NFSROOT/etc/resolv.conf $NFSROOT/etc/hostname $NFSROOT/etc/udev/rules.d/70-persistent-net.rules $NFSROOT/var/lib/dbus/machine-id
333 > $NFSROOT/etc/machine-id
334 if [ $usexz -eq 0 ]; then
335 echo "Creating base.tar.zst"
336 else
337 echo "Creating base.tar.xz"
338 fi
339 tar --xattrs --acls --one-file-system -C $NFSROOT -cf $NFSROOT/var/tmp/base.tar --exclude var/tmp/base.tar --exclude 'var/lib/apt/lists/*_*' .
340 if [ $usexz -eq 1 ]; then
341 nice xz -q $NFSROOT/var/tmp/base.tar &
342 else
343 nice zstd --rm -q -7 -T4 $NFSROOT/var/tmp/base.tar &
344 fi
345 }
346 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
347 extract_base() {
348
349 if cd $NFSROOT ; then
350 printf "Extracting $basetgz: "
351 case $basetgz in
352 *tar.zst|*.tzst)
353 tar --xattrs --acls --xattrs-include=* -C $NFSROOT --zstd -xpf "$basetgz" || die 1 "Error while extracting ${basetgz} in ${NFSROOT}."
354 cp -p "$basetgz" $NFSROOT/var/tmp/base.tar.zst
355 ;;
356
357 *tar.xz|*.txz)
358 tar --xattrs --acls --xattrs-include=* -C $NFSROOT -Jxpf "$basetgz" || die 1 "Error while extracting ${basetgz} in ${NFSROOT}."
359 cp -p "$basetgz" $NFSROOT/var/tmp/base.tar.xz
360 ;;
361 *)
362 die 14 "Unknow file name suffix"
363 esac
364 echo done
365 fi
366 }
367 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
368 create_nfsroot() {
369
370 local fslist
371
372 mkdir -p $NFSROOT/$FAI
373 cd $NFSROOT || die 1 "Can't cd to $NFSROOT"
374
375 if [ -n "$basetgz" ] ; then
376 extract_base
377 else
378 create_base
379 fi
380 touch .THIS_IS_THE_FAI_NFSROOT
381 # save the list of all packages in the base.tar.zst
382 $ROOTCMD dpkg --get-selections | awk '/install$/ {print $1}' > var/tmp/base-pkgs.lis
383
384 if [ -n "$FAI_DEBMIRROR" ]; then
385 [ X$verbose = X1 ] && echo "Mounting $FAI_DEBMIRROR to $NFSROOT/$MNTPOINT."
386 mkdir -p $NFSROOT/$MNTPOINT
387 mount -o ro,noatime $FAI_DEBMIRROR $NFSROOT/$MNTPOINT || \
388 die 1 "Can't mount $FAI_DEBMIRROR to $NFSROOT/$MNTPOINT."
389 fi
390
391 # liloconfig, dump needs this file
392 > etc/fstab
393
394 # we need these option before installing the first package. So we
395 # can't put this into fai-nfsroot /etc/apt/apt.conf.d/90fai
396 cat >$NFSROOT/etc/apt/apt.conf.d/10fai <<EOF
397 APT::Install-Recommends no;
398 APT::Get::AllowUnauthenticated true;
399 Acquire::AllowInsecureRepositories "true";
400 Aptitude::CmdLine::Ignore-Trust-Violations yes;
401 Acquire::Languages none;
402 Acquire::Check-Valid-Until false;
403 EOF
404
405 mkdir $NFSROOT/etc/dracut.conf.d
406 if [ $full = 1 ];then
407 echo 'add_dracutmodules+=" livenet "' > $NFSROOT/etc/dracut.conf.d/13-add-cd-support.conf
408 fslist="nfs lockd ext4"
409 else
410 echo 'add_dracutmodules+=" dmsquash-live "' > $NFSROOT/etc/dracut.conf.d/13-add-cd-support.conf
411 fslist="ext4"
412 fi
413 echo 'add_dracutmodules+=" systemd-hostnamed systemd-networkd "' > $NFSROOT/etc/dracut.conf.d/11-network.conf
414 echo 'hostonly="no"' > $NFSROOT/etc/dracut.conf.d/20-generic-image.conf
415 echo 'compress="zstd"' > $NFSROOT/etc/dracut.conf.d/20-zstd-compress.conf
416 # we do not need all those filesystems inside the initrd
417 cat <<EOF > $NFSROOT/etc/dracut.conf.d/22-fai.conf
418 omit_dracutmodules+=" btrfs crypt lvm resume usrmount modsign mdraid shutdown virtfs "
419 filesystems="$fslist"
420 install_items+=" ethtool "
421 EOF
422
423 local fname=$(readlink -f $cfdir/apt/sources.list)
424 if [ -d $fname ]; then
425 fcopy -s $cfdir/apt -t $NFSROOT/etc/apt sources.list
426 else
427 # this copies sources.list and sources.list.d into the nfsroot
428 cp -aL $cfdir/apt $NFSROOT/etc
429 fi
430
431 add_etc_hosts_entries
432
433 # use a proxy if variable is defined
434 if [ -n "$APTPROXY" ]; then
435 echo "Acquire::http::Proxy \"$APTPROXY\";" > $NFSROOT/etc/apt/apt.conf.d/02proxy
436 fi
437
438 [ X$verbose = X1 ] && echo "Upgrading $NFSROOT"
439 LC_ALL=C call_verbose upgrade_nfsroot
440 LC_ALL=C add_packages_nfsroot
441
442 # remove file diversion, then create initrd
443 rm $NFSROOT/etc/kernel/postinst.d/dracut
444 $ROOTCMD dpkg-divert --rename --remove /etc/kernel/postinst.d/dracut
445 $ROOTCMD dpkg-reconfigure dracut
446
447 set_root_pw
448
449 # set timezone in nfsroot
450 [ -f /etc/timezone ] && cp -fH /etc/timezone etc/timezone
451 cp -a /etc/localtime etc/localtime
452 ln -sf ../proc/self/mounts etc/mtab
453
454 echo "AUTO -all" > etc/mdadm/mdadm.conf # do not activate RAID arrays
455
456 LC_ALL=C $ROOTCMD systemctl set-default fai.target
457
458 # this second rm of the same file is needed!
459 rm -f etc/udev/rules.d/70-persistent-net.rules
460 # remove Ubuntu-specific rules to auto-start volume groups, which confuses
461 # setup-storage
462 rm -f lib/udev/rules.d/85-lvm2.rules
463 # definition for loopback device
464 mkdir -p etc/network
465 echo "iface lo inet loopback" > etc/network/interfaces
466
467 cat >> root/.profile <<-EOF
468 PATH=/usr/local/sbin:/usr/local/bin:/usr/lib/fai:/bin:/sbin:/usr/bin:/usr/sbin:
469 export PATH
470 set -a
471 . /usr/lib/fai/subroutines
472 . /tmp/fai/variables.log 2>/dev/null
473 EOF
474
475 }
476 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
477 upgrade_nfsroot() {
478
479 local pkgs
480
481 mount_special_into_nfsroot
482 /usr/lib/fai/mkramdisk $NFSROOT/var/lib/dpkg $NFSROOT/var/cache
483 mkdir $NFSROOT/etc/mdadm; touch $NFSROOT/etc/mdadm/mdadm.conf # stop mdadm from calling mkconf
484
485 # add any 3rd party repository keys that you may need installed into the NFSROOT
486 if [ -d $cfdir/apt/keys ]; then
487 local f
488 for f in $(find $cfdir/apt/keys -type f -name '*.gpg'); do
489 cp $f $NFSROOT/etc/apt/trusted.gpg.d
490 done
491 fi
492
493 if [ $full = 1 ];then
494 pkgs=nfs-common
495 fi
496
497 $ROOTCMD apt-get update
498 # disable generating the initrd
499 fdivert /etc/kernel/postinst.d/dracut
500 $ROOTCMD ln -s /bin/true /etc/kernel/postinst.d/dracut
501
502 # fai-nfsroot must be sucessfully installed before anything else
503 $ROOTCMD apt-get -yf --no-install-recommends install fai-nfsroot dracut-config-generic dracut-live dracut-network dracut-squash
504 $ROOTCMD apt-get -y dist-upgrade
505 fdivert /usr/sbin/update-grub
506 }
507 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
508 add_packages_nfsroot() {
509
510 export FAI_ROOT=$NFSROOT
511 local err
512
513 setclasses
514 > $NFSROOT/var/lib/dpkg/available
515 install_packages -l -p$cfdir > $NFSROOT/var/tmp/packages.nfsroot
516 echo "Adding additional packages to $NFSROOT:"
517 cat $NFSROOT/var/tmp/packages.nfsroot
518 set +e
519 call_verbose install_packages $v -p$cfdir
520 err=$?
521 if [ $err -ne 0 ]; then
522 die 12 "install_packages had exit code: $err"
523 fi
524 set -e
525 }
526 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
527 umount_dirs() {
528
529 cd /
530 [ -d $NFSROOT/proc/self ] && umount $NFSROOT/proc
531 [ -d $NFSROOT/sys/class ] && umount $NFSROOT/sys
532 [ -d $NFSROOT/proc/self ] && die 1 "/proc still mounted inside the nfsroot."
533 umount $NFSROOT/dev/pts 2> /dev/null || true
534
535 mountpoint -q $NFSROOT/var/lib/dpkg && \
536 /usr/lib/fai/mkramdisk -u $NFSROOT/var/lib/dpkg $NFSROOT/var/cache
537
538 if [ -n "$FAI_DEBMIRROR" ]; then
539 test -d $NFSROOT/$MNTPOINT && umount $NFSROOT/$MNTPOINT || true
540 rmdir $NFSROOT/$MNTPOINT || true
541 fi
542 # show directories still mounted on nfsroot
543 _cnfsroot=$(readlink -f $NFSROOT)
544 mount | grep " on $_cnfsroot " || true
545 }
546 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
547 setup_tftp(){
548
549 # tftp environment
550 rm -f $NFSROOT/boot/*.bak
551 mkdir -p $TFTPROOT/pxelinux.cfg
552 chmod a+r $NFSROOT/boot/initrd.img-* || die 9 "No initrd was created. Check the package name of the linux-image package in /etc/fai/NFSROOT."
553 cp -p $v $NFSROOT/boot/vmlinu?-* $NFSROOT/boot/initrd.img-* $TFTPROOT
554 cp -u $v $NFSROOT/usr/lib/PXELINUX/lpxelinux.0 $TFTPROOT/pxelinux.0
555 if [ -f $NFSROOT/usr/lib/syslinux/modules/bios/ldlinux.c32 ]; then
556 cp -u $NFSROOT/usr/lib/syslinux/modules/bios/ldlinux.c32 $TFTPROOT
557 fi
558 if [ -f $NFSROOT/usr/lib/syslinux/modules/efi64/ldlinux.e64 ]; then
559 cp -u $NFSROOT/usr/lib/syslinux/modules/efi64/ldlinux.e64 $TFTPROOT
560 fi
561 if [ -f $NFSROOT/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi ]; then
562 cp -u $NFSROOT/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi $TFTPROOT
563 fi
564 if [ X$verbose = X1 ]; then
565 echo "TFTP environment prepared. Enable DHCP and start the TFTP daemon on root $TFTPROOT."
566 fi
567 }
568 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
569 fdivert() {
570
571 local item
572 for item in "$@"; do
573 LC_ALL=C $ROOTCMD dpkg-divert --quiet --add --rename $item
574 done
575 }
576 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
577 run_hooks() {
578
579 local file
580 [ -z "$NFSROOT_HOOKS" ] && return 0
581 [ -d "$NFSROOT_HOOKS" ] || return 0
582
583 echo "Running hooks..."
584 for file in $(run-parts --lsbsysinit --list "$NFSROOT_HOOKS"); do
585 . $file $v
586 done
587 }
588 # - - - - - - - - - - - - - - - - - - - - - - - - - -
589 write_variables() {
590
591 local varfile=/var/log/fai/variables
592 local fai_version=$($ROOTCMD dpkg-query -W -f='${Version}' fai-client)
593
594 # remove old values from file
595 [ -s $varfile ] && sed -i -e '/FAI_VERSION/d' -e '/NFSROOT/d' $varfile
596 echo "FAI_VERSION=$fai_version" >> $varfile
597 echo "NFSROOT=$NFSROOT" >> $varfile
598 }
599 # - - - - - - - - - - - - - - - - - - - - - - - - - -
600
601 # main routine
602
603 trap 'echo "Aborting";umount_dirs' EXIT
604 trap "bad_exit" ERR
605
606 [ $adjust = 1 ] && adjust_nfsroot
607
608 # remove all kernels from nfsroot
609 [ -n "$kremove" ] && {
610 echo "Removing all kernels from NFSROOT."
611 LC_ALL=C $ROOTCMD apt-get -qq purge linux-image\*
612 exit
613 }
614
615 # just upgrade the nfsroot and install packages into it
616 [ -n "$kinstall" ] && {
617 echo "Upgrading nfsroot and installing new packages into the nfsroot."
618 mount_special_into_nfsroot
619 $ROOTCMD apt-get update
620 $ROOTCMD apt-get -y dist-upgrade
621 LC_ALL=C add_packages_nfsroot
622 setup_tftp
623 run_hooks
624 umount_dirs
625 trap "true" EXIT
626 echo "fai-make-nfsroot finished $merror."
627 exit $exitcode
628 }
629
630 # repeat message, so it will appear in the log file
631 rm -f /var/log/fai/fai-make-nfsroot.log
632 [ X$verbose = X1 ] && echo "Using configuration files from $cfdir" > /var/log/fai/fai-make-nfsroot.log
633
634 {
635 if [ -d $NFSROOT ] && [ "$force" -ne 1 ] ; then
636 echo "-----------------------------------------------------------------"
637 echo "Error: $NFSROOT exists already." >&2
638 echo "Neither force nor update option present, exiting." >&2
639 echo "-----------------------------------------------------------------"
640 echo "NOTE: Use -f option to force overwriting an existing nfsroot."
641 echo " Use -k option to update/install packages defined in NFSROOT config."
642 exit 1
643 fi
644
645 echo "Creating FAI nfsroot in $NFSROOT"
646
647 if [ $sshpreserve -ge 1 ]; then
648 [ -d $NFSROOT/root/.ssh ] || echo "Cannot preserve non-existing $NFSROOT/root/.ssh."
649 tmptar=$(mktemp) || die 12 "Cannot create tmp file"
650 pdirs="root/.ssh"
651 if [ $sshpreserve -eq 2 ]; then
652 pdirs="root/.ssh etc/ssh/ssh_host_*"
653 fi
654 export pdirs
655 [ X$verbose = X1 ] && echo "Preserving $pdirs from inside the nfsroot."
656 (
657 cd $NFSROOT || exit # needed for * in $pdirs to work
658 tar -C $NFSROOT -cf $tmptar $pdirs
659 )
660 unset pdirs
661 fi
662
663 if [ -d $NFSROOT/proc ]; then
664 umount $NFSROOT/dev/pts >&/dev/null || true
665 [ -L $NFSROOT/proc/self ] && umount $NFSROOT/proc || true
666 [ -L $NFSROOT/proc/self ] && die 1 "/proc is still mounted inside the nfsroot."
667 umount $NFSROOT/$MNTPOINT 2>/dev/null || true # it's safer to try to umount
668 # remove old nfsroot using a background process
669 rm -rf $deldir/../.will-now-be-deleted
670 if mv $deldir $deldir/../.will-now-be-deleted ; then
671 nice rm -rf $deldir/../.will-now-be-deleted &
672 else
673 rm -rf $deldir
674 fi
675 fi
676
677 create_nfsroot
678 setup_tftp
679 call_verbose setup_ssh
680 run_hooks
681 check_nfsroot
682 write_variables
683
684 if [ $generic = 1 ]; then
685 # remove fai.conf which comes from package fai-client
686 rm -f $NFSROOT/etc/hosts $NFSROOT/etc/resolv.conf* $NFSROOT/etc/fai/fai.conf $NFSROOT/var/lib/apt/lists/*_*
687 ainsl -as $NFSROOT/etc/hosts "127.0.0.1 localhost"
688 fi
689
690 umount_dirs
691
692 # Wait for background jobs to finish
693 BGJOBS=$(jobs -p)
694 if [ -n "$BGJOBS" ]; then
695 echo "Waiting for background jobs to finish"
696 jobs
697 for job in $BGJOBS; do
698 wait $job
699 done
700 fi
701
702 echo "fai-make-nfsroot finished $merror."
703 exit $exitcode
704 } |& tee -a /var/log/fai/fai-make-nfsroot.log
705 RC=${PIPESTATUS[0]}
706 umount_dirs
707 trap "true" EXIT
708 if [ $generic = 0 ]; then
709 cp -p /var/log/fai/fai-make-nfsroot.log $NFSROOT/var/tmp
710 msg="and $NFSROOT/var/tmp"
711 fi
712 echo "Log file written to /var/log/fai/fai-make-nfsroot.log $msg"
713 exit ${RC}