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