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 # for locating pkgs.py
17 # returns 'Fedora' or 'CentOS' for now
18 function pl_getDistro() {
19 if [ -f "/etc/redhat-release" ] ; then
20 distro=$(awk ' { print $1 } ' /etc/redhat-release)
21 if [ $distro="Red" ] ; then
25 echo "build.common.pl_getDistro-unknown"
32 # returns something like 8, 10, or 5.3
33 function pl_getRelease() {
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 ; if ($1=="Red") print $6 } ' /etc/redhat-release)
37 echo "build.common.pl_getRelease-unknown"
40 # keep only the major number
41 echo "$release" | cut -d. -f1
45 # returns stuff for vserver, i.e. something like 'f8' or 'centos5'
46 function pl_getReleaseName () {
64 releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
65 echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro"
73 # on fedora 8 or above, we use libnl from the stock repos
74 # on centos5 we build it locally
75 function pl_getKexcludes () {
79 echo 'kernel kernel-vserver kernel-devel kernel-headers kernel-debuginfo util-vserver* iptables iptables-ipv6 iproute drupal' ;;
81 echo 'kernel kernel-vserver kernel-devel kernel-headers kernel-debuginfo util-vserver* iptables iproute drupal inotify-tools* libnl' ;;
83 echo "pl_DISTRO_NAME=$pl_DISTRO_NAME not supported in getexcludes.sh" ; exit 1 ;;
87 # figure out which redhat distro we are using (fedora, centos, redhat)
88 pl_DISTRO=$(pl_getDistro)
90 # select basearch of the host devel environment - protected for macos for local tests
91 pl_DISTRO_ARCH=$(uname -i 2>/dev/null|| echo unknownarch)
93 # the release number (plain number)
94 pl_DISTRO_RELEASE=$(pl_getRelease)
96 # the release name - for vserver build ...
97 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
99 # the packages to exclude
100 pl_KEXCLUDES=$(pl_getKexcludes $pl_DISTRO_NAME)
102 # get path to appropriate yumgroups.xml file
103 # Thierry: quick & dirty improvement
104 # this file is updated by the toplevel build, from the .pkgs files
105 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
107 function pl_process_fedora_options () {
110 while getopts "l:r:a:h" opt ; do
113 pl_DISTRO_URL=$OPTARG
114 let shiftcount=$shiftcount+2
117 pl_DISTRO_RELEASE=$OPTARG
118 let shiftcount=$shiftcount+2
121 pl_DISTRO_ARCH=$OPTARG
122 let shiftcount=$shiftcount+2
125 echo "Usage: $0 [OPTION]..."
126 echo " -l url distro mirror location (default: $pl_DISTRO_URL)"
127 echo " -r release distro release number (default: $pl_DISTRO_RELEASE)"
128 echo " -a arch distro architecture (default: $pl_DISTRO_ARCH)"
129 echo "where distro can be either fedora, centos, or redhat"
130 echo " -h This message"
138 ######################################## handling a root image
139 function pl_root_rpm_macros () {
141 %_install_langs C:en_US:en
142 %_netsharedpath /proc:/dev/pts:/usr/share/info
144 %__file_context_path /dev/null
148 function pl_root_makedevs() {
150 # Clean ${vroot}/dev, but only when ${vroot}!=""
151 [ -n $vroot ] && rm -rf $vroot/dev
154 mknod -m 666 $vroot/dev/null c 1 3
155 mknod -m 666 $vroot/dev/zero c 1 5
156 mknod -m 666 $vroot/dev/full c 1 7
157 mknod -m 644 $vroot/dev/random c 1 8
158 mknod -m 644 $vroot/dev/urandom c 1 9
159 mknod -m 666 $vroot/dev/tty c 5 0
160 mknod -m 666 $vroot/dev/ptmx c 5 2
161 # For bash command substitution
162 ln -nsf ../proc/self/fd $vroot/dev/fd
164 # For df and linuxconf
165 touch $vroot/dev/hdv1
168 mkdir -p $vroot/dev/pts
170 # (Might have to remove the following for vserver-reference.)
173 mkdir -p $vroot/dev/shm
176 mkdir -p $vroot/dev/net
177 mknod -m 600 $vroot/dev/net/tun c 10 200
179 # For mkinitrd (in case a kernel is being installed)
180 # As well for loop back mounting within a vserver.
181 for i in $(seq 0 255) ; do
182 mknod -m 640 $vroot/dev/loop$i b 7 $i
186 function mkfedora_usage() {
187 echo "Usage: pl_root_mkfedora [OPTION]... basedir pldistro pkgsfile(s)"
188 echo " -l url Fedora mirror location."
189 echo " Defaults are searched in <pldistro>.mirrors"
190 echo " -v Be verbose"
191 echo " -h This message"
192 echo " target selection (defaults based on current build vserver)"
193 echo " -r release Fedora release number (default: $releasever)"
194 echo " -a arch Fedora architecture (default: $basearch)"
198 function pl_root_mkfedora () {
200 echo "* Entering pl_root_mkfedora" "$@"
202 if [ $UID -ne 0 ] ; then
203 echo "Error: You must run this script as root."
210 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
211 releasever=$pl_DISTRO_RELEASE
212 basearch=$pl_DISTRO_ARCH
215 while getopts "vh" opt ; do
217 v) verbose=1; set -x ;;
218 h|*) mkfedora_usage ;;
222 shift $(($OPTIND - 1))
223 [[ "$#" -lt 3 ]] && mkfedora_usage
227 vroot=$(cd $vroot && pwd -P)
228 [ -d $vroot ] || mkfedora_usage
231 # parse pkgsfile and add to local vars
232 fcdistro=${pl_DISTRO_NAME}
233 pkgs_packages=$(pl_parsePkgs package $fcdistro $pldistro $pkgsfile)
234 pkgs_groups=$(pl_parsePkgs group $fcdistro $pldistro $pkgsfile)
235 # what can get trashed to save space
236 pkgs_junk=$(pl_parsePkgs junk $fcdistro $pldistro $pkgsfile)
238 pkgs_precious=$(pl_parsePkgs precious $fcdistro $pldistro $pkgsfile)
239 # formerly related to mkfedora -k : packages to take from our own build
240 # and thus need be excluded frem the stock repos
241 pkgs_kexcludes=$(pl_parsePkgs kexclude $fcdistro $pldistro $pkgsfile | sed -e s,@KEXCLUDE@,"$pl_KEXCLUDES",)
242 # get mirrors if not specified with -l
243 if [ -z "$mirrors" ] ; then
244 mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
245 mirrors=$(pl_parsePkgs mirror $fcdistro $pldistro $mirrorsfile)
249 [ -n "$pkgs_kexcludes" ] && kexclude_line="exclude=$pkgs_kexcludes"
251 echo "$0: candidate mirrors"
252 for mirror in $mirrors ; do
253 echo "* candidate mirror $mirror"
256 # the repo part of the final yum.conf
257 yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx
258 if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$kexclude_line" $mirrors ; then
259 echo xxx -- error ; return 1
262 # Do not tolerate errors
265 public_gpg_key=$(yumconf_gpgkey $yum_conf_repos)
267 ## make rpms ignore installing stuff to special fs entries like /proc
268 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
269 # you have to use at least one language beside 'C'
270 # Prevent all locales from being installed in reference image
271 mkdir -p $vroot/etc/rpm
272 pl_root_rpm_macros > $vroot/etc/rpm/macros
274 # Trick rpm and yum, who read the real root /etc/rpm/macros file
275 # rather than the one installed in the reference image, despite what
276 # you might expect the --root and --installroot options to mean. Both
277 # programs always read $HOME/.rpmmacros.
278 export HOME=$vroot/tmp
280 pl_root_rpm_macros > $vroot/tmp/.rpmmacros
282 function mkfedora_cleanup () {
283 umount -l $vroot/proc
284 umount -l $vroot/dev/shm
285 umount -l $vroot/dev/pts
288 # Clean up before exiting if anything goes wrong
289 trap "mkfedora_cleanup" ERR INT
291 # Mount in reference image
292 mount -t devpts none $vroot/dev/pts
293 mount -t tmpfs none $vroot/dev/shm
295 mount -t proc none $vroot/proc
297 # Create a /var/lib dirs for yum & rpm
298 mkdir -p $vroot/var/lib/yum
299 mkdir -p $vroot/var/lib/rpm
300 mkdir -p $vroot/usr/share/info
302 # Create a dummy /etc/fstab in reference image
304 cat >$vroot/etc/fstab <<EOF
305 # This fake fstab exists only to please df and linuxconf.
306 /dev/hdv1 / ext2 defaults 1 1
308 cp $vroot/etc/fstab $vroot/etc/mtab
310 # Necessary for some scripts
311 mkdir -p $vroot/etc/sysconfig
312 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
314 # Initialize RPM database in reference image
315 mkdir -p $vroot/var/lib/rpm
316 rpm --root $vroot --initdb
317 rpm --root $vroot --import $public_gpg_key
319 # Initialize yum in reference image
320 mkdir -p $vroot/var/cache/yum $vroot/var/log
322 # yum.conf is for building only - store in different location than /etc/yum.conf
323 yum_conf=$vroot/etc/mkfedora-yum.conf
324 cat > $yum_conf <<EOF
326 cachedir=/var/cache/yum
328 logfile=/var/log/yum.log
330 distroverpkg=redhat-release
336 # Prevent yum-2.4 from loading additional repository definitions
337 # (e.g., from /etc/yum.repos.d/)
341 cat $yum_conf_repos >> $yum_conf
343 # If we are being built as part of an automated RPM build, solve the
344 # bootstrap problem by including any just built packages in the yum
345 # configuration. This cooperates with the PlanetLab build system.
346 if [ -n "$RPM_BUILD_DIR" ] ; then
347 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
348 # If run under sudo, allow user to delete the headers/ and
349 # repodata/ directories.
350 if [ -n "$SUDO_USER" ] ; then
351 chown -R $SUDO_USER $RPM_RPMS_DIR
353 cat >> $yum_conf <<EOF
356 name=Building - $basearch - $RPM_RPMS_DIR/
357 baseurl=file://$RPM_RPMS_DIR/
361 echo "========== Dumping $yum_conf"
363 echo "========== EndDump $yum_conf"
366 # yum --help | grep verbose &> /dev/null && yum_options="$yum_options --verbose"
367 yum_options="$yum_options -y"
368 yum_options="$yum_options -c $yum_conf"
369 yum_options="$yum_options --installroot=$vroot"
371 # glibc must be specified explicitly for the correct arch to be
373 echo "* Installing glibc"
374 # ignore yum's return code that is basically undefined
375 yum $yum_options install glibc || :
378 if [ -n "$pkgs_packages" ] ; then
379 echo "* Installing optional packages" $pkgs_packages
380 # ignore yum's return code that is basically undefined
381 yum $yum_options install $pkgs_packages || :
382 if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
383 echo "* Warning: Missing packages"
384 rpm --root $vroot -q $pkgs_packages | grep "not installed"
388 if [ -n "$pkgs_groups" ] ; then
389 ## call yum sequentially to get finer-grained info on dependencies
390 for group_plus in $pkgs_groups ; do
391 group=$(echo $group_plus | sed -e "s,+++, ,g")
392 echo "* Installing optional group $group"
393 # ignore yum's return code that is basically undefined
394 yum $yum_options groupinstall "$group" || :
398 # formerly in bootcd/prep.sh : to optimize footprint
399 if [ -n "$pkgs_junk" ] ; then
400 echo "* Removing unnecessary junk"
402 # Save precious files
403 [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
404 # Remove unnecessary junk
405 [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
406 # Restore precious files
407 [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
413 # ignore yum's return code that is basically undefined
414 yum $yum_options clean all || :
417 rm -f $vroot/var/lib/rpm/__db*
419 # Set time zone to UTC
420 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
421 rm -f $vroot/etc/localtime
422 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
425 echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt"
426 chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt
428 # remove trap handler, as we are about to call it directly.
437 function pl_root_tune_image () {
440 # Disable all services in reference image
441 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
443 # FC2 minilogd starts up during shutdown and makes unmounting
444 # impossible. Just get rid of it.
445 rm -f $root/sbin/minilogd
446 ln -nsf /bin/true $root/sbin/minilogd
448 # This tells the Boot Manager that it is okay to update
449 # /etc/resolv.conf and /etc/hosts whenever the network configuration
450 # changes. Users are free to delete this file.
451 touch $root/etc/AUTO_UPDATE_NET_FILES
454 # Move specified directories out of a src tree into a dst tree, and
455 # then for each moved directory create a symlink in src to dst.
456 function pl_move_dirs() {
463 for datadir in "$@" ; do
464 mkdir -p ${data}${datadir}
465 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
466 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
468 rm -rf ${root}/${datadir}
469 mkdir -p $(dirname ${root}/${datadir})
470 ln -nsf ${store}/${datadir} ${root}/${datadir}
474 # Construct an image file from given some directory
475 # XXX in the future maybe use livecdtools?
476 function pl_make_image() {
481 # Leave about 100 MB free space and allow for about 20% inode overhead
482 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
484 blocks=$(($bytes / $bs))
485 dd bs=$bs count=$blocks if=/dev/zero of=$image
486 mkfs.ext3 -b $bs -j -F $image
488 # Temporarily mount it
489 tmp=$(mktemp -d tmp.XXXXXX)
490 mount -o loop $image $tmp
491 trap "umount $tmp; rmdir $tmp" ERR INT
494 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
502 # Fix permissions on tmp directories
503 function pl_fixtmp_permissions() {
505 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
508 function pl_fixdirs() {
511 for datadir in datadirs ; do
512 if [ -h ${root}/${datadir} ] ; then
513 rm -f ${root}/${datadir}
514 mkdir -p ${root}/${datadir}
519 ########## .pkgs format
520 # Usage: pl_parsePkgs keyword [-a arch] fcdistro pldistro pkgs-file[..s]
521 # pkgs.py should be found in PATH, like this file build.common
522 function pl_parsePkgs () {
523 target_arch=$pl_DISTRO_ARCH
525 [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
529 pkgs.py -a $target_arch -f $fcdistro -d $pldistro $keyword "$@"
531 # usage: pl_getPackages [-a arch] fcdistro pldistro pkg-file[..s]
532 function pl_getPackages() { pl_parsePkgs package "$@" ; }
533 function pl_getGroups() { pl_parsePkgs group "$@" ; }
535 ##############################
536 ### temporary - only for checking the new python version
537 # for a given keyword like 'package' :
538 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
540 # package: pkg1 .. pkgn
541 # package+f8: pkg1 .. pkgn
542 # package-f8: pkg1 .. pkgn
544 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
546 # the reason for the -a option is for when we build the build vserver itself; in this case
547 # pl_DISTRO_ARCH is the one we obtain from the root context, and that's wrong
548 # specify -sa arch AFTER keyword, so as to keep pl_getPackages and pl_getGroups simple
550 function pl_parsePkgs_old () {
552 target_arch=$pl_DISTRO_ARCH
554 [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
557 # remaining arguments are paths to the pkgs files
559 # grab regular descriptions
560 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
562 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
564 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
566 for rpm in $all $add; do
567 for exclude in $sub; do
568 [ "$rpm" = "$exclude" ] && continue 2
570 echo "${rpm} " | sed -e "s,@arch@,${target_arch},g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
574 ##############################
576 # locates a pldistro-dependant file
577 # tries first in build/<pldistro>/, then in build/planetlab/
578 function pl_locateDistroFile () {
584 # if config dir is missing but a .svnpath exists
585 if [[ -f "$builddir/config.${pldistro}.svnpath" && ! -d "$builddir/config.${pldistro}" ]] ; then
586 echo 1>&2 "Invoking make to extract remote config.${pldistro}"
587 make 1>&2 --silent --no-print-directory -C $builddir stage1=true config.${pldistro}
590 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
591 for path in $paths; do
592 if [ -f $path ] ; then
597 if [ -z "$pkgspath" ] ; then
598 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
599 echo 1>&2 "candidates were $paths"
600 echo "not-found-by-pl_locateDistroFile"
603 echo 1>&2 "pl_locateDistroFile: using $pkgspath"
609 function yumgroups_from_pkgs () {
615 sedargs="-e s,@FCDISTRO@,$fcdistro,g"
618 <?xml version="1.0"?>
619 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
623 for pkgsname in $pkgsnames; do
624 pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
625 packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
627 groupname=$(pkgs.py groupname $pkgsfile | sed $sedargs)
628 groupdesc=$(pkgs.py groupdesc $pkgsfile | sed $sedargs)
630 if [ -z "$groupname" -o -z "$groupdesc" ] ; then
631 echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
635 cat << __group_header
637 <id>$(echo $groupname|tr A-Z a-z)</id>
638 <name>$groupname</name>
639 <description>$groupdesc</description>
640 <uservisible>true</uservisible>
643 for package in $packages; do
644 echo "<packagereq type=\"mandatory\">$package</packagereq>"
646 cat << __group_footer
657 function build_fetch () {
658 curl --fail --silent --max-time 60 --output /dev/null "$1"
661 # tries to compute a valid yum.conf for that pldistro from the template in mirroring/
662 # returns 0 and writes <dest_yumconf> on success
663 # returns 1 on failure, in which case <dest_yumconf> is deleted
664 function yumconf_mirrors () {
665 dest_yumconf=$1; shift
668 kexclude_line="$1" ; shift
671 template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
673 if [ ! -f $template ] ; then
674 echo "yumconf_mirrors: cannot locate template $template"
679 for mirror in $mirrors; do
680 if yumconf_mirror $dest_yumconf $template "$kexclude_line" $mirror; then
684 echo 'yumconf_mirrors in build.common : ran out of mirrors -- BAILING OUT'
689 # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
690 # returns 0 on success and 1 on failure
691 function yumconf_mirror () {
692 dest_yumconf=$1; shift
694 kexclude_line="$1" ; shift
697 sed -e "s,@MIRRORURL@,$mirror,g" \
699 $kexclude_line" $template > $dest_yumconf
701 # capture all lines defining baseurl
702 baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
703 if [ -z "$baseurl_defs" ] ; then
707 for baseurl_def in $baseurl_defs; do
708 baseurl=$(echo $baseurl_def | sed \
710 -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
711 repomd=$baseurl/repodata/repomd.xml
713 echo "* Trying to fetch $repomd"
714 if ! build_fetch $repomd ; then
715 echo "* Failed to fetch $repomd"
719 echo "* Selected mirror $mirror"
723 # from a yum.conf as generated above, computes the (first) gpgkey url
724 function yumconf_gpgkey () {
725 dest_yumconf=$1; shift
727 first_line=$(grep '^gpgkey=' $dest_yumconf | head -1)
728 values=$(echo $first_line | sed -e s,gpgkey=,,)
729 value=$(echo $values | awk '{print $1;}' | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
730 [ -n "$value" ] || return 1
735 # patches a yum conf to insert an exclude line in each declared repo
736 function yumconf_exclude () {
738 kexclude_line="$1" ; shift
740 sed -i -e "/#baseurl=.*$/i\\
741 $kexclude_line" $repo