source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/VE.pm@ 1528

Last change on this file since 1528 was 1528, checked in by Bruno Cornec, 12 years ago
  • Add Copyrights specified by HP Open Source Review Board (Eric Anderson)
  • Clarification of copyrights, dates and licences (Bruno Cornec)
File size: 10.3 KB
Line 
1#!/usr/bin/perl -w
2#
3# Common functions for virtual environment
4#
5# Copyright B. Cornec 2007-2012
6# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
7# Provided under the GPL v2
8#
9# $Id$
10#
11
12package ProjectBuilder::VE;
13
14use strict;
15use Data::Dumper;
16use Carp 'confess';
17use English;
18use ProjectBuilder::Version;
19use ProjectBuilder::Base;
20use ProjectBuilder::Conf;
21use ProjectBuilder::Distribution;
22
23# Global vars
24# Inherit from the "Exporter" module which handles exporting functions.
25
26use vars qw($VERSION $REVISION @ISA @EXPORT);
27use Exporter;
28
29# Export, by default, all the functions into the namespace of
30# any code which uses this module.
31
32our @ISA = qw(Exporter);
33our @EXPORT = qw(pb_ve_launch);
34
35($VERSION,$REVISION) = pb_version_init();
36
37=pod
38
39=head1 NAME
40
41ProjectBuilder::VE, part of the project-builder.org - module dealing with Virtual Environment
42
43=head1 DESCRIPTION
44
45This modules provides functions to deal with Virtual Environements (VE), aka chroot.
46
47=head1 SYNOPSIS
48
49 use ProjectBuilder::VE;
50
51 #
52 # Return information on the running distro
53 #
54 my $pbos = pb_ve_launch();
55
56=head1 USAGE
57
58=over 4
59
60=item B<pb_ve_launch>
61
62This function launch a VE, creating it if necessary using multiple external potential tools.
63
64=cut
65
66sub pb_ve_launch {
67
68my $v = shift || undef;
69my $pbforce = shift || 0; # By default do not rebuild VE
70my $pbsnap = shift || 0; # By default do not snap VE
71
72# Get distro context
73my $pbos = pb_distro_get_context($v);
74
75# Get VE context
76my ($ptr,$vepath) = pb_conf_get("vetype","vepath");
77my $vetype = $ptr->{$ENV{'PBPROJ'}};
78
79confess "No vetype defined for $ENV{PBPROJ}" unless (defined $vetype);
80pb_log(1, "Using vetype $vetype for $ENV{PBPROJ}\n");
81
82if (($vetype eq "chroot") || ($vetype eq "schroot")) {
83
84 # We need to avoid umask propagation to the VE
85 umask 0022;
86
87 # We can probably only get those params now we have the distro context
88 my ($rbsb4pi,$rbspi,$vesnap,$oscodename,$osmindep,$verebuild,$rbsmirrorsrv) = pb_conf_get_if("rbsb4pi","rbspi","vesnap","oscodename","osmindep","verebuild","rbsmirrorsrv");
89
90 # Architecture consistency
91 my $arch = pb_get_arch();
92 if ($arch ne $pbos->{'arch'}) {
93 die "Unable to launch a VE of architecture $pbos->{'arch'} on a $arch platform" unless (($pbos->{'arch'} =~ /i?86/o) && ($arch eq "x86_64"));
94 }
95
96 # If we are already root (from pbmkbm e.g.) don't use sudo, just call the command
97 my $sudocmd="";
98 if ($EFFECTIVE_USER_ID != 0) {
99 $sudocmd ="sudo ";
100 foreach my $proxy (qw/http_proxy ftp_proxy/) {
101 if (defined $ENV{$proxy}) {
102 open(<CMD>,"sudo sh -c 'echo \$$proxy' |") or die "can't run sudo sh?: $!";
103 $_ = <CMD>;
104 chomp();
105 die "sudo not passing through env var $proxy; '$ENV{$proxy}' != '$_'\nAdd line Defaults:`whoami` env_keep += \"$proxy\" to sudoers file?" unless $_ eq $ENV{$proxy};
106 }
107 }
108 # Handle cross arch on Intel based platforms
109 $sudocmd = "setarch i386 $sudocmd" if (($pbos->{arch} =~ /i?86/) && ($arch eq 'x86_64'));
110
111 my $root = pb_path_expand($vepath->{$ENV{PBPROJ}});
112 if (((defined $verebuild) && ($verebuild->{$ENV{'PBPROJ'}} =~ /true/i)) || ($pbforce == 1)) {
113 my ($verpmtype,$vedebtype) = pb_conf_get("verpmtype","vedebtype");
114 my ($rbsopt1) = pb_conf_get_if("rbsopt");
115
116 # We have to rebuild the chroot
117 if ($pbos->{'type'} eq "rpm") {
118
119 # Which tool is used
120 my $verpmstyle = $verpmtype->{$ENV{'PBPROJ'}};
121 die "No verpmtype defined for $ENV{PBPROJ}" unless (defined $verpmstyle);
122
123 # Get potential rbs option
124 my $rbsopt = "";
125 if (defined $rbsopt1) {
126 if (defined $rbsopt1->{$verpmstyle}) {
127 $rbsopt = $rbsopt1->{$verpmstyle};
128 } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
129 $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
130 } else {
131 $rbsopt = "";
132 }
133 }
134
135 my $postinstall = pb_ve_get_postinstall($pbos,$rbspi,$verpmstyle);
136 if ($verpmstyle eq "rinse") {
137 # Need to reshape the mirrors generated with local before-post-install script
138 my $b4post = "--before-post-install ";
139 my $postparam = pb_distro_get_param($pbos,$rbsb4pi);
140 if ($postparam eq "") {
141 $b4post = "";
142 } else {
143 $b4post .= $postparam;
144 }
145
146 # Need to reshape the package list for pb
147 my $addpkgs;
148 $postparam = "";
149 $postparam .= pb_distro_get_param($pbos,$osmindep);
150 if ($postparam eq "") {
151 $addpkgs = "";
152 } else {
153 my $pkgfile = "$ENV{'PBTMP'}/addpkgs.lis";
154 open(PKG,"> $pkgfile") || die "Unable to create $pkgfile";
155 foreach my $p (split(/,/,$postparam)) {
156 print PKG "$p\n";
157 }
158 close(PKG);
159 $addpkgs = "--add-pkg-list $pkgfile";
160 }
161
162 my $rinseverb = "";
163 $rinseverb = "--verbose" if ($pbdebug gt 0);
164 my ($rbsconf) = pb_conf_get("rbsconf");
165
166 my $command = pb_check_req("rinse",0);
167 pb_system("$sudocmd $command --directory \"$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}\" --arch \"$pbos->{'arch'}\" --distribution \"$pbos->{'name'}-$pbos->{'version'}\" --config \"$rbsconf->{$ENV{'PBPROJ'}}\" $b4post $postinstall $rbsopt $addpkgs $rinseverb","Creating the rinse VE for $pbos->{'name'}-$pbos->{'version'} ($pbos->{'arch'})", "verbose");
168 } elsif ($verpmstyle eq "rpmbootstrap") {
169 my $rbsverb = "";
170 foreach my $i (1..$pbdebug) {
171 $rbsverb .= " -v";
172 }
173 my $addpkgs = "";
174 my $postparam = "";
175 $postparam .= pb_distro_get_param($pbos,$osmindep);
176 if ($postparam eq "") {
177 $addpkgs = "";
178 } else {
179 $addpkgs = "-a $postparam";
180 }
181 my $command = pb_check_req("rpmbootstrap",0);
182 pb_system("$sudocmd $command $rbsopt $postinstall $addpkgs $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} $rbsverb","Creating the rpmbootstrap VE for $pbos->{'name'}-$pbos->{'version'} ($pbos->{'arch'})", "verbose");
183 } elsif ($verpmstyle eq "mock") {
184 my ($rbsconf) = pb_conf_get("rbsconf");
185 my $command = pb_check_req("mock",0);
186 pb_system("$sudocmd $command --init --resultdir=\"/tmp\" --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v $rbsopt","Creating the mock VE for $pbos->{'name'}-$pbos->{'version'} ($pbos->{'arch'})");
187 # Once setup we need to install some packages, the pb account, ...
188 pb_system("$sudocmd $command --install --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
189 } else {
190 die "Unknown verpmtype type $verpmstyle. Report to dev team";
191 }
192 } elsif ($pbos->{'type'} eq "deb") {
193 my $vedebstyle = $vedebtype->{$ENV{'PBPROJ'}};
194
195 my $codename = pb_distro_get_param($pbos,$oscodename);
196 my $postparam = "";
197 my $addpkgs;
198 $postparam .= pb_distro_get_param($pbos,$osmindep);
199 if ($postparam eq "") {
200 $addpkgs = "";
201 } else {
202 $addpkgs = "--include $postparam";
203 }
204 my $debmir = "";
205 $debmir .= pb_distro_get_param($pbos,$rbsmirrorsrv);
206
207 # Get potential rbs option
208 my $rbsopt = "";
209 if (defined $rbsopt1) {
210 if (defined $rbsopt1->{$vedebstyle}) {
211 $rbsopt = $rbsopt1->{$vedebstyle};
212 } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
213 $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
214 } else {
215 $rbsopt = "";
216 }
217 }
218
219 # debootstrap works with amd64 not x86_64
220 my $debarch = $pbos->{'arch'};
221 $debarch = "amd64" if ($pbos->{'arch'} eq "x86_64");
222 if ($vedebstyle eq "debootstrap") {
223 my $dbsverb = "";
224 $dbsverb = "--verbose" if ($pbdebug gt 0);
225
226 # Some perl modules are in Universe on Ubuntu
227 $rbsopt .= " --components=main,universe" if ($pbos->{'name'} eq "ubuntu");
228
229 pb_system("$sudocmd mkdir -p $root/$pbos->{name}/$pbos->{version}/$pbos->{arch} ; $sudocmd /usr/sbin/debootstrap $dbsverb $rbsopt --arch=$debarch $addpkgs $codename \"$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}\" $debmir","Creating the debootstrap VE for $pbos->{'name'}-$pbos->{'version'} ($pbos->{'arch'})", "verbose");
230 # debootstrap doesn't create an /etc/hosts file
231 if (! -f "$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/etc/hosts" ) {
232 pb_system("$sudocmd cp /etc/hosts $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/etc/hosts");
233 }
234 } else {
235 die "Unknown vedebtype type $vedebstyle. Report to dev team";
236 }
237 } elsif ($pbos->{'type'} eq "ebuild") {
238 die "Please teach the dev team how to build gentoo chroot";
239 } else {
240 die "Unknown distribution type $pbos->{'type'}. Report to dev team";
241 }
242 }
243
244 # Fix modes to allow access to the VE for pb user
245 pb_system("$sudocmd chmod 755 $root/$pbos->{'name'} $root/$pbos->{'name'}/$pbos->{'version'} $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}","Fixing permissions");
246
247 # Test if an existing snapshot exists and use it if appropriate
248 # And also use it of no local extracted VE is present
249 if ((-f "$root/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz") &&
250 (((defined $vesnap->{$v}) && ($vesnap->{$v} =~ /true/i)) ||
251 ((defined $vesnap->{$ENV{'PBPROJ'}}) && ($vesnap->{$ENV{'PBPROJ'}} =~ /true/i)) ||
252 ($pbsnap eq 1) ||
253 (! -d "$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}"))) {
254 pb_system("$sudocmd rm -rf $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'} ; $sudocmd mkdir -p $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'} ; $sudocmd tar xz -C $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'} -f $root/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz","Extracting snapshot of $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz under $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}");
255 }
256 # Nothing more to do for VE. No real launch
257} else {
258 die "VE of type $vetype not supported. Report to the dev team";
259}
260}
261
262#
263# Return the postinstall line if needed
264#
265
266sub pb_ve_get_postinstall {
267
268my $pbos = shift;
269my $rbspi = shift;
270my $vestyle = shift;
271my $post = "";
272
273# Do we have a local post-install script
274if ($vestyle eq "rinse") {
275 $post = "--post-install ";
276} elsif ($vestyle eq "rpmbootstrap") {
277 $post = "-s ";
278}
279
280my $postparam = pb_distro_get_param($pbos,$rbspi);
281if ($postparam eq "") {
282 $post = "";
283} else {
284 $post .= $postparam;
285}
286return($post);
287}
288
289
290=head1 WEB SITES
291
292The 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/>.
293
294=head1 USER MAILING LIST
295
296None exists for the moment.
297
298=head1 AUTHORS
299
300The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
301
302=head1 COPYRIGHT
303
304Project-Builder.org is distributed under the GPL v2.0 license
305described in the file C<COPYING> included with the distribution.
306
307=cut
308
309
3101;
Note: See TracBrowser for help on using the repository browser.