remove backward compatibility symlink, yum.conf generation now fixed
[myplc.git] / build.sh
1 #!/bin/bash
2 #
3 # Builds a Fedora based PLC image. You should be able to run this
4 # script multiple times without a problem.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2006 The Trustees of Princeton University
8 #
9 # $Id$
10 #
11
12 PATH=/sbin:/bin:/usr/sbin:/usr/bin
13
14 # In both a normal CVS environment and a PlanetLab RPM
15 # build environment, all of our dependencies are checked out into
16 # directories at the same level as us.
17 if [ -d ../build ] ; then
18     PATH=$PATH:../build
19     srcdir=..
20 else
21     echo "Error: Could not find $(cd .. && pwd -P)/build/"
22     exit 1
23 fi
24
25 export PATH
26
27 # PLC configuration file
28 config=plc_config.xml
29
30 # Release and architecture to install
31 releasever=2
32 basearch=i386
33
34 # Data directory base
35 usr_share=/usr/share
36
37 # Initial size of the image
38 size=1000000000
39
40 usage()
41 {
42     echo "Usage: build.sh [OPTION]..."
43     echo "      -c file         PLC configuration file (default: $config)"
44     echo "      -r release      Fedora release number (default: $releasever)"
45     echo "      -a arch         Fedora architecture (default: $basearch)"
46     echo "      -d datadir      Data directory base (default: $usr_share)"
47     echo "      -s size         Approximate size of the installation (default: $size)"
48     echo "      -h              This message"
49     exit 1
50 }
51
52 # Get options
53 while getopts "c:r:a:d:s:h" opt ; do
54     case $opt in
55         c)
56             config=$OPTARG
57             ;;
58         r)
59             releasever=$OPTARG
60             ;;
61         a)
62             basearch=$OPTARG
63             ;;
64         d)
65             usr_share=$OPTARG
66             ;;
67         s)
68             size=$OPTARG
69             ;;
70         h|*)
71             usage
72             ;;
73     esac
74 done
75
76 # Do not tolerate errors
77 set -e
78
79 root=fc$releasever
80 data=data$releasever
81
82 if [ ! -f $root.img ] ; then
83     bs=4096
84     count=$(($size / 4096))
85     dd bs=$bs count=$count if=/dev/zero of=$root.img
86     mkfs.ext3 -j -F $root.img
87 fi
88
89 mkdir -p $root $data
90 mount -o loop $root.img $root
91 trap "umount $root" ERR
92
93 #
94 # Build
95 #
96
97 # Get package list
98 while read package ; do
99     packages="$packages -p $package"
100 done < <(./plc-config --packages $config)
101
102 # Install base system
103 mkfedora -v -r $releasever -a $basearch $packages $root
104
105 # Disable all services in reference image
106 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
107
108 # FC2 minilogd starts up during shutdown and makes unmounting
109 # impossible. Just get rid of it.
110 rm -f $root/sbin/minilogd
111 ln -nsf /bin/true $root/sbin/minilogd
112
113 # Build schema
114 make -C $srcdir/pl_db
115
116 #
117 # Install
118 #
119
120 # Install configuration scripts
121 echo "* Installing configuration scripts"
122 install -D -m 755 plc_config.py $root/tmp/plc_config.py
123 chroot $root sh -c 'cd /tmp; python plc_config.py build; python plc_config.py install'
124 install -D -m 755 plc-config $root/usr/bin/plc-config
125 install -D -m 755 api-config $root/usr/bin/api-config
126
127 # Install init script
128 echo "* Installing initscript"
129 install -D -m 755 guest.init $root/etc/init.d/plc
130 chroot $root sh -c 'chkconfig --add plc; chkconfig plc on'
131
132 # Install DB schema and API code
133 echo "* Installing DB schema and API code"
134 mkdir -p $root/usr/share
135 rsync -a $srcdir/pl_db $srcdir/plc_api $root/usr/share/
136
137 # Install web scripts
138 echo "* Installing web scripts"
139 mkdir -p $root/usr/bin
140 install -m 755 \
141     $srcdir/plc/scripts/gen-sites-xml.py \
142     $srcdir/plc/scripts/gen-slices-xml-05.py \
143     $srcdir/plc/scripts/gen-static-content.py \
144     $root/usr/bin/
145
146 # Install web pages
147 echo "* Installing web pages"
148 mkdir -p $root/var/www/html
149 # Exclude old cruft, unrelated GENI pages, and official documents
150 rsync -a \
151     --exclude='*2002' --exclude='*2003' \
152     --exclude=geni --exclude=PDN --exclude=Talks \
153     $srcdir/plc_www/ $root/var/www/html/
154
155 # XXX Build imprintable BootCD and BootManager images.
156
157 # Install configuration file
158 echo "* Installing configuration file"
159 install -D -m 644 $config $data/etc/planetlab/plc_config.xml
160
161 # Move "data" directories out of the installation
162 datadirs=(
163 /etc/planetlab
164 /var/lib/pgsql
165 /var/www/html/alpina-logs
166 /var/www/html/boot
167 /var/www/html/download
168 /var/www/html/generated
169 /var/www/html/install-rpms
170 /var/www/html/xml
171 )
172
173 echo "* Moving data directories out of the installation"
174 mkdir -p $root/data
175 for datadir in "${datadirs[@]}" ; do
176     mkdir -p ${data}$datadir
177     if [ -d $root/$datadir -a ! -h $root/$datadir ] ; then
178         (cd $root && find ./$datadir | cpio -p -d -u ../$data/)
179     fi
180     rm -rf $root/$datadir
181     mkdir -p $(dirname $root/$datadir)
182     ln -nsf /data$datadir $root/$datadir
183 done
184
185 # Shrink to 100 MB free space
186 kb=$(python <<EOF
187 import os
188 df = os.statvfs('$root')
189 target = 100 * 1024 * 1024 / df.f_bsize
190 if df.f_bavail > target:
191     print (df.f_blocks - (df.f_bavail - target)) * df.f_bsize / 1024
192 EOF
193 )
194
195 umount $root
196 trap - ERR
197
198 if [ -n "$kb" ] ; then
199     # Setup loopback association. Newer versions of losetup have a -f
200     # option which finds an unused loopback device, but we must
201     # support FC2 for now.
202     # dev_loop=$(losetup -f)
203     for i in `seq 1 7` ; do
204         if ! grep -q "^/dev/loop$i" /proc/mounts ; then
205             dev_loop="/dev/loop$i"
206             break
207         fi
208     done
209     losetup $dev_loop $root.img
210     trap "losetup -d $dev_loop" ERR
211
212     # Resize the filesystem
213     echo "* Checking filesystem"
214     e2fsck -a -f $dev_loop
215     echo "* Shrinking filesystem"
216     resize2fs $dev_loop ${kb}K
217
218     # Tear down loopback association
219     losetup -d $dev_loop
220     trap - ERR
221
222     # Truncate the image file
223     perl -e "truncate '$root.img', $kb*1024"
224 fi
225
226 # Write sysconfig
227 cat >plc.sysconfig <<EOF
228 PLC_ROOT=$usr_share/plc/$root
229 PLC_DATA=$usr_share/plc/$data
230 #PLC_OPTIONS="-v"
231 EOF
232
233 # Install node RPMs
234 if [ -n "$RPM_BUILD_DIR" ] ; then
235     echo "* Installing node RPMs"
236     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
237     mkdir -p $data/var/www/html/install-rpms/planetlab
238     # Exclude ourself (e.g., if rebuilding), the bootcd and
239     # bootmanager builds, and debuginfo RPMs.
240     rsync -a \
241         --exclude='myplc-*' \
242         --exclude='bootcd-*' --exclude='bootmanager-*' \
243         --exclude='*-debuginfo-*' \
244         $(find $RPM_RPMS_DIR -type f -and -name '*.rpm') \
245         $data/var/www/html/install-rpms/planetlab/
246     if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
247         install -D -m 644 $RPM_RPMS_DIR/yumgroups.xml \
248             $data/var/www/html/install-rpms/planetlab/yumgroups.xml
249     fi
250     # yum-2.0.x
251     if [ -x /usr/bin/yum-arch ] ; then
252         yum-arch $data/var/www/html/install-rpms/planetlab
253     fi
254     # yum-2.4.x
255     if [ -x /usr/bin/createrepo ] ; then
256         if [ -f $data/var/www/html/install-rpms/planetlab/yumgroups.xml ] ; then
257             groupfile="-g yumgroups.xml"
258         fi
259         createrepo $groupfile $data/var/www/html/install-rpms/planetlab
260     fi
261 fi
262
263 # Bootstrap the system for quicker startup (and to populate the
264 # PlanetLabConf tables from PLC, which may not be accessible
265 # later). The bootstrap.xml configuration overlay configures the web
266 # server to run on an alternate port (in case the build machine itself
267 # is running a web server on port 80). Start everything up to
268 # bootstrap the database, then shut it back down again immediately.
269 echo "* Bootstrapping installation"
270
271 ./plc-config --save $data/etc/planetlab/plc_config.xml bootstrap.xml
272
273 # Otherwise, host.init will try to read /etc/sysconfig/plc
274 export PLC_ROOT=$PWD/$root
275 export PLC_DATA=$PWD/$data
276 #export PLC_OPTIONS="-v"
277
278 ./host.init start
279 RETVAL=$?
280
281 # Restore default configuration before shutting down
282 install -D -m 644 $config $data/etc/planetlab/plc_config.xml
283
284 ./host.init stop
285 RETVAL=$(($RETVAL+$?))
286
287 exit $RETVAL