- make sure coreutils is installed; it's basically mandatory for any
[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.8 2006/04/05 21:20:27 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 fetch ()
107 {
108     curl --fail --silent --max-time 60 "$1"
109 }
110
111 for mirror in "${mirrors[@]}" ; do
112     baseurl=$mirror/linux/core/$releasever/$basearch/os
113     if fetch $baseurl/repodata/repomd.xml >/dev/null ; then
114         break
115     fi
116     unset baseurl
117 done
118
119 if [ -z "$baseurl" ] ; then
120     echo "Error: linux/core/$releasever/$basearch/os/repodata/repomd.xml"
121     echo "       could not be found in any of the following locations:"
122     echo
123     for mirror in ${mirrors[@]} ; do
124         echo $mirror
125     done
126     echo
127     usage
128 fi
129
130 exec 3>&1
131 exec 4>&2
132 if [ $verbose -eq 0 ] ; then
133     exec 1>/dev/null
134     exec 2>/dev/null
135 fi
136
137 # Minimally initialize /dev in reference image. If installed, the dev
138 # or udev RPMs will fill in the rest.
139 mkdir -p $vroot/dev
140 mknod -m 666 $vroot/dev/null c 1 3
141 mknod -m 666 $vroot/dev/zero c 1 5
142 mknod -m 666 $vroot/dev/full c 1 7
143 mknod -m 644 $vroot/dev/random c 1 8
144 mknod -m 644 $vroot/dev/urandom c 1 9
145 mknod -m 666 $vroot/dev/tty c 5 0
146 mknod -m 666 $vroot/dev/ptmx c 5 2
147 # For bash command substitution
148 ln -nsf ../proc/self/fd $vroot/dev/fd
149 # For df and linuxconf
150 touch $vroot/dev/hdv1
151
152 # Do not tolerate errors
153 set -e
154
155 # Mount /dev/pts in reference image
156 mkdir -p $vroot/dev/pts
157 mount -t devpts none $vroot/dev/pts
158
159 # Mount /dev/shm in reference image
160 mkdir -p $vroot/dev/shm
161 mount -t tmpfs none $vroot/dev/shm
162
163 # Mount /proc in reference image
164 mkdir -p $vroot/proc
165 mount -t proc none $vroot/proc
166
167 cleanup ()
168 {
169     umount $vroot/proc
170     umount $vroot/dev/shm
171     umount $vroot/dev/pts
172 }
173
174 # Clean up before exiting if anything goes wrong
175 trap "cleanup" ERR
176
177 # Create a dummy /etc/fstab in reference image
178 mkdir -p $vroot/etc
179 cat >$vroot/etc/fstab <<EOF
180 # This fake fstab exists only to please df and linuxconf.
181 /dev/hdv1       /       ext2    defaults        1 1
182 EOF
183 cp $vroot/etc/fstab $vroot/etc/mtab
184
185 # Prevent all locales from being installed in reference image
186 mkdir -p $vroot/etc/rpm
187 cat >$vroot/etc/rpm/macros <<EOF
188 %_install_langs en_US:en
189 %_excludedocs 1
190 %__file_context_path /dev/null
191 EOF
192
193 # Necessary for some scripts
194 mkdir -p $vroot/etc/sysconfig
195 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
196
197 # Trick rpm and yum, who read the real root /etc/rpm/macros file
198 # rather than the one installed in the reference image, despite what
199 # you might expect the --root and --installroot options to mean. Both
200 # programs always read $HOME/.rpmmacros.
201 export HOME=$vroot/tmp
202 mkdir -p $vroot/tmp
203 cp $vroot/etc/rpm/macros $vroot/tmp/.rpmmacros
204
205 # Initialize RPM database in reference image
206 mkdir -p $vroot/var/lib/rpm
207 rpm --root $vroot --initdb
208 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
209
210 # Initialize yum in reference image
211 mkdir -p $vroot/var/cache/yum $vroot/var/log
212 cat >$vroot/etc/yum.conf <<EOF
213 [main]
214 cachedir=/var/cache/yum
215 debuglevel=2
216 logfile=/var/log/yum.log
217 pkgpolicy=newest
218 distroverpkg=redhat-release
219 tolerant=1
220 exactarch=1
221 retries=20
222 obsoletes=1
223 gpgcheck=0
224 # Prevent yum-2.4 from loading additional repository definitions
225 # (e.g., from /etc/yum.repos.d/)
226 reposdir=/dev/null
227
228 [base]
229 name=Fedora Core $releasever - $basearch - base
230 baseurl=$baseurl/
231 EOF
232
233 for optional in core/updates extras ; do
234     if fetch $mirror/linux/$optional/$releasever/$basearch/repodata/repomd.xml ; then
235         cat >>$vroot/etc/yum.conf <<EOF
236
237 [$(basename $optional)]
238 name=Fedora Core $releasever - $basearch - $(basename $optional)
239 baseurl=$mirror/linux/$optional/$releasever/$basearch/
240 EOF
241     fi
242 done
243
244 # If we are being built as part of an automated RPM build, solve the
245 # bootstrap problem by including any just built packages in the yum
246 # configuration. This cooperates with the PlanetLab build system.
247 if [ -n "$RPM_BUILD_DIR" ] ; then
248     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
249     # yum-2.0.x
250     if [ -x /usr/bin/yum-arch ] ; then
251         yum-arch $RPM_RPMS_DIR
252     fi
253     # yum-2.4.x
254     if [ -x /usr/bin/createrepo ] ; then
255         if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
256             groupfile="-g yumgroups.xml"
257         fi
258         createrepo $groupfile $RPM_RPMS_DIR
259     fi
260     # If run under sudo, allow user to delete the headers/ and
261     # repodata/ directories.
262     if [ -n "$SUDO_USER" ] ; then
263         chown -R $SUDO_USER $RPM_RPMS_DIR
264     fi
265     cat >>$vroot/etc/yum.conf <<EOF
266
267 [bootstrap]
268 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
269 baseurl=file://$RPM_RPMS_DIR/
270 EOF
271 fi
272
273 # glibc must be specified explicitly for the correct arch to be
274 # chosen. coreutils are basically mandatory for any useful
275 # installation.
276 echo "* Installing glibc and coreutils" >&3
277 yum -c $vroot/etc/yum.conf --installroot=$vroot -y install glibc coreutils
278
279 # Go, baby, go
280 if [ ${#groups[*]} -gt 0 ] ; then
281     echo "* Installing optional groups" >&3
282     yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
283         groupinstall "${groups[@]}"
284 fi
285
286 if [ ${#packages[*]} -gt 0 ] ; then
287     echo "* Installing optional packages" >&3
288     yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
289         install "${packages[@]}"
290 fi
291
292 # FC2 dev %preinstall checks /proc/mounts to make sure that /dev is
293 # not currently mounted as devfs. If it thinks it is, it will refuse
294 # to install the package. On a modern system running udev that mounts
295 # /dev as tmpfs, this check fails. Since we are installing into a
296 # chroot, whether /dev is mounted on the host system or not doesn't
297 # matter. If dev was explicitly mentioned in the packages list, force
298 # its installation.
299 if [ "$releasever" = "2" ] ; then
300     for package in "${packages[@]}" ; do
301         if [ "$package" = "dev" ] && ! rpm --root $vroot -q dev >/dev/null 2>&1 ; then
302             rpm --root $vroot -Uvh --noscripts $baseurl/Fedora/RPMS/dev-3.3.13-1.i386.rpm
303             break
304         fi
305     done
306 fi
307
308 # Clean yum cache
309 echo "* Cleaning up" >&3
310 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
311     clean all
312
313 # Clean RPM state
314 rm -f $vroot/var/lib/rpm/__db*
315
316 # Set time zone to UTC
317 if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
318     rm -f $vroot/etc/localtime
319     ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
320 fi
321
322 # Clean up
323 cleanup
324
325 exit 0