- run yum-arch on Bootstrap RPMS directory before using
[bootcd.git] / build.sh
1 #!/bin/bash
2
3 set -e
4
5 # where the boot cd build config files are stored (and certificats/keys)
6 CONFIGURATIONS_DIR=configurations/
7
8 # where built files are stored
9 BUILD_DIR=build/
10
11 BOOTCD_VERSION="3.3"
12 FULL_VERSION_STRING="PlanetLab BootCD"
13 OUTPUT_IMAGE_NAME='PlanetLab-BootCD'
14     
15 SYSLINUX_SRC=sources/syslinux-2.11.tar.bz2
16
17 BOOTCD_YUM_GROUP=BootCD
18
19 CDRECORD_FLAGS="-v -dao"
20
21 CONF_FILES_DIR=conf_files/
22
23 # size of the ram disk in MB
24 RAMDISK_SIZE=64
25
26 # the bytes per inode ratio (the -i value in mkfs.ext2) for the ramdisk
27 INITRD_BYTES_PER_INODE=1024
28
29
30 # make sure the boot manager source is checked out in the same directory
31 # as the bootcd_v3 repository
32 for BOOTMANAGER_DIR in ../bootmanager-* ../bootmanager ; do
33     [ -d $BOOTMANAGER_DIR ] && break
34 done
35
36 if [ ! -d $BOOTMANAGER_DIR ]; then
37     echo "the bootmanager repository needs to be checked out at the same"
38     echo "level as this directory, for the merge_hw_tables.py script"
39     exit
40 fi
41
42
43 function usage()
44 {
45     echo "Usage: build.sh <action> [<configuration>]"
46     echo "Action: build burn clean"
47     echo
48     echo "If configuration is missing, 'default' is loaded"
49     exit
50 }
51
52
53 function build_cdroot()
54 {
55     if [ -f $CD_ROOT/.built ]; then
56         echo "cd root already built, skipping"
57         return
58     fi
59
60     clean
61     
62     mkdir -p $CD_ROOT/dev/pts
63     mkdir -p $CD_ROOT/proc
64     mkdir -p $CD_ROOT/etc
65
66     echo "copy fstab and mtab"
67     cp -f $CONF_FILES_DIR/fstab $CD_ROOT/etc/
68     cp -f $CONF_FILES_DIR/mtab $CD_ROOT/etc/
69
70     echo "setup rpm to install only en_US locale and no docs"
71     mkdir -p $CD_ROOT/etc/rpm
72     cp -f $CONF_FILES_DIR/macros $CD_ROOT/etc/rpm
73     # trick rpm and yum
74     export HOME=$PWD
75     cp -f $CONF_FILES_DIR/macros $PWD/.rpmmacros
76
77     echo "initialize rpm db"
78     mkdir -p $CD_ROOT/var/lib/rpm
79     rpm --root $CD_ROOT --initdb
80     
81     # XXX Should download yum.conf from the boot server?
82     echo "generate yum.conf"
83 cat >yum.conf <<EOF
84 [main]
85 cachedir=/var/cache/yum
86 debuglevel=2
87 logfile=/var/log/yum.log
88 pkgpolicy=newest
89 ### for yum-2.4 in fc4 (this will be ignored by yum-2.0)
90 ### everything in here, do not scan /etc/yum.repos.d/
91 reposdir=/dev/null
92
93 [FedoraCore2Base]
94 name=Fedora Core 2 Base -- PlanetLab Central
95 baseurl=http://$PRIMARY_SERVER/install-rpms/stock-fc2/
96
97 [FedoraCore2Updates]
98 name=Fedora Core 2 Updates -- PlanetLab Central
99 baseurl=http://$PRIMARY_SERVER/install-rpms/updates-fc2/
100
101 [PlanetLab]
102 name=PlanetLab RPMS -- PlanetLab Central
103 baseurl=http://$PRIMARY_SERVER/install-rpms/planetlab-rollout/
104 EOF
105     # XXX Temporary hack until the 3.2 rollout is complete and the
106     # /planetlab/yumgroups.xml file contains the BootCD group.
107     yumgroups="http://$PRIMARY_SERVER/install-rpms/planetlab-rollout/yumgroups.xml"
108
109    # Solve the bootstrap problem by including any just built packages in
110    # the yum configuration. This cooperates with the PlanetLab build
111    # system.
112    if [ -n "$RPM_BUILD_DIR" ] ; then
113        yum-arch $(dirname $RPM_BUILD_DIR)/RPMS
114        cat >>yum.conf <<EOF
115 [Bootstrap]
116 name=Bootstrap RPMS -- $(dirname $RPM_BUILD_DIR)/RPMS/
117 baseurl=file://$(dirname $RPM_BUILD_DIR)/RPMS/
118 EOF
119        yumgroups="file://$(dirname $RPM_BUILD_DIR)/RPMS/yumgroups.xml"
120    fi
121
122     echo "install boot cd base rpms"
123     yum -c yum.conf --installroot=$CD_ROOT -y groupinstall $BOOTCD_YUM_GROUP
124
125     # Retrieve all of the packagereq declarations in the BootCD group of the yumgroups.xml file
126     echo "checking to make sure rpms were installed"
127     packages=$(curl $yumgroups | sed -n -e '/<name>BootCD<\/name>/,/<name>/{ s/.*<packagereq.*>\(.*\)<\/packagereq>/\1/p }')
128     set +e
129     for package in $packages; do
130         echo "checking for package $package"
131         /usr/sbin/chroot $CD_ROOT /bin/rpm -qi $package > /dev/null
132         if [[ "$?" -ne 0 ]]; then
133             echo "package $package was not installed in the cd root."
134             echo "make sure it exists in the yum repository."
135             exit 1
136         fi
137     done
138     set -e
139     
140     echo "removing unneccessary build files"
141     (cd $CD_ROOT/lib/modules && \
142         find ./ -type d -name build -maxdepth 2 -exec rm -rf {} \;)
143
144     echo "setting up non-ssh authentication"
145     mkdir -p $CD_ROOT/etc/samba
146     /usr/sbin/chroot $CD_ROOT /usr/sbin/authconfig --nostart --kickstart \
147         --enablemd5 --enableshadow
148
149     echo "setting root password"
150     sed -i "s#root::#root:$ROOT_PASSWORD:#g" $CD_ROOT/etc/shadow
151
152     echo "relocate some large directories out of the root system"
153     # get /var/lib/rpm out, its 12mb. create in its place a 
154     # symbolic link to /usr/relocated/var/lib/rpm
155     mkdir -p $CD_ROOT/usr/relocated/var/lib/
156     mv $CD_ROOT/var/lib/rpm $CD_ROOT/usr/relocated/var/lib/
157     (cd $CD_ROOT/var/lib && ln -s ../../usr/relocated/var/lib/rpm rpm)
158
159     # get /var/cache/yum out, its 100Mb. create in its place a 
160     # symbolic link to /usr/relocated/var/cache/yum
161     mkdir -p $CD_ROOT/usr/relocated/var/cache/
162     mv $CD_ROOT/var/cache/yum $CD_ROOT/usr/relocated/var/cache/
163     (cd $CD_ROOT/var/cache && ln -s ../../usr/relocated/var/cache/yum yum)
164
165     # get /lib/tls out
166     mkdir -p $CD_ROOT/usr/relocated/lib
167     mv $CD_ROOT/lib/tls $CD_ROOT/usr/relocated/lib/
168     (cd $CD_ROOT/lib && ln -s ../usr/relocated/lib/tls tls)
169
170     echo "extracting syslinux, copying isolinux files to cd"
171     mkdir -p $CD_ROOT/usr/isolinux/
172     mkdir -p $BUILD_DIR/syslinux
173     tar -C $BUILD_DIR/syslinux -xjvf $SYSLINUX_SRC
174     find $BUILD_DIR/syslinux -name isolinux.bin \
175         -exec cp -f {} $CD_ROOT/usr/isolinux/ \;
176
177     echo "moving kernel to isolinux directory"
178     KERNEL=$CD_ROOT/boot/vmlinuz-*
179     mv -f $KERNEL $CD_ROOT/usr/isolinux/kernel
180
181     echo "moving /usr/bin/find and /usr/bin/dirname to /bin"
182     mv $CD_ROOT/usr/bin/find $CD_ROOT/bin/
183     mv $CD_ROOT/usr/bin/dirname $CD_ROOT/bin/
184
185     echo "creating version files"
186     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/pl_version
187     echo "$FULL_VERSION_STRING" > $CD_ROOT/pl_version
188
189     touch $CD_ROOT/.built
190 }
191
192 function init_initrd()
193 {
194     echo "initialize configuration files"
195
196     echo "copy fstab and mtab"
197     cp -f $CONF_FILES_DIR/fstab $CD_ROOT/etc/
198     cp -f $CONF_FILES_DIR/mtab $CD_ROOT/etc/
199
200     echo "installing generic modprobe.conf"
201     cp -f $CONF_FILES_DIR/modprobe.conf $CD_ROOT/etc/
202
203     echo "installing our own inittab and init scripts"
204     cp -f $CONF_FILES_DIR/inittab $CD_ROOT/etc
205     init_scripts="pl_sysinit pl_hwinit pl_netinit pl_validateconf pl_boot"
206     for script in $init_scripts; do
207         cp -f $CONF_FILES_DIR/$script $CD_ROOT/etc/init.d/
208         chmod +x $CD_ROOT/etc/init.d/$script
209     done
210
211     echo "setup basic networking files"
212     cp -f $CONF_FILES_DIR/hosts $CD_ROOT/etc/
213
214     echo "copying sysctl.conf (fix tcp window scaling and broken routers)"
215     cp -f $CONF_FILES_DIR/sysctl.conf $CD_ROOT/etc/
216
217     echo "setup default network conf file"
218     mkdir -p $CD_ROOT/usr/boot
219     cp -f $CONF_FILES_DIR/default-net.cnf $CD_ROOT/usr/boot/
220
221     echo "setup boot server configuration"
222     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_CERT $CD_ROOT/usr/boot/cacert.pem
223     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_GPG $CD_ROOT/usr/boot/pubring.gpg
224     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/boot/boot_server
225     echo "$PRIMARY_SERVER_PORT" > $CD_ROOT/usr/boot/boot_server_port
226     echo "$PRIMARY_SERVER_PATH" > $CD_ROOT/usr/boot/boot_server_path
227
228     echo "setup backup boot server configuration"
229     mkdir -p $CD_ROOT/usr/boot/backup
230     cp -f $CURRENT_CONFIG_DIR/$BACKUP_SERVER_CERT \
231         $CD_ROOT/usr/boot/backup/cacert.pem
232     cp -f $CURRENT_CONFIG_DIR/$BACKUP_SERVER_GPG \
233         $CD_ROOT/usr/boot/backup/pubring.gpg
234     echo "$BACKUP_SERVER" > $CD_ROOT/usr/boot/backup/boot_server
235     echo "$BACKUP_SERVER_PORT" > $CD_ROOT/usr/boot/backup/boot_server_port
236     echo "$BACKUP_SERVER_PATH" > $CD_ROOT/usr/boot/backup/boot_server_path
237
238     echo "copying old boot cd directory bootme (TEMPORARY)"
239     cp -r bootme_old $CD_ROOT/usr/bootme
240     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/bootme/ID
241     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/bootme/BOOTSERVER
242     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/bootme/BOOTSERVER_IP
243     echo "$PRIMARY_SERVER_PORT" > $CD_ROOT/usr/bootme/BOOTPORT
244
245     echo "copying cacert to old boot cd directory bootme (TEMPORARY)"
246     mkdir -p $CD_ROOT/usr/bootme/cacert/$PRIMARY_SERVER/
247     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_CERT \
248         $CD_ROOT/usr/bootme/cacert/$PRIMARY_SERVER/cacert.pem
249
250     echo "forcing lvm to make lvm1 partitions (TEMPORARY)"
251     cp -f $CONF_FILES_DIR/lvm.conf $CD_ROOT/etc/lvm/
252
253     echo "copying isolinux configuration files"
254     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/message.txt
255
256     echo "writing /etc/issue"
257     echo "$FULL_VERSION_STRING" > $CD_ROOT/etc/issue
258     echo "Kernel \r on an \m" >> $CD_ROOT/etc/issue
259     echo "" >> $CD_ROOT/etc/issue
260     echo "" >> $CD_ROOT/etc/issue
261
262     if [[ ! -z "$NODE_CONFIGURATION_FILE" ]]; then
263         echo "Copying node configuration file to cd"
264         cp -f $CURRENT_CONFIG_DIR/$NODE_CONFIGURATION_FILE \
265             $CD_ROOT/usr/boot/plnode.txt
266     fi
267
268     echo "building pcitable for hardware detection"
269     pci_map_file=`find $CD_ROOT/lib/modules/ -name modules.pcimap | head -1`
270     module_dep_file=`find $CD_ROOT/lib/modules/ -name modules.dep | head -1`
271     pci_table=$CD_ROOT/usr/share/hwdata/pcitable
272     $BOOTMANAGER_DIR/source/merge_hw_tables.py \
273         $module_dep_file $pci_map_file $pci_table $CD_ROOT/etc/pl_pcitable
274
275 }
276
277 function build_initrd()
278 {
279     big=$1
280
281     echo "building initrd"
282     rm -f $INITRD
283     rm -f $INITRD.gz
284     rm -f $CD_ROOT/usr/isolinux/initrd
285     rm -f $CD_ROOT/usr/isolinux/initrd.gz
286
287     TOTAL_SIZE=$(du -ksc $CD_ROOT | awk '$2 == "total" { print $1}')
288     if [ $big -eq 1 ]; then
289         let TOTAL_SIZE=$TOTAL_SIZE-$(du -ksc $CD_ROOT/usr/isolinux | awk '$2 == "total" { print $1}')
290     else
291         let TOTAL_SIZE=$TOTAL_SIZE-$(du -ksc $CD_ROOT/usr | awk '$2 == "total" { print $1}')
292     fi
293     let INITRD_SIZE=($TOTAL_SIZE/1024)+$RAMDISK_SIZE+4
294
295     echo "making the isolinux initrd kernel command line match rd size"
296     let INITRD_SIZE_KB=$(($INITRD_SIZE * 1024))
297     cp -f $CONF_FILES_DIR/isolinux.cfg $CD_ROOT/usr/isolinux/
298     sed -i "s#ramdisk_size=0#ramdisk_size=$INITRD_SIZE_KB#g" \
299         $CD_ROOT/usr/isolinux/isolinux.cfg
300
301     dd if=/dev/zero of=$INITRD bs=1M count=$INITRD_SIZE
302     /sbin/mkfs.ext2 -F -m 0 -i $INITRD_BYTES_PER_INODE $INITRD
303     mkdir -p $INITRD_MOUNT
304     mount -o loop,rw $INITRD $INITRD_MOUNT
305
306     echo "copy all files except usr to ramdisk"
307     pushd .
308     cd $CD_ROOT
309     find . -path ./usr -prune -o -print | cpio -p -d -u $INITRD_MOUNT
310     popd
311
312     if [ $big -eq 1 ]; then
313         echo "copy usr to ramdisk"
314         pushd .
315         cd $CD_ROOT
316         find ./usr -path ./usr/isolinux -o -print | cpio -p -d -u $INITRD_MOUNT
317         popd
318     fi
319
320     umount $INITRD_MOUNT
321     rmdir $INITRD_MOUNT
322     
323     # INITRD_SIZE=$(`ls -sh $INITRD | awk '{print $1}'`)
324     # echo "initrd size = $INITRD_SIZE"
325     echo "compressing ramdisk"
326     gzip -9 $INITRD
327     mv -f $INITRD.gz $CD_ROOT/usr/isolinux
328 }
329
330 function build()
331 {
332     # build base image via yum
333     build_cdroot
334
335     # always build/rebuild initrd
336     init_initrd
337
338     ####################################################################################
339     # build small initrd for small iso and usb image
340     build_initrd 0
341
342     # build iso image
343     rm -f $ISO
344     mkisofs -o $ISO -R -allow-leading-dots -J -r -b isolinux/isolinux.bin \
345         -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table \
346         $CD_ROOT/usr
347
348     # build usb image and make it bootable with syslinux (instead of isolinux)
349     USB_IMAGE=${ISO%*.iso}.usb
350     # leave 1 MB of free space on the filesystem
351     USB_KB=$(du -kc $ISO $CD_ROOT/usr/isolinux | awk '$2 == "total" { print $1 + 1024 }')
352     /sbin/mkfs.vfat -C $USB_IMAGE $USB_KB
353
354     mkdir -p $INITRD_MOUNT
355     mount -o loop,rw $USB_IMAGE $INITRD_MOUNT
356
357     # populate the root of the image with pl_version, and the syslinux files
358     cp -a $ISO $INITRD_MOUNT
359     cp -a $CD_ROOT/usr/isolinux/{initrd.gz,kernel,message.txt,pl_version} $INITRD_MOUNT
360     cp -a $CD_ROOT/usr/isolinux/isolinux.cfg $INITRD_MOUNT/syslinux.cfg
361
362     umount $INITRD_MOUNT
363     rmdir $INITRD_MOUNT
364
365     # make it bootable
366     syslinux $USB_IMAGE
367
368     ####################################################################################
369     # build small initrd for small iso and usb image
370     build_initrd 1
371     
372     # build usb image and make it bootable with syslinux (instead of isolinux)
373     USB_IMAGE=${ISO%*.iso}-biginitrd.usb
374     # leave 1 MB of free space on the filesystem
375     USB_KB=$(du -kc $CD_ROOT/usr/isolinux | awk '$2 == "total" { print $1 + 1024 }')
376     /sbin/mkfs.vfat -C $USB_IMAGE $USB_KB
377
378     mkdir -p $INITRD_MOUNT
379     mount -o loop,rw $USB_IMAGE $INITRD_MOUNT
380
381     # populate the root of the image with pl_version, and the syslinux files
382     cp -a $CD_ROOT/usr/isolinux/{initrd.gz,kernel,message.txt,pl_version} $INITRD_MOUNT
383     cp -a $CD_ROOT/usr/isolinux/isolinux.cfg $INITRD_MOUNT/syslinux.cfg
384
385     umount $INITRD_MOUNT
386     rmdir $INITRD_MOUNT
387
388     # make it bootable
389     syslinux $USB_IMAGE
390 }
391
392 function burn()
393 {
394     cdrecord $CDRECORD_FLAGS -data $ISO
395 }
396
397 function clean()
398 {
399     rm -rf $CD_ROOT
400     rm -rf $BUILD_DIR/syslinux
401     rm -rf $BUILD_DIR/$INITRD_MOUNT
402     rm -rf $BUILD_DIR
403     rm -f $ISO
404     rmdir --ignore-fail-on-non-empty build
405 }
406
407 if [[ "$1" == "clean" || "$1" == "burn" || "$1" == "build" ]]; then
408     action=$1
409     configuration=$2
410
411     if [[ -z "$configuration" ]]; then
412         configuration=default
413     fi
414
415     echo "Loading configuration $configuration"
416     CURRENT_CONFIG_DIR=$CONFIGURATIONS_DIR/$configuration
417     . $CURRENT_CONFIG_DIR/configuration
418
419     # setup vars for this configuration
420
421     # version string for this build
422     if [[ ! -z "$EXTRA_VERSION" ]]; then
423         FULL_VERSION_STRING="$FULL_VERSION_STRING $EXTRA_VERSION"
424     fi
425     FULL_VERSION_STRING="$FULL_VERSION_STRING $BOOTCD_VERSION"
426
427     # destination image
428     if [[ ! -z "$EXTRA_VERSION" ]]; then
429         OUTPUT_IMAGE_NAME="$OUTPUT_IMAGE_NAME-$EXTRA_VERSION"
430     fi
431     OUTPUT_IMAGE_NAME="$OUTPUT_IMAGE_NAME-$BOOTCD_VERSION"
432
433     # setup build directories
434     BUILD_DIR=build/$configuration
435     mkdir -p $BUILD_DIR
436     ISO=$BUILD_DIR/`echo $OUTPUT_IMAGE_NAME | sed -e "s/%version/$BOOTCD_VERSION/"`.iso
437
438     CD_ROOT=`pwd`/$BUILD_DIR/cdroot
439     mkdir -p $CD_ROOT
440
441     # location of the uncompressed ramdisk image
442     INITRD=`pwd`/$BUILD_DIR/initrd
443
444     # temporary mount point for rd
445     MYPWD=`pwd`
446     INITRD_MOUNT=`mktemp -q -p $MYPWD/$BUILD_DIR -d -t rd.XXXXXXXX`
447     if [ $? -ne 0 ]; then
448         echo "failed to create tempory mount point for initrd"
449         exit 1
450     fi
451
452
453     case $action in 
454         build )
455             echo "Proceeding with building $DESCRIPTION"
456             build;;
457
458         clean )
459             echo "Removing built files for $DESCRIPTION"
460             clean;;
461
462         burn )
463             echo "Burning $DESCRIPTION"
464             burn;;
465     esac    
466 else
467     usage
468 fi
469