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