- just get rid of the whole Core/Base thing; none of the current users
[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.2 2006/03/29 17:10:21 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 # Initialize /dev in reference image
151 mkdir -p $vroot/dev
152 mknod -m 666 $vroot/dev/null c 1 3
153 mknod -m 666 $vroot/dev/zero c 1 5
154 mknod -m 666 $vroot/dev/full c 1 7
155 mknod -m 644 $vroot/dev/random c 1 8
156 mknod -m 644 $vroot/dev/urandom c 1 9
157 mknod -m 666 $vroot/dev/tty c 5 0
158 mknod -m 666 $vroot/dev/ptmx c 5 2
159 # For bash command substitution
160 ln -nsf ../proc/self/fd $vroot/dev/fd
161 # For df and linuxconf
162 touch $vroot/dev/hdv1
163
164 # Mount /dev/pts in reference image
165 mkdir -p $vroot/dev/pts
166 mount -t devpts none $vroot/dev/pts
167
168 # Mount /dev/shm in reference image
169 mkdir -p $vroot/dev/shm
170 mount -t tmpfs none $vroot/dev/shm
171
172 # Mount /proc in reference image
173 mkdir -p $vroot/proc
174 mount -t proc none $vroot/proc
175
176 cleanup ()
177 {
178     umount $vroot/proc
179     umount $vroot/dev/shm
180     umount $vroot/dev/pts
181     rm -rf $tmp
182 }
183
184 # Clean up before exiting if anything goes wrong
185 trap "cleanup; exit 1" ERR
186
187 # Create a dummy /etc/fstab in reference image
188 mkdir -p $vroot/etc
189 cat >$vroot/etc/fstab <<EOF
190 # This fake fstab exists only to please df and linuxconf.
191 /dev/hdv1       /       ext2    defaults        1 1
192 EOF
193 cp $vroot/etc/fstab $vroot/etc/mtab
194
195 # Prevent all locales from being installed in reference image
196 mkdir -p $vroot/etc/rpm
197 cat >$vroot/etc/rpm/macros <<EOF
198 %_install_langs en_US:en
199 %_excludedocs 1
200 %__file_context_path /dev/null
201 EOF
202
203 # Necessary for some scripts
204 mkdir -p $vroot/etc/sysconfig
205 echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
206
207 # Trick rpm and yum, who read the real root /etc/rpm/macros file
208 # rather than the one installed in the reference image, despite what
209 # you might expect the --root and --installroot options to mean. Both
210 # programs always read $HOME/.rpmmacros.
211 export HOME=$vroot/tmp
212 mkdir -p $vroot/tmp
213 cp $vroot/etc/rpm/macros $vroot/tmp/.rpmmacros
214
215 # Initialize RPM database in reference image
216 mkdir -p $vroot/var/lib/rpm
217 rpm --root $vroot --initdb
218 rpm --root $vroot --import $baseurl/RPM-GPG-KEY-fedora
219
220 # Initialize yum in reference image
221 mkdir -p $vroot/var/cache/yum $vroot/var/log
222 cat >$vroot/etc/yum.conf <<EOF
223 [main]
224 cachedir=/var/cache/yum
225 debuglevel=2
226 logfile=/var/log/yum.log
227 pkgpolicy=newest
228 distroverpkg=redhat-release
229 tolerant=1
230 exactarch=1
231 retries=20
232 obsoletes=1
233 gpgcheck=0
234 # Prevent yum-2.4 from loading additional repository definitions
235 # (e.g., from /etc/yum.repos.d/)
236 reposdir=/dev/null
237
238 [base]
239 name=Fedora Core $releasever - $basearch - base
240 baseurl=$baseurl/
241 EOF
242
243 for optional in core/updates extras ; do
244     if fetch $mirror/linux/$optional/$releasever/$basearch/repodata/repomd.xml ; then
245         cat >>$vroot/etc/yum.conf <<EOF
246
247 [$(basename $optional)]
248 name=Fedora Core $releasever - $basearch - $(basename $optional)
249 baseurl=$mirror/linux/$optional/$releasever/$basearch/
250 EOF
251     fi
252 done
253
254 # If we are being built as part of an automated RPM build, solve the
255 # bootstrap problem by including any just built packages in the yum
256 # configuration. This cooperates with the PlanetLab build system.
257 if [ -n "$RPM_BUILD_DIR" ] ; then
258     RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
259     # yum-2.0.x
260     if [ -x /usr/bin/yum-arch ] ; then
261         yum-arch $RPM_RPMS_DIR
262     fi
263     # yum-2.4.x
264     if [ -x /usr/bin/createrepo ] ; then
265         if [ -f $RPM_RPMS_DIR/yumgroups.xml ] ; then
266             groupfile="-g yumgroups.xml"
267         fi
268         createrepo $groupfile $RPM_RPMS_DIR
269     fi
270     # If run under sudo, allow user to delete the headers/ and
271     # repodata/ directories.
272     if [ -n "$SUDO_USER" ] ; then
273         chown -R $SUDO_USER $RPM_RPMS_DIR
274     fi
275     cat >>$vroot/etc/yum.conf <<EOF
276
277 [bootstrap]
278 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
279 baseurl=file://$RPM_RPMS_DIR/
280 EOF
281 fi
282
283 echo -n "* Installing base system..." >&3
284
285 # glibc must be specified explicitly for the correct arch to be chosen.
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 -n "* Installing optional groups..." >&3
291     yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
292         groupinstall "${groups[@]}"
293     echo "done" >&3
294 fi
295
296 if [ ${#packages[*]} -gt 0 ] ; then
297     echo -n "* Installing optional packages..." >&3
298     yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
299         install "${packages[@]}"
300     echo "done" >&3
301 fi
302
303 # Clean yum cache
304 echo -n "* Cleaning up..." >&3
305 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
306     clean all
307 echo "done" >&3
308
309 # Clean RPM state
310 rm -f $vroot/var/lib/rpm/__db*
311
312 # Disable all services in reference image
313 chroot $vroot /bin/sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
314
315 # Clean up
316 cleanup
317
318 exit 0