- run createrepo with the correct -g option
[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        createrepo -g yumgroups.xml $(dirname $RPM_BUILD_DIR)/RPMS || :
115        # If run under sudo, allow user to delete the headers/ and
116        # repodata/ directories.
117        if [ -n "$SUDO_USER" ] ; then
118            chown -R $SUDO_USER $(dirname $RPM_BUILD_DIR)/RPMS
119        fi
120        cat >>yum.conf <<EOF
121 [Bootstrap]
122 name=Bootstrap RPMS -- $(dirname $RPM_BUILD_DIR)/RPMS/
123 baseurl=file://$(dirname $RPM_BUILD_DIR)/RPMS/
124 EOF
125        yumgroups="file://$(dirname $RPM_BUILD_DIR)/RPMS/yumgroups.xml"
126    fi
127
128     echo "install boot cd base rpms"
129     yum -c yum.conf --installroot=$CD_ROOT -y groupinstall $BOOTCD_YUM_GROUP
130
131     # Retrieve all of the packagereq declarations in the BootCD group of the yumgroups.xml file
132     echo "checking to make sure rpms were installed"
133     packages=$(curl $yumgroups | sed -n -e '/<name>BootCD<\/name>/,/<name>/{ s/.*<packagereq.*>\(.*\)<\/packagereq>/\1/p }')
134     set +e
135     for package in $packages; do
136         echo "checking for package $package"
137         /usr/sbin/chroot $CD_ROOT /bin/rpm -qi $package > /dev/null
138         if [[ "$?" -ne 0 ]]; then
139             echo "package $package was not installed in the cd root."
140             echo "make sure it exists in the yum repository."
141             exit 1
142         fi
143     done
144     set -e
145     
146     echo "removing unneccessary build files"
147     (cd $CD_ROOT/lib/modules && \
148         find ./ -type d -name build -maxdepth 2 -exec rm -rf {} \;)
149
150     echo "setting up non-ssh authentication"
151     mkdir -p $CD_ROOT/etc/samba
152     /usr/sbin/chroot $CD_ROOT /usr/sbin/authconfig --nostart --kickstart \
153         --enablemd5 --enableshadow
154
155     echo "setting root password"
156     sed -i "s#root::#root:$ROOT_PASSWORD:#g" $CD_ROOT/etc/shadow
157
158     echo "relocate some large directories out of the root system"
159     # get /var/lib/rpm out, its 12mb. create in its place a 
160     # symbolic link to /usr/relocated/var/lib/rpm
161     mkdir -p $CD_ROOT/usr/relocated/var/lib/
162     mv $CD_ROOT/var/lib/rpm $CD_ROOT/usr/relocated/var/lib/
163     (cd $CD_ROOT/var/lib && ln -s ../../usr/relocated/var/lib/rpm rpm)
164
165     # get /var/cache/yum out, its 100Mb. create in its place a 
166     # symbolic link to /usr/relocated/var/cache/yum
167     mkdir -p $CD_ROOT/usr/relocated/var/cache/
168     mv $CD_ROOT/var/cache/yum $CD_ROOT/usr/relocated/var/cache/
169     (cd $CD_ROOT/var/cache && ln -s ../../usr/relocated/var/cache/yum yum)
170
171     # get /lib/tls out
172     mkdir -p $CD_ROOT/usr/relocated/lib
173     mv $CD_ROOT/lib/tls $CD_ROOT/usr/relocated/lib/
174     (cd $CD_ROOT/lib && ln -s ../usr/relocated/lib/tls tls)
175
176     echo "extracting syslinux, copying isolinux files to cd"
177     mkdir -p $CD_ROOT/usr/isolinux/
178     mkdir -p $BUILD_DIR/syslinux
179     tar -C $BUILD_DIR/syslinux -xjvf $SYSLINUX_SRC
180     find $BUILD_DIR/syslinux -name isolinux.bin \
181         -exec cp -f {} $CD_ROOT/usr/isolinux/ \;
182
183     echo "moving kernel to isolinux directory"
184     KERNEL=$CD_ROOT/boot/vmlinuz-*
185     mv -f $KERNEL $CD_ROOT/usr/isolinux/kernel
186
187     echo "moving /usr/bin/find and /usr/bin/dirname to /bin"
188     mv $CD_ROOT/usr/bin/find $CD_ROOT/bin/
189     mv $CD_ROOT/usr/bin/dirname $CD_ROOT/bin/
190
191     echo "creating version files"
192     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/pl_version
193     echo "$FULL_VERSION_STRING" > $CD_ROOT/pl_version
194
195     touch $CD_ROOT/.built
196 }
197
198 function init_initrd()
199 {
200     echo "initialize configuration files"
201
202     echo "copy fstab and mtab"
203     cp -f $CONF_FILES_DIR/fstab $CD_ROOT/etc/
204     cp -f $CONF_FILES_DIR/mtab $CD_ROOT/etc/
205
206     echo "installing generic modprobe.conf"
207     cp -f $CONF_FILES_DIR/modprobe.conf $CD_ROOT/etc/
208
209     echo "installing our own inittab and init scripts"
210     cp -f $CONF_FILES_DIR/inittab $CD_ROOT/etc
211     init_scripts="pl_sysinit pl_hwinit pl_netinit pl_validateconf pl_boot"
212     for script in $init_scripts; do
213         cp -f $CONF_FILES_DIR/$script $CD_ROOT/etc/init.d/
214         chmod +x $CD_ROOT/etc/init.d/$script
215     done
216
217     echo "setup basic networking files"
218     cp -f $CONF_FILES_DIR/hosts $CD_ROOT/etc/
219
220     echo "copying sysctl.conf (fix tcp window scaling and broken routers)"
221     cp -f $CONF_FILES_DIR/sysctl.conf $CD_ROOT/etc/
222
223     echo "setup default network conf file"
224     mkdir -p $CD_ROOT/usr/boot
225     cp -f $CONF_FILES_DIR/default-net.cnf $CD_ROOT/usr/boot/
226
227     echo "setup boot server configuration"
228     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_CERT $CD_ROOT/usr/boot/cacert.pem
229     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_GPG $CD_ROOT/usr/boot/pubring.gpg
230     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/boot/boot_server
231     echo "$PRIMARY_SERVER_PORT" > $CD_ROOT/usr/boot/boot_server_port
232     echo "$PRIMARY_SERVER_PATH" > $CD_ROOT/usr/boot/boot_server_path
233
234     echo "setup backup boot server configuration"
235     mkdir -p $CD_ROOT/usr/boot/backup
236     cp -f $CURRENT_CONFIG_DIR/$BACKUP_SERVER_CERT \
237         $CD_ROOT/usr/boot/backup/cacert.pem
238     cp -f $CURRENT_CONFIG_DIR/$BACKUP_SERVER_GPG \
239         $CD_ROOT/usr/boot/backup/pubring.gpg
240     echo "$BACKUP_SERVER" > $CD_ROOT/usr/boot/backup/boot_server
241     echo "$BACKUP_SERVER_PORT" > $CD_ROOT/usr/boot/backup/boot_server_port
242     echo "$BACKUP_SERVER_PATH" > $CD_ROOT/usr/boot/backup/boot_server_path
243
244     echo "copying old boot cd directory bootme (TEMPORARY)"
245     cp -r bootme_old $CD_ROOT/usr/bootme
246     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/bootme/ID
247     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/bootme/BOOTSERVER
248     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/bootme/BOOTSERVER_IP
249     echo "$PRIMARY_SERVER_PORT" > $CD_ROOT/usr/bootme/BOOTPORT
250
251     echo "copying cacert to old boot cd directory bootme (TEMPORARY)"
252     mkdir -p $CD_ROOT/usr/bootme/cacert/$PRIMARY_SERVER/
253     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_CERT \
254         $CD_ROOT/usr/bootme/cacert/$PRIMARY_SERVER/cacert.pem
255
256     echo "forcing lvm to make lvm1 partitions (TEMPORARY)"
257     cp -f $CONF_FILES_DIR/lvm.conf $CD_ROOT/etc/lvm/
258
259     echo "copying isolinux configuration files"
260     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/message.txt
261
262     echo "writing /etc/issue"
263     echo "$FULL_VERSION_STRING" > $CD_ROOT/etc/issue
264     echo "Kernel \r on an \m" >> $CD_ROOT/etc/issue
265     echo "" >> $CD_ROOT/etc/issue
266     echo "" >> $CD_ROOT/etc/issue
267
268     if [[ ! -z "$NODE_CONFIGURATION_FILE" ]]; then
269         echo "Copying node configuration file to cd"
270         cp -f $CURRENT_CONFIG_DIR/$NODE_CONFIGURATION_FILE \
271             $CD_ROOT/usr/boot/plnode.txt
272     fi
273
274     echo "building pcitable for hardware detection"
275     pci_map_file=`find $CD_ROOT/lib/modules/ -name modules.pcimap | head -1`
276     module_dep_file=`find $CD_ROOT/lib/modules/ -name modules.dep | head -1`
277     pci_table=$CD_ROOT/usr/share/hwdata/pcitable
278     $BOOTMANAGER_DIR/source/merge_hw_tables.py \
279         $module_dep_file $pci_map_file $pci_table $CD_ROOT/etc/pl_pcitable
280
281 }
282
283 function build_initrd()
284 {
285     big=$1
286
287     echo "building initrd"
288     rm -f $INITRD
289     rm -f $INITRD.gz
290     rm -f $CD_ROOT/usr/isolinux/initrd
291     rm -f $CD_ROOT/usr/isolinux/initrd.gz
292
293     TOTAL_SIZE=$(du -ksc $CD_ROOT | awk '$2 == "total" { print $1}')
294     if [ $big -eq 1 ]; then
295         let TOTAL_SIZE=$TOTAL_SIZE-$(du -ksc $CD_ROOT/usr/isolinux | awk '$2 == "total" { print $1}')
296     else
297         let TOTAL_SIZE=$TOTAL_SIZE-$(du -ksc $CD_ROOT/usr | awk '$2 == "total" { print $1}')
298     fi
299     let INITRD_SIZE=($TOTAL_SIZE/1024)+$RAMDISK_SIZE+4
300
301     echo "making the isolinux initrd kernel command line match rd size"
302     let INITRD_SIZE_KB=$(($INITRD_SIZE * 1024))
303     cp -f $CONF_FILES_DIR/isolinux.cfg $CD_ROOT/usr/isolinux/
304     sed -i "s#ramdisk_size=0#ramdisk_size=$INITRD_SIZE_KB#g" \
305         $CD_ROOT/usr/isolinux/isolinux.cfg
306
307     dd if=/dev/zero of=$INITRD bs=1M count=$INITRD_SIZE
308     /sbin/mkfs.ext2 -F -m 0 -i $INITRD_BYTES_PER_INODE $INITRD
309     mkdir -p $INITRD_MOUNT
310     mount -o loop,rw $INITRD $INITRD_MOUNT
311
312     echo "copy all files except usr to ramdisk"
313     pushd .
314     cd $CD_ROOT
315     find . -path ./usr -prune -o -print | cpio -p -d -u $INITRD_MOUNT
316     popd
317
318     if [ $big -eq 1 ]; then
319         echo "copy usr to ramdisk"
320         pushd .
321         cd $CD_ROOT
322         find ./usr -path ./usr/isolinux -o -print | cpio -p -d -u $INITRD_MOUNT
323         popd
324     fi
325
326     umount $INITRD_MOUNT
327     rmdir $INITRD_MOUNT
328     
329     # INITRD_SIZE=$(`ls -sh $INITRD | awk '{print $1}'`)
330     # echo "initrd size = $INITRD_SIZE"
331     echo "compressing ramdisk"
332     gzip -9 $INITRD
333     mv -f $INITRD.gz $CD_ROOT/usr/isolinux
334 }
335
336 function build()
337 {
338     # build base image via yum
339     build_cdroot
340
341     # always build/rebuild initrd
342     init_initrd
343
344     ####################################################################################
345     # build small initrd for small iso and usb image
346     build_initrd 0
347
348     # build iso image
349     rm -f $ISO
350     mkisofs -o $ISO -R -allow-leading-dots -J -r -b isolinux/isolinux.bin \
351         -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table \
352         $CD_ROOT/usr
353
354     # build usb image and make it bootable with syslinux (instead of isolinux)
355     USB_IMAGE=${ISO%*.iso}.usb
356     # leave 1 MB of free space on the filesystem
357     USB_KB=$(du -kc $ISO $CD_ROOT/usr/isolinux | awk '$2 == "total" { print $1 + 1024 }')
358     /sbin/mkfs.vfat -C $USB_IMAGE $USB_KB
359
360     mkdir -p $INITRD_MOUNT
361     mount -o loop,rw $USB_IMAGE $INITRD_MOUNT
362
363     # populate the root of the image with pl_version, and the syslinux files
364     cp -a $ISO $INITRD_MOUNT
365     cp -a $CD_ROOT/usr/isolinux/{initrd.gz,kernel,message.txt,pl_version} $INITRD_MOUNT
366     cp -a $CD_ROOT/usr/isolinux/isolinux.cfg $INITRD_MOUNT/syslinux.cfg
367
368     umount $INITRD_MOUNT
369     rmdir $INITRD_MOUNT
370
371     # make it bootable
372     syslinux $USB_IMAGE
373
374     ####################################################################################
375     # build small initrd for small iso and usb image
376     build_initrd 1
377     
378     # build usb image and make it bootable with syslinux (instead of isolinux)
379     USB_IMAGE=${ISO%*.iso}-biginitrd.usb
380     # leave 1 MB of free space on the filesystem
381     USB_KB=$(du -kc $CD_ROOT/usr/isolinux | awk '$2 == "total" { print $1 + 1024 }')
382     /sbin/mkfs.vfat -C $USB_IMAGE $USB_KB
383
384     mkdir -p $INITRD_MOUNT
385     mount -o loop,rw $USB_IMAGE $INITRD_MOUNT
386
387     # populate the root of the image with pl_version, and the syslinux files
388     cp -a $CD_ROOT/usr/isolinux/{initrd.gz,kernel,message.txt,pl_version} $INITRD_MOUNT
389     cp -a $CD_ROOT/usr/isolinux/isolinux.cfg $INITRD_MOUNT/syslinux.cfg
390
391     umount $INITRD_MOUNT
392     rmdir $INITRD_MOUNT
393
394     # make it bootable
395     syslinux $USB_IMAGE
396 }
397
398 function burn()
399 {
400     cdrecord $CDRECORD_FLAGS -data $ISO
401 }
402
403 function clean()
404 {
405     rm -rf $CD_ROOT
406     rm -rf $BUILD_DIR/syslinux
407     rm -rf $BUILD_DIR/$INITRD_MOUNT
408     rm -rf $BUILD_DIR
409     rm -f $ISO
410     rmdir --ignore-fail-on-non-empty build
411 }
412
413 if [[ "$1" == "clean" || "$1" == "burn" || "$1" == "build" ]]; then
414     action=$1
415     configuration=$2
416
417     if [[ -z "$configuration" ]]; then
418         configuration=default
419     fi
420
421     echo "Loading configuration $configuration"
422     CURRENT_CONFIG_DIR=$CONFIGURATIONS_DIR/$configuration
423     . $CURRENT_CONFIG_DIR/configuration
424
425     # setup vars for this configuration
426
427     # version string for this build
428     if [[ ! -z "$EXTRA_VERSION" ]]; then
429         FULL_VERSION_STRING="$FULL_VERSION_STRING $EXTRA_VERSION"
430     fi
431     FULL_VERSION_STRING="$FULL_VERSION_STRING $BOOTCD_VERSION"
432
433     # destination image
434     if [[ ! -z "$EXTRA_VERSION" ]]; then
435         OUTPUT_IMAGE_NAME="$OUTPUT_IMAGE_NAME-$EXTRA_VERSION"
436     fi
437     OUTPUT_IMAGE_NAME="$OUTPUT_IMAGE_NAME-$BOOTCD_VERSION"
438
439     # setup build directories
440     BUILD_DIR=build/$configuration
441     mkdir -p $BUILD_DIR
442     ISO=$BUILD_DIR/`echo $OUTPUT_IMAGE_NAME | sed -e "s/%version/$BOOTCD_VERSION/"`.iso
443
444     CD_ROOT=`pwd`/$BUILD_DIR/cdroot
445     mkdir -p $CD_ROOT
446
447     # location of the uncompressed ramdisk image
448     INITRD=`pwd`/$BUILD_DIR/initrd
449
450     # temporary mount point for rd
451     MYPWD=`pwd`
452     INITRD_MOUNT=`mktemp -q -p $MYPWD/$BUILD_DIR -d -t rd.XXXXXXXX`
453     if [ $? -ne 0 ]; then
454         echo "failed to create tempory mount point for initrd"
455         exit 1
456     fi
457
458
459     case $action in 
460         build )
461             echo "Proceeding with building $DESCRIPTION"
462             build;;
463
464         clean )
465             echo "Removing built files for $DESCRIPTION"
466             clean;;
467
468         burn )
469             echo "Burning $DESCRIPTION"
470             burn;;
471     esac    
472 else
473     usage
474 fi
475