]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Migrate.pm
1 package PVE
::LXC
::Migrate
;
5 use PVE
::AbstractMigrate
;
7 use File
::Copy
; # fixme: remove
14 use PVE
::ReplicationConfig
;
15 use PVE
::ReplicationState
;
18 use base
qw(PVE::AbstractMigrate);
21 my ($self, $vmid, $code, @param) = @_;
23 return PVE
::LXC
::Config-
>lock_config($vmid, $code, @param);
27 my ($self, $vmid) = @_;
29 my $online = $self->{opts
}->{online
};
30 my $restart= $self->{opts
}->{restart
};
32 $self->{storecfg
} = PVE
::Storage
::config
();
35 my $conf = $self->{vmconf
} = PVE
::LXC
::Config-
>load_config($vmid);
37 PVE
::LXC
::Config-
>check_lock($conf);
40 if (PVE
::LXC
::check_running
($vmid)) {
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;
45 $self->{was_running
} = $running;
47 my $force = $self->{opts
}->{force
} // 0;
48 my $need_activate = [];
50 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
51 my ($ms, $mountpoint) = @_;
53 my $volid = $mountpoint->{volume
};
54 my $type = $mountpoint->{type
};
56 # skip dev/bind mps when forced / shared
57 if ($type ne 'volume') {
59 warn "-force is deprecated, please use the 'shared' property on individual non-volume mount points instead!\n";
62 if ($mountpoint->{shared
}) {
65 die "cannot migrate local $type mount point '$ms'\n";
69 my ($storage, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1) if $volid;
70 die "can't determine assigned storage for mount point '$ms'\n" if !$storage;
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
});
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);
83 # only activate if not shared
84 push @$need_activate, $volid;
86 # unless in restart mode because we shut the container down
87 die "unable to migrate local mount point '$volid' while CT is running"
88 if $running && !$restart;
93 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $need_activate);
95 # todo: test if VM uses local resources
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 $@;
102 # in restart mode, we shutdown the container before migrating
103 if ($restart && $running) {
104 my $timeout = $self->{opts
}->{timeout
} // 180;
106 $self->log('info', "shutdown CT $vmid\n");
108 PVE
::LXC
::vm_stop
($vmid, 0, $timeout);
117 my ($self, $vmid) = @_;
119 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
121 my $conf = $self->{vmconf
};
122 $conf->{lock} = 'migrate';
123 PVE
::LXC
::Config-
>write_config($vmid, $conf);
125 if ($self->{running
}) {
126 $self->log('info', "container is running - using online migration");
129 $self->{volumes
} = []; # list of already migrated volumes
130 my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
131 my $volhash_errors = {};
134 my $log_error = sub {
135 my ($msg, $volid) = @_;
137 $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid});
141 my $test_volid = sub {
142 my ($volid, $snapname) = @_;
146 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
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
});
152 if ($scfg->{shared
}) {
153 $self->log('info', "volume '$volid' is on shared storage '$sid'")
158 $volhash->{$volid}->{ref} = defined($snapname) ?
'snapshot' : 'config';
159 $volhash->{$volid}->{snapshots
} = defined($snapname);
161 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
163 die "owned by other guest (owner = $owner)\n"
164 if !$owner || ($owner != $self->{vmid
});
166 if (defined($snapname)) {
167 # we cannot migrate shapshots on local storage
168 # exceptions: 'zfspool'
169 if (($scfg->{type
} eq 'zfspool')) {
172 die "non-migratable snapshot exists\n";
177 my ($ms, $mountpoint, $snapname) = @_;
179 my $volid = $mountpoint->{volume
};
180 # already checked in prepare
181 if ($mountpoint->{type
} ne 'volume') {
182 $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')")
188 &$test_volid($volid, $snapname);
191 &$log_error($@, $volid) if $@;
194 # first unused / lost volumes owned by this container
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);
201 # get list from PVE::Storage (for unused volumes)
202 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
204 next if @{$dl->{$storeid}} == 0;
206 # check if storage is available on target node
207 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $storeid, $self->{node
});
209 PVE
::Storage
::foreach_volid
($dl, sub {
210 my ($volid, $sid, $volname) = @_;
212 $volhash->{$volid}->{ref} = 'storage';
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);
223 # finally all currently used volumes
224 PVE
::LXC
::Config-
>foreach_mountpoint($conf, $test_mp);
227 # additional checks for local storage
228 foreach my $volid (keys %$volhash) {
230 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
231 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
233 my $migratable = ($scfg->{type
} eq 'dir') || ($scfg->{type
} eq 'zfspool') ||
234 ($scfg->{type
} eq 'lvmthin') || ($scfg->{type
} eq 'lvm');
236 die "storage type '$scfg->{type}' not supported\n"
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'";
244 &$log_error($@, $volid) if $@;
247 foreach my $volid (sort keys %$volhash) {
248 my $ref = $volhash->{$volid}->{ref};
249 if ($ref eq 'storage') {
250 $self->log('info', "found local volume '$volid' (via storage)\n");
251 } elsif ($ref eq 'config') {
252 $self->log('info', "found local volume '$volid' (in current VM config)\n");
253 } elsif ($ref eq 'snapshot') {
254 $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n");
256 $self->log('info', "found local volume '$volid'\n");
260 foreach my $volid (sort keys %$volhash_errors) {
261 $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}");
265 die "can't migrate CT - check log\n";
270 my $rep_cfg = PVE
::ReplicationConfig-
>new();
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);
280 my $insecure = $self->{opts
}->{migration_type
} eq 'insecure';
281 foreach my $volid (keys %$volhash) {
282 next if $rep_volumes->{$volid};
283 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
284 push @{$self->{volumes
}}, $volid;
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);
289 my $conffile = PVE
::LXC
::Config-
>config_file($vmid);
290 my $newconffile = PVE
::LXC
::Config-
>config_file($vmid, $self->{node
});
292 if ($self->{running
}) {
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)
300 PVE
::LXC
::umount_all
($vmid, $self->{storecfg
}, $conf);
302 #to be sure there are no active volumes
303 my $vollist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
304 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
306 # transfer replication state before move config
307 $self->transfer_replication_state();
310 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
311 if !rename($conffile, $newconffile);
313 $self->{conf_migrated
} = 1;
315 $self->switch_replication_job_target();
319 my ($self, $vmid, $err) = @_;
321 $self->log('info', "aborting phase 1 - cleanup resources");
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 ?
332 my ($self, $vmid) = @_;
334 my $volids = $self->{volumes
};
336 # destroy local copies
337 foreach my $volid (@$volids) {
338 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
340 $self->log('err', "removing local copy of '$volid' failed - $err");
342 last if $err =~ /^interrupted by signal$/;
348 my ($self, $vmid) = @_;
350 $self->log('info', "start final cleanup");
352 if (!$self->{conf_migrated
}) {
353 my $conf = $self->{vmconf
};
354 delete $conf->{lock};
356 eval { PVE
::LXC
::Config-
>write_config($vmid, $conf); };
358 $self->log('err', $err);
361 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'unlock', $vmid ];
362 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
364 # in restart mode, we start the container on the target node
366 if ($self->{opts
}->{restart
} && $self->{was_running
}) {
367 $self->log('info', "start container on target node");
368 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'start', $vmid];