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
18 function pl_getDefaultRelease() {
19 # FC4 is currently the default release
24 function pl_getDistro() {
25 defaultDistro=$(pl_getDefaultDistro)
26 if [ -f "/etc/redhat-release" ] ; then
27 distro=$(awk ' { print $1 } ' /etc/redhat-release)
35 function pl_getRelease() {
36 defaultRelease=$(pl_getDefaultRelease)
37 if [ -f "/etc/redhat-release" ] ; then
38 release=$(awk ' { if ($1=="Fedora" && $2=="Core") print $4 ; if (($1=="Fedora" && $2!="Core")||$1=="CentOS") print $3 } ' /etc/redhat-release)
39 [ $release -lt $defaultRelease ] && release=$defaultRelease
41 release=$defaultRelease
47 # vserver expects something like fc4 or f7
48 # for fedora and centos only for now
49 function pl_getReleaseName () {
54 if [ "$release" -le 6 ] ; then
55 releasename=fc$release
60 if [ "$release" = "4.5" ] ; then
61 # centos 4.5 is just centos4 + enhancements
64 releasename=centos$release
67 releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
68 echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro"
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 # let mkfedora select one of its mirrors
85 # the release number (plain number)
86 pl_DISTRO_RELEASE=$(pl_getRelease)
88 # the release name - for vserver build - like fc4, f8 or centos4
89 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
91 # get path to appropriate yumgroups.xml file
92 # Thierry: quick & dirty improvement
93 # this file is updated by the toplevel build, from groups/<pldistro>.xml
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 # Default yum repositories to try
178 http://localhost/fedora
179 http://build.planet-lab.org/fedora
180 http://coblitz.codeen.org/coblitz.planet-lab.org/pub/fedora
181 ftp://mirror.cs.princeton.edu/pub/mirrors/fedora
182 ftp://mirror.stanford.edu/pub/mirrors/fedora
183 ftp://rpmfind.net/linux/fedora
186 function mkfedora_usage() {
187 echo "Usage: pl_root_mkfedora [OPTION]... basedir"
188 echo " -l url Fedora mirror location. Default is to try:"
189 for mirror in "${mirrors[@]}" ; do
192 echo " -f pkgsfile use this pkgs file for packages, groups, excludes.."
193 echo " -k Exclude kernel* packages from all repositories except bootstrap"
194 echo " -v Be verbose"
195 echo " -h This message"
196 echo " target selection (defaults based on current build vserver)"
197 echo " -r release Fedora release number (default: $releasever)"
198 echo " -a arch Fedora architecture (default: $basearch)"
199 echo " legacy (use -f instead)"
200 echo " -g group1 -g group2 ..."
201 echo " Yumgroups to install (default: none)"
202 echo " -p package1 -p package2 ..."
203 echo " Additional packages to install (default: none)"
204 echo " -x package1 -x package2 ..."
205 echo " Packages to exclude (default: none)"
209 function pl_root_mkfedora () {
211 echo "* Entering pl_root_mkfedora" "$@"
213 if [ $UID -ne 0 ] ; then
214 echo "Error: You must run this script as root."
221 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
222 releasever=$pl_DISTRO_RELEASE
223 basearch=$pl_DISTRO_ARCH
224 [ -n "$pl_DISTRO_URL" ] && mirrors=($pl_DISTRO_URL)
226 # Yum groups to install
229 # Packages to install
232 # Packages to exclude
235 # Exclude kernel* (and related) packages from all repositories except bootstrap
239 while getopts "l:r:a:g:p:x:f:kvh" opt ; do
242 if echo $OPTARG | grep -q -i '^\(file\|http[s]*\)://' ; then
245 # xxx rather use this as a .mirrors file, searched in config.pldistro/
246 # that would use the pkgs syntax with mirror:
247 #mirrors=($(pl_parsePkgs mirror <fcdistro> $(pl_locateDistroFile ../build/ <pldistro> $OPTARG.mirrors)))
248 mirrors=(file://$OPTARG)
258 groups[${#groups[*]}]="$OPTARG"
261 packages[${#packages[*]}]="$OPTARG"
264 exclude[${#exclude[*]}]="$OPTARG"
270 exclude_kernel="exclude=kernel* ulogd iptables"
282 shift $(($OPTIND - 1))
283 [[ -n "$@" ]] || mkfedora_usage
285 vroot=$(cd $vroot && pwd -P)
286 [[ -z "$@" ]] || mkfedora_usage
287 [ -d $vroot ] || mkfedora_usage
289 function mkfedora_fetch ()
291 curl --fail --silent --max-time 60 "$1"
294 # set list of attempted locations according to releasever
295 if [ $releasever -ge 7 ] ; then
297 linux/releases/$releasever/Everything/$basearch/os
298 $releasever/Everything/$basearch/os
299 core/$releasever/Everything/$basearch/os
300 linux/core/$releasever/$basearch/os
304 linux/core/$releasever/$basearch/os
305 core/$releasever/$basearch/os
306 $releasever/$basearch/os
310 echo "$0: candidate mirrors"
311 for mirror in "${mirrors[@]}" ; do
312 echo "* candidate mirror $mirror"
315 for mirror in "${mirrors[@]}" ; do
316 for attempt in $attempts; do
317 attempturl=$mirror/$attempt
318 if mkfedora_fetch $attempturl/repodata/repomd.xml >/dev/null ; then
325 if [ -z "$baseurl" ] ; then
326 echo "Error: $releasever/$basearch/os/repodata/repomd.xml"
327 echo " could not be found in any of the following locations:"
329 for mirror in ${mirrors[@]} ; do
330 for attempt in $attempts ; do
331 echo $mirror/$attempt
338 # Do not tolerate errors
341 ## make rpms ignore installing stuff to special fs entries like /proc
342 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
343 # you have to use at least one language beside 'C'
344 # Prevent all locales from being installed in reference image
345 mkdir -p $vroot/etc/rpm
346 pl_root_rpm_macros > $vroot/etc/rpm/macros
348 # Trick rpm and yum, who read the real root /etc/rpm/macros file
349 # rather than the one installed in the reference image, despite what
350 # you might expect the --root and --installroot options to mean. Both
351 # programs always read $HOME/.rpmmacros.
352 export HOME=$vroot/tmp
354 pl_root_rpm_macros > $vroot/tmp/.rpmmacros
356 function mkfedora_cleanup () {
357 umount -l $vroot/proc
358 umount -l $vroot/dev/shm
359 umount -l $vroot/dev/pts
362 # Clean up before exiting if anything goes wrong
363 trap "mkfedora_cleanup" ERR INT
365 # Mount in reference image
366 mount -t devpts none $vroot/dev/pts
367 mount -t tmpfs none $vroot/dev/shm
369 mount -t proc none $vroot/proc
371 # Create a /var/lib dirs for yum & rpm
372 mkdir -p $vroot/var/lib/yum
373 mkdir -p $vroot/var/lib/rpm
374 mkdir -p $vroot/usr/share/info
376 # Create a dummy /etc/fstab in reference image
378 cat >$vroot/etc/fstab <<EOF
379 # This fake fstab exists only to please df and linuxconf.
380 /dev/hdv1 / ext2 defaults 1 1
382 cp $vroot/etc/fstab $vroot/etc/mtab
384 # Necessary for some scripts
385 mkdir -p $vroot/etc/sysconfig
386 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
388 # Initialize RPM database in reference image
389 mkdir -p $vroot/var/lib/rpm
390 rpm --root $vroot --initdb
391 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
393 # Initialize yum in reference image
394 mkdir -p $vroot/var/cache/yum $vroot/var/log
395 if [ $releasever -lt 7 ] ; then
401 cat >$vroot/etc/yum.conf <<EOF
403 cachedir=/var/cache/yum
405 logfile=/var/log/yum.log
407 distroverpkg=redhat-release
413 # Prevent yum-2.4 from loading additional repository definitions
414 # (e.g., from /etc/yum.repos.d/)
418 name=Fedora ${corename}${releasever} - $basearch - base
423 for optional in updates extras ; do
425 $mirror/linux/core/$optional/$releasever/$basearch \
426 $mirror/core/$optional/$releasever/$basearch \
427 $mirror/linux/$optional/$releasever/$basearch \
428 $mirror/$optional/$releasever/$basearch ; do
429 if mkfedora_fetch $optionalurl/repodata/repomd.xml ; then
430 cat >>$vroot/etc/yum.conf <<EOF
432 [$(basename $optional)]
433 name=Fedora ${corename}${releasever} - $basearch - $(basename $optional)
434 baseurl=$optionalurl/
442 # If we are being built as part of an automated RPM build, solve the
443 # bootstrap problem by including any just built packages in the yum
444 # configuration. This cooperates with the PlanetLab build system.
445 ### make takes care of that
446 if [ -n "$RPM_BUILD_DIR" ] ; then
447 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
449 ### if [ -x /usr/bin/yum-arch ] ; then
450 ### yum-arch -q $RPM_RPMS_DIR
453 ### if [ -x /usr/bin/createrepo ] ; then
454 ### if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
455 ### groupfile="-g yumgroups.xml"
457 ### createrepo --quiet $groupfile $RPM_RPMS_DIR
459 # If run under sudo, allow user to delete the headers/ and
460 # repodata/ directories.
461 if [ -n "$SUDO_USER" ] ; then
462 chown -R $SUDO_USER $RPM_RPMS_DIR
464 cat >>$vroot/etc/yum.conf <<EOF
467 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
468 baseurl=file://$RPM_RPMS_DIR/
473 if [ -n "$pkgsfile" ] ; then
474 # parse pkgsfile and add to local vars
475 fcdistro=$(pl_getReleaseName "Fedora" $releasever)
476 for i in $(pl_parsePkgs package $fcdistro $pkgsfile) ; do
477 packages[${#packages[*]}]="$i"
479 for i in $(pl_parsePkgs group $fcdistro $pkgsfile) ; do
480 groups[${#groups[*]}]="$i"
482 for i in $(pl_parsePkgs exclude $fcdistro $pkgsfile) ; do
483 exclude[${#exclude[*]}]="$i"
485 junk=$(pl_parsePkgs junk $fcdistro $pkgsfile)
486 precious=$(pl_parsePkgs precious $fcdistro $pkgsfile)
490 for package in "${exclude[@]}" ; do
491 excludes="$excludes --exclude=$package"
494 # glibc must be specified explicitly for the correct arch to be
496 echo "* Installing glibc"
497 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes install glibc
500 if [ ${#packages[*]} -gt 0 ] ; then
501 echo "* Installing optional packages" "${packages[@]}"
502 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
503 install "${packages[@]}"
504 if ! rpm --root $vroot -q "${packages[@]}" >/dev/null ; then
505 echo "* Warning: Missing packages"
506 rpm --root $vroot -q "${packages[@]}" | grep "not installed"
510 if [ ${#groups[*]} -gt 0 ] ; then
511 ## call yum sequentially to get finer-grained info on dependencies
512 for grp in "${groups[@]}" ; do
513 echo "* Installing optional group $grp"
514 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
519 # formerly in bootcd/prep.sh : to optimize footprint
520 echo "* Removing unnecessary junk"
524 # Save precious files
525 [ -n "$precious" ] && tar --ignore-failed-read -cpf precious.tar $precious
527 # Remove unnecessary junk
528 [ -n "$junk" ] && rm -rf $junk
530 # Restore precious files
531 [ -n "$precious" ] && tar -xpf precious.tar && rm -f precious.tar
535 ### # FC2 dev %preinstall checks /proc/mounts to make sure that /dev is
536 ### # not currently mounted as devfs. If it thinks it is, it will refuse
537 ### # to install the package. On a modern system running udev that mounts
538 ### # /dev as tmpfs, this check fails. Since we are installing into a
539 ### # chroot, whether /dev is mounted on the host system or not doesn't
540 ### # matter. If dev was explicitly mentioned in the packages list, force
541 ### # its installation.
542 ### if [ "$releasever" = "2" ] ; then
543 ### for package in "${packages[@]}" ; do
544 ### if [ "$package" = "dev" ] && ! rpm --root $vroot -q dev >/dev/null 2>&1 ; then
545 ### rpm --root $vroot -Uvh --noscripts $baseurl/Fedora/RPMS/dev-3.3.13-1.i386.rpm
553 yum -c $vroot/etc/yum.conf --installroot=$vroot -y clean all
556 rm -f $vroot/var/lib/rpm/__db*
558 # Set time zone to UTC
559 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
560 rm -f $vroot/etc/localtime
561 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
564 # remove trap handler, as we are about to call it directly.
573 function pl_root_setup_chroot() {
577 pl_root_makedevs $root
578 pl_root_mkfedora $options $root
580 # Disable all services in reference image
581 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
583 # FC2 minilogd starts up during shutdown and makes unmounting
584 # impossible. Just get rid of it.
585 rm -f $root/sbin/minilogd
586 ln -nsf /bin/true $root/sbin/minilogd
588 # This tells the Boot Manager that it is okay to update
589 # /etc/resolv.conf and /etc/hosts whenever the network configuration
590 # changes. Users are free to delete this file.
591 touch $vroot/etc/AUTO_UPDATE_NET_FILES
594 # Move specified directories out of a src tree into a dst tree, and
595 # then for each moved directory create a symlink in src to dst.
596 function pl_move_dirs() {
603 for datadir in "$@" ; do
604 mkdir -p ${data}${datadir}
605 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
606 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
608 rm -rf ${root}/${datadir}
609 mkdir -p $(dirname ${root}/${datadir})
610 ln -nsf /${store}/${datadir} ${root}/${datadir}
614 # Construct an image file from given some directory
615 # XXX in the future maybe use livecdtools?
616 function pl_make_image() {
621 # Leave about 100 MB free space and allow for about 20% inode overhead
622 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
624 blocks=$(($bytes / $bs))
625 dd bs=$bs count=$blocks if=/dev/zero of=$image
626 mkfs.ext3 -b $bs -j -F $image
628 # Temporarily mount it
629 tmp=$(mktemp -d tmp.XXXXXX)
630 mount -o loop $image $tmp
631 trap "umount $tmp; rmdir $tmp" ERR INT
634 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
642 # Fix permissions on tmp directories
643 function pl_fixtmp_permissions() {
645 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
648 function pl_fixdirs() {
651 for datadir in datadirs ; do
652 if [ -h ${root}/${datadir} ] ; then
653 rm -f ${root}/${datadir}
654 mkdir -p ${root}/${datadir}
659 ########## .pkgs format
660 # comments start with a # - this is needed only if you use a keyword in a comment
662 function pl_getPkgsAttribute () {
665 grep -v '^#' $file | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,"
669 # for a given keyword like 'package' :
670 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
672 # package: pkg1 .. pkgn
673 # package+f8: pkg1 .. pkgn
674 # package-f8: pkg1 .. pkgn
676 function pl_parsePkgs () {
680 # remaining arguments are paths to the pkgs files
682 # grab regular descriptions
683 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
685 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}\+${fcdistro}:,,")
687 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}\-${fcdistro}:" | sed -e "s,${keyword}\-${fcdistro}:,,")
689 for i in $all $add; do
690 for exclude in $sub; do
691 [ "$i" = "$exclude" ] && continue 2
698 function pl_getPackages() { fcdistro=$1; shift ; pl_parsePkgs package $fcdistro "$@" ; }
699 function pl_getGroups() { fcdistro=$1; shift ; pl_parsePkgs group $fcdistro "$@" ; }
701 # locates a pldistro-dependant file
702 # tries first in build/<pldistro>/, then in build/planetlab/
703 function pl_locateDistroFile () {
710 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
711 for path in $paths; do
712 if [ -f $path ] ; then
717 if [ -z "$pkgspath" ] ; then
718 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
719 echo 1>&2 "candidates were $paths"
720 echo "not-found-by-pl_locateDistroFile"
723 echo 1>&2 "pl_locateDistroFile: using $pkgspath"
730 function yumgroups_from_pkgs () {
736 sedargs="-e s,@FCDISTRO@,$fcdistro,g"
739 <?xml version="1.0"?>
740 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
744 for pkgsname in $pkgsnames; do
745 pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
746 packages=$(pl_getPackages $fcdistro $pkgsfile)
748 groupname=$(pl_getPkgsAttribute groupname $pkgsfile | sed $sedargs)
749 groupdesc=$(pl_getPkgsAttribute groupdesc $pkgsfile | sed $sedargs)
751 if [ -z "$groupname" -o -z "$groupdesc" ] ; then
752 echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
756 cat << __group_header
758 <id>$(echo $groupname|tr A-Z a-z)</id>
759 <name>$groupname</name>
760 <default>true</default>
761 <description>$groupdesc</description>
762 <uservisible>false</uservisible>
765 for package in $packages; do
766 echo "<packagereq type=\"mandatory\">$package</packagereq>"
768 cat << __group_footer