source: ProjectBuilder/devel/rpmbootstrap/bin/rpmbootstrap@ 991

Last change on this file since 991 was 991, checked in by Bruno Cornec, 14 years ago

r3711@localhost: bruno | 2010-03-01 07:29:29 +0100

  • veconf => rbsconf
  • Adds option -a to rpmbootstrap in order for pb to automatically install the packages it needs
  • Check CentOS4 VE OK
  • Adds pb.conf man page to spec build
File size: 14.1 KB
Line 
1#!/usr/bin/perl -w
2#
3# rpmbootstrap application, a debootstrap like for RPM distros
4#
5# $Id$
6#
7# Copyright B. Cornec 2010
8# Provided under the GPL v2
9
10# Syntax: see at end
11
12use strict 'vars';
13use Getopt::Long qw(:config auto_abbrev no_ignore_case);
14use Data::Dumper;
15use English;
16use LWP::UserAgent;
17use File::Basename;
18use File::Copy;
19use File::Find;
20use ProjectBuilder::Version;
21use ProjectBuilder::Base;
22use ProjectBuilder::Env;
23use ProjectBuilder::Conf;
24use ProjectBuilder::Distribution;
25
26# Global variables
27my %opts; # CLI Options
28
29=pod
30
31=head1 NAME
32
33rpmbootstrap - creates a chrooted RPM based distribution a la debootstrap, aka Virtual Environment (VE)
34
35=head1 DESCRIPTION
36
37rpmbootstrap creates a chroot environment (Virtual Environment or VE) with a minimal distribution in it,
38suited for building packages for example. It's very much like debootstrap but for RPM based distribution.
39It aims at supporting all distributions supported by project-builder;org (RHEL, RH, Fedora, OpeSUSE, SLES, Mandriva, ...)
40
41It is inspired by work done by Steve Kemp for rinse (http://www.steve.org.uk/), and similar to mock, but fully integrated with project-builder.org (which also supports rinse and mock).
42
43=head1 SYNOPSIS
44
45rpmbootstrap [-vhmqpdk][-s script][-i iso][-a pkg1[,pkg2,...]] distribution-version-arch [target-dir] [mirror [script]]
46
47pb [--verbose][--help][--man][--quiet][--print-rpms][--download-only][--keep][--add pkg1,[pkg2,...]][--script script][--iso iso] distribution-version-arch [target-dir] [mirror [script]]
48
49=head1 OPTIONS
50
51=over 4
52
53=item B<-v|--verbose>
54
55Print a brief help message and exits.
56
57=item B<-h|--help>
58
59Print a brief help message and exits.
60
61=item B<--man>
62
63Prints the manual page and exits.
64
65=item B<-q|--quiet>
66
67Do not print any output.
68
69=item B<-p|--print-rpms>
70
71Print the packages to be installed, and exit. Note that a target directory must be specified so
72rpmbootstrap can determine which packages should be installed, and to resolve dependencies. The target directory will be deleted.
73
74=item B<-d|--download-only>
75
76Download packages, but don't perform installation.
77
78=item B<-k|--keep>
79
80Keep packages in the cache dir for later reuse. By default remove them.
81
82=item B<-s|--script script>
83
84Name of the script you want to execute on the related VEs after the installation.
85It is executed in host environment. You can use the chroot command to execute actions in the VE.
86
87=item B<-i|--iso iso_image>
88
89Name of the ISO image of the distribution you want to install on the related VE.
90
91=item B<-a|--add pkg1[,pkg2,...]>
92
93Additional packages to add from the distribution you want to install on the related VE at the end of the chroot build.
94
95=back
96
97=head1 ARGUMENTS
98
99=item B<distribution-version-arch>
100
101Full name of the distribution that needs to be installed in the VE. E.g. fedora-11-x86_64.
102
103=item B<target-dir>
104
105This is the target directory under which the VE will be created. Created on the fly if needed. If none is given use the default directory hosting VE for project-builder.org (Cf: vepath parameter in $HOME/.pbrc)
106
107=head1 EXAMPLE
108
109To setup a Fedora 12 distribution with an i386 architecture issue:
110
111rpmbootstrap fedora-12-i386 /tmp/fedora/12/i386
112
113
114=head1 WEB SITES
115
116The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
117
118=head1 USER MAILING LIST
119
120Cf: L<http://www.mondorescue.org/sympa/info/pb-announce> for announces and L<http://www.mondorescue.org/sympa/info/pb-devel> for the development of the pb project.
121
122=head1 CONFIGURATION FILE
123
124Uses Project-Builder.org configuration file (/etc/pb/pb.conf or /usr/local/etc/pb/pb.conf)
125
126=head1 AUTHORS
127
128The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
129
130=head1 COPYRIGHT
131
132Project-Builder.org is distributed under the GPL v2.0 license
133described in the file C<COPYING> included with the distribution.
134
135=cut
136
137# ---------------------------------------------------------------------------
138
139my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
140my $appname = "rpmbootstrap";
141$ENV{'PBPROJ'} = $appname;
142
143# Initialize the syntax string
144
145pb_syntax_init("$appname Version $projectbuilderver-$projectbuilderrev\n");
146pb_temp_init();
147
148GetOptions("help|?|h" => \$opts{'h'},
149 "man|m" => \$opts{'man'},
150 "verbose|v+" => \$opts{'v'},
151 "quiet|q" => \$opts{'q'},
152 "log-files|l=s" => \$opts{'l'},
153 "script|s=s" => \$opts{'s'},
154 "print-rpms|p" => \$opts{'p'},
155 "download-only|d" => \$opts{'d'},
156 "keep|k" => \$opts{'k'},
157 "iso|i=s" => \$opts{'i'},
158 "add|a=s" => \$opts{'a'},
159 "version|V=s" => \$opts{'V'},
160) || pb_syntax(-1,0);
161
162if (defined $opts{'h'}) {
163 pb_syntax(0,1);
164}
165if (defined $opts{'man'}) {
166 pb_syntax(0,2);
167}
168if (defined $opts{'v'}) {
169 $pbdebug = $opts{'v'};
170}
171if (defined $opts{'q'}) {
172 $pbdebug=-1;
173}
174if (defined $opts{'l'}) {
175 open(pbLOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
176 $pbLOG = \*pbLOG;
177 $pbdebug = 0 if ($pbdebug == -1);
178 }
179pb_log_init($pbdebug, $pbLOG);
180#pb_display_init("text","");
181
182#if (defined $opts{'s'}) {
183#$pbscript = $opts{'s'};
184#}
185#if (defined $opts{'i'}) {
186#$iso = $opts{'i'};
187#}
188
189# Get VE name
190$ENV{'PBV'} = shift @ARGV;
191die pb_syntax(-1,1) if (not defined $ENV{'PBV'});
192
193die "Needs to be run as root" if ($EFFECTIVE_USER_ID != 0);
194
195#
196# Initialize distribution info from pb conf file
197#
198pb_log(0,"Starting VE build for $ENV{'PBV'}\n");
199my ($name,$ver,$darch) = split(/-/,$ENV{'PBV'});
200chomp($darch);
201my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd) = pb_distro_init($name,$ver,$darch);
202
203#
204# Check target dir
205# Create if not existent and use default if none given
206#
207pb_env_init_pbrc(); # to get content of HOME/.pbrc
208my $vepath = shift @ARGV;
209
210#
211# Check for command requirements
212#
213my ($req,$opt) = pb_conf_get_if("oscmd","oscmdopt");
214my ($req2,$opt2) = (undef,undef);
215$req2 = $req->{$appname} if (defined $req);
216$opt2 = $opt->{$appname} if (defined $opt);
217pb_check_requirements($req2,$opt2);
218
219if (not defined $vepath) {
220 my ($vestdpath) = pb_conf_get_if("vepath");
221 $vepath = "$vestdpath->{'default'}/$ddir/$dver/$darch";
222}
223
224die pb_log(0,"No target-dir specified and no default vepath found in $ENV{'PBETC'}\n") if (not defined $vepath);
225
226pb_mkdir_p($vepath) if (! -d $vepath);
227
228#
229# Get the package list to download, store them in a cache directory
230#
231my ($rbsmindep,$rbsmirrorsrv) = pb_conf_get("rbsmindep","rbsmirrorsrv");
232my ($rbscachedir) = pb_conf_get_if("rbscachedir");
233my $pkgs = pb_distro_get_param($ddir,$dver,$darch,$rbsmindep);
234my $mirror = pb_distro_get_param($ddir,$dver,$darch,$rbsmirrorsrv);
235
236my $cachedir = "/var/cache/rpmbootstrap";
237$cachedir = $rbscachedir->{'default'} if (defined $rbscachedir->{'default'});
238
239# Point to the right subdir and create it if needed
240$cachedir .= "/$ddir-$dver-$darch";
241pb_mkdir_p($cachedir) if (! -d $cachedir);
242
243# Get the complete package name from the mirror
244#
245my $ua = LWP::UserAgent->new;
246$ua->timeout(10);
247$ua->env_proxy;
248
249pb_log(0,"Downloading package list from $mirror ...\n");
250my $response = $ua->get($mirror);
251if (! $response->is_success) {
252 die "Unable to download packages from $mirror for $ddir-$dver-$darch";
253}
254pb_log(3,"Mirror $mirror gave answer: ".Dumper($response->dump(maxlength => 0))."\n");
255
256# Manages architectures specificities
257my $parch = $darch;
258$parch = "i[3456]86" if ($darch eq "i386");
259
260# Get the list of packages and their URL in this hash
261my %url;
262foreach my $l (split(/\n/,$response->as_string())) {
263 # Find a href ref
264 if ($l =~ /<a href="(.*)">(.*)<\/a>/i) {
265 my $url = $1;
266 my $pkg = $1;
267 my $desc = $2;
268 pb_log(3,"Found desc URL $desc: ");
269 # find an rpm package ref name-ver-tag.arch.rpm
270 if ($pkg =~ /(.+)-([^-]+)-([^-]+)\.(noarch|$parch)\.rpm$/) {
271 pb_log(3,"package ($1 + $2 + $3 + $4)\n");
272 $url{$1} = "$mirror/$url";
273 } else {
274 pb_log(3,"not a package\n");
275 }
276 }
277}
278
279#
280# Prepare early the yum cache env for the VE in order to copy in it packages on the fly
281#
282my $oscachedir = "/tmp";
283my $osupdcachedir;
284my $osupdname = "";
285
286if ($pbupd =~ /yum/) {
287 $oscachedir = "$vepath/var/cache/yum/core/packages/";
288 $osupdcachedir = "$vepath/var/cache/yum/updates-released/packages/";
289 $osupdname = "YUM";
290 # Recent Fedora release use a new yum cache dir
291 if (($ddir eq "fedora") && ($dver > 8)) {
292 $oscachedir = "$vepath/var/cache/yum/$darch/$dver/fedora/packages";
293 $osupdcachedir = "$vepath/var/cache/yum/$darch/$dver/updates/packages";
294 $osupdcachedir = "$vepath/var/cache/yum/updates-released/packages/";
295 }
296} elsif ($pbupd =~ /zypper/) {
297 $oscachedir = "$vepath/var/cache/zypp/packages/opensuse/suse/$darch";
298 $osupdname = "Zypper";
299} elsif ($pbupd =~ /urpmi/) {
300 $oscachedir = "$vepath/var/cache/zypp/packages/opensuse/suse/$darch";
301 $osupdname = "Zypper";
302}
303pb_log(1,"Setting up $osupdname cache in VE\n");
304pb_mkdir_p($oscachedir);
305pb_mkdir_p($osupdcachedir) if (defined $osupdcachedir);
306
307# For each package to process, get it, put it in the cache dir
308# and extract it in the target dir. If not asked to keep, remove it
309# Just download if asked so.
310
311my $warning = 0;
312my $lwpkg ="";
313foreach my $p (split(/,/,$pkgs)) {
314 pb_log(1,"Processing package $p ...\n");
315 # Just print packages names if asked so.
316 if (defined $url{$p}) {
317 if ($opts{'p'}) {
318 pb_log(0,"$url{$p}\n");
319 next;
320 } else {
321 # Now download if not already in cache
322 my $p1 = basename($url{$p});
323 if (! -f "$cachedir/$p1") {
324 pb_system("wget --quiet -O $cachedir/$p1 $url{$p}","Downloading package $p1 ...");
325 } else {
326 pb_log(1,"Package $p1 already in cache\n");
327 }
328
329 # End if download only
330 if ($opts{'d'}) {
331 next;
332 }
333
334 #
335 # Copy the cached .RPM files into the oscachedir directory, so that os doesn't need to download them again.
336 #
337 pb_log(1,"Link package into $oscachedir\n");
338 copy("$cachedir/$p1",$oscachedir) if (defined $oscachedir);
339 symlink("$oscachedir/$p1","$osupdcachedir/p1") if (defined $osupdcachedir);
340
341 # And extract it to the finale dir
342 pb_system("cd $vepath ; rpm2cpio $cachedir/$p1 | cpio -ivdum","Extracting package $p1 into $vepath");
343
344 # Remove cached package if not asked to keep
345 if (! $opts{'k'}) {
346 unlink("$cachedir/$p1");
347 }
348
349 }
350 } else {
351 pb_log(0,"WARNING: unable to find URL for $p\n");
352 $warning++;
353 $lwpkg .= " $p";
354 }
355}
356
357if ($warning ge 1) {
358 pb_log(0,"$warning WARNINGS found.\nMaybe you should review your package list for $ddir-$dver-$darch\nand remove$lwpkg\n");
359}
360
361# Stop here if we just print
362if ($opts{'p'}) {
363 exit(0);
364}
365
366# Now executes the VE finalization steps required for it to work correctly
367pb_log(0,"VE post configuration\n");
368
369# yum needs that distro-release package be installed, so force it
370if ($pbupd =~ /yum/) {
371 foreach my $p1 (<$cachedir/($ddir|redhat)-release-*.rpm>) {
372 copy("$cachedir/$p1","$vepath/tmp");
373 pb_system("chroot $vepath rpm -ivh --force --nodeps /tmp/$p1","Forcing RPM installation of $p1");
374 unlink("$vepath/tmp/$p1");
375 }
376}
377#
378# Make sure there is a resolv.conf file present, such that DNS lookups succeed.
379#
380pb_log(1,"Creating resolv.conf\n");
381pb_mkdir_p("$vepath/etc");
382copy("/etc/resolv.conf","$vepath/etc/");
383
384#
385# BUGFIX:
386#
387if ((($ddir eq "centos") || ($ddir eq "rhel")) && ($dver eq "5")) {
388 pb_log(1,"BUGFIX for centos-5\n");
389 pb_mkdir_p("$vepath/usr/lib/python2.4/site-packages/urlgrabber.skx");
390 foreach my $i (<$vepath/usr/lib/python2.4/site-packages/urlgrabber/keepalive.*>) {
391 move($i,"$vepath/usr/lib/python2.4/site-packages/urlgrabber.skx/");
392 }
393}
394
395#
396# /proc needed
397#
398pb_mkdir_p("$vepath/proc");
399pb_system("mount -o bind /proc $vepath/proc","Mounting /proc");
400
401#
402# Some devices may be needed
403#
404pb_system("mknod -m 644 $vepath/dev/random c 1 8","Creating $vepath/dev/random") if (! -c "$vepath/dev/random");
405pb_system("mknod -m 644 $vepath/dev/urandom c 1 9","Creating $vepath/dev/urandom") if (! -c "$vepath/dev/urandom");
406pb_system("mknod -m 666 $vepath/dev/zero c 1 5","Creating $vepath/dev/zero") if (! -c "$vepath/dev/zero");
407
408my $minipkglist;
409
410pb_log(1,"Adapting $osupdname repository entries");
411if ($pbupd =~ /yum/) {
412 #
413 # Force the architecture for yum
414 # The goal is to allow i386 chroot on x86_64
415 #
416 # FIX: Not sufficient to have yum working
417 # mirrorlist is not usable
418 # $releasever also needs to be filtered
419 # yum.conf as well
420 foreach my $i (<$vepath/etc/yum.repos.d/*>,"$vepath/etc/yum.conf") {
421 pb_system("sed -i -e 's/\$basearch/$darch/g' $i","","quiet");
422 pb_system("sed -i -e 's/\$releasever/$dver/g' $i","","quiet");
423 pb_system("sed -i -e 's/^mirrorlist/#mirrorlist/' $i","","quiet");
424 pb_system("sed -i -e 's/^#baseurl/baseurl/' $i","","quiet");
425 }
426 $minipkglist = "ldconfig yum passwd vim-minimal dhclient authconfig";
427} elsif ($pbupd =~ /zypper/) {
428 pb_mkdir_p("$vepath/etc/zypp/repos.d");
429 open(REPO,"> $vepath/etc/zypp/repos.d/$ddir-$dver") || die "Unable to create repo file";
430 my $baseurl = dirname(dirname($mirror));
431 print REPO << 'EOF';
432[opensuse]
433name=$ddir-$dver
434baseurl=$baseurl
435enabled=1
436gpgcheck=1
437
438EOF
439 $minipkglist = "zypper vim-minimal dhclient";
440 # Bootstraping zypper
441 if ($dver eq "10.2") {
442 pb_system("chroot $vepath /bin/bash -c \"yes | /usr/bin/zypper sa $baseurl $ddir-$dver\"","Bootstrapping Zypper");
443 }
444}
445
446#
447# Run "install the necessary modules".
448# No need for sudo here
449#
450$pbupd =~ s/sudo//g;
451pb_system("chroot $vepath /bin/bash -c \"$pbupd $minipkglist \"","Bootstrapping OS by running $pbupd $minipkglist");
452
453#
454# make 'passwd' work.
455#
456pb_log(1,"Authfix\n");
457pb_system("chroot $vepath /bin/bash -c \"if [ -x /usr/bin/authconfig ]; then /usr/bin/authconfig --enableshadow --update; fi\"","Calling authconfig");
458
459# Installed additional packages we were asked to
460if (defined $opts{'a'}) {
461 $opts{'a'} =~ s/,/ /g;
462 pb_system("chroot $vepath /bin/bash -c \"$pbupd $opts{'a'} \"","Adding packages to OS by running $pbupd $opts{'a'}");
463}
464
465#
466# Clean up
467#
468pb_log(1,"Cleaning up\n");
469if ($pbupd =~ /yum/) {
470 pb_system("chroot $vepath /usr/bin/yum clean all","Cleaning yum");
471}
472pb_system("umount $vepath/proc","Unmounting /proc");
473find(\&unlink_old_conf, $vepath);
474
475# Add additional packages if asked for
476
477# Executes post-install step if asked for
478if ($opts{'s'}) {
479 pb_system("$opts{'s'} $vepath","Executing the post-install script: $opts{'s'} $vepath");
480}
481
482# Function for File::Find
483sub unlink_old_conf {
484
485 unlink($_) if ($_ =~ /\.rpmorig$/);
486 unlink($_) if ($_ =~ /\.rpmnew$/);
487}
488
489
Note: See TracBrowser for help on using the repository browser.