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