c7eeca4e2eb337b54ce80eaee10fa9b37cbcf9b3
[febootstrap.git] / febootstrap.sh
1 #!/bin/bash -
2 # febootstrap
3 # (C) Copyright 2009 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # Written by Richard W.M. Jones <rjones@redhat.com>
20
21 TEMP=`getopt \
22         -o g:i:p: \
23         --long groupinstall:,group-install:,help,install:,noclean,no-clean,proxy:  \
24         -n febootstrap -- "$@"`
25 if [ $? != 0 ]; then
26     echo "febootstrap: problem parsing the command line arguments"
27     exit 1
28 fi
29 eval set -- "$TEMP"
30
31 declare -a packages
32 packages[0]="@Core"
33 i=0
34
35 clean=yes
36
37 usage ()
38 {
39     echo "Usage: febootstrap [--options] REPO TARGET [MIRROR]"
40     echo "Please read febootstrap(8) man page for more information."
41 }
42
43 while true; do
44     case "$1" in
45         -i|--install)
46             packages[i++]="$2"
47             shift 2;;
48         -p|--proxy)
49             proxy="proxy=$2"
50             shift 2;;
51         --groupinstall|--group-install)
52             packages[i++]="@$2"
53             shift 2;;
54         --noclean|--no-clean)
55             clean=no
56             shift;;
57         --help)
58             usage
59             exit 0;;
60         --)
61             shift
62             break;;
63         *)
64             echo "Internal error!"
65             exit 1;;
66     esac
67 done
68
69 if [ $# -lt 2 -o $# -gt 3 ]; then
70     usage
71     exit 1
72 fi
73
74 repo="$1"
75 target="$2"
76 mirror="$3"
77
78 # Architecture is currently always the same as the current arch.  We
79 # cannot do --foreign builds.  See discussion in the manpage.
80 arch=$(arch)
81 case $arch in
82     i?86) arch=i386 ;;
83 esac
84
85 # Create a temporary directory, make sure it gets cleaned up at the end.
86 tmpdir=$(mktemp -d)
87 remove_tmpdir ()
88 {
89   status=$?
90   rm -rf "$tmpdir" && exit $status
91 }
92 trap remove_tmpdir EXIT
93
94 # Create the temporary repository configuration.  The name of the
95 # repository is always 'febootstrap'.
96 cat > $tmpdir/febootstrap.repo <<__EOF__
97 [febootstrap]
98 name=febootstrap $repo $arch
99 failovermethod=priority
100 enabled=1
101 gpgcheck=0
102 $proxy
103 __EOF__
104
105 # "Mirror" parameter is a bit misnamed, but it means a local mirror,
106 # instead of the public Fedora mirrors.
107 if [ -n "$mirror" ]; then
108     echo "baseurl=$mirror" >> "$tmpdir"/febootstrap.repo
109 else
110     echo "mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=$repo&arch=$arch" >> "$tmpdir"/febootstrap.repo
111 fi
112
113 # Create the target filesystem.
114 rm -rf "$target"
115 mkdir "$target"
116
117 # Target must be an absolute path.
118 target=$(cd "$target"; pwd)
119
120 # This is necessary to keep yum happy.  It's not clear why yum can't
121 # just create this file itself.
122 mkdir -p "$target"/var/cache/yum/febootstrap/packages
123
124 # NB: REQUIRED for useradd/groupadd to run properly.
125 #
126 # However this causes 'filesystem' RPM install to give the
127 # following error.  Not sure how serious the error is:
128 # error: unpacking of archive failed on file /proc: cpio: utime
129 export FAKECHROOT_EXCLUDE_PATH=/proc
130
131 # Substitute some statically-linked commands.  This is only supported
132 # in fakechroot > 2.9.  For previous versions of fakechroot it is
133 # ignored.
134 export FAKECHROOT_CMD_SUBST=/sbin/ldconfig=/bin/true:/usr/sbin/glibc_post_upgrade.i686=/bin/true:/usr/sbin/glibc_post_upgrade.x86_64=/bin/true:/usr/sbin/build-locale-archive=/bin/true:/usr/sbin/libgcc_post_upgrade=/bin/true
135
136 # Make the device nodes inside the fake chroot.
137 # (Copied from mock/backend.py)  Why isn't there a base package which
138 # creates these?
139 make_device_nodes ()
140 {
141     mkdir "$target"/proc
142     mkdir "$target"/sys
143     mkdir "$target"/dev
144     (
145         cd "$target"/dev
146         mkdir pts
147         mkdir shm
148         mkdir mapper
149         mknod null c 1 3;    chmod 0666 null
150         mknod full c 1 7;    chmod 0666 full
151         mknod zero c 1 5;    chmod 0666 zero
152         mknod random c 1 8;  chmod 0666 random
153         mknod urandom c 1 9; chmod 0444 urandom
154         mknod tty c 5 0;     chmod 0666 tty
155         mknod console c 5 1; chmod 0600 console
156         mknod ptmx c 5 2;    chmod 0666 ptmx
157         ln -sf /proc/self/fd/0 stdin
158         ln -sf /proc/self/fd/1 stdout
159         ln -sf /proc/self/fd/2 stderr
160     )
161 }
162 export -f make_device_nodes
163 export target
164
165 if [ $(id -u) -ne 0 ]; then
166     fakeroot -s "$target"/fakeroot.log \
167     make_device_nodes
168 else
169     make_device_nodes
170 fi
171
172 # Run yum.
173 run_yum ()
174 {
175     yum \
176         -y -c "$tmpdir"/febootstrap.repo \
177         --disablerepo=* --enablerepo=febootstrap \
178         --noplugins --nogpgcheck \
179         --installroot="$target" \
180         install "$@"
181 }
182 export -f run_yum
183 export tmpdir
184
185 if [ $(id -u) -ne 0 ]; then
186     # Bash doesn't support exporting array variables, hence this
187     # tortuous workaround.
188     fakeroot -i "$target"/fakeroot.log -s "$target"/fakeroot.log \
189     fakechroot -s \
190     bash -c 'run_yum "$@"' run_yum "${packages[@]}"
191 else
192     run_yum "${packages[@]}"
193 fi
194
195 # Clean up the yum repository.
196 if [ "$clean" = "yes" ]; then
197     rm -rf "$target"/var/cache/yum/febootstrap
198 fi