File: update-modules

package info (click to toggle)
xen-tools 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,568 kB
  • ctags: 172
  • sloc: perl: 5,082; sh: 2,092; makefile: 239
file content (233 lines) | stat: -rw-r--r-- 4,136 bytes parent folder | download | duplicates (9)
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
#!/usr/bin/perl
#
#  This code is intended to update the modules installed within each
# Xen guest domain to the named version.
#
#  It should be fairly safe, but I make no promises - hence why this
# is an example.
#
# Steve
# --
#

use strict;
use warnings;

use File::Path qw/ rmtree  /;
use File::Temp qw/ tempdir /;


my $modules = shift;
if ( ! defined( $modules ) )
{
    print <<EOF;

 Usage: $0 <modules>

  eg: $0 2.6.18.xx|/usr/lib/modules/nn.nn.nn-xen

EOF
  exit;
}


#
#  Make sure the path is fully qualified.
#
if ( $modules !~ /^[\/\\]/ )
{
    $modules = "/lib/modules/" . $modules;
}


#
#  Make sure the module directory exists.
#
if ( ! -d $modules )
{
    print "The modules directory $modules doesn't exist.\n";
    exit;
}


#
#  OK now we have the modules so we need to:
#
#  0.  Read our configuration file.
#  1.  Find each xen guest.
#  2.  Ensure it isn't running (TODO)
#  3.  Mount the disk image.
#  4.  Remove existing modules, and copy in the specified ones.
#
#


my %CONFIG;
readConfigurationFile( "/etc/xen-tools/xen-tools.conf" );


foreach my $guest ( findGuests() )
{
    print "Attempting to update guest: $guest\n";

    #
    #  Create a temporary directory to mount the disk upon.
    #
    my $tmp = tempdir( CLEANUP => 1 );

    #
    # Mount the disk.
    #
    if ( $CONFIG{'dir'} )
    {
        # The loopback image.
        my $img =  $CONFIG{'dir'} . "/domains/" . $guest . "/disk.img";
        system( "mount -o loop $img $tmp" );
    }
    elsif ( $CONFIG{'lvm'} )
    {
        # The LVM volume
        my $img = "/dev/" . $CONFIG{'lvm'} . "/$guest-disk";
        system( "mount $img $tmp" );
    }
    else
    {
        print "Unhandled disk format - can't mount\n";
        next;
    }


    #
    #  We've got it mounted.
    #
    print "\tMounted disk image.\n";

    # make sure we have a directory
    if ( ! -d $tmp . "/lib/modules" )
    {
        print "\tMissing modules.  Skipping\n";
        next;
    }
    #
    #  Remove the existing module directories.
    #
    `rm -rf  $tmp/lib/modules`;
    mkdir $tmp . "/lib/modules";
    print "\tRemoved existing modules\n";

    #
    #  Copy existing directory.
    #
    if ( -d $tmp . "/lib/modules" )
    {
        `cp -R $modules $tmp/lib/modules/`;
        print "\tCopied over $modules\n";
    }
    else
    {
        print "No module directory .. weirdness\n";
    }

    #
    #  Unmount
    #
    system( "umount $tmp" );
    print "\tUnmounted disk image.\n\n";
}




=begin doc

  Find each xen guest upon the system.

=end doc

=cut

sub findGuests
{
    my @results;

    #
    #  Assume xen-tools.
    #
    foreach my $file ( glob( "/etc/xen/*.cfg" ) )
    {
        #
        #  Find the name.
        #
        open( INPUT, "<" , $file );
        foreach my $line ( <INPUT> )
        {
            chomp( $line );
            if ( $line =~ /name\s*=\s*['"]([^'"]+)["']/ )
            {
                push @results, $1;
            }
        }
        close( INPUT );
    }
    return( sort( @results ) );
}




=begin doc

  Read the configuration file specified.

=end doc

=cut

sub readConfigurationFile
{
    my ($file) = ( @_ );

    open( FILE, "<", $file ) or die "Cannot read file '$file' - $!";

    my $line       = "";

    while (defined($line = <FILE>) )
    {
        chomp $line;
        if ($line =~ s/\\$//)
        {
            $line .= <FILE>;
            redo unless eof(FILE);
        }

        # Skip lines beginning with comments
        next if ( $line =~ /^([ \t]*)\#/ );

        # Skip blank lines
        next if ( length( $line ) < 1 );

        # Strip trailing comments.
        if ( $line =~ /(.*)\#(.*)/ )
        {
            $line = $1;
        }

        # Find variable settings
        if ( $line =~ /([^=]+)=([^\n]+)/ )
        {
            my $key = $1;
            my $val = $2;

            # Strip leading and trailing whitespace.
            $key =~ s/^\s+//;
            $key =~ s/\s+$//;
            $val =~ s/^\s+//;
            $val =~ s/\s+$//;

            # Store value.
            $CONFIG{ $key } = $val;
        }
    }

    close( FILE );
}