replacing pldistro-fcdistro-*.lst with a pldistro-*.lst with variants embedded
[build.git] / build.common
1 # -*-Shell-script-*-
2 #
3 # Common functions for build scripts used by various packages
4 # incorporated (e.g., build, myplc, myplc-devel, vserver-reference)
5 #
6 # Marc E. Fiuczynski <mef@cs.princeton.edu>
7 # Copyright (C) 2007 The Trustees of Princeton University
8 #
9 # $Id$
10 #
11
12 function pl_getDefaultDistro() {
13     # FC4 is currently the default release
14     echo "Fedora"
15 }
16
17 function pl_getDefaultRelease() {
18     # FC4 is currently the default release
19     echo "4"
20 }
21
22 function pl_getDistro() {
23     defaultDistro=$(pl_getDefaultDistro)
24     if [ -f "/etc/redhat-release" ] ; then
25         distro=$(awk ' { print $1 } ' /etc/redhat-release)
26     else
27         distro=$defaultDistro
28     fi
29     echo "$distro"
30 }
31
32 function pl_getRelease() {
33     defaultRelease=$(pl_getDefaultRelease)
34     if [ -f "/etc/redhat-release" ] ; then
35         release=$(awk ' { if ($1=="Fedora" && $2=="Core") print $4 ; if (($1=="Fedora" && $2!="Core")||$1=="CentOS") print $3 } ' /etc/redhat-release)
36         [ $release -lt $defaultRelease ] && release=$defaultRelease
37     else
38         release=$defaultRelease
39     fi
40     echo "$release"
41 }
42
43 # figure out which redhat distro we are using (fedora, centos, redhat)
44 pl_DISTRO=$(pl_getDistro)
45
46 # select basearch of the host devel environment
47 pl_DISTRO_ARCH=$(uname -i)
48
49 # let mkfedora select one of its mirrors
50 pl_DISTRO_URL=""
51
52 pl_DISTRO_RELEASE=$(pl_getRelease)
53
54 # vserver expects something like fc4 or f7
55 # for fedora only as of now
56 case $pl_DISTRO in
57     [Ff]edora*)
58         if [ "$pl_DISTRO_RELEASE" -le 6 ] ; then
59             pl_DISTRO_NAME=fc$pl_DISTRO_RELEASE
60         else
61             pl_DISTRO_NAME=f$pl_DISTRO_RELEASE
62         fi ;;
63     [Cc]entOS*)
64         if [ "$pl_DISTRO_RELEASE" = "4.5" ] ; then
65             # centos 4.5 is just centos4 + enhancements
66             pl_DISTRO_NAME=centos4
67         else
68             pl_DISTRO_NAME=centos$pl_DISTRO_RELEASE
69         fi ;;
70     *)
71         echo "build.common: WARNING - pl_DISTRO_NAME not set for distro=$pl_DISTRO" ;;
72 esac
73
74 # get patch to appropriate yumgroups.xml file
75 # XXX This path should be relative to PLDISTRO, as defined in
76 # build/Makefile
77 pl_YUMGROUPSXML="../build/groups/v4_yumgroups.xml"
78 # Thierry: quick & dirty improvement 
79 # this is updated by the toplevel build, from the appropriate pldistro-dep file
80 # use another var for now for safety
81 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
82
83 function pl_process_fedora_options () {
84     # Get options
85     shiftcount=0
86     while getopts "l:r:a:h" opt ; do
87         case $opt in
88             l)
89                 pl_DISTRO_URL=$OPTARG
90                 let shiftcount=$shiftcount+2
91                 ;;
92             r)
93                 pl_DISTRO_RELEASE=$OPTARG
94                 let shiftcount=$shiftcount+2
95                 ;;
96             a)
97                 pl_DISTRO_ARCH=$OPTARG
98                 let shiftcount=$shiftcount+2
99                 ;;
100             h|*)
101                 echo "Usage: $0 [OPTION]..."
102                 echo "  -l url          distro mirror location (default: $pl_DISTRO_URL)"
103                 echo "  -r release      distro release number (default: $pl_DISTRO_RELEASE)"
104                 echo "  -a arch         distro architecture (default: $pl_DISTRO_ARCH)"
105                 echo "where distro can be either fedora, centos, or redhat"
106                 echo "  -h              This message"
107                 exit 1
108                 ;;
109         esac
110     done
111     return $shiftcount
112 }
113
114 function pl_makedevs() {
115     vroot=$1
116     # Clean ${vroot}/dev, but only when ${vroot}!=""
117     [ -n $vroot ] && rm -rf $vroot/dev
118     
119     mkdir -p $vroot/dev
120     mknod -m 666 $vroot/dev/null c 1 3
121     mknod -m 666 $vroot/dev/zero c 1 5
122     mknod -m 666 $vroot/dev/full c 1 7
123     mknod -m 644 $vroot/dev/random c 1 8
124     mknod -m 644 $vroot/dev/urandom c 1 9
125     mknod -m 666 $vroot/dev/tty c 5 0
126     mknod -m 666 $vroot/dev/ptmx c 5 2
127     # For bash command substitution
128     ln -nsf ../proc/self/fd $vroot/dev/fd
129
130     # For df and linuxconf
131     touch $vroot/dev/hdv1
132
133     # For pseudo ttys
134     mkdir -p $vroot/dev/pts
135
136     # (Might have to remove the following for vserver-reference.)
137
138     # for tmpfs mount
139     mkdir -p $vroot/dev/shm
140
141     # For TUN/TAP
142     mkdir -p $vroot/dev/net
143     mknod -m 600 $vroot/dev/net/tun c 10 200
144
145     # For mkinitrd (in case a kernel is being installed)
146     # As well for loop back mounting within a vserver. 
147     for i in $(seq 0 255) ; do
148         mknod -m 640 $vroot/dev/loop$i b 7 $i
149     done
150 }
151
152 function pl_mkfedora() {
153     root=$1
154     shift
155     options=$@
156
157     pl_makedevs $root
158
159     [ -n "$pl_DISTRO_URL" ] && options="$options -l $pl_DISTRO_URL"
160     [ -n "$pl_DISTRO_ARCH" ] && options="$options -a $pl_DISTRO_ARCH"
161     [ -n "$pl_DISTRO_RELEASE" ] && options="$options -r $pl_DISTRO_RELEASE"
162     # echo "mkfedora -v $options $root"
163     eval mkfedora -v $options $root
164 }
165
166 function pl_setup_chroot() {
167     root=$1
168     shift
169     options=$@
170
171     pl_mkfedora $root $options
172
173     # Disable all services in reference image
174     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
175
176     # FC2 minilogd starts up during shutdown and makes unmounting
177     # impossible. Just get rid of it.
178     rm -f $root/sbin/minilogd
179     ln -nsf /bin/true $root/sbin/minilogd
180
181     # This tells the Boot Manager that it is okay to update
182     # /etc/resolv.conf and /etc/hosts whenever the network configuration
183     # changes. Users are free to delete this file.
184     touch $vroot/etc/AUTO_UPDATE_NET_FILES
185 }
186
187 # Move specified directories out of a src tree into a dst tree, and
188 # then for each moved directory create a symlink in src to dst.
189 function pl_move_dirs() {
190     root=$1
191     data=$2
192     store=$3
193     shift 3
194
195     mkdir -p $root/data
196     for datadir in "$@" ; do
197         mkdir -p ${data}${datadir}
198         if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
199             (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
200         fi
201         rm -rf ${root}/${datadir}
202         mkdir -p $(dirname ${root}/${datadir})
203         ln -nsf /${store}/${datadir} ${root}/${datadir}
204     done
205 }
206
207 # Construct an image file from given some directory
208 # XXX in the future maybe use livecdtools?
209 function pl_make_image() {
210     root=$1
211     image=$2
212     extraspace=$3
213
214     # Leave about 100 MB free space and allow for about 20% inode overhead
215     bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
216     bs=4096
217     blocks=$(($bytes / $bs))
218     dd bs=$bs count=$blocks if=/dev/zero of=$image
219     mkfs.ext3 -b $bs -j -F $image
220
221     # Temporarily mount it
222     tmp=$(mktemp -d tmp.XXXXXX)
223     mount -o loop $image $tmp
224     trap "umount $tmp; rmdir $tmp" ERR INT
225
226     # Move files to it
227     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
228
229     # Unmount it
230     umount -l $tmp
231     rmdir $tmp
232     trap - ERR INT
233 }
234
235 # Fix permissions on tmp directories
236 function pl_fixtmp_permissions() {
237     root=$1
238     chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
239 }
240
241 function pl_fixdirs() {
242     root=$1
243     datadirs=$2
244     for datadir in datadirs ; do
245         if [ -h ${root}/${datadir} ] ; then
246             rm -f ${root}/${datadir}
247             mkdir -p ${root}/${datadir}
248         fi
249     done
250 }
251
252 ########## .lst format
253 # comments start with a # - this is needed only if you use a keyword in a comment
254 # lst_parse keyword fcdistro lst1 .. lstn
255 # for a given keyword like 'package' :
256 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
257 # you can e.g. use
258 # package: pkg1 .. pkgn 
259 # package+f8: pkg1 .. pkgn
260 # package-f8: pkg1 .. pkgn
261
262 function lst_parse () {
263
264     keyword=$1;shift
265     fcdistro=$1; shift
266
267     # grab regular descriptions
268     all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
269     # grab additions
270     add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}\+${fcdistro}:,,")
271     # grab exclusions
272     sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}\-${fcdistro}:" | sed -e "s,${keyword}\-${fcdistro}:,,")
273
274     for i in $all $add; do
275         for exclude in $sub; do
276             [ "$i" = "$exclude" ] && continue 2
277         done
278         echo "$i "
279     done
280     return 0
281 }
282
283 function pl_getPackages2() { fcdistro=$1; shift ; lst_parse package $fcdistro "$@" }
284 function pl_getGroups2() { fcdistro=$1; shift ; lst_parse group $fcdistro "$@" }
285
286 function pl_getPackages() {
287     filename=$1
288     packages=$(grep "^package:" $filename | sed -e s,package:,,)
289     echo "$packages"
290     return 0
291 }
292
293 # add -p before each package for mkfedora
294 function pl_getPackagesOptions() {
295     pl_getPackages "$@" | awk '{for (i=1;i<=NF;i++) {print "-p " $i}}'
296 }
297
298 function pl_getGroups() {
299     filename=$1
300     groups=$(grep "^group:" $filename | sed -e s,group:,,)
301     echo "$groups"
302     return 0
303 }
304
305 # add -g before each group for mkfedora
306 function pl_getGroupsOptions() {
307     pl_getGroups "$@" | awk '{for (i=1;i<=NF;i++) {print "-g " $i}}'
308 }
309