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