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