]>
git.proxmox.com Git - pve-common.git/blob - data/PVE/AbstractMigrate.pm
ccfcfb69b2d716d2edc911b0f2b20c8ff59d99b0
1 package PVE
::AbstractMigrate
;
5 use POSIX
qw(strftime);
9 my ($level, $msg) = @_;
17 my $tstr = strftime
("%b %d %H:%M:%S", localtime);
19 foreach my $line (split (/\n/, $msg)) {
20 if ($level eq 'err') {
21 $res .= "$tstr ERROR: $line\n";
23 $res .= "$tstr $line\n";
31 my ($self, $level, $msg) = @_;
37 print &$msg2text($level, $msg);
41 my ($self, $cmd, %param) = @_;
45 $self->log('info', $line);
48 $self->log('info', "# " . PVE
::Tools
::cmd2string
($cmd));
50 PVE
::Tools
::run_command
($cmd, %param, outfunc
=> $logfunc, errfunc
=> $logfunc);
53 my $run_command_quiet_full = sub {
54 my ($self, $cmd, $logerr, %param) = @_;
59 $log .= &$msg2text('info', $line);;
62 eval { PVE
::Tools
::run_command
($cmd, %param, outfunc
=> $logfunc, errfunc
=> $logfunc); };
64 $self->log('info', "# " . PVE
::Tools
::cmd2string
($cmd));
68 $self->log('err', $err);
76 my ($self, $cmd, %param) = @_;
77 return &$run_command_quiet_full($self, $cmd, 0, %param);
81 my ($self, $cmd, %param) = @_;
82 return &$run_command_quiet_full($self, $cmd, 1, %param);
86 my ($self, $func, @param) = @_;
89 local $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = sub {
90 $self->{delayed_interrupt
} = 0;
91 die "interrupted by signal\n";
93 local $SIG{PIPE
} = sub {
94 $self->{delayed_interrupt
} = 0;
95 die "interrupted by signal\n";
98 my $di = $self->{delayed_interrupt
};
99 $self->{delayed_interrupt
} = 0;
101 die "interrupted by signal\n" if $di;
103 &$func($self, @param);
107 # blowfish is a fast block cipher, much faster then 3des
108 my @ssh_opts = ('-c', 'blowfish', '-o', 'BatchMode=yes');
109 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
110 my @scp_cmd = ('/usr/bin/scp', @ssh_opts);
111 my @rsync_opts = ('-aHAX', '--delete', '--numeric-ids');
112 my @rsync_cmd = ('/usr/bin/rsync', @rsync_opts);
115 my ($class, $node, $nodeip, $vmid, $opts) = @_;
117 $class = ref($class) || $class;
120 delayed_interrupt
=> 0,
125 rsync_cmd
=> [ @rsync_cmd ],
126 rem_ssh
=> [ @ssh_cmd, "root\@$nodeip" ],
127 scp_cmd
=> [ @scp_cmd ],
130 $self = bless $self, $class;
132 my $starttime = time();
134 local $ENV{RSYNC_RSH
} = join(' ', @ssh_cmd);
136 local $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
137 $self->log('err', "received interrupt - delayed");
138 $self->{delayed_interrupt
} = 1;
141 local $ENV{RSYNC_RSH
} = join(' ', @ssh_cmd);
143 # lock container during migration
144 eval { $self->lock_vm($self->{vmid
}, sub {
146 $self->{running
} = 0;
147 &$eval_int($self, sub { $self->{running
} = $self->prepare($self->{vmid
}); });
150 &$eval_int($self, sub { $self->phase1($self->{vmid
}); });
153 $self->log('err', $err);
154 eval { $self->phase1_cleanup($self->{vmid
}, $err); };
155 if (my $tmperr = $@) {
156 $self->log('err', $tmperr);
158 eval { $self->final_cleanup($self->{vmid
}); };
159 if (my $tmperr = $@) {
160 $self->log('err', $tmperr);
165 # vm is now owned by other node
166 # Note: there is no VM config file on the local node anymore
168 if ($self->{running
}) {
170 &$eval_int($self, sub { $self->phase2($self->{vmid
}); });
174 $self->log('err', "online migrate failure - $phase2err");
176 eval { $self->phase2_cleanup($self->{vmid
}, $phase2err); };
178 $self->log('err', $err);
184 &$eval_int($self, sub { $self->phase3($self->{vmid
}); });
187 $self->log('err', $phase3err);
190 eval { $self->phase3_cleanup($self->{vmid
}, $phase3err); };
192 $self->log('err', $err);
195 eval { $self->final_cleanup($self->{vmid
}); };
197 $self->log('err', $err);
204 my $delay = time() - $starttime;
205 my $mins = int($delay/60);
206 my $secs = $delay - $mins*60;
207 my $hours = int($mins/60);
208 $mins = $mins - $hours*60;
210 my $duration = sprintf "%02d:%02d:%02d", $hours, $mins, $secs;
213 $self->log('err', "migration aborted (duration $duration): $err");
214 die "migration aborted\n";
217 if ($self->{errors
}) {
218 $self->log('err', "migration finished with problems (duration $duration)");
219 die "migration problems\n"
222 $self->log('info', "migration finished successfuly (duration $duration)");
226 my ($self, $vmid, $code, @param) = @_;
228 die "abstract method - implement me";
232 my ($self, $vmid) = @_;
234 die "abstract method - implement me";
239 # transfer all data and move VM config files
241 my ($self, $vmid) = @_;
242 die "abstract method - implement me";
245 # only called if there are errors in phase1
247 my ($self, $vmid, $err) = @_;
248 die "abstract method - implement me";
251 # only called when VM is running and phase1 was successful
253 my ($self, $vmid) = @_;
254 die "abstract method - implement me";
257 # only called when VM is running and phase1 was successful
259 my ($self, $vmid, $err) = @_;
262 # only called when phase1 was successful
264 my ($self, $vmid) = @_;
267 # only called when phase1 was successful
269 my ($self, $vmid, $err) = @_;
272 # final cleanup - always called
274 my ($self, $vmid) = @_;
275 die "abstract method - implement me";