diff --git a/Makefile b/Makefile index d9dff3d..4182eba 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ # -- # http://www.steve.org.uk/ # -# $Id: Makefile,v 1.67 2006-06-23 08:56:32 steve Exp $ +# $Id: Makefile,v 1.68 2006-06-23 18:32:26 steve Exp $ # @@ -136,7 +136,7 @@ install-hooks: cp -R hooks/ubuntu/role.d/* ${prefix}/usr/lib/xen-tools/ubuntu.d/role.d cp -R hooks/ubuntu/*-* ${prefix}/usr/lib/xen-tools/ubuntu.d cp hooks/common.sh ${prefix}/usr/lib/xen-tools - @find ${prefix}/usr/lib/xen-tools -name 'CVS' -exec rm \{\} \; + @-find ${prefix}/usr/lib/xen-tools -name 'CVS' -exec rm -rf \{\} \; # diff --git a/bin/xen-create-image b/bin/xen-create-image index b4f156e..975a023 100755 --- a/bin/xen-create-image +++ b/bin/xen-create-image @@ -16,6 +16,7 @@ xen-create-image - Create a new Xen instance --version Show the version number and exit. Size / General options: + --accounts Copy all non-system accounts to the guest image --boot Boot the new instance after creating it. --cache Cache .deb files on the host when using B<--debootstrap> --force Force overwriting existing images. @@ -432,7 +433,7 @@ Install an X11 server, using VNC and XDM -- http://www.steve.org.uk/ - $Id: xen-create-image,v 1.43 2006-06-23 08:56:32 steve Exp $ + $Id: xen-create-image,v 1.44 2006-06-23 18:32:26 steve Exp $ =cut @@ -581,9 +582,9 @@ runRoleScript(); runXenConfigCreation(); # -# Unmount the disk image, and remove the temporary directory. +# Setup the password if the user wanted that. # -# skipped: this is handled in END +setupRootPassword() if ( $CONFIG{'passwd'} ); # @@ -824,6 +825,7 @@ sub parseCommandLineArguments "tar=s", \$CONFIG{'tar'}, # Misc. options + "accounts", \$CONFIG{'accounts'}, "fs", \$CONFIG{'fs'}, "boot", \$CONFIG{'boot'}, "cache=s", \$CONFIG{'cache'}, @@ -846,7 +848,7 @@ sub parseCommandLineArguments if ( $VERSION ) { - my $REVISION = '$Revision: 1.43 $'; + my $REVISION = '$Revision: 1.44 $'; if ( $REVISION =~ /1.([0-9.]+) / ) { @@ -1642,6 +1644,28 @@ sub runXenConfigCreation +=head2 setupRootPassword + + chroot() into the new system and setup the password. + +=cut + +sub setupRootPassword +{ + print "Setting up root password\n"; + + if ( -x $MOUNT_POINT . "/usr/bin/passwd" ) + { + system( "chroot $MOUNT_POINT /usr/bin/passwd" ); + } + else + { + print "/usr/bin/passwd on the new system doesn't exist...\n"; + } +} + + + =head2 runCommand A utility method to run a system command. We will capture the return diff --git a/hooks/centos4/35-setup-users b/hooks/centos4/35-setup-users new file mode 100755 index 0000000..5d333b2 --- /dev/null +++ b/hooks/centos4/35-setup-users @@ -0,0 +1,147 @@ +#!/usr/bin/perl -w +# +# This script attempts to copy all user accounts from the host to +# the guest. It does this by copying all user accounts which are not +# already present. +# +# NOTE: Unless '--accounts' was specified upon the 'xen-create-image' +# command line we don't do this. +# +# Steve +# -- +# http://www.steve.org.uk/ + + +use strict; +use Env; + + +my $prefix = shift; + +die "Prefix must be given" unless defined( $prefix ); +die "Prefix must be a directory" unless ( -d $prefix ); + + +# +# Exit unless the 'accounts' variable is set. +# +exit unless ( $ENV{'accounts'} ); + + +# +# Make sure we have $prefix/etc +# +die "Prefix is missing /etc : $prefix" unless ( -d $prefix . "/etc" ); + + +# +# Read all accounts from the installed /etc/passwd on the guest. +# +my %present; +if ( -e $prefix . "/etc/passwd" ) +{ + %present = readAccounts( $prefix . "/etc/passwd" ); +} + + +# +# Now read the accounts on the host. +# +my %host = readAccounts( "/etc/passwd" ); + + +# +# For each account not present on new installation then add it +# +foreach my $account ( sort keys( %host ) ) +{ + if ( ! $present{ $account } ) + { + print "Adding: $account\n"; + addAccount( $account ); + } +} + + + +# +# Read the accounts which are already present on the guest image. +# +sub readAccounts +{ + my ( $file ) = ( @_ ); + + + my %found; + + open( EXISTING, "<", $file ); + foreach my $line ( ) + { + # + # Record the userid + username + # + if ( $line =~ /^([^:]+):([^:]+):([^:]+)/ ) + { + my $user = $1; + my $pass = $2; + my $uid = $3; + + $found{$user} = 1; + } + } + close( EXISTING ); + + return( %found ); +} + + + + +# +# Add the passwd + shadow accounts for the given user. +# +sub addAccount +{ + my ( $user ) = ( @_ ); + + # + # passwd file. + # + open( PASSWD, "<", "/etc/passwd" ); + foreach my $line ( ) + { + chomp( $line ); + if ( $line =~ /^\Q$user\E:/ ) + { + # + # Add the line + # + open( OUTY, ">>", $prefix . "/etc/passwd" ); + print OUTY $line . "\n"; + close( OUTY ); + + print "addexd\n"; + } + } + close( PASSWD ); + + + # + # shadow file. + # + open( SHADOW, "<", "/etc/shadow" ) or return; + foreach my $line ( ) + { + chomp( $line ); + if ( $line =~ /\Q^$user:^E/ ) + { + # + # Add the line + # + open( OUTY, ">", $prefix . "/etc/shadow" ); + print OUTY $line . "\n"; + close( OUTY ); + } + } + close( SHADOW ); +} diff --git a/hooks/debian/35-setup-users b/hooks/debian/35-setup-users new file mode 100755 index 0000000..5d333b2 --- /dev/null +++ b/hooks/debian/35-setup-users @@ -0,0 +1,147 @@ +#!/usr/bin/perl -w +# +# This script attempts to copy all user accounts from the host to +# the guest. It does this by copying all user accounts which are not +# already present. +# +# NOTE: Unless '--accounts' was specified upon the 'xen-create-image' +# command line we don't do this. +# +# Steve +# -- +# http://www.steve.org.uk/ + + +use strict; +use Env; + + +my $prefix = shift; + +die "Prefix must be given" unless defined( $prefix ); +die "Prefix must be a directory" unless ( -d $prefix ); + + +# +# Exit unless the 'accounts' variable is set. +# +exit unless ( $ENV{'accounts'} ); + + +# +# Make sure we have $prefix/etc +# +die "Prefix is missing /etc : $prefix" unless ( -d $prefix . "/etc" ); + + +# +# Read all accounts from the installed /etc/passwd on the guest. +# +my %present; +if ( -e $prefix . "/etc/passwd" ) +{ + %present = readAccounts( $prefix . "/etc/passwd" ); +} + + +# +# Now read the accounts on the host. +# +my %host = readAccounts( "/etc/passwd" ); + + +# +# For each account not present on new installation then add it +# +foreach my $account ( sort keys( %host ) ) +{ + if ( ! $present{ $account } ) + { + print "Adding: $account\n"; + addAccount( $account ); + } +} + + + +# +# Read the accounts which are already present on the guest image. +# +sub readAccounts +{ + my ( $file ) = ( @_ ); + + + my %found; + + open( EXISTING, "<", $file ); + foreach my $line ( ) + { + # + # Record the userid + username + # + if ( $line =~ /^([^:]+):([^:]+):([^:]+)/ ) + { + my $user = $1; + my $pass = $2; + my $uid = $3; + + $found{$user} = 1; + } + } + close( EXISTING ); + + return( %found ); +} + + + + +# +# Add the passwd + shadow accounts for the given user. +# +sub addAccount +{ + my ( $user ) = ( @_ ); + + # + # passwd file. + # + open( PASSWD, "<", "/etc/passwd" ); + foreach my $line ( ) + { + chomp( $line ); + if ( $line =~ /^\Q$user\E:/ ) + { + # + # Add the line + # + open( OUTY, ">>", $prefix . "/etc/passwd" ); + print OUTY $line . "\n"; + close( OUTY ); + + print "addexd\n"; + } + } + close( PASSWD ); + + + # + # shadow file. + # + open( SHADOW, "<", "/etc/shadow" ) or return; + foreach my $line ( ) + { + chomp( $line ); + if ( $line =~ /\Q^$user:^E/ ) + { + # + # Add the line + # + open( OUTY, ">", $prefix . "/etc/shadow" ); + print OUTY $line . "\n"; + close( OUTY ); + } + } + close( SHADOW ); +} diff --git a/hooks/gentoo/35-setup-users b/hooks/gentoo/35-setup-users new file mode 100755 index 0000000..5d333b2 --- /dev/null +++ b/hooks/gentoo/35-setup-users @@ -0,0 +1,147 @@ +#!/usr/bin/perl -w +# +# This script attempts to copy all user accounts from the host to +# the guest. It does this by copying all user accounts which are not +# already present. +# +# NOTE: Unless '--accounts' was specified upon the 'xen-create-image' +# command line we don't do this. +# +# Steve +# -- +# http://www.steve.org.uk/ + + +use strict; +use Env; + + +my $prefix = shift; + +die "Prefix must be given" unless defined( $prefix ); +die "Prefix must be a directory" unless ( -d $prefix ); + + +# +# Exit unless the 'accounts' variable is set. +# +exit unless ( $ENV{'accounts'} ); + + +# +# Make sure we have $prefix/etc +# +die "Prefix is missing /etc : $prefix" unless ( -d $prefix . "/etc" ); + + +# +# Read all accounts from the installed /etc/passwd on the guest. +# +my %present; +if ( -e $prefix . "/etc/passwd" ) +{ + %present = readAccounts( $prefix . "/etc/passwd" ); +} + + +# +# Now read the accounts on the host. +# +my %host = readAccounts( "/etc/passwd" ); + + +# +# For each account not present on new installation then add it +# +foreach my $account ( sort keys( %host ) ) +{ + if ( ! $present{ $account } ) + { + print "Adding: $account\n"; + addAccount( $account ); + } +} + + + +# +# Read the accounts which are already present on the guest image. +# +sub readAccounts +{ + my ( $file ) = ( @_ ); + + + my %found; + + open( EXISTING, "<", $file ); + foreach my $line ( ) + { + # + # Record the userid + username + # + if ( $line =~ /^([^:]+):([^:]+):([^:]+)/ ) + { + my $user = $1; + my $pass = $2; + my $uid = $3; + + $found{$user} = 1; + } + } + close( EXISTING ); + + return( %found ); +} + + + + +# +# Add the passwd + shadow accounts for the given user. +# +sub addAccount +{ + my ( $user ) = ( @_ ); + + # + # passwd file. + # + open( PASSWD, "<", "/etc/passwd" ); + foreach my $line ( ) + { + chomp( $line ); + if ( $line =~ /^\Q$user\E:/ ) + { + # + # Add the line + # + open( OUTY, ">>", $prefix . "/etc/passwd" ); + print OUTY $line . "\n"; + close( OUTY ); + + print "addexd\n"; + } + } + close( PASSWD ); + + + # + # shadow file. + # + open( SHADOW, "<", "/etc/shadow" ) or return; + foreach my $line ( ) + { + chomp( $line ); + if ( $line =~ /\Q^$user:^E/ ) + { + # + # Add the line + # + open( OUTY, ">", $prefix . "/etc/shadow" ); + print OUTY $line . "\n"; + close( OUTY ); + } + } + close( SHADOW ); +} diff --git a/hooks/ubuntu/35-setup-users b/hooks/ubuntu/35-setup-users new file mode 100755 index 0000000..5d333b2 --- /dev/null +++ b/hooks/ubuntu/35-setup-users @@ -0,0 +1,147 @@ +#!/usr/bin/perl -w +# +# This script attempts to copy all user accounts from the host to +# the guest. It does this by copying all user accounts which are not +# already present. +# +# NOTE: Unless '--accounts' was specified upon the 'xen-create-image' +# command line we don't do this. +# +# Steve +# -- +# http://www.steve.org.uk/ + + +use strict; +use Env; + + +my $prefix = shift; + +die "Prefix must be given" unless defined( $prefix ); +die "Prefix must be a directory" unless ( -d $prefix ); + + +# +# Exit unless the 'accounts' variable is set. +# +exit unless ( $ENV{'accounts'} ); + + +# +# Make sure we have $prefix/etc +# +die "Prefix is missing /etc : $prefix" unless ( -d $prefix . "/etc" ); + + +# +# Read all accounts from the installed /etc/passwd on the guest. +# +my %present; +if ( -e $prefix . "/etc/passwd" ) +{ + %present = readAccounts( $prefix . "/etc/passwd" ); +} + + +# +# Now read the accounts on the host. +# +my %host = readAccounts( "/etc/passwd" ); + + +# +# For each account not present on new installation then add it +# +foreach my $account ( sort keys( %host ) ) +{ + if ( ! $present{ $account } ) + { + print "Adding: $account\n"; + addAccount( $account ); + } +} + + + +# +# Read the accounts which are already present on the guest image. +# +sub readAccounts +{ + my ( $file ) = ( @_ ); + + + my %found; + + open( EXISTING, "<", $file ); + foreach my $line ( ) + { + # + # Record the userid + username + # + if ( $line =~ /^([^:]+):([^:]+):([^:]+)/ ) + { + my $user = $1; + my $pass = $2; + my $uid = $3; + + $found{$user} = 1; + } + } + close( EXISTING ); + + return( %found ); +} + + + + +# +# Add the passwd + shadow accounts for the given user. +# +sub addAccount +{ + my ( $user ) = ( @_ ); + + # + # passwd file. + # + open( PASSWD, "<", "/etc/passwd" ); + foreach my $line ( ) + { + chomp( $line ); + if ( $line =~ /^\Q$user\E:/ ) + { + # + # Add the line + # + open( OUTY, ">>", $prefix . "/etc/passwd" ); + print OUTY $line . "\n"; + close( OUTY ); + + print "addexd\n"; + } + } + close( PASSWD ); + + + # + # shadow file. + # + open( SHADOW, "<", "/etc/shadow" ) or return; + foreach my $line ( ) + { + chomp( $line ); + if ( $line =~ /\Q^$user:^E/ ) + { + # + # Add the line + # + open( OUTY, ">", $prefix . "/etc/shadow" ); + print OUTY $line . "\n"; + close( OUTY ); + } + } + close( SHADOW ); +}