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 # support for fedora and centos only for now
14 function pl_getDistro() {
15 if [ -f "/etc/redhat-release" ] ; then
16 distro=$(awk ' { print $1 } ' /etc/redhat-release)
18 echo "build.common.pl_getDistro-unknown"
25 function pl_getRelease() {
26 if [ -f "/etc/redhat-release" ] ; then
27 release=$(awk ' { if ($1=="Fedora" && $2=="Core") print $4 ; if (($1=="Fedora" && $2!="Core")||$1=="CentOS") print $3 } ' /etc/redhat-release)
29 echo "build.common.pl_getRelease-unknown"
36 # vserver expects something like f8 or centos5
37 function pl_getReleaseName () {
52 releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
53 echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro"
61 function pl_getKexcludes () {
65 echo 'kernel* util-vserver* iptables iproute inotify-tools* libnl*'
68 echo 'kernel* util-vserver* iptables iproute inotify-tools* libnl*'
71 echo "pl_DISTRO_NAME=$pl_DISTRO_NAME not supported in getexcludes.sh"
76 # figure out which redhat distro we are using (fedora, centos, redhat)
77 pl_DISTRO=$(pl_getDistro)
79 # select basearch of the host devel environment - protected for macos for local tests
80 pl_DISTRO_ARCH=$(uname -i 2>/dev/null|| echo unknownarch)
82 # the release number (plain number)
83 pl_DISTRO_RELEASE=$(pl_getRelease)
85 # the release name - for vserver build ...
86 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
88 # the packages to exclude
89 pl_KEXCLUDES=$(pl_getKexcludes $pl_DISTRO_NAME)
91 # get path to appropriate yumgroups.xml file
92 # Thierry: quick & dirty improvement
93 # this file is updated by the toplevel build, from the .pkgs files
94 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
96 function pl_process_fedora_options () {
99 while getopts "l:r:a:h" opt ; do
102 pl_DISTRO_URL=$OPTARG
103 let shiftcount=$shiftcount+2
106 pl_DISTRO_RELEASE=$OPTARG
107 let shiftcount=$shiftcount+2
110 pl_DISTRO_ARCH=$OPTARG
111 let shiftcount=$shiftcount+2
114 echo "Usage: $0 [OPTION]..."
115 echo " -l url distro mirror location (default: $pl_DISTRO_URL)"
116 echo " -r release distro release number (default: $pl_DISTRO_RELEASE)"
117 echo " -a arch distro architecture (default: $pl_DISTRO_ARCH)"
118 echo "where distro can be either fedora, centos, or redhat"
119 echo " -h This message"
127 ######################################## handling a root image
128 function pl_root_rpm_macros () {
130 %_install_langs C:en_US:en
131 %_netsharedpath /proc:/dev/pts:/usr/share/info
133 %__file_context_path /dev/null
137 function pl_root_makedevs() {
139 # Clean ${vroot}/dev, but only when ${vroot}!=""
140 [ -n $vroot ] && rm -rf $vroot/dev
143 mknod -m 666 $vroot/dev/null c 1 3
144 mknod -m 666 $vroot/dev/zero c 1 5
145 mknod -m 666 $vroot/dev/full c 1 7
146 mknod -m 644 $vroot/dev/random c 1 8
147 mknod -m 644 $vroot/dev/urandom c 1 9
148 mknod -m 666 $vroot/dev/tty c 5 0
149 mknod -m 666 $vroot/dev/ptmx c 5 2
150 # For bash command substitution
151 ln -nsf ../proc/self/fd $vroot/dev/fd
153 # For df and linuxconf
154 touch $vroot/dev/hdv1
157 mkdir -p $vroot/dev/pts
159 # (Might have to remove the following for vserver-reference.)
162 mkdir -p $vroot/dev/shm
165 mkdir -p $vroot/dev/net
166 mknod -m 600 $vroot/dev/net/tun c 10 200
168 # For mkinitrd (in case a kernel is being installed)
169 # As well for loop back mounting within a vserver.
170 for i in $(seq 0 255) ; do
171 mknod -m 640 $vroot/dev/loop$i b 7 $i
175 function mkfedora_usage() {
176 echo "Usage: pl_root_mkfedora [OPTION]... basedir pldistro pkgsfile(s)"
177 echo " -l url Fedora mirror location."
178 echo " Defaults are searched in <pldistro>.mirrors"
179 echo " -v Be verbose"
180 echo " -h This message"
181 echo " target selection (defaults based on current build vserver)"
182 echo " -r release Fedora release number (default: $releasever)"
183 echo " -a arch Fedora architecture (default: $basearch)"
187 function pl_root_mkfedora () {
189 echo "* Entering pl_root_mkfedora" "$@"
191 if [ $UID -ne 0 ] ; then
192 echo "Error: You must run this script as root."
199 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
200 releasever=$pl_DISTRO_RELEASE
201 basearch=$pl_DISTRO_ARCH
204 while getopts "vh" opt ; do
206 v) verbose=1; set -x ;;
207 h|*) mkfedora_usage ;;
211 shift $(($OPTIND - 1))
212 [[ "$#" -lt 3 ]] && mkfedora_usage
216 vroot=$(cd $vroot && pwd -P)
217 [ -d $vroot ] || mkfedora_usage
220 # parse pkgsfile and add to local vars
221 fcdistro=${pl_DISTRO_NAME}
222 pkgs_packages=$(pl_parsePkgs package $fcdistro $pldistro $pkgsfile)
223 pkgs_groups=$(pl_parsePkgs group $fcdistro $pldistro $pkgsfile)
224 # packages to exclude
225 pkgs_excludes=$(pl_parsePkgs exclude $fcdistro $pldistro $pkgsfile)
226 # what can get trashed to save space
227 pkgs_junk=$(pl_parsePkgs junk $fcdistro $pldistro $pkgsfile)
229 pkgs_precious=$(pl_parsePkgs precious $fcdistro $pldistro $pkgsfile)
230 # formerly related to mkfedora -k : packages to take from our own build
231 # and thus need be excluded frem the stock repos
232 pkgs_kexcludes="$pl_KEXCLUDES"
233 # get mirrors if not specified with -l
234 if [ -z "$mirrors" ] ; then
235 mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
236 mirrors=$(pl_parsePkgs mirror $fcdistro $pldistro $mirrorsfile)
240 # add them manually as the output of pl_parsePkgs is line-separated
241 if [ -n "$pkgs_kexcludes" ] ; then
242 exclude_line="exclude="
243 for kexclude in $pkgs_kexcludes ; do
244 exclude_line="$exclude_line $kexclude"
248 echo "$0: candidate mirrors"
249 for mirror in $mirrors ; do
250 echo "* candidate mirror $mirror"
253 # the repo part of the final yum.conf
254 yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx
255 if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$exclude_line" $mirrors ; then
256 echo xxx -- error ; return 1
259 public_gpg_key=$(yumconf_gpgkey $yum_conf_repos)
261 # Do not tolerate errors
264 ## make rpms ignore installing stuff to special fs entries like /proc
265 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
266 # you have to use at least one language beside 'C'
267 # Prevent all locales from being installed in reference image
268 mkdir -p $vroot/etc/rpm
269 pl_root_rpm_macros > $vroot/etc/rpm/macros
271 # Trick rpm and yum, who read the real root /etc/rpm/macros file
272 # rather than the one installed in the reference image, despite what
273 # you might expect the --root and --installroot options to mean. Both
274 # programs always read $HOME/.rpmmacros.
275 export HOME=$vroot/tmp
277 pl_root_rpm_macros > $vroot/tmp/.rpmmacros
279 function mkfedora_cleanup () {
280 umount -l $vroot/proc
281 umount -l $vroot/dev/shm
282 umount -l $vroot/dev/pts
285 # Clean up before exiting if anything goes wrong
286 trap "mkfedora_cleanup" ERR INT
288 # Mount in reference image
289 mount -t devpts none $vroot/dev/pts
290 mount -t tmpfs none $vroot/dev/shm
292 mount -t proc none $vroot/proc
294 # Create a /var/lib dirs for yum & rpm
295 mkdir -p $vroot/var/lib/yum
296 mkdir -p $vroot/var/lib/rpm
297 mkdir -p $vroot/usr/share/info
299 # Create a dummy /etc/fstab in reference image
301 cat >$vroot/etc/fstab <<EOF
302 # This fake fstab exists only to please df and linuxconf.
303 /dev/hdv1 / ext2 defaults 1 1
305 cp $vroot/etc/fstab $vroot/etc/mtab
307 # Necessary for some scripts
308 mkdir -p $vroot/etc/sysconfig
309 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
311 # Initialize RPM database in reference image
312 mkdir -p $vroot/var/lib/rpm
313 rpm --root $vroot --initdb
314 rpm --root $vroot --import $public_gpg_key
316 # Initialize yum in reference image
317 mkdir -p $vroot/var/cache/yum $vroot/var/log
319 # yum.conf is for building only - store in different location than /etc/yum.conf
320 yum_conf=$vroot/etc/mkfedora-yum.conf
321 cat > $yum_conf <<EOF
323 cachedir=/var/cache/yum
325 logfile=/var/log/yum.log
327 distroverpkg=redhat-release
333 # Prevent yum-2.4 from loading additional repository definitions
334 # (e.g., from /etc/yum.repos.d/)
338 cat $yum_conf_repos >> $yum_conf
340 # If we are being built as part of an automated RPM build, solve the
341 # bootstrap problem by including any just built packages in the yum
342 # configuration. This cooperates with the PlanetLab build system.
343 if [ -n "$RPM_BUILD_DIR" ] ; then
344 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
345 # If run under sudo, allow user to delete the headers/ and
346 # repodata/ directories.
347 if [ -n "$SUDO_USER" ] ; then
348 chown -R $SUDO_USER $RPM_RPMS_DIR
350 cat >> $yum_conf <<EOF
353 name=Building - $basearch - $RPM_RPMS_DIR/
354 baseurl=file://$RPM_RPMS_DIR/
358 echo "========== Dumping $yum_conf"
360 echo "========== EndDump $yum_conf"
363 yum --help | grep verbose &> /dev/null && yum_options="$yum_options --verbose"
364 yum_options="$yum_options -y"
365 yum_options="$yum_options -c $yum_conf"
366 yum_options="$yum_options --installroot=$vroot"
369 for exclude in $pkgs_excludes; do
370 exclude_arg="$exclude_arg --exclude $exclude"
373 # glibc must be specified explicitly for the correct arch to be
375 echo "* Installing glibc"
376 yum $yum_options $exclude_arg install glibc
379 if [ -n "$pkgs_packages" ] ; then
380 echo "* Installing optional packages" $pkgs_packages
381 yum $yum_options $exclude_arg 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 grp in $pkgs_groups ; do
391 echo "* Installing optional group $grp"
392 yum $yum_options $exclude_arg groupinstall "$grp"
396 # formerly in bootcd/prep.sh : to optimize footprint
397 if [ -n "$pkgs_junk" ] ; then
398 echo "* Removing unnecessary junk"
400 # Save precious files
401 [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
402 # Remove unnecessary junk
403 [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
404 # Restore precious files
405 [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
411 yum $yum_options clean all
414 rm -f $vroot/var/lib/rpm/__db*
416 # Set time zone to UTC
417 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
418 rm -f $vroot/etc/localtime
419 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
422 echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt"
423 chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt
425 # remove trap handler, as we are about to call it directly.
434 function pl_root_tune_image () {
437 # Disable all services in reference image
438 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
440 # FC2 minilogd starts up during shutdown and makes unmounting
441 # impossible. Just get rid of it.
442 rm -f $root/sbin/minilogd
443 ln -nsf /bin/true $root/sbin/minilogd
445 # This tells the Boot Manager that it is okay to update
446 # /etc/resolv.conf and /etc/hosts whenever the network configuration
447 # changes. Users are free to delete this file.
448 touch $root/etc/AUTO_UPDATE_NET_FILES
451 # Move specified directories out of a src tree into a dst tree, and
452 # then for each moved directory create a symlink in src to dst.
453 function pl_move_dirs() {
460 for datadir in "$@" ; do
461 mkdir -p ${data}${datadir}
462 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
463 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
465 rm -rf ${root}/${datadir}
466 mkdir -p $(dirname ${root}/${datadir})
467 ln -nsf ${store}/${datadir} ${root}/${datadir}
471 # Construct an image file from given some directory
472 # XXX in the future maybe use livecdtools?
473 function pl_make_image() {
478 # Leave about 100 MB free space and allow for about 20% inode overhead
479 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
481 blocks=$(($bytes / $bs))
482 dd bs=$bs count=$blocks if=/dev/zero of=$image
483 mkfs.ext3 -b $bs -j -F $image
485 # Temporarily mount it
486 tmp=$(mktemp -d tmp.XXXXXX)
487 mount -o loop $image $tmp
488 trap "umount $tmp; rmdir $tmp" ERR INT
491 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
499 # Fix permissions on tmp directories
500 function pl_fixtmp_permissions() {
502 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
505 function pl_fixdirs() {
508 for datadir in datadirs ; do
509 if [ -h ${root}/${datadir} ] ; then
510 rm -f ${root}/${datadir}
511 mkdir -p ${root}/${datadir}
516 ########## .pkgs format
517 # comments start with a # - this is needed only if you use a keyword in a comment
519 function pl_getPkgsAttribute () {
522 # remove any initial white spaces from the result
523 grep -v '^#' $file | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,," -e "s,^[[:space:]][[:space:]]*,,"
526 # for a given keyword like 'package' :
527 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
529 # package: pkg1 .. pkgn
530 # package+f8: pkg1 .. pkgn
531 # package-f8: pkg1 .. pkgn
533 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
535 # Usage: pl_parsePkgs keyword [-a arch] fcdistro pldistro pkgs-file[..s]
536 # the reason for the -a option is for when we build the build vserver itself; in this case
537 # pl_DISTRO_ARCH is the one we obtain from the root context, and that's wrong
538 # specify -sa arch AFTER keyword, so as to keep pl_getPackages and pl_getGroups simple
540 function pl_parsePkgs () {
542 target_arch=$pl_DISTRO_ARCH
544 [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
547 # remaining arguments are paths to the pkgs files
549 # grab regular descriptions
550 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
552 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
554 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
556 for rpm in $all $add; do
557 for exclude in $sub; do
558 [ "$rpm" = "$exclude" ] && continue 2
560 echo "${rpm} " | sed -e "s,@arch@,${target_arch},g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
565 # usage: pl_getPackages [-a arch] fcdistro pldistro pkg-file[..s]
566 function pl_getPackages() { pl_parsePkgs package "$@" ; }
567 function pl_getGroups() { pl_parsePkgs group "$@" ; }
569 # locates a pldistro-dependant file
570 # tries first in build/<pldistro>/, then in build/planetlab/
571 function pl_locateDistroFile () {
578 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
579 for path in $paths; do
580 if [ -f $path ] ; then
585 if [ -z "$pkgspath" ] ; then
586 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
587 echo 1>&2 "candidates were $paths"
588 echo "not-found-by-pl_locateDistroFile"
591 echo 1>&2 "pl_locateDistroFile: using $pkgspath"
597 function yumgroups_from_pkgs () {
603 sedargs="-e s,@FCDISTRO@,$fcdistro,g"
606 <?xml version="1.0"?>
607 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
611 for pkgsname in $pkgsnames; do
612 pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
613 packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
615 groupname=$(pl_getPkgsAttribute groupname $pkgsfile | sed $sedargs)
616 groupdesc=$(pl_getPkgsAttribute groupdesc $pkgsfile | sed $sedargs)
618 if [ -z "$groupname" -o -z "$groupdesc" ] ; then
619 echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
623 cat << __group_header
625 <id>$(echo $groupname|tr A-Z a-z)</id>
626 <name>$groupname</name>
627 <description>$groupdesc</description>
628 <uservisible>true</uservisible>
631 for package in $packages; do
632 echo "<packagereq type=\"mandatory\">$package</packagereq>"
634 cat << __group_footer
645 function build_fetch () {
646 curl --fail --silent --max-time 60 --output /dev/null "$1"
649 # tries to compute a valid yum.conf for that pldistro from the template in mirroring/
650 # returns 0 and writes <dest_yumconf> on success
651 # returns 1 on failure, in which case <dest_yumconf> is deleted
652 function yumconf_mirrors () {
653 dest_yumconf=$1; shift
656 exclude_line="$1" ; shift
659 template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
661 if [ ! -f $template ] ; then
662 echo "yumconf_mirrors: cannot locate template $template"
667 for mirror in $mirrors; do
668 if yumconf_mirror $dest_yumconf $template "$exclude_line" $mirror; then
676 # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
677 # returns 0 on success and 1 on failure
678 function yumconf_mirror () {
679 dest_yumconf=$1; shift
681 exclude_line="$1" ; shift
684 sed -e "s,@MIRRORURL@,$mirror,g" \
686 $exclude_line" $template > $dest_yumconf
688 # capture all lines defining baseurl
689 baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
690 if [ -z "$baseurl_defs" ] ; then
694 for baseurl_def in $baseurl_defs; do
695 baseurl=$(echo $baseurl_def | sed \
697 -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
698 repomd=$baseurl/repodata/repomd.xml
700 echo "* Trying to fetch $repomd"
701 if ! build_fetch $repomd ; then
702 echo "* Failed to fetch $repomd"
706 echo "* Selected mirror $mirror"
710 # from a yum.conf as generated above, computes the (first) gpgkey url
711 function yumconf_gpgkey () {
712 dest_yumconf=$1; shift
714 first_line=$(grep '^gpgkey=' $dest_yumconf | head -1)
715 values=$(echo $first_line | sed -e s,gpgkey=,,)
716 value=$(echo $values | awk '{print $1;}' | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
717 [ -n "$value" ] || return 1