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

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