1 package PVE
::Replication
;
7 use Time
::HiRes
qw(gettimeofday tv_interval);
14 use PVE
::GuestHelpers
;
15 use PVE
::ReplicationConfig
;
16 use PVE
::ReplicationState
;
19 # regression tests should overwrite this
25 sub remote_prepare_local_job
{
26 my ($ssh_info, $jobid, $vmid, $volumes, $storeid_list, $last_sync, $parent_snapname, $force, $logfunc) = @_;
28 my $ssh_cmd = PVE
::Cluster
::ssh_info_to_command
($ssh_info);
29 my $cmd = [@$ssh_cmd, '--', 'pvesr', 'prepare-local-job', $jobid];
30 push @$cmd, '--scan', join(',', @$storeid_list) if scalar(@$storeid_list);
31 push @$cmd, @$volumes if scalar(@$volumes);
33 push @$cmd, '--last_sync', $last_sync;
34 push @$cmd, '--parent_snapname', $parent_snapname
36 push @$cmd, '--force' if $force;
42 $remote_snapshots = JSON
::decode_json
($line);
48 $logfunc->("(remote_prepare_local_job) $line");
51 PVE
::Tools
::run_command
($cmd, outfunc
=> $parser, errfunc
=> $logger);
53 die "prepare remote node failed - no result\n"
54 if !defined($remote_snapshots);
56 return $remote_snapshots;
59 sub remote_finalize_local_job
{
60 my ($ssh_info, $jobid, $vmid, $volumes, $last_sync, $logfunc) = @_;
62 my $ssh_cmd = PVE
::Cluster
::ssh_info_to_command
($ssh_info);
63 my $cmd = [@$ssh_cmd, '--', 'pvesr', 'finalize-local-job', $jobid,
64 @$volumes, '--last_sync', $last_sync];
69 $logfunc->("(remote_finalize_local_job) $line");
72 PVE
::Tools
::run_command
($cmd, outfunc
=> $logger, errfunc
=> $logger);
75 # finds local replication snapshots from $last_sync
76 # and removes all replication snapshots with other time stamps
78 my ($storecfg, $volids, $jobid, $last_sync, $parent_snapname, $logfunc) = @_;
82 my ($prefix, $snapname);
84 if (defined($jobid)) {
85 ($prefix, $snapname) = PVE
::ReplicationState
::replication_snapshot_name
($jobid, $last_sync);
87 $prefix = '__replicate_';
90 my $last_snapshots = {};
91 my $cleaned_replicated_volumes = {};
92 foreach my $volid (@$volids) {
93 my $list = PVE
::Storage
::volume_snapshot_list
($storecfg, $volid);
94 foreach my $snap (@$list) {
95 if ((defined($snapname) && ($snap eq $snapname)) ||
96 (defined($parent_snapname) && ($snap eq $parent_snapname))) {
97 $last_snapshots->{$volid}->{$snap} = 1;
98 } elsif ($snap =~ m/^\Q$prefix\E/) {
99 $logfunc->("delete stale replication snapshot '$snap' on $volid");
100 PVE
::Storage
::volume_snapshot_delete
($storecfg, $volid, $snap);
101 $cleaned_replicated_volumes->{$volid} = 1;
106 return wantarray ?
($last_snapshots, $cleaned_replicated_volumes) : $last_snapshots;
109 sub replicate_volume
{
110 my ($ssh_info, $storecfg, $volid, $base_snapshot, $sync_snapname, $rate, $insecure) = @_;
112 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
114 # fixme: handle $rate, $insecure ??
115 PVE
::Storage
::storage_migrate
($storecfg, $volid, $ssh_info, $storeid, $volname,
116 $base_snapshot, $sync_snapname);
121 my ($guest_class, $jobcfg, $state, $start_time, $logfunc) = @_;
123 my $local_node = PVE
::INotify
::nodename
();
125 die "not implemented - internal error" if $jobcfg->{type
} ne 'local';
127 my $dc_conf = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
129 my $migration_network;
130 my $migration_type = 'secure';
131 if (my $mc = $dc_conf->{migration
}) {
132 $migration_network = $mc->{network
};
133 $migration_type = $mc->{type
} if defined($mc->{type
});
136 my $jobid = $jobcfg->{id
};
137 my $storecfg = PVE
::Storage
::config
();
138 my $last_sync = $state->{last_sync
};
140 die "start time before last sync ($start_time <= $last_sync) - abort sync\n"
141 if $start_time <= $last_sync;
143 my $vmid = $jobcfg->{guest
};
144 my $vmtype = $jobcfg->{vmtype
};
146 my $conf = $guest_class->load_config($vmid);
147 my ($running, $freezefs) = $guest_class->__snapshot_check_freeze_needed($vmid, $conf, 0);
148 my $volumes = $guest_class->get_replicatable_volumes($storecfg, $conf, defined($jobcfg->{remove_job
}));
150 my $sorted_volids = [ sort keys %$volumes ];
152 $running //= 0; # to avoid undef warnings from logfunc
154 $logfunc->("guest => $vmid, type => $vmtype, running => $running");
155 $logfunc->("volumes => " . join(',', @$sorted_volids));
157 if (my $remove_job = $jobcfg->{remove_job
}) {
159 $logfunc->("start job removal - mode '${remove_job}'");
161 if ($remove_job eq 'full' && $jobcfg->{target
} ne $local_node) {
162 # remove all remote volumes
163 my $ssh_info = PVE
::Cluster
::get_ssh_info
($jobcfg->{target
});
164 remote_prepare_local_job
($ssh_info, $jobid, $vmid, [], $state->{storeid_list
}, 0, undef, 1, $logfunc);
167 # remove all local replication snapshots (lastsync => 0)
168 prepare
($storecfg, $sorted_volids, $jobid, 0, undef, $logfunc);
170 PVE
::ReplicationConfig
::delete_job
($jobid); # update config
171 $logfunc->("job removed");
176 my $ssh_info = PVE
::Cluster
::get_ssh_info
($jobcfg->{target
}, $migration_network);
178 my $last_sync_snapname =
179 PVE
::ReplicationState
::replication_snapshot_name
($jobid, $last_sync);
181 PVE
::ReplicationState
::replication_snapshot_name
($jobid, $start_time);
183 my $parent_snapname = $conf->{parent
};
185 # test if we have a replication_ snapshot from last sync
186 # and remove all other/stale replication snapshots
188 my $last_snapshots = prepare
(
189 $storecfg, $sorted_volids, $jobid, $last_sync, $parent_snapname, $logfunc);
191 # prepare remote side
192 my $remote_snapshots = remote_prepare_local_job
(
193 $ssh_info, $jobid, $vmid, $sorted_volids, $state->{storeid_list
}, $last_sync, $parent_snapname, 0, $logfunc);
195 my $storeid_hash = {};
196 foreach my $volid (@$sorted_volids) {
197 my ($storeid) = PVE
::Storage
::parse_volume_id
($volid);
198 $storeid_hash->{$storeid} = 1;
200 $state->{storeid_list
} = [ sort keys %$storeid_hash ];
202 # freeze filesystem for data consistency
204 $logfunc->("freeze guest filesystem");
205 $guest_class->__snapshot_freeze($vmid, 0);
208 # make snapshot of all volumes
209 my $replicate_snapshots = {};
211 foreach my $volid (@$sorted_volids) {
212 $logfunc->("create snapshot '${sync_snapname}' on $volid");
213 PVE
::Storage
::volume_snapshot
($storecfg, $volid, $sync_snapname);
214 $replicate_snapshots->{$volid} = 1;
219 # unfreeze immediately
221 $guest_class->__snapshot_freeze($vmid, 1);
224 my $cleanup_local_snapshots = sub {
225 my ($volid_hash, $snapname) = @_;
226 foreach my $volid (sort keys %$volid_hash) {
227 $logfunc->("delete previous replication snapshot '$snapname' on $volid");
228 eval { PVE
::Storage
::volume_snapshot_delete
($storecfg, $volid, $snapname); };
234 $cleanup_local_snapshots->($replicate_snapshots, $sync_snapname); # try to cleanup
240 my $rate = $jobcfg->{rate
};
241 my $insecure = $migration_type eq 'insecure';
243 foreach my $volid (@$sorted_volids) {
246 if (defined($last_snapshots->{$volid}) && defined($remote_snapshots->{$volid})) {
247 if ($last_snapshots->{$volid}->{$last_sync_snapname} &&
248 $remote_snapshots->{$volid}->{$last_sync_snapname}) {
249 $logfunc->("incremental sync '$volid' ($last_sync_snapname => $sync_snapname)");
250 $base_snapname = $last_sync_snapname;
251 } elsif (defined($parent_snapname) &&
252 ($last_snapshots->{$volid}->{$parent_snapname} &&
253 $remote_snapshots->{$volid}->{$parent_snapname})) {
254 $logfunc->("incremental sync '$volid' ($parent_snapname => $sync_snapname)");
255 $base_snapname = $parent_snapname;
259 $logfunc->("full sync '$volid' ($sync_snapname)") if !defined($base_snapname);
260 replicate_volume
($ssh_info, $storecfg, $volid, $base_snapname, $sync_snapname, $rate, $insecure);
266 $cleanup_local_snapshots->($replicate_snapshots, $sync_snapname); # try to cleanup
267 # we do not cleanup the remote side here - this is done in
268 # next run of prepare_local_job
272 # remove old snapshots because they are no longer needed
273 $cleanup_local_snapshots->($last_snapshots, $last_sync_snapname);
275 remote_finalize_local_job
($ssh_info, $jobid, $vmid, $sorted_volids, $start_time, $logfunc);
280 my $run_replication_nolock = sub {
281 my ($guest_class, $jobcfg, $iteration, $start_time, $logfunc) = @_;
283 my $jobid = $jobcfg->{id
};
285 # we normaly write errors into the state file,
286 # but we also catch unexpected errors and log them to syslog
287 # (for examply when there are problems writing the state file)
289 my $state = PVE
::ReplicationState
::read_job_state
($jobcfg);
291 my $t0 = [gettimeofday
];
294 $state->{ptime
} = PVE
::ProcFSTools
::read_proc_starttime
($state->{pid
});
295 $state->{last_node
} = PVE
::INotify
::nodename
();
296 $state->{last_try
} = $start_time;
297 $state->{last_iteration
} = $iteration;
298 $state->{storeid_list
} //= [];
300 PVE
::ReplicationState
::write_job_state
($jobcfg, $state);
302 mkdir $PVE::ReplicationState
::replicate_logdir
;
303 my $logfile = PVE
::ReplicationState
::job_logfile_name
($jobid);
304 open(my $logfd, '>', $logfile) ||
305 die "unable to open replication log '$logfile' - $!\n";
307 my $logfunc_wrapper = sub {
310 my $ctime = get_log_time
();
311 print $logfd "$ctime $jobid: $msg\n";
312 $logfunc->("$ctime $jobid: $msg") if $logfunc;
315 $logfunc_wrapper->("start replication job");
318 replicate
($guest_class, $jobcfg, $state, $start_time, $logfunc_wrapper);
322 $state->{duration
} = tv_interval
($t0);
323 delete $state->{pid
};
324 delete $state->{ptime
};
328 $state->{fail_count
}++;
329 $state->{error
} = "$err";
330 PVE
::ReplicationState
::write_job_state
($jobcfg, $state);
331 $logfunc_wrapper->("end replication job with error: $err");
333 $logfunc_wrapper->("end replication job");
334 $state->{last_sync
} = $start_time;
335 $state->{fail_count
} = 0;
336 delete $state->{error
};
337 PVE
::ReplicationState
::write_job_state
($jobcfg, $state);
343 warn "$jobid: got unexpected replication job error - $err";
347 sub run_replication
{
348 my ($guest_class, $jobcfg, $iteration, $start_time, $logfunc, $noerr) = @_;
351 my $timeout = 2; # do not wait too long - we repeat periodically anyways
352 PVE
::GuestHelpers
::guest_migration_lock
(
353 $jobcfg->{guest
}, $timeout, $run_replication_nolock,
354 $guest_class, $jobcfg, $iteration, $start_time, $logfunc);
357 return undef if $noerr;