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 # FIXME: nodeip is now unused
112 my ($class, $node, $nodeip, $vmid, $opts) = @_;
114 $class = ref($class) || $class;
116 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
118 my $migration_network = $opts->{migration_network
};
119 if (!defined($migration_network)) {
120 $migration_network = $dc_conf->{migration
}->{network
};
122 my $ssh_info = PVE
::Cluster
::get_ssh_info
($node, $migration_network);
123 $nodeip = $ssh_info->{ip
};
125 my $migration_type = 'secure';
126 if (defined($opts->{migration_type
})) {
127 $migration_type = $opts->{migration_type
};
128 } elsif (defined($dc_conf->{migration
}->{type
})) {
129 $migration_type = $dc_conf->{migration
}->{type
};
131 $opts->{migration_type
} = $migration_type;
134 delayed_interrupt
=> 0,
138 ssh_info
=> $ssh_info,
140 rem_ssh
=> PVE
::Cluster
::ssh_info_to_command
($ssh_info)
143 $self = bless $self, $class;
145 my $starttime = time();
151 local $SIG{PIPE
} = sub {
152 $self->log('err', "received interrupt - delayed");
153 $self->{delayed_interrupt
} = 1;
156 # lock container during migration
157 eval { $self->lock_vm($self->{vmid
}, sub {
159 $self->{running
} = 0;
160 &$eval_int($self, sub { $self->{running
} = $self->prepare($self->{vmid
}); });
163 if (defined($migration_network)) {
164 $self->log('info', "use dedicated network address for sending " .
165 "migration traffic ($self->{nodeip})");
167 # test if we can connect to new IP
168 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
169 eval { $self->cmd_quiet($cmd); };
170 die "Can't connect to destination address ($self->{nodeip}) using " .
171 "public key authentication\n" if $@;
174 &$eval_int($self, sub { $self->phase1($self->{vmid
}); });
177 $self->log('err', $err);
178 eval { $self->phase1_cleanup($self->{vmid
}, $err); };
179 if (my $tmperr = $@) {
180 $self->log('err', $tmperr);
182 eval { $self->final_cleanup($self->{vmid
}); };
183 if (my $tmperr = $@) {
184 $self->log('err', $tmperr);
189 # vm is now owned by other node
190 # Note: there is no VM config file on the local node anymore
192 if ($self->{running
}) {
194 &$eval_int($self, sub { $self->phase2($self->{vmid
}); });
198 $self->log('err', "online migrate failure - $phase2err");
200 eval { $self->phase2_cleanup($self->{vmid
}, $phase2err); };
202 $self->log('err', $err);
208 &$eval_int($self, sub { $self->phase3($self->{vmid
}); });
211 $self->log('err', $phase3err);
214 eval { $self->phase3_cleanup($self->{vmid
}, $phase3err); };
216 $self->log('err', $err);
219 eval { $self->final_cleanup($self->{vmid
}); };
221 $self->log('err', $err);
228 my $delay = time() - $starttime;
229 my $mins = int($delay/60);
230 my $secs = $delay - $mins*60;
231 my $hours = int($mins/60);
232 $mins = $mins - $hours*60;
234 my $duration = sprintf "%02d:%02d:%02d", $hours, $mins, $secs;
237 $self->log('err', "migration aborted (duration $duration): $err");
238 die "migration aborted\n";
241 if ($self->{errors
}) {
242 $self->log('err', "migration finished with problems (duration $duration)");
243 die "migration problems\n"
246 $self->log('info', "migration finished successfully (duration $duration)");
250 my ($self, $vmid, $code, @param) = @_;
252 die "abstract method - implement me";
256 my ($self, $vmid) = @_;
258 die "abstract method - implement me";
263 # transfer all data and move VM config files
265 my ($self, $vmid) = @_;
266 die "abstract method - implement me";
269 # only called if there are errors in phase1
271 my ($self, $vmid, $err) = @_;
272 die "abstract method - implement me";
275 # only called when VM is running and phase1 was successful
277 my ($self, $vmid) = @_;
278 die "abstract method - implement me";
281 # only called when VM is running and phase1 was successful
283 my ($self, $vmid, $err) = @_;
286 # only called when phase1 was successful
288 my ($self, $vmid) = @_;
291 # only called when phase1 was successful
293 my ($self, $vmid, $err) = @_;
296 # final cleanup - always called
298 my ($self, $vmid) = @_;
299 die "abstract method - implement me";
302 # transfer replication state helper - call this before moving the guest config
303 # - just log the error is something fails
304 sub transfer_replication_state
{
307 my $local_node = PVE
::INotify
::nodename
();
310 my $stateobj = PVE
::ReplicationState
::read_state
();
311 my $vmstate = PVE
::ReplicationState
::extract_vmid_tranfer_state
($stateobj, $self->{vmid
}, $self->{node
}, $local_node);
312 # This have to be quoted when it run it over ssh.
313 my $state = PVE
::Tools
::shellquote
(encode_json
($vmstate));
314 my $cmd = [ @{$self->{rem_ssh
}}, 'pvesr', 'set-state', $self->{vmid
}, $state];
318 $self->log('err', "transfer replication state failed - $err");
323 # switch replication job target - call this after moving the guest config
324 # - does nothing if there is no relevant replication job
325 # - just log the error is something fails
326 sub switch_replication_job_target
{
329 my $local_node = PVE
::INotify
::nodename
();
331 eval { PVE
::ReplicationConfig
::switch_replication_job_target
($self->{vmid
}, $self->{node
}, $local_node); };
333 $self->log('err', "switch replication job target failed - $err");