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

Last change on this file since 1588 was 1588, checked in by Bruno Cornec, 12 years ago
  • Use pb_cjeck_req for all commands called in the VE for its setup, so it can be used correctly in a secured environement with a sudoers conf file (Cf pbtest)
File size: 10.6 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 close(CMD);
107 }
108 }
109 }
110
111 # Handle cross arch on Intel based platforms
112 $sudocmd = "setarch i386 $sudocmd" if (($pbos->{'arch'} =~ /i[3456]86/) && ($arch eq 'x86_64'));
113
114 my $root = pb_path_expand($vepath->{$ENV{PBPROJ}});
115 if (((defined $verebuild) && ($verebuild->{$ENV{'PBPROJ'}} =~ /true/i)) || ($pbforce == 1)) {
116 my ($verpmtype,$vedebtype) = pb_conf_get("verpmtype","vedebtype");
117 my ($rbsopt1) = pb_conf_get_if("rbsopt");
118
119 # We have to rebuild the chroot
120 if ($pbos->{'type'} eq "rpm") {
121
122 # Which tool is used
123 my $verpmstyle = $verpmtype->{$ENV{'PBPROJ'}};
124 die "No verpmtype defined for $ENV{PBPROJ}" unless (defined $verpmstyle);
125
126 # Get potential rbs option
127 my $rbsopt = "";
128 if (defined $rbsopt1) {
129 if (defined $rbsopt1->{$verpmstyle}) {
130 $rbsopt = $rbsopt1->{$verpmstyle};
131 } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
132 $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
133 } else {
134 $rbsopt = "";
135 }
136 }
137
138 my $postinstall = pb_ve_get_postinstall($pbos,$rbspi,$verpmstyle);
139 if ($verpmstyle eq "rinse") {
140 # Need to reshape the mirrors generated with local before-post-install script
141 my $b4post = "--before-post-install ";
142 my $postparam = pb_distro_get_param($pbos,$rbsb4pi);
143 if ($postparam eq "") {
144 $b4post = "";
145 } else {
146 $b4post .= $postparam;
147 }
148
149 # Need to reshape the package list for pb
150 my $addpkgs;
151 $postparam = "";
152 $postparam .= pb_distro_get_param($pbos,$osmindep);
153 if ($postparam eq "") {
154 $addpkgs = "";
155 } else {
156 my $pkgfile = "$ENV{'PBTMP'}/addpkgs.lis";
157 open(PKG,"> $pkgfile") || die "Unable to create $pkgfile";
158 foreach my $p (split(/,/,$postparam)) {
159 print PKG "$p\n";
160 }
161 close(PKG);
162 $addpkgs = "--add-pkg-list $pkgfile";
163 }
164
165 my $rinseverb = "";
166 $rinseverb = "--verbose" if ($pbdebug gt 0);
167 my ($rbsconf) = pb_conf_get("rbsconf");
168
169 my $command = pb_check_req("rinse",0);
170 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");
171 } elsif ($verpmstyle eq "rpmbootstrap") {
172 my $rbsverb = "";
173 foreach my $i (1..$pbdebug) {
174 $rbsverb .= " -v";
175 }
176 my $addpkgs = "";
177 my $postparam = "";
178 $postparam .= pb_distro_get_param($pbos,$osmindep);
179 if ($postparam eq "") {
180 $addpkgs = "";
181 } else {
182 $addpkgs = "-a $postparam";
183 }
184 my $command = pb_check_req("rpmbootstrap",0);
185 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");
186 } elsif ($verpmstyle eq "mock") {
187 my ($rbsconf) = pb_conf_get("rbsconf");
188 my $command = pb_check_req("mock",0);
189 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'})");
190 # Once setup we need to install some packages, the pb account, ...
191 pb_system("$sudocmd $command --install --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
192 } else {
193 die "Unknown verpmtype type $verpmstyle. Report to dev team";
194 }
195 } elsif ($pbos->{'type'} eq "deb") {
196 my $vedebstyle = $vedebtype->{$ENV{'PBPROJ'}};
197
198 my $codename = pb_distro_get_param($pbos,$oscodename);
199 my $postparam = "";
200 my $addpkgs;
201 $postparam .= pb_distro_get_param($pbos,$osmindep);
202 if ($postparam eq "") {
203 $addpkgs = "";
204 } else {
205 $addpkgs = "--include $postparam";
206 }
207 my $debmir = "";
208 $debmir .= pb_distro_get_param($pbos,$rbsmirrorsrv);
209
210 # Get potential rbs option
211 my $rbsopt = "";
212 if (defined $rbsopt1) {
213 if (defined $rbsopt1->{$vedebstyle}) {
214 $rbsopt = $rbsopt1->{$vedebstyle};
215 } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
216 $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
217 } else {
218 $rbsopt = "";
219 }
220 }
221
222 # debootstrap works with amd64 not x86_64
223 my $debarch = $pbos->{'arch'};
224 $debarch = "amd64" if ($pbos->{'arch'} eq "x86_64");
225 if ($vedebstyle eq "debootstrap") {
226 my $dbsverb = "";
227 $dbsverb = "--verbose" if ($pbdebug gt 0);
228
229 # Some perl modules are in Universe on Ubuntu
230 $rbsopt .= " --components=main,universe" if ($pbos->{'name'} eq "ubuntu");
231
232 my $cmd1 = pb_check_req("mkdir",0);
233 my $cmd2 = pb_check_req("debootstrap",0);
234 pb_system("$sudocmd $cmd1 -p $root/$pbos->{name}/$pbos->{version}/$pbos->{arch} ; $sudocmd $cmd2 $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");
235 # debootstrap doesn't create an /etc/hosts file
236 if (! -f "$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/etc/hosts" ) {
237 my $cmd = pb_check_req("cp",0);
238 pb_system("$sudocmd $cmd /etc/hosts $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/etc/hosts");
239 }
240 } else {
241 die "Unknown vedebtype type $vedebstyle. Report to dev team";
242 }
243 } elsif ($pbos->{'type'} eq "ebuild") {
244 die "Please teach the dev team how to build gentoo chroot";
245 } else {
246 die "Unknown distribution type $pbos->{'type'}. Report to dev team";
247 }
248 }
249
250 # Fix modes to allow access to the VE for pb user
251 my $command = pb_check_req("chmod",0);
252 pb_system("$sudocmd $command 755 $root/$pbos->{'name'} $root/$pbos->{'name'}/$pbos->{'version'} $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}","Fixing permissions");
253
254 # Test if an existing snapshot exists and use it if appropriate
255 # And also use it of no local extracted VE is present
256 if ((-f "$root/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz") &&
257 (((defined $vesnap->{$v}) && ($vesnap->{$v} =~ /true/i)) ||
258 ((defined $vesnap->{$ENV{'PBPROJ'}}) && ($vesnap->{$ENV{'PBPROJ'}} =~ /true/i)) ||
259 ($pbsnap eq 1) ||
260 (! -d "$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}"))) {
261 my $cmd1 = pb_check_req("rm",0);
262 my $cmd2 = pb_check_req("mkdir",0);
263 my $cmd3 = pb_check_req("tar",0);
264 pb_system("$sudocmd $cmd1 -rf $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'} ; $sudocmd $cmd2 -p $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'} ; $sudocmd $cmd3 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'}");
265 }
266 # Nothing more to do for VE. No real launch
267} else {
268 die "VE of type $vetype not supported. Report to the dev team";
269}
270}
271
272#
273# Return the postinstall line if needed
274#
275
276sub pb_ve_get_postinstall {
277
278my $pbos = shift;
279my $rbspi = shift;
280my $vestyle = shift;
281my $post = "";
282
283# Do we have a local post-install script
284if ($vestyle eq "rinse") {
285 $post = "--post-install ";
286} elsif ($vestyle eq "rpmbootstrap") {
287 $post = "-s ";
288}
289
290my $postparam = pb_distro_get_param($pbos,$rbspi);
291if ($postparam eq "") {
292 $post = "";
293} else {
294 $post .= $postparam;
295}
296return($post);
297}
298
299
300=head1 WEB SITES
301
302The 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/>.
303
304=head1 USER MAILING LIST
305
306None exists for the moment.
307
308=head1 AUTHORS
309
310The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
311
312=head1 COPYRIGHT
313
314Project-Builder.org is distributed under the GPL v2.0 license
315described in the file C<COPYING> included with the distribution.
316
317=cut
318
319
3201;
Note: See TracBrowser for help on using the repository browser.