3 # Common functions for build scripts used by various packages
4 # incorporated (e.g., build, myplc, myplc-devel, vserver-reference)
6 # Marc E. Fiuczynski <mef@cs.princeton.edu>
7 # Copyright (C) 2007 The Trustees of Princeton University
12 function pl_getDefaultDistro() {
13 # FC4 is currently the default release
17 function pl_getDefaultRelease() {
18 # FC4 is currently the default release
22 function pl_getDistro() {
23 defaultDistro=$(pl_getDefaultDistro)
24 if [ -f "/etc/redhat-release" ] ; then
25 distro=$(awk ' { print $1 } ' /etc/redhat-release)
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
38 release=$defaultRelease
43 # figure out which redhat distro we are using (fedora, centos, redhat)
44 pl_DISTRO=$(pl_getDistro)
46 # select basearch of the host devel environment
47 pl_DISTRO_ARCH=$(uname -i)
49 # let mkfedora select one of its mirrors
52 pl_DISTRO_RELEASE=$(pl_getRelease)
54 # vserver expects something like fc4 or f7
55 # for fedora only as of now
58 if [ "$pl_DISTRO_RELEASE" -le 6 ] ; then
59 pl_DISTRO_NAME=fc$pl_DISTRO_RELEASE
61 pl_DISTRO_NAME=f$pl_DISTRO_RELEASE
64 if [ "$pl_DISTRO_RELEASE" = "4.5" ] ; then
65 # centos 4.5 is just centos4 + enhancements
66 pl_DISTRO_NAME=centos4
68 pl_DISTRO_NAME=centos$pl_DISTRO_RELEASE
71 echo "build.common: WARNING - pl_DISTRO_NAME not set for distro=$pl_DISTRO" ;;
74 # get patch to appropriate yumgroups.xml file
75 # Thierry: quick & dirty improvement
76 # this file is updated by the toplevel build, from groups/pldistro>.xml
77 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
79 function pl_process_fedora_options () {
82 while getopts "l:r:a:h" opt ; do
86 let shiftcount=$shiftcount+2
89 pl_DISTRO_RELEASE=$OPTARG
90 let shiftcount=$shiftcount+2
93 pl_DISTRO_ARCH=$OPTARG
94 let shiftcount=$shiftcount+2
97 echo "Usage: $0 [OPTION]..."
98 echo " -l url distro mirror location (default: $pl_DISTRO_URL)"
99 echo " -r release distro release number (default: $pl_DISTRO_RELEASE)"
100 echo " -a arch distro architecture (default: $pl_DISTRO_ARCH)"
101 echo "where distro can be either fedora, centos, or redhat"
102 echo " -h This message"
110 function pl_makedevs() {
112 # Clean ${vroot}/dev, but only when ${vroot}!=""
113 [ -n $vroot ] && rm -rf $vroot/dev
116 mknod -m 666 $vroot/dev/null c 1 3
117 mknod -m 666 $vroot/dev/zero c 1 5
118 mknod -m 666 $vroot/dev/full c 1 7
119 mknod -m 644 $vroot/dev/random c 1 8
120 mknod -m 644 $vroot/dev/urandom c 1 9
121 mknod -m 666 $vroot/dev/tty c 5 0
122 mknod -m 666 $vroot/dev/ptmx c 5 2
123 # For bash command substitution
124 ln -nsf ../proc/self/fd $vroot/dev/fd
126 # For df and linuxconf
127 touch $vroot/dev/hdv1
130 mkdir -p $vroot/dev/pts
132 # (Might have to remove the following for vserver-reference.)
135 mkdir -p $vroot/dev/shm
138 mkdir -p $vroot/dev/net
139 mknod -m 600 $vroot/dev/net/tun c 10 200
141 # For mkinitrd (in case a kernel is being installed)
142 # As well for loop back mounting within a vserver.
143 for i in $(seq 0 255) ; do
144 mknod -m 640 $vroot/dev/loop$i b 7 $i
148 function pl_mkfedora() {
155 [ -n "$pl_DISTRO_URL" ] && options="$options -l $pl_DISTRO_URL"
156 [ -n "$pl_DISTRO_ARCH" ] && options="$options -a $pl_DISTRO_ARCH"
157 [ -n "$pl_DISTRO_RELEASE" ] && options="$options -r $pl_DISTRO_RELEASE"
158 # echo "mkfedora -v $options $root"
159 eval mkfedora -v $options $root
162 function pl_setup_chroot() {
167 pl_mkfedora $root $options
169 # Disable all services in reference image
170 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
172 # FC2 minilogd starts up during shutdown and makes unmounting
173 # impossible. Just get rid of it.
174 rm -f $root/sbin/minilogd
175 ln -nsf /bin/true $root/sbin/minilogd
177 # This tells the Boot Manager that it is okay to update
178 # /etc/resolv.conf and /etc/hosts whenever the network configuration
179 # changes. Users are free to delete this file.
180 touch $vroot/etc/AUTO_UPDATE_NET_FILES
183 # Move specified directories out of a src tree into a dst tree, and
184 # then for each moved directory create a symlink in src to dst.
185 function pl_move_dirs() {
192 for datadir in "$@" ; do
193 mkdir -p ${data}${datadir}
194 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
195 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
197 rm -rf ${root}/${datadir}
198 mkdir -p $(dirname ${root}/${datadir})
199 ln -nsf /${store}/${datadir} ${root}/${datadir}
203 # Construct an image file from given some directory
204 # XXX in the future maybe use livecdtools?
205 function pl_make_image() {
210 # Leave about 100 MB free space and allow for about 20% inode overhead
211 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
213 blocks=$(($bytes / $bs))
214 dd bs=$bs count=$blocks if=/dev/zero of=$image
215 mkfs.ext3 -b $bs -j -F $image
217 # Temporarily mount it
218 tmp=$(mktemp -d tmp.XXXXXX)
219 mount -o loop $image $tmp
220 trap "umount $tmp; rmdir $tmp" ERR INT
223 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
231 # Fix permissions on tmp directories
232 function pl_fixtmp_permissions() {
234 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
237 function pl_fixdirs() {
240 for datadir in datadirs ; do
241 if [ -h ${root}/${datadir} ] ; then
242 rm -f ${root}/${datadir}
243 mkdir -p ${root}/${datadir}
248 ########## .lst format
249 # comments start with a # - this is needed only if you use a keyword in a comment
250 # lst_parse keyword fcdistro lst1 .. lstn
251 # for a given keyword like 'package' :
252 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
254 # package: pkg1 .. pkgn
255 # package+f8: pkg1 .. pkgn
256 # package-f8: pkg1 .. pkgn
258 function lst_parse () {
263 # grab regular descriptions
264 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
266 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}\+${fcdistro}:,,")
268 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}\-${fcdistro}:" | sed -e "s,${keyword}\-${fcdistro}:,,")
270 for i in $all $add; do
271 for exclude in $sub; do
272 [ "$i" = "$exclude" ] && continue 2
279 function pl_getPackages2() { fcdistro=$1; shift ; lst_parse package $fcdistro "$@" }
280 function pl_getGroups2() { fcdistro=$1; shift ; lst_parse group $fcdistro "$@" }
282 function pl_getPackages() {
284 packages=$(grep "^package:" $filename | sed -e s,package:,,)
289 # add -p before each package for mkfedora
290 function pl_getPackagesOptions() {
291 pl_getPackages "$@" | awk '{for (i=1;i<=NF;i++) {print "-p " $i}}'
294 function pl_getGroups() {
296 groups=$(grep "^group:" $filename | sed -e s,group:,,)
301 # add -g before each group for mkfedora
302 function pl_getGroupsOptions() {
303 pl_getGroups "$@" | awk '{for (i=1;i<=NF;i++) {print "-g " $i}}'