(*) groups distro-dependent files in build/config.<distro>
[vserver-reference.git] / build.sh
1 #!/bin/bash
2 #
3 # Builds all reference image for vservers.  To optimize for space it
4 # will only build a complete base vserver reference image and then
5 # builds "stub" images that are just contain the additional files
6 # and/or changes for a given reference image.  This is done to shrink
7 # the RPM itself.  These will be pieced back together with the base
8 # vserver reference image by an init script that is installed on the
9 # node.
10 #
11 # Mark Huang <mlhuang@cs.princeton.edu>
12 # Marc E. Fiuczynski <mef@cs.princeton.edu>
13 # Copyright (C) 2004-2007 The Trustees of Princeton University
14 #
15 # $Id: build.sh,v 1.20 2007/09/06 20:41:23 faiyaza Exp $
16 #
17
18 PATH=/sbin:/bin:/usr/sbin:/usr/bin
19
20 # In both a normal CVS environment and a PlanetLab RPM
21 # build environment, all of our dependencies are checked out into
22 # directories at the same level as us.
23 if [ -d ../build ] ; then
24     PATH=$PATH:../build
25     srcdir=..
26 else
27     echo "Error: Could not find $(cd .. && pwd -P)/build/"
28     exit 1
29 fi
30
31 export PATH
32
33 # build.common comes from the build module
34 . build.common
35
36 pl_process_fedora_options $@
37 shiftcount=$?
38 shift $shiftcount
39
40 # pldistro expected as $1 - defaults to planetlab
41 pldistro=planetlab
42 [ -n "$@" ] && pldistro=$1
43
44 # Do not tolerate errors
45 set -e
46
47 # Path's to the vserver references images and stubs
48 vrefdir=$PWD/vservers/.vref
49 vstubdir=$PWD/vservers/.vstub
50
51 # XXX: The vserver reference name should be passed in as an argument
52 # rather than being hardcoded.
53 vrefname=default
54
55 # Make /vservers and default vserver reference image
56 vref=${vrefdir}/${vrefname}
57 install -d -m 755 ${vref}
58
59 # "Parse" out the packages and groups for mkfedora
60 pkgsfile=$(pl_locateDistroFile ../build/ ${pldistro} vserver.pkgs)
61
62 # Populate a minimal /dev in the reference image
63 pl_makedevs ${vref}
64
65 # Populate image with vserver-reference packages
66 pl_setup_chroot ${vref} -k -f $pkgsfile
67
68 for systemvserver in ../build/config.${pldistro}/vserver-*.pkgs ; do
69     NAME=$(basename $systemvserver .pkgs | sed -e s,vserver-,,)
70
71     echo "--------START BUILDING system vserver ${NAME}: $(date)"
72
73     # "Parse" out the packages and groups for yum
74     systempackages=$(pl_getPackages ${pl_DISTRO_NAME} $systemvserver)
75     systemgroups=$(pl_getGroups ${pl_DISTRO_NAME} $systemvserver)
76
77     vdir=${vstubdir}/${NAME}
78     rm -rf ${vdir}/*
79     install -d -m 755 ${vdir}
80
81     # Clone the base vserver reference to the system vserver reference
82     (cd ${vref} && find . | cpio -m -d -u -p ${vdir})
83     rm -f ${vdir}/var/lib/rpm/__db*
84
85     # Communicate to the initialization script from which vref this stub was cloned
86     echo ${vrefname} > ${vdir}.cloned
87
88     # Install the system vserver specific packages
89     [ -n "$systempackages" ] && yum -c ${vdir}/etc/yum.conf --installroot=${vdir} -y install $systempackages
90     [ -n "$systemgroups" ] && yum -c ${vdir}/etc/yum.conf --installroot=${vdir} -y groupinstall $systemgroups
91
92     # Create a copy of the system vserver w/o the vserver reference files and make it smaller. 
93     # This is a three step process:
94
95     # step 1: clean out yum cache to reduce space requirements
96     yum -c ${vdir}/etc/yum.conf --installroot=${vdir} -y clean all
97
98     # step 2: figure out the new/changed files in ${vdir} vs. ${vref} and compute ${vdir}.changes
99     rsync -anv ${vdir}/ ${vref}/ > ${vdir}.changes
100     linecount=$(wc -l ${vdir}.changes | awk ' { print $1 } ')
101     let headcount=$linecount-3
102     let tailcount=$headcount-1
103     # get rid of the last 3 lines of the rsync output
104     head -${headcount} ${vdir}.changes > ${vdir}.changes.1
105     # get rid of the first line of the rsync output
106     tail -${tailcount} ${vdir}.changes.1 > ${vdir}.changes.2
107     # post process rsync output to get rid of symbolic link embellish output
108     awk ' { print $1 } ' ${vdir}.changes.2 > ${vdir}.changes
109     rm -f ${vdir}.changes.*
110
111     # step 3: create the ${vdir} with just the list given in ${vdir}.changes 
112     install -d -m 755 ${vdir}-tmp/
113     rm -rf ${vdir}-tmp/*
114     (cd ${vdir} && cpio -m -d -u -p ${vdir}-tmp < ${vdir}.changes)
115     rm -rf ${vdir}
116     rm -f  ${vdir}.changes
117     mv ${vdir}-tmp ${vdir}
118     echo "--------DONE BUILDING system vserver ${NAME}: $(date)"
119 done
120
121 exit 0