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