d403caf792e9ce6400f1c00c59c38b8e11a49cbb
[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: mkfedora,v 1.3 2006/03/29 19:19:19 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
41 # Packages to install
42 packages=()
43
44 usage()
45 {
46     echo "Usage: mkfedora [OPTION]... [basedir]"
47     echo "      -l url          Fedora mirror location. Defaults to try:"
48     for mirror in "${mirrors[@]}" ; do
49         echo "                  $mirror"
50     done
51     echo "      -r release      Fedora release number (default: $releasever)"
52     echo "      -a arch         Fedora architecture (default: $basearch)"
53     echo "      -g group1 -g group2 ..."
54     echo "                      Yumgroups to install (default: none)"
55     echo "      -p package1 -p package2 ..."
56     echo "                      Additional packages to install (default: none)"
57     echo "      -v              Be verbose"
58     echo "      -h              This message"
59     exit 1
60 }
61
62 # Get options
63 while getopts "l:r:a:g:p:vh" opt ; do
64     case $opt in
65         l)
66             if echo $OPTARG | grep -q -i '^\(file\|http[s]*\)://' ; then
67                 mirrors=($OPTARG)
68             else
69                 mirrors=(file://$OPTARG)
70             fi
71             ;;
72         r)
73             releasever=$OPTARG
74             ;;
75         a)
76             basearch=$OPTARG
77             ;;
78         g)
79             groups[${#groups[*]}]="$OPTARG"
80             ;;
81         p)
82             packages[${#packages[*]}]="$OPTARG"
83             ;;
84         v)
85             verbose=1
86             set -x
87             ;;
88         h|*)
89             usage
90             ;;
91     esac
92 done
93
94 shift $(($OPTIND - 1))
95 if [ ! -d "$1" ] ; then
96     usage
97 fi
98
99 vroot=$(cd $1 && pwd -P)
100
101 if [ $UID -ne 0 ] ; then
102     echo "Error: You must run this script as root."
103     exit 1
104 fi
105
106 # Old versions of curl don't understand file:// URLs
107 fetch ()
108 {
109     url=$1
110     if curl --fail --silent --max-time 60 $url ; then
111         return 0
112     else
113         if [ -f ${url#file://} ] ; then
114             cat ${url#file://}
115             return 0
116         fi
117     fi
118     return 1
119 }
120
121 for mirror in "${mirrors[@]}" ; do
122     baseurl=$mirror/linux/core/$releasever/$basearch/os
123     if fetch $baseurl/repodata/repomd.xml >/dev/null ; then
124         break
125     fi
126     unset baseurl
127 done
128
129 if [ -z "$baseurl" ] ; then
130     echo "Error: linux/core/$releasever/$basearch/os/repodata/repomd.xml"
131     echo "       could not be found in any of the following locations:"
132     echo
133     for mirror in ${mirrors[@]} ; do
134         echo $mirror
135     done
136     echo
137     usage
138 fi
139
140 exec 3>&1
141 exec 4>&2
142 if [ $verbose -eq 0 ] ; then
143     exec 1>/dev/null
144     exec 2>/dev/null
145 fi
146
147 # Scratch space
148 tmp=$(mktemp -d /tmp/mkfedora.XXXXXX)
149
150 # Minimally initialize /dev in reference image. If installed, the dev
151 # or udev RPMs will fill in the rest.
152 mkdir -p $vroot/dev
153 mknod -m 666 $vroot/dev/null c 1 3
154 mknod -m 666 $vroot/dev/zero c 1 5
155 mknod -m 666 $vroot/dev/full c 1 7
156 mknod -m 644 $vroot/dev/random c 1 8
157 mknod -m 644 $vroot/dev/urandom c 1 9
158 mknod -m 666 $vroot/dev/tty c 5 0
159 mknod -m 666 $vroot/dev/ptmx c 5 2
160 # For bash command substitution
161 ln -nsf ../proc/self/fd $vroot/dev/fd
162 # For df and linuxconf
163 touch $vroot/dev/hdv1
164
165 # Mount /dev/pts in reference image
166 mkdir -p $vroot/dev/pts
167 mount -t devpts none $vroot/dev/pts
168
169 # Mount /dev/shm in reference image
170 mkdir -p $vroot/dev/shm
171 mount -t tmpfs none $vroot/dev/shm
172
173 # Mount /proc in reference image
174 mkdir -p $vroot/proc
175 mount -t proc none $vroot/proc
176
177 cleanup ()
178 {
179     umount $vroot/proc
180     umount $vroot/dev/shm
181     umount $vroot/dev/pts
182     rm -rf $tmp
183 }
184
185 # Clean up before exiting if anything goes wrong
186 trap "cleanup; exit 1" ERR
187
188 # Create a dummy /etc/fstab in reference image
189 mkdir -p $vroot/etc
190 cat >$vroot/etc/fstab <<EOF
191 # This fake fstab exists only to please df and linuxconf.
192 /dev/hdv1       /       ext2    defaults        1 1
193 EOF
194 cp $vroot/etc/fstab $vroot/etc/mtab
195
196 # Prevent all locales from being installed in reference image
197 mkdir -p $vroot/etc/rpm
198 cat >$vroot/etc/rpm/macros <<EOF
199 %_install_langs en_US:en
200 %_excludedocs 1
201 %__file_context_path /dev/null
202 EOF
203
204 # Necessary for some scripts
205 mkdir -p $vroot/etc/sysconfig
206 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
207
208 # Trick rpm and yum, who read the real root /etc/rpm/macros file
209 # rather than the one installed in the reference image, despite what
210 # you might expect the --root and --installroot options to mean. Both
211 # programs always read $HOME/.rpmmacros.
212 export HOME=$vroot/tmp
213 mkdir -p $vroot/tmp
214 cp $vroot/etc/rpm/macros $vroot/tmp/.rpmmacros
215
216 # Initialize RPM database in reference image
217 mkdir -p $vroot/var/lib/rpm
218 rpm --root $vroot --initdb
219 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
220
221 # Initialize yum in reference image
222 mkdir -p $vroot/var/cache/yum $vroot/var/log
223 cat >$vroot/etc/yum.conf <<EOF
224 [main]
225 cachedir=/var/cache/yum
226 debuglevel=2
227 logfile=/var/log/yum.log
228 pkgpolicy=newest
229 distroverpkg=redhat-release
230 tolerant=1
231 exactarch=1
232 retries=20
233 obsoletes=1
234 gpgcheck=0
235 # Prevent yum-2.4 from loading additional repository definitions
236 # (e.g., from /etc/yum.repos.d/)
237 reposdir=/dev/null
238
239 [base]
240 name=Fedora Core $releasever - $basearch - base
241 baseurl=$baseurl/
242 EOF
243
244 for optional in core/updates extras ; do
245     if fetch $mirror/linux/$optional/$releasever/$basearch/repodata/repomd.xml ; then
246         cat >>$vroot/etc/yum.conf <<EOF
247
248 [$(basename $optional)]
249 name=Fedora Core $releasever - $basearch - $(basename $optional)
250 baseurl=$mirror/linux/$optional/$releasever/$basearch/
251 EOF
252     fi
253 done
254
255 # If we are being built as part of an automated RPM build, solve the
256 # bootstrap problem by including any just built packages in the yum
257 # configuration. This cooperates with the PlanetLab build system.
258 if [ -n "$RPM_BUILD_DIR" ] ; then
259     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
260     # yum-2.0.x
261     if [ -x /usr/bin/yum-arch ] ; then
262         yum-arch $RPM_RPMS_DIR
263     fi
264     # yum-2.4.x
265     if [ -x /usr/bin/createrepo ] ; then
266         if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
267             groupfile="-g yumgroups.xml"
268         fi
269         createrepo $groupfile $RPM_RPMS_DIR
270     fi
271     # If run under sudo, allow user to delete the headers/ and
272     # repodata/ directories.
273     if [ -n "$SUDO_USER" ] ; then
274         chown -R $SUDO_USER $RPM_RPMS_DIR
275     fi
276     cat >>$vroot/etc/yum.conf <<EOF
277
278 [bootstrap]
279 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
280 baseurl=file://$RPM_RPMS_DIR/
281 EOF
282 fi
283
284 # glibc must be specified explicitly for the correct arch to be chosen.
285 echo "* Installing glibc" >&3
286 yum -c $vroot/etc/yum.conf --installroot=$vroot -y install glibc
287
288 # Go, baby, go
289 if [ ${#groups[*]} -gt 0 ] ; then
290     echo "* Installing optional groups" >&3
291     yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
292         groupinstall "${groups[@]}"
293 fi
294
295 if [ ${#packages[*]} -gt 0 ] ; then
296     echo "* Installing optional packages" >&3
297     yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
298         install "${packages[@]}"
299 fi
300
301 # FC2 dev %preinstall checks /proc/mounts to make sure that /dev is
302 # not currently mounted as devfs. If it thinks it is, it will refuse
303 # to install the package. On a modern system running udev that mounts
304 # /dev as tmpfs, this check fails. Since we are installing into a
305 # chroot, whether /dev is mounted on the host system or not doesn't
306 # matter. If dev was explicitly mentioned in the packages list, force
307 # its installation.
308 if [ "$releasever" = "2" ] ; then
309     for package in "${packages[@]}" ; do
310         if [ "$package" = "dev" ] && ! rpm --root $vroot -q dev >/dev/null 2>&1 ; then
311             rpm --root $vroot -Uvh --noscripts $baseurl/Fedora/RPMS/dev-3.3.13-1.i386.rpm
312             break
313         fi
314     done
315 fi
316
317 # Clean yum cache
318 echo "* Cleaning up" >&3
319 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
320     clean all
321
322 # Clean RPM state
323 rm -f $vroot/var/lib/rpm/__db*
324
325 # Disable all services in reference image
326 chroot $vroot /bin/sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
327
328 # Clean up
329 cleanup
330
331 exit 0