File: buildimage

package info (click to toggle)
systeminstaller 1.04-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 748 kB
  • ctags: 261
  • sloc: perl: 5,769; makefile: 70
file content (507 lines) | stat: -rwxr-xr-x 14,121 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/usr/bin/perl


#   $Header: /cvsroot/systeminstaller/systeminstaller/bin/buildimage,v 1.48 2003/04/11 20:44:27 mchasal Exp $

#   Copyright (c) 2001 International Business Machines

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#   Michael Chase-Salerno (mchasal@users.sourceforge.net)

use strict;
use POSIX;
use Carp;
use vars qw($VERSION $config);
use lib "/usr/lib/systeminstaller", "/usr/local/lib/systemimager/perl", "/usr/lib/systemimager/perl";
use SystemInstaller::Env;
use SystemInstaller::Passwd qw(update_user);
use SystemInstaller::Image qw(find_distro split_version);
 
$VERSION = sprintf("%d.%02d", q$Revision: 1.48 $ =~ /(\d+)\.(\d+)/); 

#Set the path
$ENV{PATH}=$config->binpath .":" . $ENV{PATH};

# Image building options
my %imgopts = ();

# Addclients options
my %cliopts = ();

my $distro;
my $version;

# All known architectures
my %KNOWNARCH;
	$KNOWNARCH{i386}=1;
	$KNOWNARCH{i486}=1;
	$KNOWNARCH{i586}=1;
	$KNOWNARCH{i686}=1;
	$KNOWNARCH{ppc}=1;
	$KNOWNARCH{alpha}=1;
	$KNOWNARCH{ia64}=1;

# The default architecture, grokked from uname
my $ARCH = (uname)[4];
my $HOST = (uname)[1];
my ($junk,$DOM)  = split(/\./,$HOST,2);

my %IPMETH;
	$IPMETH{dhcp}="IP addresses assigned by DHCP, a different address may be assigned each boot.\n";
	$IPMETH{static}="IP addresses manually set.\n";
	$IPMETH{replicant}="Don't set network address, used for backup.\n";

my %PIACTION;
	$PIACTION{beep}="Beep incessantly after completion of an install.\n";
	$PIACTION{reboot}="Reboot after completion of an install.\n";
	$PIACTION{shutdown}="Halt after completion of an install.\n";

my %DISKTYPE;
	$DISKTYPE{scsi}="SCSI disks(/dev/sdx).\n";
	$DISKTYPE{ide}="IDE disks(/dev/hdx).\n";

my $INPUTNOK=1;

#
#Gather image information
#

# Get the image name
while ($INPUTNOK) {
	print "\nEnter a name for the image, this will be used in the path for the image.\n";
	$imgopts{name} = promptedRead("name");
	# Check for bad input
	if ($imgopts{name} =~ /\//) {
		print "Error, image name cannot contain '/' characters.\n\n";
	} elsif (&image_exists($imgopts{name})) {
		print "Error, image named $imgopts{name} already exists.\n\n";
	} else {
		$INPUTNOK=0;
	}
}
$INPUTNOK=1;

#Get the path to the image
while ($INPUTNOK) {
	print "\nEnter a path for the image, the image name will be appended to this path.\n";
	$imgopts{imgpath} = promptedRead("path", 0, $config->DEFAULT_IMAGE_DIR);
	$imgopts{imgpath} = $imgopts{imgpath} . "/" . $imgopts{name};
	# Not sure what to check here, permissions,mkdir?
		$INPUTNOK=0;
}
$INPUTNOK=1;

#Get the architecture
while ($INPUTNOK) {
	print "\nEnter the architecture that the image will be used for,\n";
	$imgopts{arch} = promptedRead("architecture", 0, $ARCH);
	if (! defined $KNOWNARCH{$imgopts{arch}} ) {
		print "Error, architecture $imgopts{arch} is not known.\n\n";
	} else {
		$INPUTNOK=0;
	}
}
$INPUTNOK=1;

# Get the location of the packages
while ($INPUTNOK) {
	print "\nEnter the path where the installation packages are stored,\n";
	$imgopts{pkgpath} = promptedRead("package path",0,$config->pkgpath);
        if (! -e $imgopts{pkgpath}  ) {
		print "Error, path $imgopts{pkgpath} does not exist.\n\n";
	} else {
		$INPUTNOK=0;
	}
}
$INPUTNOK=1;

# Try to determine the distro from the directory
print "\n";
($distro,$version)=&find_distro($imgopts{pkgpath});
my ($maj,$min)=&split_version($version);
if ($distro eq "") {
	print "Unable to determine distribution from package directory.\n";
	print "Please enter a file name for the package list.\n";
	while ($INPUTNOK) {
		$imgopts{pkgfile} = promptedRead("package file name");
		if (! -e $imgopts{pkgfile}  ) {
			print "Error, file $imgopts{pkgfile} does not exist.\n\n";
		} else {
			$INPUTNOK=0;
		}
	}
} else {
	print "The path $imgopts{pkgpath} appears to contain $distro version $version.\n";
	print "The following predefined package lists are available:\n";
	print "-----------------------------------------------------\n";
	my $pkgpath= $config->distinfo . "/" . $distro . "/" . $maj . "/" . $min;
	my @pkgfiles=glob("$pkgpath/*.pkg");
	my @pkgfilenames=@pkgfiles;
	my $file;
	my $i=1;
	foreach $file (@pkgfiles) {
		$file=~s/.*\///;
		$file=~s/\.pkg$//;
		print "\t$i.\t$file\n";
		$i++;
	}
	print "\nEnter the number of the desired package file\n";
	print "\tor enter a full filename for a different file,\n";
	my $imgselect=0;
	while ($INPUTNOK) {
		$imgopts{pkgfile} = promptedRead("package file number or name");
		if ($imgopts{pkgfile} =~ /^[1-9][0-9]*$/ ) {
			$imgopts{pkgfile}=@pkgfilenames[$imgopts{pkgfile}-1];
			$imgselect=1;
		}
		if (($imgselect ) && ($imgopts{pkgfile} eq "")){
			print "Error, selection not valid.\n\n";
		} elsif (! -e $imgopts{pkgfile}  ) {
			print "Error, file $imgopts{pkgfile} does not exist.\n\n";
		} else {
			$INPUTNOK=0;
		}
	}
}
$INPUTNOK=1;

# Get IP assignment method
print "\nEnter the ip assignment method of the image. Valid methods are:\n";
my $meth;
foreach $meth (keys(%IPMETH)){
	print "\t$meth\t$IPMETH{$meth}";
}
while ($INPUTNOK) {
	$imgopts{ipmeth} = promptedRead("method", 0, $config->ipmeth);
	if (! defined $IPMETH{$imgopts{ipmeth}}) {
		print "Valid IP assignment methods are ", join(",",keys(%IPMETH)),".\n";
	} else {
		$INPUTNOK=0;
	}
}
$INPUTNOK=1;

# Get the post install action.
my $act;
print "\nEnter the post install action of the image. Valid actions are:\n";
foreach $act (keys(%PIACTION)){
	print "\t$act\t$PIACTION{$act}";
}
while ($INPUTNOK) {
	$imgopts{piact} = promptedRead("action", 0, $config->piaction);
	if (! defined $PIACTION{$imgopts{piact}}) {
		print "Valid post install actions are ", join(",",keys(%PIACTION)),".\n";
	} else {
		$INPUTNOK=0;
	}
}
$INPUTNOK=1;

#Set image root password
print "\nEnter a root password for the image,\n";
my ($rootpw,$rootpwv) = (undef,1);
my $sttyset=`stty -g`;
system("stty -echo");
while ($INPUTNOK) {
    $rootpw = promptedRead("password",1);
    print "\n";
    $rootpwv =  promptedRead("password again to verify",1);
    print "\n";
    if($rootpw ne $rootpwv) {
		 print "Error, your passwords don't match, please try again...\n";
    } else {
		$INPUTNOK=0;
    }
}
system("stty $sttyset");
$INPUTNOK=1;

#Gather partition info
print "\nDefining disk partitioning...\n";

my $partitionfile;
print "\nEnter the disk partition file name \n";
$INPUTNOK = 1;
while ($INPUTNOK) { 
        $partitionfile = promptedRead("file name",0,"/usr/share/doc/systeminstaller-".&get_version()."/disktable"); 
	if (! -e $partitionfile) {
		print "Error, file $partitionfile does not exist.\n\n";
	} else {
		$INPUTNOK=0;
	}
}

my $disktype;
print "\nEnter the disk type if you wish to override the type in the file. \n";
$INPUTNOK = 1;
while ($INPUTNOK) { 
        $disktype = promptedRead("disk type",1); 
        if ($disktype && (! defined $DISKTYPE{$disktype})) {
                print "Valid disk types are ", join(",",keys(%DISKTYPE)),".\n";
        } else {
                $INPUTNOK=0;
        }
}
if ($disktype) {
        $disktype="--type ".$disktype;
}

#Build image
my $CMD;
print "Now building the image, issuing command .....\n";
$CMD=$config->siimage . " --Add --host $HOST --name $imgopts{name} --path $imgopts{imgpath} --filename $imgopts{pkgfile} --location $imgopts{pkgpath} --arch $imgopts{arch}";
print "$CMD\n";
my $RC=system("$CMD");
if ($RC != 0 ) {
	$RC=$RC>>8;
        carp("Failed to build image $imgopts{name}.  Cleaning up now...\n");
	system($config->siimage . " --Delete --name $imgopts{name}");
        croak("Cleaned up image $imgopts{name}");
}

print "Setting root password in image.....\n";

update_user(
            imagepath => $imgopts{imgpath},
            user => 'root',
            password => $rootpw
           );

#Build partition table
print "\n\nGenerating partition files, issuing command.....\n";
	$CMD=$config->sidisk . " --Add --name $imgopts{name} --file $partitionfile $disktype";
print "$CMD\n";
my $RC=system("$CMD");
if ($RC != 0 ) {
	$RC=$RC>>8;
	croak("Command failed with RC=$RC, exiting...\n");
}

#Call mkautoinstallscript
print "\n\nGenerating autoinstall script, issuing command.....\n";
$CMD=$config->mkaiscript . " -quiet -image $imgopts{name} -ip-assignment $imgopts{ipmeth} -post-install $imgopts{piact}";
print "$CMD\n";
my $RC=system("$CMD");
if ($RC != 0 ) {
	$RC=$RC>>8;
	croak("Command failed with RC=$RC, exiting...\n");
}

print "\n\nWould you like to create clients to use this image?\n";
$INPUTNOK=1;
my $choice;
while ($INPUTNOK) {
	$choice = promptedRead("y/n", 0, "y");
	$choice=lc($choice);
	if (($choice =~ /^y/) || ($choice =~ /^n/)) {
		$INPUTNOK=0;
    	} 
}
$INPUTNOK=1;
if ($choice =~ /^n/){
	print "Thanks for playing....\n";
	exit 0;
} 

#Gather client information
print "\n\nEnter the basename for the clients, this will be combined with the\n";
print "the suffix number to derive the actual hostnames.\n";
$INPUTNOK=1;
while ($INPUTNOK) {
	$cliopts{basename} = promptedRead("basename");
	if ($cliopts{basename} =~ /\./) {
		print "Error, the basename cannot contain '.'.\n";
	} else {
		$INPUTNOK=0;
	}
}
$INPUTNOK=1;

print "\n\nEnter the number of clients to create.\n";
$INPUTNOK=1;
while ($INPUTNOK) {
    $cliopts{count} = promptedRead("count");
    if ( $cliopts{count} !~ /^[0-9]+$/ ) {
        carp "Error, the count must be a number.\n";
    } else {
        $INPUTNOK=0;
    }
}
$INPUTNOK=1;

print "\n\nEnter the starting number for the suffix of the clients, this number\n";
print "will be combined with the hostname stub to derive actual hostnames.\n";
print "for example a hostname stub of 'www' and a suffix of 3 will name the clients\n";
print "thusly 'www3,www4,...'. This number will be incremented for each client.\n";
$INPUTNOK=1;
while ($INPUTNOK) {
    $cliopts{start} = promptedRead("starting suffix");
    if ( $cliopts{start} !~ /^[0-9]+$/ ) {
        carp "Error, the suffix must be a number.\n";
    } else {
        $INPUTNOK=0;
    }
}
$INPUTNOK=1;


print "\n\nEnter the ip address for the first client.\n";
print "This ip address will be incremented for each client.\n";
while ($INPUTNOK) {
    $cliopts{ipstart} = promptedRead("IP address");
    if ( $cliopts{ipstart} !~ /^[0-9\.]+$/ ) {
        carp "Error, the range must be of the form 'a.b.c.d'.\n";
    } else {
        $INPUTNOK=0;
    }
}
$INPUTNOK=1;
print "\n\nEnter the ip netmask for the clients.\n";
while ($INPUTNOK) {
    $cliopts{netmask} = promptedRead("IP netmask",0,"255.255.255.0");
    if ( $cliopts{netmask} !~ /^[0-9\.]+$/ ) {
        carp "Error, the netmask must be of the form 'a.b.c.d'.\n";
    } else {
        $INPUTNOK=0;
    }
}
$INPUTNOK=1;
print "\n\nEnter the default gateway for the clients.\n";
while ($INPUTNOK) {
    $cliopts{gateway} = promptedRead("Gateway",1);
    if ( $cliopts{gateway} !~ /^[0-9\.]+$/ ) {
        carp "Error, the gateway must be of the form 'a.b.c.d'.\n";
    } else {
        $INPUTNOK=0;
    }
}
$INPUTNOK=1;

my $gateway = "";
if ($cliopts{gateway}) {
	$gateway="--gateway $cliopts{gateway}";
} 


print "\n\nEnter the domain for the clients,\n";
$cliopts{domain} = promptedRead("domain",1);

my $domain = "";
if ($cliopts{domain}) {
	$domain="--domain $cliopts{domain}";
} 


#Call addclients
print "Now creating clients.....\n";
$CMD= $config->sirange." --basename $cliopts{basename} --start $cliopts{start} --count $cliopts{count} --ipstart $cliopts{ipstart} $domain --image $imgopts{name} --netmask $cliopts{netmask} $gateway";
print "$CMD\n";
my $RC=system("$CMD");
if ($RC != 0 ) {
	$RC=$RC>>8;
	croak("Command failed with RC=$RC, exiting...\n");
}

exit 0;

## SUBROUTINES

#
# prompts user for input (using optional/default replies)
#
sub promptedRead {
    my ($prompt, $optional, $default) = @_;
    local $_;
    print "Enter $prompt ";
    print "[ $default ] " if $default;
    print ": ";
    while (<>) {
        chomp;
        s/\s+//g;
        $_ = $default if defined($default) and $_ eq '';
        last if $_ ne '' or $optional;
        print "Enter $prompt ";
        print "[ $default ] " if $default;
        print ": ";
    }
    return $_;
}


sub execute {
    my ($cmd, @params) = @_;
    my @output;
    open(PIPE, "$cmd @params |") or die "could not execute $cmd: $!\n";
    local $_;
    while (<PIPE>) {
        chomp;
        push @output, $_;
    }
    close PIPE;
    return @output;
}

sub image_exists {
	my $name=shift;
	my $CMD=$config->siimage . " -L";
	my @IMAGES=`$CMD`;
	foreach (@IMAGES) {
		my ($iname)=split;
		$iname=~s/NAME=//;
		if ($iname eq $name) {
			return 1;
		}
	}
}

## POD
 
__END__
 
=head1 NAME
 
buildimage - Interactive, text based interface to SystemInstaller.
 
=head1 SYNOPSIS
 
  buildimage
 
=head1 DESCRIPTION
 
The buildimage command is used to create images and clients for SIS.
 
=head2 Syntax
 
buildimage 
 
=head1 NOTES
 
There are no options for buildimage. It will interactively ask questions and
call the required commands to build the image and the clients for the image. In
the process it will ask you for a disk partition file and a package file. You
can find an example of a disk partition file in
/usr/share/doc/systeminstaller-[version]/disktable. The man page for mksidisk
has more information about this. Package files for various distros can be found
in /usr/share/systeminstaller/distinfo. The man page for mksiimage has more
information about package files.
 
=head1 AUTHOR
 
Michael Chase-Salerno <mchasal@users.sourceforge.net>
 
=head1 SEE ALSO
 
perl(1), mksidisk(1), mksiimage(1), SIS(3).