and again
[bootstrapfs.git] / build.sh
1 #!/bin/bash
2 #
3 # Build tgz root image for PlanetLab nodes.
4 #
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Marc E. Fiuczynski <mef@cs.princeton.edu>
7 # Copyright (C) 2005-2007 The Trustees of Princeton University
8 #
9
10 #
11 # This will build the bootstrafs-*.tar.bz2 images, which comprises
12 # the base root image on the node, then create the ${NAME} bootstrap image
13 # which is made up of just the additional files needed for a ${NAME} nodegroup 
14 # node.
15 #
16 # It is expected to be in sync with the getNodeFlavour PLCAPI method
17
18 PATH=/sbin:/bin:/usr/sbin:/usr/bin
19
20 # in the PlanetLab build environment, our dependencies are checked out 
21 # into directories at the same level as us.
22 if [ -d ../build ] ; then
23     PATH=$PATH:../build
24     srcdir=../
25 else
26     echo "Error: Could not find ../build in $(pwd)"
27     exit 1
28 fi
29
30 export PATH
31
32 . build.common
33
34 pl_process_fedora_options $@
35 shiftcount=$?
36 shift $shiftcount
37
38 # expecting fcdistro and pldistro on the command line
39 pldistro=$1; shift
40 fcdistro=${pl_DISTRO_NAME}
41 arch=${pl_DISTRO_ARCH}
42 nodefamily=${pldistro}-${fcdistro}-${arch}
43 extensionfamily=${fcdistro}-${arch}
44
45 # Do not tolerate errors
46 set -e
47
48 # Some of the PlanetLab RPMs attempt to (re)start themselves in %post,
49 # unless the installation is running inside the BootCD environment. We
50 # would like to pretend that we are.
51 export PL_BOOTCD=1
52
53 # Populate a minimal /dev and then the files for the base nodeimage content
54 vref=${PWD}/base
55 install -d -m 755 ${vref}
56 pl_root_makedevs $vref
57
58 pkgsfile=$(pl_locateDistroFile ../build/ ${pldistro} nodeimage.pkgs)
59 echo "* Building nodeimage for ${pldistro}: $(date)"
60 # -k = exclude kernel* packages
61 pl_root_mkfedora ${vref} ${pldistro} $pkgsfile
62
63 # optionally invoke a post processing script after packages from
64 # $pkgsfile have been installed
65 postfile=$(pl_locateDistroFile ../build/ ${pldistro} nodeimage.post || : )
66 [ -f $postfile ] && { echo "Running post install file $postfile" ; /bin/bash $postfile ${vref} || : ; }
67
68 displayed=""
69
70 # for distros that do define node extensions
71 pkgs_count=$(ls ../build/config.${pldistro}/nodeimage-*.pkgs 2> /dev/null | wc -l)
72 [ $pkgs_count -gt 0 ] && for pkgs in $(ls ../build/config.${pldistro}/nodeimage-*.pkgs); do
73     NAME=$(basename $pkgs .pkgs | sed -e s,nodeimage-,,)
74
75     [ -z "$displayed" ] && echo "* Handling ${pldistro} nodeimage extensions"
76     displayed=true
77
78     extension_plain=nodeimage-${NAME}-${extensionfamily}.tar
79     extension_name=nodeimage-${NAME}-${extensionfamily}.tar.bz2
80
81     echo "* Start Building $extension_name: $(date)"
82
83     # "Parse" out the packages and groups for yum
84     packages=$(pl_getPackages $fcdistro $pldistro $pkgs)
85     groups=$(pl_getGroups $fcdistro $pldistro $pkgs)
86     echo "${NAME} has the following packages : ${packages}"
87     echo "${NAME} has the following groups : ${groups}"
88
89     vdir=${PWD}/${pldistro}-filesystems/${NAME}
90     rm -rf ${vdir}/*
91     install -d -m 755 ${vdir}
92
93     # Clone the base reference to the bootstrap fs
94     (cd ${vref} && find . | cpio -m -d -u -p ${vdir})
95     rm -f ${vdir}/var/lib/rpm/__db*
96
97     # Install the system vserver specific packages
98     [ -n "$packages" ] && yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y install $packages
99     for group_plus in $groups; do
100         group=$(echo $group_plus | sed -e "s,+++, ,g")
101         yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y groupinstall "$group"
102     done
103
104
105     if [ -f "${vdir}/proc/cpuinfo" ] ; then
106         echo "WARNING: some RPM appears to have mounted /proc in ${NAME}. Unmounting it!"
107         umount ${vdir}/proc
108     fi
109
110     # optionally invoke a post processing script after packages from
111     # $pkgs have been installed
112     postfile=$(pl_locateDistroFile ../build/ ${pldistro} nodeimage-${NAME}.post || : )
113     [ -f $postfile ] && { echo "Running post install file $postfile" ; /bin/bash $postfile ${vdir} || : ; }
114
115     # Create a copy of the ${NAME} bootstrap filesystem w/o the base
116     # bootstrap filesystem and make it smaller.  This is a three step
117     # process:
118
119     # step 1: clean out yum cache to reduce space requirements
120     yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y clean all
121
122     # step 2: figure out the new/changed files in ${vdir} vs. ${vref} and compute ${vdir}.changes
123     rsync -anv ${vdir}/ ${vref}/ > ${vdir}.changes
124     linecount=$(wc -l ${vdir}.changes | awk ' { print $1 } ')
125     let headcount=$linecount-3
126     let tailcount=$headcount-1
127     # get rid of the last 3 lines of the rsync output
128     head -${headcount} ${vdir}.changes > ${vdir}.changes.1
129     # get rid of the first line of the rsync output
130     tail -${tailcount} ${vdir}.changes.1 > ${vdir}.changes.2
131     # post process rsync output to get rid of symbolic link embellish output
132     awk ' { print $1 } ' ${vdir}.changes.2 > ${vdir}.changes
133     rm -f ${vdir}.changes.*
134
135     # step 3: create the ${vdir} with just the list given in ${vdir}.changes 
136     install -d -m 755 ${vdir}-tmp/
137     rm -rf ${vdir}-tmp/*
138     (cd ${vdir} && cpio -m -d -u -p ${vdir}-tmp < ${vdir}.changes)
139     rm -rf ${vdir}
140     rm -f  ${vdir}.changes
141     mv ${vdir}-tmp ${vdir}
142     
143     echo -n "* tar $extension_name s=$(date +%H-%M-%S)"
144     tar -cpf ${pldistro}-filesystems/$extension_plain -C ${vdir} .
145     echo -n " m=$(date +%H-%M-%S) "
146     bzip2 --compress --stdout ${pldistro}-filesystems/$extension_plain > ${pldistro}-filesystems/$extension_name
147     echo " e=$(date +%H-%M-%S) "
148 done
149
150 # Build the base Bootstrap filesystem
151 # clean out yum cache to reduce space requirements
152 yum -c ${vref}/etc/mkfedora-yum.conf --installroot=${vref} -y clean all
153 # create stamp on node image to help out node upgrades in the future
154 mkdir -p ${vref}/etc/planetlab
155 echo ${nodefamily} > ${vref}/etc/planetlab/nodefamily
156
157 bootstrapfs_plain=bootstrapfs-${nodefamily}.tar
158 bootstrapfs_name=bootstrapfs-${nodefamily}.tar.bz2
159 echo -n "* tar $bootstrapfs_name s=$(date +%H-%M-%S)"
160 tar -cpf $bootstrapfs_plain -C ${vref} .
161 echo -n " m=$(date +%H-%M-%S) "
162 bzip2 --compress --stdout $bootstrapfs_plain > $bootstrapfs_name
163 echo " e=$(date +%H-%M-%S) "
164
165 exit 0