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