]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/AbstractMigrate.pm
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 my @ssh_opts = ('-o', 'BatchMode=yes');
108 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
109 my @scp_cmd = ('/usr/bin/scp', @ssh_opts);
110 my @rsync_opts = ('-aHAX', '--delete', '--numeric-ids');
111 my @rsync_cmd = ('/usr/bin/rsync', @rsync_opts);
114 my ($class, $node, $nodeip, $vmid, $opts) = @_;
116 $class = ref($class) || $class;
119 delayed_interrupt
=> 0,
124 rsync_cmd
=> [ @rsync_cmd ],
125 rem_ssh
=> [ @ssh_cmd, "root\@$nodeip" ],
126 scp_cmd
=> [ @scp_cmd ],
129 $self = bless $self, $class;
131 my $starttime = time();
133 local $ENV{RSYNC_RSH
} = join(' ', @ssh_cmd);
135 local $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
136 $self->log('err', "received interrupt - delayed");
137 $self->{delayed_interrupt
} = 1;
140 local $ENV{RSYNC_RSH
} = join(' ', @ssh_cmd);
142 # lock container during migration
143 eval { $self->lock_vm($self->{vmid
}, sub {
145 $self->{running
} = 0;
146 &$eval_int($self, sub { $self->{running
} = $self->prepare($self->{vmid
}); });
149 &$eval_int($self, sub { $self->phase1($self->{vmid
}); });
152 $self->log('err', $err);
153 eval { $self->phase1_cleanup($self->{vmid
}, $err); };
154 if (my $tmperr = $@) {
155 $self->log('err', $tmperr);
157 eval { $self->final_cleanup($self->{vmid
}); };
158 if (my $tmperr = $@) {
159 $self->log('err', $tmperr);
164 # vm is now owned by other node
165 # Note: there is no VM config file on the local node anymore
167 if ($self->{running
}) {
169 &$eval_int($self, sub { $self->phase2($self->{vmid
}); });
173 $self->log('err', "online migrate failure - $phase2err");
175 eval { $self->phase2_cleanup($self->{vmid
}, $phase2err); };
177 $self->log('err', $err);
183 &$eval_int($self, sub { $self->phase3($self->{vmid
}); });
186 $self->log('err', $phase3err);
189 eval { $self->phase3_cleanup($self->{vmid
}, $phase3err); };
191 $self->log('err', $err);
194 eval { $self->final_cleanup($self->{vmid
}); };
196 $self->log('err', $err);
203 my $delay = time() - $starttime;
204 my $mins = int($delay/60);
205 my $secs = $delay - $mins*60;
206 my $hours = int($mins/60);
207 $mins = $mins - $hours*60;
209 my $duration = sprintf "%02d:%02d:%02d", $hours, $mins, $secs;
212 $self->log('err', "migration aborted (duration $duration): $err");
213 die "migration aborted\n";
216 if ($self->{errors
}) {
217 $self->log('err', "migration finished with problems (duration $duration)");
218 die "migration problems\n"
221 $self->log('info', "migration finished successfully (duration $duration)");
225 my ($self, $vmid, $code, @param) = @_;
227 die "abstract method - implement me";
231 my ($self, $vmid) = @_;
233 die "abstract method - implement me";
238 # transfer all data and move VM config files
240 my ($self, $vmid) = @_;
241 die "abstract method - implement me";
244 # only called if there are errors in phase1
246 my ($self, $vmid, $err) = @_;
247 die "abstract method - implement me";
250 # only called when VM is running and phase1 was successful
252 my ($self, $vmid) = @_;
253 die "abstract method - implement me";
256 # only called when VM is running and phase1 was successful
258 my ($self, $vmid, $err) = @_;
261 # only called when phase1 was successful
263 my ($self, $vmid) = @_;
266 # only called when phase1 was successful
268 my ($self, $vmid, $err) = @_;
271 # final cleanup - always called
273 my ($self, $vmid) = @_;
274 die "abstract method - implement me";