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