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