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