* tentative merge of the onelab build, be sure to run
[build.git] / mkfedora
index 29e167c..d0751f3 100755 (executable)
--- a/mkfedora
+++ b/mkfedora
@@ -1,6 +1,6 @@
 #!/bin/bash
 #
-# Builds a Fedora Core reference image. Requires the build server to
+# Builds a Fedora reference image. Requires the build server to
 # host a local yum repository in one of:
 #
 # /usr/share/mirrors/fedora
@@ -13,7 +13,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2004-2006 The Trustees of Princeton University
 #
-# $Id: build.sh,v 1.2 2005/10/01 18:20:08 mlhuang Exp $
+# $Id: mkfedora 1073 2007-11-15 09:56:03Z thierry $
 #
 
 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
@@ -23,26 +23,41 @@ verbose=0
 
 # Default yum repositories to try
 mirrors=(
-file:///usr/share/mirrors/fedora
-file:///var/www/html/mirrors/fedora
+file:///data/fedora
+http://localhost/fedora
 ftp://smoke.cs.princeton.edu/pub/mirrors/fedora
-ftp://128.112.137.30/pub/mirrors/fedora
-http://coblitz.planet-lab.org/pub/fedora
+http://coblitz.codeen.org/coblitz.planet-lab.org/pub/fedora
 ftp://mirror.cs.princeton.edu/pub/mirrors/fedora
+http://coblitz.planet-lab.org/pub/fedora
+ftp://mirror.stanford.edu/pub/mirrors/fedora
+ftp://rpmfind.net/linux/fedora
+http://fedora.laptop.org/fedora-linux-releases
 )
 
 # Release and architecture to install
-releasever=2
+releasever=4
 basearch=i386
 
 # Yum groups to install
-groups=(
-"Core and Base"
-)
+groups=()
 
 # Packages to install
 packages=()
 
+# Packages to exclude
+exclude=()
+
+# Exclude kernel* (and related) packages from all repositories except bootstrap
+exclude_kernel=
+
+# PlanetLab development environment
+if [ -f /etc/planetlab/plc_config ] ; then
+    . /etc/planetlab/plc_config
+    if [ -n "$PLC_DEVEL_FEDORA_URL" ] ; then
+       mirrors=($PLC_DEVEL_FEDORA_URL)
+    fi
+fi
+
 usage()
 {
     echo "Usage: mkfedora [OPTION]... [basedir]"
@@ -53,19 +68,19 @@ usage()
     echo "     -r release      Fedora release number (default: $releasever)"
     echo "     -a arch         Fedora architecture (default: $basearch)"
     echo "     -g group1 -g group2 ..."
-    echo "                     Additional yumgroups to install. Defaults:"
-    for group in "${groups[@]}" ; do
-       echo "                  $group"
-    done
+    echo "                     Yumgroups to install (default: none)"
     echo "     -p package1 -p package2 ..."
     echo "                     Additional packages to install (default: none)"
+    echo "     -x package1 -x package2 ..."
+    echo "                     Packages to exclude (default: none)"
+    echo "     -k              Exclude kernel* packages from all repositories except bootstrap"
     echo "     -v              Be verbose"
     echo "     -h              This message"
     exit 1
 }
 
 # Get options
-while getopts "l:r:a:g:p:vh" opt ; do
+while getopts "l:r:a:g:p:x:kvh" opt ; do
     case $opt in
        l)
            if echo $OPTARG | grep -q -i '^\(file\|http[s]*\)://' ; then
@@ -86,6 +101,12 @@ while getopts "l:r:a:g:p:vh" opt ; do
        p)
            packages[${#packages[*]}]="$OPTARG"
            ;;
+       x)
+           exclude[${#exclude[*]}]="$OPTARG"
+           ;;
+       k)
+           exclude_kernel="exclude=kernel* ulogd iptables"
+           ;;
        v)
            verbose=1
            set -x
@@ -108,35 +129,49 @@ if [ $UID -ne 0 ] ; then
     exit 1
 fi
 
-# Old versions of curl don't understand file:// URLs
 fetch ()
 {
-    url=$1
-    if curl --fail --silent --max-time 60 $url ; then
-       return 0
-    else
-       if [ -f ${url#file://} ] ; then
-           cat ${url#file://}
-           return 0
-       fi
-    fi
-    return 1
+    curl --fail --silent --max-time 60 "$1"
 }
 
+# hard to find two mirrors with a similar layout
+# set list of attempted locations according to releasever
+if [ $releasever -ge 7 ] ; then
+    attempts="
+$releasever/Everything/$basearch/os
+core/$releasever/Everything/$basearch/os \
+linux/core/$releasever/$basearch/os
+"
+else
+    attempts="
+linux/core/$releasever/$basearch/os 
+core/$releasever/$basearch/os 
+$releasever/$basearch/os
+"
+fi
+
 for mirror in "${mirrors[@]}" ; do
-    baseurl=$mirror/linux/core/$releasever/$basearch/os
-    if fetch $baseurl/repodata/repomd.xml >/dev/null ; then
+    for attempt in $attempts; do 
+       baseurl=$mirror/$attempt
+       if fetch $baseurl/repodata/repomd.xml >/dev/null ; then
+           break
+       fi
+       unset baseurl
+    done
+    if [ -n "$baseurl" ] ; then
        break
     fi
     unset baseurl
 done
 
 if [ -z "$baseurl" ] ; then
-    echo "Error: linux/core/$releasever/$basearch/os/repodata/repomd.xml"
+    echo "Error: $releasever/$basearch/os/repodata/repomd.xml"
     echo "       could not be found in any of the following locations:"
     echo
     for mirror in ${mirrors[@]} ; do
-       echo $mirror
+       for attempt in $attempts ; do
+           echo $mirror/$attempt
+       done
     done
     echo
     usage
@@ -149,29 +184,11 @@ if [ $verbose -eq 0 ] ; then
     exec 2>/dev/null
 fi
 
-# Scratch space
-tmp=$(mktemp -d /tmp/mkfedora.XXXXXX)
-
-# Initialize /dev in reference image
-mkdir -p $vroot/dev
-mknod -m 666 $vroot/dev/null c 1 3
-mknod -m 666 $vroot/dev/zero c 1 5
-mknod -m 666 $vroot/dev/full c 1 7
-mknod -m 644 $vroot/dev/random c 1 8
-mknod -m 644 $vroot/dev/urandom c 1 9
-mknod -m 666 $vroot/dev/tty c 5 0
-mknod -m 666 $vroot/dev/ptmx c 5 2
-# For bash command substitution
-ln -nsf ../proc/self/fd $vroot/dev/fd
-# For df and linuxconf
-touch $vroot/dev/hdv1
-
-# Mount /dev/pts in reference image
-mkdir -p $vroot/dev/pts
-mount -t devpts none $vroot/dev/pts
+# Do not tolerate errors
+set -e
 
-# Mount /dev/shm in reference image
-mkdir -p $vroot/dev/shm
+# Mount /dev/pts and /dev/shm in reference image
+mount -t devpts none $vroot/dev/pts
 mount -t tmpfs none $vroot/dev/shm
 
 # Mount /proc in reference image
@@ -180,14 +197,18 @@ mount -t proc none $vroot/proc
 
 cleanup ()
 {
-    umount $vroot/proc
-    umount $vroot/dev/shm
-    umount $vroot/dev/pts
-    rm -rf $tmp
+    umount -l $vroot/proc
+    umount -l $vroot/dev/shm
+    umount -l $vroot/dev/pts
 }
 
 # Clean up before exiting if anything goes wrong
-trap "cleanup; exit 1" ERR
+trap "cleanup" ERR INT
+
+# Create a /var/lib dirs for yum & rpm
+mkdir -p $vroot/var/lib/yum
+mkdir -p $vroot/var/lib/rpm
+mkdir -p $vroot/usr/share/info
 
 # Create a dummy /etc/fstab in reference image
 mkdir -p $vroot/etc
@@ -224,6 +245,12 @@ rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
 
 # Initialize yum in reference image
 mkdir -p $vroot/var/cache/yum $vroot/var/log
+if [ $releasever -lt 7 ] ; then
+       corename="Core "
+else
+       corename=""
+fi
+
 cat >$vroot/etc/yum.conf <<EOF
 [main]
 cachedir=/var/cache/yum
@@ -241,19 +268,28 @@ gpgcheck=0
 reposdir=/dev/null
 
 [base]
-name=Fedora Core $releasever - $basearch - base
+name=Fedora ${corename}${releasever} - $basearch - base
 baseurl=$baseurl/
+$exclude_kernel
 EOF
 
-for optional in core/updates extras ; do
-    if fetch $mirror/linux/$optional/$releasever/$basearch/repodata/repomd.xml ; then
-       cat >>$vroot/etc/yum.conf <<EOF
+for optional in updates extras ; do
+    for optionalurl in \
+       $mirror/linux/core/$optional/$releasever/$basearch \
+       $mirror/core/$optional/$releasever/$basearch \
+       $mirror/linux/$optional/$releasever/$basearch \
+       $mirror/$optional/$releasever/$basearch ; do
+        if fetch $optionalurl/repodata/repomd.xml ; then
+           cat >>$vroot/etc/yum.conf <<EOF
 
 [$(basename $optional)]
-name=Fedora Core $releasever - $basearch - $(basename $optional)
-baseurl=$mirror/linux/$optional/$releasever/$basearch/
+name=Fedora ${corename}${releasever} - $basearch - $(basename $optional)
+baseurl=$optionalurl/
+$exclude_kernel
 EOF
-    fi
+           break
+       fi
+    done
 done
 
 # If we are being built as part of an automated RPM build, solve the
@@ -261,8 +297,17 @@ done
 # configuration. This cooperates with the PlanetLab build system.
 if [ -n "$RPM_BUILD_DIR" ] ; then
     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
-    yum-arch $RPM_RPMS_DIR || :
-    createrepo $RPM_RPMS_DIR || :
+    # yum-2.0.x
+    if [ -x /usr/bin/yum-arch ] ; then
+       yum-arch -q $RPM_RPMS_DIR
+    fi
+    # yum-2.4.x
+    if [ -x /usr/bin/createrepo ] ; then
+       if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
+           groupfile="-g yumgroups.xml"
+       fi
+       createrepo --quiet $groupfile $RPM_RPMS_DIR
+    fi
     # If run under sudo, allow user to delete the headers/ and
     # repodata/ directories.
     if [ -n "$SUDO_USER" ] ; then
@@ -274,73 +319,71 @@ if [ -n "$RPM_BUILD_DIR" ] ; then
 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
 baseurl=file://$RPM_RPMS_DIR/
 EOF
-
-    # XXX Build system should generate yumgroups.xml automatically
 fi
 
-# The "Core" and "Base" groups are not uservisible by default in
-# comps.xml, and old (Fedora Core 2) versions of yum will refuse to
-# recognize them explicitly.
-mkdir -p $tmp/corebase
-cat >$tmp/corebase/yumgroups.xml <<EOF
-<?xml version="1.0"?>
-<!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
-<comps>
-  <group>
-    <id>corebase</id>
-    <name>Core and Base</name>
-    <default>true</default>
-    <description>Core and Base Packages</description>
-    <uservisible>true</uservisible>
-    <grouplist>
-      <groupreq>core</groupreq>
-      <groupreq>base</groupreq>
-    </grouplist>
-  </group>
-</comps>
-EOF
-
-# yum-2.0.x
-yum-arch $tmp/corebase || :
-# yum-2.4.x
-createrepo -g yumgroups.xml $tmp/corebase || :
-
-cat >>$vroot/etc/yum.conf <<EOF
-
-[corebase]
-name=Core and Base
-baseurl=file://$tmp/corebase/
-EOF
-
-echo -n "* Installing base system..." >&3
+excludes=
+for package in "${exclude[@]}" ; do
+    excludes="$excludes --exclude=$package"
+done
 
-# glibc must be specified explicitly for the correct arch to be chosen.
-yum -c $vroot/etc/yum.conf --installroot=$vroot -y install glibc
+# glibc must be specified explicitly for the correct arch to be
+# chosen.
+echo "* Installing glibc" >&3
+yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes install glibc
 
 # Go, baby, go
-yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
-    groupinstall "${groups[@]}"
+if [ ${#packages[*]} -gt 0 ] ; then
+   echo "* Installing optional packages" "${packages[@]}" >&3
+   yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
+         install "${packages[@]}"
+   if ! rpm --root $vroot -q "${packages[@]}" >/dev/null ; then
+       echo "* Warning: Missing packages"
+       rpm --root $vroot -q "${packages[@]}" | grep "not installed"
+   fi
+fi
 
-echo "done" >&3
+if [ ${#groups[*]} -gt 0 ] ; then
+   ## call yum sequentially to get finer-grained info on dependencies
+   for grp in "${groups[@]}" ; do
+      echo "* Installing optional group $grp" >&3
+      yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
+       groupinstall "$grp"
+   done
+fi
 
-if [ ${#packages[*]} -gt 0 ] ; then
-    echo -n "* Installing optional packages..."
-    yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
-       install "${packages[@]}"
-    echo "done"
+# FC2 dev %preinstall checks /proc/mounts to make sure that /dev is
+# not currently mounted as devfs. If it thinks it is, it will refuse
+# to install the package. On a modern system running udev that mounts
+# /dev as tmpfs, this check fails. Since we are installing into a
+# chroot, whether /dev is mounted on the host system or not doesn't
+# matter. If dev was explicitly mentioned in the packages list, force
+# its installation.
+if [ "$releasever" = "2" ] ; then
+    for package in "${packages[@]}" ; do
+       if [ "$package" = "dev" ] && ! rpm --root $vroot -q dev >/dev/null 2>&1 ; then
+           rpm --root $vroot -Uvh --noscripts $baseurl/Fedora/RPMS/dev-3.3.13-1.i386.rpm
+           break
+       fi
+    done
 fi
 
 # Clean yum cache
-echo -n "* Cleaning up..." >&3
+echo "* Cleaning up" >&3
 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
     clean all
-echo "done" >&3
 
 # Clean RPM state
 rm -f $vroot/var/lib/rpm/__db*
 
-# Disable all services in reference image
-chroot $vroot /bin/sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
+# Set time zone to UTC
+if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
+    rm -f $vroot/etc/localtime
+    ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
+fi
+
+
+# remove trap handler, as we are about to call it directly.
+trap - ERR INT
 
 # Clean up
 cleanup