- explicitly add openssh requirement
[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 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 # XXX Build imprintable BootCD and BootManager images.
150
151 # Install configuration file
152 echo "* Installing configuration file"
153 install -D -m 644 $config $data/etc/planetlab/plc_config.xml
154
155 # Move "data" directories out of the installation
156 datadirs=(
157 /etc/planetlab
158 /var/lib/pgsql
159 /var/www/html/alpina-logs
160 /var/www/html/boot
161 /var/www/html/download
162 /var/www/html/generated
163 /var/www/html/install-rpms
164 /var/www/html/xml
165 )
166
167 echo "* Moving data directories out of the installation"
168 mkdir -p $root/data
169 for datadir in "${datadirs[@]}" ; do
170     mkdir -p ${data}$datadir
171     if [ -d $root/$datadir -a ! -h $root/$datadir ] ; then
172         (cd $root && find ./$datadir | cpio -p -d -u ../$data/)
173     fi
174     rm -rf $root/$datadir
175     mkdir -p $(dirname $root/$datadir)
176     ln -nsf /data$datadir $root/$datadir
177 done
178
179 # Shrink to 100 MB free space
180 kb=$(python <<EOF
181 import os
182 df = os.statvfs('$root')
183 target = 100 * 1024 * 1024 / df.f_bsize
184 if df.f_bavail > target:
185     print (df.f_blocks - (df.f_bavail - target)) * df.f_bsize / 1024
186 EOF
187 )
188
189 umount $root
190 trap - ERR
191
192 if [ -n "$kb" ] ; then
193     # Setup loopback association. Newer versions of losetup have a -f
194     # option which finds an unused loopback device, but we must
195     # support FC2 for now.
196     # dev_loop=$(losetup -f)
197     for i in `seq 1 7` ; do
198         if ! grep -q "^/dev/loop$i" /proc/mounts ; then
199             dev_loop="/dev/loop$i"
200             break
201         fi
202     done
203     losetup $dev_loop $root.img
204     trap "losetup -d $dev_loop" ERR
205
206     # Resize the filesystem
207     echo "* Checking filesystem"
208     e2fsck -a -f $dev_loop
209     echo "* Shrinking filesystem"
210     resize2fs $dev_loop ${kb}K
211
212     # Tear down loopback association
213     losetup -d $dev_loop
214     trap - ERR
215
216     # Truncate the image file
217     perl -e "truncate '$root.img', $kb*1024"
218 fi
219
220 # Write sysconfig
221 cat >plc.sysconfig <<EOF
222 PLC_ROOT=$usr_share/plc/$root
223 PLC_DATA=$usr_share/plc/$data
224 #PLC_OPTIONS="-v"
225 EOF
226
227 # Install node RPMs
228 if [ -n "$RPM_BUILD_DIR" ] ; then
229     echo "* Installing node RPMs"
230     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
231     mkdir -p $data/var/www/html/install-rpms/planetlab
232     # Exclude ourself (e.g., if rebuilding), the bootcd and
233     # bootmanager builds, and debuginfo RPMs.
234     rsync -a \
235         --exclude='myplc-*' \
236         --exclude='bootcd-*' --exclude='bootmanager-*' \
237         --exclude='*-debuginfo-*' \
238         $(find $RPM_RPMS_DIR -type f -and -name '*.rpm') \
239         $data/var/www/html/install-rpms/planetlab/
240     if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
241         install -D -m 644 $RPM_RPMS_DIR/yumgroups.xml \
242             $data/var/www/html/install-rpms/planetlab/yumgroups.xml
243     fi
244     # yum-2.0.x
245     if [ -x /usr/bin/yum-arch ] ; then
246         yum-arch $data/var/www/html/install-rpms/planetlab
247     fi
248     # yum-2.4.x
249     if [ -x /usr/bin/createrepo ] ; then
250         if [ -f $data/var/www/html/install-rpms/planetlab/yumgroups.xml ] ; then
251             groupfile="-g yumgroups.xml"
252         fi
253         createrepo $groupfile $data/var/www/html/install-rpms/planetlab
254     fi
255 fi
256
257 # Bootstrap the system for quicker startup (and to populate the
258 # PlanetLabConf tables from PLC, which may not be accessible
259 # later). The bootstrap.xml configuration overlay configures the web
260 # server to run on an alternate port (in case the build machine itself
261 # is running a web server on port 80). Start everything up to
262 # bootstrap the database, then shut it back down again immediately.
263 echo "* Bootstrapping installation"
264
265 ./plc-config --save $data/etc/planetlab/plc_config.xml bootstrap.xml
266
267 # Otherwise, host.init will try to read /etc/sysconfig/plc
268 export PLC_ROOT=$PWD/$root
269 export PLC_DATA=$PWD/$data
270 #export PLC_OPTIONS="-v"
271
272 ./host.init start
273 RETVAL=$?
274
275 # Restore default configuration before shutting down
276 install -D -m 644 $config $data/etc/planetlab/plc_config.xml
277
278 ./host.init stop
279 RETVAL=$(($RETVAL+$?))
280
281 exit $RETVAL