]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Migrate.pm
fix #1147: allow marking non-volume mps as shared
[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;
13
14 use base qw(PVE::AbstractMigrate);
15
16 sub lock_vm {
17 my ($self, $vmid, $code, @param) = @_;
18
19 return PVE::LXC::Config->lock_config($vmid, $code, @param);
20 }
21
22 sub prepare {
23 my ($self, $vmid) = @_;
24
25 my $online = $self->{opts}->{online};
26
27 $self->{storecfg} = PVE::Storage::config();
28
29 # test if CT exists
30 my $conf = $self->{vmconf} = PVE::LXC::Config->load_config($vmid);
31
32 PVE::LXC::Config->check_lock($conf);
33
34 my $running = 0;
35 if (PVE::LXC::check_running($vmid)) {
36 die "lxc live migration is currently not implemented\n";
37
38 die "can't migrate running container without --online\n" if !$online;
39 $running = 1;
40 }
41
42 my $force = $self->{opts}->{force} // 0;
43 my $need_activate = [];
44
45 PVE::LXC::Config->foreach_mountpoint($conf, sub {
46 my ($ms, $mountpoint) = @_;
47
48 my $volid = $mountpoint->{volume};
49 my $type = $mountpoint->{type};
50
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 }
62 }
63
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
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 }
84
85 });
86
87 PVE::Storage::activate_volumes($self->{storecfg}, $need_activate);
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';
106 PVE::LXC::Config->write_config($vmid, $conf);
107
108 if ($self->{running}) {
109 $self->log('info', "container is running - using online migration");
110 }
111
112 $self->{volumes} = []; # list of already migrated volumes
113 my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
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 };
123
124 my $test_volid = sub {
125 my ($volid, $snapname) = @_;
126
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
135 if ($scfg->{shared}) {
136 $self->log('info', "volume '$volid' is on shared storage '$sid'")
137 if !$snapname;
138 return;
139 }
140
141 $volhash->{$volid} = defined($snapname) ? 'snapshot' : 'config';
142
143 my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid);
144
145 die "owned by other guest (owner = $owner)\n"
146 if !$owner || ($owner != $self->{vmid});
147
148 if (defined($snapname)) {
149 # we cannot migrate shapshots on local storage
150 # exceptions: 'zfspool'
151 if (($scfg->{type} eq 'zfspool')) {
152 return;
153 }
154 die "non-migratable snapshot exists\n";
155 }
156 };
157
158 my $test_mp = sub {
159 my ($ms, $mountpoint, $snapname) = @_;
160
161 my $volid = $mountpoint->{volume};
162 # already checked in prepare
163 if ($mountpoint->{type} ne 'volume') {
164 $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')")
165 if !$snapname;
166 return;
167 }
168
169 eval {
170 &$test_volid($volid, $snapname);
171 };
172
173 &$log_error($@, $volid) if $@;
174 };
175
176 # first unused / lost volumes owned by this container
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
194 $volhash->{$volid} = 'storage';
195 });
196 }
197
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
209 # additional checks for local storage
210 foreach my $volid (keys %$volhash) {
211 eval {
212 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
213 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid);
214
215 my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') ||
216 ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm');
217
218 die "storage type '$scfg->{type}' not supported\n"
219 if !$migratable;
220
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 $@;
227 }
228
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
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
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 }
254
255 my $conffile = PVE::LXC::Config->config_file($vmid);
256 my $newconffile = PVE::LXC::Config->config_file($vmid, $self->{node});
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
268 #to be sure there are no active volumes
269 my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
270 PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
271
272 # move config
273 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
274 if !rename($conffile, $newconffile);
275
276 $self->{conf_migrated} = 1;
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
313 if (!$self->{conf_migrated}) {
314 my $conf = $self->{vmconf};
315 delete $conf->{lock};
316
317 eval { PVE::LXC::Config->write_config($vmid, $conf); };
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");
324 }
325 }
326
327 1;