source: devel/rpmbootstrap/bin/rpmbootstrap @ 1029

Last change on this file since 1029 was 1029, checked in by bruno, 9 years ago
  • Fix umask propagation in VE, fixing issues in directory creation with wrong rights
  • Remove the useless vemindep option and fix ospkgdep accordingly
  • Fix the rights for the dev dir in rpmbootstrap
File size: 14.7 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    if ($mirror =~ /i386/) {
253        # Some distro have an i586 or i686 mirror dir instead for i386
254        warn "Unable to download packages from $mirror for $ddir-$dver-$darch.";
255        $mirror =~ s|/i386/|/i586/|;
256        $response = $ua->get($mirror);
257        if (! $response->is_success) {
258            die "Unable to download packages from $mirror for $ddir-$dver-$darch";
259        }
260    }
261}
262pb_log(3,"Mirror $mirror gave answer: ".Dumper($response->dump(maxlength => 0))."\n");
263
264# Manages architectures specificities
265my $parch = $darch;
266$parch = "i[3456]86" if ($darch eq "i386");
267
268# Get the list of packages and their URL in this hash
269my %url;
270foreach my $l (split(/\n/,$response->as_string())) {
271    # Find a href ref
272    if ($l =~ /<a href="(.*)">(.*)<\/a>/i) {
273        my $url = $1;
274        my $pkg = $1;
275        my $desc = $2;
276        pb_log(3,"Found desc URL $desc: ");
277        # find an rpm package ref name-ver-tag.arch.rpm
278        if ($pkg =~ /(.+)-([^-]+)-([^-]+)\.(noarch|$parch)\.rpm$/) {
279            pb_log(3,"package ($1 + $2 + $3 + $4)\n");
280            $url{$1} = "$mirror/$url";
281        } else {
282            pb_log(3,"not a package\n");
283        }
284    }
285}
286
287#
288# Prepare early the yum cache env for the VE in order to copy in it packages on the fly
289#
290my $oscachedir = "/tmp";
291my $osupdcachedir;
292my $osupdname = "";
293
294if ($pbupd =~ /yum/) {
295    $oscachedir = "$vepath/var/cache/yum/core/packages/";
296    $osupdcachedir = "$vepath/var/cache/yum/updates-released/packages/";
297    $osupdname = "YUM";
298    # Recent Fedora release use a new yum cache dir
299    if (($ddir eq "fedora") && ($dver > 8)) {
300        $oscachedir = "$vepath/var/cache/yum/$darch/$dver/fedora/packages";
301        $osupdcachedir = "$vepath/var/cache/yum/$darch/$dver/updates/packages";
302        $osupdcachedir = "$vepath/var/cache/yum/updates-released/packages/";
303    }
304} elsif ($pbupd =~ /zypper/) {
305    $oscachedir = "$vepath/var/cache/zypp/packages/opensuse/suse/$darch";
306    $osupdname = "Zypper";
307} elsif ($pbupd =~ /urpmi/) {
308    $oscachedir = "$vepath/var/cache/urpmi/rpms";
309    $osupdname = "URPMI";
310}
311pb_log(1,"Setting up $osupdname cache in VE\n");
312pb_mkdir_p($oscachedir);
313pb_mkdir_p($osupdcachedir) if (defined $osupdcachedir);
314
315# For each package to process, get it, put it in the cache dir
316# and extract it in the target dir. If not asked to keep, remove it
317# Just download if asked so.
318
319my $warning = 0;
320my $lwpkg ="";
321foreach my $p (split(/,/,$pkgs)) {
322    pb_log(1,"Processing package $p ...\n");
323    # Just print packages names if asked so.
324    if (defined $url{$p}) {
325        if ($opts{'p'}) {
326            pb_log(0,"$url{$p}\n");
327            next;
328        } else {
329            # Now download if not already in cache
330            my $p1 = basename($url{$p});
331            if (! -f "$cachedir/$p1") {
332                pb_system("wget --quiet -O $cachedir/$p1 $url{$p}","Downloading package $p1 ...");
333            } else {
334                pb_log(1,"Package $p1 already in cache\n");
335            }
336           
337            # End if download only
338            if ($opts{'d'}) {
339                next;
340            }
341
342            #
343            # Copy the cached .RPM files into the oscachedir directory, so that os doesn't need to download them again.
344            #
345            pb_log(1,"Link package into $oscachedir\n");
346            copy("$cachedir/$p1",$oscachedir) if (defined $oscachedir);
347            symlink("$oscachedir/$p1","$osupdcachedir/p1") if (defined $osupdcachedir);
348
349            # And extract it to the finale dir
350            pb_system("cd $vepath ; rpm2cpio $cachedir/$p1 | cpio -ivdum","Extracting package $p1 into $vepath");
351
352            # Remove cached package if not asked to keep
353            if (! $opts{'k'}) {
354                unlink("$cachedir/$p1");
355            }
356
357        }
358    } else {
359        pb_log(0,"WARNING: unable to find URL for $p\n");
360        $warning++;
361        $lwpkg .= " $p";
362    }
363}
364
365if ($warning ge 1) {
366    pb_log(0,"$warning WARNINGS found.\nMaybe you should review your package list for $ddir-$dver-$darch\nand remove$lwpkg\n");
367}
368
369# Stop here if we just print
370if ($opts{'p'}) {
371    exit(0);
372}
373
374# Now executes the VE finalization steps required for it to work correctly
375pb_log(0,"VE post configuration\n");
376
377# yum needs that distro-release package be installed, so force it
378if ($pbupd =~ /yum/) {
379    foreach my $p1 (<$cachedir/($ddir|redhat)-release-*.rpm>) {
380        copy("$cachedir/$p1","$vepath/tmp");
381        pb_system("chroot $vepath rpm -ivh --force --nodeps /tmp/$p1","Forcing RPM installation of $p1");
382        unlink("$vepath/tmp/$p1");
383    }
384}
385#
386# Make sure there is a resolv.conf file present, such that DNS lookups succeed.
387#
388pb_log(1,"Creating resolv.conf\n");
389pb_mkdir_p("$vepath/etc");
390copy("/etc/resolv.conf","$vepath/etc/");
391
392#
393# BUGFIX:
394#
395if ((($ddir eq "centos") || ($ddir eq "rhel")) && ($dver eq "5")) {
396    pb_log(1,"BUGFIX for centos-5\n");
397    pb_mkdir_p("$vepath/usr/lib/python2.4/site-packages/urlgrabber.skx");
398    foreach my $i (<$vepath/usr/lib/python2.4/site-packages/urlgrabber/keepalive.*>) {
399        move($i,"$vepath/usr/lib/python2.4/site-packages/urlgrabber.skx/");
400    }
401}
402
403#
404# /proc needed
405#
406pb_mkdir_p("$vepath/proc");
407pb_system("mount -o bind /proc $vepath/proc","Mounting /proc");
408
409#
410# Some devices may be needed
411#
412pb_mkdir_p("$vepath/dev");
413chmod 0755,"$vepath/dev";
414pb_system("mknod -m 644 $vepath/dev/random c 1 8","Creating $vepath/dev/random") if (! -c "$vepath/dev/random");
415pb_system("mknod -m 644 $vepath/dev/urandom c 1 9","Creating $vepath/dev/urandom") if (! -c "$vepath/dev/urandom");
416pb_system("mknod -m 666 $vepath/dev/zero c 1 5","Creating $vepath/dev/zero") if (! -c "$vepath/dev/zero");
417
418my $minipkglist;
419
420pb_log(1,"Adapting $osupdname repository entries\n");
421if ($pbupd =~ /yum/) {
422    #
423    # Force the architecture for yum
424    # The goal is to allow i386 chroot on x86_64
425    #
426    # FIX: Not sufficient to have yum working
427    # mirrorlist is not usable
428    # $releasever also needs to be filtered
429    # yum.conf as well
430    foreach my $i (<$vepath/etc/yum.repos.d/*>,"$vepath/etc/yum.conf") {
431        pb_system("sed -i -e 's/\$basearch/$darch/g' $i","","quiet");
432        pb_system("sed -i -e 's/\$releasever/$dver/g' $i","","quiet");
433        pb_system("sed -i -e 's/^mirrorlist/#mirrorlist/' $i","","quiet");
434        # rather use neutral separators here
435        pb_system("sed -i -e 's|^#baseurl.*\$|baseurl=$mirror|' $i","","quiet");
436    }
437    $minipkglist = "ldconfig yum passwd vim-minimal dhclient authconfig";
438} elsif ($pbupd =~ /zypper/) {
439    pb_mkdir_p("$vepath/etc/zypp/repos.d");
440    open(REPO,"> $vepath/etc/zypp/repos.d/$ddir-$dver") || die "Unable to create repo file";
441    my $baseurl = dirname(dirname($mirror));
442    # Setup the repo
443    print REPO << 'EOF';
444[opensuse]
445name=$ddir-$dver
446baseurl=$baseurl
447enabled=1
448gpgcheck=1
449
450EOF
451    close(REPO);
452    $minipkglist = "zypper vim-minimal dhclient";
453    # Bootstraping zypper
454    if ($dver eq "10.2") {
455        pb_system("chroot $vepath /bin/bash -c \"yes | /usr/bin/zypper sa $baseurl $ddir-$dver\"","Bootstrapping Zypper");
456    }
457} elsif ($pbupd =~ /urpmi/) {
458    # Setup the repo
459    my $baseurl = dirname(dirname(dirname($mirror)));
460    pb_system("chroot $vepath /bin/bash -c \"urpmi.addmedia --distrib $baseurl\"","Bootstrapping URPMI");
461    $minipkglist = "ldconfig urpmi passwd vim-minimal dhcp-client";
462}
463
464#
465# Run "install the necessary modules".
466# No need for sudo here
467#
468$pbupd =~ s/sudo//g;
469pb_system("chroot $vepath /bin/bash -c \"$pbupd $minipkglist \"","Bootstrapping OS by running $pbupd $minipkglist");
470
471#
472# make 'passwd' work.
473#
474pb_log(1,"Authfix\n");
475pb_system("chroot $vepath /bin/bash -c \"if [ -x /usr/bin/authconfig ]; then /usr/bin/authconfig --enableshadow --update; fi\"","Calling authconfig");
476
477# Installed additional packages we were asked to
478if (defined $opts{'a'}) {
479    $opts{'a'} =~ s/,/ /g;
480    pb_system("chroot $vepath /bin/bash -c \"$pbupd $opts{'a'} \"","Adding packages to OS by running $pbupd $opts{'a'}");
481}
482
483#
484# Clean up
485#
486pb_log(1,"Cleaning up\n");
487if ($pbupd =~ /yum/) {
488    pb_system("chroot $vepath /usr/bin/yum clean all","Cleaning yum");
489}
490pb_system("umount $vepath/proc","Unmounting /proc");
491find(\&unlink_old_conf, $vepath);
492
493# Add additional packages if asked for
494
495# Executes post-install step if asked for
496if ($opts{'s'}) {
497    pb_system("$opts{'s'} $vepath","Executing the post-install script: $opts{'s'} $vepath");
498}
499
500# Function for File::Find
501sub unlink_old_conf {
502
503    unlink($_) if ($_ =~ /\.rpmorig$/);
504    unlink($_) if ($_ =~ /\.rpmnew$/);
505}
506
507
Note: See TracBrowser for help on using the repository browser.