This commit was manufactured by cvs2svn to create tag
[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: build.sh,v 1.2 2005/10/01 18:20:08 mlhuang 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:///usr/share/mirrors/fedora
27 file:///var/www/html/mirrors/fedora
28 ftp://smoke.cs.princeton.edu/pub/mirrors/fedora
29 ftp://128.112.137.30/pub/mirrors/fedora
30 http://coblitz.planet-lab.org/pub/fedora
31 ftp://mirror.cs.princeton.edu/pub/mirrors/fedora
32 )
33
34 # Release and architecture to install
35 releasever=2
36 basearch=i386
37
38 # Yum groups to install
39 groups=(
40 "Core and Base"
41 )
42
43 # Packages to install
44 packages=()
45
46 usage()
47 {
48     echo "Usage: mkfedora [OPTION]... [basedir]"
49     echo "      -l url          Fedora mirror location. Defaults to try:"
50     for mirror in "${mirrors[@]}" ; do
51         echo "                  $mirror"
52     done
53     echo "      -r release      Fedora release number (default: $releasever)"
54     echo "      -a arch         Fedora architecture (default: $basearch)"
55     echo "      -g group1 -g group2 ..."
56     echo "                      Additional yumgroups to install. Defaults:"
57     for group in "${groups[@]}" ; do
58         echo "                  $group"
59     done
60     echo "      -p package1 -p package2 ..."
61     echo "                      Additional packages to install (default: none)"
62     echo "      -v              Be verbose"
63     echo "      -h              This message"
64     exit 1
65 }
66
67 # Get options
68 while getopts "l:r:a:g:p:vh" opt ; do
69     case $opt in
70         l)
71             if echo $OPTARG | grep -q -i '^\(file\|http[s]*\)://' ; then
72                 mirrors=($OPTARG)
73             else
74                 mirrors=(file://$OPTARG)
75             fi
76             ;;
77         r)
78             releasever=$OPTARG
79             ;;
80         a)
81             basearch=$OPTARG
82             ;;
83         g)
84             groups[${#groups[*]}]="$OPTARG"
85             ;;
86         p)
87             packages[${#packages[*]}]="$OPTARG"
88             ;;
89         v)
90             verbose=1
91             set -x
92             ;;
93         h|*)
94             usage
95             ;;
96     esac
97 done
98
99 shift $(($OPTIND - 1))
100 if [ ! -d "$1" ] ; then
101     usage
102 fi
103
104 vroot=$(cd $1 && pwd -P)
105
106 if [ $UID -ne 0 ] ; then
107     echo "Error: You must run this script as root."
108     exit 1
109 fi
110
111 # Old versions of curl don't understand file:// URLs
112 fetch ()
113 {
114     url=$1
115     if curl --fail --silent --max-time 60 $url ; then
116         return 0
117     else
118         if [ -f ${url#file://} ] ; then
119             cat ${url#file://}
120             return 0
121         fi
122     fi
123     return 1
124 }
125
126 for mirror in "${mirrors[@]}" ; do
127     baseurl=$mirror/linux/core/$releasever/$basearch/os
128     if fetch $baseurl/repodata/repomd.xml >/dev/null ; then
129         break
130     fi
131     unset baseurl
132 done
133
134 if [ -z "$baseurl" ] ; then
135     echo "Error: linux/core/$releasever/$basearch/os/repodata/repomd.xml"
136     echo "       could not be found in any of the following locations:"
137     echo
138     for mirror in ${mirrors[@]} ; do
139         echo $mirror
140     done
141     echo
142     usage
143 fi
144
145 exec 3>&1
146 exec 4>&2
147 if [ $verbose -eq 0 ] ; then
148     exec 1>/dev/null
149     exec 2>/dev/null
150 fi
151
152 # Scratch space
153 tmp=$(mktemp -d /tmp/mkfedora.XXXXXX)
154
155 # Initialize /dev in reference image
156 mkdir -p $vroot/dev
157 mknod -m 666 $vroot/dev/null c 1 3
158 mknod -m 666 $vroot/dev/zero c 1 5
159 mknod -m 666 $vroot/dev/full c 1 7
160 mknod -m 644 $vroot/dev/random c 1 8
161 mknod -m 644 $vroot/dev/urandom c 1 9
162 mknod -m 666 $vroot/dev/tty c 5 0
163 mknod -m 666 $vroot/dev/ptmx c 5 2
164 # For bash command substitution
165 ln -nsf ../proc/self/fd $vroot/dev/fd
166 # For df and linuxconf
167 touch $vroot/dev/hdv1
168
169 # Mount /dev/pts in reference image
170 mkdir -p $vroot/dev/pts
171 mount -t devpts none $vroot/dev/pts
172
173 # Mount /dev/shm in reference image
174 mkdir -p $vroot/dev/shm
175 mount -t tmpfs none $vroot/dev/shm
176
177 # Mount /proc in reference image
178 mkdir -p $vroot/proc
179 mount -t proc none $vroot/proc
180
181 cleanup ()
182 {
183     umount $vroot/proc
184     umount $vroot/dev/shm
185     umount $vroot/dev/pts
186     rm -rf $tmp
187 }
188
189 # Clean up before exiting if anything goes wrong
190 trap "cleanup; exit 1" ERR
191
192 # Create a dummy /etc/fstab in reference image
193 mkdir -p $vroot/etc
194 cat >$vroot/etc/fstab <<EOF
195 # This fake fstab exists only to please df and linuxconf.
196 /dev/hdv1       /       ext2    defaults        1 1
197 EOF
198 cp $vroot/etc/fstab $vroot/etc/mtab
199
200 # Prevent all locales from being installed in reference image
201 mkdir -p $vroot/etc/rpm
202 cat >$vroot/etc/rpm/macros <<EOF
203 %_install_langs en_US:en
204 %_excludedocs 1
205 %__file_context_path /dev/null
206 EOF
207
208 # Necessary for some scripts
209 mkdir -p $vroot/etc/sysconfig
210 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
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 # Initialize RPM database in reference image
221 mkdir -p $vroot/var/lib/rpm
222 rpm --root $vroot --initdb
223 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
224
225 # Initialize yum in reference image
226 mkdir -p $vroot/var/cache/yum $vroot/var/log
227 cat >$vroot/etc/yum.conf <<EOF
228 [main]
229 cachedir=/var/cache/yum
230 debuglevel=2
231 logfile=/var/log/yum.log
232 pkgpolicy=newest
233 distroverpkg=redhat-release
234 tolerant=1
235 exactarch=1
236 retries=20
237 obsoletes=1
238 gpgcheck=0
239 # Prevent yum-2.4 from loading additional repository definitions
240 # (e.g., from /etc/yum.repos.d/)
241 reposdir=/dev/null
242
243 [base]
244 name=Fedora Core $releasever - $basearch - base
245 baseurl=$baseurl/
246 EOF
247
248 for optional in core/updates extras ; do
249     if fetch $mirror/linux/$optional/$releasever/$basearch/repodata/repomd.xml ; then
250         cat >>$vroot/etc/yum.conf <<EOF
251
252 [$(basename $optional)]
253 name=Fedora Core $releasever - $basearch - $(basename $optional)
254 baseurl=$mirror/linux/$optional/$releasever/$basearch/
255 EOF
256     fi
257 done
258
259 # If we are being built as part of an automated RPM build, solve the
260 # bootstrap problem by including any just built packages in the yum
261 # configuration. This cooperates with the PlanetLab build system.
262 if [ -n "$RPM_BUILD_DIR" ] ; then
263     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
264     yum-arch $RPM_RPMS_DIR || :
265     createrepo $RPM_RPMS_DIR || :
266     # If run under sudo, allow user to delete the headers/ and
267     # repodata/ directories.
268     if [ -n "$SUDO_USER" ] ; then
269         chown -R $SUDO_USER $RPM_RPMS_DIR
270     fi
271     cat >>$vroot/etc/yum.conf <<EOF
272
273 [bootstrap]
274 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
275 baseurl=file://$RPM_RPMS_DIR/
276 EOF
277
278     # XXX Build system should generate yumgroups.xml automatically
279 fi
280
281 # The "Core" and "Base" groups are not uservisible by default in
282 # comps.xml, and old (Fedora Core 2) versions of yum will refuse to
283 # recognize them explicitly.
284 mkdir -p $tmp/corebase
285 cat >$tmp/corebase/yumgroups.xml <<EOF
286 <?xml version="1.0"?>
287 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
288 <comps>
289   <group>
290     <id>corebase</id>
291     <name>Core and Base</name>
292     <default>true</default>
293     <description>Core and Base Packages</description>
294     <uservisible>true</uservisible>
295     <grouplist>
296       <groupreq>core</groupreq>
297       <groupreq>base</groupreq>
298     </grouplist>
299   </group>
300 </comps>
301 EOF
302
303 # yum-2.0.x
304 yum-arch $tmp/corebase || :
305 # yum-2.4.x
306 createrepo -g yumgroups.xml $tmp/corebase || :
307
308 cat >>$vroot/etc/yum.conf <<EOF
309
310 [corebase]
311 name=Core and Base
312 baseurl=file://$tmp/corebase/
313 EOF
314
315 echo -n "* Installing base system..." >&3
316
317 # glibc must be specified explicitly for the correct arch to be chosen.
318 yum -c $vroot/etc/yum.conf --installroot=$vroot -y install glibc
319
320 # Go, baby, go
321 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
322     groupinstall "${groups[@]}"
323
324 echo "done" >&3
325
326 if [ ${#packages[*]} -gt 0 ] ; then
327     echo -n "* Installing optional packages..."
328     yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
329         install "${packages[@]}"
330     echo "done"
331 fi
332
333 # Clean yum cache
334 echo -n "* Cleaning up..." >&3
335 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
336     clean all
337 echo "done" >&3
338
339 # Clean RPM state
340 rm -f $vroot/var/lib/rpm/__db*
341
342 # Disable all services in reference image
343 chroot $vroot /bin/sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
344
345 # Clean up
346 cleanup
347
348 exit 0