1
0
mirror of synced 2026-04-18 00:06:35 +00:00
Files
xen-tools.xen-tools/bin/xt-customize-image
steve 9c1d749ec8 2006-06-09 13:26:01 by steve
Added 99% of the code.
  NOTE: --location must be absolute, or the chdir() breaks things.
  FIXME?
2006-06-09 13:26:01 +00:00

310 lines
5.7 KiB
Perl
Executable File

#!/usr/bin/perl -w
=head1 NAME
xt-customize-image - Customize a freshly installed copy of GNU/Linux
=cut
=head1 SYNOPSIS
xt-customize-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 of the new installation
--dist The name of the distribution which has been installed.
All other options from B<xen-create-image> will be passed as environmental
variables.
=cut
=head1 NOTES
This script is invoked by B<xen-create-image> after it has created a
fresh installation of Linux withing a temporary location.
This script will be invoked with a full copy of the arguments from
B<xen-create-image> in its environment, along with several command line
arguments.
The command line arguments which are mandatory are:
--location - The temporary installation root of the new install
--dist - The distribution which has been installed.
=cut
=head1 AUTHOR
Steve
--
http://www.steve.org.uk/
$Id: xt-customize-image,v 1.4 2006-06-09 13:26:01 steve Exp $
=cut
=head1 LICENSE
Copyright (c) 2005-2006 by Steve Kemp. 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
#
# Customize an installed copy of Linux by running hooks, etc.
#
use strict;
use Getopt::Long;
use Pod::Usage;
#
# Configuration options, initially read from the configuration files
# but may be overridden by the command line.
#
# Command line flags *always* take precedence over the configuration file.
#
my %CONFIG;
#
# Release number.
#
my $RELEASE = '2.0';
#
# Parse arguments.
#
# We'll need at least:
#
# --location = /tmp/3980fdf890
# --dist = { sid sarge etch centos4 gentoo .. etc }
#
#
parseCommandLineArguments();
#
# Check arguments:
#
# Distro hook directory must exist: /usr/lib/${distro}.d/
#
# Mountpoint must exist: ${location}
checkArguments();
#
# Run each relevent hook script.
#
#
runDistributionHooks();
# Exit cleanly
#
exit 0;
=head2 parseArguments
Parse the command line arguments this script was given.
=cut
sub parseCommandLineArguments
{
my $HELP = 0;
my $MANUAL = 0;
my $VERSION = 0;
# Parse options.
#
GetOptions(
"location=s", \$CONFIG{'location'},
"dist=s", \$CONFIG{'dist'},
"verbose", \$CONFIG{'verbose'},
"help", \$HELP,
"manual", \$MANUAL,
"version", \$VERSION
);
pod2usage(1) if $HELP;
pod2usage(-verbose => 2 ) if $MANUAL;
if ( $VERSION )
{
my $REVISION = '$Revision: 1.4 $';
if ( $REVISION =~ /1.([0-9.]+) / )
{
$REVISION = $1;
}
print "xt-customize-image release $RELEASE - CVS: $REVISION\n";
exit;
}
}
=head2 checkArguments
Test that the command line arguments we were given make sense.
=cut
sub checkArguments
{
#
# Test that the location we've been told contains
# a fresh installation of Linux 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;
}
#
# 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're trying to configure an installation of $CONFIG{'dist'} in
$CONFIG{'location'} - but there is no hook directory for us to use.
This means we don't know how to configure this installation.
We'd expect the hook directory to be : $dir
Aborting.
E_OR
exit 1;
}
}
=head2 runDistributionHooks
This function does the real work of running each of our hook scripts.
Each hook script is executed in turn, ignoring emacs save files and
dpkg-temporary files.
We will give each script the name of the directory containing the
installation as a single argument.
NOTE: We must change to the directory of the scripts prior to running them
so that ../common.sh exists and is accessible.
=cut
sub runDistributionHooks
{
#
# Hook directory.
#
my $hooks = "/usr/lib/xen-tools/" . $CONFIG{'dist'} . ".d/";
chdir( $hooks );
#
# Installation prefix
#
my $prefix = $CONFIG{'location'};
#
# If we're running verbosely then setup the client environment
# appropriately.
#
# This is useful in case this script is called outwith the usual
# xen-create-image framework.
#
if ( $CONFIG{'verbose'} )
{
$ENV{'verbose'} = 1;
}
#
# Make sure that our scripts run in sorted order, as
# the user would expect.
#
foreach my $file ( sort( glob( $hooks . "*" ) ) )
{
# skip files that end with .dpkg-new, .dpkg-old or '~'
next if ( $file =~ /\.dpkg-(new|old)/ );
next if ( $file =~ /~$/ );
#
# Only run executable files.
#
if ( ( -x $file ) && ( -f $file ) )
{
#
# Just display the name - no need to see the full path.
#
my $name = $file;
if ( $file =~ /(.*)\/(.*)/ )
{
$name = $2;
}
#
# Complete path to the file.
#
my $cmd = $hooks . $name . " $CONFIG{'location'}";
if ( $CONFIG{'verbose'} )
{
print "Running hook $name ['$cmd']";
}
else
{
print "Running hook $name";
}
system( $cmd );
print " - done.\n";
}
}
}