From 474ee42c65a47aafbb5729f8114e035e606c81ad Mon Sep 17 00:00:00 2001 From: steve Date: Mon, 19 Jun 2006 12:49:36 +0000 Subject: [PATCH] 2006-06-19 12:49:34 by steve Updated to read the Xen configuration file from a template and process that with Text::Template to generate the output. This is nice and simple, and very extensible. --- bin/xt-create-xen-config | 126 ++++++++++++++++++++++++++------------- etc/xm.tmpl | 48 +++++++++++++++ 2 files changed, 132 insertions(+), 42 deletions(-) create mode 100644 etc/xm.tmpl diff --git a/bin/xt-create-xen-config b/bin/xt-create-xen-config index 9b8de16..aeb07bf 100755 --- a/bin/xt-create-xen-config +++ b/bin/xt-create-xen-config @@ -26,11 +26,23 @@ xt-create-config - Create a Xen configuration file for a new domain. =cut -=head1 NOTES +=head1 ABOUT - This script is invoked by xen-create-image after it has created and + This script is invoked by B after it has created and customised a new Xen domain. It is responsible for creating the - configuration file that Xen itself will use. + configuration file that Xen should use to start the instance. + + The configuration file will be created in the directory /etc/xen using + the template file B to specify what is included. + + If you wish to make changes to the Xen configuration file for every + domain which is created then you should modify the master template rather + than fixing up each new instances configuration afterwards. + +=cut + + +=head1 ARGUMENT PASSING This script will be invoked with a full copy of the arguments from xen-create-image in its environment, along with several command line @@ -45,7 +57,7 @@ xt-create-config - Create a Xen configuration file for a new domain. -- http://www.steve.org.uk/ - $Id: xt-create-xen-config,v 1.8 2006-06-13 13:21:22 steve Exp $ + $Id: xt-create-xen-config,v 1.9 2006-06-19 12:49:34 steve Exp $ =cut @@ -66,6 +78,8 @@ use strict; use Env; use Getopt::Long; use Pod::Usage; +use Text::Template; + # @@ -75,6 +89,12 @@ use Pod::Usage; # my %CONFIG; +# +# Default options +# +$CONFIG{'template'} = '/etc/xen-tools/xm.tmpl'; + + # # Release number. # @@ -100,6 +120,7 @@ checkArguments(); # createXenConfig(); + # # Exit cleanly - any errors which have already occurred will result # in "exit 1". @@ -119,15 +140,16 @@ exit 0; sub parseCommandLineArguments { - my $HELP = 0; - my $MANUAL = 0; - my $VERSION = 0; + my $HELP = 0; + my $MANUAL = 0; + my $VERSION = 0; # # Parse options. # GetOptions( "output=s", \$CONFIG{'output'}, + "template=s", \$CONFIG{'template'}, "verbose", \$CONFIG{'verbose'}, "help", \$HELP, "manual", \$MANUAL, @@ -140,14 +162,14 @@ sub parseCommandLineArguments if ( $VERSION ) { - my $REVISION = '$Revision: 1.8 $'; + my $REVISION = '$Revision: 1.9 $'; if ( $REVISION =~ /1.([0-9.]+) / ) { $REVISION = $1; } - print "xt-customize-image release $RELEASE - CVS: $REVISION\n"; + print "xt-create-xen-config release $RELEASE - CVS: $REVISION\n"; exit; } @@ -171,6 +193,23 @@ sub checkArguments print "The '--output' argument is mandatory\n"; exit 1; } + + # + # Make sure that any specified tempalte file exists. + # + if ( defined( $CONFIG{'template'} ) ) + { + if ( ! -e $CONFIG{'template'} ) + { + print "The specified template file, $CONFIG{'template'} does not exist.\n"; + exit 1; + } + } + else + { + print "A template file was not specified. Aborting\n"; + exit 1; + } } @@ -184,41 +223,37 @@ sub checkArguments sub createXenConfig { + # + # The output file we're going to process. + # my $file = $CONFIG{'output'} . '/' . $ENV{'hostname'} . '.cfg'; # - # The parameters we use. + # The template we're going to read from. + # + my $template = new Text::Template( TYPE => 'FILE', + SOURCE => $CONFIG{'template'} ); + + + # + # The device we're using. # my $device = 'sda'; if ( defined( $ENV{'ide'} ) ) { $device = 'hda'; } + $ENV{'device'} = $device; + # - # Strip a trailing qualifier from the memory + # The memory size. # if ( $ENV{'memory'} =~ /([0-9]+)/ ) { $ENV{'memory'} = $1; } - my $network = ''; - - if ( $ENV{'dhcp'} ) - { - $network =<", $file ); - print FILE <fill_in(HASH => \%vars); + if (defined $result) + { + print FILE $result ; + } + else + { + print FILE "Error creating configuration file\n"; + } close( FILE ); + } diff --git a/etc/xm.tmpl b/etc/xm.tmpl new file mode 100644 index 0000000..9dfd375 --- /dev/null +++ b/etc/xm.tmpl @@ -0,0 +1,48 @@ +# +# Configuration file for the Xen instance {$hostname}, created on +# { scalar localtime }. +# + + +# +# Kernel + memory size +# +kernel = '{$kernel}' +ramdisk = '{$initrd}' +memory = '{$memory}' + + +# +# Disk device(s). +# +root = '/dev/{$device}1 ro' +disk = [ '{$image_vbd},{$device}1,w', + '{$swap_vbd},{$device}2,w' ] + + +# +# Hostname +# +name = '{$hostname}' + + +# +# Networking +# +{ if ( $dhcp ) + { + $OUT .= "dhcp = 'dhcp'"; + $OUT .= "vif = [ '' ]"; + } + else + { + $OUT .= "vif = [ 'ip=$ip1' ]"; + } +} + +# +# Behaviour +# +on_poweroff = 'destroy' +on_reboot = 'restart' +on_crash = 'restart'