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