1
0
mirror of synced 2026-02-23 07:01:53 +00:00

2006-06-23 18:32:26 by steve

Added support for copying user accounts when given an --accounts flag.
This commit is contained in:
steve
2006-06-23 18:32:31 +00:00
parent 2196e621ad
commit c61f4c96de
6 changed files with 618 additions and 6 deletions

View File

@@ -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 \{\} \;
#

View File

@@ -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

147
hooks/centos4/35-setup-users Executable file
View File

@@ -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 ( <EXISTING> )
{
#
# 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 ( <PASSWD> )
{
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 ( <SHADOW> )
{
chomp( $line );
if ( $line =~ /\Q^$user:^E/ )
{
#
# Add the line
#
open( OUTY, ">", $prefix . "/etc/shadow" );
print OUTY $line . "\n";
close( OUTY );
}
}
close( SHADOW );
}

147
hooks/debian/35-setup-users Executable file
View File

@@ -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 ( <EXISTING> )
{
#
# 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 ( <PASSWD> )
{
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 ( <SHADOW> )
{
chomp( $line );
if ( $line =~ /\Q^$user:^E/ )
{
#
# Add the line
#
open( OUTY, ">", $prefix . "/etc/shadow" );
print OUTY $line . "\n";
close( OUTY );
}
}
close( SHADOW );
}

147
hooks/gentoo/35-setup-users Executable file
View File

@@ -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 ( <EXISTING> )
{
#
# 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 ( <PASSWD> )
{
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 ( <SHADOW> )
{
chomp( $line );
if ( $line =~ /\Q^$user:^E/ )
{
#
# Add the line
#
open( OUTY, ">", $prefix . "/etc/shadow" );
print OUTY $line . "\n";
close( OUTY );
}
}
close( SHADOW );
}

147
hooks/ubuntu/35-setup-users Executable file
View File

@@ -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 ( <EXISTING> )
{
#
# 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 ( <PASSWD> )
{
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 ( <SHADOW> )
{
chomp( $line );
if ( $line =~ /\Q^$user:^E/ )
{
#
# Add the line
#
open( OUTY, ">", $prefix . "/etc/shadow" );
print OUTY $line . "\n";
close( OUTY );
}
}
close( SHADOW );
}