]>
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 | |
9746c095 | 47 | my $force = $self->{opts}->{force} // 0; |
20ab40f3 | 48 | my $need_activate = []; |
9746c095 | 49 | |
d250604f | 50 | PVE::LXC::Config->foreach_mountpoint($conf, sub { |
6f42807e DM |
51 | my ($ms, $mountpoint) = @_; |
52 | ||
53 | my $volid = $mountpoint->{volume}; | |
552e168f | 54 | my $type = $mountpoint->{type}; |
9746c095 | 55 | |
552e168f FG |
56 | # skip dev/bind mps when forced / shared |
57 | if ($type ne 'volume') { | |
58 | if ($force) { | |
59 | warn "-force is deprecated, please use the 'shared' property on individual non-volume mount points instead!\n"; | |
60 | return; | |
61 | } | |
62 | if ($mountpoint->{shared}) { | |
63 | return; | |
64 | } else { | |
65 | die "cannot migrate local $type mount point '$ms'\n"; | |
66 | } | |
9746c095 | 67 | } |
552e168f | 68 | |
6f42807e | 69 | my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid; |
235dbdf3 | 70 | die "can't determine assigned storage for mount point '$ms'\n" if !$storage; |
6f42807e DM |
71 | |
72 | # check if storage is available on both nodes | |
73 | my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $storage); | |
74 | PVE::Storage::storage_check_node($self->{storecfg}, $storage, $self->{node}); | |
75 | ||
20ab40f3 FG |
76 | |
77 | if ($scfg->{shared}) { | |
78 | # PVE::Storage::activate_storage checks this for non-shared storages | |
79 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
80 | warn "Used shared storage '$storage' is not online on source node!\n" | |
81 | if !$plugin->check_connection($storage, $scfg); | |
82 | } else { | |
83 | # only activate if not shared | |
84 | push @$need_activate, $volid; | |
85 | ||
a7cedb73 | 86 | # unless in restart mode because we shut the container down |
235dbdf3 | 87 | die "unable to migrate local mount point '$volid' while CT is running" |
a7cedb73 | 88 | if $running && !$restart; |
20ab40f3 | 89 | } |
6f42807e DM |
90 | |
91 | }); | |
92 | ||
20ab40f3 | 93 | PVE::Storage::activate_volumes($self->{storecfg}, $need_activate); |
6f42807e DM |
94 | |
95 | # todo: test if VM uses local resources | |
96 | ||
97 | # test ssh connection | |
98 | my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ]; | |
99 | eval { $self->cmd_quiet($cmd); }; | |
100 | die "Can't connect to destination address using public key\n" if $@; | |
101 | ||
a7cedb73 DC |
102 | # in restart mode, we shutdown the container before migrating |
103 | if ($restart && $running) { | |
104 | my $timeout = $self->{opts}->{timeout} // 180; | |
105 | ||
106 | $self->log('info', "shutdown CT $vmid\n"); | |
107 | ||
b1bad293 | 108 | PVE::LXC::vm_stop($vmid, 0, $timeout); |
a7cedb73 DC |
109 | |
110 | $running = 0; | |
111 | } | |
112 | ||
6f42807e DM |
113 | return $running; |
114 | } | |
115 | ||
116 | sub phase1 { | |
117 | my ($self, $vmid) = @_; | |
118 | ||
119 | $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})"); | |
120 | ||
121 | my $conf = $self->{vmconf}; | |
122 | $conf->{lock} = 'migrate'; | |
67afe46e | 123 | PVE::LXC::Config->write_config($vmid, $conf); |
6f42807e DM |
124 | |
125 | if ($self->{running}) { | |
126 | $self->log('info', "container is running - using online migration"); | |
127 | } | |
128 | ||
3c5dabe1 | 129 | $self->{volumes} = []; # list of already migrated volumes |
f503617b | 130 | my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes |
33deb76c FG |
131 | my $volhash_errors = {}; |
132 | my $abort = 0; | |
133 | ||
134 | my $log_error = sub { | |
135 | my ($msg, $volid) = @_; | |
136 | ||
137 | $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid}); | |
138 | $abort = 1; | |
139 | }; | |
6f42807e | 140 | |
3c5dabe1 FG |
141 | my $test_volid = sub { |
142 | my ($volid, $snapname) = @_; | |
6f42807e | 143 | |
3c5dabe1 FG |
144 | return if !$volid; |
145 | ||
146 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
147 | ||
148 | # check if storage is available on both nodes | |
149 | my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid); | |
150 | PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node}); | |
151 | ||
33deb76c FG |
152 | if ($scfg->{shared}) { |
153 | $self->log('info', "volume '$volid' is on shared storage '$sid'") | |
154 | if !$snapname; | |
155 | return; | |
156 | } | |
3c5dabe1 | 157 | |
0aa2d2a2 WB |
158 | $volhash->{$volid}->{ref} = defined($snapname) ? 'snapshot' : 'config'; |
159 | $volhash->{$volid}->{snapshots} = defined($snapname); | |
f503617b | 160 | |
3c5dabe1 FG |
161 | my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid); |
162 | ||
33deb76c | 163 | die "owned by other guest (owner = $owner)\n" |
3c5dabe1 | 164 | if !$owner || ($owner != $self->{vmid}); |
9746c095 | 165 | |
3c5dabe1 FG |
166 | if (defined($snapname)) { |
167 | # we cannot migrate shapshots on local storage | |
168 | # exceptions: 'zfspool' | |
169 | if (($scfg->{type} eq 'zfspool')) { | |
3c5dabe1 FG |
170 | return; |
171 | } | |
33deb76c | 172 | die "non-migratable snapshot exists\n"; |
3c5dabe1 FG |
173 | } |
174 | }; | |
175 | ||
176 | my $test_mp = sub { | |
177 | my ($ms, $mountpoint, $snapname) = @_; | |
178 | ||
179 | my $volid = $mountpoint->{volume}; | |
9746c095 FG |
180 | # already checked in prepare |
181 | if ($mountpoint->{type} ne 'volume') { | |
552e168f | 182 | $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')") |
3c5dabe1 | 183 | if !$snapname; |
9746c095 FG |
184 | return; |
185 | } | |
186 | ||
33deb76c FG |
187 | eval { |
188 | &$test_volid($volid, $snapname); | |
189 | }; | |
6f42807e | 190 | |
33deb76c | 191 | &$log_error($@, $volid) if $@; |
3c5dabe1 FG |
192 | }; |
193 | ||
1c5f78ef | 194 | # first unused / lost volumes owned by this container |
3c5dabe1 FG |
195 | my @sids = PVE::Storage::storage_ids($self->{storecfg}); |
196 | foreach my $storeid (@sids) { | |
197 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid); | |
198 | next if $scfg->{shared}; | |
199 | next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1); | |
200 | ||
201 | # get list from PVE::Storage (for unused volumes) | |
202 | my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid); | |
203 | ||
204 | next if @{$dl->{$storeid}} == 0; | |
205 | ||
206 | # check if storage is available on target node | |
207 | PVE::Storage::storage_check_node($self->{storecfg}, $storeid, $self->{node}); | |
208 | ||
209 | PVE::Storage::foreach_volid($dl, sub { | |
210 | my ($volid, $sid, $volname) = @_; | |
211 | ||
0aa2d2a2 | 212 | $volhash->{$volid}->{ref} = 'storage'; |
3c5dabe1 FG |
213 | }); |
214 | } | |
215 | ||
1c5f78ef FG |
216 | # then all volumes referenced in snapshots |
217 | foreach my $snapname (keys %{$conf->{snapshots}}) { | |
218 | &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef) | |
219 | if defined($conf->{snapshots}->{$snapname}->{'vmstate'}); | |
220 | PVE::LXC::Config->foreach_mountpoint($conf->{snapshots}->{$snapname}, $test_mp, $snapname); | |
221 | } | |
222 | ||
223 | # finally all currently used volumes | |
224 | PVE::LXC::Config->foreach_mountpoint($conf, $test_mp); | |
225 | ||
226 | ||
3c5dabe1 FG |
227 | # additional checks for local storage |
228 | foreach my $volid (keys %$volhash) { | |
33deb76c FG |
229 | eval { |
230 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
231 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid); | |
3c5dabe1 | 232 | |
33deb76c FG |
233 | my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') || |
234 | ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm'); | |
3c5dabe1 | 235 | |
33deb76c FG |
236 | die "storage type '$scfg->{type}' not supported\n" |
237 | if !$migratable; | |
3c5dabe1 | 238 | |
33deb76c FG |
239 | # image is a linked clone on local storage, se we can't migrate. |
240 | if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) { | |
241 | die "clone of '$basename'"; | |
242 | } | |
243 | }; | |
244 | &$log_error($@, $volid) if $@; | |
3c5dabe1 FG |
245 | } |
246 | ||
f503617b | 247 | foreach my $volid (sort keys %$volhash) { |
0aa2d2a2 WB |
248 | my $ref = $volhash->{$volid}->{ref}; |
249 | if ($ref eq 'storage') { | |
f503617b | 250 | $self->log('info', "found local volume '$volid' (via storage)\n"); |
0aa2d2a2 | 251 | } elsif ($ref eq 'config') { |
f503617b | 252 | $self->log('info', "found local volume '$volid' (in current VM config)\n"); |
0aa2d2a2 | 253 | } elsif ($ref eq 'snapshot') { |
f503617b FG |
254 | $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n"); |
255 | } else { | |
256 | $self->log('info', "found local volume '$volid'\n"); | |
257 | } | |
258 | } | |
259 | ||
33deb76c FG |
260 | foreach my $volid (sort keys %$volhash_errors) { |
261 | $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}"); | |
262 | } | |
263 | ||
264 | if ($abort) { | |
265 | die "can't migrate CT - check log\n"; | |
266 | } | |
267 | ||
3efa5e3d DM |
268 | my $rep_volumes; |
269 | ||
270 | my $rep_cfg = PVE::ReplicationConfig->new(); | |
271 | ||
272 | if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node})) { | |
273 | die "can't live migrate VM with replicated volumes\n" if $self->{running}; | |
274 | my $start_time = time(); | |
275 | my $logfunc = sub { my ($msg) = @_; $self->log('info', $msg); }; | |
276 | $rep_volumes = PVE::Replication::run_replication( | |
277 | 'PVE::LXC::Config', $jobcfg, $start_time, $start_time, $logfunc); | |
278 | } | |
279 | ||
a53ce933 | 280 | my $insecure = $self->{opts}->{migration_type} eq 'insecure'; |
3c5dabe1 | 281 | foreach my $volid (keys %$volhash) { |
3efa5e3d | 282 | next if $rep_volumes->{$volid}; |
3c5dabe1 FG |
283 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); |
284 | push @{$self->{volumes}}, $volid; | |
0aa2d2a2 WB |
285 | my $with_snapshots = $volhash->{$volid}->{snapshots}; |
286 | PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $sid, undef, undef, undef, undef, $insecure, $with_snapshots); | |
3c5dabe1 | 287 | } |
6f42807e | 288 | |
67afe46e FG |
289 | my $conffile = PVE::LXC::Config->config_file($vmid); |
290 | my $newconffile = PVE::LXC::Config->config_file($vmid, $self->{node}); | |
6f42807e DM |
291 | |
292 | if ($self->{running}) { | |
293 | die "implement me"; | |
294 | } | |
295 | ||
296 | # make sure everything on (shared) storage is unmounted | |
297 | # Note: we must be 100% sure, else we get data corruption because | |
298 | # non-shared file system could be mounted twice (on shared storage) | |
299 | ||
300 | PVE::LXC::umount_all($vmid, $self->{storecfg}, $conf); | |
301 | ||
c9bc5018 | 302 | #to be sure there are no active volumes |
d250604f | 303 | my $vollist = PVE::LXC::Config->get_vm_volumes($conf); |
c9bc5018 WL |
304 | PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist); |
305 | ||
3efa5e3d | 306 | # transfer replication state before move config |
522d3569 | 307 | $self->transfer_replication_state() if $rep_volumes; |
3efa5e3d | 308 | |
6f42807e DM |
309 | # move config |
310 | die "Failed to move config to node '$self->{node}' - rename failed: $!\n" | |
311 | if !rename($conffile, $newconffile); | |
1d1c1b4f WL |
312 | |
313 | $self->{conf_migrated} = 1; | |
3efa5e3d | 314 | |
522d3569 | 315 | $self->switch_replication_job_target() if $rep_volumes; |
6f42807e DM |
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) { | |
8fdd1fc8 DM |
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$/; | |
6f42807e DM |
343 | } |
344 | } | |
345 | } | |
346 | ||
347 | sub final_cleanup { | |
348 | my ($self, $vmid) = @_; | |
349 | ||
350 | $self->log('info', "start final cleanup"); | |
351 | ||
1d1c1b4f | 352 | if (!$self->{conf_migrated}) { |
dd3bcfb2 | 353 | eval { PVE::LXC::Config->remove_lock($vmid, 'migrate'); }; |
1d1c1b4f WL |
354 | if (my $err = $@) { |
355 | $self->log('err', $err); | |
356 | } | |
6725e93c AA |
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 | } | |
1d1c1b4f WL |
364 | } else { |
365 | my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ]; | |
ee8d9207 FG |
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 | } | |
6f42807e | 375 | } |
a7cedb73 | 376 | |
6f42807e DM |
377 | } |
378 | ||
379 | 1; |