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 # the release number (plain number)
83 pl_DISTRO_RELEASE=$(pl_getRelease)
85 # the release name - for vserver build - like fc4, f8 or centos4
86 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
88 # get path to appropriate yumgroups.xml file
89 # Thierry: quick & dirty improvement
90 # this file is updated by the toplevel build, from groups/<pldistro>.xml
91 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
93 function pl_process_fedora_options () {
96 while getopts "l:r:a:h" opt ; do
100 let shiftcount=$shiftcount+2
103 pl_DISTRO_RELEASE=$OPTARG
104 let shiftcount=$shiftcount+2
107 pl_DISTRO_ARCH=$OPTARG
108 let shiftcount=$shiftcount+2
111 echo "Usage: $0 [OPTION]..."
112 echo " -l url distro mirror location (default: $pl_DISTRO_URL)"
113 echo " -r release distro release number (default: $pl_DISTRO_RELEASE)"
114 echo " -a arch distro architecture (default: $pl_DISTRO_ARCH)"
115 echo "where distro can be either fedora, centos, or redhat"
116 echo " -h This message"
124 ######################################## handling a root image
125 function pl_root_rpm_macros () {
127 %_install_langs C:en_US:en
128 %_netsharedpath /proc:/dev/pts:/usr/share/info
130 %__file_context_path /dev/null
134 function pl_root_makedevs() {
136 # Clean ${vroot}/dev, but only when ${vroot}!=""
137 [ -n $vroot ] && rm -rf $vroot/dev
140 mknod -m 666 $vroot/dev/null c 1 3
141 mknod -m 666 $vroot/dev/zero c 1 5
142 mknod -m 666 $vroot/dev/full c 1 7
143 mknod -m 644 $vroot/dev/random c 1 8
144 mknod -m 644 $vroot/dev/urandom c 1 9
145 mknod -m 666 $vroot/dev/tty c 5 0
146 mknod -m 666 $vroot/dev/ptmx c 5 2
147 # For bash command substitution
148 ln -nsf ../proc/self/fd $vroot/dev/fd
150 # For df and linuxconf
151 touch $vroot/dev/hdv1
154 mkdir -p $vroot/dev/pts
156 # (Might have to remove the following for vserver-reference.)
159 mkdir -p $vroot/dev/shm
162 mkdir -p $vroot/dev/net
163 mknod -m 600 $vroot/dev/net/tun c 10 200
165 # For mkinitrd (in case a kernel is being installed)
166 # As well for loop back mounting within a vserver.
167 for i in $(seq 0 255) ; do
168 mknod -m 640 $vroot/dev/loop$i b 7 $i
172 function mkfedora_usage() {
173 echo "Usage: pl_root_mkfedora [OPTION]... basedir pldistro pkgsfile(s)"
174 echo " -l url Fedora mirror location."
175 echo " Defaults are searched in <pldistro>.mirrors"
176 echo " -v Be verbose"
177 echo " -h This message"
178 echo " target selection (defaults based on current build vserver)"
179 echo " -r release Fedora release number (default: $releasever)"
180 echo " -a arch Fedora architecture (default: $basearch)"
184 function pl_root_mkfedora () {
186 echo "* Entering pl_root_mkfedora" "$@"
188 if [ $UID -ne 0 ] ; then
189 echo "Error: You must run this script as root."
196 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
197 releasever=$pl_DISTRO_RELEASE
198 basearch=$pl_DISTRO_ARCH
202 while getopts "l:r:a:vh" opt ; do
205 mirrors="$mirrors $OPTARG"
223 shift $(($OPTIND - 1))
224 [[ "$#" -lt 3 ]] && mkfedora_usage
228 vroot=$(cd $vroot && pwd -P)
229 [ -d $vroot ] || mkfedora_usage
232 # parse pkgsfile and add to local vars
233 fcdistro=${pl_DISTRO_NAME}
234 pkgs_packages=$(pl_parsePkgs package $fcdistro $pldistro $pkgsfile)
235 pkgs_groups=$(pl_parsePkgs group $fcdistro $pldistro $pkgsfile)
236 # packages to exclude - obsolete, was maybe useful when installing a group
237 pkgs_excludes=$(pl_parsePkgs exclude $fcdistro $pldistro $pkgsfile)
238 pkgs_junk=$(pl_parsePkgs junk $fcdistro $pldistro $pkgsfile)
239 pkgs_precious=$(pl_parsePkgs precious $fcdistro $pldistro $pkgsfile)
240 # formerly related to mkfedora -k
241 pkgs_kexcludes=$(pl_parsePkgs kexclude $fcdistro $pldistro $pkgsfile)
242 # get mirrors if not specified with -l
243 if [ -z "$mirrors" ] ; then
244 mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
245 mirrors=$(pl_parsePkgs mirror $fcdistro $pldistro $mirrorsfile)
249 [ -n "$pkgs_kexcludes" ] && exclude_kernel="exclude=$pkgs_excludes"
251 function mkfedora_fetch ()
253 curl --fail --silent --max-time 60 "$1"
256 # set list of attempted locations according to releasever
257 if [ $releasever -ge 7 ] ; then
259 linux/releases/$releasever/Everything/$basearch/os
260 $releasever/Everything/$basearch/os
261 core/$releasever/Everything/$basearch/os
262 linux/core/$releasever/$basearch/os
266 linux/core/$releasever/$basearch/os
267 core/$releasever/$basearch/os
268 $releasever/$basearch/os
272 echo "$0: candidate mirrors"
273 for mirror in $mirrors ; do
274 echo "* candidate mirror $mirror"
277 for mirror in $mirrors ; do
278 for attempt in $attempts; do
279 attempturl=$mirror/$attempt
280 if mkfedora_fetch $attempturl/repodata/repomd.xml >/dev/null ; then
287 if [ -z "$baseurl" ] ; then
288 echo "Error: $releasever/$basearch/os/repodata/repomd.xml"
289 echo " could not be found in any of the following locations:"
291 for mirror in $mirrors ; do
292 for attempt in $attempts ; do
293 echo $mirror/$attempt
299 echo "* selecting mirror with baseurl=$baseurl"
302 # Do not tolerate errors
305 ## make rpms ignore installing stuff to special fs entries like /proc
306 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
307 # you have to use at least one language beside 'C'
308 # Prevent all locales from being installed in reference image
309 mkdir -p $vroot/etc/rpm
310 pl_root_rpm_macros > $vroot/etc/rpm/macros
312 # Trick rpm and yum, who read the real root /etc/rpm/macros file
313 # rather than the one installed in the reference image, despite what
314 # you might expect the --root and --installroot options to mean. Both
315 # programs always read $HOME/.rpmmacros.
316 export HOME=$vroot/tmp
318 pl_root_rpm_macros > $vroot/tmp/.rpmmacros
320 function mkfedora_cleanup () {
321 umount -l $vroot/proc
322 umount -l $vroot/dev/shm
323 umount -l $vroot/dev/pts
326 # Clean up before exiting if anything goes wrong
327 trap "mkfedora_cleanup" ERR INT
329 # Mount in reference image
330 mount -t devpts none $vroot/dev/pts
331 mount -t tmpfs none $vroot/dev/shm
333 mount -t proc none $vroot/proc
335 # Create a /var/lib dirs for yum & rpm
336 mkdir -p $vroot/var/lib/yum
337 mkdir -p $vroot/var/lib/rpm
338 mkdir -p $vroot/usr/share/info
340 # Create a dummy /etc/fstab in reference image
342 cat >$vroot/etc/fstab <<EOF
343 # This fake fstab exists only to please df and linuxconf.
344 /dev/hdv1 / ext2 defaults 1 1
346 cp $vroot/etc/fstab $vroot/etc/mtab
348 # Necessary for some scripts
349 mkdir -p $vroot/etc/sysconfig
350 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
352 # Initialize RPM database in reference image
353 mkdir -p $vroot/var/lib/rpm
354 rpm --root $vroot --initdb
355 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
357 # Initialize yum in reference image
358 mkdir -p $vroot/var/cache/yum $vroot/var/log
359 if [ $releasever -lt 7 ] ; then
365 cat >$vroot/etc/yum.conf <<EOF
367 cachedir=/var/cache/yum
369 logfile=/var/log/yum.log
371 distroverpkg=redhat-release
377 # Prevent yum-2.4 from loading additional repository definitions
378 # (e.g., from /etc/yum.repos.d/)
382 name=Fedora ${corename}${releasever} - $basearch - base
387 for optional in updates extras ; do
389 $mirror/linux/core/$optional/$releasever/$basearch \
390 $mirror/core/$optional/$releasever/$basearch \
391 $mirror/linux/$optional/$releasever/$basearch \
392 $mirror/$optional/$releasever/$basearch ; do
393 if mkfedora_fetch $optionalurl/repodata/repomd.xml ; then
394 cat >>$vroot/etc/yum.conf <<EOF
396 [$(basename $optional)]
397 name=Fedora ${corename}${releasever} - $basearch - $(basename $optional)
398 baseurl=$optionalurl/
406 # If we are being built as part of an automated RPM build, solve the
407 # bootstrap problem by including any just built packages in the yum
408 # configuration. This cooperates with the PlanetLab build system.
409 if [ -n "$RPM_BUILD_DIR" ] ; then
410 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
411 # If run under sudo, allow user to delete the headers/ and
412 # repodata/ directories.
413 if [ -n "$SUDO_USER" ] ; then
414 chown -R $SUDO_USER $RPM_RPMS_DIR
416 cat >>$vroot/etc/yum.conf <<EOF
419 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
420 baseurl=file://$RPM_RPMS_DIR/
425 for exclude in $pkgs_excludes; do
426 exclude_arg="$exclude_arg --exclude $exclude"
429 # glibc must be specified explicitly for the correct arch to be
431 echo "* Installing glibc"
432 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $exclude_arg install glibc
435 if [ -n "$pkgs_packages" ] ; then
436 echo "* Installing optional packages" $pkgs_packages
437 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $exclude_arg \
438 install $pkgs_packages
439 if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
440 echo "* Warning: Missing packages"
441 rpm --root $vroot -q $pkgs_packages | grep "not installed"
445 if [ -n "$pkgs_groups" ] ; then
446 ## call yum sequentially to get finer-grained info on dependencies
447 for grp in $pkgs_groups ; do
448 echo "* Installing optional group $grp"
449 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $exclude_arg \
454 # formerly in bootcd/prep.sh : to optimize footprint
455 if [ -n "$pkgs_junk" ] ; then
456 echo "* Removing unnecessary junk"
458 # Save precious files
459 [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
460 # Remove unnecessary junk
461 [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
462 # Restore precious files
463 [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
469 yum -c $vroot/etc/yum.conf --installroot=$vroot -y clean all
472 rm -f $vroot/var/lib/rpm/__db*
474 # Set time zone to UTC
475 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
476 rm -f $vroot/etc/localtime
477 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
480 # remove trap handler, as we are about to call it directly.
489 function pl_root_tune_image () {
492 # Disable all services in reference image
493 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
495 # FC2 minilogd starts up during shutdown and makes unmounting
496 # impossible. Just get rid of it.
497 rm -f $root/sbin/minilogd
498 ln -nsf /bin/true $root/sbin/minilogd
500 # This tells the Boot Manager that it is okay to update
501 # /etc/resolv.conf and /etc/hosts whenever the network configuration
502 # changes. Users are free to delete this file.
503 touch $root/etc/AUTO_UPDATE_NET_FILES
506 # Move specified directories out of a src tree into a dst tree, and
507 # then for each moved directory create a symlink in src to dst.
508 function pl_move_dirs() {
515 for datadir in "$@" ; do
516 mkdir -p ${data}${datadir}
517 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
518 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
520 rm -rf ${root}/${datadir}
521 mkdir -p $(dirname ${root}/${datadir})
522 ln -nsf ${store}/${datadir} ${root}/${datadir}
526 # Construct an image file from given some directory
527 # XXX in the future maybe use livecdtools?
528 function pl_make_image() {
533 # Leave about 100 MB free space and allow for about 20% inode overhead
534 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
536 blocks=$(($bytes / $bs))
537 dd bs=$bs count=$blocks if=/dev/zero of=$image
538 mkfs.ext3 -b $bs -j -F $image
540 # Temporarily mount it
541 tmp=$(mktemp -d tmp.XXXXXX)
542 mount -o loop $image $tmp
543 trap "umount $tmp; rmdir $tmp" ERR INT
546 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
554 # Fix permissions on tmp directories
555 function pl_fixtmp_permissions() {
557 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
560 function pl_fixdirs() {
563 for datadir in datadirs ; do
564 if [ -h ${root}/${datadir} ] ; then
565 rm -f ${root}/${datadir}
566 mkdir -p ${root}/${datadir}
571 ########## .pkgs format
572 # comments start with a # - this is needed only if you use a keyword in a comment
574 function pl_getPkgsAttribute () {
577 grep -v '^#' $file | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,"
580 # for a given keyword like 'package' :
581 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
583 # package: pkg1 .. pkgn
584 # package+f8: pkg1 .. pkgn
585 # package-f8: pkg1 .. pkgn
587 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
589 function pl_parsePkgs () {
594 # remaining arguments are paths to the pkgs files
596 # grab regular descriptions
597 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
599 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
601 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
603 for i in $all $add; do
604 for exclude in $sub; do
605 [ "$i" = "$exclude" ] && continue 2
607 echo "$i " | sed -e "s,@arch@,$pl_DISTRO_ARCH,g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
612 function pl_getPackages() { fcdistro=$1; shift ; pldistro=$1; shift ; pl_parsePkgs package $fcdistro $pldistro "$@" ; }
613 function pl_getGroups() { fcdistro=$1; shift ; pldistro=$1; shift ; pl_parsePkgs group $fcdistro $pldistro "$@" ; }
615 # locates a pldistro-dependant file
616 # tries first in build/<pldistro>/, then in build/planetlab/
617 function pl_locateDistroFile () {
624 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
625 for path in $paths; do
626 if [ -f $path ] ; then
631 if [ -z "$pkgspath" ] ; then
632 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
633 echo 1>&2 "candidates were $paths"
634 echo "not-found-by-pl_locateDistroFile"
637 echo 1>&2 "pl_locateDistroFile: using $pkgspath"
644 function yumgroups_from_pkgs () {
650 sedargs="-e s,@FCDISTRO@,$fcdistro,g"
653 <?xml version="1.0"?>
654 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
658 for pkgsname in $pkgsnames; do
659 pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
660 packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
662 groupname=$(pl_getPkgsAttribute groupname $pkgsfile | sed $sedargs)
663 groupdesc=$(pl_getPkgsAttribute groupdesc $pkgsfile | sed $sedargs)
665 if [ -z "$groupname" -o -z "$groupdesc" ] ; then
666 echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
670 cat << __group_header
672 <id>$(echo $groupname|tr A-Z a-z)</id>
673 <name>$groupname</name>
674 <default>true</default>
675 <description>$groupdesc</description>
676 <uservisible>false</uservisible>
679 for package in $packages; do
680 echo "<packagereq type=\"mandatory\">$package</packagereq>"
682 cat << __group_footer