294d6a29a4d311c8309703f873efe00fdb6f8a29
[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: build.sh,v 1.4 2006/03/29 03:36:53 mlhuang Exp $
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 sources in either . or .."
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 root=fc$releasever
77 data=data$releasever
78
79 if [ ! -f $root.img ] ; then
80     bs=4096
81     count=$(($size / 4096))
82     dd bs=$bs count=$count if=/dev/zero of=$root.img
83     mkfs.ext3 -j -F $root.img
84 fi
85
86 mkdir -p $root $data
87 mount -o loop $root.img $root
88 trap "umount $root; exit 1" ERR
89
90 #
91 # Build
92 #
93
94 # Get package list
95 while read package ; do
96     packages="$packages -p $package"
97 done < <(./plc-config --packages $config)
98
99 # Install base system
100 mkfedora -v -r $releasever -a $basearch $packages $root
101
102 # FC2 minilogd starts up during shutdown and makes unmounting
103 # impossible. Just get rid of it.
104 rm -f $root/sbin/minilogd
105 ln -nsf /bin/true $root/sbin/minilogd
106
107 # Build schema
108 make -C $srcdir/pl_db
109
110 #
111 # Install
112 #
113
114 # Install configuration scripts
115 echo "* Installing configuration scripts"
116 install -D -m 755 plc_config.py $root/tmp/plc_config.py
117 chroot $root sh -c 'cd /tmp; python plc_config.py build; python plc_config.py install'
118 install -D -m 755 plc-config $root/usr/bin/plc-config
119 install -D -m 755 api-config $root/usr/bin/api-config
120
121 # Install init script
122 echo "* Installing initscript"
123 install -D -m 755 guest.init $root/etc/init.d/plc
124 chroot $root sh -c 'chkconfig --add plc; chkconfig plc on'
125
126 # Install DB schema and API code
127 echo "* Installing DB schema and API code"
128 mkdir -p $root/usr/share
129 rsync -a $srcdir/pl_db $srcdir/plc_api $root/usr/share/
130
131 # Install web scripts
132 echo "* Installing web scripts"
133 mkdir -p $root/usr/bin
134 install -m 755 \
135     $srcdir/plc/scripts/gen-sites-xml.py \
136     $srcdir/plc/scripts/gen-slices-xml-05.py \
137     $srcdir/plc/scripts/gen-static-content.py \
138     $root/usr/bin/
139
140 # Install web pages
141 echo "* Installing web pages"
142 mkdir -p $root/var/www/html
143 # Exclude old cruft, unrelated GENI pages, and official documents
144 rsync -a \
145     --exclude='*2002' --exclude='*2003' \
146     --exclude=geni --exclude=PDN --exclude=Talks \
147     $srcdir/plc_www/ $root/var/www/html/
148
149 # Install node RPMs
150 if [ -n "$RPM_BUILD_DIR" ] ; then
151     echo "* Installing node RPMs"
152     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
153     mkdir -p $root/var/www/html/install-rpms/planetlab
154     # Exclude ourself (e.g., if rebuilding), the bootcd and
155     # bootmanager builds, and debuginfo RPMs.
156     rsync -a \
157         --exclude='myplc-*' \
158         --exclude='bootcd-*' --exclude='bootmanager-*' \
159         --exclude='*-debuginfo-*' \
160         $(find $RPM_RPMS_DIR -type f -and -name '*.rpm') \
161         $root/var/www/html/install-rpms/planetlab/
162     if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
163         install -D -m 644 $RPM_RPMS_DIR/yumgroups.xml \
164             $root/var/www/html/install-rpms/planetlab/yumgroups.xml
165     fi
166     yum-arch $root/var/www/html/install-rpms/planetlab || :
167     createrepo $root/var/www/html/install-rpms/planetlab || :
168 fi
169
170 # XXX Build imprintable BootCD and BootManager images.
171
172 # Install configuration file
173 echo "* Installing configuration file"
174 install -D -m 644 $config $data/etc/planetlab/plc_config.xml
175
176 # Move "data" directories out of the installation
177 datadirs=(
178 /etc/planetlab
179 /var/lib/pgsql
180 /var/www/html/alpina-logs
181 /var/www/html/boot
182 /var/www/html/download
183 /var/www/html/generated
184 /var/www/html/xml
185 )
186
187 echo "* Moving data directories out of the installation"
188 mkdir -p $root/data
189 for datadir in "${datadirs[@]}" ; do
190     mkdir -p ${data}$datadir
191     if [ -d $root/$datadir -a ! -h $root/$datadir ] ; then
192         (cd $root && find ./$datadir | cpio -p -d -u ../$data/)
193     fi
194     rm -rf $root/$datadir
195     mkdir -p $(dirname $root/$datadir)
196     ln -nsf /data$datadir $root/$datadir
197 done
198
199 # Shrink to 100 MB free space
200 kb=$(python <<EOF
201 import os
202 df = os.statvfs('$root')
203 target = 100 * 1024 * 1024 / df.f_bsize
204 if df.f_bavail > target:
205     print (df.f_blocks - (df.f_bavail - target)) * df.f_bsize / 1024
206 EOF
207 )
208
209 umount $root
210 trap - ERR
211
212 if [ -n "$kb" ] ; then
213     # Setup loopback association. Newer versions of losetup have a -f
214     # option which finds an unused loopback device, but we must
215     # support FC2 for now.
216     # dev_loop=$(losetup -f)
217     for i in `seq 1 7` ; do
218         if ! grep -q "^/dev/loop$i" /proc/mounts ; then
219             dev_loop="/dev/loop$i"
220             break
221         fi
222     done
223     losetup $dev_loop $root.img
224     trap "losetup -d $dev_loop" ERR
225
226     # Resize the filesystem
227     echo "* Checking filesystem"
228     e2fsck -a -f $dev_loop
229     echo "* Shrinking filesystem"
230     resize2fs $dev_loop ${kb}K
231
232     # Tear down loopback association
233     losetup -d $dev_loop
234     trap - ERR
235
236     # Truncate the image file
237     perl -e "truncate '$root.img', $kb*1024"
238 fi
239
240 # Write sysconfig
241 cat >plc.sysconfig <<EOF
242 PLC_ROOT=$usr_share/plc/$root
243 PLC_DATA=$usr_share/plc/$data
244 #PLC_OPTIONS="-v"
245 EOF
246
247 # Bootstrap the system for quicker startup (and to populate the
248 # PlanetLabConf tables from PLC, which may not be accessible
249 # later). The bootstrap.xml configuration overlay configures the web
250 # server to run on an alternate port (in case the build machine itself
251 # is running a web server on port 80). Start everything up to
252 # bootstrap the database, then shut it back down again immediately.
253 echo "* Bootstrapping installation"
254
255 ./plc-config --save $data/etc/planetlab/plc_config.xml bootstrap.xml
256
257 # Otherwise, host.init will try to read /etc/sysconfig/plc
258 export PLC_ROOT=$PWD/$root
259 export PLC_DATA=$PWD/$data
260 #export PLC_OPTIONS="-v"
261
262 ./host.init start
263 RETVAL=$?
264
265 # Restore default configuration before shutting down
266 install -D -m 644 $config $data/etc/planetlab/plc_config.xml
267
268 ./host.init stop
269 RETVAL=$(($RETVAL+$?))
270
271 exit $RETVAL