comment out requiretty from /etc/sudoers.
[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 
41 pldistro=$1 ; shift
42 # The vserver reference name - this comes from spec's slicefamily
43 slicefamily=$1; shift
44
45 # Do not tolerate errors
46 set -e
47
48 # Path's to the vserver references images and stubs
49 vrefdir=$PWD/vservers/.vref
50 vref=${vrefdir}/${slicefamily}
51 # stubs are created in a subdir per slicefamily
52 vstubdir=$PWD/vservers/.vstub/${slicefamily}
53
54 # Make /vservers and default vserver reference image
55 install -d -m 755 ${vref}
56 # needed for pldistros that don't have any system vserver images
57 install -d -m 755 ${vstubdir}
58
59 # Some of the PlanetLab RPMs attempt to (re)start themselves in %post,
60 # unless the installation is running inside the BootCD environment. We
61 # would like to pretend that we are.
62 export PL_BOOTCD=1
63
64 # Populate image with vserver-reference packages
65 pl_root_makedevs ${vref}
66 # locate the packages and groups file
67 pkgsfile=$(pl_locateDistroFile ../build/ ${pldistro} vserver.pkgs)
68 pl_root_mkfedora ${vref} ${pldistro} $pkgsfile
69 pl_root_tune_image ${vref}
70
71 systemvserver_count=$(ls ../build/config.${pldistro}/vserver-*.pkgs 2> /dev/null | wc -l)
72 [ $systemvserver_count -gt 0 ] && for systemvserver in $(ls ../build/config.${pldistro}/vserver-*.pkgs) ; do
73     NAME=$(basename $systemvserver .pkgs | sed -e s,vserver-,,)
74
75     echo "--------START BUILDING system vserver ${NAME}: $(date)"
76
77     # "Parse" out the packages and groups for yum
78     systempackages=$(pl_getPackages ${pl_DISTRO_NAME} $pldistro $systemvserver)
79     systemgroups=$(pl_getGroups ${pl_DISTRO_NAME} $pldistro $systemvserver)
80
81     vdir=${vstubdir}/${NAME}
82     rm -rf ${vdir}/*
83     install -d -m 755 ${vdir}
84
85     # Clone the base vserver reference to the system vserver reference
86     (cd ${vref} && find . | cpio -m -d -u -p ${vdir})
87     rm -f ${vdir}/var/lib/rpm/__db*
88
89     # Communicate to the initialization script from which vref this stub was cloned
90     echo ${slicefamily} > ${vdir}.cloned
91
92     # Install the system vserver specific packages
93     [ -n "$systempackages" ] && yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y install $systempackages
94     for group_plus in $systemgroups; do
95         group=$(echo $group_plus | sed -e "s,+++, ,g")
96         yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y groupinstall "$group"
97     done
98
99     pkgsdir=$(dirname $pkgsfile)
100     pkgsname=$(basename $pkgsfile .pkgs)
101     postfile="${pkgsdir}/${pkgsname}.post"
102     [ -f $postfile ] && /bin/bash $postfile ${vdir} || :
103
104     # Create a copy of the system vserver w/o the vserver reference files and make it smaller. 
105     # This is a three step process:
106
107     # step 1: clean out yum cache to reduce space requirements
108     yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y clean all
109
110     # step 2: figure out the new/changed files in ${vdir} vs. ${vref} and compute ${vdir}.changes
111     rsync -anv ${vdir}/ ${vref}/ > ${vdir}.changes
112     linecount=$(wc -l ${vdir}.changes | awk ' { print $1 } ')
113     let headcount=$linecount-3
114     let tailcount=$headcount-1
115     # get rid of the last 3 lines of the rsync output
116     head -${headcount} ${vdir}.changes > ${vdir}.changes.1
117     # get rid of the first line of the rsync output
118     tail -${tailcount} ${vdir}.changes.1 > ${vdir}.changes.2
119     # post process rsync output to get rid of symbolic link embellish output
120     awk ' { print $1 } ' ${vdir}.changes.2 > ${vdir}.changes
121     rm -f ${vdir}.changes.*
122
123     # step 3: create the ${vdir} with just the list given in ${vdir}.changes 
124     install -d -m 755 ${vdir}-tmp/
125     rm -rf ${vdir}-tmp/*
126     (cd ${vdir} && cpio -m -d -u -p ${vdir}-tmp < ${vdir}.changes)
127     rm -rf ${vdir}
128     rm -f  ${vdir}.changes
129     mv ${vdir}-tmp ${vdir}
130     echo "--------DONE BUILDING system vserver ${NAME}: $(date)"
131 done
132
133 pkgsdir=$(dirname $pkgsfile)
134 pkgsname=$(basename $pkgsfile .pkgs)
135 postfile="${pkgsdir}/${pkgsname}.post"
136 [ -f $postfile ] && /bin/bash $postfile ${vref} || :
137
138 # fix sudoers config
139 [ -f ${vref}/etc/sudoers ] && echo -e "\nDefaults\tlogfile=/var/log/sudo\n" >> ${vref}/etc/sudoers
140 [ -f ${vref}/etc/sudoers ] && sed -i 's,^Defaults.*requiretty,#Defaults requiretty,' ${vref}/etc/sudoers
141
142 exit 0