]>
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; | |
12 | use PVE::LXC; | |
13 | ||
14 | use base qw(PVE::AbstractMigrate); | |
15 | ||
16 | sub lock_vm { | |
17 | my ($self, $vmid, $code, @param) = @_; | |
18 | ||
67afe46e | 19 | return PVE::LXC::Config->lock_config($vmid, $code, @param); |
6f42807e DM |
20 | } |
21 | ||
22 | sub prepare { | |
23 | my ($self, $vmid) = @_; | |
24 | ||
25 | my $online = $self->{opts}->{online}; | |
26 | ||
27 | $self->{storecfg} = PVE::Storage::config(); | |
28 | ||
1cd1fa12 | 29 | # test if CT exists |
67afe46e | 30 | my $conf = $self->{vmconf} = PVE::LXC::Config->load_config($vmid); |
6f42807e | 31 | |
67afe46e | 32 | PVE::LXC::Config->check_lock($conf); |
6f42807e DM |
33 | |
34 | my $running = 0; | |
35 | if (PVE::LXC::check_running($vmid)) { | |
36 | die "lxc live migration is currently not implemented\n"; | |
37 | ||
fc181735 | 38 | die "can't migrate running container without --online\n" if !$online; |
6f42807e DM |
39 | $running = 1; |
40 | } | |
41 | ||
9746c095 | 42 | my $force = $self->{opts}->{force} // 0; |
20ab40f3 | 43 | my $need_activate = []; |
9746c095 | 44 | |
d250604f | 45 | PVE::LXC::Config->foreach_mountpoint($conf, sub { |
6f42807e DM |
46 | my ($ms, $mountpoint) = @_; |
47 | ||
48 | my $volid = $mountpoint->{volume}; | |
552e168f | 49 | my $type = $mountpoint->{type}; |
9746c095 | 50 | |
552e168f FG |
51 | # skip dev/bind mps when forced / shared |
52 | if ($type ne 'volume') { | |
53 | if ($force) { | |
54 | warn "-force is deprecated, please use the 'shared' property on individual non-volume mount points instead!\n"; | |
55 | return; | |
56 | } | |
57 | if ($mountpoint->{shared}) { | |
58 | return; | |
59 | } else { | |
60 | die "cannot migrate local $type mount point '$ms'\n"; | |
61 | } | |
9746c095 | 62 | } |
552e168f | 63 | |
6f42807e DM |
64 | my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid; |
65 | die "can't determine assigned storage for mountpoint '$ms'\n" if !$storage; | |
66 | ||
67 | # check if storage is available on both nodes | |
68 | my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $storage); | |
69 | PVE::Storage::storage_check_node($self->{storecfg}, $storage, $self->{node}); | |
70 | ||
20ab40f3 FG |
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 | # only activate if not shared | |
79 | push @$need_activate, $volid; | |
80 | ||
81 | die "unable to migrate local mountpoint '$volid' while CT is running" | |
82 | if $running; | |
83 | } | |
6f42807e DM |
84 | |
85 | }); | |
86 | ||
20ab40f3 | 87 | PVE::Storage::activate_volumes($self->{storecfg}, $need_activate); |
6f42807e DM |
88 | |
89 | # todo: test if VM uses local resources | |
90 | ||
91 | # test ssh connection | |
92 | my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ]; | |
93 | eval { $self->cmd_quiet($cmd); }; | |
94 | die "Can't connect to destination address using public key\n" if $@; | |
95 | ||
96 | return $running; | |
97 | } | |
98 | ||
99 | sub phase1 { | |
100 | my ($self, $vmid) = @_; | |
101 | ||
102 | $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})"); | |
103 | ||
104 | my $conf = $self->{vmconf}; | |
105 | $conf->{lock} = 'migrate'; | |
67afe46e | 106 | PVE::LXC::Config->write_config($vmid, $conf); |
6f42807e DM |
107 | |
108 | if ($self->{running}) { | |
109 | $self->log('info', "container is running - using online migration"); | |
110 | } | |
111 | ||
3c5dabe1 | 112 | $self->{volumes} = []; # list of already migrated volumes |
f503617b | 113 | my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes |
33deb76c FG |
114 | my $volhash_errors = {}; |
115 | my $abort = 0; | |
116 | ||
117 | my $log_error = sub { | |
118 | my ($msg, $volid) = @_; | |
119 | ||
120 | $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid}); | |
121 | $abort = 1; | |
122 | }; | |
6f42807e | 123 | |
3c5dabe1 FG |
124 | my $test_volid = sub { |
125 | my ($volid, $snapname) = @_; | |
6f42807e | 126 | |
3c5dabe1 FG |
127 | return if !$volid; |
128 | ||
129 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
130 | ||
131 | # check if storage is available on both nodes | |
132 | my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid); | |
133 | PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node}); | |
134 | ||
33deb76c FG |
135 | if ($scfg->{shared}) { |
136 | $self->log('info', "volume '$volid' is on shared storage '$sid'") | |
137 | if !$snapname; | |
138 | return; | |
139 | } | |
3c5dabe1 | 140 | |
f503617b FG |
141 | $volhash->{$volid} = defined($snapname) ? 'snapshot' : 'config'; |
142 | ||
3c5dabe1 FG |
143 | my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid); |
144 | ||
33deb76c | 145 | die "owned by other guest (owner = $owner)\n" |
3c5dabe1 | 146 | if !$owner || ($owner != $self->{vmid}); |
9746c095 | 147 | |
3c5dabe1 FG |
148 | if (defined($snapname)) { |
149 | # we cannot migrate shapshots on local storage | |
150 | # exceptions: 'zfspool' | |
151 | if (($scfg->{type} eq 'zfspool')) { | |
3c5dabe1 FG |
152 | return; |
153 | } | |
33deb76c | 154 | die "non-migratable snapshot exists\n"; |
3c5dabe1 FG |
155 | } |
156 | }; | |
157 | ||
158 | my $test_mp = sub { | |
159 | my ($ms, $mountpoint, $snapname) = @_; | |
160 | ||
161 | my $volid = $mountpoint->{volume}; | |
9746c095 FG |
162 | # already checked in prepare |
163 | if ($mountpoint->{type} ne 'volume') { | |
552e168f | 164 | $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')") |
3c5dabe1 | 165 | if !$snapname; |
9746c095 FG |
166 | return; |
167 | } | |
168 | ||
33deb76c FG |
169 | eval { |
170 | &$test_volid($volid, $snapname); | |
171 | }; | |
6f42807e | 172 | |
33deb76c | 173 | &$log_error($@, $volid) if $@; |
3c5dabe1 FG |
174 | }; |
175 | ||
1c5f78ef | 176 | # first unused / lost volumes owned by this container |
3c5dabe1 FG |
177 | my @sids = PVE::Storage::storage_ids($self->{storecfg}); |
178 | foreach my $storeid (@sids) { | |
179 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid); | |
180 | next if $scfg->{shared}; | |
181 | next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1); | |
182 | ||
183 | # get list from PVE::Storage (for unused volumes) | |
184 | my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid); | |
185 | ||
186 | next if @{$dl->{$storeid}} == 0; | |
187 | ||
188 | # check if storage is available on target node | |
189 | PVE::Storage::storage_check_node($self->{storecfg}, $storeid, $self->{node}); | |
190 | ||
191 | PVE::Storage::foreach_volid($dl, sub { | |
192 | my ($volid, $sid, $volname) = @_; | |
193 | ||
1c5f78ef | 194 | $volhash->{$volid} = 'storage'; |
3c5dabe1 FG |
195 | }); |
196 | } | |
197 | ||
1c5f78ef FG |
198 | # then all volumes referenced in snapshots |
199 | foreach my $snapname (keys %{$conf->{snapshots}}) { | |
200 | &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef) | |
201 | if defined($conf->{snapshots}->{$snapname}->{'vmstate'}); | |
202 | PVE::LXC::Config->foreach_mountpoint($conf->{snapshots}->{$snapname}, $test_mp, $snapname); | |
203 | } | |
204 | ||
205 | # finally all currently used volumes | |
206 | PVE::LXC::Config->foreach_mountpoint($conf, $test_mp); | |
207 | ||
208 | ||
3c5dabe1 FG |
209 | # additional checks for local storage |
210 | foreach my $volid (keys %$volhash) { | |
33deb76c FG |
211 | eval { |
212 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
213 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid); | |
3c5dabe1 | 214 | |
33deb76c FG |
215 | my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') || |
216 | ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm'); | |
3c5dabe1 | 217 | |
33deb76c FG |
218 | die "storage type '$scfg->{type}' not supported\n" |
219 | if !$migratable; | |
3c5dabe1 | 220 | |
33deb76c FG |
221 | # image is a linked clone on local storage, se we can't migrate. |
222 | if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) { | |
223 | die "clone of '$basename'"; | |
224 | } | |
225 | }; | |
226 | &$log_error($@, $volid) if $@; | |
3c5dabe1 FG |
227 | } |
228 | ||
f503617b FG |
229 | foreach my $volid (sort keys %$volhash) { |
230 | if ($volhash->{$volid} eq 'storage') { | |
231 | $self->log('info', "found local volume '$volid' (via storage)\n"); | |
232 | } elsif ($volhash->{$volid} eq 'config') { | |
233 | $self->log('info', "found local volume '$volid' (in current VM config)\n"); | |
234 | } elsif ($volhash->{$volid} eq 'snapshot') { | |
235 | $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n"); | |
236 | } else { | |
237 | $self->log('info', "found local volume '$volid'\n"); | |
238 | } | |
239 | } | |
240 | ||
33deb76c FG |
241 | foreach my $volid (sort keys %$volhash_errors) { |
242 | $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}"); | |
243 | } | |
244 | ||
245 | if ($abort) { | |
246 | die "can't migrate CT - check log\n"; | |
247 | } | |
248 | ||
3c5dabe1 FG |
249 | foreach my $volid (keys %$volhash) { |
250 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
251 | push @{$self->{volumes}}, $volid; | |
252 | PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid); | |
253 | } | |
6f42807e | 254 | |
67afe46e FG |
255 | my $conffile = PVE::LXC::Config->config_file($vmid); |
256 | my $newconffile = PVE::LXC::Config->config_file($vmid, $self->{node}); | |
6f42807e DM |
257 | |
258 | if ($self->{running}) { | |
259 | die "implement me"; | |
260 | } | |
261 | ||
262 | # make sure everything on (shared) storage is unmounted | |
263 | # Note: we must be 100% sure, else we get data corruption because | |
264 | # non-shared file system could be mounted twice (on shared storage) | |
265 | ||
266 | PVE::LXC::umount_all($vmid, $self->{storecfg}, $conf); | |
267 | ||
c9bc5018 | 268 | #to be sure there are no active volumes |
d250604f | 269 | my $vollist = PVE::LXC::Config->get_vm_volumes($conf); |
c9bc5018 WL |
270 | PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist); |
271 | ||
6f42807e DM |
272 | # move config |
273 | die "Failed to move config to node '$self->{node}' - rename failed: $!\n" | |
274 | if !rename($conffile, $newconffile); | |
1d1c1b4f WL |
275 | |
276 | $self->{conf_migrated} = 1; | |
6f42807e DM |
277 | } |
278 | ||
279 | sub phase1_cleanup { | |
280 | my ($self, $vmid, $err) = @_; | |
281 | ||
282 | $self->log('info', "aborting phase 1 - cleanup resources"); | |
283 | ||
284 | if ($self->{volumes}) { | |
285 | foreach my $volid (@{$self->{volumes}}) { | |
286 | $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'"); | |
287 | # fixme: try to remove ? | |
288 | } | |
289 | } | |
290 | } | |
291 | ||
292 | sub phase3 { | |
293 | my ($self, $vmid) = @_; | |
294 | ||
295 | my $volids = $self->{volumes}; | |
296 | ||
297 | # destroy local copies | |
298 | foreach my $volid (@$volids) { | |
299 | eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); }; | |
300 | if (my $err = $@) { | |
301 | $self->log('err', "removing local copy of '$volid' failed - $err"); | |
302 | $self->{errors} = 1; | |
303 | last if $err =~ /^interrupted by signal$/; | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
308 | sub final_cleanup { | |
309 | my ($self, $vmid) = @_; | |
310 | ||
311 | $self->log('info', "start final cleanup"); | |
312 | ||
1d1c1b4f WL |
313 | if (!$self->{conf_migrated}) { |
314 | my $conf = $self->{vmconf}; | |
315 | delete $conf->{lock}; | |
6f42807e | 316 | |
67afe46e | 317 | eval { PVE::LXC::Config->write_config($vmid, $conf); }; |
1d1c1b4f WL |
318 | if (my $err = $@) { |
319 | $self->log('err', $err); | |
320 | } | |
321 | } else { | |
322 | my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ]; | |
323 | $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock"); | |
6f42807e DM |
324 | } |
325 | } | |
326 | ||
327 | 1; |