isolinux.bin needs to be writable.
[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-2007 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 TYPES="usb iso usb_serial iso_serial"
21 # Leave 4 MB of free space
22 FREE_SPACE=4096
23 CUSTOM_DIR=
24 OUTPUT_BASE=
25 MKISOFS_OPTS="-R -J -r -f -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table"
26
27 usage()
28 {
29     echo "Usage: build.sh [OPTION]..."
30     echo "    -c name          (Deprecated) Static configuration to use (default: $CONFIGURATION)"
31     echo "    -f planet.cnf    Node to customize CD for (default: none)"
32     echo "    -t 'types'       Build the specified images (default: $TYPES)"
33     echo "    -a               Build all supported images"
34     echo "    -C custom-dir    Custom directory"
35     echo "    -O output-base   The basename of the generated files (default: PLC_NAME-BootCD-VERSION)"
36     echo "    -h               This message"
37     exit 1
38 }
39
40 # Get options
41 while getopts "O:c:f:t:C:ah" opt ; do
42     case $opt in
43     c)
44         CONFIGURATION=$OPTARG
45         ;;
46     f)
47         NODE_CONFIGURATION_FILE=$OPTARG
48         ;;
49     t)
50         TYPES="$OPTARG"
51         ;;
52     C)
53         CUSTOM_DIR="$OPTARG"
54         ;;
55     O)
56         OUTPUT_BASE="$OPTARG"
57         ;;
58     a)
59         TYPES="usb iso usb_serial iso_serial usb_cramfs iso_cramfs usb_cramfs_serial iso_cramfs_serial"
60         ;;
61     h|*)
62         usage
63         ;;
64     esac
65 done
66
67 # Do not tolerate errors
68 set -e
69
70 # Change to our source directory
71 srcdir=$(cd $(dirname $0) && pwd -P)
72 pushd $srcdir
73
74 # Root of the isofs
75 isofs=$PWD/build/isofs
76
77 # The reference image is expected to have been built by prep.sh (see .spec)
78 # we disable the initial logic that called prep.sh if that was not the case
79 # this is because prep.sh needs to know pldistro 
80 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
81     echo "You have to run prep.sh prior to calling $0 - exiting"
82     exit 1
83 fi
84
85 # build/version.txt written by prep.sh
86 BOOTCD_VERSION=$(cat build/version.txt)
87
88 if [ -f /etc/planetlab/plc_config ] ; then
89     # Source PLC configuration
90     . /etc/planetlab/plc_config
91 fi
92
93 ### This support for backwards compatibility can be taken out in the
94 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
95 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
96 ### set.
97 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
98     PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
99 fi
100
101 # If PLC configuration is not valid, try a static configuration
102 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
103     # (Deprecated) Source static configuration
104     . configurations/$CONFIGURATION/configuration
105     PLC_NAME="PlanetLab"
106     PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
107     PLC_WWW_HOST="www.planet-lab.org"
108     PLC_WWW_PORT=80
109     if [ -n "$EXTRA_VERSION" ] ; then
110     BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
111     fi
112     PLC_BOOT_HOST=$PRIMARY_SERVER
113     PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
114     PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
115     PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
116 fi
117
118 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
119
120 echo "* Building images for $FULL_VERSION_STRING"
121
122 # From within a myplc chroot /usr/tmp is too small 
123 # to build all possible images, whereas /data is part of the host
124 # filesystem and usually has sufficient space.  What we
125 # should do is check whether the expected amount of space
126 # is available.
127 BUILDTMP=/usr/tmp
128 if [ -d /data/tmp ] ; then
129     isreadonly=$(mktemp /data/tmp/isreadonly.XXXXXX || /bin/true)
130     if [ -n "$isreadonly" ] ; then
131         rm -f "$isreadonly"
132         BUILDTMP=/data/tmp
133     fi
134 fi
135
136 declare -a _CLEANUPS=()
137 function do_cleanup()
138 {
139     cd /
140     for i in "${_CLEANUPS[@]}"; do
141         $i
142     done
143 }
144 function push_cleanup()
145 {
146     _CLEANUPS=( "${_CLEANUPS[@]}" "$*" )
147 }
148 function pop_cleanup()
149 {
150     unset _CLEANUPS[$((${#_CLEANUPS[@]} - 1))]
151 }
152
153 trap "do_cleanup" ERR INT EXIT
154
155 BUILDTMP=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
156 push_cleanup rm -fr "${BUILDTMP}"
157 mkdir "${BUILDTMP}/isofs"
158 for i in "$isofs"/{bootcd.img,kernel}; do
159     ln -s "$i" "${BUILDTMP}/isofs"
160 done
161 cp "$isofs/isolinux.bin" "${BUILDTMP}/isofs"
162 isofs="${BUILDTMP}/isofs"
163
164 # Root of the ISO and USB images
165 echo "* Populating root filesystem..."
166 overlay="${BUILDTMP}/overlay"
167 install -d -m 755 $overlay
168 push_cleanup rm -fr $overlay
169
170 # Create version files
171 echo "* Creating version files"
172
173 # Boot Manager compares pl_version in both places to make sure that
174 # the right CD is mounted. We used to boot from an initrd and mount
175 # the CD on /usr. Now we just run everything out of the initrd.
176 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
177     mkdir -p $(dirname $file)
178     echo "$FULL_VERSION_STRING" >$file
179 done
180
181 # Install boot server configuration files
182 echo "* Installing boot server configuration files"
183
184 # We always intended to bring up and support backup boot servers,
185 # but never got around to it. Just install the same parameters for
186 # both for now.
187 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
188     install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
189     install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
190     echo "$PLC_BOOT_HOST" >$dir/boot_server
191     echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
192     echo "/boot/" >$dir/boot_server_path
193 done
194
195 # (Deprecated) Install old-style boot server configuration files
196 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
197 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
198 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
199 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
200 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
201
202 # Generate /etc/issue
203 echo "* Generating /etc/issue"
204
205 if [ "$PLC_WWW_PORT" = "443" ] ; then
206     PLC_WWW_URL="https://$PLC_WWW_HOST/"
207 elif [ "$PLC_WWW_PORT" != "80" ] ; then
208     PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
209 else
210     PLC_WWW_URL="http://$PLC_WWW_HOST/"
211 fi
212
213 mkdir -p $overlay/etc
214 cat >$overlay/etc/issue <<EOF
215 $FULL_VERSION_STRING
216 $PLC_NAME Node: \n
217 Kernel \r on an \m
218 $PLC_WWW_URL
219
220 This machine is a node in the $PLC_NAME distributed network.  It has
221 not fully booted yet. If you have cancelled the boot process at the
222 request of $PLC_NAME Support, please follow the instructions provided
223 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
224
225 Console login at this point is restricted to root. Provide the root
226 password of the default $PLC_NAME Central administrator account at the
227 time that this CD was created.
228
229 EOF
230
231 # Set root password
232 echo "* Setting root password"
233
234 if [ -z "$ROOT_PASSWORD" ] ; then
235     # Generate an encrypted password with crypt() if not defined
236     # in a static configuration.
237     ROOT_PASSWORD=$(python <<EOF
238 import crypt, random, string
239 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
240 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
241 EOF
242 )
243 fi
244
245 # build/passwd copied out by prep.sh
246 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
247     >$overlay/etc/passwd
248
249 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
250 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
251     echo "* Installing node configuration file"
252     install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
253 fi
254
255 # Pack overlay files into a compressed archive
256 echo "* Compressing overlay image"
257 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
258
259 rm -rf $overlay
260 pop_cleanup
261
262 if [ -n "$CUSTOM_DIR" ]; then
263     echo "* Compressing custom image"
264     (cd "$CUSTOM_DIR" && find . | cpio --quiet -c -o) | gzip -9 >$isofs/custom.img
265 fi
266
267 # Calculate ramdisk size (total uncompressed size of both archives)
268 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img ${CUSTOM_DIR:+$isofs/custom.img} | tail -1 | awk '{ print $2; }') # bytes
269 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
270
271 echo "$FULL_VERSION_STRING" >$isofs/pl_version
272
273 popd
274
275 function build_iso()
276 {
277     local iso="$1"
278     local serial=$2
279     local custom=$3
280
281     # Write isolinux configuration
282     cat >$isofs/isolinux.cfg <<EOF
283 ${serial:+SERIAL 0 115200}
284 DEFAULT kernel
285 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
286 DISPLAY pl_version
287 PROMPT 0
288 TIMEOUT 40
289 EOF
290
291     # Create ISO image
292     echo "* Creating ISO image"
293     mkisofs -o "$iso" \
294         $MKISOFS_OPTS \
295         $isofs
296 }
297
298 # Create USB image
299 function build_usb()
300 {
301     echo -n "* Creating USB image... "
302     local usb="$1"
303     local serial=$2
304     local custom=$3
305
306     mkfs.vfat -C "$usb" $(($(du -Lsk $isofs | awk '{ print $1; }') + $FREE_SPACE))
307
308     # Populate it
309     echo -n " populating USB image... "
310     mcopy -bsQ -i "$usb" "$isofs"/* ::/
311
312     # Use syslinux instead of isolinux to make the image bootable
313     tmp="${BUILDTMP}/syslinux.cfg"
314     cat >$tmp <<EOF
315 ${serial:+SERIAL 0 115200}
316 DEFAULT kernel
317 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img${custom:+,custom.img} root=/dev/ram0 rw ${serial:+console=ttyS0,115200n8}
318 DISPLAY pl_version
319 PROMPT 0
320 TIMEOUT 40
321 EOF
322     mdel -i "$usb" ::/isolinux.cfg 2>/dev/null || :
323     mcopy -i "$usb" "$tmp" ::/syslinux.cfg
324     rm -f "$tmp"
325
326     echo "making USB image bootable."
327     $srcdir/syslinux/unix/syslinux "$usb"
328 }
329
330
331 # Setup CRAMFS related support
332 function prepare_cramfs()
333 {
334     [ -n "$CRAMFS_PREPARED" ] && return 0
335     local custom=$1
336
337     echo "* Setting up CRAMFS-based images"
338     local tmp="${BUILDTMP}/cramfs-tree"
339     mkdir -p "$tmp"
340     push_cleanup rm -rf $tmp
341     pushd $tmp
342     gzip -d -c $isofs/bootcd.img     | cpio -diu
343     gzip -d -c $isofs/overlay.img    | cpio -diu
344     [ -n "$custom" ] && \
345         gzip -d -c $isofs/custom.img | cpio -diu
346
347     # clean out unnecessary rpm lib
348     echo "* clearing var/lib/rpm/*"
349     rm -f var/lib/rpm/*
350
351     # bootcd requires this directory
352     mkdir -p mnt/confdevice
353
354     # relocate various directory to /tmp
355     rm -rf root
356     ln -fs /tmp/root root
357     ln -fs /sbin/init linuxrc 
358     ln -fs /tmp/resolv.conf etc/resolv.conf
359     ln -fs /tmp/etc/mtab etc/mtab
360
361     # have pl_rsysinit copy over appropriate etc & var directories into /tmp/etc/
362     # make /tmp/etc
363     echo "* renaming dirs in ./etc"
364     pushd etc
365     for dir in `find * -type d -prune | grep -v rc.d`; do
366         mv ${dir} ${dir}_o
367         ln -fs /tmp/etc/${dir} ${dir}
368     done
369     popd
370
371     echo "* renaming dirs in ./var"
372     # rename all top-level directories and put in a symlink to /tmp/var
373     pushd var
374     for dir in `find * -type d -prune`; do
375         mv ${dir} ${dir}_o
376         ln -fs /tmp/var/${dir} ${dir}
377     done
378     popd
379
380     # overwrite fstab to mount / as cramfs and /tmp as tmpfs
381     echo "* Overwriting etc/fstab to use cramfs and tmpfs"
382     rm -f ./etc/fstab
383     cat >./etc/fstab <<EOF
384 /dev/ram0     /              cramfs     ro              0 0
385 none          /dev/pts       devpts     gid=5,mode=620  0 0
386 none          /proc          proc       defaults        0 0
387 none          /sys           sysfs      defaults        0 0
388 EOF
389
390     pushd dev
391     rm -f console
392     mknod console c 5 1
393     #for i in 0 1 2 3 4 5 6 7 8; do rm -f ram${i} ; done
394     #for i in 0 1 2 3 4 5 6 7 8; do mknod ram${i} b 1 ${i} ; done
395     #ln -fs ram1 ram
396     #ln -fs ram0 ramdisk
397     popd
398
399     # update etc/inittab to start with pl_rsysinit
400     sed -i 's,pl_sysinit,pl_rsysinit,' etc/inittab
401
402     # modify inittab to have a serial console
403     echo "T0:23:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> etc/inittab
404     # and let root log in
405     echo "ttyS0" >> etc/securetty
406
407     # calculate the size of /tmp based on the size of /etc & /var + 8MB slack
408     etcsize=$(du -s ./etc | awk '{ print $1 }')
409     varsize=$(du -s ./var | awk '{ print $1 }')
410     let msize=($varsize+$etcsize+8192)/1024
411
412
413     # generate pl_rsysinit
414     cat > etc/rc.d/init.d/pl_rsysinit <<EOF
415 #!/bin/sh
416 # generated by build.sh
417 echo -n "pl_rsysinit: preparing /etc and /var for pl_sysinit..."
418 mount -t tmpfs -orw,size=${msize}M,mode=1777 tmpfs /tmp
419 mkdir -p /tmp/root
420 mkdir -p /tmp/etc
421 touch /tmp/etc/resolv.conf
422 touch /tmp/etc/mtab
423 mkdir -p /tmp/var
424
425 # make mtab happy
426 echo "tmpfs /tmp tmpfs rw,size=${msize}M,mode=1777 1 1" > /tmp/etc/mtab
427
428 # copy over directory contents of all _o directories from /etc and /var
429 # /tmp/etc and /tmp/var
430 pushd /etc
431 for odir in \$(cd /etc && ls -d *_o); do dir=\$(echo \$odir | sed 's,\_o$,,'); (mkdir -p /tmp/etc/\$dir && cd \$odir && find . | cpio -p -d -u /tmp/etc/\$dir); done
432 popd
433 pushd /var
434 for odir in \$(cd /var && ls -d *_o); do dir=\$(echo \$odir | sed 's,\_o$,,'); (mkdir -p /tmp/var/\$dir && cd \$odir && find . | cpio -p -d -u /tmp/var/\$dir); done
435 popd
436
437 echo "done"
438 # hand over to pl_sysinit
439 echo "pl_rsysinit: handing over to pl_sysinit"
440 /etc/init.d/pl_sysinit
441 EOF
442     chmod +x etc/rc.d/init.d/pl_rsysinit
443
444     popd
445
446     # create the cramfs image
447     echo "* Creating cramfs image"
448     mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
449     cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
450     rm -rf $tmp
451     pop_cleanup
452 }
453
454 # Create ISO CRAMFS image
455 function build_iso_cramfs()
456 {
457     local iso="$1"
458     local serial=$2
459     prepare_cramfs $3
460     echo "* Creating ISO CRAMFS-based image"
461
462     local tmp="${BUILDTMP}/cramfs-iso"
463     mkdir -p "$tmp"
464     push_cleanup rm -rf $tmp
465     (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
466     cat >$tmp/isolinux.cfg <<EOF
467 ${serial:+SERIAL 0 9600}
468 DEFAULT kernel
469 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
470 DISPLAY pl_version
471 PROMPT 0
472 TIMEOUT 40
473 EOF
474
475     cp ${BUILDTMP}/cramfs.img $tmp
476     mkisofs -o "$iso" \
477         "$MKISOFS_OPTS" \
478         $tmp
479
480     rm -fr "$tmp"
481     pop_cleanup
482 }
483
484 # Create USB CRAMFS based image
485 function build_usb_cramfs()
486 {
487     local usb="$1"
488     local serial=$2
489     prepare_cramfs $3
490     echo "* Creating USB CRAMFS based image"
491
492     let vfat_size=${cramfs_size}+$FREE_SPACE
493
494     # Make VFAT filesystem for USB
495     mkfs.vfat -C "$usb" $vfat_size
496
497     # Populate it
498     echo "* Populating USB with overlay images and cramfs"
499     mcopy -bsQ -i "$usb" $isofs/kernel $isofs/pl_version ::/
500     mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
501
502     # Use syslinux instead of isolinux to make the image bootable
503     tmp="${BUILDTMP}/syslinux.cfg"
504     cat >$tmp <<EOF
505 ${serial:+SERIAL 0 9600}
506 DEFAULT kernel
507 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${serial:+console=ttyS0,9600n8}
508 DISPLAY pl_version
509 PROMPT 0
510 TIMEOUT 40
511 EOF
512   mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
513   rm -f "$tmp"
514
515   echo "* Making USB CRAMFS based image bootable"
516   $srcdir/syslinux/unix/syslinux "$usb"
517 }
518
519 function type_to_name()
520 {
521     echo $1 | sed '
522         s/usb$/.usb/;
523         s/usb_serial$/-serial.usb/;
524         s/iso$/.iso/;
525         s/iso_serial$/-serial.iso/;
526         s/usb_cramfs$/-cramfs.usb/;
527         s/usb_cramfs_serial$/-cramfs-serial.usb/;
528         s/iso_cramfs$/-cramfs.iso/;
529         s/iso_cramfs_serial$/-cramfs-serial.iso/;
530         '
531 }
532
533 [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
534
535 for t in $TYPES; do
536     serial=
537     tname=`type_to_name $t`
538     if [[ "$t" == *_serial ]]; then
539         serial=1
540         t=`echo $t | sed 's/_serial$//'`
541     fi
542     build_$t "${OUTPUT_BASE}${tname}" $serial $CUSTOM_DIR
543 done
544
545 exit 0