Subversion Repositories sysadmin_scripts

Rev

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