source: ProjectBuilder/devel/pbmkbm/bin/pbmkbm@ 1352

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

r4384@localhost: bruno | 2011-10-30 01:28:14 +0200

  • Change pb_ve_launch interface (remove useless second parameter)
  • Force call to pb_ve_launch with pbforce to always create the chroot in pbmkbm
File size: 8.9 KB
RevLine 
[1341]1#!/usr/bin/perl -w
2#
3# pbmkbm, a project-builder.org utility to make boot media
4#
5# $Id$
6#
7# Copyright B. Cornec 2011
8# Provided under the GPL v2
9
10# Syntax: see at end
11
12use strict 'vars';
13use Getopt::Long qw(:config auto_abbrev no_ignore_case);
14use Data::Dumper;
15use English;
16use File::Basename;
17use File::Copy;
18use ProjectBuilder::Version;
19use ProjectBuilder::Base;
20use ProjectBuilder::Env;
21use ProjectBuilder::Conf;
22use ProjectBuilder::Distribution;
[1348]23use ProjectBuilder::VE;
[1341]24
25# Global variables
26my %opts; # CLI Options
27
28=pod
29
30=head1 NAME
31
32pbmkbm - a project-builder.org utility to make boot media
33
34=head1 DESCRIPTION
35
36pbmkbm creates a bootable media (CD/DVD, USB device, Network, tape, ...)
37with a minimal distribution in it, suited for building packages for example.
38It aims at supporting all distributions supported by project-builder.org
39(RHEL, RH, Fedora, OpeSUSE, SLES, Mandriva, ...)
40
41It is inspired by work done by Jean-Marc André around the HP SSSTK and
42aim at replacing the mindi project (http://www.mondorescue.org), but
43fully integrated with project-builder.org
44
45pbmkbm works in different phases. The first one is to check all
46
47pbmkbm needs to gather a certain number of components that could come
48from various sources and could be put on a different target media.
49We need a kernel, an initrd/initramfs for additional modules and init script,
50a root filesystem and a boot configuration file.
51Kernel, modules could come either from the local installed system
52(typically for disaster recovery context) or from a kernel package of a
[1348]53given configuration or a referenced content.
[1341]54Utilities could come from busybox, local utilities or set of packages.
55The root filesystem is made with them.
56The initrd/initramfs could be made internaly or by calling dracut.
[1348]57The boot config file is generated from analysis content or provided externally.
[1341]58
59=head1 SYNOPSIS
60
[1348]61pbmkbm [-vhq][-t boot-type [-d device]][-b boot-method][-m os-ver-arch]
[1341]62[-s script][-a pkg1[,pkg2,...]] [target-dir]
63
[1348]64pbmkbm [--verbose][--help][--man][--quiet][--type boot-type [--device device]]
65[--machine os-ver-arch][--boot boot-method]
66[--script script][--add pkg1,[pkg2,...]] [target-dir]
[1341]67
68=head1 OPTIONS
69
70=over 4
71
72=item B<-v|--verbose>
73
74Print a brief help message and exits.
75
76=item B<-h|--help>
77
78Print a brief help message and exits.
79
80=item B<--man>
81
82Prints the manual page and exits.
83
84=item B<-q|--quiet>
85
86Do not print any output.
87
[1348]88=item B<-t|--type boot-type>
89
90Type of the boot device to generate. A boot-type can be:
91
92=over 4
93
94=item B<iso>
95
96Generate an ISO9660 image format (suitable to be burned later on or loopback mounted. Uses isolinux.
97
98=item B<usb>
99
100Generate a USB image format (typically a key of external hard drive). Uses syslinux.
101
102=item B<pxe>
103
104Generate a PXE environement (suitable to be integrated in a PXElinux configuration). Uses pxelinux.
105
106=back
107
108=item B<-d|--device device-file>
109
110Name of the device or file on which you want to create the boot media.
111
112=item B<-b|--boot boot-method>
113
114This is the boot method to use to create the boot media. A boot-method can be:
115
116=over 4
117
118=item B<native>
119
120Use the tools of the native distribution to create the boot media. No other dependency.
121
122=item B<ve>
123
124Use the project-builder.org virtual environment notion to create the boot media. No other dependency outside of the project.
125
126=item B<busybox>
127
128Use the busybox tool to create the boot media. Cf: L<http://www.busybox.net>
129
130=item B<dracut>
131
132Use the dracut tool to create the boot media. Cf: L<http://www.dracut.net>
133
134=back
135
[1341]136=item B<-s|--script script>
137
[1348]138Name of the script you want to execute on the related boot media at the end o the build.
[1341]139
140=item B<-a|--add pkg1[,pkg2,...]>
141
[1348]142Additional packages to add from the distribution you want to install on the related boot media
143at the end of the build.
[1341]144
[1348]145=item B<-m|--machine os-ver-arch>
146
147This is the target tuple operating system-version-architecture for which you want to create the boot media.
148
[1341]149=back
150
151=head1 ARGUMENTS
152
[1348]153target-dir is the directory under which the boot media will be build.
154
[1341]155=over 4
156
157=back
158
159=head1 EXAMPLE
160
[1348]161To setup a USB busybox based boot media on the /dev/sdb device for a Fedora 12 distribution with an i386 architecture issue:
[1341]162
[1348]163pbmkbm -t usb -d /dev/sdb -m fedora-12-i386 -b busybox
[1341]164
[1348]165To setup an ISO image under /tmp for a RHEL 6 x86_64 distribution issue using the native environment:
166
167pbmkbm -t iso -d /tmp -m rhel-6-x86_64 -b ve
168
[1341]169=head1 WEB SITES
170
171The main Web site of the project is available at L<http://www.project-builder.org/>.
172Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
173
174=head1 USER MAILING LIST
175
176Cf: L<http://www.mondorescue.org/sympa/info/pb-announce> for announces and
177L<http://www.mondorescue.org/sympa/info/pb-devel> for the development of the pb project.
178
179=head1 CONFIGURATION FILE
180
181Uses Project-Builder.org configuration file (/etc/pb/pb.conf or /usr/local/etc/pb/pb.conf)
182
183=head1 AUTHORS
184
185The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
186
187=head1 COPYRIGHT
188
189Project-Builder.org is distributed under the GPL v2.0 license
190described in the file C<COPYING> included with the distribution.
191
192=cut
193
194# ---------------------------------------------------------------------------
195
196my ($projectbuilderver,$projectbuilderrev) = pb_version_init();
197my $appname = "pbmkbm";
198$ENV{'PBPROJ'} = $appname;
199
200# Initialize the syntax string
201
202pb_syntax_init("$appname Version $projectbuilderver-$projectbuilderrev\n");
203pb_temp_init();
204
205GetOptions("help|?|h" => \$opts{'h'},
[1348]206 "man" => \$opts{'man'},
[1341]207 "verbose|v+" => \$opts{'v'},
208 "quiet|q" => \$opts{'q'},
209 "log-files|l=s" => \$opts{'l'},
210 "script|s=s" => \$opts{'s'},
211 "machine|m=s" => \$opts{'m'},
212 "add|a=s" => \$opts{'a'},
[1348]213 "device|d=s" => \$opts{'d'},
214 "type|t=s" => \$opts{'t'},
215 "boot|b=s" => \$opts{'b'},
[1341]216 "version|V=s" => \$opts{'V'},
217) || pb_syntax(-1,0);
218
219if (defined $opts{'h'}) {
220 pb_syntax(0,1);
221}
222if (defined $opts{'man'}) {
223 pb_syntax(0,2);
224}
225if (defined $opts{'v'}) {
226 $pbdebug = $opts{'v'};
227}
228if (defined $opts{'q'}) {
229 $pbdebug=-1;
230}
231if (defined $opts{'l'}) {
232 open(pbLOG,"> $opts{'l'}") || die "Unable to log to $opts{'l'}: $!";
233 $pbLOG = \*pbLOG;
234 $pbdebug = 0 if ($pbdebug == -1);
235}
236pb_log_init($pbdebug, $pbLOG);
237
238# Get VE name
239$ENV{'PBV'} = $opts{'m'};
240
241#
242# Initialize distribution info from pb conf file
243#
244my $pbos = pb_distro_get_context($ENV{'PBV'});
[1348]245pb_log(0,"Starting boot media build for $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}\n");
[1341]246
[1348]247pb_env_init_pbrc(); # to get content of HOME/.pbrc
248
[1341]249#
250# Check target dir
251# Create if not existent and use default if none given
252#
[1348]253my $targetdir = shift @ARGV;
[1341]254
255#
256# Check for command requirements
257#
258my ($req,$opt) = pb_conf_get_if("mkbmcmd","mkbmcmdopt");
259pb_check_requirements($req,$opt,$appname);
260
[1348]261# After that we will need root access
262die "$appname needs to be run as root" if ($EFFECTIVE_USER_ID != 0);
263
264#
265# Where is our build target directory
266#
267
268if (not defined $targetdir) {
269 $targetdir = "/var/cache/pbmkbm";
270 my ($vestdpath) = pb_conf_get("mkbmpath");
271 $targetdir = "$vestdpath->{'default'}/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}" if (defined $vestdpath->{'default'});
[1341]272}
273
[1348]274die pb_log(0,"No target-dir specified and no default mkbmpath found in $ENV{'PBETC'}\n") if (not defined $targetdir);
[1341]275
[1348]276# Point to the right subdir and create it if needed
277pb_mkdir_p($targetdir) if (! -d $targetdir);
[1341]278
[1348]279
280# Now the preparation is over, we need to do something useful :-)
281# But it all depends on how we're asked to do it.
[1341]282#
[1348]283# First we need to copy into the target dir all the relevant content
284pb_mkbm_create_content();
285
286# Then we need to package this content in the destination format
287pb_mkbm_create_media();
288
289sub pb_mkbm_create_content {
290
291pb_log(1,"Creating boot media content\n");
[1350]292
293# If not defined use VE mode by default
294$opts{'b'} = "ve" if (not defined $opts{'b'});
295
[1348]296if ($opts{'b'} eq "ve") {
297 # Use existing tools to create a good VE !
[1352]298 pb_ve_launch($ENV{'PBV'},1);
[1348]299} elsif ($opts{'b'} eq "native") {
300 # Use native tools to create a good VE !
301} else {
302 die "Unknown method $opts{'b'} used to create the media content";
303}
[1352]304
305# Once the environment is made, add what is needed for this boot media to it.
[1348]306}
307
308sub pb_mkbm_create_media {
309
310}
311
[1341]312# Get the package list to download, store them in a cache directory
313#
[1352]314#my ($mkbmcachedir) = pb_conf_get_if("mkbmcachedir");
315#my ($pkgs) = pb_distro_get_param($pbos,pb_conf_get("mkbmmindep"));
[1341]316
317#
318# /proc needed
319#
[1352]320#pb_system("mount -o bind /proc $targetdir/proc","Mounting /proc");
[1341]321
322# Installed additional packages we were asked to
[1352]323#if (defined $opts{'a'}) {
324#$opts{'a'} =~ s/,/ /g;
325#pb_system("chroot $targetdir /bin/bash -c \"$pbos->{'install'} $opts{'a'} \"","Adding packages to OS by running $pbos->{'install'} $opts{'a'}");
326#}
[1341]327
328#
329# Clean up
330#
[1352]331#pb_log(1,"Cleaning up\n");
332#pb_system("umount $targetdir/proc","Unmounting /proc");
[1341]333
334# Executes post-install step if asked for
[1352]335#if ($opts{'s'}) {
336#pb_system("$opts{'s'} $targetdir","Executing the post-install script: $opts{'s'} $targetdir");
337#}
Note: See TracBrowser for help on using the repository browser.