This file is indexed.

/usr/bin/xen-update-image is in xen-tools 4.2.1-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/perl -w

=head1 NAME

xen-update-image - Update the software installed upon offline Xen images.

=head1 SYNOPSIS

  xen-update-image [options] imageName1 imageName2 .. imageNameN

  Help Options:
   --help      Show this scripts help information.
   --manual    Read this scripts manual.
   --version   Show the version number and exit.

  General Options:
   --dir       Specify the directory which contains the image(s).
   --lvm       Specify the LVM volume group which contains the image(s).
   --evms      Specify the EVMS container which contains the image(s).

=cut



=head1 OPTIONS

=over 8

=item B<--dir>
Specify the directory which contains the image(s).

=item B<--evms>
Specify the EVMS container which contains the image(s).

=item B<--help>
Show the script help.

=item B<--lvm>
Specify the LVM volume group which contains the image(s).

=item B<--manual>
Read the manual.

=item B<--version>
Show the version number and exit.

=back

=cut


=head1 DESCRIPTION

  xen-update-image is a simple script which allows you to update
 a Xen image of Debian which has been created with xen-create-image.

  It does this by mounting the image inside a temporary directory
 then running:

      apt-get update

      apt-get upgrade

  NOTE If the image is already running within Xen this will cause
 corruption otherwise it will allow you to update your image without
 booting it.

=cut


=head1 EXAMPLES

  The following assumes there are two images which are not currently
 running.  The images are called 'test.my.flat', and 'x11.my.flat'.

  Updating both images can be accomplished by executing:

     xen-update-images --dir=/home/xen test.my.flat x11.my.flat

=cut


=head1 AUTHORS

 Steve Kemp, http://www.steve.org.uk/
 Axel Beckert, http://noone.org/abe/
 Stéphane Jourdois

=cut

=head1 LICENSE

Copyright (c) 2005-2009 by Steve Kemp, (c) 2010 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 File::Copy qw/ mv cp /;
use Getopt::Long;
use Pod::Usage;


#
#  Configuration options, initially read from the configuration file
# but may be overridden by the command line.
#
#  Command line flags *always* take precedence over the configuration file.
#
my %CONFIG;

#
# Release number.
#
my $RELEASE = '4.2.1';


#
#  Read configuration file if it exists.
#
if ( -e "/etc/xen-tools/xen-tools.conf" )
{
    readConfigurationFile("/etc/xen-tools/xen-tools.conf");
}


#
#  Parse command line arguments, these override the values from the
# configuration file.
#
parseCommandLineArguments();


#
#  Test that our arguments are sane.
#
checkArguments();


#
#  Abort if non-root user.
#
if ( $EFFECTIVE_USER_ID != 0 )
{
    print <<E_O_ROOT;

  This script is not running with root privileges.

  root privileges are required to successfully mount the disk image(s).

E_O_ROOT

    exit;
}



#
#  Loop over the supplied arguments, and attempt to update each image.
#
while ( my $name = shift )
{
    if ( !xenRunning($name) )
    {
        updateXenImage($name);
    }
    else
    {
        print "Skipping xen guest '$name' - it appears to be running.\n";
    }
}


#
#  All done.
#
exit;



=begin doc

  Mount the primary disk image, so that we're ready to update it.

=end doc

=cut

sub updateXenImage
{
    my ($name) = (@_);

    #
    #  Create a temporary directory, and prepare to mount the
    # image there.
    #
    my $tmp       = tempdir( CLEANUP => 1 );
    my $img       = '';
    my $mount_cmd = '';

    #
    #  If we're dealing with loopback images find the main one,
    # and mount it.
    #
    if ( $CONFIG{ 'dir' } )
    {

        # The loopback image.
        $img = $CONFIG{ 'dir' } . "/domains/" . $name . "/disk.img";

        if ( !-e $img )
        {
            print "Disk image '$img' for host '$name' not found\n";
            return;
        }

        $mount_cmd = "mount -t auto -o loop $img $tmp";
    }
    elsif ( $CONFIG{ 'lvm' } )
    {

        # The LVM volume
        $img = "/dev/" . $CONFIG{ 'lvm' } . "/$name-disk";

        # make sure it exists.
        if ( !-e $img )
        {
            print "Logical volume '$img' for host '$name' not found\n";
            return;
        }

        $mount_cmd = "mount -t auto $img $tmp";
    }
    elsif ( $CONFIG{ 'evms' } )
    {

        # The EVMS volume -- note, unlike LVM, you don't need the
        # $CONFIG{'evms'} to see it and mount the
        # volume. $CONFIG{'evms'} is only used for manipulating the
        # underlying object.  Still, I don't want to mess with the
        # parse code and make it confusing - otherwise --evms takes an
        # argument everywhere but here, which will confuse users.  The
        # better solution is to make it so that --evms can take a
        # following container, but doesn't require it.  For the
        # moment, it is better to leave it as it is, take a container,
        # and then ignore it.

        # The best way to do it is to just read it out of the
        # configuration file, tell the user what you got and where you
        # got it from, and not bother the user with picking --dir or
        # --lvm or --evms at all, but infer it from the config file's
        # disk = parameter.  xen-delete-image might work the same way,
        # but it could be *slightly* more dangerous in the context of
        # deleting.
        $img = "/dev/evms/$name-disk";

        # make sure it exists.
        if ( !-e $img )
        {
            print "EVMS volume '$img' for host '$name' not found\n";
            return;
        }

        $mount_cmd = "mount -t auto $img $tmp";
    }
    else
    {
        die "Can't happen?\n";
    }

    #
    #  Mount the image.
    #
    `$mount_cmd`;

    #
    #  Make sure this is a Debian image.
    #
    if ( ( -e $tmp . "/usr/bin/apt-get" ) &&
         ( -x $tmp . "/usr/bin/apt-get" ) )
    {
        #
        #  Copy dom0's resolv.conf to domU
        #
        mv("$tmp/etc/resolv.conf", "$tmp/etc/resolv.conf.old") if -f "$tmp/etc/resolv.conf";
        cp("/etc/resolv.conf", "$tmp/etc/resolv.conf");

        #
        #  Now run the update command.
        #
        system("chroot $tmp /usr/bin/apt-get update");


        #
        #  Now upgrade
        #
        system(
            "DEBIAN_FRONTEND=noninteractive  chroot $tmp /usr/bin/apt-get upgrade --yes --force-yes"
        );

        #
        #  Restore domU's resolv.conf if needed
        #
        if (-f "$tmp/etc/resolv.conf") {
            mv("$tmp/etc/resolv.conf.old", "$tmp/etc/resolv.conf");
        } else {
            unlink "$tmp/etc/resolv.conf";
        }
    }
    else
    {
        print "Xen image $name is not a Debian GNU/Linux image.  Skipping\n";
    }


    #
    #  Unmount
    #
    `umount -l $tmp`;
    `umount $tmp 2>/dev/null >/dev/null`;

}



=begin doc

  Test to see if the given instance is running.

=end doc

=cut

sub xenRunning
{
    my ($hostname) = (@_);

    my $running = 0;

    open( CMD, "xm list 2>/dev/null |" ) or
      die "Failed to run 'xm list $hostname'";
    while (<CMD>)
    {
        my $line = $_;
        $running = 1 if ( $line =~ /\Q$hostname\E/ );
    }
    close(CMD);

    return ($running);
}



=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+$//;

            # command expansion?
            if ( $val =~ /(.*)`([^`]+)`(.*)/ )
            {

                # store
                my $pre  = $1;
                my $cmd  = $2;
                my $post = $3;

                # get output
                my $output = `$cmd`;
                chomp($output);

                # build up replacement.
                $val = $pre . $output . $post;
            }

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

    close(FILE);
}



=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( "dir=s",  \$CONFIG{ 'dir' },
                "lvm=s",   \$CONFIG{ 'lvm' },
                "evms=s", \$CONFIG{ 'evms' },
                "help",    \$HELP,
                "manual", \$MANUAL,
                "version", \$VERSION );

    pod2usage(1) if $HELP;
    pod2usage( -verbose => 2 ) if $MANUAL;

    if ($VERSION)
    {
        my $REVISION = '$Revision: 1.29 $';

        if ( $REVISION =~ /1.([0-9.]+) / )
        {
            $REVISION = $1;
        }
        print "xen-update-image release $RELEASE - CVS: $REVISION\n";
        exit;

    }
}



=begin doc

  Test that the options we received from the command line, or our
 configuration file, make sense.

=end doc

=cut

sub checkArguments
{

    #
    #  Make sure we got one and only one installation method.
    #
    my $count = 0;
    foreach my $type (qw/dir lvm evms/)
    {
        $count += 1 if defined( $CONFIG{ $type } );
    }

    #
    #  Show a decent error for when either zero or more than one options
    # were selected.
    #
    if ( $count != 1 )
    {
        print "Please select one and only one of the installation methods:\n";
        print " --dir\n";
        print " --evms\n";
        print " --lvm\n";
        exit;
    }
}