update the version file in the old location (used for backward compatibility)
[bootcd.git] / build.sh
1 #!/bin/bash
2
3 set -e
4
5 BOOTCD_VERSION="3.0-beta0.4"
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     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/bootme/ID
167
168     echo "forcing lvm to make lvm1 partitions (TEMPORARY)"
169     cp -f $CONF_FILES_DIR/lvm.conf $CD_ROOT/etc/lvm/
170
171     echo "copying isolinux configuration files"
172     cp -f $CONF_FILES_DIR/isolinux.cfg $CD_ROOT/usr/isolinux/
173     echo "$FULL_VERSION_STRING" > $CD_ROOT/usr/isolinux/message.txt
174
175     echo "writing /etc/issue"
176     echo "$FULL_VERSION_STRING" > $CD_ROOT/etc/issue
177     echo "Kernel \r on an \m" >> $CD_ROOT/etc/issue
178     echo "" >> $CD_ROOT/etc/issue
179     echo "" >> $CD_ROOT/etc/issue
180
181     echo "making the isolinux initrd kernel command line match rd size"
182     let INITRD_SIZE_KB=$(($RAMDISK_SIZE * 1024))
183     sed -i "s#ramdisk_size=0#ramdisk_size=$INITRD_SIZE_KB#g" \
184         $CD_ROOT/usr/isolinux/isolinux.cfg
185
186     echo "building pcitable for hardware detection"
187     pci_map_file=`find $CD_ROOT/lib/modules/ -name modules.pcimap | head -1`
188     module_dep_file=`find $CD_ROOT/lib/modules/ -name modules.dep | head -1`
189     pci_table=$CD_ROOT/usr/share/hwdata/pcitable
190     ./scripts/rewrite-pcitable.py $module_dep_file $pci_map_file $pci_table \
191         $CD_ROOT/etc/pl_pcitable
192
193     dd if=/dev/zero of=$INITRD bs=1M count=$RAMDISK_SIZE
194     mkfs.ext2 -F -m 0 -i $INITRD_BYTES_PER_INODE $INITRD
195     mkdir -p $INITRD_MOUNT
196     mount -o loop,rw $INITRD $INITRD_MOUNT
197
198     echo "copy all files except usr to ramdisk"
199     pushd .
200     cd $CD_ROOT
201     find . -path ./usr -prune -o -print | cpio -p -d -u $INITRD_MOUNT
202     popd
203
204     umount $INITRD_MOUNT
205     rmdir $INITRD_MOUNT
206     
207     echo "compressing ramdisk"
208     gzip $INITRD
209 }
210
211 function build_iso()
212 {
213     echo "building iso"
214     rm -f $ISO
215     mkisofs -o $ISO -R -allow-leading-dots -J -r -b isolinux/isolinux.bin \
216         -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table \
217         -V PlanetLab-3-0 $CD_ROOT/usr
218 }
219
220 function burn()
221 {
222     cdrecord $CDRECORD_FLAGS -data $ISO
223 }
224
225 function clean()
226 {
227     echo "removing built files"
228     rm -rf cdroot
229     rm -rf syslinux
230     rm -rf $INITRD_MOUNT
231     rm -f $ISO
232 }
233
234
235 if [ "$1" == "clean" ]; then
236     clean
237     exit
238 fi
239
240 if [ "$1" == "burn" ]; then
241     burn
242     exit
243 fi
244
245 if [ "$1" == "force" ]; then
246     clean
247 fi
248
249 # build base image via yum
250 build_cdroot
251
252 # always build/rebuild initrd
253 build_initrd
254
255 build_iso