- revert to 1.36 (install boot server cert, which may optionally be a
[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$
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 elif [ -d configurations/$CONFIGURATION ] ; then
68     # (Deprecated) Source static configuration
69     . configurations/$CONFIGURATION/configuration
70     PLC_NAME="PlanetLab"
71     PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
72     PLC_WWW_HOST="www.planet-lab.org"
73     PLC_WWW_PORT=80
74     if [ -n "$EXTRA_VERSION" ] ; then
75         BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
76     fi
77     PLC_BOOT_HOST=$PRIMARY_SERVER
78     PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
79     PLC_BOOT_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
80     PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
81 fi
82
83 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
84
85 # Root of the ISO and USB images
86 overlay=$(mktemp -d /tmp/overlay.XXXXXX)
87 install -d -m 755 $overlay
88 trap "rm -rf $overlay" ERR
89
90 # Create version files
91 echo "* Creating version files"
92
93 # Boot Manager compares pl_version in both places to make sure that
94 # the right CD is mounted. We used to boot from an initrd and mount
95 # the CD on /usr. Now we just run everything out of the initrd.
96 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
97     mkdir -p $(dirname $file)
98     echo "$FULL_VERSION_STRING" >$file
99 done
100
101 # Install boot server configuration files
102 echo "* Installing boot server configuration files"
103
104 # We always intended to bring up and support backup boot servers,
105 # but never got around to it. Just install the same parameters for
106 # both for now.
107 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
108         install -D -m 644 $PLC_BOOT_SSL_CRT $dir/cacert.pem
109         install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
110         echo "$PLC_BOOT_HOST" >$dir/boot_server
111         echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
112         echo "/boot/" >$dir/boot_server_path
113 done
114
115 # (Deprecated) Install old-style boot server configuration files
116 install -D -m 644 $PLC_BOOT_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
117 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
118 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
119 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
120 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
121
122 # Generate /etc/issue
123 echo "* Generating /etc/issue"
124
125 if [ "$PLC_WWW_PORT" = "443" ] ; then
126     PLC_WWW_URL="https://$PLC_WWW_HOST/"
127 elif [ "$PLC_WWW_PORT" != "80" ] ; then
128     PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
129 else
130     PLC_WWW_URL="http://$PLC_WWW_HOST/"
131 fi
132
133 mkdir -p $overlay/etc
134 cat >$overlay/etc/issue <<EOF
135 $FULL_VERSION_STRING
136 $PLC_NAME Node: \n
137 Kernel \r on an \m
138 $PLC_WWW_URL
139
140 This machine is a node in the $PLC_NAME distributed network.  It has
141 not fully booted yet. If you have cancelled the boot process at the
142 request of $PLC_NAME Support, please follow the instructions provided
143 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
144
145 Console login at this point is restricted to root. Provide the root
146 password of the default $PLC_NAME Central administrator account at the
147 time that this CD was created.
148
149 EOF
150
151 # Set root password
152 echo "* Setting root password"
153
154 if [ -z "$ROOT_PASSWORD" ] ; then
155     # Generate an encrypted password with crypt() if not defined
156     # in a static configuration.
157     ROOT_PASSWORD=$(python <<EOF
158 import crypt, random, string
159 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
160 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
161 EOF
162 )
163 fi
164
165 # build/passwd copied out by prep.sh
166 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
167     >$overlay/etc/passwd
168
169 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
170 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
171     echo "* Installing node configuration file"
172     install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
173 fi
174
175 # Pack overlay files into a compressed archive
176 echo "* Compressing overlay image"
177 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
178
179 rm -rf $overlay
180 trap - ERR
181
182 # Calculate ramdisk size (total uncompressed size of both archives)
183 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img | tail -1 | awk '{ print $2; }') # bytes
184 ramdisk_size=$(($ramdisk_size / 1024)) # kilobytes
185
186 # Write isolinux configuration
187 echo "$FULL_VERSION_STRING" >$isofs/pl_version
188 cat >$isofs/isolinux.cfg <<EOF
189 DEFAULT kernel
190 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
191 DISPLAY pl_version
192 PROMPT 0
193 TIMEOUT 40
194 EOF
195
196 # Change back to output directory
197 popd
198
199 # Create ISO image
200 echo "* Creating ISO image"
201 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
202 mkisofs -o "$iso" \
203     -R -allow-leading-dots -J -r \
204     -b isolinux.bin -c boot.cat \
205     -no-emul-boot -boot-load-size 4 -boot-info-table \
206     $isofs
207
208 # Create USB image
209 echo "* Creating USB image"
210 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
211
212 # Leave 1 MB of free space on the VFAT filesystem
213 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
214
215 # Mount it
216 tmp=$(mktemp -d /tmp/bootcd.XXXXXX)
217 mount -o loop "$usb" $tmp
218 trap "umount $tmp; rm -rf $tmp" ERR
219
220 # Populate it
221 echo "* Populating USB image"
222 (cd $isofs && find . | cpio -p -d -u $tmp/)
223
224 # Use syslinux instead of isolinux to make the image bootable
225 mv $tmp/isolinux.cfg $tmp/syslinux.cfg
226 umount $tmp
227 rmdir $tmp
228 trap - ERR
229
230 echo "* Making USB image bootable"
231 $srcdir/syslinux/unix/syslinux "$usb"
232
233 exit 0