-
Notifications
You must be signed in to change notification settings - Fork 21
/
xen-list-images
executable file
·273 lines (190 loc) · 4.73 KB
/
xen-list-images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/perl -w
=encoding utf8
=head1 NAME
xen-list-images - List all the created and configured Xen images.
=head1 SYNOPSIS
xen-list-image [options]
Filename Options:
--extension Specify the file extension to use. An empty extension is equal
to any extension.
Help Options:
--help Show this scripts help information.
--manual Read this scripts manual.
--version Show the version number and exit.
Testing options:
--test Specify an alternate Xen configuration directory.
=head1 OPTIONS
=over 8
=item B<--help>
Show the scripts help information.
=item B<--manual>
Read the manual.
=item B<--test>
This flag causes the script to load the Xen configuration files from a different directory than the default of B</etc/xen>.
=item B<--version>
Show the version number and exit.
=back
=head1 DESCRIPTION
xen-list-images is a simple script which will display all the
Xen images which have been created.
This works by iterating over all files matching the pattern
/etc/xen/*.cfg which is what the xen-create-image script would
create.
For each instance which has been created we'll display the name,
and then either the IP address configured, or "DHCP" to denote
a dynamic host.
=head1 TODO
It should be possible to determine the disk(s) used by the images,
and then display their sizes.
=head1 AUTHORS
Steve Kemp, http://www.steve.org.uk/
Stéphane Jourdois
=head1 LICENSE
Copyright (c) 2005-2009 by Steve Kemp, (c) 2010-2013 by The Xen-Tools
Development Team. 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
use strict;
use English;
use File::Temp qw/ tempdir /;
use Getopt::Long;
use Pod::Usage;
use Xen::Tools::Common;
#
# 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;
#
# Default values
#
$CONFIG{ 'prefix' } = "/etc/xen";
$CONFIG{ 'extension' } = '.cfg';
#
# Release number.
#
my $RELEASE = '4.6.1';
#
# Read the global configuration file if it exists.
#
readConfigurationFile("/etc/xen-tools/xen-tools.conf", \%CONFIG);
#
# Parse command line arguments, these override the values from the
# configuration file.
#
parseCommandLineArguments();
#
# Read all the xen configuration files.
#
my @instances = findXenInstances();
#
# Now process each instance.
#
my $count = 0;
foreach my $instance (@instances)
{
if ($count) {print "\n";}
displayInstance($instance);
$count += 1;
}
#
# All done.
#
exit;
=begin doc
Parse the arguments specified upon the command line.
=end doc
=cut
sub parseCommandLineArguments
{
my $HELP = 0;
my $MANUAL = 0;
my $VERSION = 0;
# Parse options.
#
GetOptions( "test=s", \$CONFIG{ 'prefix' },
"extension:s", \$CONFIG{ 'extension' },
"help", \$HELP,
"manual", \$MANUAL,
"version", \$VERSION );
pod2usage(1) if $HELP;
pod2usage( -verbose => 2 ) if $MANUAL;
if ($VERSION)
{
print "xen-list-images release $RELEASE\n";
exit;
}
}
=begin doc
Return an array containing the names of each xen configuration
file we found.
=end doc
=cut
sub findXenInstances
{
my @found;
foreach my $file ( sort( glob( $CONFIG{ 'prefix' } . "/*" . $CONFIG{ 'extension' } ) ) )
{
push @found, $file if (
-f $file and
$file !~ m(~$|\.dpkg-[a-z]+$|\.sxp$|/xl\.conf$)
);
}
return (@found);
}
=begin doc
Show details about the the Xen instance contained in the given
configuration file.
=end doc
=cut
sub displayInstance
{
my ($file) = (@_);
#
# Read each line.
#
open( FILY, "<", $file );
my @LINES = <FILY>;
close(FILY);
#
# Is it dynamic?
#
my $dhcp = 0;
my $ip = '';
my $mac = '';
my $name = '';
my $mem = 0;
foreach my $line (@LINES)
{
if ( $line =~ /^\s*dhcp\s*=\s*"dhcp\"/i )
{
$dhcp = 1;
}
if ( $line =~ /^\s*name\s*=\s*["']([^'"]+)['"]/i )
{
$name = $1;
}
if ( $line =~ /^\s*memory[^0-9]*([0-9]+)/i )
{
$mem = $1;
}
if ( $line =~ /ip=([0-9\.]+)/ )
{
$ip = $1;
}
if ( $line =~ /mac=['\"]([^'\"]+)['\"]/ )
{
$mac = " [MAC: $1]";
}
}
print "Name: $name\n";
print "Memory: $mem MB\n";
print "IP: " . $ip . $mac . "\n" if length($ip);
print "DHCP" . $mac . "\n" if $dhcp;
print "Config: $file\n";
}