As of release version 7, Fedora dropped 'core'
[build.git] / mkfedora
1 #!/bin/bash
2 #
3 # Builds a Fedora Core 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: mkfedora,v 1.29 2007/08/24 04:47:00 mef Exp $
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 ftp://smoke.cs.princeton.edu/pub/mirrors/fedora
28 http://coblitz.codeen.org/coblitz.planet-lab.org/pub/fedora
29 ftp://mirror.cs.princeton.edu/pub/mirrors/fedora
30 http://coblitz.planet-lab.org/pub/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 for mirror in "${mirrors[@]}" ; do
136     if [ $releasever -ge 7 ] ; then
137         for baseurl in \
138                 $mirror/$releasever/Everything/$basearch/os ; do
139                 if fetch $baseurl/repodata/repomd.xml >/dev/null ; then
140                     break
141                 fi
142                 unset baseurl
143         done
144     else
145         for baseurl in \
146                 $mirror/linux/core/$releasever/$basearch/os \
147                 $mirror/core/$releasever/$basearch/os \
148                 $mirror/$releasever/$basearch/os ; do
149                 if fetch $baseurl/repodata/repomd.xml >/dev/null ; then
150                         break
151                 fi
152                 unset baseurl
153         done
154     fi
155     if [ -n "$baseurl" ] ; then
156         break
157     fi
158     unset baseurl
159 done
160
161 if [ -z "$baseurl" ] ; then
162     if [ $releasever -ge 7 ] ; then
163         echo "Error: $releasever/Everything/$basearch/os/repodata/repomd.xml"
164         echo "       could not be found in any of the following locations:"
165         echo
166         for mirror in ${mirrors[@]} ; do
167                 echo $mirror/linux
168         done
169     else
170         echo "Error: $releasever/$basearch/os/repodata/repomd.xml"
171         echo "       could not be found in any of the following locations:"
172         echo
173         for mirror in ${mirrors[@]} ; do
174                 echo $mirror/linux/core
175                 echo $mirror/core
176                 echo $mirror
177         done
178     fi
179     echo
180     usage
181 fi
182
183 exec 3>&1
184 exec 4>&2
185 if [ $verbose -eq 0 ] ; then
186     exec 1>/dev/null
187     exec 2>/dev/null
188 fi
189
190 # Minimally initialize /dev in reference image. If installed, the dev
191 # or udev RPMs will fill in the rest.
192 mkdir -p $vroot/dev
193 mknod -m 666 $vroot/dev/null c 1 3
194 mknod -m 666 $vroot/dev/zero c 1 5
195 mknod -m 666 $vroot/dev/full c 1 7
196 mknod -m 644 $vroot/dev/random c 1 8
197 mknod -m 644 $vroot/dev/urandom c 1 9
198 mknod -m 666 $vroot/dev/tty c 5 0
199 mknod -m 666 $vroot/dev/ptmx c 5 2
200 # For bash command substitution
201 ln -nsf ../proc/self/fd $vroot/dev/fd
202 # For df and linuxconf
203 touch $vroot/dev/hdv1
204 # For mkinitrd (in case a kernel is being installed)
205 for i in $(seq 0 7) ; do
206     mknod -m 640 $vroot/dev/loop$i b 7 $i
207 done
208
209 # Do not tolerate errors
210 set -e
211
212 # Mount /dev/pts in reference image
213 mkdir -p $vroot/dev/pts
214 mount -t devpts none $vroot/dev/pts
215
216 # Mount /dev/shm in reference image
217 mkdir -p $vroot/dev/shm
218 mount -t tmpfs none $vroot/dev/shm
219
220 # Mount /proc in reference image
221 mkdir -p $vroot/proc
222 mount -t proc none $vroot/proc
223
224 cleanup ()
225 {
226     umount $vroot/proc
227     umount $vroot/dev/shm
228     umount $vroot/dev/pts
229 }
230
231 # Clean up before exiting if anything goes wrong
232 trap "cleanup" ERR INT
233
234 # Create a dummy /etc/fstab in reference image
235 mkdir -p $vroot/etc
236 cat >$vroot/etc/fstab <<EOF
237 # This fake fstab exists only to please df and linuxconf.
238 /dev/hdv1       /       ext2    defaults        1 1
239 EOF
240 cp $vroot/etc/fstab $vroot/etc/mtab
241
242 # Prevent all locales from being installed in reference image
243 mkdir -p $vroot/etc/rpm
244 cat >$vroot/etc/rpm/macros <<EOF
245 %_install_langs en_US:en
246 %_excludedocs 1
247 %__file_context_path /dev/null
248 EOF
249
250 # Necessary for some scripts
251 mkdir -p $vroot/etc/sysconfig
252 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
253
254 # Trick rpm and yum, who read the real root /etc/rpm/macros file
255 # rather than the one installed in the reference image, despite what
256 # you might expect the --root and --installroot options to mean. Both
257 # programs always read $HOME/.rpmmacros.
258 export HOME=$vroot/tmp
259 mkdir -p $vroot/tmp
260 cp $vroot/etc/rpm/macros $vroot/tmp/.rpmmacros
261
262 # Initialize RPM database in reference image
263 mkdir -p $vroot/var/lib/rpm
264 rpm --root $vroot --initdb
265 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
266
267 # Initialize yum in reference image
268 mkdir -p $vroot/var/cache/yum $vroot/var/log
269 cat >$vroot/etc/yum.conf <<EOF
270 [main]
271 cachedir=/var/cache/yum
272 debuglevel=2
273 logfile=/var/log/yum.log
274 pkgpolicy=newest
275 distroverpkg=redhat-release
276 tolerant=1
277 exactarch=1
278 retries=20
279 obsoletes=1
280 gpgcheck=0
281 # Prevent yum-2.4 from loading additional repository definitions
282 # (e.g., from /etc/yum.repos.d/)
283 reposdir=/dev/null
284
285 [base]
286 name=Fedora Core $releasever - $basearch - base
287 baseurl=$baseurl/
288 $exclude_kernel
289 EOF
290
291 for optional in updates extras ; do
292     for optionalurl in \
293         $mirror/linux/core/$optional/$releasever/$basearch \
294         $mirror/core/$optional/$releasever/$basearch \
295         $mirror/linux/$optional/$releasever/$basearch \
296         $mirror/$optional/$releasever/$basearch ; do
297         if fetch $optionalurl/repodata/repomd.xml ; then
298             cat >>$vroot/etc/yum.conf <<EOF
299
300 [$(basename $optional)]
301 name=Fedora Core $releasever - $basearch - $(basename $optional)
302 baseurl=$optionalurl/
303 $exclude_kernel
304 EOF
305             break
306         fi
307     done
308 done
309
310 # If we are being built as part of an automated RPM build, solve the
311 # bootstrap problem by including any just built packages in the yum
312 # configuration. This cooperates with the PlanetLab build system.
313 if [ -n "$RPM_BUILD_DIR" ] ; then
314     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
315     # yum-2.0.x
316     if [ -x /usr/bin/yum-arch ] ; then
317         yum-arch -q $RPM_RPMS_DIR
318     fi
319     # yum-2.4.x
320     if [ -x /usr/bin/createrepo ] ; then
321         if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
322             groupfile="-g yumgroups.xml"
323         fi
324         createrepo --quiet $groupfile $RPM_RPMS_DIR
325     fi
326     # If run under sudo, allow user to delete the headers/ and
327     # repodata/ directories.
328     if [ -n "$SUDO_USER" ] ; then
329         chown -R $SUDO_USER $RPM_RPMS_DIR
330     fi
331     cat >>$vroot/etc/yum.conf <<EOF
332
333 [bootstrap]
334 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
335 baseurl=file://$RPM_RPMS_DIR/
336 EOF
337 fi
338
339 excludes=
340 for package in "${exclude[@]}" ; do
341     excludes="$excludes --exclude=$package"
342 done
343
344 # glibc must be specified explicitly for the correct arch to be
345 # chosen.
346 echo "* Installing glibc" >&3
347 yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes install glibc
348
349 # Go, baby, go
350 if [ ${#packages[*]} -gt 0 ] ; then
351    echo "* Installing optional packages" "${packages[@]}" >&3
352    yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
353           install "${packages[@]}"
354    if ! rpm --root $vroot -q "${packages[@]}" >/dev/null ; then
355        echo "* Warning: Missing packages"
356        rpm --root $vroot -q "${packages[@]}" | grep "not installed"
357    fi
358 fi
359
360 if [ ${#groups[*]} -gt 0 ] ; then
361    ## call yum sequentially to get finer-grained info on dependencies
362    for grp in "${groups[@]}" ; do
363       echo "* Installing optional group $grp" >&3
364       yum -c $vroot/etc/yum.conf --installroot=$vroot -y $excludes \
365         groupinstall "$grp"
366    done
367 fi
368
369 # FC2 dev %preinstall checks /proc/mounts to make sure that /dev is
370 # not currently mounted as devfs. If it thinks it is, it will refuse
371 # to install the package. On a modern system running udev that mounts
372 # /dev as tmpfs, this check fails. Since we are installing into a
373 # chroot, whether /dev is mounted on the host system or not doesn't
374 # matter. If dev was explicitly mentioned in the packages list, force
375 # its installation.
376 if [ "$releasever" = "2" ] ; then
377     for package in "${packages[@]}" ; do
378         if [ "$package" = "dev" ] && ! rpm --root $vroot -q dev >/dev/null 2>&1 ; then
379             rpm --root $vroot -Uvh --noscripts $baseurl/Fedora/RPMS/dev-3.3.13-1.i386.rpm
380             break
381         fi
382     done
383 fi
384
385 # Clean yum cache
386 echo "* Cleaning up" >&3
387 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
388     clean all
389
390 # Clean RPM state
391 rm -f $vroot/var/lib/rpm/__db*
392
393 # Set time zone to UTC
394 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
395     rm -f $vroot/etc/localtime
396     ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
397 fi
398
399 # Clean up
400 cleanup
401
402 exit 0