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