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