1 package PVE
::AbstractMigrate
;
5 use POSIX
qw(strftime);
9 use PVE
::ReplicationState
;
12 my ($level, $msg) = @_;
20 my $tstr = strftime
("%F %H:%M:%S", localtime);
22 foreach my $line (split (/\n/, $msg)) {
23 if ($level eq 'err') {
24 $res .= "$tstr ERROR: $line\n";
26 $res .= "$tstr $line\n";
34 my ($self, $level, $msg) = @_;
40 print &$msg2text($level, $msg);
44 my ($self, $cmd, %param) = @_;
48 $self->log('info', $line);
51 $self->log('info', "# " . PVE
::Tools
::cmd2string
($cmd));
53 PVE
::Tools
::run_command
($cmd, %param, outfunc
=> $logfunc, errfunc
=> $logfunc);
56 my $run_command_quiet_full = sub {
57 my ($self, $cmd, $logerr, %param) = @_;
62 $log .= &$msg2text('info', $line);;
65 eval { PVE
::Tools
::run_command
($cmd, %param, outfunc
=> $logfunc, errfunc
=> $logfunc); };
67 $self->log('info', "# " . PVE
::Tools
::cmd2string
($cmd));
71 $self->log('err', $err);
79 my ($self, $cmd, %param) = @_;
80 return &$run_command_quiet_full($self, $cmd, 0, %param);
84 my ($self, $cmd, %param) = @_;
85 return &$run_command_quiet_full($self, $cmd, 1, %param);
89 my ($self, $func, @param) = @_;
96 local $SIG{PIPE
} = sub {
97 $self->{delayed_interrupt
} = 0;
98 die "interrupted by signal\n";
101 my $di = $self->{delayed_interrupt
};
102 $self->{delayed_interrupt
} = 0;
104 die "interrupted by signal\n" if $di;
106 &$func($self, @param);
110 my @ssh_opts = ('-o', 'BatchMode=yes');
111 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
113 # FIXME: nodeip is now unused
115 my ($class, $node, $nodeip, $vmid, $opts) = @_;
117 $class = ref($class) || $class;
119 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
121 my $migration_network = $opts->{migration_network
};
122 if (!defined($migration_network)) {
123 $migration_network = $dc_conf->{migration
}->{network
};
125 my $ssh_info = PVE
::Cluster
::get_ssh_info
($node, $migration_network);
126 $nodeip = $ssh_info->{ip
};
128 my $migration_type = 'secure';
129 if (defined($opts->{migration_type
})) {
130 $migration_type = $opts->{migration_type
};
131 } elsif (defined($dc_conf->{migration
}->{type
})) {
132 $migration_type = $dc_conf->{migration
}->{type
};
134 $opts->{migration_type
} = $migration_type;
137 delayed_interrupt
=> 0,
141 ssh_info
=> $ssh_info,
143 rem_ssh
=> PVE
::Cluster
::ssh_info_to_command
($ssh_info)
146 $self = bless $self, $class;
148 my $starttime = time();
154 local $SIG{PIPE
} = sub {
155 $self->log('err', "received interrupt - delayed");
156 $self->{delayed_interrupt
} = 1;
159 # lock container during migration
160 eval { $self->lock_vm($self->{vmid
}, sub {
162 $self->{running
} = 0;
163 &$eval_int($self, sub { $self->{running
} = $self->prepare($self->{vmid
}); });
166 if (defined($migration_network)) {
167 $self->log('info', "use dedicated network address for sending " .
168 "migration traffic ($self->{nodeip})");
170 # test if we can connect to new IP
171 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
172 eval { $self->cmd_quiet($cmd); };
173 die "Can't connect to destination address ($self->{nodeip}) using " .
174 "public key authentication\n" if $@;
177 &$eval_int($self, sub { $self->phase1($self->{vmid
}); });
180 $self->log('err', $err);
181 eval { $self->phase1_cleanup($self->{vmid
}, $err); };
182 if (my $tmperr = $@) {
183 $self->log('err', $tmperr);
185 eval { $self->final_cleanup($self->{vmid
}); };
186 if (my $tmperr = $@) {
187 $self->log('err', $tmperr);
192 # vm is now owned by other node
193 # Note: there is no VM config file on the local node anymore
195 if ($self->{running
}) {
197 &$eval_int($self, sub { $self->phase2($self->{vmid
}); });
201 $self->log('err', "online migrate failure - $phase2err");
203 eval { $self->phase2_cleanup($self->{vmid
}, $phase2err); };
205 $self->log('err', $err);
211 &$eval_int($self, sub { $self->phase3($self->{vmid
}); });
214 $self->log('err', $phase3err);
217 eval { $self->phase3_cleanup($self->{vmid
}, $phase3err); };
219 $self->log('err', $err);
222 eval { $self->final_cleanup($self->{vmid
}); };
224 $self->log('err', $err);
231 my $delay = time() - $starttime;
232 my $mins = int($delay/60);
233 my $secs = $delay - $mins*60;
234 my $hours = int($mins/60);
235 $mins = $mins - $hours*60;
237 my $duration = sprintf "%02d:%02d:%02d", $hours, $mins, $secs;
240 $self->log('err', "migration aborted (duration $duration): $err");
241 die "migration aborted\n";
244 if ($self->{errors
}) {
245 $self->log('err', "migration finished with problems (duration $duration)");
246 die "migration problems\n"
249 $self->log('info', "migration finished successfully (duration $duration)");
253 my ($self, $vmid, $code, @param) = @_;
255 die "abstract method - implement me";
259 my ($self, $vmid) = @_;
261 die "abstract method - implement me";
266 # transfer all data and move VM config files
268 my ($self, $vmid) = @_;
269 die "abstract method - implement me";
272 # only called if there are errors in phase1
274 my ($self, $vmid, $err) = @_;
275 die "abstract method - implement me";
278 # only called when VM is running and phase1 was successful
280 my ($self, $vmid) = @_;
281 die "abstract method - implement me";
284 # only called when VM is running and phase1 was successful
286 my ($self, $vmid, $err) = @_;
289 # only called when phase1 was successful
291 my ($self, $vmid) = @_;
294 # only called when phase1 was successful
296 my ($self, $vmid, $err) = @_;
299 # final cleanup - always called
301 my ($self, $vmid) = @_;
302 die "abstract method - implement me";
305 # transfer replication state helper - call this before moving the guest config
306 # - just log the error is something fails
307 sub transfer_replication_state
{
310 my $local_node = PVE
::INotify
::nodename
();
313 my $stateobj = PVE
::ReplicationState
::read_state
();
314 my $vmstate = PVE
::ReplicationState
::extract_vmid_tranfer_state
($stateobj, $self->{vmid
}, $self->{node
}, $local_node);
315 # This have to be quoted when it run it over ssh.
316 my $state = PVE
::Tools
::shellquote
(encode_json
($vmstate));
317 my $cmd = [ @{$self->{rem_ssh
}}, 'pvesr', 'set-state', $self->{vmid
}, $state];
321 $self->log('err', "transfer replication state failed - $err");
326 # switch replication job target - call this after moving the guest config
327 # - does nothing if there is no relevant replication job
328 # - just log the error is something fails
329 sub switch_replication_job_target
{
332 my $local_node = PVE
::INotify
::nodename
();
334 eval { PVE
::ReplicationConfig
::switch_replication_job_target
($self->{vmid
}, $self->{node
}, $local_node); };
336 $self->log('err', "switch replication job target failed - $err");