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 fc4 or f7
37 function pl_getReleaseName () {
42 if [ "$release" -le 6 ] ; then
43 releasename=fc$release
55 releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
56 echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro"
64 # figure out which redhat distro we are using (fedora, centos, redhat)
65 pl_DISTRO=$(pl_getDistro)
67 # select basearch of the host devel environment - protected for macos for local tests
68 pl_DISTRO_ARCH=$(uname -i 2>/dev/null|| echo unknownarch)
70 # the release number (plain number)
71 pl_DISTRO_RELEASE=$(pl_getRelease)
73 # the release name - for vserver build - like fc4, f8 or centos4
74 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
76 # get path to appropriate yumgroups.xml file
77 # Thierry: quick & dirty improvement
78 # this file is updated by the toplevel build, from groups/<pldistro>.xml
79 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
81 function pl_process_fedora_options () {
84 while getopts "l:r:a:h" opt ; do
88 let shiftcount=$shiftcount+2
91 pl_DISTRO_RELEASE=$OPTARG
92 let shiftcount=$shiftcount+2
95 pl_DISTRO_ARCH=$OPTARG
96 let shiftcount=$shiftcount+2
99 echo "Usage: $0 [OPTION]..."
100 echo " -l url distro mirror location (default: $pl_DISTRO_URL)"
101 echo " -r release distro release number (default: $pl_DISTRO_RELEASE)"
102 echo " -a arch distro architecture (default: $pl_DISTRO_ARCH)"
103 echo "where distro can be either fedora, centos, or redhat"
104 echo " -h This message"
112 ######################################## handling a root image
113 function pl_root_rpm_macros () {
115 %_install_langs C:en_US:en
116 %_netsharedpath /proc:/dev/pts:/usr/share/info
118 %__file_context_path /dev/null
122 function pl_root_makedevs() {
124 # Clean ${vroot}/dev, but only when ${vroot}!=""
125 [ -n $vroot ] && rm -rf $vroot/dev
128 mknod -m 666 $vroot/dev/null c 1 3
129 mknod -m 666 $vroot/dev/zero c 1 5
130 mknod -m 666 $vroot/dev/full c 1 7
131 mknod -m 644 $vroot/dev/random c 1 8
132 mknod -m 644 $vroot/dev/urandom c 1 9
133 mknod -m 666 $vroot/dev/tty c 5 0
134 mknod -m 666 $vroot/dev/ptmx c 5 2
135 # For bash command substitution
136 ln -nsf ../proc/self/fd $vroot/dev/fd
138 # For df and linuxconf
139 touch $vroot/dev/hdv1
142 mkdir -p $vroot/dev/pts
144 # (Might have to remove the following for vserver-reference.)
147 mkdir -p $vroot/dev/shm
150 mkdir -p $vroot/dev/net
151 mknod -m 600 $vroot/dev/net/tun c 10 200
153 # For mkinitrd (in case a kernel is being installed)
154 # As well for loop back mounting within a vserver.
155 for i in $(seq 0 255) ; do
156 mknod -m 640 $vroot/dev/loop$i b 7 $i
160 function mkfedora_usage() {
161 echo "Usage: pl_root_mkfedora [OPTION]... basedir pldistro pkgsfile(s)"
162 echo " -l url Fedora mirror location."
163 echo " Defaults are searched in <pldistro>.mirrors"
164 echo " -v Be verbose"
165 echo " -h This message"
166 echo " target selection (defaults based on current build vserver)"
167 echo " -r release Fedora release number (default: $releasever)"
168 echo " -a arch Fedora architecture (default: $basearch)"
172 function pl_root_mkfedora () {
174 echo "* Entering pl_root_mkfedora" "$@"
176 if [ $UID -ne 0 ] ; then
177 echo "Error: You must run this script as root."
184 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
185 releasever=$pl_DISTRO_RELEASE
186 basearch=$pl_DISTRO_ARCH
190 ### while getopts "l:r:a:vh" opt ; do
191 while getopts "vh" opt ; do
193 ### l) mirrors="$mirrors $OPTARG" ;;
194 ### r) releasever=$OPTARG ;;
195 ### a) basearch=$OPTARG ;;
196 v) verbose=1; set -x ;;
197 h|*) mkfedora_usage ;;
201 shift $(($OPTIND - 1))
202 [[ "$#" -lt 3 ]] && mkfedora_usage
206 vroot=$(cd $vroot && pwd -P)
207 [ -d $vroot ] || mkfedora_usage
210 # parse pkgsfile and add to local vars
211 fcdistro=${pl_DISTRO_NAME}
212 pkgs_packages=$(pl_parsePkgs package $fcdistro $pldistro $pkgsfile)
213 pkgs_groups=$(pl_parsePkgs group $fcdistro $pldistro $pkgsfile)
214 # packages to exclude - obsolete, was maybe useful when installing a group
215 pkgs_excludes=$(pl_parsePkgs exclude $fcdistro $pldistro $pkgsfile)
216 pkgs_junk=$(pl_parsePkgs junk $fcdistro $pldistro $pkgsfile)
217 pkgs_precious=$(pl_parsePkgs precious $fcdistro $pldistro $pkgsfile)
218 # formerly related to mkfedora -k
219 pkgs_kexcludes=$(pl_parsePkgs kexclude $fcdistro $pldistro $pkgsfile)
220 # get mirrors if not specified with -l
221 if [ -z "$mirrors" ] ; then
222 mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
223 mirrors=$(pl_parsePkgs mirror $fcdistro $pldistro $mirrorsfile)
227 # add them manually as the output of pl_parsePkgs is line-separated
228 if [ -n "$pkgs_kexcludes" ] ; then
229 exclude_line="exclude="
230 for kexclude in $pkgs_kexcludes ; do
231 exclude_line="$exclude_line $kexclude"
235 echo "$0: candidate mirrors"
236 for mirror in $mirrors ; do
237 echo "* candidate mirror $mirror"
240 # the repo part of the final yum.conf
241 yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx
242 if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$exclude_line" $mirrors ; then
243 echo xxx -- error ; return 1
246 public_gpg_key=$(yumconf_gpgkey $yum_conf_repos)
248 # Do not tolerate errors
251 ## make rpms ignore installing stuff to special fs entries like /proc
252 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
253 # you have to use at least one language beside 'C'
254 # Prevent all locales from being installed in reference image
255 mkdir -p $vroot/etc/rpm
256 pl_root_rpm_macros > $vroot/etc/rpm/macros
258 # Trick rpm and yum, who read the real root /etc/rpm/macros file
259 # rather than the one installed in the reference image, despite what
260 # you might expect the --root and --installroot options to mean. Both
261 # programs always read $HOME/.rpmmacros.
262 export HOME=$vroot/tmp
264 pl_root_rpm_macros > $vroot/tmp/.rpmmacros
266 function mkfedora_cleanup () {
267 umount -l $vroot/proc
268 umount -l $vroot/dev/shm
269 umount -l $vroot/dev/pts
272 # Clean up before exiting if anything goes wrong
273 trap "mkfedora_cleanup" ERR INT
275 # Mount in reference image
276 mount -t devpts none $vroot/dev/pts
277 mount -t tmpfs none $vroot/dev/shm
279 mount -t proc none $vroot/proc
281 # Create a /var/lib dirs for yum & rpm
282 mkdir -p $vroot/var/lib/yum
283 mkdir -p $vroot/var/lib/rpm
284 mkdir -p $vroot/usr/share/info
286 # Create a dummy /etc/fstab in reference image
288 cat >$vroot/etc/fstab <<EOF
289 # This fake fstab exists only to please df and linuxconf.
290 /dev/hdv1 / ext2 defaults 1 1
292 cp $vroot/etc/fstab $vroot/etc/mtab
294 # Necessary for some scripts
295 mkdir -p $vroot/etc/sysconfig
296 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
298 # Initialize RPM database in reference image
299 mkdir -p $vroot/var/lib/rpm
300 rpm --root $vroot --initdb
301 rpm --root $vroot --import $public_gpg_key
303 # Initialize yum in reference image
304 mkdir -p $vroot/var/cache/yum $vroot/var/log
306 # yum.conf is for building only - store in different location than /etc/yum.conf
307 yum_conf=$vroot/etc/mkfedora-yum.conf
308 cat > $yum_conf <<EOF
310 cachedir=/var/cache/yum
312 logfile=/var/log/yum.log
314 distroverpkg=redhat-release
320 # Prevent yum-2.4 from loading additional repository definitions
321 # (e.g., from /etc/yum.repos.d/)
325 cat $yum_conf_repos >> $yum_conf
327 # If we are being built as part of an automated RPM build, solve the
328 # bootstrap problem by including any just built packages in the yum
329 # configuration. This cooperates with the PlanetLab build system.
330 if [ -n "$RPM_BUILD_DIR" ] ; then
331 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
332 # If run under sudo, allow user to delete the headers/ and
333 # repodata/ directories.
334 if [ -n "$SUDO_USER" ] ; then
335 chown -R $SUDO_USER $RPM_RPMS_DIR
337 cat >> $yum_conf <<EOF
340 name=Building - $basearch - $RPM_RPMS_DIR/
341 baseurl=file://$RPM_RPMS_DIR/
345 echo "========== Dumping $yum_conf"
347 echo "========== EndDump $yum_conf"
350 yum --help | grep verbose &> /dev/null && yum_options="$yum_options --verbose"
351 yum_options="$yum_options -y"
352 yum_options="$yum_options -c $yum_conf"
353 yum_options="$yum_options --installroot=$vroot"
356 for exclude in $pkgs_excludes; do
357 exclude_arg="$exclude_arg --exclude $exclude"
360 # glibc must be specified explicitly for the correct arch to be
362 echo "* Installing glibc"
363 yum $yum_options $exclude_arg install glibc
366 if [ -n "$pkgs_packages" ] ; then
367 echo "* Installing optional packages" $pkgs_packages
368 yum $yum_options $exclude_arg install $pkgs_packages
369 if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
370 echo "* Warning: Missing packages"
371 rpm --root $vroot -q $pkgs_packages | grep "not installed"
375 if [ -n "$pkgs_groups" ] ; then
376 ## call yum sequentially to get finer-grained info on dependencies
377 for grp in $pkgs_groups ; do
378 echo "* Installing optional group $grp"
379 yum $yum_options $exclude_arg groupinstall "$grp"
383 # formerly in bootcd/prep.sh : to optimize footprint
384 if [ -n "$pkgs_junk" ] ; then
385 echo "* Removing unnecessary junk"
387 # Save precious files
388 [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
389 # Remove unnecessary junk
390 [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
391 # Restore precious files
392 [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
398 yum $yum_options clean all
401 rm -f $vroot/var/lib/rpm/__db*
403 # Set time zone to UTC
404 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
405 rm -f $vroot/etc/localtime
406 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
409 echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt"
410 chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt
412 # remove trap handler, as we are about to call it directly.
421 function pl_root_tune_image () {
424 # Disable all services in reference image
425 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
427 # FC2 minilogd starts up during shutdown and makes unmounting
428 # impossible. Just get rid of it.
429 rm -f $root/sbin/minilogd
430 ln -nsf /bin/true $root/sbin/minilogd
432 # This tells the Boot Manager that it is okay to update
433 # /etc/resolv.conf and /etc/hosts whenever the network configuration
434 # changes. Users are free to delete this file.
435 touch $root/etc/AUTO_UPDATE_NET_FILES
438 # Move specified directories out of a src tree into a dst tree, and
439 # then for each moved directory create a symlink in src to dst.
440 function pl_move_dirs() {
447 for datadir in "$@" ; do
448 mkdir -p ${data}${datadir}
449 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
450 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
452 rm -rf ${root}/${datadir}
453 mkdir -p $(dirname ${root}/${datadir})
454 ln -nsf ${store}/${datadir} ${root}/${datadir}
458 # Construct an image file from given some directory
459 # XXX in the future maybe use livecdtools?
460 function pl_make_image() {
465 # Leave about 100 MB free space and allow for about 20% inode overhead
466 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
468 blocks=$(($bytes / $bs))
469 dd bs=$bs count=$blocks if=/dev/zero of=$image
470 mkfs.ext3 -b $bs -j -F $image
472 # Temporarily mount it
473 tmp=$(mktemp -d tmp.XXXXXX)
474 mount -o loop $image $tmp
475 trap "umount $tmp; rmdir $tmp" ERR INT
478 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
486 # Fix permissions on tmp directories
487 function pl_fixtmp_permissions() {
489 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
492 function pl_fixdirs() {
495 for datadir in datadirs ; do
496 if [ -h ${root}/${datadir} ] ; then
497 rm -f ${root}/${datadir}
498 mkdir -p ${root}/${datadir}
503 ########## .pkgs format
504 # comments start with a # - this is needed only if you use a keyword in a comment
506 function pl_getPkgsAttribute () {
509 # remove any initial white spaces from the result
510 grep -v '^#' $file | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,," -e "s,^[[:space:]][[:space:]]*,,"
513 # for a given keyword like 'package' :
514 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
516 # package: pkg1 .. pkgn
517 # package+f8: pkg1 .. pkgn
518 # package-f8: pkg1 .. pkgn
520 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
522 function pl_parsePkgs () {
527 # remaining arguments are paths to the pkgs files
529 # grab regular descriptions
530 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
532 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
534 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
536 for i in $all $add; do
537 for exclude in $sub; do
538 [ "$i" = "$exclude" ] && continue 2
540 echo "$i " | sed -e "s,@arch@,$pl_DISTRO_ARCH,g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
545 function pl_getPackages() { fcdistro=$1; shift ; pldistro=$1; shift ; pl_parsePkgs package $fcdistro $pldistro "$@" ; }
546 function pl_getGroups() { fcdistro=$1; shift ; pldistro=$1; shift ; pl_parsePkgs group $fcdistro $pldistro "$@" ; }
548 # locates a pldistro-dependant file
549 # tries first in build/<pldistro>/, then in build/planetlab/
550 function pl_locateDistroFile () {
557 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
558 for path in $paths; do
559 if [ -f $path ] ; then
564 if [ -z "$pkgspath" ] ; then
565 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
566 echo 1>&2 "candidates were $paths"
567 echo "not-found-by-pl_locateDistroFile"
570 echo 1>&2 "pl_locateDistroFile: using $pkgspath"
576 function yumgroups_from_pkgs () {
582 sedargs="-e s,@FCDISTRO@,$fcdistro,g"
585 <?xml version="1.0"?>
586 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
590 for pkgsname in $pkgsnames; do
591 pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
592 packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
594 groupname=$(pl_getPkgsAttribute groupname $pkgsfile | sed $sedargs)
595 groupdesc=$(pl_getPkgsAttribute groupdesc $pkgsfile | sed $sedargs)
597 if [ -z "$groupname" -o -z "$groupdesc" ] ; then
598 echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
602 cat << __group_header
604 <id>$(echo $groupname|tr A-Z a-z)</id>
605 <name>$groupname</name>
606 <description>$groupdesc</description>
607 <uservisible>true</uservisible>
610 for package in $packages; do
611 echo "<packagereq type=\"mandatory\">$package</packagereq>"
613 cat << __group_footer
624 function build_fetch () {
625 curl --fail --silent --max-time 60 --output /dev/null "$1"
628 # tries to compute a valid yum.conf for that pldistro from the template in mirroring/
629 # returns 0 and writes <dest_yumconf> on success
630 # returns 1 on failure, in which case <dest_yumconf> is deleted
631 function yumconf_mirrors () {
632 dest_yumconf=$1; shift
635 exclude_line="$1" ; shift
638 template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
640 if [ ! -f $template ] ; then
641 echo "yumconf_mirrors: cannot locate template $template"
646 for mirror in $mirrors; do
647 if yumconf_mirror $dest_yumconf $template "$exclude_line" $mirror; then
655 # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
656 # returns 0 on success and 1 on failure
657 function yumconf_mirror () {
658 dest_yumconf=$1; shift
660 exclude_line="$1" ; shift
663 sed -e "s,@MIRRORURL@,$mirror,g" \
665 $exclude_line" $template > $dest_yumconf
667 # capture all lines defining baseurl
668 baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
669 if [ -z "$baseurl_defs" ] ; then
673 for baseurl_def in $baseurl_defs; do
674 baseurl=$(echo $baseurl_def | sed \
676 -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
677 repomd=$baseurl/repodata/repomd.xml
679 echo "* Trying to fetch $repomd"
680 if ! build_fetch $repomd ; then
681 echo "* Failed to fetch $repomd"
685 echo "* Selected mirror $mirror"
689 # from a yum.conf as generated above, computes the (first) gpgkey url
690 function yumconf_gpgkey () {
691 dest_yumconf=$1; shift
693 first_line=$(grep '^gpgkey=' $dest_yumconf | head -1)
694 values=$(echo $first_line | sed -e s,gpgkey=,,)
695 value=$(echo $values | awk '{print $1;}' | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
696 [ -n "$value" ] || return 1