- leave 1 MB of free space on the USB image filesystem
[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.1"
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 BOOTMANAGER_DIR=../bootmanager/
33
34 if [ ! -d $BOOTMANAGER_DIR ]; then
35     echo "the bootmanager repository needs to be checked out at the same"
36     echo "level as this directory, for the merge_hw_tables.py script"
37     exit
38 fi
39
40
41 function usage()
42 {
43     echo "Usage: build.sh <action> [<configuration>]"
44     echo "Action: build burn clean"
45     echo
46     echo "If configuration is missing, 'default' is loaded"
47     exit
48 }
49
50
51 function build_cdroot()
52 {
53     if [ -f $CD_ROOT/.built ]; then
54         echo "cd root already built, skipping"
55         return
56     fi
57
58     clean
59     
60     mkdir -p $CD_ROOT/dev/pts
61     mkdir -p $CD_ROOT/proc
62     mkdir -p $CD_ROOT/etc
63
64     echo "copy fstab and mtab"
65     cp -f $CONF_FILES_DIR/fstab $CD_ROOT/etc/
66     cp -f $CONF_FILES_DIR/mtab $CD_ROOT/etc/
67
68     echo "setup rpm to install only en_US locale and no docs"
69     mkdir -p $CD_ROOT/etc/rpm
70     cp -f $CONF_FILES_DIR/macros $CD_ROOT/etc/rpm
71     # trick rpm and yum
72     export HOME=$PWD
73     cp -f $CONF_FILES_DIR/macros $PWD/.rpmmacros
74
75     echo "initialize rpm db"
76     mkdir -p $CD_ROOT/var/lib/rpm
77     rpm --root $CD_ROOT --initdb
78
79     echo "install boot cd base rpms"
80     yum -c yum.conf --installroot=$CD_ROOT -y groupinstall $BOOTCD_YUM_GROUP
81
82     echo "checking to make sure rpms were installed"
83     packages=`cat yumgroups.xml | grep packagereq | sed 's#<[^<]*>##g'`
84     set +e
85     for package in $packages; do
86         echo "checking for package $package"
87         chroot $CD_ROOT /bin/rpm -qi $package > /dev/null
88         if [[ "$?" -ne 0 ]]; then
89             echo "package $package was not installed in the cd root."
90             echo "make sure it exists in the yum repository."
91             exit 1
92         fi
93     done
94     set -e
95     
96     echo "removing unneccessary build files"
97     (cd $CD_ROOT/lib/modules && \
98         find ./ -type d -name build -maxdepth 2 -exec rm -rf {} \;)
99
100     echo "setting up non-ssh authentication"
101     mkdir -p $CD_ROOT/etc/samba
102     chroot $CD_ROOT /usr/sbin/authconfig --nostart --kickstart \
103         --enablemd5 --enableshadow
104
105     echo "setting root password"
106     sed -i "s#root::#root:$ROOT_PASSWORD:#g" $CD_ROOT/etc/shadow
107
108     echo "relocate some large directories out of the root system"
109     # get /var/lib/rpm out, its 12mb. create in its place a 
110     # symbolic link to /usr/relocated/var/lib/rpm
111     mkdir -p $CD_ROOT/usr/relocated/var/lib/
112     mv $CD_ROOT/var/lib/rpm $CD_ROOT/usr/relocated/var/lib/
113     (cd $CD_ROOT/var/lib && ln -s ../../usr/relocated/var/lib/rpm rpm)
114
115     # get /lib/tls out
116     mkdir -p $CD_ROOT/usr/relocated/lib
117     mv $CD_ROOT/lib/tls $CD_ROOT/usr/relocated/lib/
118     (cd $CD_ROOT/lib && ln -s ../usr/relocated/lib/tls tls)
119
120     echo "extracting syslinux, copying isolinux files to cd"
121     mkdir -p $CD_ROOT/usr/isolinux/
122     mkdir -p $BUILD_DIR/syslinux
123     tar -C $BUILD_DIR/syslinux -xjvf $SYSLINUX_SRC
124     find $BUILD_DIR/syslinux -name isolinux.bin \
125         -exec cp -f {} $CD_ROOT/usr/isolinux/ \;
126
127     echo "moving kernel to isolinux directory"
128     KERNEL=$CD_ROOT/boot/vmlinuz-*
129     mv -f $KERNEL $CD_ROOT/usr/isolinux/kernel
130
131     echo "moving /usr/bin/find and /usr/bin/dirname to /bin"
132     mv $CD_ROOT/usr/bin/find $CD_ROOT/bin/
133     mv $CD_ROOT/usr/bin/dirname $CD_ROOT/bin/
134
135     echo "creating version files"
136     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/pl_version
137     echo "$FULL_VERSION_STRING" > $CD_ROOT/pl_version
138
139     touch $CD_ROOT/.built
140 }
141
142 function build_initrd()
143 {
144     echo "building initrd"
145     rm -f $INITRD
146     rm -f $INITRD.gz
147
148     echo "copy fstab and mtab"
149     cp -f $CONF_FILES_DIR/fstab $CD_ROOT/etc/
150     cp -f $CONF_FILES_DIR/mtab $CD_ROOT/etc/
151
152     echo "installing generic modprobe.conf"
153     cp -f $CONF_FILES_DIR/modprobe.conf $CD_ROOT/etc/
154
155     echo "installing our own inittab and init scripts"
156     cp -f $CONF_FILES_DIR/inittab $CD_ROOT/etc
157     init_scripts="pl_sysinit pl_hwinit pl_netinit pl_validateconf pl_boot"
158     for script in $init_scripts; do
159         cp -f $CONF_FILES_DIR/$script $CD_ROOT/etc/init.d/
160         chmod +x $CD_ROOT/etc/init.d/$script
161     done
162
163     echo "setup basic networking files"
164     cp -f $CONF_FILES_DIR/hosts $CD_ROOT/etc/
165
166     echo "copying sysctl.conf (fix tcp window scaling and broken routers)"
167     cp -f $CONF_FILES_DIR/sysctl.conf $CD_ROOT/etc/
168
169     echo "setup default network conf file"
170     mkdir -p $CD_ROOT/usr/boot
171     cp -f $CONF_FILES_DIR/default-net.cnf $CD_ROOT/usr/boot/
172
173     echo "setup boot server configuration"
174     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_CERT $CD_ROOT/usr/boot/cacert.pem
175     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_GPG $CD_ROOT/usr/boot/pubring.gpg
176     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/boot/boot_server
177     echo "$PRIMARY_SERVER_PORT" > $CD_ROOT/usr/boot/boot_server_port
178     echo "$PRIMARY_SERVER_PATH" > $CD_ROOT/usr/boot/boot_server_path
179
180     echo "setup backup boot server configuration"
181     mkdir -p $CD_ROOT/usr/boot/backup
182     cp -f $CURRENT_CONFIG_DIR/$BACKUP_SERVER_CERT \
183         $CD_ROOT/usr/boot/backup/cacert.pem
184     cp -f $CURRENT_CONFIG_DIR/$BACKUP_SERVER_GPG \
185         $CD_ROOT/usr/boot/backup/pubring.gpg
186     echo "$BACKUP_SERVER" > $CD_ROOT/usr/boot/backup/boot_server
187     echo "$BACKUP_SERVER_PORT" > $CD_ROOT/usr/boot/backup/boot_server_port
188     echo "$BACKUP_SERVER_PATH" > $CD_ROOT/usr/boot/backup/boot_server_path
189
190     echo "copying old boot cd directory bootme (TEMPORARY)"
191     cp -r bootme_old $CD_ROOT/usr/bootme
192     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/bootme/ID
193     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/bootme/BOOTSERVER
194     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/bootme/BOOTSERVER_IP
195     echo "$PRIMARY_SERVER_PORT" > $CD_ROOT/usr/bootme/BOOTPORT
196
197     echo "copying cacert to old boot cd directory bootme (TEMPORARY)"
198     mkdir -p $CD_ROOT/usr/bootme/cacert/$PRIMARY_SERVER/
199     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_CERT \
200         $CD_ROOT/usr/bootme/cacert/$PRIMARY_SERVER/cacert.pem
201
202     echo "forcing lvm to make lvm1 partitions (TEMPORARY)"
203     cp -f $CONF_FILES_DIR/lvm.conf $CD_ROOT/etc/lvm/
204
205     echo "copying isolinux configuration files"
206     cp -f $CONF_FILES_DIR/isolinux.cfg $CD_ROOT/usr/isolinux/
207     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/message.txt
208
209     echo "writing /etc/issue"
210     echo "$FULL_VERSION_STRING" > $CD_ROOT/etc/issue
211     echo "Kernel \r on an \m" >> $CD_ROOT/etc/issue
212     echo "" >> $CD_ROOT/etc/issue
213     echo "" >> $CD_ROOT/etc/issue
214
215     if [[ ! -z "$NODE_CONFIGURATION_FILE" ]]; then
216         echo "Copying node configuration file to cd"
217         cp -f $CURRENT_CONFIG_DIR/$NODE_CONFIGURATION_FILE \
218             $CD_ROOT/usr/boot/plnode.txt
219     fi
220
221     echo "making the isolinux initrd kernel command line match rd size"
222     let INITRD_SIZE_KB=$(($RAMDISK_SIZE * 1024))
223     sed -i "s#ramdisk_size=0#ramdisk_size=$INITRD_SIZE_KB#g" \
224         $CD_ROOT/usr/isolinux/isolinux.cfg
225
226     echo "building pcitable for hardware detection"
227     pci_map_file=`find $CD_ROOT/lib/modules/ -name modules.pcimap | head -1`
228     module_dep_file=`find $CD_ROOT/lib/modules/ -name modules.dep | head -1`
229     pci_table=$CD_ROOT/usr/share/hwdata/pcitable
230     $BOOTMANAGER_DIR/source/merge_hw_tables.py \
231         $module_dep_file $pci_map_file $pci_table $CD_ROOT/etc/pl_pcitable
232
233     dd if=/dev/zero of=$INITRD bs=1M count=$RAMDISK_SIZE
234     mkfs.ext2 -F -m 0 -i $INITRD_BYTES_PER_INODE $INITRD
235     mkdir -p $INITRD_MOUNT
236     mount -o loop,rw $INITRD $INITRD_MOUNT
237
238     echo "copy all files except usr to ramdisk"
239     pushd .
240     cd $CD_ROOT
241     find . -path ./usr -prune -o -print | cpio -p -d -u $INITRD_MOUNT
242     popd
243
244     umount $INITRD_MOUNT
245     rmdir $INITRD_MOUNT
246     
247     echo "compressing ramdisk"
248     gzip $INITRD
249 }
250
251 function build()
252 {
253     # build base image via yum
254     build_cdroot
255
256     # always build/rebuild initrd
257     build_initrd
258
259     # build iso image
260     rm -f $ISO
261     mkisofs -o $ISO -R -allow-leading-dots -J -r -b isolinux/isolinux.bin \
262         -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table \
263         $CD_ROOT/usr
264
265     # build usb image and make it bootable with syslinux (instead of isolinux)
266     USB_IMAGE=${ISO%*.iso}.usb
267     # leave 1 MB of free space on the filesystem
268     USB_KB=$(du -kc $ISO $CD_ROOT/usr/isolinux | awk '$2 == "total" { print $1 + 1024 }')
269     mkfs.vfat -C $USB_IMAGE $USB_KB
270
271     mkdir -p $INITRD_MOUNT
272     mount -o loop,rw $USB_IMAGE $INITRD_MOUNT
273
274     # populate the root of the image with the iso, pl_version, and the syslinux files
275     cp -a $ISO $INITRD_MOUNT
276     cp -a $CD_ROOT/usr/isolinux/{initrd.gz,kernel,message.txt,pl_version} $INITRD_MOUNT
277     cp -a $CD_ROOT/usr/isolinux/isolinux.cfg $INITRD_MOUNT/syslinux.cfg
278
279     umount $INITRD_MOUNT
280     rmdir $INITRD_MOUNT
281
282     # make it bootable
283     syslinux $USB_IMAGE
284 }
285
286 function burn()
287 {
288     cdrecord $CDRECORD_FLAGS -data $ISO
289 }
290
291 function clean()
292 {
293     rm -rf $CD_ROOT
294     rm -rf $BUILD_DIR/syslinux
295     rm -rf $BUILD_DIR/$INITRD_MOUNT
296     rm -rf $BUILD_DIR
297     rm -f $ISO
298     rmdir --ignore-fail-on-non-empty build
299 }
300
301 if [[ "$1" == "clean" || "$1" == "burn" || "$1" == "build" ]]; then
302     action=$1
303     configuration=$2
304
305     if [[ -z "$configuration" ]]; then
306         configuration=default
307     fi
308
309     echo "Loading configuration $configuration"
310     CURRENT_CONFIG_DIR=$CONFIGURATIONS_DIR/$configuration
311     . $CURRENT_CONFIG_DIR/configuration
312
313     # setup vars for this configuration
314
315     # version string for this build
316     if [[ ! -z "$EXTRA_VERSION" ]]; then
317         FULL_VERSION_STRING="$FULL_VERSION_STRING $EXTRA_VERSION"
318     fi
319     FULL_VERSION_STRING="$FULL_VERSION_STRING $BOOTCD_VERSION"
320
321     # destination image
322     if [[ ! -z "$EXTRA_VERSION" ]]; then
323         OUTPUT_IMAGE_NAME="$OUTPUT_IMAGE_NAME-$EXTRA_VERSION"
324     fi
325     OUTPUT_IMAGE_NAME="$OUTPUT_IMAGE_NAME-$BOOTCD_VERSION"
326
327     # setup build directories
328     BUILD_DIR=build/$configuration
329     mkdir -p $BUILD_DIR
330     ISO=$BUILD_DIR/`echo $OUTPUT_IMAGE_NAME | sed -e "s/%version/$BOOTCD_VERSION/"`.iso
331
332     CD_ROOT=`pwd`/$BUILD_DIR/cdroot
333     mkdir -p $CD_ROOT
334
335     # location of the uncompressed ramdisk image
336     INITRD=$CD_ROOT/usr/isolinux/initrd
337
338     # temporary mount point for rd
339     INITRD_MOUNT=`pwd`/$BUILD_DIR/rd
340
341
342     case $action in 
343         build )
344             echo "Proceeding with building $DESCRIPTION"
345             build;;
346
347         clean )
348             echo "Removing built files for $DESCRIPTION"
349             clean;;
350
351         burn )
352             echo "Burning $DESCRIPTION"
353             burn;;
354     esac    
355 else
356     usage
357 fi
358