Subversion Repositories sysadmin_scripts

Rev

Rev 115 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
114 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
use Data::Dumper;
6
 
7
 
8
my $target = shift;
9
my @toProcess = @ARGV;
10
 
11
my $hypervisor = 'virsh'; $hypervisor = 'xl' unless `which $hypervisor`;
12
 
13
die "Could not determine hypervisor\n" unless `which $hypervisor`;
14
 
15
 
16
sub checkConnection {
17
   my ($hypervisor,$target) = @_;
18
   if ( $hypervisor eq 'virsh' ) {
19
      die "Could not make a connection to $target\n" unless `virsh -c qemu+ssh://$target/system list`;
20
   } else {
21
      die "I don't know how to migrate Xen\n";
22
   }
23
}
24
 
25
sub trim {
26
   my $string = shift;
27
   $string =~ s/^\s+|\s+$//;
28
   return $string;
29
}
30
 
31
# get the name and ID of all virtuals running on this machine
32
sub getRunningVirtuals {
33
   my $hypervisor = shift;
34
   my %virtuals;
35
   if ( $hypervisor eq 'virsh' ) {
36
      my @output = `virsh list`;
37
      my @headers = split( '\s+', lc( trim( $output[0] ) ) );
38
#      die join( "\n", @headers ) . "\n"; 
39
      for (my $line = 1; $line < @output; $line++ ) {
40
         next if $output[$line] =~ m/^[^\s]*$/; # skip line of all dashes or empty
41
         my %temp;
42
         @temp{@headers} = split( '\s+', trim( $output[$line] ) );
43
         $virtuals{$temp{'name'}} = $temp{'id'};
44
      }
45
   } else {
46
      die "I don't know how to get a list of virtuals for $hypervisor\n";
47
   }
48
   return \%virtuals;
49
}
50
 
51
 
52
sub checkVirtual {
53
   my ($runningDomains, $domain ) = @_;
54
   # if they gave us a valid name, just return it
55
   return $domain if $runningDomains->{$domain};
56
   if ( $domain =~ m/^\d+$/ ) { # they passed a number, is it an ID?
57
      foreach my $this ( keys %$runningDomains ) {
58
         # if it is the id, return the name
59
         return $this if ( $runningDomains->{$this} == $domain );
60
      }
61
   }
62
   die "The domain, $domain, was not found on this machine\n";
63
}
64
 
65
&checkConnection( $hypervisor,$target );
66
my $running = &getRunningVirtuals( $hypervisor );
67
 
68
if ( $toProcess[0] eq 'ALL' ) {
69
   print "Warning, you are moving everything! Type 'yes' to continue ";
70
   my $yesno = <STDIN>;
71
   chomp $yesno;
72
   die unless ( lc $yesno eq 'yes' );
73
   @toProcess = keys %$running;
74
}
75
 
76
 
77
while ( my $domain = shift @toProcess ) {
78
   my $domainName = &checkVirtual( $running, $domain );
79
   print "\n==Migrating $domainName to $target\n";
80
   print "\tvirsh migrate --live --persistent --verbose  $domain qemu+ssh://$target/system\n";
115 rodolico 81
   `virsh migrate --live --persistent --verbose  $domain qemu+ssh://$target/system`
114 rodolico 82
}