less confusing output in specfile when using unknown distro
[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         pl_DISTRO_NAME="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
72         echo 1>&2 "build.common: WARNING - pl_DISTRO_NAME not set for distro=$pl_DISTRO" 
73         ;;
74 esac
75
76 # get patch to appropriate yumgroups.xml file
77 # Thierry: quick & dirty improvement 
78 # this file is updated by the toplevel build, from groups/pldistro>.xml
79 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
80
81 function pl_process_fedora_options () {
82     # Get options
83     shiftcount=0
84     while getopts "l:r:a:h" opt ; do
85         case $opt in
86             l)
87                 pl_DISTRO_URL=$OPTARG
88                 let shiftcount=$shiftcount+2
89                 ;;
90             r)
91                 pl_DISTRO_RELEASE=$OPTARG
92                 let shiftcount=$shiftcount+2
93                 ;;
94             a)
95                 pl_DISTRO_ARCH=$OPTARG
96                 let shiftcount=$shiftcount+2
97                 ;;
98             h|*)
99                 echo "Usage: $0 [OPTION]..."
100                 echo "  -l url          distro mirror location (default: $pl_DISTRO_URL)"
101                 echo "  -r release      distro release number (default: $pl_DISTRO_RELEASE)"
102                 echo "  -a arch         distro architecture (default: $pl_DISTRO_ARCH)"
103                 echo "where distro can be either fedora, centos, or redhat"
104                 echo "  -h              This message"
105                 exit 1
106                 ;;
107         esac
108     done
109     return $shiftcount
110 }
111
112 function pl_makedevs() {
113     vroot=$1
114     # Clean ${vroot}/dev, but only when ${vroot}!=""
115     [ -n $vroot ] && rm -rf $vroot/dev
116     
117     mkdir -p $vroot/dev
118     mknod -m 666 $vroot/dev/null c 1 3
119     mknod -m 666 $vroot/dev/zero c 1 5
120     mknod -m 666 $vroot/dev/full c 1 7
121     mknod -m 644 $vroot/dev/random c 1 8
122     mknod -m 644 $vroot/dev/urandom c 1 9
123     mknod -m 666 $vroot/dev/tty c 5 0
124     mknod -m 666 $vroot/dev/ptmx c 5 2
125     # For bash command substitution
126     ln -nsf ../proc/self/fd $vroot/dev/fd
127
128     # For df and linuxconf
129     touch $vroot/dev/hdv1
130
131     # For pseudo ttys
132     mkdir -p $vroot/dev/pts
133
134     # (Might have to remove the following for vserver-reference.)
135
136     # for tmpfs mount
137     mkdir -p $vroot/dev/shm
138
139     # For TUN/TAP
140     mkdir -p $vroot/dev/net
141     mknod -m 600 $vroot/dev/net/tun c 10 200
142
143     # For mkinitrd (in case a kernel is being installed)
144     # As well for loop back mounting within a vserver. 
145     for i in $(seq 0 255) ; do
146         mknod -m 640 $vroot/dev/loop$i b 7 $i
147     done
148 }
149
150 function pl_mkfedora() {
151     root=$1
152     shift
153     options=$@
154
155     pl_makedevs $root
156
157     [ -n "$pl_DISTRO_URL" ] && options="$options -l $pl_DISTRO_URL"
158     [ -n "$pl_DISTRO_ARCH" ] && options="$options -a $pl_DISTRO_ARCH"
159     [ -n "$pl_DISTRO_RELEASE" ] && options="$options -r $pl_DISTRO_RELEASE"
160     # echo "mkfedora -v $options $root"
161     eval mkfedora -v $options $root
162 }
163
164 function pl_setup_chroot() {
165     root=$1
166     shift
167     options=$@
168
169     pl_mkfedora $root $options
170
171     # Disable all services in reference image
172     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
173
174     # FC2 minilogd starts up during shutdown and makes unmounting
175     # impossible. Just get rid of it.
176     rm -f $root/sbin/minilogd
177     ln -nsf /bin/true $root/sbin/minilogd
178
179     # This tells the Boot Manager that it is okay to update
180     # /etc/resolv.conf and /etc/hosts whenever the network configuration
181     # changes. Users are free to delete this file.
182     touch $vroot/etc/AUTO_UPDATE_NET_FILES
183 }
184
185 # Move specified directories out of a src tree into a dst tree, and
186 # then for each moved directory create a symlink in src to dst.
187 function pl_move_dirs() {
188     root=$1
189     data=$2
190     store=$3
191     shift 3
192
193     mkdir -p $root/data
194     for datadir in "$@" ; do
195         mkdir -p ${data}${datadir}
196         if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
197             (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
198         fi
199         rm -rf ${root}/${datadir}
200         mkdir -p $(dirname ${root}/${datadir})
201         ln -nsf /${store}/${datadir} ${root}/${datadir}
202     done
203 }
204
205 # Construct an image file from given some directory
206 # XXX in the future maybe use livecdtools?
207 function pl_make_image() {
208     root=$1
209     image=$2
210     extraspace=$3
211
212     # Leave about 100 MB free space and allow for about 20% inode overhead
213     bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
214     bs=4096
215     blocks=$(($bytes / $bs))
216     dd bs=$bs count=$blocks if=/dev/zero of=$image
217     mkfs.ext3 -b $bs -j -F $image
218
219     # Temporarily mount it
220     tmp=$(mktemp -d tmp.XXXXXX)
221     mount -o loop $image $tmp
222     trap "umount $tmp; rmdir $tmp" ERR INT
223
224     # Move files to it
225     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
226
227     # Unmount it
228     umount -l $tmp
229     rmdir $tmp
230     trap - ERR INT
231 }
232
233 # Fix permissions on tmp directories
234 function pl_fixtmp_permissions() {
235     root=$1
236     chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
237 }
238
239 function pl_fixdirs() {
240     root=$1
241     datadirs=$2
242     for datadir in datadirs ; do
243         if [ -h ${root}/${datadir} ] ; then
244             rm -f ${root}/${datadir}
245             mkdir -p ${root}/${datadir}
246         fi
247     done
248 }
249
250 ########## .lst format
251 # comments start with a # - this is needed only if you use a keyword in a comment
252 # lst_parse keyword fcdistro lst1 .. lstn
253 # for a given keyword like 'package' :
254 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
255 # you can e.g. use
256 # package: pkg1 .. pkgn 
257 # package+f8: pkg1 .. pkgn
258 # package-f8: pkg1 .. pkgn
259
260 function lst_parse () {
261
262     keyword=$1;shift
263     fcdistro=$1; shift
264
265     # grab regular descriptions
266     all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
267     # grab additions
268     add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}\+${fcdistro}:,,")
269     # grab exclusions
270     sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}\-${fcdistro}:" | sed -e "s,${keyword}\-${fcdistro}:,,")
271
272     for i in $all $add; do
273         for exclude in $sub; do
274             [ "$i" = "$exclude" ] && continue 2
275         done
276         echo "$i "
277     done
278     return 0
279 }
280
281 function pl_getPackages2() { fcdistro=$1; shift ; lst_parse package $fcdistro "$@" ; }
282 function pl_getGroups2() { fcdistro=$1; shift ; lst_parse group $fcdistro "$@" ; }
283 # add -p before each package for mkfedora
284 function pl_getPackagesOptions2 () { pl_getPackages2 "$@" | awk '{for (i=1;i<=NF;i++) {print "-p " $i}}' ; }
285 # add -g before each group for mkfedora
286 function pl_getGroupsOptions2 () { pl_getGroups2 "$@" | awk '{for (i=1;i<=NF;i++) {print "-g " $i}}' ; }