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