More minimization (now 15.9 MB)
[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: \
23         --long groupinstall:,group-install:,help,install:,noclean,no-clean \
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         --groupinstall|--group-install)
49             packages[i++]="@$2"
50             shift 2;;
51         --noclean|--no-clean)
52             clean=no
53             shift;;
54         --help)
55             usage
56             exit 0;;
57         --)
58             shift
59             break;;
60         *)
61             echo "Internal error!"
62             exit 1;;
63     esac
64 done
65
66 if [ $# -lt 2 -o $# -gt 3 ]; then
67     usage
68     exit 1
69 fi
70
71 repo="$1"
72 target="$2"
73 mirror="$3"
74
75 # Architecture is currently always the same as the current arch.  We
76 # cannot do --foreign builds.  See discussion in the manpage.
77 arch=$(arch)
78
79 # Create a temporary directory, make sure it gets cleaned up at the end.
80 tmpdir=$(mktemp -d)
81 remove_tmpdir ()
82 {
83   status=$?
84   rm -rf "$tmpdir" && exit $status
85 }
86 trap remove_tmpdir EXIT
87
88 # Create the temporary repository configuration.  The name of the
89 # repository is always 'febootstrap'.
90 cat > $tmpdir/febootstrap.repo <<__EOF__
91 [febootstrap]
92 name=febootstrap $repo $arch
93 failovermethod=priority
94 enabled=1
95 gpgcheck=0
96 __EOF__
97
98 # "Mirror" parameter is a bit misnamed, but it means a local mirror,
99 # instead of the public Fedora mirrors.
100 if [ -n "$mirror" ]; then
101     echo "baseurl=$mirror" >> "$tmpdir"/febootstrap.repo
102 else
103     echo "mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=$repo&arch=$arch" >> "$tmpdir"/febootstrap.repo
104 fi
105
106 # Create the target filesystem.
107 rm -rf "$target"
108 mkdir "$target"
109
110 # Target must be an absolute path.
111 target=$(cd "$target"; pwd)
112
113 # This is necessary to keep yum happy.  It's not clear why yum can't
114 # just create this file itself.
115 mkdir -p "$target"/var/cache/yum/febootstrap/packages
116
117 # Make the device nodes inside the fake chroot.
118 # (Copied from mock/backend.py)  Why isn't there a base package which
119 # creates these?
120 make_device_nodes ()
121 {
122     mkdir "$target"/dev
123     (
124         cd "$target"/dev
125         mkdir pts
126         mkdir shm
127         mknod null c 1 3;    chmod 0666 null
128         mknod full c 1 7;    chmod 0666 full
129         mknod zero c 1 5;    chmod 0666 zero
130         mknod random c 1 8;  chmod 0666 random
131         mknod urandom c 1 9; chmod 0444 urandom
132         mknod tty c 5 0;     chmod 0666 tty
133         mknod console c 5 1; chmod 0600 console
134         mknod ptmx c 5 2;    chmod 0666 ptmx
135         ln -sf /proc/self/fd/0 stdin
136         ln -sf /proc/self/fd/1 stdout
137         ln -sf /proc/self/fd/2 stderr
138     )
139 }
140 export -f make_device_nodes
141 export target
142
143 if [ $(id -u) -ne 0 ]; then
144     fakeroot -s "$target"/fakeroot.log \
145     make_device_nodes
146 else
147     make_device_nodes
148 fi
149
150 # Run yum.
151 run_yum ()
152 {
153     yum \
154         -y -c "$tmpdir"/febootstrap.repo \
155         --disablerepo=* --enablerepo=febootstrap \
156         --noplugins --nogpgcheck \
157         --installroot="$target" \
158         install "$@"
159 }
160 export -f run_yum
161 export tmpdir
162
163 if [ $(id -u) -ne 0 ]; then
164     # Bash doesn't support exporting array variables, hence this
165     # tortuous workaround.
166     fakeroot -i "$target"/fakeroot.log -s "$target"/fakeroot.log \
167     fakechroot -s \
168     bash -c 'run_yum "$@"' run_yum "${packages[@]}"
169 else
170     run_yum "${packages[@]}"
171 fi
172
173 # Clean up the yum repository.
174 if [ "$clean" = "yes" ]; then
175     rm -rf "$target"/var/cache/yum/febootstrap
176 fi