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