3 # Common functions for build scripts used by various packages
4 # incorporated (e.g., build, bootcd, nodeimage, sliceimage)
6 # Marc E. Fiuczynski <mef@cs.princeton.edu>
7 # Copyright (C) 2007 The Trustees of Princeton University
8 # Thierry Parmentelat <thierry.parmentelat@inria.fr> INRIA
10 # supported distros f12 f14 f16 f18 f20
11 # and debians/ubuntus to a lesser extent
13 # for locating pkgs.py
16 # returns 'Fedora' or 'CentOS' for now
17 function pl_getDistro() {
18 if [ -f "/etc/redhat-release" ] ; then
19 distro=$(awk ' { print $1 } ' /etc/redhat-release)
20 case $distro in Scientific*) distro="SL" ; esac
21 elif [ -f /etc/lsb-release ] ; then
23 echo $DISTRIB_CODENAME
24 elif [ -f /etc/debian_version ] ; then
25 case $(cat /etc/debian_version) in
26 6.*) distro=squeeze ;;
28 *) distro=unknown.debian.in.build.common ;;
31 [ -z "$distro" ] && { echo "build.common.pl_getDistro-unknown"; exit 1; }
36 # returns something like 8, 10, or 5.3
37 function pl_getRelease() {
38 if [ -f "/etc/redhat-release" ] ; then
39 release=$(awk ' { if ($1=="Fedora" && $2=="Core") print $4 ; if (($1=="Fedora" && $2!="Core")||$1=="CentOS") print $3 ; if ($1=="Scientific") print $4 } ' /etc/redhat-release)
41 echo "build.common.pl_getRelease-unknown"
44 # keep only the major number
45 echo "$release" | cut -d. -f1
49 # returns distro shortname, something like 'f8' or 'centos5'
50 function pl_getReleaseName () {
65 releasename=sl$release
67 squeeze|wheezy|oneiric|precise|quantal|raring|saucy)
71 releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
72 echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro"
80 # yum exclusions are now defined in yumexclude.pkgs
81 # so they can now depend both on the linux distro and the pl distro
82 function pl_yumexclude () {
87 # search for file "yumexclude.pkgs"
88 yumexclude_file=$(pl_locateDistroFile $builddir $pldistro "yumexclude.pkgs")
90 # check if pkgs.py is in PATH
91 type -p pkgs.py >& /dev/null || export PATH=$builddir:$PATH
94 pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro $keyword $yumexclude_file || \
95 { echo pl_yumexclude failed with fcdistro=$fcdistro and pldistro=$pldistro; return 1 ; }
98 # <> fcdistro pldistro builddir
99 # node side : use the 'nodeyumexclude' keywork in yumexclude.pkgs
100 function pl_nodeyumexclude () { pl_yumexclude 'nodeyumexclude' "$@" ; }
101 # server side : use the 'plcyumexclude' keywork in yumexclude.pkgs
102 function pl_plcyumexclude () { pl_yumexclude 'plcyumexclude' "$@" ; }
105 # figure out which redhat distro we are using (fedora, centos, redhat)
106 pl_DISTRO=$(pl_getDistro)
108 # select basearch of the host devel environment - protected for macos for local tests
109 # try arch for testing stuff on a mac
110 pl_DISTRO_ARCH=$(uname -i 2>/dev/null || arch 2> /dev/null || echo unknownarch)
112 # the release number (plain number)
113 pl_DISTRO_RELEASE=$(pl_getRelease)
115 # the release name - something like 'f8' or 'sl6'
116 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
118 # get path to appropriate yumgroups.xml file
119 # Thierry: quick & dirty improvement
120 # this file is updated by the toplevel build, from the .pkgs files
121 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
123 function pl_process_fedora_options () {
126 while getopts "l:r:a:h" opt ; do
129 pl_DISTRO_URL=$OPTARG
130 let shiftcount=$shiftcount+2
133 pl_DISTRO_RELEASE=$OPTARG
134 let shiftcount=$shiftcount+2
137 pl_DISTRO_ARCH=$OPTARG
138 let shiftcount=$shiftcount+2
141 echo "Usage: $0 [OPTION]..."
142 echo " -l url distro mirror location (default: $pl_DISTRO_URL)"
143 echo " -r release distro release number (default: $pl_DISTRO_RELEASE)"
144 echo " -a arch distro architecture (default: $pl_DISTRO_ARCH)"
145 echo "where distro can be either fedora, centos, or redhat"
146 echo " -h This message"
154 ######################################## handling a root image
155 function pl_root_rpm_macros () {
157 %_install_langs C:en_US:en
158 %_netsharedpath /proc:/dev/pts:/usr/share/info
160 %__file_context_path /dev/null
164 function pl_root_makedevs() {
166 # Clean ${vroot}/dev, but only when ${vroot}!=""
167 [ -n $vroot ] && rm -rf $vroot/dev
170 mknod -m 666 $vroot/dev/null c 1 3
171 mknod -m 666 $vroot/dev/zero c 1 5
172 mknod -m 666 $vroot/dev/full c 1 7
173 mknod -m 644 $vroot/dev/random c 1 8
174 mknod -m 644 $vroot/dev/urandom c 1 9
175 mknod -m 666 $vroot/dev/tty c 5 0
176 mknod -m 666 $vroot/dev/ptmx c 5 2
177 # For bash command substitution
178 ln -nsf ../proc/self/fd $vroot/dev/fd
180 # For df and linuxconf
181 touch $vroot/dev/hdv1
184 mkdir -p $vroot/dev/pts
187 mkdir -p $vroot/dev/shm
190 mkdir -p $vroot/dev/net
191 mknod -m 600 $vroot/dev/net/tun c 10 200
193 # For mkinitrd (in case a kernel is being installed)
194 # As well as for loop back mounting within a vm.
195 for i in $(seq 0 255) ; do
196 mknod -m 640 $vroot/dev/loop$i b 7 $i
200 function mkfedora_usage() {
201 echo "Usage: pl_root_mkfedora [OPTION]... basedir pldistro pkgsfile(s)"
202 echo " -l url Fedora mirror location."
203 echo " Defaults are searched in <pldistro>.mirrors"
204 echo " -v Be verbose"
205 echo " -h This message"
206 echo " target selection (defaults based on current build VM context)"
207 echo " -r release Fedora release number (default: $releasever)"
208 echo " -a arch Fedora architecture (default: $basearch)"
212 function pl_root_mkfedora () {
214 echo "* Entering pl_root_mkfedora" "$@"
216 if [ $UID -ne 0 ] ; then
217 echo "Error: You must run this script as root."
224 # Release and architecture to install : defaults to current vm settings or previously parsed fedora_options
225 releasever=$pl_DISTRO_RELEASE
226 basearch=$pl_DISTRO_ARCH
229 while getopts "vh" opt ; do
231 v) verbose=1; set -x ;;
232 h|*) mkfedora_usage ;;
236 shift $(($OPTIND - 1))
237 [[ "$#" -lt 3 ]] && mkfedora_usage
241 vroot=$(cd $vroot && pwd -P)
242 [ -d $vroot ] || mkfedora_usage
245 # parse pkgsfile and add to local vars
246 fcdistro=${pl_DISTRO_NAME}
247 pkgs_packages=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro package $pkgsfile)
248 pkgs_groups=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro group $pkgsfile)
249 # what can get trashed to save space
250 pkgs_junk=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro junk $pkgsfile)
252 pkgs_precious=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro precious $pkgsfile)
253 # formerly related to mkfedora -k : packages to take from our own build
254 # and thus need be excluded frem the stock repos
255 # locate builddir by looking for pkgs.py
256 builddir=$(dirname $(type -p pkgs.py))
257 SUBST_NODEYUMEXCLUDE=$(pl_nodeyumexclude $fcdistro $pldistro $builddir)
258 pkgs_yumexclude=$(pkgs.py -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro yumexclude $pkgsfile | sed -e s,@NODEYUMEXCLUDE@,"$SUBST_NODEYUMEXCLUDE",)
259 # get mirrors if not specified with -l
260 if [ -z "$mirrors" ] ; then
261 mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
262 # do not sort mirrors, keep order from input
263 mirrors=$(pkgs.py -u -a $pl_DISTRO_ARCH -f $fcdistro -d $pldistro mirror $mirrorsfile)
267 [ -n "$pkgs_yumexclude" ] && yumexclude_line="exclude=$pkgs_yumexclude"
269 echo "$0: candidate mirrors"
270 for mirror in $mirrors ; do
271 echo "* candidate mirror $mirror"
274 # the repo part of the final yum.conf
275 yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx
276 if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$yumexclude_line" $mirrors ; then
277 echo xxx -- error ; return 1
280 # Do not tolerate errors
283 public_gpg_key=$(yumconf_gpgkey $yum_conf_repos)
285 ## make rpms ignore installing stuff to special fs entries like /proc
286 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
287 # you have to use at least one language beside 'C'
288 # Prevent all locales from being installed in reference image
289 mkdir -p $vroot/etc/rpm
290 pl_root_rpm_macros > $vroot/etc/rpm/macros
292 # Trick rpm and yum, who read the real root /etc/rpm/macros file
293 # rather than the one installed in the reference image, despite what
294 # you might expect the --root and --installroot options to mean. Both
295 # programs always read $HOME/.rpmmacros.
296 export HOME=$vroot/tmp
298 pl_root_rpm_macros > $vroot/tmp/.rpmmacros
300 function mkfedora_cleanup () {
301 umount -l $vroot/proc
302 umount -l $vroot/dev/shm
303 umount -l $vroot/dev/pts
306 # Clean up before exiting if anything goes wrong
307 trap "mkfedora_cleanup" ERR INT
309 # Mount in reference image
310 mount -t devpts none $vroot/dev/pts
311 mount -t tmpfs none $vroot/dev/shm
313 mount -t proc none $vroot/proc
315 # Create a /var/lib dirs for yum & rpm
316 mkdir -p $vroot/var/lib/yum
317 mkdir -p $vroot/var/lib/rpm
318 mkdir -p $vroot/usr/share/info
320 # Create a dummy /etc/fstab in reference image
322 cat >$vroot/etc/fstab <<EOF
323 # This fake fstab exists only to please df and linuxconf.
324 /dev/hdv1 / ext2 defaults 1 1
326 cp $vroot/etc/fstab $vroot/etc/mtab
328 # Necessary for some scripts
329 mkdir -p $vroot/etc/sysconfig
330 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
332 # Initialize RPM database in reference image
333 mkdir -p $vroot/var/lib/rpm
334 rpm --root $vroot --initdb
335 rpm --root $vroot --import $public_gpg_key
337 # Initialize yum in reference image
338 mkdir -p $vroot/var/cache/yum $vroot/var/log
340 # yum.conf is for building only - store in different location than /etc/yum.conf
341 yum_conf=$vroot/etc/mkfedora-yum.conf
342 cat > $yum_conf <<EOF
344 cachedir=/var/cache/yum
346 logfile=/var/log/yum.log
349 distroverpkg=redhat-release
355 # Prevent yum-2.4 from loading additional repository definitions
356 # (e.g., from /etc/yum.repos.d/)
360 cat $yum_conf_repos >> $yum_conf
362 # If we are being built as part of an automated RPM build, solve the
363 # bootstrap problem by including any just built packages in the yum
364 # configuration. This cooperates with the PlanetLab build system.
365 if [ -n "$RPM_BUILD_DIR" ] ; then
366 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
367 # If run under sudo, allow user to delete the headers/ and
368 # repodata/ directories.
369 if [ -n "$SUDO_USER" ] ; then
370 chown -R $SUDO_USER $RPM_RPMS_DIR
372 cat >> $yum_conf <<EOF
375 name=Building - $basearch - $RPM_RPMS_DIR/
376 baseurl=file://$RPM_RPMS_DIR/
380 echo "========== Dumping $yum_conf"
382 echo "========== EndDump $yum_conf"
385 # yum --help | grep verbose &> /dev/null && yum_options="$yum_options --verbose"
386 yum_options="$yum_options -y"
387 yum_options="$yum_options -c $yum_conf"
388 yum_options="$yum_options --installroot=$vroot"
390 # glibc must be specified explicitly for the correct arch to be
392 echo "* Installing glibc"
393 # ignore yum's return code that is basically undefined
394 yum $yum_options install glibc || :
397 if [ -n "$pkgs_packages" ] ; then
398 echo "* Installing optional packages" $pkgs_packages
399 # ignore yum's return code that is basically undefined
400 echo "* Install options" $vroot $yum_options
401 yum $yum_options install $pkgs_packages || :
402 if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
403 echo "* Warning: Missing packages"
404 rpm --root $vroot -q $pkgs_packages | grep "not installed"
408 if [ -n "$pkgs_groups" ] ; then
409 ## call yum sequentially to get finer-grained info on dependencies
410 for group_plus in $pkgs_groups ; do
411 group=$(echo $group_plus | sed -e "s,+++, ,g")
412 echo "* Installing optional group $group"
413 # ignore yum's return code that is basically undefined
414 yum $yum_options groupinstall "$group" || :
418 # formerly in bootcd/prep.sh : to optimize footprint
419 if [ -n "$pkgs_junk" ] ; then
420 echo "* Removing unnecessary junk"
422 # Save precious files
423 [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
424 # Remove unnecessary junk
425 [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
426 # Restore precious files
427 [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
434 # NOTE: this hack is for Fedora >= 12.
435 # if kernel-debug is installed, clean it up
436 # we link to our version of kernel/initrd and clean up
437 # kernel-debug manually
438 if rpm --root $vroot --quiet -q kernel-debug ; then
439 echo "* Cleaning up kernel-debug - (workaround for f12)"
441 rm -rf kernel-boot kernel-bootsmp initrd-boot initrd-bootsmp
442 ln -s vmlinuz-*${pldistro}* kernel-boot
443 ln -s vmlinuz-*${pldistro}* kernel-bootsmp
444 ln -s initrd-*${pldistro}* initrd-boot
445 ln -s initrd-*${pldistro}* initrd-bootsmp
446 rpm --root $vroot --nodeps -e kernel-debug || :
450 # ignore yum's return code that is basically undefined
451 yum $yum_options clean all || :
454 rm -f $vroot/var/lib/rpm/__db*
456 # Set time zone to UTC
457 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
458 rm -f $vroot/etc/localtime
459 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
462 echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt"
463 chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt
465 # remove trap handler, as we are about to call it directly.
474 function pl_root_tune_image () {
477 # Disable all services in reference image
478 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
480 # FC2 minilogd starts up during shutdown and makes unmounting
481 # impossible. Just get rid of it.
482 rm -f $root/sbin/minilogd
483 ln -nsf /bin/true $root/sbin/minilogd
485 # This tells the Boot Manager that it is okay to update
486 # /etc/resolv.conf and /etc/hosts whenever the network configuration
487 # changes. Users are free to delete this file.
488 touch $root/etc/AUTO_UPDATE_NET_FILES
491 # Move specified directories out of a src tree into a dst tree, and
492 # then for each moved directory create a symlink in src to dst.
493 function pl_move_dirs() {
500 for datadir in "$@" ; do
501 mkdir -p ${data}${datadir}
502 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
503 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
505 rm -rf ${root}/${datadir}
506 mkdir -p $(dirname ${root}/${datadir})
507 ln -nsf ${store}/${datadir} ${root}/${datadir}
511 # Construct an image file from given some directory
512 # XXX in the future maybe use livecdtools?
513 function pl_make_image() {
518 # Leave about 100 MB free space and allow for about 20% inode overhead
519 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
521 blocks=$(($bytes / $bs))
522 dd bs=$bs count=$blocks if=/dev/zero of=$image
523 mkfs.ext3 -b $bs -j -F $image
525 # Temporarily mount it
526 tmp=$(mktemp -d tmp.XXXXXX)
527 mount -o loop $image $tmp
528 trap "umount $tmp; rmdir $tmp" ERR INT
531 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
539 # Fix permissions on tmp directories
540 function pl_fixtmp_permissions() {
542 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
545 function pl_fixdirs() {
548 for datadir in datadirs ; do
549 if [ -h ${root}/${datadir} ] ; then
550 rm -f ${root}/${datadir}
551 mkdir -p ${root}/${datadir}
556 ########## .pkgs format
557 # Usage: pl_parsePkgs keyword [-a arch] fcdistro pldistro pkgs-file[..s]
558 # pkgs.py should be found in PATH, like this file build.common
559 # only usage should be for pl_getPackages and pl_getGroups,
560 # which in turn are usednow be in {node,slice}image/build.sh
561 function pl_parsePkgs () {
562 target_arch=$pl_DISTRO_ARCH
564 [ "$1" == "-a" ] && { shift; target_arch="$1"; shift; }
568 echo 1>&2 "pl_parsePkgs: using -a $target_arch -f $fcdistro -d $pldistro $keyword $@"
569 pkgs.py -a $target_arch -f $fcdistro -d $pldistro $keyword "$@"
571 # usage: pl_getPackages [-a arch] fcdistro pldistro pkg-file[..s]
572 function pl_getPackages() { pl_parsePkgs package "$@" ; }
573 function pl_getGroups() { pl_parsePkgs group "$@" ; }
575 ##############################
577 # locates a pldistro-dependant file
578 # tries first in build/<pldistro>/, then in build/planetlab/
579 function pl_locateDistroFile () {
585 # if config dir is missing but a .svnpath or a .gitpath exists, use it to extract the config dir
586 configdir="$builddir/config.${pldistro}"
587 if [ ! -d $configdir ] ; then
588 if [ -f "${configdir}.svnpath" -o -f "${configdir}.gitpath" ] ; then
589 echo 1>&2 "Invoking make to extract remote config.${pldistro}"
590 # we set PLDISTROTAGS here to /dev/null because when dealing with remote distros
591 # at a very early stage (like searching for devel.pkgs even before the build VM is created)
592 # then make screams because it cannot find a mandatory include file
593 # OTOH this mechanism here is not intended to depend on tags specifically
594 make 1>&2 --no-print-directory -C $builddir stage1=true config.${pldistro} PLDISTROTAGS=/dev/null
598 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
599 for path in $paths; do
600 if [ -f $path ] ; then
605 if [ -z "$pkgspath" ] ; then
606 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
607 echo 1>&2 "candidates were $paths"
608 echo "not-found-by-pl_locateDistroFile"
611 echo 1>&2 "pl_locateDistroFile: using $pkgspath"
617 function yumgroups_from_pkgs () {
623 sedargs="-e s,@FCDISTRO@,$fcdistro,g"
626 <?xml version="1.0"?>
627 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
631 for pkgsname in $pkgsnames; do
632 pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
633 packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
635 groupname=$(pkgs.py groupname $pkgsfile | sed $sedargs)
636 groupdesc=$(pkgs.py groupdesc $pkgsfile | sed $sedargs)
638 if [ -z "$groupname" -o -z "$groupdesc" ] ; then
639 echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
643 cat << __group_header
645 <id>$(echo $groupname|tr A-Z a-z)</id>
646 <name>$groupname</name>
647 <description>$groupdesc</description>
648 <uservisible>true</uservisible>
651 for package in $packages; do
652 echo "<packagereq type=\"mandatory\">$package</packagereq>"
654 cat << __group_footer
665 function build_fetch () {
666 curl --fail --silent --max-time 60 --output /dev/null "$1"
669 # tries to compute a valid yum.conf for that pldistro from the template in mirroring/
670 # returns 0 and writes <dest_yumconf> on success
671 # returns 1 on failure, in which case <dest_yumconf> is deleted
672 function yumconf_mirrors () {
673 dest_yumconf=$1; shift
676 yumexclude_line="$1" ; shift
679 template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
681 if [ ! -f $template ] ; then
682 echo "yumconf_mirrors: cannot locate template $template"
687 for mirror in $mirrors; do
688 if yumconf_mirror $dest_yumconf $template "$yumexclude_line" $mirror; then
692 echo 'yumconf_mirrors in build.common : ran out of mirrors -- BAILING OUT'
697 # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
698 # returns 0 on success and 1 on failure
699 function yumconf_mirror () {
700 dest_yumconf=$1; shift
702 yumexclude_line="$1" ; shift
705 sed -e "s,@MIRRORURL@,$mirror,g" \
707 $yumexclude_line" $template > $dest_yumconf
709 # capture all lines defining baseurl
710 baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
711 if [ -z "$baseurl_defs" ] ; then
715 for baseurl_def in $baseurl_defs; do
716 baseurl=$(echo $baseurl_def | sed \
718 -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
719 repomd=$baseurl/repodata/repomd.xml
721 echo "* Trying to fetch $repomd"
722 if ! build_fetch $repomd ; then
723 echo "* Failed to fetch $repomd"
727 echo "* Selected mirror $mirror"
731 # from a yum.conf as generated above, computes the gpgkey urls
732 function yumconf_gpgkey () {
733 dest_yumconf=$1; shift
735 values=$(grep -h '^gpgkey=' $dest_yumconf | sed -e s,gpgkey=,, | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g | sed -e 's, ,\n,g' | sort | uniq | xargs)
736 [ -n "$values" ] || return 1
741 # patches a yum conf to insert an exclude line in each declared repo
742 function yumconf_exclude () {
744 yumexclude_line="$1" ; shift
746 sed -i -e "/#baseurl=.*$/i\\
747 $yumexclude_line" $repo