~morty/fai/trunk

« back to all changes in this revision

Viewing changes to bin/fai-cd

  • Committer: Thomas Lange
  • Date: 2005-11-10 12:47:47 UTC
  • Revision ID: git-v1:cf3cbb08a817ca4214ba8686e237d2ea61fdf30d
rename directory scripts to bin, fix pathes in Makefile

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
# $Id$
 
4
#*********************************************************************
 
5
#
 
6
# fai-cd -- make a fai CD, a bootable CD that performs the FAI
 
7
#
 
8
# This script is part of FAI (Fully Automatic Installation)
 
9
# (c) 2004-2005 by Thomas Lange, lange@informatik.uni-koeln.de
 
10
# Universitaet zu Koeln
 
11
#
 
12
# based on a script called make-fai-bootcd by Niall Young <niall@holbytla.org>
 
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
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
30
#*********************************************************************
 
31
 
 
32
set -e 
 
33
version="fai-cd 1.2.1"
 
34
 
 
35
isoversion="$version -- build $(date '+%c')"
 
36
vname="Fully Automatic Installation CD"
 
37
aname="Fully Automatic Installation by Thomas Lange, $isoversion"
 
38
 
 
39
grub_config=/etc/fai/menu.lst
 
40
 
 
41
burn=0
 
42
bimagesize=2.88 # mkbimage can create greater images, but mkisofs only accepts up to 2.88
 
43
hidedirs="/usr/share/locale /usr/share/doc /var/lib/apt /var/cache/apt /usr/share/man /var/lib/dpkg/info /media/mirror/aptcache /media/mirror/.apt-move"
 
44
 
 
45
# we need FAI_CONFIGDIR, NFSROOT
 
46
. /etc/fai/fai.conf
 
47
 
 
48
trap "unhide_dirs;umount_dirs; mhide -u xfs reiserfs ext3" EXIT ERR
 
49
 
 
50
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
51
usage() {
 
52
 
 
53
    cat <<-EOF
 
54
        $version. Copyright (C) 2004-2005 Thomas Lange
 
55
        Report bugs to <fai@informatik.uni-koeln.de>.
 
56
 
 
57
        Usage: fai-cd [OPTIONS] -m MIRRORDIR ISONAME
 
58
        Create a fai CD, a bootable CD that performs the FAI.
 
59
        Read the man pages pages fai-cd(8).
 
60
EOF
 
61
exit 0
 
62
}
 
63
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
64
die() {
 
65
 
 
66
    local e=$1   # first parameter is the exit code
 
67
    shift
 
68
 
 
69
    echo "$@"    # print error message
 
70
    exit $e
 
71
}
 
72
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
73
mhide() {
 
74
 
 
75
    local mode m
 
76
 
 
77
    mode=$1
 
78
    shift
 
79
    for m in $@; do
 
80
        module-hide $mode $m
 
81
    done
 
82
}
 
83
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
84
module-hide() {
 
85
 
 
86
    # hide or unhide a kernel module, so mkinitrd-cd can't see it
 
87
    # therefore we rename the modules to XXX-<modulename.o>
 
88
    # this works for 2.4 and 2.6 kernel (.o and .ko modules)
 
89
 
 
90
    local dir module newname nam
 
91
    local kdir=$NFSROOT/lib/modules/$kernelversion
 
92
 
 
93
    mode=$1
 
94
    shift
 
95
    module=$1
 
96
 
 
97
    case $mode in
 
98
        -h) nam=$module;;
 
99
        -u) nam=XXX-$module;;
 
100
    esac
 
101
 
 
102
    found=$(find $kdir -name ${nam}.o -o -name ${nam}.ko)
 
103
    # echo "FOUND: $found"
 
104
    if [ -z "$found" ]; then
 
105
#       echo "$module not found."
 
106
       return
 
107
    fi
 
108
    [ -f $found ] || return
 
109
 
 
110
    newname=$(basename $found)
 
111
    newname=${newname/XXX-/}  # substitute XXX- with nothing
 
112
    dir=$(dirname $found)
 
113
 
 
114
    set +e
 
115
    case $mode in
 
116
        -h) mv $found $dir/XXX-$newname ;;
 
117
        -u) mv $found $dir/$newname ;;
 
118
    esac
 
119
    set -e
 
120
}
 
121
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
122
create_initrd_image() {
 
123
 
 
124
    local size=min # full is often bigger than 2.88mb
 
125
    local kdir=$NFSROOT/lib/modules/$kernelversion
 
126
 
 
127
    echo "Creating initrd image with kernel $kernelversion"
 
128
 
 
129
    sed "s/_VERSION_/$isoversion/" < /etc/mkinitrd-cd/id.txt > $NFSROOT/id.txt
 
130
 
 
131
    # mkbimage complains if the image will be too large. So do not include some modules
 
132
    # It would be nice if mkinitrd-cd had a config file!
 
133
 
 
134
    mhide -h xfs reiserfs ext3
 
135
 
 
136
    mkinitrd-cd $kdir $tmp/initrd.img $size "$isoversion"
 
137
    mhide -u xfs reiserfs ext3
 
138
}
 
139
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
140
create_grub_image() {
 
141
 
 
142
    echo -n "Creating grub boot image ..."
 
143
 
 
144
    mkdir -p $tmp/boot/grub
 
145
    > $tmp/boot/RUNNING_FROM_FAICD
 
146
    cp /lib/grub/i386-pc/stage{1,2} $tmp/boot/grub
 
147
    cp $grub_config $tmp/boot/grub/menu.lst
 
148
    # insert date into grub menu
 
149
    perl -pi -e "s/_VERSIONSTRING_/   $isoversion     /" $tmp/boot/grub/menu.lst
 
150
    cp $NFSROOT/boot/vmlinuz-$kernelversion $tmp/boot
 
151
    ln -s boot/vmlinuz-$kernelversion $tmp/vmlinuz
 
152
    tar -C $tmp -cf $tmp/make-fai-cd.tar boot vmlinuz initrd.img
 
153
    mkbimage -d $tmp -f $tmp/make-fai-cd.tar -t $bimagesize >/dev/null 2>&1
 
154
    # mkbimage creates the file $tmp/$bimagesize.image
 
155
    rm $tmp/initrd.img $tmp/make-fai-cd.tar
 
156
    mv $tmp/$bimagesize.image $tmp/boot
 
157
    echo "done"
 
158
}
 
159
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
160
customize_nfsroot() {
 
161
 
 
162
    # hide some dirs to save space and make the CD image smaller
 
163
    local d
 
164
 
 
165
    mkdir $tmp/empty
 
166
    for d in $hidedirs; do
 
167
        if [ -d $NFSROOT/$d ]; then
 
168
            [ "$debug" ] && echo "hiding $d"
 
169
            mount --bind $tmp/empty $NFSROOT/$d
 
170
        fi
 
171
    done
 
172
}
 
173
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
174
unhide_dirs() {
 
175
 
 
176
    set +e
 
177
    for d in $hidedirs; do
 
178
        if [ -d $NFSROOT/$d ]; then
 
179
            [ "$debug" ] && echo "disclosing $d"
 
180
            umount $NFSROOT/$d 2>/dev/null
 
181
        fi
 
182
    done
 
183
    set -e
 
184
}
 
185
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
186
umount_dirs() {
 
187
 
 
188
    set +e
 
189
    local d
 
190
    local dirs="boot fai media/mirror etc/apt/sources.list"
 
191
    for d in $dirs; do
 
192
        umount $NFSROOT/$d 2>/dev/null
 
193
    done
 
194
    set -e
 
195
}
 
196
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
197
create_iso() {
 
198
 
 
199
    # Create the El Torito bootable iso9660 cdrom image
 
200
 
 
201
    echo "Mounting all needed parts"
 
202
 
 
203
    mkdir -p $NFSROOT/media/mirror
 
204
    mount --bind $tmp/boot $NFSROOT/boot
 
205
    mount --bind $FAI_CONFIGDIR $NFSROOT/fai && echo "Config space $FAI_CONFIGDIR mounted"
 
206
    mount --bind $mirrordir $NFSROOT/media/mirror && echo "Mirror $mirrordir mounted"
 
207
# TODO: customize /etc/apt, copy apt preferences etc.
 
208
 
 
209
    # this will be the sources.list for the CD
 
210
    tmp1=$(mktemp)
 
211
    cat > $tmp1 <<EOF
 
212
# mirror location for fai CD, file generated by fai-cd
 
213
 
 
214
deb file:/media/mirror testing main contrib non-free
 
215
deb file:/fai/files packages/
 
216
EOF
 
217
    mount --bind $tmp1 $NFSROOT/etc/apt/sources.list
 
218
    customize_nfsroot
 
219
    echo "Writing FAI CD-ROM image to $isoname. This may need some time."
 
220
    mkisofs -V "$vname" -A "$aname" -log-file /dev/null -quiet -RU -b boot/$bimagesize.image -c boot.catalog -o $isoname $NFSROOT
 
221
    echo -n "ISO image size and filename: "; du -h $isoname
 
222
    rm $tmp/boot/$bimagesize.image $tmp1
 
223
    unhide_dirs
 
224
    umount_dirs
 
225
}
 
226
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
227
burniso() {
 
228
 
 
229
    cdrecord -v -eject $isoname
 
230
}
 
231
# - - - - - - - - - - - - - - - - - - - - - - - - - -
 
232
# main program
 
233
 
 
234
# Parse commandline options
 
235
while getopts "hg:bm:" opt ; do
 
236
    case "$opt" in
 
237
        h)  usage ;;
 
238
        g)  grub_config="$OPTARG" ;;
 
239
        m)  mirrordir="$OPTARG" ;;
 
240
        b)  burn=1 ;;
 
241
        ?)  usage ;;
 
242
    esac
 
243
done
 
244
shift $(($OPTIND - 1))
 
245
isoname=$1
 
246
 
 
247
[ "$#" -eq 0 ]        && die 2 "Please specify the output file for the ISO image."
 
248
[ -z "$mirrordir" ]   && die 4 "Please specify the directory of your mirror using -m"
 
249
[ -d "$mirrordir" ]   || die 5 "$mirrordir is not a directory"
 
250
[ -f "$isoname" ]     && die 3 "Outputfile $isoname already exists. Please remove it."
 
251
[ -f "$grub_config" ] || die 6 "Grub menu file $grub_config not found."
 
252
[ `id -u` -ne 0 ]     && die 9 "Run this program as root."
 
253
 
 
254
[ -x "$(which mkinitrd-cd)" ] || die 4 "mkinitrd-cd not found. Please install package."
 
255
tmp=$(mktemp -t -d fai-cd.XXXXXX)
 
256
kernelversion=$(ls -tr $NFSROOT/boot/vmlinu?-* | tail -1 | sed -e 's#.*/vmlinuz-##')
 
257
 
 
258
create_initrd_image
 
259
create_grub_image
 
260
create_iso
 
261
rm -rf $tmp
 
262
[ "$burn" -eq 1 ] && burniso
 
263
exit 0