6 # Common functions for build scripts used by various packages
7 # incorporated (e.g., build, myplc, myplc-devel, vserver-reference)
9 # Marc E. Fiuczynski <mef@cs.princeton.edu>
10 # Copyright (C) 2007 The Trustees of Princeton University
12 # supported distros f8, f10, f12, centos5
14 # returns 'Fedora' or 'CentOS' for now
15 function pl_getDistro() {
16 if [ -f "/etc/redhat-release" ] ; then
17 distro=$(awk ' { print $1 } ' /etc/redhat-release)
19 echo "build.common.pl_getDistro-unknown"
26 # returns something like 8, 10, or 5.3
27 function pl_getRelease() {
28 if [ -f "/etc/redhat-release" ] ; then
29 release=$(awk ' { if ($1=="Fedora" && $2=="Core") print $4 ; if (($1=="Fedora" && $2!="Core")||$1=="CentOS") print $3 } ' /etc/redhat-release)
31 echo "build.common.pl_getRelease-unknown"
38 # returns stuff for vserver, i.e. something like 'f8' or 'centos5'
39 function pl_getReleaseName () {
54 releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
55 echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro"
63 # on fedora 8 or above, we use libnl from the stock repos
64 # on centos5 we build it locally
65 function pl_getKexcludes () {
69 echo 'kernel kernel-vserver kernel-devel kernel-headers kernel-debuginfo util-vserver* iptables iptables-ipv6 iproute drupal' ;;
71 echo 'kernel kernel-vserver kernel-devel kernel-headers kernel-debuginfo util-vserver* iptables iproute drupal inotify-tools* libnl' ;;
73 echo "pl_DISTRO_NAME=$pl_DISTRO_NAME not supported in getexcludes.sh" ; exit 1 ;;
77 # figure out which redhat distro we are using (fedora, centos, redhat)
78 pl_DISTRO=$(pl_getDistro)
80 # select basearch of the host devel environment - protected for macos for local tests
81 pl_DISTRO_ARCH=$(uname -i 2>/dev/null|| echo unknownarch)
83 # the release number (plain number)
84 pl_DISTRO_RELEASE=$(pl_getRelease)
86 # the release name - for vserver build ...
87 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
89 # the packages to exclude
90 pl_KEXCLUDES=$(pl_getKexcludes $pl_DISTRO_NAME)
92 # get path to appropriate yumgroups.xml file
93 # Thierry: quick & dirty improvement
94 # this file is updated by the toplevel build, from the .pkgs files
95 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
97 function pl_process_fedora_options () {
100 while getopts "l:r:a:h" opt ; do
103 pl_DISTRO_URL=$OPTARG
104 let shiftcount=$shiftcount+2
107 pl_DISTRO_RELEASE=$OPTARG
108 let shiftcount=$shiftcount+2
111 pl_DISTRO_ARCH=$OPTARG
112 let shiftcount=$shiftcount+2
115 echo "Usage: $0 [OPTION]..."
116 echo " -l url distro mirror location (default: $pl_DISTRO_URL)"
117 echo " -r release distro release number (default: $pl_DISTRO_RELEASE)"
118 echo " -a arch distro architecture (default: $pl_DISTRO_ARCH)"
119 echo "where distro can be either fedora, centos, or redhat"
120 echo " -h This message"
128 ######################################## handling a root image
129 function pl_root_rpm_macros () {
131 %_install_langs C:en_US:en
132 %_netsharedpath /proc:/dev/pts:/usr/share/info
134 %__file_context_path /dev/null
138 function pl_root_makedevs() {
140 # Clean ${vroot}/dev, but only when ${vroot}!=""
141 [ -n $vroot ] && rm -rf $vroot/dev
144 mknod -m 666 $vroot/dev/null c 1 3
145 mknod -m 666 $vroot/dev/zero c 1 5
146 mknod -m 666 $vroot/dev/full c 1 7
147 mknod -m 644 $vroot/dev/random c 1 8
148 mknod -m 644 $vroot/dev/urandom c 1 9
149 mknod -m 666 $vroot/dev/tty c 5 0
150 mknod -m 666 $vroot/dev/ptmx c 5 2
151 # For bash command substitution
152 ln -nsf ../proc/self/fd $vroot/dev/fd
154 # For df and linuxconf
155 touch $vroot/dev/hdv1
158 mkdir -p $vroot/dev/pts
160 # (Might have to remove the following for vserver-reference.)
163 mkdir -p $vroot/dev/shm
166 mkdir -p $vroot/dev/net
167 mknod -m 600 $vroot/dev/net/tun c 10 200
169 # For mkinitrd (in case a kernel is being installed)
170 # As well for loop back mounting within a vserver.
171 for i in $(seq 0 255) ; do
172 mknod -m 640 $vroot/dev/loop$i b 7 $i
176 function mkfedora_usage() {
177 echo "Usage: pl_root_mkfedora [OPTION]... basedir pldistro pkgsfile(s)"
178 echo " -l url Fedora mirror location."
179 echo " Defaults are searched in <pldistro>.mirrors"
180 echo " -v Be verbose"
181 echo " -h This message"
182 echo " target selection (defaults based on current build vserver)"
183 echo " -r release Fedora release number (default: $releasever)"
184 echo " -a arch Fedora architecture (default: $basearch)"
188 function pl_root_mkfedora () {
190 echo "* Entering pl_root_mkfedora" "$@"
192 if [ $UID -ne 0 ] ; then
193 echo "Error: You must run this script as root."
200 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
201 releasever=$pl_DISTRO_RELEASE
202 basearch=$pl_DISTRO_ARCH
205 while getopts "vh" opt ; do
207 v) verbose=1; set -x ;;
208 h|*) mkfedora_usage ;;
212 shift $(($OPTIND - 1))
213 [[ "$#" -lt 3 ]] && mkfedora_usage
217 vroot=$(cd $vroot && pwd -P)
218 [ -d $vroot ] || mkfedora_usage
221 # parse pkgsfile and add to local vars
222 fcdistro=${pl_DISTRO_NAME}
223 pkgs_packages=$(pl_parsePkgs package $fcdistro $pldistro $pkgsfile)
224 pkgs_groups=$(pl_parsePkgs group $fcdistro $pldistro $pkgsfile)
225 # packages to exclude
226 pkgs_excludes=$(pl_parsePkgs exclude $fcdistro $pldistro $pkgsfile)
227 # what can get trashed to save space
228 pkgs_junk=$(pl_parsePkgs junk $fcdistro $pldistro $pkgsfile)
230 pkgs_precious=$(pl_parsePkgs precious $fcdistro $pldistro $pkgsfile)
231 # formerly related to mkfedora -k : packages to take from our own build
232 # and thus need be excluded frem the stock repos
233 pkgs_kexcludes=$(pl_parsePkgs kexclude $fcdistro $pldistro $pkgsfile | sed -e s,@KEXCLUDE@,"$pl_KEXCLUDES",)
234 # get mirrors if not specified with -l
235 if [ -z "$mirrors" ] ; then
236 mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
237 mirrors=$(pl_parsePkgs mirror $fcdistro $pldistro $mirrorsfile)
241 # add them manually as the output of pl_parsePkgs is line-separated
242 if [ -n "$pkgs_kexcludes" ] ; then
243 kexclude_line="exclude="
244 for kexclude in $pkgs_kexcludes ; do
245 kexclude_line="$kexclude_line $kexclude"
249 echo "$0: candidate mirrors"
250 for mirror in $mirrors ; do
251 echo "* candidate mirror $mirror"
254 # the repo part of the final yum.conf
255 yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx
256 if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$kexclude_line" $mirrors ; then
257 echo xxx -- error ; return 1
260 # Do not tolerate errors
263 public_gpg_key=$(yumconf_gpgkey $yum_conf_repos)
265 ## make rpms ignore installing stuff to special fs entries like /proc
266 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
267 # you have to use at least one language beside 'C'
268 # Prevent all locales from being installed in reference image
269 mkdir -p $vroot/etc/rpm
270 pl_root_rpm_macros > $vroot/etc/rpm/macros
272 # Trick rpm and yum, who read the real root /etc/rpm/macros file
273 # rather than the one installed in the reference image, despite what
274 # you might expect the --root and --installroot options to mean. Both
275 # programs always read $HOME/.rpmmacros.
276 export HOME=$vroot/tmp
278 pl_root_rpm_macros > $vroot/tmp/.rpmmacros
280 function mkfedora_cleanup () {
281 umount -l $vroot/proc
282 umount -l $vroot/dev/shm
283 umount -l $vroot/dev/pts
286 # Clean up before exiting if anything goes wrong
287 trap "mkfedora_cleanup" ERR INT
289 # Mount in reference image
290 mount -t devpts none $vroot/dev/pts
291 mount -t tmpfs none $vroot/dev/shm
293 mount -t proc none $vroot/proc
295 # Create a /var/lib dirs for yum & rpm
296 mkdir -p $vroot/var/lib/yum
297 mkdir -p $vroot/var/lib/rpm
298 mkdir -p $vroot/usr/share/info
300 # Create a dummy /etc/fstab in reference image
302 cat >$vroot/etc/fstab <<EOF
303 # This fake fstab exists only to please df and linuxconf.
304 /dev/hdv1 / ext2 defaults 1 1
306 cp $vroot/etc/fstab $vroot/etc/mtab
308 # Necessary for some scripts
309 mkdir -p $vroot/etc/sysconfig
310 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
312 # Initialize RPM database in reference image
313 mkdir -p $vroot/var/lib/rpm
314 rpm --root $vroot --initdb
315 rpm --root $vroot --import $public_gpg_key
317 # Initialize yum in reference image
318 mkdir -p $vroot/var/cache/yum $vroot/var/log
320 # yum.conf is for building only - store in different location than /etc/yum.conf
321 yum_conf=$vroot/etc/mkfedora-yum.conf
322 cat > $yum_conf <<EOF
324 cachedir=/var/cache/yum
326 logfile=/var/log/yum.log
328 distroverpkg=redhat-release
334 # Prevent yum-2.4 from loading additional repository definitions
335 # (e.g., from /etc/yum.repos.d/)
339 cat $yum_conf_repos >> $yum_conf
341 # If we are being built as part of an automated RPM build, solve the
342 # bootstrap problem by including any just built packages in the yum
343 # configuration. This cooperates with the PlanetLab build system.
344 if [ -n "$RPM_BUILD_DIR" ] ; then
345 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
346 # If run under sudo, allow user to delete the headers/ and
347 # repodata/ directories.
348 if [ -n "$SUDO_USER" ] ; then
349 chown -R $SUDO_USER $RPM_RPMS_DIR
351 cat >> $yum_conf <<EOF
354 name=Building - $basearch - $RPM_RPMS_DIR/
355 baseurl=file://$RPM_RPMS_DIR/
359 echo "========== Dumping $yum_conf"
361 echo "========== EndDump $yum_conf"
364 # yum --help | grep verbose &> /dev/null && yum_options="$yum_options --verbose"
365 yum_options="$yum_options -y"
366 yum_options="$yum_options -c $yum_conf"
367 yum_options="$yum_options --installroot=$vroot"
370 for exclude in $pkgs_excludes; do
371 exclude_arg="$exclude_arg --exclude $exclude"
374 # glibc must be specified explicitly for the correct arch to be
376 echo "* Installing glibc"
377 # ignore yum's return code that is basically undefined
378 yum $yum_options $exclude_arg install glibc || :
381 if [ -n "$pkgs_packages" ] ; then
382 echo "* Installing optional packages" $pkgs_packages
383 # ignore yum's return code that is basically undefined
384 yum $yum_options $exclude_arg install $pkgs_packages || :
385 if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
386 echo "* Warning: Missing packages"
387 rpm --root $vroot -q $pkgs_packages | grep "not installed"
391 if [ -n "$pkgs_groups" ] ; then
392 ## call yum sequentially to get finer-grained info on dependencies
393 for group_plus in $pkgs_groups ; do
394 group=$(echo $group_plus | sed -e "s,+++, ,g")
395 echo "* Installing optional group $group"
396 # ignore yum's return code that is basically undefined
397 yum $yum_options $exclude_arg groupinstall "$group" || :
401 # formerly in bootcd/prep.sh : to optimize footprint
402 if [ -n "$pkgs_junk" ] ; then
403 echo "* Removing unnecessary junk"
405 # Save precious files
406 [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
407 # Remove unnecessary junk
408 [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
409 # Restore precious files
410 [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
416 # ignore yum's return code that is basically undefined
417 yum $yum_options clean all || :
420 rm -f $vroot/var/lib/rpm/__db*
422 # Set time zone to UTC
423 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
424 rm -f $vroot/etc/localtime
425 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
428 echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt"
429 chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt
431 # remove trap handler, as we are about to call it directly.
440 function pl_root_tune_image () {
443 # Disable all services in reference image
444 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
446 # FC2 minilogd starts up during shutdown and makes unmounting
447 # impossible. Just get rid of it.
448 rm -f $root/sbin/minilogd
449 ln -nsf /bin/true $root/sbin/minilogd
451 # This tells the Boot Manager that it is okay to update
452 # /etc/resolv.conf and /etc/hosts whenever the network configuration
453 # changes. Users are free to delete this file.
454 touch $root/etc/AUTO_UPDATE_NET_FILES
457 # Move specified directories out of a src tree into a dst tree, and
458 # then for each moved directory create a symlink in src to dst.
459 function pl_move_dirs() {
466 for datadir in "$@" ; do
467 mkdir -p ${data}${datadir}
468 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
469 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
471 rm -rf ${root}/${datadir}
472 mkdir -p $(dirname ${root}/${datadir})
473 ln -nsf ${store}/${datadir} ${root}/${datadir}
477 # Construct an image file from given some directory
478 # XXX in the future maybe use livecdtools?
479 function pl_make_image() {
484 # Leave about 100 MB free space and allow for about 20% inode overhead
485 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
487 blocks=$(($bytes / $bs))
488 dd bs=$bs count=$blocks if=/dev/zero of=$image
489 mkfs.ext3 -b $bs -j -F $image
491 # Temporarily mount it
492 tmp=$(mktemp -d tmp.XXXXXX)
493 mount -o loop $image $tmp
494 trap "umount $tmp; rmdir $tmp" ERR INT
497 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
505 # Fix permissions on tmp directories
506 function pl_fixtmp_permissions() {
508 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
511 function pl_fixdirs() {
514 for datadir in datadirs ; do
515 if [ -h ${root}/${datadir} ] ; then
516 rm -f ${root}/${datadir}
517 mkdir -p ${root}/${datadir}
522 ########## .pkgs format
523 # comments start with a # - this is needed only if you use a keyword in a comment
525 function pl_getPkgsAttribute () {
528 # remove any initial white spaces from the result
529 grep -v '^#' $file | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,," -e "s,^[[:space:]][[:space:]]*,,"
532 # for a given keyword like 'package' :
533 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
535 # package: pkg1 .. pkgn
536 # package+f8: pkg1 .. pkgn
537 # package-f8: pkg1 .. pkgn
539 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
541 # Usage: pl_parsePkgs keyword [-a arch] fcdistro pldistro pkgs-file[..s]
542 # the reason for the -a option is for when we build the build vserver itself; in this case
543 # pl_DISTRO_ARCH is the one we obtain from the root context, and that's wrong
544 # specify -sa arch AFTER keyword, so as to keep pl_getPackages and pl_getGroups simple
546 function pl_parsePkgs () {
548 target_arch=$pl_DISTRO_ARCH
550 [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
553 # remaining arguments are paths to the pkgs files
555 # grab regular descriptions
556 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
558 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
560 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
562 for rpm in $all $add; do
563 for exclude in $sub; do
564 [ "$rpm" = "$exclude" ] && continue 2
566 echo "${rpm} " | sed -e "s,@arch@,${target_arch},g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
571 # usage: pl_getPackages [-a arch] fcdistro pldistro pkg-file[..s]
572 function pl_getPackages() { pl_parsePkgs package "$@" ; }
573 function pl_getGroups() { pl_parsePkgs group "$@" ; }
575 # locates a pldistro-dependant file
576 # tries first in build/<pldistro>/, then in build/planetlab/
577 function pl_locateDistroFile () {
583 # if config dir is missing but a .svnpath exists
584 if [[ -f "$builddir/config.${pldistro}.svnpath" && ! -d "$builddir/config.${pldistro}" ]] ; then
585 echo 1>&2 "Invoking make to extract remote config.${pldistro}"
586 make 1>&2 --silent --no-print-directory -C $builddir stage1=true config.${pldistro}
589 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
590 for path in $paths; do
591 if [ -f $path ] ; then
596 if [ -z "$pkgspath" ] ; then
597 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
598 echo 1>&2 "candidates were $paths"
599 echo "not-found-by-pl_locateDistroFile"
602 echo 1>&2 "pl_locateDistroFile: using $pkgspath"
608 function yumgroups_from_pkgs () {
614 sedargs="-e s,@FCDISTRO@,$fcdistro,g"
617 <?xml version="1.0"?>
618 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
622 for pkgsname in $pkgsnames; do
623 pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
624 packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
626 groupname=$(pl_getPkgsAttribute groupname $pkgsfile | sed $sedargs)
627 groupdesc=$(pl_getPkgsAttribute groupdesc $pkgsfile | sed $sedargs)
629 if [ -z "$groupname" -o -z "$groupdesc" ] ; then
630 echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
634 cat << __group_header
636 <id>$(echo $groupname|tr A-Z a-z)</id>
637 <name>$groupname</name>
638 <description>$groupdesc</description>
639 <uservisible>true</uservisible>
642 for package in $packages; do
643 echo "<packagereq type=\"mandatory\">$package</packagereq>"
645 cat << __group_footer
656 function build_fetch () {
657 curl --fail --silent --max-time 60 --output /dev/null "$1"
660 # tries to compute a valid yum.conf for that pldistro from the template in mirroring/
661 # returns 0 and writes <dest_yumconf> on success
662 # returns 1 on failure, in which case <dest_yumconf> is deleted
663 function yumconf_mirrors () {
664 dest_yumconf=$1; shift
667 kexclude_line="$1" ; shift
670 template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
672 if [ ! -f $template ] ; then
673 echo "yumconf_mirrors: cannot locate template $template"
678 for mirror in $mirrors; do
679 if yumconf_mirror $dest_yumconf $template "$kexclude_line" $mirror; then
683 echo 'yumconf_mirrors in build.common : ran out of mirrors -- BAILING OUT'
688 # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
689 # returns 0 on success and 1 on failure
690 function yumconf_mirror () {
691 dest_yumconf=$1; shift
693 kexclude_line="$1" ; shift
696 sed -e "s,@MIRRORURL@,$mirror,g" \
698 $kexclude_line" $template > $dest_yumconf
700 # capture all lines defining baseurl
701 baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
702 if [ -z "$baseurl_defs" ] ; then
706 for baseurl_def in $baseurl_defs; do
707 baseurl=$(echo $baseurl_def | sed \
709 -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
710 repomd=$baseurl/repodata/repomd.xml
712 echo "* Trying to fetch $repomd"
713 if ! build_fetch $repomd ; then
714 echo "* Failed to fetch $repomd"
718 echo "* Selected mirror $mirror"
722 # from a yum.conf as generated above, computes the (first) gpgkey url
723 function yumconf_gpgkey () {
724 dest_yumconf=$1; shift
726 first_line=$(grep '^gpgkey=' $dest_yumconf | head -1)
727 values=$(echo $first_line | sed -e s,gpgkey=,,)
728 value=$(echo $values | awk '{print $1;}' | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
729 [ -n "$value" ] || return 1
734 # patches a yum conf to insert an exclude line in each declared repo
735 function yumconf_exclude () {
737 kexclude_line="$1" ; shift
739 sed -i -e "/baseurl=.*$/i\\
740 $kexclude_line" $repo