- install /usr/bin/dns-config script
[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=4
32 basearch=i386
33
34 # Initial size of the image
35 size=1000000000
36
37 usage()
38 {
39     echo "Usage: build.sh [OPTION]..."
40     echo "      -c file         PLC configuration file (default: $config)"
41     echo "      -r release      Fedora release number (default: $releasever)"
42     echo "      -a arch         Fedora architecture (default: $basearch)"
43     echo "      -s size         Approximate size of the installation (default: $size)"
44     echo "      -h              This message"
45     exit 1
46 }
47
48 # Get options
49 while getopts "c:r:a:s:h" opt ; do
50     case $opt in
51         c)
52             config=$OPTARG
53             ;;
54         r)
55             releasever=$OPTARG
56             ;;
57         a)
58             basearch=$OPTARG
59             ;;
60         s)
61             size=$OPTARG
62             ;;
63         h|*)
64             usage
65             ;;
66     esac
67 done
68
69 # Do not tolerate errors
70 set -e
71
72 root=root
73 data=data
74
75 if [ ! -f $root.img ] ; then
76     bs=4096
77     count=$(($size / 4096))
78     dd bs=$bs count=$count if=/dev/zero of=$root.img
79     mkfs.ext3 -j -F $root.img
80 fi
81
82 mkdir -p $root $data
83 mount -o loop $root.img $root
84 trap "umount $root" ERR
85
86 #
87 # Build
88 #
89
90 # Get package list
91 while read package ; do
92     packages="$packages -p $package"
93 done < <(./plc-config --packages $config)
94
95 # Install base system
96 mkfedora -v -r $releasever -a $basearch -k $packages $root
97
98 # Disable all services in reference image
99 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
100
101 # FC2 minilogd starts up during shutdown and makes unmounting
102 # impossible. Just get rid of it.
103 rm -f $root/sbin/minilogd
104 ln -nsf /bin/true $root/sbin/minilogd
105
106 # Build schema
107 make -C $srcdir/pl_db
108
109 #
110 # Install
111 #
112
113 # Install configuration scripts
114 echo "* Installing configuration scripts"
115 install -D -m 755 plc_config.py $root/tmp/plc_config.py
116 chroot $root sh -c 'cd /tmp; python plc_config.py build; python plc_config.py install'
117 install -D -m 755 plc-config $root/usr/bin/plc-config
118 install -D -m 755 api-config $root/usr/bin/api-config
119 install -D -m 755 dns-config $root/usr/bin/dns-config
120
121 # Install initscripts
122 echo "* Installing initscripts"
123 find plc.d | cpio -p -d -u $root/etc/
124 install -D -m 755 guest.init $root/etc/init.d/plc
125 chroot $root sh -c 'chkconfig --add plc; chkconfig plc on'
126
127 # Install DB schema and API code
128 echo "* Installing DB schema and API code"
129 mkdir -p $root/usr/share
130 rsync -a $srcdir/pl_db $srcdir/plc_api $root/usr/share/
131
132 # Install web scripts
133 echo "* Installing web scripts"
134 mkdir -p $root/usr/bin
135 install -m 755 \
136     $srcdir/plc/scripts/gen-sites-xml.py \
137     $srcdir/plc/scripts/gen-slices-xml-05.py \
138     $srcdir/plc/scripts/gen-static-content.py \
139     $root/usr/bin/
140
141 # Install web pages
142 echo "* Installing web pages"
143 mkdir -p $root/var/www/html
144 # Exclude old cruft, unrelated GENI pages, and official documents
145 rsync -a \
146     --exclude='*2002' --exclude='*2003' \
147     --exclude=geni --exclude=PDN --exclude=Talks \
148     $srcdir/plc_www/ $root/var/www/html/
149
150 # Install configuration file
151 echo "* Installing configuration file"
152 install -D -m 444 $config $data/etc/planetlab/default_config.xml
153 install -D -m 444 plc_config.dtd $data/etc/planetlab/plc_config.dtd
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=/plc/$root
223 PLC_DATA=/plc/$data
224 #PLC_OPTIONS="-v"
225 EOF
226
227 # Initialize node RPMs directory. The PlanetLab-Bootstrap.tar.bz2
228 # tarball already contains all of the node RPMs pre-installed. Only
229 # updates or optional packages should be placed in this directory.
230 if [ -n "$RPM_BUILD_DIR" ] ; then
231     echo "* Initializing node RPMs directory"
232     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
233     mkdir -p $data/var/www/html/install-rpms/planetlab
234     if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
235         install -D -m 644 $RPM_RPMS_DIR/yumgroups.xml \
236             $data/var/www/html/install-rpms/planetlab/yumgroups.xml
237     fi
238     # yum-2.0.x
239     if [ -x /usr/bin/yum-arch ] ; then
240         yum-arch $data/var/www/html/install-rpms/planetlab
241     fi
242     # yum-2.4.x
243     if [ -x /usr/bin/createrepo ] ; then
244         if [ -f $data/var/www/html/install-rpms/planetlab/yumgroups.xml ] ; then
245             groupfile="-g yumgroups.xml"
246         fi
247         createrepo $groupfile $data/var/www/html/install-rpms/planetlab
248     fi
249 fi
250
251 # Bootstrap the system for quicker startup (and to populate the
252 # PlanetLabConf tables from PLC, which may not be accessible
253 # later). The bootstrap.xml configuration overlay configures the web
254 # server to run on an alternate port (in case the build machine itself
255 # is running a web server on port 80). Start everything up to
256 # bootstrap the database, then shut it back down again immediately.
257 echo "* Bootstrapping installation"
258
259 install -D -m 644 bootstrap.xml $data/etc/planetlab/configs/bootstrap.xml
260
261 # Otherwise, host.init will try to read /etc/sysconfig/plc
262 export PLC_ROOT=$PWD/$root
263 export PLC_DATA=$PWD/$data
264 #export PLC_OPTIONS="-v"
265
266 ./host.init start
267 RETVAL=$?
268
269 # Remove ISO and USB images, which take up >100MB but only take a
270 # couple of seconds to generate at first boot.
271 rm -f $data/var/www/html/download/*.{iso,usb}
272
273 ./host.init stop
274 RETVAL=$(($RETVAL+$?))
275
276 # Restore default configuration
277 rm -f $data/etc/planetlab/configs/bootstrap.xml
278 install -D -m 444 $config $data/etc/planetlab/plc_config.xml
279
280 exit $RETVAL