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 pldistro pkgsfile(s)"
188 echo " -l url Fedora mirror location. Default is to try:"
189 for mirror in "${mirrors[@]}" ; do
192 echo " -v Be verbose"
193 echo " -h This message"
194 echo " target selection (defaults based on current build vserver)"
195 echo " -r release Fedora release number (default: $releasever)"
196 echo " -a arch Fedora architecture (default: $basearch)"
200 function pl_root_mkfedora () {
202 echo "* Entering pl_root_mkfedora" "$@"
204 if [ $UID -ne 0 ] ; then
205 echo "Error: You must run this script as root."
212 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
213 releasever=$pl_DISTRO_RELEASE
214 basearch=$pl_DISTRO_ARCH
215 [ -n "$pl_DISTRO_URL" ] && mirrors=($pl_DISTRO_URL)
218 while getopts "l:r:a:vh" opt ; do
221 if echo $OPTARG | grep -q -i '^\(file\|http[s]*\)://' ; then
224 # xxx rather use this as a .mirrors file, searched in config.pldistro/
225 # that would use the pkgs syntax with mirror:
226 #mirrors=($(pl_parsePkgs mirror <fcdistro> <pldistro> $(pl_locateDistroFile ../build/ <pldistro> $OPTARG.mirrors)))
227 mirrors=(file://$OPTARG)
246 shift $(($OPTIND - 1))
247 [[ "$#" -lt 3 ]] && mkfedora_usage
251 vroot=$(cd $vroot && pwd -P)
252 [ -d $vroot ] || mkfedora_usage
255 # parse pkgsfile and add to local vars
256 fcdistro=${pl_DISTRO_NAME}
257 pkgs_packages=$(pl_parsePkgs package $fcdistro $pldistro $pkgsfile)
258 pkgs_groups=$(pl_parsePkgs group $fcdistro $pldistro $pkgsfile)
259 # packages to exclude - obsolete, was maybe useful when installing a group
260 pkgs_excludes=$(pl_parsePkgs exclude $fcdistro $pldistro $pkgsfile)
261 pkgs_junk=$(pl_parsePkgs junk $fcdistro $pldistro $pkgsfile)
262 pkgs_precious=$(pl_parsePkgs precious $fcdistro $pldistro $pkgsfile)
263 # formerly related to mkfedora -k
264 pkgs_kexcludes=$(pl_parsePkgs kexclude $fcdistro $pldistro $pkgsfile)
267 [ -n "$pkgs_kexcludes" ] && exclude_kernel="exclude=$pkgs_excludes"
269 function mkfedora_fetch ()
271 curl --fail --silent --max-time 60 "$1"
274 # set list of attempted locations according to releasever
275 if [ $releasever -ge 7 ] ; then
277 linux/releases/$releasever/Everything/$basearch/os
278 $releasever/Everything/$basearch/os
279 core/$releasever/Everything/$basearch/os
280 linux/core/$releasever/$basearch/os
284 linux/core/$releasever/$basearch/os
285 core/$releasever/$basearch/os
286 $releasever/$basearch/os
290 echo "$0: candidate mirrors"
291 for mirror in "${mirrors[@]}" ; do
292 echo "* candidate mirror $mirror"
295 for mirror in "${mirrors[@]}" ; do
296 for attempt in $attempts; do
297 attempturl=$mirror/$attempt
298 if mkfedora_fetch $attempturl/repodata/repomd.xml >/dev/null ; then
305 if [ -z "$baseurl" ] ; then
306 echo "Error: $releasever/$basearch/os/repodata/repomd.xml"
307 echo " could not be found in any of the following locations:"
309 for mirror in ${mirrors[@]} ; do
310 for attempt in $attempts ; do
311 echo $mirror/$attempt
317 echo "* selecting mirror with baseurl=$baseurl"
320 # Do not tolerate errors
323 ## make rpms ignore installing stuff to special fs entries like /proc
324 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
325 # you have to use at least one language beside 'C'
326 # Prevent all locales from being installed in reference image
327 mkdir -p $vroot/etc/rpm
328 pl_root_rpm_macros > $vroot/etc/rpm/macros
330 # Trick rpm and yum, who read the real root /etc/rpm/macros file
331 # rather than the one installed in the reference image, despite what
332 # you might expect the --root and --installroot options to mean. Both
333 # programs always read $HOME/.rpmmacros.
334 export HOME=$vroot/tmp
336 pl_root_rpm_macros > $vroot/tmp/.rpmmacros
338 function mkfedora_cleanup () {
339 umount -l $vroot/proc
340 umount -l $vroot/dev/shm
341 umount -l $vroot/dev/pts
344 # Clean up before exiting if anything goes wrong
345 trap "mkfedora_cleanup" ERR INT
347 # Mount in reference image
348 mount -t devpts none $vroot/dev/pts
349 mount -t tmpfs none $vroot/dev/shm
351 mount -t proc none $vroot/proc
353 # Create a /var/lib dirs for yum & rpm
354 mkdir -p $vroot/var/lib/yum
355 mkdir -p $vroot/var/lib/rpm
356 mkdir -p $vroot/usr/share/info
358 # Create a dummy /etc/fstab in reference image
360 cat >$vroot/etc/fstab <<EOF
361 # This fake fstab exists only to please df and linuxconf.
362 /dev/hdv1 / ext2 defaults 1 1
364 cp $vroot/etc/fstab $vroot/etc/mtab
366 # Necessary for some scripts
367 mkdir -p $vroot/etc/sysconfig
368 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
370 # Initialize RPM database in reference image
371 mkdir -p $vroot/var/lib/rpm
372 rpm --root $vroot --initdb
373 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
375 # Initialize yum in reference image
376 mkdir -p $vroot/var/cache/yum $vroot/var/log
377 if [ $releasever -lt 7 ] ; then
383 cat >$vroot/etc/yum.conf <<EOF
385 cachedir=/var/cache/yum
387 logfile=/var/log/yum.log
389 distroverpkg=redhat-release
395 # Prevent yum-2.4 from loading additional repository definitions
396 # (e.g., from /etc/yum.repos.d/)
400 name=Fedora ${corename}${releasever} - $basearch - base
405 for optional in updates extras ; do
407 $mirror/linux/core/$optional/$releasever/$basearch \
408 $mirror/core/$optional/$releasever/$basearch \
409 $mirror/linux/$optional/$releasever/$basearch \
410 $mirror/$optional/$releasever/$basearch ; do
411 if mkfedora_fetch $optionalurl/repodata/repomd.xml ; then
412 cat >>$vroot/etc/yum.conf <<EOF
414 [$(basename $optional)]
415 name=Fedora ${corename}${releasever} - $basearch - $(basename $optional)
416 baseurl=$optionalurl/
424 # If we are being built as part of an automated RPM build, solve the
425 # bootstrap problem by including any just built packages in the yum
426 # configuration. This cooperates with the PlanetLab build system.
427 if [ -n "$RPM_BUILD_DIR" ] ; then
428 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
429 # If run under sudo, allow user to delete the headers/ and
430 # repodata/ directories.
431 if [ -n "$SUDO_USER" ] ; then
432 chown -R $SUDO_USER $RPM_RPMS_DIR
434 cat >>$vroot/etc/yum.conf <<EOF
437 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
438 baseurl=file://$RPM_RPMS_DIR/
443 for exclude in $pkgs_excludes; do
444 exclude_arg="$exclude_arg --exclude $exclude"
447 # glibc must be specified explicitly for the correct arch to be
449 echo "* Installing glibc"
450 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $exclude_arg install glibc
453 if [ -n "$pkgs_packages" ] ; then
454 echo "* Installing optional packages" $pkgs_packages
455 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $exclude_arg \
456 install $pkgs_packages
457 if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
458 echo "* Warning: Missing packages"
459 rpm --root $vroot -q $pkgs_packages | grep "not installed"
463 if [ -n "$pkgs_groups" ] ; then
464 ## call yum sequentially to get finer-grained info on dependencies
465 for grp in $pkgs_groups ; do
466 echo "* Installing optional group $grp"
467 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $exclude_arg \
472 # formerly in bootcd/prep.sh : to optimize footprint
473 if [ -n "$pkgs_junk" ] ; then
474 echo "* Removing unnecessary junk"
476 # Save precious files
477 [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
478 # Remove unnecessary junk
479 [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
480 # Restore precious files
481 [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
487 yum -c $vroot/etc/yum.conf --installroot=$vroot -y clean all
490 rm -f $vroot/var/lib/rpm/__db*
492 # Set time zone to UTC
493 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
494 rm -f $vroot/etc/localtime
495 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
498 # remove trap handler, as we are about to call it directly.
507 function pl_root_tune_image () {
510 # Disable all services in reference image
511 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
513 # FC2 minilogd starts up during shutdown and makes unmounting
514 # impossible. Just get rid of it.
515 rm -f $root/sbin/minilogd
516 ln -nsf /bin/true $root/sbin/minilogd
518 # This tells the Boot Manager that it is okay to update
519 # /etc/resolv.conf and /etc/hosts whenever the network configuration
520 # changes. Users are free to delete this file.
521 touch $root/etc/AUTO_UPDATE_NET_FILES
524 # Move specified directories out of a src tree into a dst tree, and
525 # then for each moved directory create a symlink in src to dst.
526 function pl_move_dirs() {
533 for datadir in "$@" ; do
534 mkdir -p ${data}${datadir}
535 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
536 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
538 rm -rf ${root}/${datadir}
539 mkdir -p $(dirname ${root}/${datadir})
540 ln -nsf ${store}/${datadir} ${root}/${datadir}
544 # Construct an image file from given some directory
545 # XXX in the future maybe use livecdtools?
546 function pl_make_image() {
551 # Leave about 100 MB free space and allow for about 20% inode overhead
552 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
554 blocks=$(($bytes / $bs))
555 dd bs=$bs count=$blocks if=/dev/zero of=$image
556 mkfs.ext3 -b $bs -j -F $image
558 # Temporarily mount it
559 tmp=$(mktemp -d tmp.XXXXXX)
560 mount -o loop $image $tmp
561 trap "umount $tmp; rmdir $tmp" ERR INT
564 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
572 # Fix permissions on tmp directories
573 function pl_fixtmp_permissions() {
575 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
578 function pl_fixdirs() {
581 for datadir in datadirs ; do
582 if [ -h ${root}/${datadir} ] ; then
583 rm -f ${root}/${datadir}
584 mkdir -p ${root}/${datadir}
589 ########## .pkgs format
590 # comments start with a # - this is needed only if you use a keyword in a comment
592 function pl_getPkgsAttribute () {
595 grep -v '^#' $file | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,"
598 # for a given keyword like 'package' :
599 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
601 # package: pkg1 .. pkgn
602 # package+f8: pkg1 .. pkgn
603 # package-f8: pkg1 .. pkgn
605 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
607 function pl_parsePkgs () {
612 # remaining arguments are paths to the pkgs files
614 # grab regular descriptions
615 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
617 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
619 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
621 for i in $all $add; do
622 for exclude in $sub; do
623 [ "$i" = "$exclude" ] && continue 2
625 echo "$i " | sed -e "s,@arch@,$pl_DISTRO_ARCH,g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
630 function pl_getPackages() { fcdistro=$1; shift ; pldistro=$1; shift ; pl_parsePkgs package $fcdistro $pldistro "$@" ; }
631 function pl_getGroups() { fcdistro=$1; shift ; pldistro=$1; shift ; pl_parsePkgs group $fcdistro $pldistro "$@" ; }
633 # locates a pldistro-dependant file
634 # tries first in build/<pldistro>/, then in build/planetlab/
635 function pl_locateDistroFile () {
642 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
643 for path in $paths; do
644 if [ -f $path ] ; then
649 if [ -z "$pkgspath" ] ; then
650 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
651 echo 1>&2 "candidates were $paths"
652 echo "not-found-by-pl_locateDistroFile"
655 echo 1>&2 "pl_locateDistroFile: using $pkgspath"
662 function yumgroups_from_pkgs () {
668 sedargs="-e s,@FCDISTRO@,$fcdistro,g"
671 <?xml version="1.0"?>
672 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
676 for pkgsname in $pkgsnames; do
677 pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
678 packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
680 groupname=$(pl_getPkgsAttribute groupname $pkgsfile | sed $sedargs)
681 groupdesc=$(pl_getPkgsAttribute groupdesc $pkgsfile | sed $sedargs)
683 if [ -z "$groupname" -o -z "$groupdesc" ] ; then
684 echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
688 cat << __group_header
690 <id>$(echo $groupname|tr A-Z a-z)</id>
691 <name>$groupname</name>
692 <default>true</default>
693 <description>$groupdesc</description>
694 <uservisible>false</uservisible>
697 for package in $packages; do
698 echo "<packagereq type=\"mandatory\">$package</packagereq>"
700 cat << __group_footer