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

Last change on this file since 1350 was 1350, checked in by Bruno Cornec, 12 years ago

r4380@localhost: bruno | 2011-10-26 15:30:49 +0200

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