figures where modprobe.conf.dist gets installed
[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 # The reference image is expected to have been built by prep.sh (see .spec)
61 # we disable the initial logic that called prep.sh if that was not the case
62 # this is because prep.sh needs to know pldistro 
63 if [ ! -f $isofs/bootcd.img -o ! -f build/version.txt ] ; then
64     echo "you have to run prep.sh prior to calling $0 - exiting"
65     exit 1
66 fi
67
68 # build/version.txt written by prep.sh
69 BOOTCD_VERSION=$(cat build/version.txt)
70
71 if [ -f /etc/planetlab/plc_config ] ; then
72     # Source PLC configuration
73     . /etc/planetlab/plc_config
74 fi
75
76 ### This support for backwards compatibility can be taken out in the
77 ### future. RC1 based MyPLCs set $PLC_BOOT_SSL_CRT in the plc_config
78 ### file, but >=RC2 based bootcd assumes that $PLC_BOOT_CA_SSL_CRT is
79 ### set.
80 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a ! -z "$PLC_BOOT_SSL_CRT" ] ; then
81     PLC_BOOT_CA_SSL_CRT=$PLC_BOOT_SSL_CRT
82 fi
83
84 # If PLC configuration is not valid, try a static configuration
85 if [ -z "$PLC_BOOT_CA_SSL_CRT" -a -d configurations/$CONFIGURATION ] ; then
86     # (Deprecated) Source static configuration
87     . configurations/$CONFIGURATION/configuration
88     PLC_NAME="PlanetLab"
89     PLC_MAIL_SUPPORT_ADDRESS="support@planet-lab.org"
90     PLC_WWW_HOST="www.planet-lab.org"
91     PLC_WWW_PORT=80
92     if [ -n "$EXTRA_VERSION" ] ; then
93         BOOTCD_VERSION="$BOOTCD_VERSION $EXTRA_VERSION"
94     fi
95     PLC_BOOT_HOST=$PRIMARY_SERVER
96     PLC_BOOT_SSL_PORT=$PRIMARY_SERVER_PORT
97     PLC_BOOT_CA_SSL_CRT=configurations/$CONFIGURATION/$PRIMARY_SERVER_CERT
98     PLC_ROOT_GPG_KEY_PUB=configurations/$CONFIGURATION/$PRIMARY_SERVER_GPG
99 fi
100
101 FULL_VERSION_STRING="$PLC_NAME BootCD $BOOTCD_VERSION"
102
103 echo "* Building images for $FULL_VERSION_STRING"
104
105 # From within a myplc chroot /usr/tmp is too small 
106 # to build all possible images, whereas /data is part of the host
107 # filesystem and usually has sufficient space.  What we
108 # should do is check whether the expected amount of space
109 # is available.
110 BUILDTMP=/usr/tmp
111 if [ -d /data ] ; then
112         isreadonly=$(mktemp /data/isreadonly.XXXXXX || /bin/true)
113         if [ -n "$isreadonly" ] ; then
114                 rm -f "$isreadonly"
115                 BUILDTMP=/data
116         fi
117 fi
118
119 # Root of the ISO and USB images
120 echo "* Populating root filesystem..."
121 overlay=$(mktemp -d ${BUILDTMP}/overlay.XXXXXX)
122 install -d -m 755 $overlay
123 trap "rm -rf $overlay" ERR INT
124
125 # Create version files
126 echo "* Creating version files"
127
128 # Boot Manager compares pl_version in both places to make sure that
129 # the right CD is mounted. We used to boot from an initrd and mount
130 # the CD on /usr. Now we just run everything out of the initrd.
131 for file in $overlay/pl_version $overlay/usr/isolinux/pl_version ; do
132     mkdir -p $(dirname $file)
133     echo "$FULL_VERSION_STRING" >$file
134 done
135
136 # Install boot server configuration files
137 echo "* Installing boot server configuration files"
138
139 # We always intended to bring up and support backup boot servers,
140 # but never got around to it. Just install the same parameters for
141 # both for now.
142 for dir in $overlay/usr/boot $overlay/usr/boot/backup ; do
143         install -D -m 644 $PLC_BOOT_CA_SSL_CRT $dir/cacert.pem
144         install -D -m 644 $PLC_ROOT_GPG_KEY_PUB $dir/pubring.gpg
145         echo "$PLC_BOOT_HOST" >$dir/boot_server
146         echo "$PLC_BOOT_SSL_PORT" >$dir/boot_server_port
147         echo "/boot/" >$dir/boot_server_path
148 done
149
150 # (Deprecated) Install old-style boot server configuration files
151 install -D -m 644 $PLC_BOOT_CA_SSL_CRT $overlay/usr/bootme/cacert/$PLC_BOOT_HOST/cacert.pem
152 echo "$FULL_VERSION_STRING" >$overlay/usr/bootme/ID
153 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER
154 echo "$PLC_BOOT_HOST" >$overlay/usr/bootme/BOOTSERVER_IP
155 echo "$PLC_BOOT_SSL_PORT" >$overlay/usr/bootme/BOOTPORT
156
157 # Generate /etc/issue
158 echo "* Generating /etc/issue"
159
160 if [ "$PLC_WWW_PORT" = "443" ] ; then
161     PLC_WWW_URL="https://$PLC_WWW_HOST/"
162 elif [ "$PLC_WWW_PORT" != "80" ] ; then
163     PLC_WWW_URL="http://$PLC_WWW_HOST:$PLC_WWW_PORT/"
164 else
165     PLC_WWW_URL="http://$PLC_WWW_HOST/"
166 fi
167
168 mkdir -p $overlay/etc
169 cat >$overlay/etc/issue <<EOF
170 $FULL_VERSION_STRING
171 $PLC_NAME Node: \n
172 Kernel \r on an \m
173 $PLC_WWW_URL
174
175 This machine is a node in the $PLC_NAME distributed network.  It has
176 not fully booted yet. If you have cancelled the boot process at the
177 request of $PLC_NAME Support, please follow the instructions provided
178 to you. Otherwise, please contact $PLC_MAIL_SUPPORT_ADDRESS.
179
180 Console login at this point is restricted to root. Provide the root
181 password of the default $PLC_NAME Central administrator account at the
182 time that this CD was created.
183
184 EOF
185
186 # Set root password
187 echo "* Setting root password"
188
189 if [ -z "$ROOT_PASSWORD" ] ; then
190     # Generate an encrypted password with crypt() if not defined
191     # in a static configuration.
192     ROOT_PASSWORD=$(python <<EOF
193 import crypt, random, string
194 salt = [random.choice(string.letters + string.digits + "./") for i in range(0,8)]
195 print crypt.crypt('$PLC_ROOT_PASSWORD', '\$1\$' + "".join(salt) + '\$')
196 EOF
197 )
198 fi
199
200 # build/passwd copied out by prep.sh
201 sed -e "s@^root:[^:]*:\(.*\)@root:$ROOT_PASSWORD:\1@" build/passwd \
202     >$overlay/etc/passwd
203
204 # Install node configuration file (e.g., if node has no floppy disk or USB slot)
205 if [ -f "$NODE_CONFIGURATION_FILE" ] ; then
206     echo "* Installing node configuration file"
207     install -D -m 644 $NODE_CONFIGURATION_FILE $overlay/usr/boot/plnode.txt
208 fi
209
210 # Pack overlay files into a compressed archive
211 echo "* Compressing overlay image"
212 (cd $overlay && find . | cpio --quiet -c -o) | gzip -9 >$isofs/overlay.img
213
214 rm -rf $overlay
215 trap - ERR INT
216
217 # Calculate ramdisk size (total uncompressed size of both archives)
218 ramdisk_size=$(gzip -l $isofs/bootcd.img $isofs/overlay.img | tail -1 | awk '{ print $2; }') # bytes
219 ramdisk_size=$((($ramdisk_size + 1023) / 1024)) # kilobytes
220
221 # Write isolinux configuration
222 echo "$FULL_VERSION_STRING" >$isofs/pl_version
223 cat >$isofs/isolinux.cfg <<EOF
224 DEFAULT kernel
225 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
226 DISPLAY pl_version
227 PROMPT 0
228 TIMEOUT 40
229 EOF
230
231 # Change back to output directory
232 popd
233
234 # Create ISO image
235 echo "* Creating ISO image"
236 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
237 mkisofs -o "$iso" \
238     -R -allow-leading-dots -J -r \
239     -b isolinux.bin -c boot.cat \
240     -no-emul-boot -boot-load-size 4 -boot-info-table \
241     $isofs
242
243 echo "* Creating ISO image with serial line support"
244 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.iso"
245 cat >$isofs/isolinux.cfg <<EOF
246 SERIAL 0 115200
247 PROMPT 0
248 TIMEOUT 120
249 DISPLAY pl_version
250 DEFAULT serial
251 LABEL serial
252         KERNEL kernel
253         APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw  console=ttyS0,115200n8
254 EOF
255 mkisofs -o "$iso" \
256     -R -allow-leading-dots -J -r \
257     -b isolinux.bin -c boot.cat \
258     -no-emul-boot -boot-load-size 4 -boot-info-table \
259     $isofs
260
261 # Create USB image
262 echo -n "* Creating USB image... "
263 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
264
265 # Leave 1 MB of free space on the VFAT filesystem
266 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
267
268 # Mount it
269 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
270 mount -o loop "$usb" $tmp
271 trap "umount $tmp; rm -rf $tmp" ERR INT
272
273 # Populate it
274 echo -n " populating USB image... "
275 (cd $isofs && find . | cpio -p -d -u $tmp/)
276
277 # Use syslinux instead of isolinux to make the image bootable
278 rm -f $tmp/isolinux.cfg
279 cat >$tmp/syslinux.cfg <<EOF
280 DEFAULT kernel
281 APPEND ramdisk_size=$ramdisk_size initrd=bootcd.img,overlay.img root=/dev/ram0 rw
282 DISPLAY pl_version
283 PROMPT 0
284 TIMEOUT 40
285 EOF
286 umount $tmp
287 rmdir $tmp
288 trap - ERR INT
289
290 echo "making USB image bootable."
291 $srcdir/syslinux/unix/syslinux "$usb"
292
293
294 # Create USB image with serial line support
295 echo -n "* Creating USB image... "
296 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-serial.usb"
297
298 # Leave 1 MB of free space on the VFAT filesystem
299 mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
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 ./etc | awk '{ print $1 }')
398 let msize=($vsize+$esize+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 $cramfs
435
436 #create the cramfs image
437 echo "* Creating cramfs image"
438 mkfs.cramfs $tmp/ $cramfs
439 # Leave 1 MB of free space on the VFAT filesystem
440 cramfs_size=$(($(du -sk $cramfs | awk '{ print $1; }')))
441 mv $cramfs ${BUILDTMP}/cramfs.img
442 rm -rf $tmp
443 trap - ERR INT
444
445 # Create ISO CRAMFS image
446 echo "* Creating ISO CRAMFS-based image"
447 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.iso"
448
449 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
450 trap "$tmp; rm -rf $tmp" ERR INT
451 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
452 cat >$tmp/isolinux.cfg <<EOF
453 DEFAULT kernel
454 APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
455 DISPLAY pl_version
456 PROMPT 0
457 TIMEOUT 40
458 EOF
459
460 cp ${BUILDTMP}/cramfs.img $tmp
461 mkisofs -o "$iso" \
462     -R -allow-leading-dots -J -r \
463     -b isolinux.bin -c boot.cat \
464     -no-emul-boot -boot-load-size 4 -boot-info-table \
465     $tmp
466
467 # Create ISO CRAMFS image with serial line support
468 echo "* Creating ISO image with cramfs and serial line support"
469 cat >$tmp/isolinux.cfg <<EOF
470 SERIAL 0 115200
471 PROMPT 0
472 TIMEOUT 120
473 DISPLAY pl_version
474 DEFAULT serial
475 LABEL serial
476         KERNEL kernel
477         APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro  console=ttyS0,115200n8
478 EOF
479
480 iso="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.iso"
481 mkisofs -o "$iso" \
482     -R -allow-leading-dots -J -r \
483     -b isolinux.bin -c boot.cat \
484     -no-emul-boot -boot-load-size 4 -boot-info-table \
485     $tmp
486
487 rm -rf $tmp
488 trap - ERR INT
489
490 # Create USB CRAMFS based image
491 echo "* Creating USB CRAMFS based image"
492 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs.usb"
493
494 # leave 1MB of space on the USB VFAT
495 let vfat_size=${cramfs_size}+2048
496
497 # Make VFAT filesystem for USB
498 mkfs.vfat -C "$usb" $vfat_size
499
500 # Mount it
501 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
502 mount -o loop "$usb" $tmp
503 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
504
505 # Populate it
506 echo "* Populating USB with overlay images and cramfs"
507 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
508 cp ${BUILDTMP}/cramfs.img $tmp/
509
510 # Use syslinux instead of isolinux to make the image bootable
511 cat >$tmp/syslinux.cfg <<EOF
512 TIMEOUT 120
513 DISPLAY pl_version
514 DEFAULT vga
515 LABEL vga
516         KERNEL kernel
517         APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro
518 EOF
519 umount $tmp
520 rmdir $tmp
521 trap - ERR INT
522
523 echo "* Making USB CRAMFS based image bootable"
524 $srcdir/syslinux/unix/syslinux "$usb"
525
526 # Create USB CRAMFS based image w/ serial line support
527 echo "* Creating USB CRAMFS based image w/ serial line support"
528 usb="$PLC_NAME-BootCD-$BOOTCD_VERSION-cramfs-serial.usb"
529
530 # leave 4MB of space on the USB VFAT
531 let vfat_size=${cramfs_size}+2048
532
533 # Make VFAT filesystem for USB
534 mkfs.vfat -C "$usb" $vfat_size
535
536 # Mount it
537 tmp=$(mktemp -d ${BUILDTMP}/bootcd.XXXXXX)
538 mount -o loop "$usb" $tmp
539 trap "umount $tmp; rm -rf $tmp ${BUILDTMP}/cramfs.img" ERR INT
540
541 # Populate it
542 echo "* Populating USB with overlay images and cramfs"
543 (cd $isofs && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
544 cp ${BUILDTMP}/cramfs.img $tmp/
545
546 # Use syslinux instead of isolinux to make the image bootable
547 cat >$tmp/syslinux.cfg <<EOF
548 SERIAL 0 9600
549 PROMPT 0
550 TIMEOUT 120
551 DISPLAY pl_version
552 DEFAULT serial
553 LABEL serial
554         KERNEL kernel
555         APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro  console=ttyS0,9600n8
556 EOF
557 umount $tmp
558 rmdir $tmp
559 trap - ERR INT
560
561 echo "* Making USB CRAMFS based image /w serial line support bootable"
562 $srcdir/syslinux/unix/syslinux "$usb"
563
564 exit 0