This change is for yum-2.4 from fc4
[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 /var/cache/yum out, its 100Mb. create in its place a 
116     # symbolic link to /usr/relocated/var/cache/yum
117     mkdir -p $CD_ROOT/usr/relocated/var/cache/
118     mv $CD_ROOT/var/cache/yum $CD_ROOT/usr/relocated/var/cache/
119     (cd $CD_ROOT/var/cache && ln -s ../../usr/relocated/var/cache/yum yum)
120
121     # get /lib/tls out
122     mkdir -p $CD_ROOT/usr/relocated/lib
123     mv $CD_ROOT/lib/tls $CD_ROOT/usr/relocated/lib/
124     (cd $CD_ROOT/lib && ln -s ../usr/relocated/lib/tls tls)
125
126     echo "extracting syslinux, copying isolinux files to cd"
127     mkdir -p $CD_ROOT/usr/isolinux/
128     mkdir -p $BUILD_DIR/syslinux
129     tar -C $BUILD_DIR/syslinux -xjvf $SYSLINUX_SRC
130     find $BUILD_DIR/syslinux -name isolinux.bin \
131         -exec cp -f {} $CD_ROOT/usr/isolinux/ \;
132
133     echo "moving kernel to isolinux directory"
134     KERNEL=$CD_ROOT/boot/vmlinuz-*
135     mv -f $KERNEL $CD_ROOT/usr/isolinux/kernel
136
137     echo "moving /usr/bin/find and /usr/bin/dirname to /bin"
138     mv $CD_ROOT/usr/bin/find $CD_ROOT/bin/
139     mv $CD_ROOT/usr/bin/dirname $CD_ROOT/bin/
140
141     echo "creating version files"
142     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/pl_version
143     echo "$FULL_VERSION_STRING" > $CD_ROOT/pl_version
144
145     touch $CD_ROOT/.built
146 }
147
148 function build_initrd()
149 {
150     echo "building initrd"
151     rm -f $INITRD
152     rm -f $INITRD.gz
153
154     echo "copy fstab and mtab"
155     cp -f $CONF_FILES_DIR/fstab $CD_ROOT/etc/
156     cp -f $CONF_FILES_DIR/mtab $CD_ROOT/etc/
157
158     echo "installing generic modprobe.conf"
159     cp -f $CONF_FILES_DIR/modprobe.conf $CD_ROOT/etc/
160
161     echo "installing our own inittab and init scripts"
162     cp -f $CONF_FILES_DIR/inittab $CD_ROOT/etc
163     init_scripts="pl_sysinit pl_hwinit pl_netinit pl_validateconf pl_boot"
164     for script in $init_scripts; do
165         cp -f $CONF_FILES_DIR/$script $CD_ROOT/etc/init.d/
166         chmod +x $CD_ROOT/etc/init.d/$script
167     done
168
169     echo "setup basic networking files"
170     cp -f $CONF_FILES_DIR/hosts $CD_ROOT/etc/
171
172     echo "copying sysctl.conf (fix tcp window scaling and broken routers)"
173     cp -f $CONF_FILES_DIR/sysctl.conf $CD_ROOT/etc/
174
175     echo "setup default network conf file"
176     mkdir -p $CD_ROOT/usr/boot
177     cp -f $CONF_FILES_DIR/default-net.cnf $CD_ROOT/usr/boot/
178
179     echo "setup boot server configuration"
180     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_CERT $CD_ROOT/usr/boot/cacert.pem
181     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_GPG $CD_ROOT/usr/boot/pubring.gpg
182     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/boot/boot_server
183     echo "$PRIMARY_SERVER_PORT" > $CD_ROOT/usr/boot/boot_server_port
184     echo "$PRIMARY_SERVER_PATH" > $CD_ROOT/usr/boot/boot_server_path
185
186     echo "setup backup boot server configuration"
187     mkdir -p $CD_ROOT/usr/boot/backup
188     cp -f $CURRENT_CONFIG_DIR/$BACKUP_SERVER_CERT \
189         $CD_ROOT/usr/boot/backup/cacert.pem
190     cp -f $CURRENT_CONFIG_DIR/$BACKUP_SERVER_GPG \
191         $CD_ROOT/usr/boot/backup/pubring.gpg
192     echo "$BACKUP_SERVER" > $CD_ROOT/usr/boot/backup/boot_server
193     echo "$BACKUP_SERVER_PORT" > $CD_ROOT/usr/boot/backup/boot_server_port
194     echo "$BACKUP_SERVER_PATH" > $CD_ROOT/usr/boot/backup/boot_server_path
195
196     echo "copying old boot cd directory bootme (TEMPORARY)"
197     cp -r bootme_old $CD_ROOT/usr/bootme
198     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/bootme/ID
199     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/bootme/BOOTSERVER
200     echo "$PRIMARY_SERVER" > $CD_ROOT/usr/bootme/BOOTSERVER_IP
201     echo "$PRIMARY_SERVER_PORT" > $CD_ROOT/usr/bootme/BOOTPORT
202
203     echo "copying cacert to old boot cd directory bootme (TEMPORARY)"
204     mkdir -p $CD_ROOT/usr/bootme/cacert/$PRIMARY_SERVER/
205     cp -f $CURRENT_CONFIG_DIR/$PRIMARY_SERVER_CERT \
206         $CD_ROOT/usr/bootme/cacert/$PRIMARY_SERVER/cacert.pem
207
208     echo "forcing lvm to make lvm1 partitions (TEMPORARY)"
209     cp -f $CONF_FILES_DIR/lvm.conf $CD_ROOT/etc/lvm/
210
211     echo "copying isolinux configuration files"
212     cp -f $CONF_FILES_DIR/isolinux.cfg $CD_ROOT/usr/isolinux/
213     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/message.txt
214
215     echo "writing /etc/issue"
216     echo "$FULL_VERSION_STRING" > $CD_ROOT/etc/issue
217     echo "Kernel \r on an \m" >> $CD_ROOT/etc/issue
218     echo "" >> $CD_ROOT/etc/issue
219     echo "" >> $CD_ROOT/etc/issue
220
221     if [[ ! -z "$NODE_CONFIGURATION_FILE" ]]; then
222         echo "Copying node configuration file to cd"
223         cp -f $CURRENT_CONFIG_DIR/$NODE_CONFIGURATION_FILE \
224             $CD_ROOT/usr/boot/plnode.txt
225     fi
226
227     echo "making the isolinux initrd kernel command line match rd size"
228     let INITRD_SIZE_KB=$(($RAMDISK_SIZE * 1024))
229     sed -i "s#ramdisk_size=0#ramdisk_size=$INITRD_SIZE_KB#g" \
230         $CD_ROOT/usr/isolinux/isolinux.cfg
231
232     echo "building pcitable for hardware detection"
233     pci_map_file=`find $CD_ROOT/lib/modules/ -name modules.pcimap | head -1`
234     module_dep_file=`find $CD_ROOT/lib/modules/ -name modules.dep | head -1`
235     pci_table=$CD_ROOT/usr/share/hwdata/pcitable
236     $BOOTMANAGER_DIR/source/merge_hw_tables.py \
237         $module_dep_file $pci_map_file $pci_table $CD_ROOT/etc/pl_pcitable
238
239     dd if=/dev/zero of=$INITRD bs=1M count=$RAMDISK_SIZE
240     mkfs.ext2 -F -m 0 -i $INITRD_BYTES_PER_INODE $INITRD
241     mkdir -p $INITRD_MOUNT
242     mount -o loop,rw $INITRD $INITRD_MOUNT
243
244     echo "copy all files except usr to ramdisk"
245     pushd .
246     cd $CD_ROOT
247     find . -path ./usr -prune -o -print | cpio -p -d -u $INITRD_MOUNT
248     popd
249
250     umount $INITRD_MOUNT
251     rmdir $INITRD_MOUNT
252     
253     echo "compressing ramdisk"
254     gzip $INITRD
255 }
256
257 function build()
258 {
259     # build base image via yum
260     build_cdroot
261
262     # always build/rebuild initrd
263     build_initrd
264
265     # build iso image
266     rm -f $ISO
267     mkisofs -o $ISO -R -allow-leading-dots -J -r -b isolinux/isolinux.bin \
268         -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table \
269         $CD_ROOT/usr
270
271     # build usb image and make it bootable with syslinux (instead of isolinux)
272     USB_IMAGE=${ISO%*.iso}.usb
273     # leave 1 MB of free space on the filesystem
274     USB_KB=$(du -kc $ISO $CD_ROOT/usr/isolinux | awk '$2 == "total" { print $1 + 1024 }')
275     mkfs.vfat -C $USB_IMAGE $USB_KB
276
277     mkdir -p $INITRD_MOUNT
278     mount -o loop,rw $USB_IMAGE $INITRD_MOUNT
279
280     # populate the root of the image with the iso, pl_version, and the syslinux files
281     cp -a $ISO $INITRD_MOUNT
282     cp -a $CD_ROOT/usr/isolinux/{initrd.gz,kernel,message.txt,pl_version} $INITRD_MOUNT
283     cp -a $CD_ROOT/usr/isolinux/isolinux.cfg $INITRD_MOUNT/syslinux.cfg
284
285     umount $INITRD_MOUNT
286     rmdir $INITRD_MOUNT
287
288     # make it bootable
289     syslinux $USB_IMAGE
290 }
291
292 function burn()
293 {
294     cdrecord $CDRECORD_FLAGS -data $ISO
295 }
296
297 function clean()
298 {
299     rm -rf $CD_ROOT
300     rm -rf $BUILD_DIR/syslinux
301     rm -rf $BUILD_DIR/$INITRD_MOUNT
302     rm -rf $BUILD_DIR
303     rm -f $ISO
304     rmdir --ignore-fail-on-non-empty build
305 }
306
307 if [[ "$1" == "clean" || "$1" == "burn" || "$1" == "build" ]]; then
308     action=$1
309     configuration=$2
310
311     if [[ -z "$configuration" ]]; then
312         configuration=default
313     fi
314
315     echo "Loading configuration $configuration"
316     CURRENT_CONFIG_DIR=$CONFIGURATIONS_DIR/$configuration
317     . $CURRENT_CONFIG_DIR/configuration
318
319     # setup vars for this configuration
320
321     # version string for this build
322     if [[ ! -z "$EXTRA_VERSION" ]]; then
323         FULL_VERSION_STRING="$FULL_VERSION_STRING $EXTRA_VERSION"
324     fi
325     FULL_VERSION_STRING="$FULL_VERSION_STRING $BOOTCD_VERSION"
326
327     # destination image
328     if [[ ! -z "$EXTRA_VERSION" ]]; then
329         OUTPUT_IMAGE_NAME="$OUTPUT_IMAGE_NAME-$EXTRA_VERSION"
330     fi
331     OUTPUT_IMAGE_NAME="$OUTPUT_IMAGE_NAME-$BOOTCD_VERSION"
332
333     # setup build directories
334     BUILD_DIR=build/$configuration
335     mkdir -p $BUILD_DIR
336     ISO=$BUILD_DIR/`echo $OUTPUT_IMAGE_NAME | sed -e "s/%version/$BOOTCD_VERSION/"`.iso
337
338     CD_ROOT=`pwd`/$BUILD_DIR/cdroot
339     mkdir -p $CD_ROOT
340
341     # location of the uncompressed ramdisk image
342     INITRD=$CD_ROOT/usr/isolinux/initrd
343
344     # temporary mount point for rd
345     INITRD_MOUNT=`pwd`/$BUILD_DIR/rd
346
347
348     case $action in 
349         build )
350             echo "Proceeding with building $DESCRIPTION"
351             build;;
352
353         clean )
354             echo "Removing built files for $DESCRIPTION"
355             clean;;
356
357         burn )
358             echo "Burning $DESCRIPTION"
359             burn;;
360     esac    
361 else
362     usage
363 fi
364