1
0
mirror of synced 2026-05-01 14:16:09 +00:00

2005-12-24 09:28:53 by steve

When executed by non-root user still work; just don't show the IP details.
This commit is contained in:
steve
2005-12-24 09:28:53 +00:00
parent 507a8a6b3e
commit 5f077309be

View File

@@ -39,7 +39,6 @@ Show the version number and exit.
=head1 DESCRIPTION =head1 DESCRIPTION
xen-list-images is a simple script which will display all the xen-list-images is a simple script which will display all the
images which have been created in a given directory. images which have been created in a given directory.
@@ -66,6 +65,19 @@ Show the version number and exit.
networking information. networking information.
=head2 NOTES
If the script is run by a non-root user the networking information
will not be displayed. This is because a non-user may not mount
the disk images to read the configuration.
If you wish to see the networking details you must execute this
script as root.
=cut
=head1 AUTHOR =head1 AUTHOR
@@ -73,7 +85,7 @@ Show the version number and exit.
-- --
http://www.steve.org.uk/ http://www.steve.org.uk/
$Id: xen-list-images,v 1.5 2005-12-21 19:26:05 steve Exp $ $Id: xen-list-images,v 1.6 2005-12-24 09:28:53 steve Exp $
=cut =cut
@@ -128,26 +140,16 @@ if ( -e $ENV{'HOME'} . ".xen-tools.conf" )
parseCommandLineArguments(); parseCommandLineArguments();
# #
# If we're not root stop here - root can't do the mounting which # Get the directory which holds the images.
# is required to find the networking details.
# #
if ( $EFFECTIVE_USER_ID != 0 )
{
print <<E_O_ROOT;
In order to use this script you must be running with root privileges.
(This is necessary to mount the disk images to determine networking info.)
E_O_ROOT
exit;
}
my $dir = $CONFIG{'dir'} . "/domains/"; my $dir = $CONFIG{'dir'} . "/domains/";
#
# Get the name of the image.
#
foreach my $entry ( glob( $dir . "*" ) ) foreach my $entry ( glob( $dir . "*" ) )
{ {
if ( $entry =~ /(.*)\/domains\/(.*)/ ) if ( $entry =~ /(.*)\/domains\/(.*)/ )
@@ -170,47 +172,22 @@ foreach my $entry ( glob( $dir . "*" ) )
( -e $swap ) ) ( -e $swap ) )
{ {
print "Image: $entry "; if ( $EFFECTIVE_USER_ID != 0 )
#
# Mount the image securely
#
my $dir = tempdir( CLEANUP => 1 );
my $mount_cmd = "mount -t auto -o loop $image $dir";
`$mount_cmd`;
#
# Read /etc/network/interfaces
#
my $found = 0;
open( IN, "<", $dir . "/etc/network/interfaces" );
foreach my $line ( <IN> )
{ {
if ( ( $line =~ /dhcp/ ) && print "Image: $entry\n";
( $line =~ /eth/ ) )
{
print " DHCP\n";
$found = 1;
}
if ( $line =~ /address ([0-9\.]+)/ )
{
print $1 . "\n";
$found = 1;
}
} }
close( IN ); else
if ( ! $found ) { print "Unknown IP address\n"; } {
print "Image: $entry ";
#
# Unmount the image.
#
`umount $dir`;
showNetworkingDetails( $image );
}
} }
} }
} }
# #
# All done. # All done.
# #
@@ -303,7 +280,7 @@ sub parseCommandLineArguments
if ( $VERSION ) if ( $VERSION )
{ {
my $REVISION = '$Id: xen-list-images,v 1.5 2005-12-21 19:26:05 steve Exp $'; my $REVISION = '$Id: xen-list-images,v 1.6 2005-12-24 09:28:53 steve Exp $';
$VERSION = join (' ', (split (' ', $REVISION))[2]); $VERSION = join (' ', (split (' ', $REVISION))[2]);
$VERSION =~ s/,v\b//; $VERSION =~ s/,v\b//;
$VERSION =~ s/(\S+)$/$1/; $VERSION =~ s/(\S+)$/$1/;
@@ -315,3 +292,49 @@ sub parseCommandLineArguments
} }
=head2 showNetworkingDetails
Mount the given disk image and read the networking details from it.
=cut
sub showNetworkingDetails
{
my ( $image ) = ( @_ );
#
# Mount the image securely
#
my $dir = tempdir( CLEANUP => 1 );
my $mount_cmd = "mount -t auto -o loop $image $dir";
`$mount_cmd`;
#
# Read /etc/network/interfaces
#
my $found = 0;
open( IN, "<", $dir . "/etc/network/interfaces" );
foreach my $line ( <IN> )
{
if ( ( $line =~ /dhcp/ ) &&
( $line =~ /eth/ ) )
{
print " DHCP\n";
$found = 1;
}
if ( $line =~ /address ([0-9\.]+)/ )
{
print $1 . "\n";
$found = 1;
}
}
close( IN );
if ( ! $found ) { print "Unknown IP address\n"; }
#
# Unmount the image.
#
`umount $dir`;
}