3 # rpmbased-dist-chroot.sh - builds a simple chroot from a directory of .rpms
4 # best to feed it the RPMS directory of a particular
5 # distribution. Has been tested with FC4.
6 # Copyright (C) 2004 Brett Parker <iDunno@sommitrealweird.co.uk>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # Default to expecting a Fedora CD mounted at /media/cdrom
23 # and installing bash, coreutils and yum to /mnt/temp/fedora
25 RPMArchives=/media/cdrom/Fedora/RPMS
26 InstallRoot=/mnt/temp/fedora
27 installpackages="bash coreutils yum basesystem redhat-release"
29 ourcachedirectory=$(mktemp -d)
31 architecture=$(uname -m)
32 export ARCH=$architecture
36 architecture="x86_64,i686,i586,i486,i386"
39 architecture="i686,i586,i486,i386"
42 architecture="i586,i486,i386"
45 architecture="i486,i386"
49 function displayHelp() {
54 --install-root <directorytoinstallto>
55 --rpm-archives <pathtorpms>
56 --install-packages <listofpackagestoinstall>
61 function startswithmm() {
62 if [ "${1##--*}" == "" ]; then
69 function containsequals() {
70 if [ "${1//*=*}" == "" ]; then
79 if ( ! startswithmm $option ); then
80 if [ "x$argcommand" == "xinstall-root" ]; then
82 elif [ "x$argcommand" == "xrpm-archives" ]; then
84 elif [ "x$argcommand" == "xinstall-packages" ]; then
85 if [ "x$installpackages" == "x" ]; then
86 installpackages=$option
88 installpackages="$installpackages $option"
92 if ( containsequals $option ); then
93 argcommand=${option%%=*}
94 argcommand=${argcommand##--}
97 if [ "x$argcommand" == "xinstall-root" ]; then
99 elif [ "x$argcommand" == "xrpm-archives" ]; then
101 elif [ "x$argcommand" == "xinstall-packages" ]; then
102 installpackages=$option
104 echo "Unknown option $option"
108 if [ "x$option" == "x--help" ]; then
110 elif [ "x$option" == "x--install-root" ]; then
111 argcommand=${option##--}
112 elif [ "x$option" == "x--rpm-archives" ]; then
113 argcommand=${option##--}
114 elif [ "x$option" == "x--install-packages" ]; then
115 argcommand=${option##--}
118 echo "Unknown option $option"
125 echo "Install Packages: $installpackages"
126 echo "RPMArchives : $RPMArchives"
127 echo "InstallRoot : $InstallRoot"
129 function buildcache() {
132 for arch in noarch $(echo $architecture | sed -e 's/,/\n/g'); do
133 for file in *.$arch.rpm; do
134 rpm -qpl $file | sed 's#^#'$file':#' >> $ourcachedirectory/files-cache
135 rpm -qp --provides $file | cut -d " " -f 1 | sed 's#^#'$file':#' >> $ourcachedirectory/package-cache
142 if [ -z $name ]; then
147 escapename=$(echo $name | sed -e 's#\+#\\+#g; s#\.#\\.#g; s#(#\\(#g; s#)#\\)#g;')
149 if [ ${name:0:1} == "/" ]; then
150 grep -E "^.*:$escapename\$" $ourcachedirectory/files-cache | cut -d ":" -f 1 | head -n 1
152 grep -E "^.*:$escapename\$" $ourcachedirectory/package-cache | cut -d ":" -f 1 | head -n 1
156 function getdependencies() {
160 rpm -qp --requires ${RPMArchives}/$filename | grep -v '^.*('$name')' | cut -d " " -f 1 | sort | uniq | grep -Ev '^[ ]*$' | while read line; do
161 if [ ! -z $line ]; then
162 fname=$(findrpm $line)
163 packagename=$(getpackagename $fname)
164 if [ ! -z $name ] && [ ! -z $packagename ]; then
165 if [[ $firstpackage == 0 ]]; then
166 if ( ! grep '^'$fname' ' $ourcachedirectory/build-packagelist > /dev/null ) && ( ! rpm -r ${InstallRoot} -qv $packagename > /dev/null ); then
167 echo $fname $packagename >> $ourcachedirectory/build-packagelist
168 getdependencies $fname $packagename
171 if ( ! grep '^'$fname' ' $ourcachedirectory/build-packagelist > /dev/null ); then
172 echo $fname $packagename >> $ourcachedirectory/build-packagelist
173 getdependencies $fname $packagename
181 function getpackagename() {
183 if [ ! -z $filename ]; then
184 rpm -qp --queryformat "%{NAME}\n" ${RPMArchives}/$filename
194 cp /etc/resolv.conf etc/
195 mknod -m 666 dev/null c 1 3
200 rpm -r ${InstallRoot} --initdb
204 for package in $installpackages; do
206 rm -f $ourcachedirectory/build-packagelist
207 touch $ourcachedirectory/build-packagelist
209 echo "Installing packages required for $package"
211 if [[ "x$(findrpm $package)" != "x" ]]; then
213 getdependencies $(findrpm $package) $package
214 echo " RPM File :" $(findrpm $package)
215 echo " Dependencies:"
216 cat $ourcachedirectory/build-packagelist | cut -d ' ' -f 2 | sed -e 's#^# #;'
218 echo rpm -r ${InstallRoot} --ignorearch --install $(cat $ourcachedirectory/build-packagelist | cut -d ' ' -f 1) >> $ourcachedirectory/install-log
222 cd ${RPMArchives} && rpm -r ${InstallRoot} --ignorearch --install $(cat $ourcachedirectory/build-packagelist | cut -d ' ' -f 1)
223 if ( ! grep " $package\$" $ourcachedirectory/build-packagelist > /dev/null 2>/dev/null ); then
224 packages="$(findrpm $package)"
225 echo rpm -r ${InstallRoot} --ignorearch --install "${packages}" >> $ourcachedirectory/install-log
226 echo "${packages}" >> $ourcachedirectory/packages-installed
227 cd ${RPMArchives} && rpm -r ${InstallRoot} --ignorearch --install ${packages}
230 echo " Failed to find the RPM providing $package - skipping"
234 read -p "Remove cache directory ($ourcachedirectory)? [Yn]: " cleanup
236 cleanup=${cleanup:0:1}
238 if [ "$cleanup" == "N" ] || [ "$cleanup" == "n" ] ; then
239 echo leaving logs in $ourcachedirectory
241 echo removing logs directory
242 rm -r $ourcachedirectory