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