add a nice little hack to make sure that yum installs all the rpms
[bootcd.git] / build.sh
1 #!/bin/bash
2
3 set -e
4
5 BOOTCD_VERSION="3.0-beta0.3"
6 FULL_VERSION_STRING="PlanetLab BootCD $BOOTCD_VERSION"
7
8 # which boot server to contact
9 BOOTSERVER='boot.planet-lab.org'
10
11 # and on which port (protocol will be https)
12 BOOTSERVER_PORT='443'
13
14 # finally, what path to request from the server
15 BOOTSERVER_PATH='boot/'
16
17 SYSLINUX_SRC=sources/syslinux-2.11.tar.bz2
18
19 ISO=cd.iso
20
21 CD_ROOT=`pwd`/cdroot
22 ROOT_PASSWD='$1$IdEn2srw$/TfrjZSPUC1xP244YCuIi0'
23
24 BOOTCD_YUM_GROUP=BootCD
25
26 CDRECORD_FLAGS="-v -dao"
27
28 CONF_FILES_DIR=conf_files/
29
30 # location of the uncompressed ramdisk image
31 INITRD=$CD_ROOT/usr/isolinux/initrd
32
33 # temporary mount point for rd
34 INITRD_MOUNT=`pwd`/rd
35
36 # size of the ram disk in MB
37 RAMDISK_SIZE=64
38
39 # the bytes per inode ratio (the -i value in mkfs.ext2) for the ramdisk
40 INITRD_BYTES_PER_INODE=1024
41
42
43 function build_cdroot()
44 {
45     if [ -f $CD_ROOT/.built ]; then
46         echo "cd root already built, skipping"
47         return
48     fi
49
50     clean
51     
52     mkdir -p $CD_ROOT/dev/pts
53     mkdir -p $CD_ROOT/proc
54     mkdir -p $CD_ROOT/etc
55
56     echo "copy fstab and mtab"
57     cp -f $CONF_FILES_DIR/fstab $CD_ROOT/etc/
58     cp -f $CONF_FILES_DIR/mtab $CD_ROOT/etc/
59
60     echo "setup rpm to install only en_US locale and no docs"
61     mkdir -p $CD_ROOT/etc/rpm
62     cp -f $CONF_FILES_DIR/macros $CD_ROOT/etc/rpm
63
64     echo "initialize rpm db"
65     mkdir -p $CD_ROOT/var/lib/rpm
66     rpm --root $CD_ROOT --initdb
67
68     echo "install boot cd base rpms"
69     yum -c yum.conf --installroot=$CD_ROOT -y groupinstall $BOOTCD_YUM_GROUP
70
71     echo "checking to make sure rpms were installed"
72     packages=`cat yumgroups.xml | grep packagereq | sed 's#<[^<]*>##g'`
73     set +e
74     for package in $packages; do
75         echo "checking for package $package"
76         chroot $CD_ROOT /bin/rpm -qi $package > /dev/null
77         if [[ "$?" -ne 0 ]]; then
78             echo "package $package was not installed in the cd root."
79             echo "make sure it exists in the yum repository."
80             exit 1
81         fi
82     done
83     set -e
84     
85     echo "removing unneccessary build files"
86     (cd $CD_ROOT/lib/modules && \
87         find ./ -type d -name build -maxdepth 2 -exec rm -rf {} \;)
88
89     echo "setting up non-ssh authentication"
90     mkdir -p $CD_ROOT/etc/samba
91     chroot $CD_ROOT /usr/sbin/authconfig --nostart --kickstart \
92         --enablemd5 --enableshadow
93
94     echo "setting root password"
95     sed -i "s#root::#root:$ROOT_PASSWD:#g" $CD_ROOT/etc/shadow
96
97     echo "relocate some large directories out of the root system"
98     # get /var/lib/rpm out, its 12mb. create in its place a 
99     # symbolic link to /usr/relocated/var/lib/rpm
100     mkdir -p $CD_ROOT/usr/relocated/var/lib/
101     mv $CD_ROOT/var/lib/rpm $CD_ROOT/usr/relocated/var/lib/
102     (cd $CD_ROOT/var/lib && ln -s ../../usr/relocated/var/lib/rpm rpm)
103
104     # get /lib/tls out
105     mkdir -p $CD_ROOT/usr/relocated/lib
106     mv $CD_ROOT/lib/tls $CD_ROOT/usr/relocated/lib/
107     (cd $CD_ROOT/lib && ln -s ../usr/relocated/lib/tls tls)
108
109     echo "extracting syslinux, copying isolinux files to cd"
110     mkdir -p syslinux
111     mkdir -p $CD_ROOT/usr/isolinux/
112     tar -C syslinux -xjvf $SYSLINUX_SRC
113     find syslinux -name isolinux.bin -exec cp -f {} $CD_ROOT/usr/isolinux/ \;
114
115     echo "moving kernel to isolinux directory"
116     KERNEL=$CD_ROOT/boot/vmlinuz-*
117     mv -f $KERNEL $CD_ROOT/usr/isolinux/kernel
118
119     echo "creating version files"
120     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/pl_version
121     echo "$FULL_VERSION_STRING" > $CD_ROOT/pl_version
122
123     touch $CD_ROOT/.built
124 }
125
126 function build_initrd()
127 {
128     echo "building initrd"
129     rm -f $INITRD
130     rm -f $INITRD.gz
131
132     echo "copy fstab and mtab"
133     cp -f $CONF_FILES_DIR/fstab $CD_ROOT/etc/
134     cp -f $CONF_FILES_DIR/mtab $CD_ROOT/etc/
135
136     echo "installing generic modprobe.conf"
137     cp -f $CONF_FILES_DIR/modprobe.conf $CD_ROOT/etc/
138
139     echo "installing our own inittab and init scripts"
140     cp -f $CONF_FILES_DIR/inittab $CD_ROOT/etc
141     init_scripts="pl_sysinit pl_hwinit pl_netinit pl_validateconf pl_boot"
142     for script in $init_scripts; do
143         cp -f $CONF_FILES_DIR/$script $CD_ROOT/etc/init.d/
144         chmod +x $CD_ROOT/etc/init.d/$script
145     done
146
147     echo "setup basic networking files"
148     cp -f $CONF_FILES_DIR/hosts $CD_ROOT/etc/
149
150     echo "copying sysctl.conf (fix tcp window scaling and broken routers)"
151     cp -f $CONF_FILES_DIR/sysctl.conf $CD_ROOT/etc/
152
153     echo "setup default network conf file"
154     mkdir -p $CD_ROOT/usr/boot
155     cp -f $CONF_FILES_DIR/default-net.cnf $CD_ROOT/usr/boot/
156
157     echo "setup boot server configuration"
158     cp -f $CONF_FILES_DIR/cacert.pem $CD_ROOT/usr/boot/
159     cp -f $CONF_FILES_DIR/pubring.gpg $CD_ROOT/usr/boot/
160     echo "$BOOTSERVER" > $CD_ROOT/usr/boot/boot_server
161     echo "$BOOTSERVER_PORT" > $CD_ROOT/usr/boot/boot_server_port
162     echo "$BOOTSERVER_PATH" > $CD_ROOT/usr/boot/boot_server_path
163
164     echo "copying old boot cd directory bootme (TEMPORARY)"
165     cp -r bootme_old $CD_ROOT/usr/bootme
166
167     echo "forcing lvm to make lvm1 partitions (TEMPORARY)"
168     cp -f $CONF_FILES_DIR/lvm.conf $CD_ROOT/etc/lvm/
169
170     echo "copying isolinux configuration files"
171     cp -f $CONF_FILES_DIR/isolinux.cfg $CD_ROOT/usr/isolinux/
172     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/message.txt
173
174     echo "writing /etc/issue"
175     echo "$FULL_VERSION_STRING" > $CD_ROOT/etc/issue
176     echo "Kernel \r on an \m" >> $CD_ROOT/etc/issue
177     echo "" >> $CD_ROOT/etc/issue
178     echo "" >> $CD_ROOT/etc/issue
179
180     echo "making the isolinux initrd kernel command line match rd size"
181     let INITRD_SIZE_KB=$(($RAMDISK_SIZE * 1024))
182     sed -i "s#ramdisk_size=0#ramdisk_size=$INITRD_SIZE_KB#g" \
183         $CD_ROOT/usr/isolinux/isolinux.cfg
184
185     echo "building pcitable for hardware detection"
186     pci_map_file=`find $CD_ROOT/lib/modules/ -name modules.pcimap | head -1`
187     module_dep_file=`find $CD_ROOT/lib/modules/ -name modules.dep | head -1`
188     pci_table=$CD_ROOT/usr/share/hwdata/pcitable
189     ./scripts/rewrite-pcitable.py $module_dep_file $pci_map_file $pci_table \
190         $CD_ROOT/etc/pl_pcitable
191
192     dd if=/dev/zero of=$INITRD bs=1M count=$RAMDISK_SIZE
193     mkfs.ext2 -F -m 0 -i $INITRD_BYTES_PER_INODE $INITRD
194     mkdir -p $INITRD_MOUNT
195     mount -o loop,rw $INITRD $INITRD_MOUNT
196
197     echo "copy all files except usr to ramdisk"
198     pushd .
199     cd $CD_ROOT
200     find . -path ./usr -prune -o -print | cpio -p -d -u $INITRD_MOUNT
201     popd
202
203     umount $INITRD_MOUNT
204     rmdir $INITRD_MOUNT
205     
206     echo "compressing ramdisk"
207     gzip $INITRD
208 }
209
210 function build_iso()
211 {
212     echo "building iso"
213     rm -f $ISO
214     mkisofs -o $ISO -R -allow-leading-dots -J -r -b isolinux/isolinux.bin \
215         -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table \
216         -V PlanetLab-3-0 $CD_ROOT/usr
217 }
218
219 function burn()
220 {
221     cdrecord $CDRECORD_FLAGS -data $ISO
222 }
223
224 function clean()
225 {
226     echo "removing built files"
227     rm -rf cdroot
228     rm -rf syslinux
229     rm -rf $INITRD_MOUNT
230     rm -f $ISO
231 }
232
233
234 if [ "$1" == "clean" ]; then
235     clean
236     exit
237 fi
238
239 if [ "$1" == "burn" ]; then
240     burn
241     exit
242 fi
243
244 if [ "$1" == "force" ]; then
245     clean
246 fi
247
248 # build base image via yum
249 build_cdroot
250
251 # always build/rebuild initrd
252 build_initrd
253
254 build_iso