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
|
#! /bin/bash
# $Id: make-fai-bootfloppy 3027 2005-11-10 22:37:16Z lange $
#*********************************************************************
#
# make-fai-bootfloppy -- create a boot floppy for FAI
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2000-2005 by Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************
# 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.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#*********************************************************************
version="Version 2.5.6, 22-april-2005"
set -e
timeout=15
mkimage=0
floppydev=/dev/fd0
[ -d /floppy ] && mountpoint=/floppy
[ -d /media ] && mountpoint=/media/floppy
mountopts="-t ext2"
sd="savedefault"
BTYPE=d # default boot protocol is DHCP
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
umount_dirs() {
cd /
umount "$mountpoint" 2>/dev/null || true
[ -n "$mpcreated" ] && rmdir $mountpoint
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
die() {
echo -e "$@"
exit 99
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
usage() {
cat <<EOF
make-fai-bootfloppy, create a boot floppy for FAI. $version
Copyright (C) 2000-2005 by Thomas Lange
Usage: make-fai-bootfloppy [parameter]
-c CFDIR use CFDIR instead of /etc/fai as configuration directory
-d LABEL use LABEL when selecting the default boot kernel (and parameters).
a for any boot protocol (kernel tries all compiled in)
b for BOOTP
d for DHCP
f use fixed IP, needs companion option -s
r for RARP
Without this option DHCP is used.
-h print this message.
-F append default flags to kernel parameters.
Same as "FAI_FLAGS=verbose,sshd,createvt,syslogd"
-f FILE make a 1440k floppy image in FILE
-g use GRUB loader on bootfloppy (default)
-i FILE make a 1440k iso9660 image in FILE
-l use LILO loader on bootfloppy
-m DIR use DIR as mountpoint [/floppy or /media/floppy]
-s HOST use this static ip for FAI client; try to get all info from DNS
-v print verbose output
DESCRIPTION
Creates a boot floppy for booting a FAI install client.
No arguments are needed but you must be root.
You may need to use "nfsroot=serverip:path" if you use RARP or if your
BOOTP or DHCP server does not pass that info to the clients.
All parameters are passed to the kernel via append in lilo.conf,
or the kernel commandline when using grub.
EXAMPLE
Create a generic boot floppy for James ;-)
# make-fai-bootfloppy "FAI_FLAGS=verbose,createvt,sshd BOND=007"
Make a boot floppy with some common flags and action sysinfo
# make-fai-bootfloppy -vF FAI_ACTION=sysinfo
EOF
exit 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
determine_network_parameters() {
local dev ndevices devices
ndevices=0
devices=`egrep -v "lo:|^Inter-|^ face" /proc/net/dev | awk -F: 'ORS=" " {print $1}'`
for dev in $devices; do
ndevices=$(($ndevices+1))
done
[ $ndevices = 0 ] && die "No network interface found. Can't determine IP address."
if [ $ndevices = 1 ]; then
SERVERINTERFACE=$dev
else
case $BTYPE in
b|d)
# assume BOOTP/DHCP server will pass NFS info to client
;;
*)
# find out which IP to use...
while [ -z "$SERVERINTERFACE" ]; do
echo "This FAI server has multiple network interfaces: $devices"
echo -n "Specify which one FAI clients will connect to (eg. eth1): "
read SERVERINTERFACE
done
;;
esac
fi
SERVERIP=`LC_ALL=C ifconfig $SERVERINTERFACE | perl -ne '/inet addr:([0-9\.]+)/ && print $1'`
[ -z "$SERVERIP" ] && echo "WARNING: Can't determine IP-address for $SERVERINTERFACE"
if [ -n "${TARGETHOST}" ] ; then
# if targethost only contains digits and dots, it's an ip address
chars=`echo $TARGETHOST | tr -d /0-9./`
if [ -z "$chars" ] ; then
TARGETIP=$TARGETHOST
else
TARGETIP=`perl -e 'use Socket; use Net::hostent; printf "%s\n", inet_ntoa((gethost($ARGV[0]))->addr);' "${TARGETHOST}"`
fi
TARGETHOST=`perl -e 'use Socket; print gethostbyaddr(inet_aton($ARGV[0]), AF_INET)."\n";' ${TARGETHOST}`
BROADCAST=`LC_ALL=C ifconfig $SERVERINTERFACE | perl -ne '/Bcast:([0-9\.]+)/ && print $1'`
NETMASK=`LC_ALL=C ifconfig $SERVERINTERFACE | perl -ne '/Mask:([0-9\.]+)/ && print $1'`
GATEWAY=`LC_ALL=C route -n | grep '^0\.0\.0\.0' | awk '{ print $2 }'`
fixedparams="ip=${TARGETIP}:${SERVERIP}:${GATEWAY}:${NETMASK}:${TARGETHOST}::off"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
lilo_floppy() {
# make a boot floppy using lilo
echo "Creating the boot floppy with lilo."
mkdir $mountpoint/boot $mountpoint/etc
rmdir $mountpoint/lost+found
[ "$verbose" ] && echo "Copying the kernel $NFSROOT/boot/$kernel to the floppy."
cd $NFSROOT && cp -dp boot/vmlinuz-$KERNELVERSION $mountpoint/boot
# this is only needed for lilo versions < 22; newer lilos
# have this functionality built-in with no .b bootblocks req'd
if [ -r "boot/boot-menu.b" ]; then
cp -dp boot/boot-menu.b $mountpoint/boot
ln -fs $mountpoint/boot/boot-menu.b $mountpoint/boot/boot.b
fi
ln -s boot/vmlinuz-$KERNELVERSION $mountpoint/vmlinuz
[ $mkimage -eq 1 ] && cat > $mountpoint/etc/lilo.conf <<-EOF
disk=$(mount | grep $mountpoint | sed -e 's/.*loop=\([^)]*\))/\1/')
bios=0x00
cylinders=80
heads=2
sectors=18
EOF
cat >> $mountpoint/etc/lilo.conf <<-EOF
boot=$floppydev
map=$mountpoint/boot/map
delay=$timeout
compact
read-only
default=$lilodef
menu-title="FAI $KERNELVERSION"
image=$mountpoint/vmlinuz
append="ip=::::::any root=/dev/nfs $params"
label=FAI-ANY
image=$mountpoint/vmlinuz
append="ip=::::::bootp root=/dev/nfs $params"
label=FAI-BOOTP
image=$mountpoint/vmlinuz
append="ip=::::::dhcp root=/dev/nfs $params"
label=FAI-DHCP
image=$mountpoint/vmlinuz
label=FAI-FIXED-IP
append="root=/dev/nfs nfsroot=$SERVERIP:$NFSROOT $fixedparams $params"
image=$mountpoint/vmlinuz
append="ip=::::::rarp root=/dev/nfs $params"
label=FAI-RARP
EOF
$NFSROOT/sbin/lilo -C $mountpoint/etc/lilo.conf || die "lilo failed."
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
grub_floppy() {
echo "Creating the boot floppy with grub."
# make a boot floppy using grub
kernel=vmlinuz-$KERNELVERSION
mkdir -p $mountpoint/boot/grub
echo "(fd0) /dev/fd0" > $mountpoint/boot/grub/device.map
[ "$verbose" ] && echo "Copying the kernel $NFSROOT/boot/$kernel to the floppy."
cp -p $NFSROOT/lib/grub/i386-pc/stage? $mountpoint/boot/grub
cp $NFSROOT/boot/$kernel $mountpoint
grubconf="
default $grubdef
#default saved
timeout $timeout
title FAI-ANY $KERNELVERSION
kernel (fd0)/$kernel root=/dev/nfs ip=::::::any $params
$sd
title FAI-BOOTP $KERNELVERSION
kernel (fd0)/$kernel root=/dev/nfs ip=::::::bootp $params
$sd
title FAI-DHCP $KERNELVERSION
kernel (fd0)/$kernel root=/dev/nfs ip=::::::dhcp $params
$sd
title FAI-FIXED-IP $KERNELVERSION
kernel (fd0)/$kernel root=/dev/nfs nfsroot=$SERVERIP:$NFSROOT $fixedparams $params
$sd
title FAI-RARP $KERNELVERSION
kernel (fd0)/$kernel root=/dev/nfs ip=::::::rarp $params
$sd
"
echo "$grubconf" > $mountpoint/boot/grub/menu.lst
$NFSROOT/sbin/grub --batch --device-map=$mountpoint/boot/grub/device.map >/dev/null <<EOF
device (fd0) $floppydev
root (fd0)
setup (fd0)
quit
EOF
umount $mountpoint
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cd_image() {
[ -x "`which mkisofs`" ] || {
echo "Can't find command mkisofs."
exit 5
}
echo "Creating the iso9660 cd image in $cddev from floppy image $floppydev."
cd /tmp
TMPDIR=`mktemp -d`
cd $TMPDIR || exit 7
mkdir $TMPDIR/boot
cp $floppydev ./boot/boot.img
mkisofs -V "FAI boot CDROM" -b boot/boot.img -c boot/boot.catalog -o bootcd.iso .
rm -rf $TMPDIR/boot
mv $TMPDIR/bootcd.iso $cddev
echo "ISO-Image created in $cddev"
rmdir $TMPDIR
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main part
while getopts "lgvf:s:m:hd:c:Fi:" opt ; do
case "$opt" in
c) cfdir=$OPTARG ;;
l) lilo=1 ;;
g) grub=1 ;;
h) usage ;;
i) mkcd=1; cddev="$OPTARG" ; sd='';;
f) mkimage=1; floppydev="$OPTARG" ;;
m) mountpoint="$OPTARG" ;;
s) TARGETHOST="$OPTARG" ;;
d) BTYPE="$OPTARG" ;;
v) verbose=1 ;;
F) flags="FAI_FLAGS=verbose,sshd,createvt,syslogd" ;;
?) echo "Error parsing arguments"; exit 1;;
esac
done
shift `expr $OPTIND - 1`
case "$BTYPE" in
a) lilodef=FAI-ANY ; grubdef=0 ;;
b) lilodef=FAI-BOOTP ; grubdef=1 ;;
d) lilodef=FAI-DHCP ; grubdef=2 ;;
f) lilodef=FAI-FIXED-IP ; grubdef=3 ;;
r) lilodef=FAI-RARP ; grubdef=4 ;;
*) echo "$BTYPE is unknown boot label. Use a,b,d,f or r." ; exit 3 ;;
esac
[ $lilo ] && [ $grub ] && die "Specify only one of -l or -g"
[ $lilo ] || [ $grub ] || grub=1
[ $mkcd ] && [ $mkimage -eq 0 ] && die "You must use -f with -i"
[ `id -u` -eq 0 ] || die "You must be root! Or try $0 -h"
trap "umount_dirs" EXIT
# additional kernel parameter
params="$flags $@"
[ -z "$cfdir" ] && cfdir=/etc/fai
if [ ! -d "$cfdir" ]; then
echo "$cfdir is not a directory"
exit 6
fi
[ "$verbose" ] && echo "Using configuration files from directory $cfdir."
. $cfdir/fai.conf
# use newest kernel if several are installed
KERNELVERSION=$(ls -tr $NFSROOT/boot/vmlinu?-* | tail -1 | sed -e 's#.*/vmlinuz-##')
determine_network_parameters
if [ $mkimage -eq 1 ]; then
[ -e $floppydev ] && die "$floppydev already exists. Please remove it and restart again."
dd if=/dev/zero of=$floppydev bs=1024 count=1440
mountopts="$mountopts -o loop"
mkfsopt=-F
else
# blank first sector of floopy
dd if=/dev/zero of=$floppydev bs=512 count=1 2>/dev/null
fi
# pay attention: if -i option is too large, then too few inodes
# are created on the floppy. Check it with df -i
echo "Creating an ext2 filesystem on floppy device $floppydev."
mke2fs $mkfsopt -q -i 40000 -m 0 $floppydev || \
die "Can't create ext2 file system on $floppydev"
if [ ! -d $mountpoint ]; then
mkdir -p $mountpoint
mpcreated=1 # we created the mountpoint
fi
mount $mountopts $floppydev $mountpoint || die "Can't mount floppy $floppydev"
[ $lilo ] && lilo_floppy
[ $grub ] && grub_floppy
[ $mkcd ] && cd_image
echo "Using IP address $SERVERIP of $SERVERINTERFACE for the fixed boot menu."
cat <<-EOF
Writing boot data to floppy. The default boot label is: $liloboot
The kernel configuration is $NFSROOT/boot/config-$KERNELVERSION.
EOF
[ "$params" ] && echo "Additional kernel parameters: $params"
[ "$verbose" ] && [ $lilo ] && {
echo "The lilo.conf is:"
echo
cat $mountpoint/etc/lilo.conf
}
[ "$verbose" ] && [ $grub ] && {
echo "The grub menu is:"
echo
echo "$grubconf"
}
|