3 # Builds a Fedora based PLC image. You should be able to run this
4 # script multiple times without a problem.
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2006 The Trustees of Princeton University
9 # $Id: build.sh,v 1.2 2006/03/27 18:08:06 mlhuang Exp $
12 PATH=/sbin:/bin:/usr/sbin:/usr/bin
14 # In a normal CVS environment, the requisite CVS modules (including
15 # build/) are located at the same level we are. In a PlanetLab RPM
16 # build environment (see the RPM spec file), they are checked out into
18 if [ -d ./build ] ; then
21 elif [ -d ../build ] ; then
25 echo "Error: Could not find sources in either . or .."
31 # PLC configuration file
34 # Release and architecture to install
41 # Initial size of the image
46 echo "Usage: build.sh [OPTION]..."
47 echo " -c file PLC configuration file (default: $config)"
48 echo " -r release Fedora release number (default: $releasever)"
49 echo " -a arch Fedora architecture (default: $basearch)"
50 echo " -d datadir Data directory base (default: $usr_share)"
51 echo " -s size Approximate size of the installation (default: $size)"
52 echo " -h This message"
57 while getopts "c:r:a:d:s:h" opt ; do
83 if [ ! -f $root.img ] ; then
85 count=$(($size / 4096))
86 dd bs=$bs count=$count if=/dev/zero of=$root.img
87 mkfs.ext3 -j -F $root.img
91 mount -o loop $root.img $root
92 trap "umount $root; exit 1" ERR
99 while read package ; do
100 packages="$packages -p $package"
101 done < <(./plc-config --packages $config)
103 # Install base system
104 mkfedora -v -r $releasever -a $basearch $packages $root
106 # FC2 minilogd starts up during shutdown and makes unmounting
107 # impossible. Just get rid of it.
108 rm -f $root/sbin/minilogd
109 ln -nsf /bin/true $root/sbin/minilogd
112 make -C $srcdir/pl_db
118 # Install configuration scripts
119 install -D -m 755 plc_config.py $root/tmp/plc_config.py
120 chroot $root sh -c 'cd /tmp; python plc_config.py build; python plc_config.py install'
121 install -D -m 755 plc-config $root/usr/bin/plc-config
122 install -D -m 755 api-config $root/usr/bin/api-config
124 # Install init script
125 install -D -m 755 guest.init $root/etc/init.d/plc
126 chroot $root sh -c 'chkconfig --add plc; chkconfig plc on'
128 # Install DB schema and API code
129 mkdir -p $root/usr/share
130 rsync -a $srcdir/pl_db $srcdir/plc_api $root/usr/share/
132 # Install web scripts
133 mkdir -p $root/usr/bin
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 \
141 mkdir -p $root/var/www/html
142 rsync -a $srcdir/plc_www/ $root/var/www/html/
145 if [ -n "$RPM_BUILD_DIR" ] ; then
146 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
147 mkdir -p $root/var/www/html/install-rpms/planetlab
148 find $RPM_RPMS_DIR -type f -and -not -name '*-debuginfo-*' | \
149 cpio -p -d -u $root/var/www/html/install-rpms/planetlab/
150 yum-arch $root/var/www/html/install-rpms/planetlab || :
151 createrepo $root/var/www/html/install-rpms/planetlab || :
154 # XXX Build imprintable BootCD and BootManager images.
156 # Install configuration file
157 install -D -m 644 $config $data/etc/planetlab/plc_config.xml
159 # Move "data" directories out of the installation
163 /var/www/html/alpina-logs
165 /var/www/html/download
166 /var/www/html/generated
167 /var/www/html/install-rpms
172 for datadir in "${datadirs[@]}" ; do
173 mkdir -p ${data}$datadir
174 if [ -d $root/$datadir -a ! -h $root/$datadir ] ; then
175 (cd $root && find ./$datadir | cpio -p -d -u ../$data/)
177 rm -rf $root/$datadir
178 mkdir -p $(dirname $root/$datadir)
179 ln -nsf /data$datadir $root/$datadir
182 # Shrink to 100 MB free space
185 df = os.statvfs('$root')
186 target = 100 * 1024 * 1024 / df.f_bsize
187 if df.f_bavail > target:
188 print (df.f_blocks - (df.f_bavail - target)) * df.f_bsize / 1024
195 if [ -n "$kb" ] ; then
196 # Setup loopback association. Newer versions of losetup have a -f
197 # option which finds an unused loopback device, but we must
198 # support FC2 for now.
199 # dev_loop=$(losetup -f)
200 for i in `seq 1 7` ; do
201 if ! grep -q "^/dev/loop$i" /proc/mounts ; then
202 dev_loop="/dev/loop$i"
206 losetup $dev_loop $root.img
207 trap "losetup -d $dev_loop" ERR
209 # Resize the filesystem
211 resize2fs $dev_loop ${kb}K
213 # Tear down loopback association
217 # Truncate the image file
218 perl -e "truncate '$root.img', $kb*1024"
222 cat >plc.sysconfig <<EOF
223 PLC_ROOT=$usr_share/plc/$root
224 PLC_DATA=$usr_share/plc/$data
228 # Bootstrap the system. Configure the web server to run on an
229 # alternate port (in case the build machine is running a web server),
230 # start everything up, then shut it back down again.
231 ./plc-config --save $data/etc/planetlab/plc_config.xml bootstrap.xml
233 export PLC_ROOT=$PWD/$root
234 export PLC_DATA=$PWD/$data
239 # Restore default configuration before shutting down
240 install -D -m 644 $config $data/etc/planetlab/plc_config.xml