Should build fine now.
[build.git] / mkfedora
1 #!/bin/bash
2 #
3 # Builds a Fedora reference image. Requires the build server to
4 # host a local yum repository in one of:
5 #
6 # /usr/share/mirrors/fedora
7 # /var/www/html/mirrors/fedora
8 #
9 # Otherwise, tries using CoBlitz:
10 #
11 # http://coblitz.planet-lab.org/pub/fedora
12 #
13 # Mark Huang <mlhuang@cs.princeton.edu>
14 # Copyright (C) 2004-2006 The Trustees of Princeton University
15 #
16 # $Id$
17 #
18
19 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
20
21 # Verbosity
22 verbose=0
23
24 # Default yum repositories to try
25 mirrors=(
26 file:///data/fedora
27 http://localhost/fedora
28 http://build.planet-lab.org/fedora
29 http://coblitz.codeen.org/coblitz.planet-lab.org/pub/fedora
30 ftp://mirror.cs.princeton.edu/pub/mirrors/fedora
31 ftp://mirror.stanford.edu/pub/mirrors/fedora
32 ftp://rpmfind.net/linux/fedora
33 )
34
35 # Release and architecture to install
36 releasever=4
37 basearch=i386
38
39 # Yum groups to install
40 groups=()
41
42 # Packages to install
43 packages=()
44
45 # Packages to exclude
46 exclude=()
47
48 # Exclude kernel* (and related) packages from all repositories except bootstrap
49 exclude_kernel=
50
51 # PlanetLab development environment
52 if [ -f /etc/planetlab/plc_config ] ; then
53     . /etc/planetlab/plc_config
54     if [ -n "$PLC_DEVEL_FEDORA_URL" ] ; then
55         mirrors=($PLC_DEVEL_FEDORA_URL)
56     fi
57 fi
58
59 usage()
60 {
61     echo "Usage: mkfedora [OPTION]... [basedir]"
62     echo "      -l url          Fedora mirror location. Defaults to try:"
63     for mirror in "${mirrors[@]}" ; do
64         echo "                  $mirror"
65     done
66     echo "      -r release      Fedora release number (default: $releasever)"
67     echo "      -a arch         Fedora architecture (default: $basearch)"
68     echo "      -g group1 -g group2 ..."
69     echo "                      Yumgroups to install (default: none)"
70     echo "      -p package1 -p package2 ..."
71     echo "                      Additional packages to install (default: none)"
72     echo "      -x package1 -x package2 ..."
73     echo "                      Packages to exclude (default: none)"
74     echo "      -k              Exclude kernel* packages from all repositories except bootstrap"
75     echo "      -v              Be verbose"
76     echo "      -h              This message"
77     exit 1
78 }
79
80 # Get options
81 while getopts "l:r:a:g:p:x:kvh" opt ; do
82     case $opt in
83         l)
84             if echo $OPTARG | grep -q -i '^\(file\|http[s]*\)://' ; then
85                 mirrors=($OPTARG)
86             else
87                 mirrors=(file://$OPTARG)
88             fi
89             ;;
90         r)
91             releasever=$OPTARG
92             ;;
93         a)
94             basearch=$OPTARG
95             ;;
96         g)
97             groups[${#groups[*]}]="$OPTARG"
98             ;;
99         p)
100             packages[${#packages[*]}]="$OPTARG"
101             ;;
102         x)
103             exclude[${#exclude[*]}]="$OPTARG"
104             ;;
105         k)
106             exclude_kernel="exclude=kernel* ulogd iptables"
107             ;;
108         v)
109             verbose=1
110             set -x
111             ;;
112         h|*)
113             usage
114             ;;
115     esac
116 done
117
118 shift $(($OPTIND - 1))
119 if [ ! -d "$1" ] ; then
120     usage
121 fi
122
123 vroot=$(cd $1 && pwd -P)
124
125 if [ $UID -ne 0 ] ; then
126     echo "Error: You must run this script as root."
127     exit 1
128 fi
129
130 fetch ()
131 {
132     curl --fail --silent --max-time 60 "$1"
133 }
134
135 # hard to find two mirrors with a similar layout
136 # set list of attempted locations according to releasever
137 if [ $releasever -ge 7 ] ; then
138     attempts="
139 $releasever/Everything/$basearch/os
140 core/$releasever/Everything/$basearch/os \
141 linux/core/$releasever/$basearch/os
142 "
143 else
144     attempts="
145 linux/core/$releasever/$basearch/os 
146 core/$releasever/$basearch/os 
147 $releasever/$basearch/os
148 "
149 fi
150
151 for mirror in "${mirrors[@]}" ; do
152     for attempt in $attempts; do 
153         baseurl=$mirror/$attempt
154         if fetch $baseurl/repodata/repomd.xml >/dev/null ; then
155             break
156         fi
157         unset baseurl
158     done
159     if [ -n "$baseurl" ] ; then
160         break
161     fi
162     unset baseurl
163 done
164
165 if [ -z "$baseurl" ] ; then
166     echo "Error: $releasever/$basearch/os/repodata/repomd.xml"
167     echo "       could not be found in any of the following locations:"
168     echo
169     for mirror in ${mirrors[@]} ; do
170         for attempt in $attempts ; do
171             echo $mirror/$attempt
172         done
173     done
174     echo
175     usage
176 fi
177
178 exec 3>&1
179 exec 4>&2
180 if [ $verbose -eq 0 ] ; then
181     exec 1>/dev/null
182     exec 2>/dev/null
183 fi
184
185 # Do not tolerate errors
186 set -e
187
188 # Mount /dev/pts and /dev/shm in reference image
189 mount -t devpts none $vroot/dev/pts
190 mount -t tmpfs none $vroot/dev/shm
191
192 # xxx fixme : do this mount/unmount thing for fc4 only
193 if [ "$releasever" -lt 6 ] ; then
194     ## make rpms ignore installing stuff to /proc
195     mkdir -p $vroot/etc/rpm
196     # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
197     # you have to use at least one language beside 'C'
198     echo "%_install_langs         C:de:en:es:fr" > $vroot/etc/rpm/macros
199     echo "%_netsharedpath /proc" >> $vroot/etc/rpm/macros
200     # Mount /proc in reference image
201     mkdir -p $vroot/proc
202     mount -t proc none $vroot/proc
203 fi
204
205 cleanup ()
206 {
207     # xxx fixme : do this mount/unmount thing for fc4 only
208     if [ "$releasever" -lt 6 ] ; then
209         umount -l $vroot/proc
210     fi
211     umount -l $vroot/dev/shm
212     umount -l $vroot/dev/pts
213 }
214
215 # Clean up before exiting if anything goes wrong
216 trap "cleanup" ERR INT
217
218 # Create a /var/lib dirs for yum & rpm
219 mkdir -p $vroot/var/lib/yum
220 mkdir -p $vroot/var/lib/rpm
221 mkdir -p $vroot/usr/share/info
222
223 # Create a dummy /etc/fstab in reference image
224 mkdir -p $vroot/etc
225 cat >$vroot/etc/fstab <<EOF
226 # This fake fstab exists only to please df and linuxconf.
227 /dev/hdv1       /       ext2    defaults        1 1
228 EOF
229 cp $vroot/etc/fstab $vroot/etc/mtab
230
231 # Prevent all locales from being installed in reference image
232 mkdir -p $vroot/etc/rpm
233 cat >$vroot/etc/rpm/macros <<EOF
234 %_install_langs en_US:en
235 %_excludedocs 1
236 %__file_context_path /dev/null
237 EOF
238
239 # Necessary for some scripts
240 mkdir -p $vroot/etc/sysconfig
241 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
242
243 # Trick rpm and yum, who read the real root /etc/rpm/macros file
244 # rather than the one installed in the reference image, despite what
245 # you might expect the --root and --installroot options to mean. Both
246 # programs always read $HOME/.rpmmacros.
247 export HOME=$vroot/tmp
248 mkdir -p $vroot/tmp
249 cp $vroot/etc/rpm/macros $vroot/tmp/.rpmmacros
250
251 # Initialize RPM database in reference image
252 mkdir -p $vroot/var/lib/rpm
253 rpm --root $vroot --initdb
254 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
255
256 # Initialize yum in reference image
257 mkdir -p $vroot/var/cache/yum $vroot/var/log
258 if [ $releasever -lt 7 ] ; then
259         corename="Core "
260 else
261         corename=""
262 fi
263
264 cat >$vroot/etc/yum.conf <<EOF
265 [main]
266 cachedir=/var/cache/yum
267 debuglevel=2
268 logfile=/var/log/yum.log
269 pkgpolicy=newest
270 distroverpkg=redhat-release
271 tolerant=1
272 exactarch=1
273 retries=20
274 obsoletes=1
275 gpgcheck=0
276 # Prevent yum-2.4 from loading additional repository definitions
277 # (e.g., from /etc/yum.repos.d/)
278 reposdir=/dev/null
279
280 [base]
281 name=Fedora ${corename}${releasever} - $basearch - base
282 baseurl=$baseurl/
283 $exclude_kernel
284 EOF
285
286 for optional in updates extras ; do
287     for optionalurl in \
288         $mirror/linux/core/$optional/$releasever/$basearch \
289         $mirror/core/$optional/$releasever/$basearch \
290         $mirror/linux/$optional/$releasever/$basearch \
291         $mirror/$optional/$releasever/$basearch ; do
292         if fetch $optionalurl/repodata/repomd.xml ; then
293             cat >>$vroot/etc/yum.conf <<EOF
294
295 [$(basename $optional)]
296 name=Fedora ${corename}${releasever} - $basearch - $(basename $optional)
297 baseurl=$optionalurl/
298 $exclude_kernel
299 EOF
300             break
301         fi
302     done
303 done
304
305 # If we are being built as part of an automated RPM build, solve the
306 # bootstrap problem by including any just built packages in the yum
307 # configuration. This cooperates with the PlanetLab build system.
308 if [ -n "$RPM_BUILD_DIR" ] ; then
309     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
310     # yum-2.0.x
311     if [ -x /usr/bin/yum-arch ] ; then
312         yum-arch -q $RPM_RPMS_DIR
313     fi
314     # yum-2.4.x
315     if [ -x /usr/bin/createrepo ] ; then
316         if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
317             groupfile="-g yumgroups.xml"
318         fi
319         createrepo --quiet $groupfile $RPM_RPMS_DIR
320     fi
321     # If run under sudo, allow user to delete the headers/ and
322     # repodata/ directories.
323     if [ -n "$SUDO_USER" ] ; then
324         chown -R $SUDO_USER $RPM_RPMS_DIR
325     fi
326     cat >>$vroot/etc/yum.conf <<EOF
327
328 [bootstrap]
329 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
330 baseurl=file://$RPM_RPMS_DIR/
331 EOF
332 fi
333
334 excludes=
335 for package in "${exclude[@]}" ; do
336     excludes="$excludes --exclude=$package"
337 done
338
339 # glibc must be specified explicitly for the correct arch to be
340 # chosen.
341 echo "* Installing glibc" >&3
342 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes install glibc
343
344 # Go, baby, go
345 if [ ${#packages[*]} -gt 0 ] ; then
346    echo "* Installing optional packages" "${packages[@]}" >&3
347    yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
348           install "${packages[@]}"
349    if ! rpm --root $vroot -q "${packages[@]}" >/dev/null ; then
350        echo "* Warning: Missing packages"
351        rpm --root $vroot -q "${packages[@]}" | grep "not installed"
352    fi
353 fi
354
355 if [ ${#groups[*]} -gt 0 ] ; then
356    ## call yum sequentially to get finer-grained info on dependencies
357    for grp in "${groups[@]}" ; do
358       echo "* Installing optional group $grp" >&3
359       yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
360         groupinstall "$grp"
361    done
362 fi
363
364 # FC2 dev %preinstall checks /proc/mounts to make sure that /dev is
365 # not currently mounted as devfs. If it thinks it is, it will refuse
366 # to install the package. On a modern system running udev that mounts
367 # /dev as tmpfs, this check fails. Since we are installing into a
368 # chroot, whether /dev is mounted on the host system or not doesn't
369 # matter. If dev was explicitly mentioned in the packages list, force
370 # its installation.
371 if [ "$releasever" = "2" ] ; then
372     for package in "${packages[@]}" ; do
373         if [ "$package" = "dev" ] && ! rpm --root $vroot -q dev >/dev/null 2>&1 ; then
374             rpm --root $vroot -Uvh --noscripts $baseurl/Fedora/RPMS/dev-3.3.13-1.i386.rpm
375             break
376         fi
377     done
378 fi
379
380 # Clean yum cache
381 echo "* Cleaning up" >&3
382 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
383     clean all
384
385 # Clean RPM state
386 rm -f $vroot/var/lib/rpm/__db*
387
388 # Set time zone to UTC
389 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
390     rm -f $vroot/etc/localtime
391     ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
392 fi
393
394
395 # remove trap handler, as we are about to call it directly.
396 trap - ERR INT
397
398 # Clean up
399 cleanup
400
401 exit 0