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_makedevs() {
130 # Clean ${vroot}/dev, but only when ${vroot}!=""
131 [ -n $vroot ] && rm -rf $vroot/dev
134 mknod -m 666 $vroot/dev/null c 1 3
135 mknod -m 666 $vroot/dev/zero c 1 5
136 mknod -m 666 $vroot/dev/full c 1 7
137 mknod -m 644 $vroot/dev/random c 1 8
138 mknod -m 644 $vroot/dev/urandom c 1 9
139 mknod -m 666 $vroot/dev/tty c 5 0
140 mknod -m 666 $vroot/dev/ptmx c 5 2
141 # For bash command substitution
142 ln -nsf ../proc/self/fd $vroot/dev/fd
144 # For df and linuxconf
145 touch $vroot/dev/hdv1
148 mkdir -p $vroot/dev/pts
150 # (Might have to remove the following for vserver-reference.)
153 mkdir -p $vroot/dev/shm
156 mkdir -p $vroot/dev/net
157 mknod -m 600 $vroot/dev/net/tun c 10 200
159 # For mkinitrd (in case a kernel is being installed)
160 # As well for loop back mounting within a vserver.
161 for i in $(seq 0 255) ; do
162 mknod -m 640 $vroot/dev/loop$i b 7 $i
166 function mkfedora_usage()
168 echo "Usage: mkfedora [OPTION]... [basedir]"
169 echo " -l url Fedora mirror location. Defaults to try:"
170 for mirror in "${mirrors[@]}" ; do
173 echo " -f pkgsfile use this pkgs file for packages, groups, excludes.."
174 echo " -k Exclude kernel* packages from all repositories except bootstrap"
175 echo " -v Be verbose"
176 echo " -h This message"
177 echo " target selection (defaults based on current build vserver)"
178 echo " -r release Fedora release number (default: $releasever)"
179 echo " -a arch Fedora architecture (default: $basearch)"
180 echo " legacy (use -f instead)"
181 echo " -g group1 -g group2 ..."
182 echo " Yumgroups to install (default: none)"
183 echo " -p package1 -p package2 ..."
184 echo " Additional packages to install (default: none)"
185 echo " -x package1 -x package2 ..."
186 echo " Packages to exclude (default: none)"
190 function mkfedora () {
192 echo "* Entering mkfedora " "$@"
197 # Default yum repositories to try
200 http://localhost/fedora
201 http://build.planet-lab.org/fedora
202 http://coblitz.codeen.org/coblitz.planet-lab.org/pub/fedora
203 ftp://mirror.cs.princeton.edu/pub/mirrors/fedora
204 ftp://mirror.stanford.edu/pub/mirrors/fedora
205 ftp://rpmfind.net/linux/fedora
208 # Release and architecture to install
212 # Yum groups to install
215 # Packages to install
218 # Packages to exclude
221 # Exclude kernel* (and related) packages from all repositories except bootstrap
224 # PlanetLab development environment
225 if [ -f /etc/planetlab/plc_config ] ; then
226 . /etc/planetlab/plc_config
227 if [ -n "$PLC_DEVEL_FEDORA_URL" ] ; then
228 echo "* mkfedora : setting mirrors from /etc/planetlab/config"
229 mirrors=($PLC_DEVEL_FEDORA_URL)
234 while getopts "l:r:a:g:p:x:f:kvh" opt ; do
237 if echo $OPTARG | grep -q -i '^\(file\|http[s]*\)://' ; then
240 mirrors=(file://$OPTARG)
250 groups[${#groups[*]}]="$OPTARG"
253 packages[${#packages[*]}]="$OPTARG"
256 exclude[${#exclude[*]}]="$OPTARG"
262 exclude_kernel="exclude=kernel* ulogd iptables"
274 shift $(($OPTIND - 1))
275 if [ ! -d "$1" ] ; then
279 vroot=$(cd $1 && pwd -P)
281 if [ $UID -ne 0 ] ; then
282 echo "Error: You must run this script as root."
286 function mkfedora_fetch ()
288 curl --fail --silent --max-time 60 "$1"
291 # hard to find two mirrors with a similar layout
292 # set list of attempted locations according to releasever
293 if [ $releasever -ge 7 ] ; then
295 linux/releases/$releasever/Everything/$basearch/os
296 $releasever/Everything/$basearch/os
297 core/$releasever/Everything/$basearch/os
298 linux/core/$releasever/$basearch/os
302 linux/core/$releasever/$basearch/os
303 core/$releasever/$basearch/os
304 $releasever/$basearch/os
308 echo "$0: candidate mirrors"
309 for mirror in "${mirrors[@]}" ; do
310 echo "* candidate mirror $mirror"
313 for mirror in "${mirrors[@]}" ; do
314 for attempt in $attempts; do
315 attempturl=$mirror/$attempt
316 if mkfedora_fetch $attempturl/repodata/repomd.xml >/dev/null ; then
323 if [ -z "$baseurl" ] ; then
324 echo "Error: $releasever/$basearch/os/repodata/repomd.xml"
325 echo " could not be found in any of the following locations:"
327 for mirror in ${mirrors[@]} ; do
328 for attempt in $attempts ; do
329 echo $mirror/$attempt
336 # this used to be for when mkfedora was called within myplc
337 # that is, when the bootcd was recomputed from scratch
340 ### if [ $verbose -eq 0 ] ; then
345 # Do not tolerate errors
348 ## make rpms ignore installing stuff to special fs entries like /proc
349 # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
350 # you have to use at least one language beside 'C'
351 # Prevent all locales from being installed in reference image
352 mkdir -p $vroot/etc/rpm
353 cat > $vroot/etc/rpm/macros <<EOF
354 %_install_langs C:en_US:en
355 %_netsharedpath /proc:/dev/pts
357 %__file_context_path /dev/null
360 # Trick rpm and yum, who read the real root /etc/rpm/macros file
361 # rather than the one installed in the reference image, despite what
362 # you might expect the --root and --installroot options to mean. Both
363 # programs always read $HOME/.rpmmacros.
364 export HOME=$vroot/tmp
366 cp $vroot/etc/rpm/macros $vroot/tmp/.rpmmacros
368 ### Checking whether this is really needed
369 ### # copy to the vserver's rpm macros
370 ### # xxx fixme - this must be reviewed once we get the stuff running
371 ### cp $vroot/etc/rpm/macros /etc/rpm/macros
373 # Mount in reference image
374 mount -t devpts none $vroot/dev/pts
375 mount -t tmpfs none $vroot/dev/shm
377 mount -t proc none $vroot/proc
379 function mkfedora_cleanup ()
381 umount -l $vroot/proc
382 umount -l $vroot/dev/shm
383 umount -l $vroot/dev/pts
386 # Clean up before exiting if anything goes wrong
387 trap "mkfedora_cleanup" ERR INT
389 # Create a /var/lib dirs for yum & rpm
390 mkdir -p $vroot/var/lib/yum
391 mkdir -p $vroot/var/lib/rpm
392 mkdir -p $vroot/usr/share/info
394 # Create a dummy /etc/fstab in reference image
396 cat >$vroot/etc/fstab <<EOF
397 # This fake fstab exists only to please df and linuxconf.
398 /dev/hdv1 / ext2 defaults 1 1
400 cp $vroot/etc/fstab $vroot/etc/mtab
402 # Necessary for some scripts
403 mkdir -p $vroot/etc/sysconfig
404 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
406 # Initialize RPM database in reference image
407 mkdir -p $vroot/var/lib/rpm
408 rpm --root $vroot --initdb
409 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
411 # Initialize yum in reference image
412 mkdir -p $vroot/var/cache/yum $vroot/var/log
413 if [ $releasever -lt 7 ] ; then
419 cat >$vroot/etc/yum.conf <<EOF
421 cachedir=/var/cache/yum
423 logfile=/var/log/yum.log
425 distroverpkg=redhat-release
431 # Prevent yum-2.4 from loading additional repository definitions
432 # (e.g., from /etc/yum.repos.d/)
436 name=Fedora ${corename}${releasever} - $basearch - base
441 for optional in updates extras ; do
443 $mirror/linux/core/$optional/$releasever/$basearch \
444 $mirror/core/$optional/$releasever/$basearch \
445 $mirror/linux/$optional/$releasever/$basearch \
446 $mirror/$optional/$releasever/$basearch ; do
447 if mkfedora_fetch $optionalurl/repodata/repomd.xml ; then
448 cat >>$vroot/etc/yum.conf <<EOF
450 [$(basename $optional)]
451 name=Fedora ${corename}${releasever} - $basearch - $(basename $optional)
452 baseurl=$optionalurl/
460 # If we are being built as part of an automated RPM build, solve the
461 # bootstrap problem by including any just built packages in the yum
462 # configuration. This cooperates with the PlanetLab build system.
463 if [ -n "$RPM_BUILD_DIR" ] ; then
464 RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
466 if [ -x /usr/bin/yum-arch ] ; then
467 yum-arch -q $RPM_RPMS_DIR
470 if [ -x /usr/bin/createrepo ] ; then
471 if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
472 groupfile="-g yumgroups.xml"
474 createrepo --quiet $groupfile $RPM_RPMS_DIR
476 # If run under sudo, allow user to delete the headers/ and
477 # repodata/ directories.
478 if [ -n "$SUDO_USER" ] ; then
479 chown -R $SUDO_USER $RPM_RPMS_DIR
481 cat >>$vroot/etc/yum.conf <<EOF
484 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
485 baseurl=file://$RPM_RPMS_DIR/
490 if [ -n "$pkgsfile" ] ; then
491 # parse pkgsfile and add to local vars
492 fcdistro=$(pl_getReleaseName "Fedora" $releasever)
493 for i in $(pl_parsePkgs package $fcdistro $pkgsfile) ; do
494 packages[${#packages[*]}]="$i"
496 for i in $(pl_parsePkgs group $fcdistro $pkgsfile) ; do
497 groups[${#groups[*]}]="$i"
499 for i in $(pl_parsePkgs exclude $fcdistro $pkgsfile) ; do
500 exclude[${#exclude[*]}]="$i"
502 junk=$(pl_parsePkgs junk $fcdistro $pkgsfile)
503 precious=$(pl_parsePkgs precious $fcdistro $pkgsfile)
507 for package in "${exclude[@]}" ; do
508 excludes="$excludes --exclude=$package"
511 # glibc must be specified explicitly for the correct arch to be
513 echo "* Installing glibc"
514 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes install glibc
517 if [ ${#packages[*]} -gt 0 ] ; then
518 echo "* Installing optional packages" "${packages[@]}"
519 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
520 install "${packages[@]}"
521 if ! rpm --root $vroot -q "${packages[@]}" >/dev/null ; then
522 echo "* Warning: Missing packages"
523 rpm --root $vroot -q "${packages[@]}" | grep "not installed"
527 if [ ${#groups[*]} -gt 0 ] ; then
528 ## call yum sequentially to get finer-grained info on dependencies
529 for grp in "${groups[@]}" ; do
530 echo "* Installing optional group $grp"
531 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
536 # FC2 dev %preinstall checks /proc/mounts to make sure that /dev is
537 # not currently mounted as devfs. If it thinks it is, it will refuse
538 # to install the package. On a modern system running udev that mounts
539 # /dev as tmpfs, this check fails. Since we are installing into a
540 # chroot, whether /dev is mounted on the host system or not doesn't
541 # matter. If dev was explicitly mentioned in the packages list, force
543 if [ "$releasever" = "2" ] ; then
544 for package in "${packages[@]}" ; do
545 if [ "$package" = "dev" ] && ! rpm --root $vroot -q dev >/dev/null 2>&1 ; then
546 rpm --root $vroot -Uvh --noscripts $baseurl/Fedora/RPMS/dev-3.3.13-1.i386.rpm
554 yum -c $vroot/etc/yum.conf --installroot=$vroot -y clean all
557 rm -f $vroot/var/lib/rpm/__db*
559 # Set time zone to UTC
560 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
561 rm -f $vroot/etc/localtime
562 ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
565 # formerly in bootcd/prep.sh : to optimize footprint
566 echo "* Removing unnecessary junk"
570 # Save precious files
571 [ -n "$precious" ] && tar --ignore-failed-read -cpf precious.tar $precious
573 # Remove unnecessary junk
574 [ -n "$junk" ] && rm -rf $junk
576 # Restore precious files
577 [ -n "$precious" ] && tar -xpf precious.tar && rm -f precious.tar
581 # remove trap handler, as we are about to call it directly.
590 function pl_mkfedora() {
597 [ -n "$pl_DISTRO_URL" ] && options="$options -l $pl_DISTRO_URL"
598 [ -n "$pl_DISTRO_ARCH" ] && options="$options -a $pl_DISTRO_ARCH"
599 [ -n "$pl_DISTRO_RELEASE" ] && options="$options -r $pl_DISTRO_RELEASE"
600 # echo "mkfedora -v $options $root"
601 mkfedora -v $options $root
604 function pl_setup_chroot() {
608 pl_mkfedora $root $options
610 # Disable all services in reference image
611 chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
613 # FC2 minilogd starts up during shutdown and makes unmounting
614 # impossible. Just get rid of it.
615 rm -f $root/sbin/minilogd
616 ln -nsf /bin/true $root/sbin/minilogd
618 # This tells the Boot Manager that it is okay to update
619 # /etc/resolv.conf and /etc/hosts whenever the network configuration
620 # changes. Users are free to delete this file.
621 touch $vroot/etc/AUTO_UPDATE_NET_FILES
624 # Move specified directories out of a src tree into a dst tree, and
625 # then for each moved directory create a symlink in src to dst.
626 function pl_move_dirs() {
633 for datadir in "$@" ; do
634 mkdir -p ${data}${datadir}
635 if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
636 (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
638 rm -rf ${root}/${datadir}
639 mkdir -p $(dirname ${root}/${datadir})
640 ln -nsf /${store}/${datadir} ${root}/${datadir}
644 # Construct an image file from given some directory
645 # XXX in the future maybe use livecdtools?
646 function pl_make_image() {
651 # Leave about 100 MB free space and allow for about 20% inode overhead
652 bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
654 blocks=$(($bytes / $bs))
655 dd bs=$bs count=$blocks if=/dev/zero of=$image
656 mkfs.ext3 -b $bs -j -F $image
658 # Temporarily mount it
659 tmp=$(mktemp -d tmp.XXXXXX)
660 mount -o loop $image $tmp
661 trap "umount $tmp; rmdir $tmp" ERR INT
664 (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
672 # Fix permissions on tmp directories
673 function pl_fixtmp_permissions() {
675 chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
678 function pl_fixdirs() {
681 for datadir in datadirs ; do
682 if [ -h ${root}/${datadir} ] ; then
683 rm -f ${root}/${datadir}
684 mkdir -p ${root}/${datadir}
689 ########## .pkgs format
690 # comments start with a # - this is needed only if you use a keyword in a comment
691 # for a given keyword like 'package' :
692 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
694 # package: pkg1 .. pkgn
695 # package+f8: pkg1 .. pkgn
696 # package-f8: pkg1 .. pkgn
698 function pl_parsePkgs () {
702 # remaining arguments are paths to the pkgs files
704 # grab regular descriptions
705 all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
707 add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}\+${fcdistro}:,,")
709 sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}\-${fcdistro}:" | sed -e "s,${keyword}\-${fcdistro}:,,")
711 for i in $all $add; do
712 for exclude in $sub; do
713 [ "$i" = "$exclude" ] && continue 2
720 function pl_getPackages() { fcdistro=$1; shift ; pl_parsePkgs package $fcdistro "$@" ; }
721 function pl_getGroups() { fcdistro=$1; shift ; pl_parsePkgs group $fcdistro "$@" ; }
723 # locates a pldistro-dependant file
724 # tries first in build/<pldistro>/, then in build/planetlab/
725 function pl_locateDistroFile () {
732 paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
733 for path in $paths; do
734 if [ -f $path ] ; then
739 if [ -z "$pkgspath" ] ; then
740 echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
741 echo 1>&2 "candidates were $paths"
742 echo "not-found-by-pl_locateDistroFile"
745 echo 1>&2 "pl_locateDistroFile: using $pkgspath"