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