| 1 | #!/usr/bin/perl -w |
|---|
| 2 | # |
|---|
| 3 | # rpmbootstrap application, a debootstrap like for RPM distros |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | # |
|---|
| 7 | # Copyright B. Cornec 2010-2012 |
|---|
| 8 | # Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard |
|---|
| 9 | # Provided under the GPL v2 |
|---|
| 10 | |
|---|
| 11 | # Syntax: see at end |
|---|
| 12 | |
|---|
| 13 | use strict 'vars'; |
|---|
| 14 | use Getopt::Long qw(:config auto_abbrev no_ignore_case); |
|---|
| 15 | use Data::Dumper; |
|---|
| 16 | use English; |
|---|
| 17 | use LWP::UserAgent; |
|---|
| 18 | use File::Basename; |
|---|
| 19 | use File::Copy; |
|---|
| 20 | use File::Find; |
|---|
| 21 | use ProjectBuilder::Version; |
|---|
| 22 | use ProjectBuilder::Base; |
|---|
| 23 | use ProjectBuilder::Env; |
|---|
| 24 | use ProjectBuilder::Conf; |
|---|
| 25 | use ProjectBuilder::Distribution; |
|---|
| 26 | |
|---|
| 27 | # Global variables |
|---|
| 28 | my %opts; # CLI Options |
|---|
| 29 | |
|---|
| 30 | =pod |
|---|
| 31 | |
|---|
| 32 | =head1 NAME |
|---|
| 33 | |
|---|
| 34 | rpmbootstrap - creates a chrooted RPM based distribution a la debootstrap, aka Virtual Environment (VE) |
|---|
| 35 | |
|---|
| 36 | =head1 DESCRIPTION |
|---|
| 37 | |
|---|
| 38 | rpmbootstrap creates a chroot environment (Virtual Environment or VE) |
|---|
| 39 | with a minimal distribution in it, suited for building packages for example. |
|---|
| 40 | It's very much like debootstrap but for RPM based distribution. |
|---|
| 41 | It aims at supporting all distributions supported by project-builder.org |
|---|
| 42 | (RHEL, RH, Fedora, OpeSUSE, SLES, Mandriva, ...) |
|---|
| 43 | |
|---|
| 44 | It is inspired by work done by Steve Kemp for rinse (http://www.steve.org.uk/), |
|---|
| 45 | and similar to mock or febootstrap, but fully integrated with project-builder.org |
|---|
| 46 | (which also supports rinse and mock). Contrary to these, rpmbootstrap creates |
|---|
| 47 | an environment where the packages commands are usable after build, as described |
|---|
| 48 | hereafter. |
|---|
| 49 | |
|---|
| 50 | rpmbootstrap works in 2 phases. The first one is used to download all |
|---|
| 51 | the required packages to have a working package management system in the |
|---|
| 52 | chroot working. This list of packages is stored in /etc/pb/pb.conf under |
|---|
| 53 | the rbsmindep parameter (aka rpmbootstrap minimal dependencies). Once the |
|---|
| 54 | packages have been downloaded from the mirror, they are extracted with |
|---|
| 55 | rpm2cpio. At that point you should be able to use yum on Fedora, urpmi |
|---|
| 56 | on Mandriva/Mageia and zypper on OpenSuSE. |
|---|
| 57 | The second phase uses exactly the previous mentioned tools to install |
|---|
| 58 | exactly the same package list to have a coherent RPM db at the end. |
|---|
| 59 | |
|---|
| 60 | rpmbootstrap has additional options to execute a post-install script |
|---|
| 61 | (-s) or to add packages (-a). Then pb can use the chroot to perform even |
|---|
| 62 | more actions in it. |
|---|
| 63 | |
|---|
| 64 | =head1 SYNOPSIS |
|---|
| 65 | |
|---|
| 66 | rpmbootstrap [-vhmqpdk][-s script][-i iso][-a pkg1[,pkg2,...]] distribution-version-arch [target-dir] [mirror [script]] |
|---|
| 67 | |
|---|
| 68 | rpmbootstrap [--verbose][--help][--man][--quiet][--print-rpms][--download-only] |
|---|
| 69 | [--keep][--script script][--iso iso][--add pkg1,[pkg2,...]] distribution-version-arch [target-dir] [mirror [script]] |
|---|
| 70 | |
|---|
| 71 | =head1 OPTIONS |
|---|
| 72 | |
|---|
| 73 | =over 4 |
|---|
| 74 | |
|---|
| 75 | =item B<-v|--verbose> |
|---|
| 76 | |
|---|
| 77 | Print a brief help message and exits. |
|---|
| 78 | |
|---|
| 79 | =item B<-h|--help> |
|---|
| 80 | |
|---|
| 81 | Print a brief help message and exits. |
|---|
| 82 | |
|---|
| 83 | =item B<--man> |
|---|
| 84 | |
|---|
| 85 | Prints the manual page and exits. |
|---|
| 86 | |
|---|
| 87 | =item B<-q|--quiet> |
|---|
| 88 | |
|---|
| 89 | Do not print any output. |
|---|
| 90 | |
|---|
| 91 | =item B<-p|--print-rpms> |
|---|
| 92 | |
|---|
| 93 | Print the packages to be installed, and exit. |
|---|
| 94 | Note that a target directory must be specified so rpmbootstrap can determine |
|---|
| 95 | which packages should be installed, and to resolve dependencies. |
|---|
| 96 | The target directory will be deleted. |
|---|
| 97 | |
|---|
| 98 | =item B<-d|--download-only> |
|---|
| 99 | |
|---|
| 100 | Download packages, but don't perform installation. |
|---|
| 101 | |
|---|
| 102 | =item B<-k|--keep> |
|---|
| 103 | |
|---|
| 104 | Keep packages in the cache dir for later reuse. By default remove them. |
|---|
| 105 | |
|---|
| 106 | =item B<-s|--script script> |
|---|
| 107 | |
|---|
| 108 | Name of the script you want to execute on the related VEs after the installation. |
|---|
| 109 | It is executed in host environment. |
|---|
| 110 | You can use the chroot command to execute actions in the VE. |
|---|
| 111 | |
|---|
| 112 | =item B<-i|--iso iso_image> |
|---|
| 113 | |
|---|
| 114 | Name of the ISO image of the distribution you want to install on the related VE. |
|---|
| 115 | |
|---|
| 116 | =item B<-a|--add pkg1[,pkg2,...]> |
|---|
| 117 | |
|---|
| 118 | Additional packages to add from the distribution you want to install on the related VE |
|---|
| 119 | at the end of the chroot build. |
|---|
| 120 | |
|---|
| 121 | =item B<--no-stop-on-error> |
|---|
| 122 | |
|---|
| 123 | Continue through errors with best effort. |
|---|
| 124 | |
|---|
| 125 | =back |
|---|
| 126 | |
|---|
| 127 | =head1 ARGUMENTS |
|---|
| 128 | |
|---|
| 129 | =over 4 |
|---|
| 130 | |
|---|
| 131 | =item B<distribution-version-arch> |
|---|
| 132 | |
|---|
| 133 | Full name of the distribution that needs to be installed in the VE. E.g. fedora-11-x86_64. |
|---|
| 134 | |
|---|
| 135 | =item B<target-dir> |
|---|
| 136 | |
|---|
| 137 | This is the target directory under which the VE will be created. |
|---|
| 138 | Created on the fly if needed. |
|---|
| 139 | If none is given use the default directory hosting VE for project-builder.org |
|---|
| 140 | (Cf: vepath parameter in $HOME/.pbrc) |
|---|
| 141 | |
|---|
| 142 | =back |
|---|
| 143 | |
|---|
| 144 | =head1 EXAMPLE |
|---|
| 145 | |
|---|
| 146 | To setup a Fedora 12 distribution with an i386 architecture issue: |
|---|
| 147 | |
|---|
| 148 | rpmbootstrap fedora-12-i386 /tmp/fedora/12/i386 |
|---|
| 149 | |
|---|
| 150 | =head1 WEB SITES |
|---|
| 151 | |
|---|
| 152 | The main Web site of the project is available at L<http://www.project-builder.org/>. |
|---|
| 153 | Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>. |
|---|
| 154 | |
|---|
| 155 | =head1 USER MAILING LIST |
|---|
| 156 | |
|---|
| 157 | Cf: L<http://www.mondorescue.org/sympa/info/pb-announce> for announces and |
|---|
| 158 | L<http://www.mondorescue.org/sympa/info/pb-devel> for the development of the pb project. |
|---|
| 159 | |
|---|
| 160 | =head1 CONFIGURATION FILE |
|---|
| 161 | |
|---|
| 162 | Uses Project-Builder.org configuration file (/etc/pb/pb.conf or /usr/local/etc/pb/pb.conf) |
|---|
| 163 | |
|---|
| 164 | =head1 AUTHORS |
|---|
| 165 | |
|---|
| 166 | The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>. |
|---|
| 167 | |
|---|
| 168 | =head1 COPYRIGHT |
|---|
| 169 | |
|---|
| 170 | Project-Builder.org is distributed under the GPL v2.0 license |
|---|
| 171 | described in the file C<COPYING> included with the distribution. |
|---|
| 172 | |
|---|
| 173 | =cut |
|---|
| 174 | |
|---|
| 175 | # --------------------------------------------------------------------------- |
|---|
| 176 | |
|---|
| 177 | $Global::pb_stop_on_error = 1; |
|---|
| 178 | my ($projectbuilderver,$projectbuilderrev) = pb_version_init(); |
|---|
| 179 | my $appname = "rpmbootstrap"; |
|---|
| 180 | $ENV{'PBPROJ'} = $appname; |
|---|
| 181 | |
|---|
| 182 | # Initialize the syntax string |
|---|
| 183 | |
|---|
| 184 | pb_syntax_init("$appname Version $projectbuilderver-$projectbuilderrev\n"); |
|---|
| 185 | pb_temp_init(); |
|---|
| 186 | |
|---|
| 187 | GetOptions("help|?|h" => \$opts{'h'}, |
|---|
| 188 | "man|m" => \$opts{'man'}, |
|---|
| 189 | "verbose|v+" => \$opts{'v'}, |
|---|
| 190 | "quiet|q" => \$opts{'q'}, |
|---|
| 191 | "log-files|l=s" => \$opts{'l'}, |
|---|
| 192 | "script|s=s" => \$opts{'s'}, |
|---|
| 193 | "print-rpms|p" => \$opts{'p'}, |
|---|
| 194 | "download-only|d" => \$opts{'d'}, |
|---|
| 195 | "keep|k" => \$opts{'k'}, |
|---|
| 196 | "iso|i=s" => \$opts{'i'}, |
|---|
| 197 | "add|a=s" => \$opts{'a'}, |
|---|
| 198 | "version|V=s" => \$opts{'V'}, |
|---|
| 199 | "stop-on-error!" => \$Global::pb_stop_on_error, |
|---|
| 200 | ) || pb_syntax(-1,0); |
|---|
| 201 | |
|---|
| 202 | if (defined $opts{'h'}) { |
|---|
| 203 | pb_syntax(0,1); |
|---|
| 204 | } |
|---|
| 205 | if (defined $opts{'man'}) { |
|---|
| 206 | pb_syntax(0,2); |
|---|
| 207 | } |
|---|
| 208 | if (defined $opts{'v'}) { |
|---|
| 209 | $pbdebug = $opts{'v'}; |
|---|
| 210 | } |
|---|
| 211 | if (defined $opts{'q'}) { |
|---|
| 212 | $pbdebug=-1; |
|---|
| 213 | } |
|---|
| 214 | if (defined $opts{'l'}) { |
|---|
| 215 | open(pbLOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!"; |
|---|
| 216 | $pbLOG = \*pbLOG; |
|---|
| 217 | $pbdebug = 0 if ($pbdebug == -1); |
|---|
| 218 | } |
|---|
| 219 | pb_log_init($pbdebug, $pbLOG); |
|---|
| 220 | #pb_display_init("text",""); |
|---|
| 221 | |
|---|
| 222 | #if (defined $opts{'s'}) { |
|---|
| 223 | #$pbscript = $opts{'s'}; |
|---|
| 224 | #} |
|---|
| 225 | #if (defined $opts{'i'}) { |
|---|
| 226 | #$iso = $opts{'i'}; |
|---|
| 227 | #} |
|---|
| 228 | |
|---|
| 229 | # Get VE name |
|---|
| 230 | $ENV{'PBV'} = shift @ARGV; |
|---|
| 231 | die pb_syntax(-1,1) if (not defined $ENV{'PBV'}); |
|---|
| 232 | |
|---|
| 233 | die "Needs to be run as root" if ($EFFECTIVE_USER_ID != 0); |
|---|
| 234 | |
|---|
| 235 | # |
|---|
| 236 | # Initialize distribution info from pb conf file |
|---|
| 237 | # |
|---|
| 238 | pb_log(0,"Starting VE build for $ENV{'PBV'}\n"); |
|---|
| 239 | my $pbos = pb_distro_get_context($ENV{'PBV'}); |
|---|
| 240 | |
|---|
| 241 | # |
|---|
| 242 | # Check target dir |
|---|
| 243 | # Create if not existent and use default if none given |
|---|
| 244 | # |
|---|
| 245 | pb_env_init_pbrc(); # to get content of HOME/.pbrc |
|---|
| 246 | my $vepath = shift @ARGV; |
|---|
| 247 | |
|---|
| 248 | # |
|---|
| 249 | # Check for command requirements |
|---|
| 250 | # |
|---|
| 251 | my ($req,$opt) = pb_conf_get_if("oscmd","oscmdopt"); |
|---|
| 252 | pb_check_requirements($req,$opt,$appname); |
|---|
| 253 | |
|---|
| 254 | if (not defined $vepath) { |
|---|
| 255 | my ($vestdpath) = pb_conf_get("vepath"); |
|---|
| 256 | $vepath = pb_path_expand("$vestdpath->{'default'}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}") if (defined $vestdpath->{'default'}); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | die pb_log(0,"No target-dir specified and no default vepath found in $ENV{'PBETC'}\n") if (not defined $vepath); |
|---|
| 260 | |
|---|
| 261 | pb_mkdir_p($vepath) if (! -d $vepath); |
|---|
| 262 | |
|---|
| 263 | # |
|---|
| 264 | # Get the package list to download, store them in a cache directory |
|---|
| 265 | # |
|---|
| 266 | my ($rbscachedir) = pb_conf_get_if("cachedir"); |
|---|
| 267 | my ($pkgs,$mirror) = pb_distro_get_param($pbos,pb_conf_get("rbsmindep","rbsmirrorsrv")); |
|---|
| 268 | die "No packages defined for $pbos->{name}-$pbos->{version}-$pbos->{arch}" unless $pkgs =~ /\w/; |
|---|
| 269 | |
|---|
| 270 | my $cachedir = "/var/cache/rpmbootstrap"; |
|---|
| 271 | $cachedir = $rbscachedir->{'default'} if (defined $rbscachedir->{'default'}); |
|---|
| 272 | $cachedir = $rbscachedir->{$appname} if (defined $rbscachedir->{$appname}); |
|---|
| 273 | |
|---|
| 274 | # Point to the right subdir and create it if needed |
|---|
| 275 | $cachedir .= "/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}"; |
|---|
| 276 | pb_mkdir_p($cachedir) if (! -d $cachedir); |
|---|
| 277 | |
|---|
| 278 | # Get the complete package name from the mirror |
|---|
| 279 | # |
|---|
| 280 | my $ua = LWP::UserAgent->new; |
|---|
| 281 | $ua->timeout(10); |
|---|
| 282 | $ua->env_proxy; |
|---|
| 283 | |
|---|
| 284 | die "No mirror defined for $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}" if ((not defined $mirror) || ($mirror =~ /^\t*$/)); |
|---|
| 285 | pb_log(0,"Downloading package list from $mirror ...\n"); |
|---|
| 286 | my $response = $ua->get($mirror); |
|---|
| 287 | if (! $response->is_success) { |
|---|
| 288 | if ($mirror =~ /i386/) { |
|---|
| 289 | # Some distro have an i586 or i686 mirror dir instead for i386 |
|---|
| 290 | warn "Unable to download package from $mirror for $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.".$response->status_line; |
|---|
| 291 | $mirror =~ s|/i386/|/i586/|; |
|---|
| 292 | $response = $ua->get($mirror); |
|---|
| 293 | if (! $response->is_success) { |
|---|
| 294 | die "Unable to download package from $mirror for $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}".$response->status_line; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | pb_log(3,"Mirror $mirror gave answer: ".Dumper($response->dump(maxlength => 0))."\n"); |
|---|
| 299 | |
|---|
| 300 | # Try to find where the repodata structure is for later usage |
|---|
| 301 | my $repo = $mirror; |
|---|
| 302 | my $found = 0; |
|---|
| 303 | if ($pbos->{'install'} =~ /yum/) { |
|---|
| 304 | my $response1; |
|---|
| 305 | while ($found == 0) { |
|---|
| 306 | $response1 = $ua->get("$repo/repodata"); |
|---|
| 307 | pb_log(2,"REPO analyzed: $repo\n"); |
|---|
| 308 | if (! $response1->is_success) { |
|---|
| 309 | $repo = dirname($repo); |
|---|
| 310 | |
|---|
| 311 | # There is a limit to the loop, when / is reached and nothing found |
|---|
| 312 | my ($scheme, $account, $host, $port, $path) = pb_get_uri($repo); |
|---|
| 313 | die "Unable to find the repodata structure of the mirror $mirror\nPlease check the URL or warn the dev team.\n" if (($path =~ /^[\/]+$/) || ($path =~ /^$/)); |
|---|
| 314 | |
|---|
| 315 | # / not reached, so looping |
|---|
| 316 | next; |
|---|
| 317 | } else { |
|---|
| 318 | # repodata found $repo is correct |
|---|
| 319 | $found = 1; |
|---|
| 320 | pb_log(2,"REPO found: $repo\n"); |
|---|
| 321 | last; |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | # Manages architectures specificities |
|---|
| 327 | my $parch = $pbos->{'arch'}; |
|---|
| 328 | $parch = "i[3456]86" if ($pbos->{'arch'} eq "i386"); |
|---|
| 329 | |
|---|
| 330 | # Get the list of packages and their URL in this hash |
|---|
| 331 | my %url; |
|---|
| 332 | foreach my $l (split(/\n/,$response->as_string())) { |
|---|
| 333 | # Find a href ref in first pos |
|---|
| 334 | if ($l =~ /<a href="([^<>]*)">([^<>]*)<\/a>/i) { |
|---|
| 335 | my $url = $1; |
|---|
| 336 | my $pkg = $1; |
|---|
| 337 | my $desc = $2; |
|---|
| 338 | pb_log(3,"Found desc URL $desc: "); |
|---|
| 339 | # find an rpm package ref name-ver-tag.arch.rpm |
|---|
| 340 | if ($pkg =~ /(.+)-([^-]+)-([^-]+)\.(noarch|$parch)\.rpm$/) { |
|---|
| 341 | pb_log(3,"package ($1 + $2 + $3 + $4)\n"); |
|---|
| 342 | $url{$1} = "$mirror/$url"; |
|---|
| 343 | } else { |
|---|
| 344 | pb_log(3,"not a package\n"); |
|---|
| 345 | } |
|---|
| 346 | } |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | # |
|---|
| 350 | # Prepare early the yum cache env for the VE in order to copy in it packages on the fly |
|---|
| 351 | # |
|---|
| 352 | my $oscachedir = "/tmp"; |
|---|
| 353 | my $osupdcachedir; |
|---|
| 354 | my $osupdname = ""; |
|---|
| 355 | |
|---|
| 356 | if ($pbos->{'install'} =~ /yum/) { |
|---|
| 357 | $oscachedir = "$vepath/var/cache/yum/core/packages/"; |
|---|
| 358 | $osupdcachedir = "$vepath/var/cache/yum/updates-released/packages/"; |
|---|
| 359 | $osupdname = "YUM"; |
|---|
| 360 | # Recent Fedora release use a new yum cache dir |
|---|
| 361 | if (($pbos->{'name'} eq "fedora") && ($pbos->{'version'} > 8)) { |
|---|
| 362 | $oscachedir = "$vepath/var/cache/yum/$pbos->{'arch'}/$pbos->{'version'}/fedora/packages"; |
|---|
| 363 | $osupdcachedir = "$vepath/var/cache/yum/$pbos->{'arch'}/$pbos->{'version'}/updates/packages"; |
|---|
| 364 | $osupdcachedir = "$vepath/var/cache/yum/updates-released/packages/"; |
|---|
| 365 | } |
|---|
| 366 | } elsif ($pbos->{'install'} =~ /zypper/) { |
|---|
| 367 | $oscachedir = "$vepath/var/cache/zypp/packages/opensuse/suse/$pbos->{'arch'}"; |
|---|
| 368 | $osupdname = "Zypper"; |
|---|
| 369 | } elsif ($pbos->{'install'} =~ /urpmi/) { |
|---|
| 370 | $oscachedir = "$vepath/var/cache/urpmi/rpms"; |
|---|
| 371 | $osupdname = "URPMI"; |
|---|
| 372 | } |
|---|
| 373 | pb_log(1,"Setting up $osupdname cache in VE\n"); |
|---|
| 374 | pb_mkdir_p($oscachedir); |
|---|
| 375 | pb_mkdir_p($osupdcachedir) if (defined $osupdcachedir); |
|---|
| 376 | |
|---|
| 377 | # For each package to process, get it, put it in the cache dir |
|---|
| 378 | # and extract it in the target dir. If not asked to keep, remove it |
|---|
| 379 | # Just download if asked so. |
|---|
| 380 | |
|---|
| 381 | my $warning = 0; |
|---|
| 382 | my $lwpkg =""; |
|---|
| 383 | my @installed_packages; |
|---|
| 384 | |
|---|
| 385 | foreach my $p (split(/,/,$pkgs)) { |
|---|
| 386 | $p =~ s/\s+//go; |
|---|
| 387 | pb_log(1,"Processing package $p ...\n"); |
|---|
| 388 | # Just print packages names if asked so. |
|---|
| 389 | if (defined $url{$p}) { |
|---|
| 390 | if ($opts{'p'}) { |
|---|
| 391 | pb_log(0,"$url{$p}\n"); |
|---|
| 392 | next; |
|---|
| 393 | } else { |
|---|
| 394 | # Now download if not already in cache |
|---|
| 395 | my $p1 = basename($url{$p}); |
|---|
| 396 | if (! -f "$cachedir/$p1") { |
|---|
| 397 | pb_system("wget --quiet -O $cachedir/$p1-new $url{$p}","Downloading package $p1 ..."); |
|---|
| 398 | rename("$cachedir/$p1-new", "$cachedir/$p1") || die "mv $cachedir/$p1-new $cachedir/$p1 failed: $!"; |
|---|
| 399 | } else { |
|---|
| 400 | pb_log(1,"Package $p1 already in cache\n"); |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | # End if download only |
|---|
| 404 | if ($opts{'d'}) { |
|---|
| 405 | next; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | # |
|---|
| 409 | # Copy the cached .RPM files into the oscachedir directory, so that os doesn't need to download them again. |
|---|
| 410 | # |
|---|
| 411 | pb_log(1,"Link package into $oscachedir\n"); |
|---|
| 412 | copy("$cachedir/$p1",$oscachedir) if (defined $oscachedir); |
|---|
| 413 | symlink("$oscachedir/$p1","$osupdcachedir/p1") if (defined $osupdcachedir); |
|---|
| 414 | |
|---|
| 415 | # And extract it to the finale dir |
|---|
| 416 | pb_system("cd $vepath ; rpm2cpio $cachedir/$p1 | cpio -ivdum","Extracting package $p1 into $vepath"); |
|---|
| 417 | |
|---|
| 418 | # Remove cached package if not asked to keep |
|---|
| 419 | if (! $opts{'k'}) { |
|---|
| 420 | unlink("$cachedir/$p1"); |
|---|
| 421 | } |
|---|
| 422 | push(@installed_packages, $p); |
|---|
| 423 | } |
|---|
| 424 | } else { |
|---|
| 425 | pb_log(0,"WARNING: unable to find URL for $p\n"); |
|---|
| 426 | $warning++; |
|---|
| 427 | $lwpkg .= " $p"; |
|---|
| 428 | } |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | if ($warning ge 1) { |
|---|
| 432 | pb_log(0,"$warning WARNINGS found.\nMaybe you should review your package list for $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}\nand remove$lwpkg\n"); |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | # Stop here if we just print |
|---|
| 436 | if ($opts{'p'}) { |
|---|
| 437 | exit(0); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | # Now executes the VE finalization steps required for it to work correctly |
|---|
| 441 | pb_log(0,"VE post configuration\n"); |
|---|
| 442 | |
|---|
| 443 | # yum needs that distro-release package be installed, so force it |
|---|
| 444 | if ($pbos->{'install'} =~ /yum/) { |
|---|
| 445 | my $ddir = $pbos->{'name'}; |
|---|
| 446 | foreach my $p1 (<$cachedir/($ddir|redhat)-release-*.rpm>) { |
|---|
| 447 | copy("$cachedir/$p1","$vepath/tmp"); |
|---|
| 448 | pb_system("chroot $vepath rpm -ivh --force --nodeps /tmp/$p1","Forcing RPM installation of $p1"); |
|---|
| 449 | unlink("$vepath/tmp/$p1"); |
|---|
| 450 | } |
|---|
| 451 | } |
|---|
| 452 | # |
|---|
| 453 | # Make sure there is a resolv.conf file present, such that DNS lookups succeed. |
|---|
| 454 | # |
|---|
| 455 | pb_log(1,"Creating resolv.conf\n"); |
|---|
| 456 | pb_mkdir_p("$vepath/etc"); |
|---|
| 457 | copy("/etc/resolv.conf","$vepath/etc/"); |
|---|
| 458 | |
|---|
| 459 | # |
|---|
| 460 | # BUGFIX: |
|---|
| 461 | # |
|---|
| 462 | if ((($pbos->{'name'} eq "centos") || ($pbos->{'name'} eq "rhel")) && ($pbos->{'version'} eq "5")) { |
|---|
| 463 | pb_log(1,"BUGFIX for centos-5\n"); |
|---|
| 464 | pb_mkdir_p("$vepath/usr/lib/python2.4/site-packages/urlgrabber.skx"); |
|---|
| 465 | foreach my $i (<$vepath/usr/lib/python2.4/site-packages/urlgrabber/keepalive.*>) { |
|---|
| 466 | move($i,"$vepath/usr/lib/python2.4/site-packages/urlgrabber.skx/"); |
|---|
| 467 | } |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | # |
|---|
| 471 | # /proc needed |
|---|
| 472 | # |
|---|
| 473 | pb_mkdir_p("$vepath/proc"); |
|---|
| 474 | pb_system("mount -o bind /proc $vepath/proc","Mounting /proc") unless (-d "$vepath/proc/$$";); |
|---|
| 475 | |
|---|
| 476 | # |
|---|
| 477 | # Some devices may be needed |
|---|
| 478 | # |
|---|
| 479 | pb_mkdir_p("$vepath/dev"); |
|---|
| 480 | chmod 0755,"$vepath/dev"; |
|---|
| 481 | pb_system("mknod -m 644 $vepath/dev/random c 1 8","Creating $vepath/dev/random") if (! -c "$vepath/dev/random"); |
|---|
| 482 | pb_system("mknod -m 644 $vepath/dev/urandom c 1 9","Creating $vepath/dev/urandom") if (! -c "$vepath/dev/urandom"); |
|---|
| 483 | pb_system("mknod -m 666 $vepath/dev/zero c 1 5","Creating $vepath/dev/zero") if (! -c "$vepath/dev/zero"); |
|---|
| 484 | pb_system("mknod -m 666 $vepath/dev/null c 1 3","Creating $vepath/dev/null") if (! -c "$vepath/dev/null"); |
|---|
| 485 | |
|---|
| 486 | my $minipkglist; |
|---|
| 487 | |
|---|
| 488 | pb_log(1,"Adapting $osupdname repository entries\n"); |
|---|
| 489 | if ($pbos->{'install'} =~ /yum/) { |
|---|
| 490 | # |
|---|
| 491 | # Force the architecture for yum |
|---|
| 492 | # The goal is to allow i386 chroot on x86_64 |
|---|
| 493 | # |
|---|
| 494 | # FIX: Not sufficient to have yum working |
|---|
| 495 | # mirrorlist is not usable |
|---|
| 496 | # $releasever also needs to be filtered |
|---|
| 497 | # yum.conf as well |
|---|
| 498 | foreach my $i (<$vepath/etc/yum.repos.d/*.repo>,"$vepath/etc/yum.conf") { |
|---|
| 499 | pb_system("sed -i -e 's/\$basearch/$pbos->{'arch'}/g' $i","","quiet"); |
|---|
| 500 | pb_system("sed -i -e 's/\$releasever/$pbos->{'version'}/g' $i","","quiet"); |
|---|
| 501 | pb_system("sed -i -e 's/^mirrorlist/#mirrorlist/' $i","","quiet"); |
|---|
| 502 | # rather use neutral separators here |
|---|
| 503 | pb_system("sed -i -e 's|^#baseurl.*\$|baseurl=$repo|' $i","","quiet"); |
|---|
| 504 | } |
|---|
| 505 | $minipkglist = "ldconfig yum passwd vim-minimal dhclient authconfig"; |
|---|
| 506 | # TODO |
|---|
| 507 | # $minipkglist = join(" ", "ldconfig yum passwd vim-minimal dhclient authconfig", @installed_packages); |
|---|
| 508 | } elsif ($pbos->{'install'} =~ /zypper/) { |
|---|
| 509 | pb_mkdir_p("$vepath/etc/zypp/repos.d"); |
|---|
| 510 | open(REPO,"> $vepath/etc/zypp/repos.d/$pbos->{'name'}-$pbos->{'version'}") || die "Unable to create repo file"; |
|---|
| 511 | my $baseurl = dirname(dirname($mirror)); |
|---|
| 512 | # Setup the repo |
|---|
| 513 | if ($pbos->{'version'} eq "10.2") { |
|---|
| 514 | pb_system("chroot $vepath /bin/bash -c \"yes | /usr/bin/zypper sa $baseurl $pbos->{'name'}-$pbos->{'version'}\"","Bootstrapping Zypper"); |
|---|
| 515 | } else { |
|---|
| 516 | pb_system("chroot $vepath /bin/bash -c \"/usr/bin/zypper rr $pbos->{'name'}-$pbos->{'version'}\"","Bootstrapping Zypper",undef,1); # don't care if remove fails if add succeeds. |
|---|
| 517 | pb_system("chroot $vepath /bin/bash -c \"/usr/bin/zypper ar $baseurl $pbos->{'name'}-$pbos->{'version'}\"","Bootstrapping Zypper"); |
|---|
| 518 | } |
|---|
| 519 | #print REPO << "EOF"; |
|---|
| 520 | #[opensuse] |
|---|
| 521 | #name=$pbos->{'name'}-$pbos->{'version'} |
|---|
| 522 | #baseurl=$baseurl |
|---|
| 523 | #enabled=1 |
|---|
| 524 | #gpgcheck=1 |
|---|
| 525 | # |
|---|
| 526 | #EOF |
|---|
| 527 | close(REPO); |
|---|
| 528 | $minipkglist = "zypper"; |
|---|
| 529 | # TODO: Re-installing packages missing and necessary on opensuse 11.4 to get /etc/passwd created. |
|---|
| 530 | # $minipkglist = join(" ", "zypper", @installed_packages); |
|---|
| 531 | } elsif ($pbos->{'install'} =~ /urpmi/) { |
|---|
| 532 | # Setup the repo |
|---|
| 533 | my $baseurl = dirname(dirname(dirname($mirror))); |
|---|
| 534 | pb_system("chroot $vepath /bin/bash -c \"urpmi.addmedia --distrib $baseurl\"","Bootstrapping URPMI"); |
|---|
| 535 | # TODO here too ? |
|---|
| 536 | $minipkglist = "ldconfig urpmi passwd vim-minimal dhcp-client"; |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | # |
|---|
| 540 | # Run "install the necessary modules". |
|---|
| 541 | # No need for sudo here |
|---|
| 542 | # |
|---|
| 543 | $pbos->{'install'} =~ s/sudo//g; |
|---|
| 544 | if (((defined $ENV{http_proxy}) && ($ENV{http_proxy} ne '')) || ((defined $ENV{ftp_proxy}) && ($ENV{ftp_proxy} ne ''))) { |
|---|
| 545 | if ($pbos->{'name'} eq "opensuse") { |
|---|
| 546 | # For opensuse 11.4 or 12.1 -- one of them didn't work with http_proxy or HTTP_PROXY set. |
|---|
| 547 | open(PROXY, "> $vepath/etc/sysconfig/proxy") || die "can't open $vepath/etc/sysconfig/proxy: $!"; |
|---|
| 548 | print PROXY "HTTP_PROXY=$ENV{http_proxy}\n" if ((defined $ENV{http_proxy}) && ($ENV{http_proxy} ne '')); |
|---|
| 549 | print PROXY "FTP_PROXY=$ENV{ftp_proxy}\n" if ((defined $ENV{ftp_proxy}) && ($ENV{ftp_proxy} ne '')); |
|---|
| 550 | close(PROXY); |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | # Keep redhat variants from destroying nis domain on install |
|---|
| 555 | #pb_system("chroot $vepath /bin/bash -e -c \"ln -snf /bin/true /bin/domainname\""); |
|---|
| 556 | |
|---|
| 557 | #if ($pbos->{'name'} =~ /fedora/i) { # hack to prevent fedora from destroying NIS settings on host |
|---|
| 558 | # open (AUTH, ">$vepath/etc/pam.d/system-auth-ac") || die "unable to open $vepath/etc/pam.d/system-auth-ac for write: $!"; |
|---|
| 559 | # print AUTH "#pam_systemd -- will fix later in bootstrap\n"; |
|---|
| 560 | # close(AUTH); |
|---|
| 561 | #} |
|---|
| 562 | pb_system("chroot $vepath /bin/bash -c \"$pbos->{'install'} $minipkglist \"","Bootstrapping OS by running $pbos->{'install'} $minipkglist"); |
|---|
| 563 | |
|---|
| 564 | # CentOS6 will replace the yum.repos.d files; oddly it will leave the yum.conf file alone and make the new one ".rpmnew" |
|---|
| 565 | if (($pbos->{'name'} eq "centos") && ($pbos->{'version'} =~ /^6.*/)) { |
|---|
| 566 | pb_log(0,"Fixing $pbos->{'name'} $pbos->{'version'} bug for yum conf files"); |
|---|
| 567 | foreach my $from (<$vepath/etc/yum.repos.d/*.rpmorig>) { |
|---|
| 568 | my $to = $from; |
|---|
| 569 | $to =~ s/.rpmorig$//; |
|---|
| 570 | pb_system("mv $from $to", "Recover $from"); |
|---|
| 571 | } |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | # |
|---|
| 575 | # make 'passwd' work. |
|---|
| 576 | # |
|---|
| 577 | pb_log(1,"Authfix\n"); |
|---|
| 578 | # TODO: Not generic enough to be done like that IMHO |
|---|
| 579 | # In case it was changed during the install |
|---|
| 580 | #pb_system("chroot $vepath /bin/bash -e -c \"ln -snf /bin/true /bin/domainname\""); |
|---|
| 581 | pb_system("chroot $vepath /bin/bash -c \"if [ -x /usr/bin/authconfig ]; then /usr/bin/authconfig --enableshadow --update --nostart; fi\"","Calling authconfig"); |
|---|
| 582 | |
|---|
| 583 | # Installed additional packages we were asked to |
|---|
| 584 | if (defined $opts{'a'}) { |
|---|
| 585 | $opts{'a'} =~ s/,/ /g; |
|---|
| 586 | pb_system("chroot $vepath /bin/bash -c \"$pbos->{'install'} $opts{'a'} \"","Adding packages to OS by running $pbos->{'install'} $opts{'a'}"); |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | # |
|---|
| 590 | # Clean up |
|---|
| 591 | # |
|---|
| 592 | pb_log(1,"Cleaning up\n"); |
|---|
| 593 | if ($pbos->{'install'} =~ /yum/) { |
|---|
| 594 | pb_system("chroot $vepath /usr/bin/yum clean all","Cleaning yum"); |
|---|
| 595 | } |
|---|
| 596 | pb_system("umount $vepath/proc","Unmounting /proc"); |
|---|
| 597 | find(\&unlink_old_conf, $vepath); |
|---|
| 598 | |
|---|
| 599 | # Executes post-install step if asked for |
|---|
| 600 | if ($opts{'s'}) { |
|---|
| 601 | pb_system("$opts{'s'} $vepath","Executing the post-install script: $opts{'s'} $vepath"); |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | if ($warning > 0) { |
|---|
| 605 | pb_log(0,"\n\n\n\n$warning WARNINGS found.\nMaybe you should review your package list for $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}\nand remove$lwpkg\n"); |
|---|
| 606 | pb_log(0,"waiting 60 seconds to give you a chance to notice.\n"); |
|---|
| 607 | sleep(60); |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | # Function for File::Find |
|---|
| 611 | sub unlink_old_conf { |
|---|
| 612 | |
|---|
| 613 | unlink($_) if ($_ =~ /\.rpmorig$/); |
|---|
| 614 | unlink($_) if ($_ =~ /\.rpmnew$/); |
|---|
| 615 | } |
|---|