]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Migrate.pm
migrate: pass the with_snapshots parameter
[pve-container.git] / src / PVE / LXC / Migrate.pm
CommitLineData
6f42807e
DM
1package PVE::LXC::Migrate;
2
3use strict;
4use warnings;
5use PVE::AbstractMigrate;
6use File::Basename;
7use File::Copy; # fixme: remove
8use PVE::Tools;
9use PVE::INotify;
10use PVE::Cluster;
11use PVE::Storage;
8d66edee 12use PVE::LXC::Config;
6f42807e 13use PVE::LXC;
3efa5e3d
DM
14use PVE::ReplicationConfig;
15use PVE::ReplicationState;
16use PVE::Replication;
6f42807e
DM
17
18use base qw(PVE::AbstractMigrate);
19
20sub lock_vm {
21 my ($self, $vmid, $code, @param) = @_;
22
67afe46e 23 return PVE::LXC::Config->lock_config($vmid, $code, @param);
6f42807e
DM
24}
25
26sub 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
108 my $cmd = ['lxc-stop', '-n', $vmid, '--timeout', $timeout];
109 $self->cmd($cmd, timeout => $timeout + 5);
110
111 # make sure container is stopped
112 $cmd = ['lxc-wait', '-n', $vmid, '-t', 5, '-s', 'STOPPED'];
113 $self->cmd($cmd);
114
115 $running = 0;
116 }
117
6f42807e
DM
118 return $running;
119}
120
121sub phase1 {
122 my ($self, $vmid) = @_;
123
124 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
125
126 my $conf = $self->{vmconf};
127 $conf->{lock} = 'migrate';
67afe46e 128 PVE::LXC::Config->write_config($vmid, $conf);
6f42807e
DM
129
130 if ($self->{running}) {
131 $self->log('info', "container is running - using online migration");
132 }
133
3c5dabe1 134 $self->{volumes} = []; # list of already migrated volumes
f503617b 135 my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
33deb76c
FG
136 my $volhash_errors = {};
137 my $abort = 0;
138
139 my $log_error = sub {
140 my ($msg, $volid) = @_;
141
142 $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid});
143 $abort = 1;
144 };
6f42807e 145
3c5dabe1
FG
146 my $test_volid = sub {
147 my ($volid, $snapname) = @_;
6f42807e 148
3c5dabe1
FG
149 return if !$volid;
150
151 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
152
153 # check if storage is available on both nodes
154 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
155 PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
156
33deb76c
FG
157 if ($scfg->{shared}) {
158 $self->log('info', "volume '$volid' is on shared storage '$sid'")
159 if !$snapname;
160 return;
161 }
3c5dabe1 162
0aa2d2a2
WB
163 $volhash->{$volid}->{ref} = defined($snapname) ? 'snapshot' : 'config';
164 $volhash->{$volid}->{snapshots} = defined($snapname);
f503617b 165
3c5dabe1
FG
166 my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid);
167
33deb76c 168 die "owned by other guest (owner = $owner)\n"
3c5dabe1 169 if !$owner || ($owner != $self->{vmid});
9746c095 170
3c5dabe1
FG
171 if (defined($snapname)) {
172 # we cannot migrate shapshots on local storage
173 # exceptions: 'zfspool'
174 if (($scfg->{type} eq 'zfspool')) {
3c5dabe1
FG
175 return;
176 }
33deb76c 177 die "non-migratable snapshot exists\n";
3c5dabe1
FG
178 }
179 };
180
181 my $test_mp = sub {
182 my ($ms, $mountpoint, $snapname) = @_;
183
184 my $volid = $mountpoint->{volume};
9746c095
FG
185 # already checked in prepare
186 if ($mountpoint->{type} ne 'volume') {
552e168f 187 $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')")
3c5dabe1 188 if !$snapname;
9746c095
FG
189 return;
190 }
191
33deb76c
FG
192 eval {
193 &$test_volid($volid, $snapname);
194 };
6f42807e 195
33deb76c 196 &$log_error($@, $volid) if $@;
3c5dabe1
FG
197 };
198
1c5f78ef 199 # first unused / lost volumes owned by this container
3c5dabe1
FG
200 my @sids = PVE::Storage::storage_ids($self->{storecfg});
201 foreach my $storeid (@sids) {
202 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid);
203 next if $scfg->{shared};
204 next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1);
205
206 # get list from PVE::Storage (for unused volumes)
207 my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid);
208
209 next if @{$dl->{$storeid}} == 0;
210
211 # check if storage is available on target node
212 PVE::Storage::storage_check_node($self->{storecfg}, $storeid, $self->{node});
213
214 PVE::Storage::foreach_volid($dl, sub {
215 my ($volid, $sid, $volname) = @_;
216
0aa2d2a2 217 $volhash->{$volid}->{ref} = 'storage';
3c5dabe1
FG
218 });
219 }
220
1c5f78ef
FG
221 # then all volumes referenced in snapshots
222 foreach my $snapname (keys %{$conf->{snapshots}}) {
223 &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef)
224 if defined($conf->{snapshots}->{$snapname}->{'vmstate'});
225 PVE::LXC::Config->foreach_mountpoint($conf->{snapshots}->{$snapname}, $test_mp, $snapname);
226 }
227
228 # finally all currently used volumes
229 PVE::LXC::Config->foreach_mountpoint($conf, $test_mp);
230
231
3c5dabe1
FG
232 # additional checks for local storage
233 foreach my $volid (keys %$volhash) {
33deb76c
FG
234 eval {
235 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
236 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid);
3c5dabe1 237
33deb76c
FG
238 my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') ||
239 ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm');
3c5dabe1 240
33deb76c
FG
241 die "storage type '$scfg->{type}' not supported\n"
242 if !$migratable;
3c5dabe1 243
33deb76c
FG
244 # image is a linked clone on local storage, se we can't migrate.
245 if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) {
246 die "clone of '$basename'";
247 }
248 };
249 &$log_error($@, $volid) if $@;
3c5dabe1
FG
250 }
251
f503617b 252 foreach my $volid (sort keys %$volhash) {
0aa2d2a2
WB
253 my $ref = $volhash->{$volid}->{ref};
254 if ($ref eq 'storage') {
f503617b 255 $self->log('info', "found local volume '$volid' (via storage)\n");
0aa2d2a2 256 } elsif ($ref eq 'config') {
f503617b 257 $self->log('info', "found local volume '$volid' (in current VM config)\n");
0aa2d2a2 258 } elsif ($ref eq 'snapshot') {
f503617b
FG
259 $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n");
260 } else {
261 $self->log('info', "found local volume '$volid'\n");
262 }
263 }
264
33deb76c
FG
265 foreach my $volid (sort keys %$volhash_errors) {
266 $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}");
267 }
268
269 if ($abort) {
270 die "can't migrate CT - check log\n";
271 }
272
3efa5e3d
DM
273 my $rep_volumes;
274
275 my $rep_cfg = PVE::ReplicationConfig->new();
276
277 if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node})) {
278 die "can't live migrate VM with replicated volumes\n" if $self->{running};
279 my $start_time = time();
280 my $logfunc = sub { my ($msg) = @_; $self->log('info', $msg); };
281 $rep_volumes = PVE::Replication::run_replication(
282 'PVE::LXC::Config', $jobcfg, $start_time, $start_time, $logfunc);
283 }
284
a53ce933 285 my $insecure = $self->{opts}->{migration_type} eq 'insecure';
3c5dabe1 286 foreach my $volid (keys %$volhash) {
3efa5e3d 287 next if $rep_volumes->{$volid};
3c5dabe1
FG
288 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
289 push @{$self->{volumes}}, $volid;
0aa2d2a2
WB
290 my $with_snapshots = $volhash->{$volid}->{snapshots};
291 PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $sid, undef, undef, undef, undef, $insecure, $with_snapshots);
3c5dabe1 292 }
6f42807e 293
67afe46e
FG
294 my $conffile = PVE::LXC::Config->config_file($vmid);
295 my $newconffile = PVE::LXC::Config->config_file($vmid, $self->{node});
6f42807e
DM
296
297 if ($self->{running}) {
298 die "implement me";
299 }
300
301 # make sure everything on (shared) storage is unmounted
302 # Note: we must be 100% sure, else we get data corruption because
303 # non-shared file system could be mounted twice (on shared storage)
304
305 PVE::LXC::umount_all($vmid, $self->{storecfg}, $conf);
306
c9bc5018 307 #to be sure there are no active volumes
d250604f 308 my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
c9bc5018
WL
309 PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
310
3efa5e3d
DM
311 # transfer replication state before move config
312 $self->transfer_replication_state();
313
6f42807e
DM
314 # move config
315 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
316 if !rename($conffile, $newconffile);
1d1c1b4f
WL
317
318 $self->{conf_migrated} = 1;
3efa5e3d
DM
319
320 $self->switch_replication_job_target();
6f42807e
DM
321}
322
323sub 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
336sub phase3 {
337 my ($self, $vmid) = @_;
338
339 my $volids = $self->{volumes};
340
341 # destroy local copies
342 foreach my $volid (@$volids) {
8fdd1fc8
DM
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$/;
6f42807e
DM
348 }
349 }
350}
351
352sub final_cleanup {
353 my ($self, $vmid) = @_;
354
355 $self->log('info', "start final cleanup");
356
1d1c1b4f
WL
357 if (!$self->{conf_migrated}) {
358 my $conf = $self->{vmconf};
359 delete $conf->{lock};
6f42807e 360
67afe46e 361 eval { PVE::LXC::Config->write_config($vmid, $conf); };
1d1c1b4f
WL
362 if (my $err = $@) {
363 $self->log('err', $err);
364 }
365 } else {
366 my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ];
367 $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");
6f42807e 368 }
a7cedb73
DC
369
370 # in restart mode, we start the container on the target node
371 # after migration
372 if ($self->{opts}->{restart} && $self->{was_running}) {
373 $self->log('info', "start container on target node");
374 my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'start', $vmid];
375 $self->cmd($cmd);
376 }
6f42807e
DM
377}
378
3791;