when systemslices rpm gets updated, make sure the change makes it to the .vref image
[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$
16 # $URL$
17 #
18
19 PATH=/sbin:/bin:/usr/sbin:/usr/bin
20
21 # In both a normal CVS environment and a PlanetLab RPM
22 # build environment, all of our dependencies are checked out into
23 # directories at the same level as us.
24 if [ -d ../build ] ; then
25     PATH=$PATH:../build
26     srcdir=..
27 else
28     echo "Error: Could not find $(cd .. && pwd -P)/build/"
29     exit 1
30 fi
31
32 export PATH
33
34 # build.common comes from the build module
35 . build.common
36
37 pl_process_fedora_options $@
38 shiftcount=$?
39 shift $shiftcount
40
41 # pldistro expected as $1 
42 pldistro=$1 ; shift
43 # The vserver reference name - this comes from spec's slicefamily
44 slicefamily=$1; shift
45
46 # Do not tolerate errors
47 set -e
48
49 # Path's to the vserver references images and stubs
50 vrefdir=$PWD/vservers/.vref
51 vref=${vrefdir}/${slicefamily}
52 # stubs are created in a subdir per slicefamily
53 vstubdir=$PWD/vservers/.vstub/${slicefamily}
54
55 # Make /vservers and default vserver reference image
56 install -d -m 755 ${vref}
57 # needed for pldistros that don't have any system vserver images
58 install -d -m 755 ${vstubdir}
59
60 # Some of the PlanetLab RPMs attempt to (re)start themselves in %post,
61 # unless the installation is running inside the BootCD environment. We
62 # would like to pretend that we are.
63 export PL_BOOTCD=1
64
65 # Populate image with vserver-reference packages
66 pl_root_makedevs ${vref}
67 # locate the packages and groups file
68 pkgsfile=$(pl_locateDistroFile ../build/ ${pldistro} vserver.pkgs)
69 pl_root_mkfedora ${vref} ${pldistro} $pkgsfile
70 pl_root_tune_image ${vref}
71
72 systemvserver_count=$(ls ../build/config.${pldistro}/vserver-*.pkgs 2> /dev/null | wc -l)
73 [ $systemvserver_count -gt 0 ] && for systemvserver in $(ls ../build/config.${pldistro}/vserver-*.pkgs) ; do
74     NAME=$(basename $systemvserver .pkgs | sed -e s,vserver-,,)
75
76     echo "--------START BUILDING system vserver ${NAME}: $(date)"
77
78     # "Parse" out the packages and groups for yum
79     systempackages=$(pl_getPackages ${pl_DISTRO_NAME} $pldistro $systemvserver)
80     systemgroups=$(pl_getGroups ${pl_DISTRO_NAME} $pldistro $systemvserver)
81
82     vdir=${vstubdir}/${NAME}
83     rm -rf ${vdir}/*
84     install -d -m 755 ${vdir}
85
86     # Clone the base vserver reference to the system vserver reference
87     (cd ${vref} && find . | cpio -m -d -u -p ${vdir})
88     rm -f ${vdir}/var/lib/rpm/__db*
89
90     # Communicate to the initialization script from which vref this stub was cloned
91     echo ${slicefamily} > ${vdir}.cloned
92
93     # Install the system vserver specific packages
94     [ -n "$systempackages" ] && yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y install $systempackages
95     for group_plus in $systemgroups; do
96         group=$(echo $group_plus | sed -e "s,+++, ,g")
97         yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y groupinstall "$group"
98     done
99
100     # search e.g. vserver-planetflow.post in config.<pldistro> or in config.planetlab otherwise
101     postfile=$(pl_locateDistroFile ../build/ ${pldistro} vserver-${NAME}.post || : )
102
103     [ -f $postfile ] && /bin/bash $postfile ${vdir} || :
104
105     # Create a copy of the system vserver w/o the vserver reference files and make it smaller. 
106     # This is a three step process:
107
108     # step 1: clean out yum cache to reduce space requirements
109     yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y clean all
110
111     # step 2: figure out the new/changed files in ${vdir} vs. ${vref} and compute ${vdir}.changes
112     rsync -anv ${vdir}/ ${vref}/ > ${vdir}.changes
113     linecount=$(wc -l ${vdir}.changes | awk ' { print $1 } ')
114     let headcount=$linecount-3
115     let tailcount=$headcount-1
116     # get rid of the last 3 lines of the rsync output
117     head -${headcount} ${vdir}.changes > ${vdir}.changes.1
118     # get rid of the first line of the rsync output
119     tail -${tailcount} ${vdir}.changes.1 > ${vdir}.changes.2
120     # post process rsync output to get rid of symbolic link embellish output
121     awk ' { print $1 } ' ${vdir}.changes.2 > ${vdir}.changes
122     rm -f ${vdir}.changes.*
123
124     # step 3: create the ${vdir} with just the list given in ${vdir}.changes 
125     install -d -m 755 ${vdir}-tmp/
126     rm -rf ${vdir}-tmp/*
127     (cd ${vdir} && cpio -m -d -u -p ${vdir}-tmp < ${vdir}.changes)
128     rm -rf ${vdir}
129     rm -f  ${vdir}.changes
130     mv ${vdir}-tmp ${vdir}
131
132     # cleanup yum remainings
133     rm -rf ${vdir}/build ${vdir}/longbuildroot
134
135     echo "--------DONE BUILDING system vserver ${NAME}: $(date)"
136 done
137
138 # search vserver.post in config.<pldistro> or in config.planetlab otherwise
139 postfile=$(pl_locateDistroFile ../build/ ${pldistro} vserver.post)
140
141 [ -f $postfile ] && /bin/bash $postfile ${vref} || :
142
143 # fix sudoers config
144 [ -f ${vref}/etc/sudoers ] && echo -e "\nDefaults\tlogfile=/var/log/sudo\n" >> ${vref}/etc/sudoers
145
146 # cleanup yum remainings
147 rm -rf ${vref}/build ${vref}/longbuildroot
148
149 exit 0