merge to HEAD as of 2006-08-21
[bootcd.git] / build.sh
1 #!/bin/bash
2 #
3 # Builds custom BootCD ISO and USB images in the current
4 # directory. For backward compatibility, if an old-style static
5 # configuration is specified, that configuration file will be parsed
6 # instead of the current PLC configuration in
7 # /etc/planetlab/plc_config.
8 #
9 # Aaron Klingaman <alk@absarokasoft.com>
10 # Mark Huang <mlhuang@cs.princeton.edu>
11 # Copyright (C) 2004-2006 The Trustees of Princeton University
12 #
13 # $Id: build.sh,v 1.40 2006/07/25 23:51:39 mlhuang Exp $
14 #
15
16 PATH=/sbin:/bin:/usr/sbin:/usr/bin
17
18 CONFIGURATION=default
19 NODE_CONFIGURATION_FILE=
20
21 usage()
22 {
23     echo "Usage: build.sh [OPTION]..."
24     echo "      -c name         (Deprecated) Static configuration to use (default: $CONFIGURATION)"
25     echo "      -f planet.cnf   Node to customize CD for (default: none)"
26     echo "      -h              This message"
27     exit 1
28 }
29
30 # Get options
31 while getopts "c:f:h" opt ; do
32     case $opt in
33         c)
34             CONFIGURATION=$OPTARG
35             ;;
36         f)
37             NODE_CONFIGURATION_FILE=$OPTARG
38             ;;
39         h|*)
40             usage
41             ;;
42     esac
43 done
44
45 # Do not tolerate errors
46 set -e
47
48 # Change to our source directory
49 srcdir=$(cd $(dirname $0) && pwd -P)
50 pushd $srcdir
51
52 # Root of the isofs
53 isofs=$PWD/build/isofs
54
55 # Build reference image if it does not exist. This should only need to
56 # be executed once at build time, never at run time.
57 if [ ! -f $isofs/bootcd.img ] ; then
58     ./prep.sh
59 fi
60
61 # build/version.txt written by prep.sh
62 BOOTCD_VERSION=$(cat build/version.txt)
63
64 if [ -f /etc/planetlab/plc_config ] ; then
65     # Source PLC configuration
66     . /etc/planetlab/plc_config
67 fi
68
69 # If PLC configuration is not valid, try a static configuration
70 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
71     # (Deprecated) Source static configuration
72     . configurations/$CONFIGURATION/configuration
73     PLC_NAME="PlanetLab"
74     PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
75     PLC_WWW_HOST="www.planet-lab.org"
76     PLC_WWW_PORT=80
77     if [ -n "$EXTRA_VERSION" ] ; then
78         BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
79     fi
80     PLC_BOOT_HOST=$PRIMARY_SERVER
81     PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
82     PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
83     PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
84 fi
85
86 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
87
88 # Root of the ISO and USB images
89 overlay=$(mktemp -d /tmp/overlay.XXXXXX)
90 install -d -m 755 $overlay
91 trap "rm -rf $overlay" ERR INT
92
93 # Create version files
94 echo "* Creating version files"
95
96 # Boot Manager compares pl_version in both places to make sure that
97 # the right CD is mounted. We used to boot from an initrd and mount
98 # the CD on /usr. Now we just run everything out of the initrd.
99 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
100     mkdir -p $(dirname $file)
101     echo "$FULL_VERSION_STRING" >$file
102 done
103
104 # Install boot server configuration files
105 echo "* Installing boot server configuration files"
106
107 # We always intended to bring up and support backup boot servers,
108 # but never got around to it. Just install the same parameters for
109 # both for now.
110 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
111         install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
112         install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
113         echo "$PLC_BOOT_HOST" >$dir/boot_server
114         echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
115         echo "/boot/" >$dir/boot_server_path
116 done
117
118 # (Deprecated) Install old-style boot server configuration files
119 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
120 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
121 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
122 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
123 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
124
125 # Generate /etc/issue
126 echo "* Generating /etc/issue"
127
128 if [ "$PLC_WWW_PORT" = "443" ] ; then
129     PLC_WWW_URL="https://$PLC_WWW_HOST/"
130 elif [ "$PLC_WWW_PORT" != "80" ] ; then
131     PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
132 else
133     PLC_WWW_URL="http://$PLC_WWW_HOST/"
134 fi
135
136 mkdir -p $overlay/etc
137 cat >$overlay/etc/issue <<EOF
138 $FULL_VERSION_STRING
139 $PLC_NAME Node: \n
140 Kernel \r on an \m
141 $PLC_WWW_URL
142
143 This machine is a node in the $PLC_NAME distributed network.  It has
144 not fully booted yet. If you have cancelled the boot process at the
145 request of $PLC_NAME Support, please follow the instructions provided
146 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
147
148 Console login at this point is restricted to root. Provide the root
149 password of the default $PLC_NAME Central administrator account at the
150 time that this CD was created.
151
152 EOF
153
154 # Set root password
155 echo "* Setting root password"
156
157 if [ -z "$ROOT_PASSWORD" ] ; then
158     # Generate an encrypted password with crypt() if not defined
159     # in a static configuration.
160     ROOT_PASSWORD=$(python <<EOF
161 import crypt, random, string
162 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
163 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
164 EOF
165 )
166 fi
167
168 # build/passwd copied out by prep.sh
169 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
170     >$overlay/etc/passwd
171
172 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
173 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
174     echo "* Installing node configuration file"
175     install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
176 fi
177
178 # Pack overlay files into a compressed archive
179 echo "* Compressing overlay image"
180 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
181
182 rm -rf $overlay
183 trap - ERR INT
184
185 # Calculate ramdisk size (total uncompressed size of both archives)
186 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img | tail -1 | awk '{ print $2; }') # bytes
187 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
188
189 # Write isolinux configuration
190 echo "$FULL_VERSION_STRING" >$isofs/pl_version
191 cat >$isofs/isolinux.cfg <<EOF
192 DEFAULT kernel
193 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
194 DISPLAY pl_version
195 PROMPT 0
196 TIMEOUT 40
197 EOF
198
199 # Change back to output directory
200 popd
201
202 # Create ISO image
203 echo "* Creating ISO image"
204 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
205 mkisofs -o "$iso" \
206     -R -allow-leading-dots -J -r \
207     -b isolinux.bin -c boot.cat \
208     -no-emul-boot -boot-load-size 4 -boot-info-table \
209     $isofs
210
211 # Create USB image
212 echo "* Creating USB image"
213 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
214
215 # Leave 1 MB of free space on the VFAT filesystem
216 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
217
218 # Mount it
219 tmp=$(mktemp -d /tmp/bootcd.XXXXXX)
220 mount -o loop "$usb" $tmp
221 trap "umount $tmp; rm -rf $tmp" ERR INT
222
223 # Populate it
224 echo "* Populating USB image"
225 (cd $isofs && find . | cpio -p -d -u $tmp/)
226
227 # Use syslinux instead of isolinux to make the image bootable
228 mv $tmp/isolinux.cfg $tmp/syslinux.cfg
229 umount $tmp
230 rmdir $tmp
231 trap - ERR INT
232
233 echo "* Making USB image bootable"
234 $srcdir/syslinux/unix/syslinux "$usb"
235
236 exit 0