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