]>
Commit | Line | Data |
---|---|---|
6f42807e DM |
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; | |
8d66edee | 12 | use PVE::LXC::Config; |
6f42807e | 13 | use PVE::LXC; |
3efa5e3d DM |
14 | use PVE::ReplicationConfig; |
15 | use PVE::ReplicationState; | |
16 | use PVE::Replication; | |
6f42807e DM |
17 | |
18 | use base qw(PVE::AbstractMigrate); | |
19 | ||
20 | sub lock_vm { | |
21 | my ($self, $vmid, $code, @param) = @_; | |
22 | ||
67afe46e | 23 | return PVE::LXC::Config->lock_config($vmid, $code, @param); |
6f42807e DM |
24 | } |
25 | ||
26 | sub prepare { | |
27 | my ($self, $vmid) = @_; | |
28 | ||
29 | my $online = $self->{opts}->{online}; | |
a7cedb73 | 30 | my $restart= $self->{opts}->{restart}; |
6f42807e DM |
31 | |
32 | $self->{storecfg} = PVE::Storage::config(); | |
33 | ||
1cd1fa12 | 34 | # test if CT exists |
67afe46e | 35 | my $conf = $self->{vmconf} = PVE::LXC::Config->load_config($vmid); |
6f42807e | 36 | |
67afe46e | 37 | PVE::LXC::Config->check_lock($conf); |
6f42807e DM |
38 | |
39 | my $running = 0; | |
40 | if (PVE::LXC::check_running($vmid)) { | |
a7cedb73 DC |
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; | |
6f42807e DM |
43 | $running = 1; |
44 | } | |
a7cedb73 | 45 | $self->{was_running} = $running; |
6f42807e | 46 | |
2334b15d | 47 | PVE::LXC::Config->foreach_volume_full($conf, { include_unused => 1 }, sub { |
6f42807e DM |
48 | my ($ms, $mountpoint) = @_; |
49 | ||
50 | my $volid = $mountpoint->{volume}; | |
552e168f | 51 | my $type = $mountpoint->{type}; |
9746c095 | 52 | |
154b1295 | 53 | # skip dev/bind mps when shared |
552e168f | 54 | if ($type ne 'volume') { |
552e168f FG |
55 | if ($mountpoint->{shared}) { |
56 | return; | |
57 | } else { | |
58 | die "cannot migrate local $type mount point '$ms'\n"; | |
59 | } | |
9746c095 | 60 | } |
552e168f | 61 | |
6f42807e | 62 | my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid; |
235dbdf3 | 63 | die "can't determine assigned storage for mount point '$ms'\n" if !$storage; |
6f42807e DM |
64 | |
65 | # check if storage is available on both nodes | |
a1c27f86 | 66 | my $scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $storage); |
e90ddc4c FG |
67 | |
68 | my $targetsid = $storage; | |
6f42807e | 69 | |
b2f00a89 FE |
70 | die "content type 'rootdir' is not available on storage '$storage'\n" |
71 | if !$scfg->{content}->{rootdir}; | |
20ab40f3 FG |
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 { | |
a7cedb73 | 79 | # unless in restart mode because we shut the container down |
235dbdf3 | 80 | die "unable to migrate local mount point '$volid' while CT is running" |
a7cedb73 | 81 | if $running && !$restart; |
e90ddc4c FG |
82 | |
83 | $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $storage); | |
20ab40f3 | 84 | } |
6f42807e | 85 | |
e90ddc4c FG |
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}; | |
6f42807e DM |
90 | }); |
91 | ||
6f42807e DM |
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 | ||
a7cedb73 DC |
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 | ||
b1bad293 | 105 | PVE::LXC::vm_stop($vmid, 0, $timeout); |
a7cedb73 DC |
106 | |
107 | $running = 0; | |
108 | } | |
109 | ||
6f42807e DM |
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'; | |
67afe46e | 120 | PVE::LXC::Config->write_config($vmid, $conf); |
6f42807e DM |
121 | |
122 | if ($self->{running}) { | |
123 | $self->log('info', "container is running - using online migration"); | |
124 | } | |
125 | ||
3c5dabe1 | 126 | $self->{volumes} = []; # list of already migrated volumes |
f503617b | 127 | my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes |
33deb76c FG |
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 | }; | |
6f42807e | 137 | |
3c5dabe1 FG |
138 | my $test_volid = sub { |
139 | my ($volid, $snapname) = @_; | |
6f42807e | 140 | |
3c5dabe1 FG |
141 | return if !$volid; |
142 | ||
143 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
144 | ||
e90ddc4c | 145 | # check if storage is available on source node |
a1c27f86 | 146 | my $scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $sid); |
e90ddc4c FG |
147 | |
148 | my $targetsid = $sid; | |
3c5dabe1 | 149 | |
33deb76c FG |
150 | if ($scfg->{shared}) { |
151 | $self->log('info', "volume '$volid' is on shared storage '$sid'") | |
152 | if !$snapname; | |
153 | return; | |
e90ddc4c FG |
154 | } else { |
155 | $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $sid); | |
33deb76c | 156 | } |
3c5dabe1 | 157 | |
e90ddc4c FG |
158 | PVE::Storage::storage_check_enabled($self->{storecfg}, $targetsid, $self->{node}); |
159 | ||
160 | my $bwlimit = $self->get_bwlimit($sid, $targetsid); | |
161 | ||
0aa2d2a2 | 162 | $volhash->{$volid}->{ref} = defined($snapname) ? 'snapshot' : 'config'; |
7121bd69 | 163 | $volhash->{$volid}->{snapshots} = 1 if defined($snapname); |
e90ddc4c FG |
164 | $volhash->{$volid}->{targetsid} = $targetsid; |
165 | $volhash->{$volid}->{bwlimit} = $bwlimit; | |
f503617b | 166 | |
3c5dabe1 FG |
167 | my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid); |
168 | ||
33deb76c | 169 | die "owned by other guest (owner = $owner)\n" |
3c5dabe1 | 170 | if !$owner || ($owner != $self->{vmid}); |
9746c095 | 171 | |
3c5dabe1 FG |
172 | if (defined($snapname)) { |
173 | # we cannot migrate shapshots on local storage | |
026ff6fa WB |
174 | # exceptions: 'zfspool', 'btrfs' |
175 | if ($scfg->{type} eq 'zfspool' || $scfg->{type} eq 'btrfs') { | |
3c5dabe1 FG |
176 | return; |
177 | } | |
33deb76c | 178 | die "non-migratable snapshot exists\n"; |
3c5dabe1 FG |
179 | } |
180 | }; | |
181 | ||
182 | my $test_mp = sub { | |
183 | my ($ms, $mountpoint, $snapname) = @_; | |
184 | ||
185 | my $volid = $mountpoint->{volume}; | |
9746c095 FG |
186 | # already checked in prepare |
187 | if ($mountpoint->{type} ne 'volume') { | |
552e168f | 188 | $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')") |
3c5dabe1 | 189 | if !$snapname; |
9746c095 FG |
190 | return; |
191 | } | |
192 | ||
33deb76c FG |
193 | eval { |
194 | &$test_volid($volid, $snapname); | |
195 | }; | |
6f42807e | 196 | |
33deb76c | 197 | &$log_error($@, $volid) if $@; |
3c5dabe1 FG |
198 | }; |
199 | ||
1c5f78ef | 200 | # first unused / lost volumes owned by this container |
3c5dabe1 FG |
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 | ||
2334b15d | 207 | # get list from PVE::Storage (for unreferenced volumes) |
b2f00a89 | 208 | my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid, undef, 'rootdir'); |
3c5dabe1 FG |
209 | |
210 | next if @{$dl->{$storeid}} == 0; | |
211 | ||
212 | # check if storage is available on target node | |
e90ddc4c | 213 | my $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $storeid); |
26115ef2 | 214 | my $target_scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $targetsid, $self->{node}); |
3c5dabe1 | 215 | |
26115ef2 FG |
216 | die "content type 'rootdir' is not available on storage '$targetsid'\n" |
217 | if !$target_scfg->{content}->{rootdir}; | |
b2f00a89 | 218 | |
3c5dabe1 FG |
219 | PVE::Storage::foreach_volid($dl, sub { |
220 | my ($volid, $sid, $volname) = @_; | |
221 | ||
0aa2d2a2 | 222 | $volhash->{$volid}->{ref} = 'storage'; |
b9d0dcb4 | 223 | $volhash->{$volid}->{targetsid} = $targetsid; |
3c5dabe1 FG |
224 | }); |
225 | } | |
226 | ||
1c5f78ef FG |
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'}); | |
015740e6 | 231 | PVE::LXC::Config->foreach_volume($conf->{snapshots}->{$snapname}, $test_mp, $snapname); |
1c5f78ef FG |
232 | } |
233 | ||
2334b15d FE |
234 | # finally all current volumes |
235 | PVE::LXC::Config->foreach_volume_full($conf, { include_unused => 1 }, $test_mp); | |
1c5f78ef | 236 | |
3c5dabe1 FG |
237 | # additional checks for local storage |
238 | foreach my $volid (keys %$volhash) { | |
33deb76c FG |
239 | eval { |
240 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
241 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid); | |
3c5dabe1 | 242 | |
026ff6fa WB |
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'); | |
3c5dabe1 | 246 | |
33deb76c FG |
247 | die "storage type '$scfg->{type}' not supported\n" |
248 | if !$migratable; | |
3c5dabe1 | 249 | |
33deb76c FG |
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 $@; | |
3c5dabe1 FG |
256 | } |
257 | ||
f503617b | 258 | foreach my $volid (sort keys %$volhash) { |
0aa2d2a2 WB |
259 | my $ref = $volhash->{$volid}->{ref}; |
260 | if ($ref eq 'storage') { | |
f503617b | 261 | $self->log('info', "found local volume '$volid' (via storage)\n"); |
0aa2d2a2 | 262 | } elsif ($ref eq 'config') { |
f503617b | 263 | $self->log('info', "found local volume '$volid' (in current VM config)\n"); |
0aa2d2a2 | 264 | } elsif ($ref eq 'snapshot') { |
f503617b FG |
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 | ||
33deb76c FG |
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 | ||
3efa5e3d DM |
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 | ||
1159329a | 291 | my $opts = $self->{opts}; |
3c5dabe1 | 292 | foreach my $volid (keys %$volhash) { |
3efa5e3d | 293 | next if $rep_volumes->{$volid}; |
3c5dabe1 FG |
294 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); |
295 | push @{$self->{volumes}}, $volid; | |
e90ddc4c | 296 | |
1159329a | 297 | # JSONSchema and get_bandwidth_limit use kbps - storage_migrate bps |
e90ddc4c | 298 | my $bwlimit = $volhash->{$volid}->{bwlimit}; |
1159329a SI |
299 | $bwlimit = $bwlimit * 1024 if defined($bwlimit); |
300 | ||
e90ddc4c FG |
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}, | |
4c4744d1 | 308 | 'allow_rename' => 1, |
e90ddc4c FG |
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 | ); | |
1d26bb86 FE |
320 | }; |
321 | ||
d79b051a | 322 | if (my $err = $@) { |
e90ddc4c | 323 | die "storage migration for '$volid' to storage '$targetsid' failed - $err\n"; |
d79b051a | 324 | } |
1e5f5da7 | 325 | |
e90ddc4c FG |
326 | $self->{volume_map}->{$volid} = $new_volid; |
327 | $self->log('info', "volume '$volid' is '$new_volid' on the target\n"); | |
328 | ||
1e5f5da7 FE |
329 | eval { PVE::Storage::deactivate_volumes($self->{storecfg}, [$volid]); }; |
330 | if (my $err = $@) { | |
331 | $self->log('warn', $err); | |
332 | } | |
3c5dabe1 | 333 | } |
6f42807e | 334 | |
6f42807e DM |
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 | ||
c9bc5018 | 345 | #to be sure there are no active volumes |
d250604f | 346 | my $vollist = PVE::LXC::Config->get_vm_volumes($conf); |
c9bc5018 WL |
347 | PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist); |
348 | ||
86df609a | 349 | # transfer replication state before moving config |
522d3569 | 350 | $self->transfer_replication_state() if $rep_volumes; |
e90ddc4c FG |
351 | PVE::LXC::Config->update_volume_ids($conf, $self->{volume_map}); |
352 | PVE::LXC::Config->write_config($vmid, $conf); | |
86df609a | 353 | PVE::LXC::Config->move_config_to_node($vmid, $self->{node}); |
1d1c1b4f | 354 | $self->{conf_migrated} = 1; |
522d3569 | 355 | $self->switch_replication_job_target() if $rep_volumes; |
6f42807e DM |
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) { | |
8fdd1fc8 DM |
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$/; | |
6f42807e DM |
383 | } |
384 | } | |
385 | } | |
386 | ||
387 | sub final_cleanup { | |
388 | my ($self, $vmid) = @_; | |
389 | ||
390 | $self->log('info', "start final cleanup"); | |
391 | ||
1d1c1b4f | 392 | if (!$self->{conf_migrated}) { |
dd3bcfb2 | 393 | eval { PVE::LXC::Config->remove_lock($vmid, 'migrate'); }; |
1d1c1b4f WL |
394 | if (my $err = $@) { |
395 | $self->log('err', $err); | |
396 | } | |
6725e93c AA |
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 | } | |
1d1c1b4f WL |
404 | } else { |
405 | my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ]; | |
ee8d9207 FG |
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 | } | |
6f42807e | 415 | } |
a7cedb73 | 416 | |
6f42807e DM |
417 | } |
418 | ||
419 | 1; |