/usr/bin/xt-install-image is in xen-tools 4.2.1-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | #!/usr/bin/perl -w
=head1 NAME
xt-install-image - Install a fresh copy of GNU/Linux into a directory
=cut
=head1 SYNOPSIS
xt-install-image [options]
Help Options:
--help Show this scripts help information.
--manual Read this scripts manual.
--version Show the version number and exit.
Debugging Options:
--verbose Be verbose in our execution.
Mandatory Options:
--location The location to use for the new installation
--dist The name of the distribution which has been installed.
Misc Options:
--arch Pass the given arch setting to debootstrap or rpmstrap.
--config Read the specified config file in addition to the global
configuration file.
--mirror The mirror to use when installing with 'debootstrap'.
Installation Options:
--install-method Specify the installation method to use.
--install-source Specify the installation source to use.
--debootstrap-cmd Specify which debootstrap command to
use. Defaults to debootstrap if both, debootstrap
and cdebootstrap are installed.
All other options from xen-create-image will be passed as environmental
variables.
=cut
=head1 NOTES
This script is invoked by xen-create-image after to create a new
distribution of Linux. Once the script has been created the companion
script xt-customize-image will be invoked to perform the network
configuration, etc.
=cut
=head1 INSTALLATION METHODS
There are several available methods of installation, depending upon the
users choice. Only one option may be chosen at any given time.
The methods available are:
=over 8
=item B<debootstrap>
Install the distribution specified by the B<--dist> argument using the debootstrap. If you use this option you must specify a mirror with B<--mirror>.
=item B<copy>
Copy the given directory recursively. This local directory is assumed to contain a complete installation. Specify the directory to copy with the B<--install-source> argument.
=item B<rinse>
Install the distribution specified by B<--dist> using the rinse command.
=item B<rpmstrap>
Install the distribution specified by B<--dist> using the rpmstrap command.
=item B<tar>
Untar a .tar file into the new installation location. This tarfile is assumed to contain a complete archived system. Specify the directory to copy with the B<--install-source> argument.
=back
=cut
=head1 AUTHORS
Steve Kemp, http://www.steve.org.uk/
Axel Beckert, http://noone.org/abe/
Dmitry Nedospasov, http://nedos.net/
Stéphane Jourdois
=cut
=head1 LICENSE
Copyright (c) 2005-2009 by Steve Kemp, (c) 2010 by The Xen-Tools
Development Team. All rights reserved.
This module is free software;
you can redistribute it and/or modify it under
the same terms as Perl itself.
The LICENSE file contains the full text of the license.
=cut
use strict;
use Env;
use File::Copy;
use Getopt::Long;
use Pod::Usage;
#
# Configuration values read from the command line.
#
my %CONFIG;
#
# Release number.
#
my $RELEASE = '4.2.1';
#
# Dispatch table mapping installation types to their names.
#
# The names are the methods, and the hash keys are:
#
# sub The routine to execute.
# needBinary If set the name of an executable we need to exist.
# needFile Defined if we need an install-source file specified.
# needDirectory Defined if we need an install-source directory specified.
#
#
my %dispatch = (
"copy" => { sub => \&do_copy,
needBinary => "/bin/cp",
needDirectory => 1,
},
"debootstrap" => { sub => \&do_debootstrap,
needBinary => "/usr/sbin/debootstrap",
},
"rinse" => { sub => \&do_rinse,
needBinary => "/usr/sbin/rinse",
},
"rpmstrap" => { sub => \&do_rpmstrap,
needBinary => "/usr/bin/rpmstrap",
},
"tar" => { sub => \&do_tar,
needBinary => "/bin/tar",
needFile => 1,
} );
#
# Read the global configuration file.
#
readConfigurationFile("/etc/xen-tools/xen-tools.conf");
#
# Parse the command line arguments.
#
parseCommandLineArguments();
#
# If we received a configuration file then read it.
#
if ( $CONFIG{ 'config' } )
{
my $path = $CONFIG{ 'config' };
# If not fully-qualified then read from /etc/xen-tools.
if ( $path !~ /^[\/]/ )
{
$path = "/etc/xen-tools/" . $path;
}
# Read the file, if it exists.
readConfigurationFile($path) if ( -e $path );
}
#
# Check our arguments
#
checkArguments();
#
# Now lookup our installation type and dispatch control to it.
#
if ( defined( $CONFIG{ 'install-method' } ) &&
length( $CONFIG{ 'install-method' } ) )
{
#
# Get the entry from the dispatch table.
#
my $installer = $dispatch{ lc( $CONFIG{ 'install-method' } ) };
if ( defined($installer) )
{
#
# If we found it.
#
# Do we need to test for a binary.
if ( ( $installer->{ 'needBinary' } ) &&
( !-x $installer->{ 'needBinary' } ) )
{
print
"The following required binary for the installation was not found\n";
print "\t" . $installer->{ 'needBinary' } . "\n";
exit 1;
}
# Do we need a directory specified as the installation source?
if ( ( $installer->{ 'needDirectory' } ) &&
( !$CONFIG{ 'install-source' } || !-d $CONFIG{ 'install-source' } )
)
{
print "Please specify the source directory with --install-source\n";
if ( $CONFIG{ 'install-source' } )
{
print
"The specified directory $CONFIG{'install-source'} does not exist.\n";
}
exit 1;
}
# Do we need a file specified as the installation source?
if ( ( $installer->{ 'needFile' } ) &&
( !$CONFIG{ 'install-source' } || !-e $CONFIG{ 'install-source' } )
)
{
print "Please specify the source file with --install-source\n";
if ( $CONFIG{ 'install-source' } )
{
print
"The specified file $CONFIG{'install-source'} does not exist.\n";
}
exit 1;
}
#
# Now we can call the appropriate handler.
#
$installer->{ 'sub' }->();
#
# Did the operation succeed?
#
# Test that we have some "standard" files present.
#
checkForCommonFilesInChroot($CONFIG{ 'location' },
"installed system");
#
# All done.
#
exit 0;
}
else
{
print "The installation method specified is invalid.\n";
exit 1;
}
}
else
{
print "An installation method is mandatory\n";
exit 1;
}
=begin doc
Read the specified configuration file, and update our global configuration
hash with the values found in it.
=end doc
=cut
sub readConfigurationFile
{
my ($file) = (@_);
# Don't read the file if it doesn't exist.
return if ( !-e $file );
my $line = "";
open( FILE, "<", $file ) or die "Cannot read file '$file' - $!";
while ( defined( $line = <FILE> ) )
{
chomp $line;
if ( $line =~ s/\\$// )
{
$line .= <FILE>;
redo unless eof(FILE);
}
# Skip lines beginning with comments
next if ( $line =~ /^([ \t]*)\#/ );
# Skip blank lines
next if ( length($line) < 1 );
# Strip trailing comments.
if ( $line =~ /(.*)\#(.*)/ )
{
$line = $1;
}
# Find variable settings
if ( $line =~ /([^=]+)=([^\n]+)/ )
{
my $key = $1;
my $val = $2;
# Strip leading and trailing whitespace.
$key =~ s/^\s+//;
$key =~ s/\s+$//;
$val =~ s/^\s+//;
$val =~ s/\s+$//;
# command expansion?
if ( $val =~ /(.*)`([^`]+)`(.*)/ )
{
# store
my $pre = $1;
my $cmd = $2;
my $post = $3;
# get output
my $output = `$cmd`;
chomp($output);
# build up replacement.
$val = $pre . $output . $post;
}
# Store value.
$CONFIG{ $key } = $val;
}
}
close(FILE);
}
=begin doc
Parse the command line arguments this script was given.
=end doc
=cut
sub parseCommandLineArguments
{
my $HELP = 0;
my $MANUAL = 0;
my $VERSION = 0;
#
# Parse options.
#
GetOptions(
# Mandatory
"location=s", \$CONFIG{ 'location' },
"dist=s", \$CONFIG{ 'dist' },
"hostname=s", \$CONFIG{ 'hostname' },
# Installation method
"install-method=s", \$CONFIG{ 'install-method' },
"install-source=s", \$CONFIG{ 'install-source' },
"debootstrap-cmd=s", \$CONFIG{ 'debootstrap-cmd' },
# Misc
"arch=s", \$CONFIG{ 'arch' },
"cache=s", \$CONFIG{ 'cache' },
"cachedir=s", \$CONFIG{ 'cachedir' },
"config=s", \$CONFIG{ 'config' },
"mirror=s", \$CONFIG{ 'mirror' },
# Help.
"verbose", \$CONFIG{ 'verbose' },
"help", \$HELP,
"manual", \$MANUAL,
"version", \$VERSION
);
pod2usage(1) if $HELP;
pod2usage( -verbose => 2 ) if $MANUAL;
if ($VERSION)
{
my $REVISION = '$Revision: 1.65 $';
if ( $REVISION =~ /1.([0-9.]+) / )
{
$REVISION = $1;
}
print "xt-install-image release $RELEASE - CVS: $REVISION\n";
exit;
}
}
=begin doc
Test that the command line arguments we were given make sense.
=end doc
=cut
sub checkArguments
{
#
# We require a location.
#
if ( !defined( $CONFIG{ 'location' } ) )
{
print "The '--location' argument is mandatory\n";
exit 1;
}
#
# Test that the location we've been given exists
#
if ( !-d $CONFIG{ 'location' } )
{
print "The installation directory we've been given doesn't exist\n";
print "We tried to use : $CONFIG{'location'}\n";
exit 1;
}
#
# We require a distribution name.
#
if ( !defined( $CONFIG{ 'dist' } ) )
{
print "The '--dist' argument is mandatory\n";
exit 1;
}
#
# Test that the distribution name we've been given
# to configure has a collection of hook scripts.
#
# If there are no scripts then we clearly cannot
# customise it!
#
my $dir = "/usr/lib/xen-tools/" . $CONFIG{ 'dist' } . ".d";
if ( !-d $dir )
{
print <<E_OR;
We are trying to configure an installation of $CONFIG{'dist'} in
$CONFIG{'location'} - but there is no hook directory for us to use.
This means we would not know how to configure this installation.
We would expect the hook directory to be $dir.
Aborting.
E_OR
exit 1;
}
#
# Test that we received a valid installation type.
#
my $valid = 0;
if ( defined( $CONFIG{ 'install-method' } ) )
{
foreach my $recognised ( keys %dispatch )
{
$valid = 1
if ( lc( $CONFIG{ 'install-method' } ) eq lc($recognised) );
}
}
else
{
$valid = 1;
}
if ( !$valid )
{
print <<EOF;
Please specify the installation method to use.
For example:
--install-method=copy --install-source=/some/path
--install-method=debootstrap
--install-method=rpmstrap
--install-method=tar --install-source=/some/file.tar
EOF
exit;
}
}
=begin doc
Check if there are some common files in some chroot
=end doc
=cut
sub checkForCommonFilesInChroot {
my ($chroot, $what) = @_;
foreach my $file (qw( /bin/ls /bin/cp ))
{
if ( !-x $chroot.$file )
{
print STDERR <<EOT;
WARNING ($0): The $what at $chroot doesn\'t seem to be a full system.
WARNING ($0): The $what is missing the common file: $file.
EOT
}
}
}
=begin doc
A utility method to run a system command. We will capture the return
value and exit if the command fails.
When running verbosely we will also display any command output.
=end doc
=cut
sub runCommand
{
my ($cmd) = (@_);
#
# Command start.
#
$CONFIG{ 'verbose' } && print "Executing : $cmd\n";
#
# Copy stderr to stdout, so we can see it, and make sure we log it.
#
$cmd .= " 2>&1 | tee --append /var/log/xen-tools/$CONFIG{'hostname'}.log";
#
# Run it.
#
my $output = `$cmd`;
if ( $? != 0 )
{
print "Running command '$cmd' failed.\n";
print "Aborting\n";
exit 127;
}
#
# Command finished.
#
$CONFIG{ 'verbose' } && print "Finished : $cmd\n";
return ($output);
}
=begin doc
This function will copy all the .deb files from one directory
to another as a caching operation which will speed up installations via
debootstrap.
=end doc
=cut
sub copyDebFiles
{
my ( $source, $dest ) = (@_);
print "Copying files from $source -> $dest\n";
#
# Loop over only .deb files.
#
foreach my $file ( sort ( glob( $source . "/*.deb" ) ) )
{
my $name = $file;
if ( $name =~ /(.*)\/(.*)/ )
{
$name = $2;
}
#
# Only copy if the file doesn't already exist.
#
if ( !( -e $dest . "/" . $name ) )
{
File::Copy::cp( $file, $dest );
}
}
print "Done\n";
}
###
#
# Installation functions follow.
#
###
=begin doc
Install a new image of a distribution using `cp`.
=end doc
=cut
sub do_copy
{
#
# Check if the copy source has at least some "standard" files present.
#
checkForCommonFilesInChroot($CONFIG{ 'install-source' },
"installation source");
#
# Find the copy command to run from the configuration file.
#
my $cmd = $CONFIG{ 'copy-cmd' };
if ( !defined($cmd) )
{
print "Falling back to default copy command\n";
$cmd = '/bin/cp -a $src/* $dest'; # Note: single quotes.
}
#
# Expand the source and the destination.
#
$cmd =~ s/\$src/$CONFIG{'install-source'}/g;
$cmd =~ s/\$dest/$CONFIG{'location'}/g;
#
# Run the copy command.
#
runCommand($cmd);
}
=begin doc
Install a new image of Debian using 'debootstrap'.
=end doc
=cut
sub do_debootstrap
{
#
# The command is a little configurable - mostly to allow you
# to use cdebootstrap.
#
my $cmd = $CONFIG{ 'debootstrap-cmd' };
my $cachedir = $CONFIG{ 'cachedir' };
if ( !$cmd )
{
if (-x '/usr/sbin/debootstrap') {
$cmd = '/usr/sbin/debootstrap';
} elsif (-x '/usr/sbin/cdebootstrap') {
$cmd = '/usr/sbin/cdebootstrap';
} else {
print STDERR "Found neither debootstrap nor cdebootstrap and no --debootstrap-cmd given\n";
exit 1;
}
print "Using $cmd as debootstrap command\n";
}
#
# Cache from host -> new installation if we've got caching
# enabled.
#
if ( $CONFIG{ 'cache' } eq "yes" )
{
print "\nCopying files from host to image.\n";
unless( -d $cachedir ) {
my $xtcache = '/var/cache/xen-tools/archives/';
print("$cachedir not found, defaulting to $xtcache\n");
unless ( -d $xtcache ) {
system "mkdir -p $xtcache";
}
$cachedir = $xtcache;
}
runCommand("mkdir -p $CONFIG{'location'}/var/cache/apt/archives");
copyDebFiles( "$cachedir",
"$CONFIG{'location'}/var/cache/apt/archives" );
print("Done\n");
}
#
# Propogate --verbose appropriately.
#
my $EXTRA = '';
if ( $CONFIG{ 'verbose' } )
{
$EXTRA = ' --verbose';
}
#
# Propogate the --arch argument
#
if ( $CONFIG{ 'arch' } )
{
$EXTRA .= " --arch $CONFIG{'arch'}";
}
#
# This is the command we'll run
#
my $command =
"$cmd $EXTRA $CONFIG{'dist'} $CONFIG{'location'} $CONFIG{'mirror'}";
#
# Run the command.
#
runCommand($command);
#
# Cache from the new installation -> the host if we've got caching
# enabled.
#
if ( $CONFIG{ 'cache' } eq "yes" )
{
print "\nCopying files from new installation to host.\n";
copyDebFiles( "$CONFIG{'location'}/var/cache/apt/archives",
"$cachedir" );
print("Done\n");
}
}
=begin doc
Install a new distribution of GNU/Linux using the rinse tool.
=end doc
=cut
sub do_rinse
{
#
# The command we're going to run.
#
my $command =
"rinse --distribution=$CONFIG{'dist'} --directory=$CONFIG{'location'}";
#
# Propogate the --arch argument
#
if ( $CONFIG{ 'arch' } )
{
$command .= " --arch $CONFIG{'arch'}";
}
#
# Propogate the verbosity setting.
#
if ( $CONFIG{ 'verbose' } )
{
$command .= " --verbose";
}
runCommand($command);
}
=begin doc
Install a new distribution of GNU/Linux using the rpmstrap tool.
=end doc
=cut
sub do_rpmstrap
{
#
# Propogate the verbosity setting.
#
my $EXTRA = '';
if ( $CONFIG{ 'verbose' } )
{
$EXTRA .= " --verbose";
}
#
# Propogate any arch setting we might have.
#
if ( $CONFIG{ 'arch' } )
{
$EXTRA .= " --arch $CONFIG{'arch'}";
}
#
# Setup mirror if present.
#
my $mirror = "";
$mirror = $CONFIG{ 'mirror' } if ( $CONFIG{ 'mirror' } );
#
# The command we're going to run.
#
my $command = "rpmstrap $EXTRA $CONFIG{'dist'} $CONFIG{'location'} $mirror";
runCommand($command);
}
=begin doc
Install a new image of a distribution using `tar`.
=end doc
=cut
sub do_tar
{
#
# Find the tar command to run from the configuration file.
#
my $cmd = $CONFIG{ 'tar-cmd' };
if ( !defined($cmd) )
{
print "Falling back to default tar command\n";
$cmd = '/bin/tar --numeric-owner -xvf $src'; # Note: single quotes.
}
#
# Expand the tarfile.
#
$cmd =~ s/\$src/$CONFIG{'install-source'}/g;
#
# Run a command to copy an installed system into the new root.
#
runCommand("cd $CONFIG{'location'} && $cmd");
}
|