]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Migrate.pm
PVE::LXC::Migrate - use replication job, transfer replication state
[pve-container.git] / src / PVE / LXC / Migrate.pm
1 package PVE::LXC::Migrate;
2
3 use strict;
4 use warnings;
5 use PVE::AbstractMigrate;
6 use File::Basename;
7 use File::Copy; # fixme: remove
8 use PVE::Tools;
9 use PVE::INotify;
10 use PVE::Cluster;
11 use PVE::Storage;
12 use PVE::LXC::Config;
13 use PVE::LXC;
14 use PVE::ReplicationConfig;
15 use PVE::ReplicationState;
16 use PVE::Replication;
17
18 use base qw(PVE::AbstractMigrate);
19
20 sub lock_vm {
21 my ($self, $vmid, $code, @param) = @_;
22
23 return PVE::LXC::Config->lock_config($vmid, $code, @param);
24 }
25
26 sub prepare {
27 my ($self, $vmid) = @_;
28
29 my $online = $self->{opts}->{online};
30 my $restart= $self->{opts}->{restart};
31
32 $self->{storecfg} = PVE::Storage::config();
33
34 # test if CT exists
35 my $conf = $self->{vmconf} = PVE::LXC::Config->load_config($vmid);
36
37 PVE::LXC::Config->check_lock($conf);
38
39 my $running = 0;
40 if (PVE::LXC::check_running($vmid)) {
41 die "lxc live migration is currently not implemented\n" if $online;
42 die "running container can only be migrated in restart mode" if !$restart;
43 $running = 1;
44 }
45 $self->{was_running} = $running;
46
47 my $force = $self->{opts}->{force} // 0;
48 my $need_activate = [];
49
50 PVE::LXC::Config->foreach_mountpoint($conf, sub {
51 my ($ms, $mountpoint) = @_;
52
53 my $volid = $mountpoint->{volume};
54 my $type = $mountpoint->{type};
55
56 # skip dev/bind mps when forced / shared
57 if ($type ne 'volume') {
58 if ($force) {
59 warn "-force is deprecated, please use the 'shared' property on individual non-volume mount points instead!\n";
60 return;
61 }
62 if ($mountpoint->{shared}) {
63 return;
64 } else {
65 die "cannot migrate local $type mount point '$ms'\n";
66 }
67 }
68
69 my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid;
70 die "can't determine assigned storage for mount point '$ms'\n" if !$storage;
71
72 # check if storage is available on both nodes
73 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $storage);
74 PVE::Storage::storage_check_node($self->{storecfg}, $storage, $self->{node});
75
76
77 if ($scfg->{shared}) {
78 # PVE::Storage::activate_storage checks this for non-shared storages
79 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
80 warn "Used shared storage '$storage' is not online on source node!\n"
81 if !$plugin->check_connection($storage, $scfg);
82 } else {
83 # only activate if not shared
84 push @$need_activate, $volid;
85
86 # unless in restart mode because we shut the container down
87 die "unable to migrate local mount point '$volid' while CT is running"
88 if $running && !$restart;
89 }
90
91 });
92
93 PVE::Storage::activate_volumes($self->{storecfg}, $need_activate);
94
95 # todo: test if VM uses local resources
96
97 # test ssh connection
98 my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ];
99 eval { $self->cmd_quiet($cmd); };
100 die "Can't connect to destination address using public key\n" if $@;
101
102 # in restart mode, we shutdown the container before migrating
103 if ($restart && $running) {
104 my $timeout = $self->{opts}->{timeout} // 180;
105
106 $self->log('info', "shutdown CT $vmid\n");
107
108 my $cmd = ['lxc-stop', '-n', $vmid, '--timeout', $timeout];
109 $self->cmd($cmd, timeout => $timeout + 5);
110
111 # make sure container is stopped
112 $cmd = ['lxc-wait', '-n', $vmid, '-t', 5, '-s', 'STOPPED'];
113 $self->cmd($cmd);
114
115 $running = 0;
116 }
117
118 return $running;
119 }
120
121 sub phase1 {
122 my ($self, $vmid) = @_;
123
124 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
125
126 my $conf = $self->{vmconf};
127 $conf->{lock} = 'migrate';
128 PVE::LXC::Config->write_config($vmid, $conf);
129
130 if ($self->{running}) {
131 $self->log('info', "container is running - using online migration");
132 }
133
134 $self->{volumes} = []; # list of already migrated volumes
135 my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
136 my $volhash_errors = {};
137 my $abort = 0;
138
139 my $log_error = sub {
140 my ($msg, $volid) = @_;
141
142 $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid});
143 $abort = 1;
144 };
145
146 my $test_volid = sub {
147 my ($volid, $snapname) = @_;
148
149 return if !$volid;
150
151 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
152
153 # check if storage is available on both nodes
154 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
155 PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
156
157 if ($scfg->{shared}) {
158 $self->log('info', "volume '$volid' is on shared storage '$sid'")
159 if !$snapname;
160 return;
161 }
162
163 $volhash->{$volid} = defined($snapname) ? 'snapshot' : 'config';
164
165 my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid);
166
167 die "owned by other guest (owner = $owner)\n"
168 if !$owner || ($owner != $self->{vmid});
169
170 if (defined($snapname)) {
171 # we cannot migrate shapshots on local storage
172 # exceptions: 'zfspool'
173 if (($scfg->{type} eq 'zfspool')) {
174 return;
175 }
176 die "non-migratable snapshot exists\n";
177 }
178 };
179
180 my $test_mp = sub {
181 my ($ms, $mountpoint, $snapname) = @_;
182
183 my $volid = $mountpoint->{volume};
184 # already checked in prepare
185 if ($mountpoint->{type} ne 'volume') {
186 $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')")
187 if !$snapname;
188 return;
189 }
190
191 eval {
192 &$test_volid($volid, $snapname);
193 };
194
195 &$log_error($@, $volid) if $@;
196 };
197
198 # first unused / lost volumes owned by this container
199 my @sids = PVE::Storage::storage_ids($self->{storecfg});
200 foreach my $storeid (@sids) {
201 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid);
202 next if $scfg->{shared};
203 next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1);
204
205 # get list from PVE::Storage (for unused volumes)
206 my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid);
207
208 next if @{$dl->{$storeid}} == 0;
209
210 # check if storage is available on target node
211 PVE::Storage::storage_check_node($self->{storecfg}, $storeid, $self->{node});
212
213 PVE::Storage::foreach_volid($dl, sub {
214 my ($volid, $sid, $volname) = @_;
215
216 $volhash->{$volid} = 'storage';
217 });
218 }
219
220 # then all volumes referenced in snapshots
221 foreach my $snapname (keys %{$conf->{snapshots}}) {
222 &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef)
223 if defined($conf->{snapshots}->{$snapname}->{'vmstate'});
224 PVE::LXC::Config->foreach_mountpoint($conf->{snapshots}->{$snapname}, $test_mp, $snapname);
225 }
226
227 # finally all currently used volumes
228 PVE::LXC::Config->foreach_mountpoint($conf, $test_mp);
229
230
231 # additional checks for local storage
232 foreach my $volid (keys %$volhash) {
233 eval {
234 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
235 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid);
236
237 my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') ||
238 ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm');
239
240 die "storage type '$scfg->{type}' not supported\n"
241 if !$migratable;
242
243 # image is a linked clone on local storage, se we can't migrate.
244 if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) {
245 die "clone of '$basename'";
246 }
247 };
248 &$log_error($@, $volid) if $@;
249 }
250
251 foreach my $volid (sort keys %$volhash) {
252 if ($volhash->{$volid} eq 'storage') {
253 $self->log('info', "found local volume '$volid' (via storage)\n");
254 } elsif ($volhash->{$volid} eq 'config') {
255 $self->log('info', "found local volume '$volid' (in current VM config)\n");
256 } elsif ($volhash->{$volid} eq 'snapshot') {
257 $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n");
258 } else {
259 $self->log('info', "found local volume '$volid'\n");
260 }
261 }
262
263 foreach my $volid (sort keys %$volhash_errors) {
264 $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}");
265 }
266
267 if ($abort) {
268 die "can't migrate CT - check log\n";
269 }
270
271 my $rep_volumes;
272
273 my $rep_cfg = PVE::ReplicationConfig->new();
274
275 if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node})) {
276 die "can't live migrate VM with replicated volumes\n" if $self->{running};
277 my $start_time = time();
278 my $logfunc = sub { my ($msg) = @_; $self->log('info', $msg); };
279 $rep_volumes = PVE::Replication::run_replication(
280 'PVE::LXC::Config', $jobcfg, $start_time, $start_time, $logfunc);
281 }
282
283 my $insecure = $self->{opts}->{migration_type} eq 'insecure';
284 foreach my $volid (keys %$volhash) {
285 next if $rep_volumes->{$volid};
286 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
287 push @{$self->{volumes}}, $volid;
288 PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $sid, undef, undef, undef, undef, $insecure);
289 }
290
291 my $conffile = PVE::LXC::Config->config_file($vmid);
292 my $newconffile = PVE::LXC::Config->config_file($vmid, $self->{node});
293
294 if ($self->{running}) {
295 die "implement me";
296 }
297
298 # make sure everything on (shared) storage is unmounted
299 # Note: we must be 100% sure, else we get data corruption because
300 # non-shared file system could be mounted twice (on shared storage)
301
302 PVE::LXC::umount_all($vmid, $self->{storecfg}, $conf);
303
304 #to be sure there are no active volumes
305 my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
306 PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
307
308 # transfer replication state before move config
309 $self->transfer_replication_state();
310
311 # move config
312 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
313 if !rename($conffile, $newconffile);
314
315 $self->{conf_migrated} = 1;
316
317 $self->switch_replication_job_target();
318 }
319
320 sub phase1_cleanup {
321 my ($self, $vmid, $err) = @_;
322
323 $self->log('info', "aborting phase 1 - cleanup resources");
324
325 if ($self->{volumes}) {
326 foreach my $volid (@{$self->{volumes}}) {
327 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
328 # fixme: try to remove ?
329 }
330 }
331 }
332
333 sub phase3 {
334 my ($self, $vmid) = @_;
335
336 my $volids = $self->{volumes};
337
338 # destroy local copies
339 foreach my $volid (@$volids) {
340 eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
341 if (my $err = $@) {
342 $self->log('err', "removing local copy of '$volid' failed - $err");
343 $self->{errors} = 1;
344 last if $err =~ /^interrupted by signal$/;
345 }
346 }
347 }
348
349 sub final_cleanup {
350 my ($self, $vmid) = @_;
351
352 $self->log('info', "start final cleanup");
353
354 if (!$self->{conf_migrated}) {
355 my $conf = $self->{vmconf};
356 delete $conf->{lock};
357
358 eval { PVE::LXC::Config->write_config($vmid, $conf); };
359 if (my $err = $@) {
360 $self->log('err', $err);
361 }
362 } else {
363 my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ];
364 $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");
365 }
366
367 # in restart mode, we start the container on the target node
368 # after migration
369 if ($self->{opts}->{restart} && $self->{was_running}) {
370 $self->log('info', "start container on target node");
371 my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'start', $vmid];
372 $self->cmd($cmd);
373 }
374 }
375
376 1;