]>
git.proxmox.com Git - pve-guest-common.git/blob - PVE/AbstractMigrate.pm
1 package PVE
::AbstractMigrate
;
5 use POSIX
qw(strftime);
10 my ($level, $msg) = @_;
18 my $tstr = strftime
("%F %H:%M:%S", localtime);
20 foreach my $line (split (/\n/, $msg)) {
21 if ($level eq 'err') {
22 $res .= "$tstr ERROR: $line\n";
24 $res .= "$tstr $line\n";
32 my ($self, $level, $msg) = @_;
38 print &$msg2text($level, $msg);
42 my ($self, $cmd, %param) = @_;
46 $self->log('info', $line);
49 $self->log('info', "# " . PVE
::Tools
::cmd2string
($cmd));
51 PVE
::Tools
::run_command
($cmd, %param, outfunc
=> $logfunc, errfunc
=> $logfunc);
54 my $run_command_quiet_full = sub {
55 my ($self, $cmd, $logerr, %param) = @_;
60 $log .= &$msg2text('info', $line);;
63 eval { PVE
::Tools
::run_command
($cmd, %param, outfunc
=> $logfunc, errfunc
=> $logfunc); };
65 $self->log('info', "# " . PVE
::Tools
::cmd2string
($cmd));
69 $self->log('err', $err);
77 my ($self, $cmd, %param) = @_;
78 return &$run_command_quiet_full($self, $cmd, 0, %param);
82 my ($self, $cmd, %param) = @_;
83 return &$run_command_quiet_full($self, $cmd, 1, %param);
87 my ($self, $func, @param) = @_;
90 local $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = sub {
91 $self->{delayed_interrupt
} = 0;
92 die "interrupted by signal\n";
94 local $SIG{PIPE
} = sub {
95 $self->{delayed_interrupt
} = 0;
96 die "interrupted by signal\n";
99 my $di = $self->{delayed_interrupt
};
100 $self->{delayed_interrupt
} = 0;
102 die "interrupted by signal\n" if $di;
104 &$func($self, @param);
108 my @ssh_opts = ('-o', 'BatchMode=yes');
109 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
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();
148 local $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
149 $self->log('err', "received interrupt - delayed");
150 $self->{delayed_interrupt
} = 1;
153 # lock container during migration
154 eval { $self->lock_vm($self->{vmid
}, sub {
156 $self->{running
} = 0;
157 &$eval_int($self, sub { $self->{running
} = $self->prepare($self->{vmid
}); });
160 if (defined($migration_network)) {
161 $self->log('info', "use dedicated network address for sending " .
162 "migration traffic ($self->{nodeip})");
164 # test if we can connect to new IP
165 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
166 eval { $self->cmd_quiet($cmd); };
167 die "Can't connect to destination address ($self->{nodeip}) using " .
168 "public key authentication\n" if $@;
171 &$eval_int($self, sub { $self->phase1($self->{vmid
}); });
174 $self->log('err', $err);
175 eval { $self->phase1_cleanup($self->{vmid
}, $err); };
176 if (my $tmperr = $@) {
177 $self->log('err', $tmperr);
179 eval { $self->final_cleanup($self->{vmid
}); };
180 if (my $tmperr = $@) {
181 $self->log('err', $tmperr);
186 # vm is now owned by other node
187 # Note: there is no VM config file on the local node anymore
189 if ($self->{running
}) {
191 &$eval_int($self, sub { $self->phase2($self->{vmid
}); });
195 $self->log('err', "online migrate failure - $phase2err");
197 eval { $self->phase2_cleanup($self->{vmid
}, $phase2err); };
199 $self->log('err', $err);
205 &$eval_int($self, sub { $self->phase3($self->{vmid
}); });
208 $self->log('err', $phase3err);
211 eval { $self->phase3_cleanup($self->{vmid
}, $phase3err); };
213 $self->log('err', $err);
216 eval { $self->final_cleanup($self->{vmid
}); };
218 $self->log('err', $err);
225 my $delay = time() - $starttime;
226 my $mins = int($delay/60);
227 my $secs = $delay - $mins*60;
228 my $hours = int($mins/60);
229 $mins = $mins - $hours*60;
231 my $duration = sprintf "%02d:%02d:%02d", $hours, $mins, $secs;
234 $self->log('err', "migration aborted (duration $duration): $err");
235 die "migration aborted\n";
238 if ($self->{errors
}) {
239 $self->log('err', "migration finished with problems (duration $duration)");
240 die "migration problems\n"
243 $self->log('info', "migration finished successfully (duration $duration)");
247 my ($self, $vmid, $code, @param) = @_;
249 die "abstract method - implement me";
253 my ($self, $vmid) = @_;
255 die "abstract method - implement me";
260 # transfer all data and move VM config files
262 my ($self, $vmid) = @_;
263 die "abstract method - implement me";
266 # only called if there are errors in phase1
268 my ($self, $vmid, $err) = @_;
269 die "abstract method - implement me";
272 # only called when VM is running and phase1 was successful
274 my ($self, $vmid) = @_;
275 die "abstract method - implement me";
278 # only called when VM is running and phase1 was successful
280 my ($self, $vmid, $err) = @_;
283 # only called when phase1 was successful
285 my ($self, $vmid) = @_;
288 # only called when phase1 was successful
290 my ($self, $vmid, $err) = @_;
293 # final cleanup - always called
295 my ($self, $vmid) = @_;
296 die "abstract method - implement me";