/[fai]/trunk/bin/make-fai-nfsroot
ViewVC logotype

Contents of /trunk/bin/make-fai-nfsroot

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5067 - (show annotations) (download)
Thu Sep 4 09:02:16 2008 UTC (4 years, 9 months ago) by lange
File size: 17591 byte(s)
adjust information of nfsroot size
1 #! /bin/bash
2
3 # $Id$
4 #*********************************************************************
5 #
6 # make-fai-nfsroot -- create nfsroot directory and add additional packages
7 #
8 # This script is part of FAI (Fully Automatic Installation)
9 # (c) 2000-2008 by Thomas Lange, lange@informatik.uni-koeln.de
10 # Universitaet zu Koeln
11 # (c) 2004 by Henning Glawe, glaweh@physik.fu-berlin.de
12 # Freie Universitaet Berlin
13 #
14 #*********************************************************************
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful, but
21 # WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 # General Public License for more details.
24 #
25 # A copy of the GNU General Public License is available as
26 # `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
27 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
28 # can also obtain it by writing to the Free Software Foundation, Inc.,
29 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
30 #*********************************************************************
31
32 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33 usage() {
34
35 cat <<-EOF
36 Copyright (C) 1999-2008 Thomas Lange
37 Report bugs to <fai@informatik.uni-koeln.de>.
38
39 Usage: make-fai-nfsroot [OPTIONS]
40 Create an NFSROOT for FAI.
41 Read the man pages pages make-fai-nfsroot(8).
42 EOF
43 exit 0
44 }
45 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 die() {
47
48 echo "ERROR: $@"
49 echo "Log file written to /var/log/fai/make-fai-nfsroot.log"
50 exit 99
51 }
52
53 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54 check_nfsroot() {
55
56 set +e
57 # simple test, to see if important thing are available inside the nfsroot
58 [ -d $NFSROOT/usr/share/live-initramfs ] || die "live-initramfs was not installed inside the nfsroot."
59 local files=$(ls $NFSROOT/boot/initrd* 2>/dev/null)
60 [ -z "$files" ] && die "No initrd installed."
61 egrep -q "^ERROR: " /var/log/fai/make-fai-nfsroot.log && bad_exit
62 return 0
63 }
64 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65 badupdateto32() {
66
67 cat <<-EOF
68 It seems that you have updated an older FAI version without checking files in /etc/fai/
69 Please check the NEWS file for changes in variable names and if you have a linux-image package
70 defined in /etc/fai/NFSROOT.
71 EOF
72 die $@
73 }
74 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75
76 PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin
77
78 merror="properly"
79 sshpreserve=0
80 divert=1
81
82 if [ -f /etc/lsb-release ]; then
83 . /etc/lsb-release
84 case "$DISTRIB_ID" in
85 Ubuntu) divert=0 ;;
86 esac
87 fi
88
89 # option e currently does nothing
90 while getopts hervC:f:kKpU opt ; do
91 case "$opt" in
92 C) cfdir=$OPTARG ;;
93 v) verbose=1 ; v=-v ;;
94 r) recover=1 ;;
95 f) die "Option -f is not supported any more. Use option -C instead" ;;
96 k) kinstall=1 ;;
97 K) kremove=1;;
98 h) usage ;;
99 e) expert=1 ;; # a dummy option, that only fai-setup uses
100 p) sshpreserve=1 ;;
101 U) divert=0 ;;
102 ?) exit 5 ;; # error in option parsing
103 esac
104 done
105
106 if [ $(id -u) != "0" ]; then
107 echo "Run this program as root."
108 exit 9
109 fi
110
111 set -e
112
113 # use FAI_ETC_DIR from environment variable
114 if [ -n "$FAI_ETC_DIR" -a -z "$cfdir" ]; then
115 echo "Using environment variable \$FAI_ETC_DIR."
116 fi
117 [ -z "$cfdir" ] && cfdir=${FAI_ETC_DIR:=/etc/fai}
118 cfdir=$(readlink -f $cfdir) # canonicalize path
119 if [ ! -d "$cfdir" ]; then
120 echo "$cfdir is not a directory"
121 exit 6
122 fi
123 [ "$verbose" ] && echo "Using configuration files from $cfdir"
124 . $cfdir/fai.conf
125
126 # read config file for this tool
127 if [ -f "$cfdir/make-fai-nfsroot.conf" ]; then
128 . $cfdir/make-fai-nfsroot.conf
129 else
130 echo "Can't read $cfdir/make-fai-nfsroot.conf"
131 exit 8
132 fi
133
134 # IMO this may be removed, since all information should be included into sources.list
135 [ -n "$FAI_LOCAL_REPOSITORY" ] && die "The use of \$FAI_LOCAL_REPOSITORY is now deprecated. Please include this information into sources.list."
136 [ -n "$packages" ] && badupdateto32 "The use of \$packages in make-fai-nfsroot.conf is now deprecated. Please include this information into $cfdir/NFSROOT."
137 [ -n "$NFSROOT_PACKAGES" ] && die "The use of \$NFSROOT_PACKAGES in make-fai-nfsroot.conf is now deprecated. Please include this information into $cfdir/NFSROOT."
138 [ -n "$FAI_SOURCES_LIST" ] && die "The use of \$FAI_SOURCES_LIST is deprecated. Please use sources.list now."
139
140 [ -z "$NFSROOT" ] && die "\$NFSROOT is not set. Please check your settings in $cfdir/fai.conf."
141 [ -z "$TFTPROOT" ] && badupdateto32 "\$TFTPROOT is not set. Please check your settings in $cfdir/make-fai-nfsroot.conf."
142 [ -n "$KERNELPACKAGE" ] && badupdateto32 "The use of \$KERNELPACKAGE is deprecated. Please use $cfdir/NFSROOT now."
143 [ ! -d "$cfdir/apt" ] && die "$cfdir/apt/ does not exists. Can't continue."
144
145 oldnfsroot=$NFSROOT
146 NFSROOT="$NFSROOT/live/filesystem.dir"
147 ROOTCMD="chroot $NFSROOT"
148
149 RUNDIR=/var/run/fai/make-fai-nfsroot
150 mkdir -p $RUNDIR
151 export DEBIAN_FRONTEND=noninteractive
152 [ "$recover" ] || rm -rf $RUNDIR/*
153 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154 bad_exit() {
155
156 merror="with errors"
157 echo "An error occured during make-fai-nfsroot."
158 echo "Please fix the error or try make-fai-nfsroot -v"
159 }
160 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161 call_with_stamp() {
162
163 local func=$1
164 local stamp=$RUNDIR/$func
165 # call subroutine
166 [ "$recover" -a -f $stamp ] && return 0
167 # make a stamp only on success.
168 "$@" && : > $stamp
169 }
170 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171 call_verbose() {
172
173 if [ "$verbose" ]; then
174 "$@"
175 return $?
176 else
177 "$@" > /dev/null
178 return $?
179 fi
180 }
181 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
182 install_kernel_nfsroot() {
183
184 if [ $divert = 1 ]; then
185 rm $NFSROOT/usr/sbin/update-initramfs
186 LC_ALL=C $ROOTCMD dpkg-divert --rename --remove /usr/sbin/update-initramfs
187 $ROOTCMD update-initramfs -k all -t -u
188 fi
189 }
190 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
191 setup_ssh() {
192
193 # nothing to do if no ssh is available in nfsroot
194 [ -f $NFSROOT/usr/bin/ssh ] || return 0
195 if [ $sshpreserve = 1 ]; then
196 tar -C $NFSROOT -xf $tmptar
197 rm $tmptar
198 return 0
199 fi
200
201 mkdir -p -m 700 $NFSROOT/root/.ssh
202 if [ -n "$LOGUSER" ] ; then
203 loguserhome=$(eval "cd ~$LOGUSER 2>/dev/null && pwd;true")
204 [ -f $loguserhome/.ssh/known_hosts ] && cp $loguserhome/.ssh/known_hosts $NFSROOT/root/.ssh/known_hosts
205 [ -d $loguserhome/.ssh ] && {
206 [ -f $loguserhome/.ssh/id_dsa ] &&
207 cp -p $loguserhome/.ssh/id_dsa* $NFSROOT/root/.ssh/
208 [ -f $loguserhome/.ssh/id_rsa ] &&
209 cp -p $loguserhome/.ssh/id_rsa* $NFSROOT/root/.ssh/
210 }
211 fi
212
213 # enable root login
214 perl -pi -e 's/PermitRootLogin no/PermitRootLogin yes/' $NFSROOT/etc/ssh/sshd_config
215 if [ -f "$SSH_IDENTITY" ]; then
216 cp $SSH_IDENTITY $NFSROOT/root/.ssh/authorized_keys
217 chmod 0644 $NFSROOT/root/.ssh/authorized_keys
218 echo You can log into install clients without password using $SSH_IDENTITY
219 fi
220 }
221 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
222 copy_fai_files() {
223
224 # copy to nfsroot
225 echo "root:$FAI_ROOTPW" | $ROOTCMD chpasswd --encrypted
226 cp -Rpv $cfdir/* $NFSROOT/etc/fai
227 # append additional variables to fai.conf for the install clients
228 [ -z "$FAI_CONFIG_SRC" ] && echo "FAI_CONFIG_SRC=nfs://`hostname`$FAI_CONFIGDIR" >> $NFSROOT/etc/fai/fai.conf
229
230 # remove some files that should not be copied
231 rm -f $NFSROOT/etc/fai/make-fai-nfsroot.conf
232 [ -f $cfdir/.cvspass ] && cp -p $v $cfdir/.cvspass $NFSROOT/.cvspass
233 $ROOTCMD shadowconfig on
234 }
235 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
236 call_debootstrap() {
237
238 # check if NFSROOT directory is mounted with bad options
239 fs=$(df $NFSROOT | tail -1 | awk '{print $6}')
240 if mount | grep "on $fs " | awk '{print $6}' | egrep -q "nosuid|nodev"; then
241 die "NFSROOT directory $NFSROOT is mounted using nosuid or nodev. Aborting"
242 fi
243
244 local dversion=$(dpkg -l debootstrap | grep debootstrap | cut -f7 -d' ')
245 echo "Creating base system using debootstrap version $dversion"
246 echo "Calling debootstrap $1 $NFSROOT $2"
247 yes '' | LC_ALL=C call_verbose debootstrap $FAI_DEBOOTSTRAP_OPTS $1 $NFSROOT $2
248 }
249 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
250 add_all_host_entries() {
251
252 local ips=$(ifconfig | grep -w inet | cut -d : -f 2 | cut -d ' ' -f 1 | grep -v 127.0.0.1)
253 for eth in $ips; do
254 getent hosts $eth >> etc/hosts || true
255 done
256 }
257 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
258 create_base() {
259
260 if [ "$FAI_DEBOOTSTRAP" ]; then
261 call_with_stamp call_debootstrap $FAI_DEBOOTSTRAP
262 $ROOTCMD apt-get clean
263 rm -f $NFSROOT/etc/resolv.conf
264 echo "Creating base.tgz"
265 tar --one-file-system -C $NFSROOT -cf - --exclude var/tmp/base.tgz --exclude 'var/lib/apt/lists/*_*' . | gzip > $NFSROOT/var/tmp/base.tgz
266 touch $NFSROOT/.THIS_IS_THE_FAI_NFSROOT
267 else
268 die "\$FAI_DEBOOTSTRAP not defined."
269 fi
270 }
271 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
272 create_nfsroot() {
273
274 mkdir -p $NFSROOT/$FAI
275 cd $NFSROOT || die "Can't cd to $NFSROOT"
276
277 call_with_stamp create_base
278 # save the list of all packages in the base.tgz
279 $ROOTCMD dpkg --get-selections | egrep 'install$' | awk '{print $1}' > var/tmp/base-pkgs.lis
280
281 if [ "$FAI_DEBMIRROR" ]; then
282 [ "$verbose" ] && echo "Mounting $FAI_DEBMIRROR to $NFSROOT/$MNTPOINT."
283 mkdir -p $NFSROOT/$MNTPOINT
284 mount -o ro,noatime,rsize=8192 $FAI_DEBMIRROR $NFSROOT/$MNTPOINT || \
285 die "Can't mount $FAI_DEBMIRROR to $NFSROOT/$MNTPOINT."
286 fi
287
288 # hoaks some packages
289 # liloconfig, dump and raidtool2 needs these files
290 echo "# UNCONFIGURED FSTAB FOR BASE SYSTEM" > etc/fstab
291 > etc/raidtab
292 echo 'NTPSERVERS=""' > etc/default/ntp-servers
293
294 cp -a $cfdir/apt $NFSROOT/etc
295
296 ainsl -as $NFSROOT/etc/hosts "127.0.0.1 localhost"
297 ainsl $NFSROOT/etc/hosts "$NFSROOT_ETC_HOSTS"
298 add_all_host_entries
299
300 # we need these option before installing the first package. So we
301 # can't put this into fai-nfsroot /etc/apt/apt.conf.d/90fai
302 cat <<EOF >$NFSROOT/etc/apt/apt.conf.d/10fai
303 APT::Get::AllowUnauthenticated "true";
304 Aptitude::CmdLine::Ignore-Trust-Violations yes;
305 EOF
306 echo "Upgrading $NFSROOT"
307 LC_ALL=C call_verbose call_with_stamp upgrade_nfsroot
308
309 # overwrite default live.conf
310 if [ -f $cfdir/live.conf ]; then
311 cp -p $cfdir/live.conf etc/live.conf
312 else
313 cat > etc/live.conf <<EOF
314 # UNIONTYPE=aufs # if unionfs is broken
315 NOUSER="Yes"
316 NOHOSTS="Yes"
317 export UNIONTYPE NOHOSTS NOUSER
318 EOF
319 fi
320
321 LC_ALL=C call_with_stamp add_packages_nfsroot
322 call_with_stamp copy_fai_files
323
324 # set timezone in nfsroot
325 timezone=$(readlink /etc/localtime | sed 's%^/usr/share/zoneinfo/%%')
326 echo $timezone > etc/timezone
327 rm -f etc/localtime && ln -sf /usr/share/zoneinfo/$timezone etc/localtime
328
329 rm etc/mtab && ln -s /proc/mounts etc/mtab
330 ln -s /usr/sbin/fai etc/init.d/rcS
331
332 # definition for loopback device
333 echo "iface lo inet loopback" > etc/network/interfaces
334
335 cat >> root/.profile <<-EOF
336 PATH=/usr/local/sbin:/usr/local/bin:/usr/lib/fai:/bin:/sbin:/usr/bin:/usr/sbin:
337 export PATH
338 . /usr/lib/fai/subroutines
339 . /usr/lib/fai/subroutines-linux
340 set -a
341 . /tmp/fai/variables.log 2>/dev/null
342 EOF
343
344 call_verbose call_with_stamp setup_ssh
345 }
346 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
347 upgrade_nfsroot() {
348
349 if [ -f /etc/resolv.conf ]; then
350 cp -p $v /etc/resolv.conf $NFSROOT/etc/resolv.conf-installserver
351 cp -p $v /etc/resolv.conf $NFSROOT/etc/resolv.conf # this is needed during make-fai-nfsroot
352 fi
353 mount -t proc /proc $NFSROOT/proc
354 mount -t devpts devpts $NFSROOT/dev/pts
355 $ROOTCMD apt-get update
356 $ROOTCMD aptitude -Rfy install fai-nfsroot
357 $ROOTCMD apt-get check
358 rm -rf $NFSROOT/etc/apm
359
360 if [ $divert = 1 ]; then
361 fdivert /usr/sbin/update-initramfs
362 ln -s /bin/true $NFSROOT/usr/sbin/update-initramfs
363 fi
364 fdivert /sbin/start-stop-daemon /sbin/discover-modprobe
365 cp -p /sbin/fai-start-stop-daemon $NFSROOT/sbin
366 cp -p /sbin/fai-start-stop-daemon $NFSROOT/sbin/start-stop-daemon
367 $ROOTCMD apt-get -y dist-upgrade
368 }
369 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
370 add_packages_nfsroot() {
371
372 local iarch=$($ROOTCMD dpkg --print-installation-architecture|tr /a-z/ /A-Z/)
373 export FAI_ROOT=$NFSROOT
374 export classes="NFSROOT $iarch"
375 cat > $NFSROOT/etc/kernel-img.conf << EOF
376 do_bootloader = No
377 do_initrd = No
378 warn_initrd = No
379 EOF
380 install_packages -l -p$cfdir > $NFSROOT/var/tmp/packages.nfsroot
381 echo "Adding additional packages to $NFSROOT:"
382 cat $NFSROOT/var/tmp/packages.nfsroot
383 call_verbose install_packages $v -p$cfdir
384 echo "install_packages exit code: $?"
385 }
386 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
387 umount_dirs() {
388
389 [ -f $NFSROOT/usr/sbin/dpkg-divert ] &&
390 LC_ALL=C $ROOTCMD dpkg-divert --rename --remove /sbin/discover-modprobe
391 cd /
392 [ -d $NFSROOT/proc/self ] && umount $NFSROOT/proc
393 [ -d $NFSROOT/proc/self ] && die "/proc still mounted inside the nfsroot."
394 umount $NFSROOT/dev/pts 2> /dev/null || true
395
396 if [ "$FAI_DEBMIRROR" ]; then
397 test -d $NFSROOT/$MNTPOINT && umount $NFSROOT/$MNTPOINT || true
398 fi
399 # show directories still mounted on nfsroot
400 mount | grep " on $NFSROOT " || true
401 }
402 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
403 setup_dhcp(){
404
405 # pxe and dhcp environment
406 local pxebin=/usr/lib/syslinux/pxelinux.0
407 rm -f $NFSROOT/boot/*.bak
408 mkdir -p $TFTPROOT/pxelinux.cfg
409 cp -pv $NFSROOT/boot/vmlinu?-* $NFSROOT/boot/initrd.img-* $TFTPROOT
410 [ -f $pxebin ] && cp $pxebin $TFTPROOT
411 echo "DHCP environment prepared. If you want to use it, you have to enable the dhcpd and the tftp-hpa daemon."
412 }
413 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
414 fdivert() {
415
416 local item
417 for item in "$@"; do
418 LC_ALL=C $ROOTCMD dpkg-divert --quiet --add --rename $item
419 done
420 }
421 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
422 kernel_install() {
423
424 # Install the kernel package
425
426 # create tftp boot images
427 call_with_stamp install_kernel_nfsroot
428
429 # setup for DHCP, BOOTP or both
430 [ "x$FAI_BOOT" = "x" ] && FAI_BOOT="dhcp"
431
432 for bootopt in $FAI_BOOT; do
433 case $bootopt in
434 dhcp|DHCP)
435 call_with_stamp setup_dhcp ;;
436 bootp|BOOTP)
437 echo "You have to setup BOOTP support manually." ;;
438 *)
439 echo "Unknown boot option" ;;
440 esac
441 done
442 }
443 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
444 run_hooks() {
445
446 local file
447 if [ -z "$NFSROOT_HOOKS" ]; then
448 return
449 fi
450 if [ -d "$NFSROOT_HOOKS" ]; then
451 echo "Running hooks..."
452 for file in $(ls $NFSROOT_HOOKS/* 2>/dev/null); do
453 . $file
454 done
455 fi
456 }
457 # - - - - - - - - - - - - - - - - - - - - - - - - - -
458 # main routine
459
460 trap 'echo "Aborting";umount_dirs' EXIT
461 trap "bad_exit" ERR
462 {
463
464 # remove all kernels from nfsroot
465 [ -n "$kremove" ] && {
466 echo "Removing all kernels from NFSROOT."
467 $ROOTCMD aptitude -y purge ~nlinux-image
468 exit
469 }
470
471 # just install a new kernel to the nfsroot
472 [ -n "$kinstall" ] && {
473 trap "true" EXIT
474 echo "Installing new kernel into the nfsroot."
475 if [ $divert = 1 ]; then
476 fdivert /usr/sbin/update-initramfs
477 ln -s /bin/true $NFSROOT/usr/sbin/fai
478 fi
479 LC_ALL=C add_packages_nfsroot
480 kernel_install
481 run_hooks
482 umount_dirs
483 trap "true" EXIT
484 echo "make-fai-nfsroot finished $merror."
485 exit 0
486 }
487
488 echo "Creating FAI nfsroot in $NFSROOT."
489 echo "By default it needs more than 390 MBytes disk space."
490 echo "This may take a long time."
491
492 if [ $sshpreserve = 1 ]; then
493 # save old .ssh directory
494 tmptar=$(mktemp) || exit 12
495 # should we set the umask before? Does it influence the other parts?
496 tar -C $NFSROOT -cvf $tmptar root/.ssh
497 fi
498
499 # Kill the directory if not in recover mode
500 if [ -d $NFSROOT/proc -a ! "$recover" ]
501 then
502 echo $NFSROOT already exists. Removing $NFSROOT
503 umount $NFSROOT/dev/pts 1>/dev/null 2>&1 || true
504 [ -L $NFSROOT/proc/self ] && umount $NFSROOT/proc || true
505 [ -L $NFSROOT/proc/self ] && die "/proc is still mounted inside the nfsroot."
506 rm -rf $oldnfsroot/.??* $oldnfsroot/*
507 # also remove files $NFSROOT/.? but not . and ..
508 find $oldnfsroot -xdev -maxdepth 1 ! -type d | xargs -r rm -f
509 fi
510
511 # Create a new nfsroot
512 if [ ! -x "$(which debootstrap)" ]; then
513 die "Can't find debootstrap command. Aborting."
514 fi
515 call_with_stamp create_nfsroot
516
517 kernel_install
518 run_hooks
519 check_nfsroot
520 umount_dirs
521 echo "make-fai-nfsroot finished $merror."
522 exit 0
523 } 2>&1 | tee /var/log/fai/make-fai-nfsroot.log
524 umount_dirs
525 trap "true" EXIT
526 echo "Log file written to /var/log/fai/make-fai-nfsroot.log"

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5