1 package PVE
::AbstractMigrate
;
5 use POSIX
qw(strftime);
9 use PVE
::DataCenterConfig
;
10 use PVE
::ReplicationState
;
13 my ($level, $msg) = @_;
21 my $tstr = strftime
("%F %H:%M:%S", localtime);
23 foreach my $line (split (/\n/, $msg)) {
24 if ($level eq 'err') {
25 $res .= "$tstr ERROR: $line\n";
27 $res .= "$tstr $line\n";
35 my ($self, $level, $msg) = @_;
41 print &$msg2text($level, $msg);
45 my ($self, $cmd, %param) = @_;
49 $self->log('info', $line);
52 $self->log('info', "# " . PVE
::Tools
::cmd2string
($cmd));
54 PVE
::Tools
::run_command
($cmd, %param, outfunc
=> $logfunc, errfunc
=> $logfunc);
57 my $run_command_quiet_full = sub {
58 my ($self, $cmd, $logerr, %param) = @_;
63 $log .= &$msg2text('info', $line);;
66 eval { PVE
::Tools
::run_command
($cmd, %param, outfunc
=> $logfunc, errfunc
=> $logfunc); };
68 $self->log('info', "# " . PVE
::Tools
::cmd2string
($cmd));
72 $self->log('err', $err);
80 my ($self, $cmd, %param) = @_;
81 return &$run_command_quiet_full($self, $cmd, 0, %param);
85 my ($self, $cmd, %param) = @_;
86 return &$run_command_quiet_full($self, $cmd, 1, %param);
90 my ($self, $func, @param) = @_;
97 local $SIG{PIPE
} = sub {
98 $self->{delayed_interrupt
} = 0;
99 die "interrupted by signal\n";
102 my $di = $self->{delayed_interrupt
};
103 $self->{delayed_interrupt
} = 0;
105 die "interrupted by signal\n" if $di;
107 &$func($self, @param);
111 # FIXME: nodeip is now unused
113 my ($class, $node, $nodeip, $vmid, $opts) = @_;
115 $class = ref($class) || $class;
117 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
119 my $migration_network = $opts->{migration_network
};
120 if (!defined($migration_network)) {
121 $migration_network = $dc_conf->{migration
}->{network
};
123 my $ssh_info = PVE
::Cluster
::get_ssh_info
($node, $migration_network);
124 $nodeip = $ssh_info->{ip
};
126 my $migration_type = 'secure';
127 if (defined($opts->{migration_type
})) {
128 $migration_type = $opts->{migration_type
};
129 } elsif (defined($dc_conf->{migration
}->{type
})) {
130 $migration_type = $dc_conf->{migration
}->{type
};
132 $opts->{migration_type
} = $migration_type;
135 delayed_interrupt
=> 0,
139 ssh_info
=> $ssh_info,
141 rem_ssh
=> PVE
::Cluster
::ssh_info_to_command
($ssh_info)
144 $self = bless $self, $class;
146 my $starttime = time();
152 local $SIG{PIPE
} = sub {
153 $self->log('err', "received interrupt - delayed");
154 $self->{delayed_interrupt
} = 1;
157 # lock container during migration
158 eval { $self->lock_vm($self->{vmid
}, sub {
160 $self->{running
} = 0;
161 &$eval_int($self, sub { $self->{running
} = $self->prepare($self->{vmid
}); });
164 if (defined($migration_network)) {
165 $self->log('info', "use dedicated network address for sending " .
166 "migration traffic ($self->{nodeip})");
168 # test if we can connect to new IP
169 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
170 eval { $self->cmd_quiet($cmd); };
171 die "Can't connect to destination address ($self->{nodeip}) using " .
172 "public key authentication\n" if $@;
175 &$eval_int($self, sub { $self->phase1($self->{vmid
}); });
178 $self->log('err', $err);
179 eval { $self->phase1_cleanup($self->{vmid
}, $err); };
180 if (my $tmperr = $@) {
181 $self->log('err', $tmperr);
183 eval { $self->final_cleanup($self->{vmid
}); };
184 if (my $tmperr = $@) {
185 $self->log('err', $tmperr);
190 # vm is now owned by other node
191 # Note: there is no VM config file on the local node anymore
193 if ($self->{running
}) {
195 &$eval_int($self, sub { $self->phase2($self->{vmid
}); });
199 $self->log('err', "online migrate failure - $phase2err");
201 eval { $self->phase2_cleanup($self->{vmid
}, $phase2err); };
203 $self->log('err', $err);
209 &$eval_int($self, sub { $self->phase3($self->{vmid
}); });
212 $self->log('err', $phase3err);
215 eval { $self->phase3_cleanup($self->{vmid
}, $phase3err); };
217 $self->log('err', $err);
220 eval { $self->final_cleanup($self->{vmid
}); };
222 $self->log('err', $err);
229 my $delay = time() - $starttime;
230 my $mins = int($delay/60);
231 my $secs = $delay - $mins*60;
232 my $hours = int($mins/60);
233 $mins = $mins - $hours*60;
235 my $duration = sprintf "%02d:%02d:%02d", $hours, $mins, $secs;
238 $self->log('err', "migration aborted (duration $duration): $err");
239 die "migration aborted\n";
242 if ($self->{errors
}) {
243 $self->log('err', "migration finished with problems (duration $duration)");
244 die "migration problems\n"
247 $self->log('info', "migration finished successfully (duration $duration)");
251 my ($self, $vmid, $code, @param) = @_;
253 die "abstract method - implement me";
257 my ($self, $vmid) = @_;
259 die "abstract method - implement me";
264 # transfer all data and move VM config files
266 my ($self, $vmid) = @_;
267 die "abstract method - implement me";
270 # only called if there are errors in phase1
272 my ($self, $vmid, $err) = @_;
273 die "abstract method - implement me";
276 # only called when VM is running and phase1 was successful
278 my ($self, $vmid) = @_;
279 die "abstract method - implement me";
282 # only called when VM is running and phase1 was successful
284 my ($self, $vmid, $err) = @_;
287 # only called when phase1 was successful
289 my ($self, $vmid) = @_;
292 # only called when phase1 was successful
294 my ($self, $vmid, $err) = @_;
297 # final cleanup - always called
299 my ($self, $vmid) = @_;
300 die "abstract method - implement me";
303 # transfer replication state helper - call this before moving the guest config
304 # - just log the error is something fails
305 sub transfer_replication_state
{
308 my $local_node = PVE
::INotify
::nodename
();
311 my $stateobj = PVE
::ReplicationState
::read_state
();
312 my $vmstate = PVE
::ReplicationState
::extract_vmid_tranfer_state
($stateobj, $self->{vmid
}, $self->{node
}, $local_node);
313 # This have to be quoted when it run it over ssh.
314 my $state = PVE
::Tools
::shellquote
(encode_json
($vmstate));
315 my $cmd = [ @{$self->{rem_ssh
}}, 'pvesr', 'set-state', $self->{vmid
}, $state];
319 $self->log('err', "transfer replication state failed - $err");
324 # switch replication job target - call this after moving the guest config
325 # - does nothing if there is no relevant replication job
326 # - just log the error is something fails
327 sub switch_replication_job_target
{
330 my $local_node = PVE
::INotify
::nodename
();
332 eval { PVE
::ReplicationConfig
::switch_replication_job_target
($self->{vmid
}, $self->{node
}, $local_node); };
334 $self->log('err', "switch replication job target failed - $err");