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