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