]>
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 PVE
::LXC
::Config-
>foreach_volume($conf, sub {
48 my ($ms, $mountpoint) = @_;
50 my $volid = $mountpoint->{volume
};
51 my $type = $mountpoint->{type
};
53 # skip dev/bind mps when shared
54 if ($type ne 'volume') {
55 if ($mountpoint->{shared
}) {
58 die "cannot migrate local $type mount point '$ms'\n";
62 my ($storage, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1) if $volid;
63 die "can't determine assigned storage for mount point '$ms'\n" if !$storage;
65 # check if storage is available on both nodes
66 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $storage);
67 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $storage, $self->{node
});
70 if ($scfg->{shared
}) {
71 # PVE::Storage::activate_storage checks this for non-shared storages
72 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
73 warn "Used shared storage '$storage' is not online on source node!\n"
74 if !$plugin->check_connection($storage, $scfg);
76 # unless in restart mode because we shut the container down
77 die "unable to migrate local mount point '$volid' while CT is running"
78 if $running && !$restart;
83 # todo: test if VM uses local resources
86 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
87 eval { $self->cmd_quiet($cmd); };
88 die "Can't connect to destination address using public key\n" if $@;
90 # in restart mode, we shutdown the container before migrating
91 if ($restart && $running) {
92 my $timeout = $self->{opts
}->{timeout
} // 180;
94 $self->log('info', "shutdown CT $vmid\n");
96 PVE
::LXC
::vm_stop
($vmid, 0, $timeout);
105 my ($self, $vmid) = @_;
107 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
109 my $conf = $self->{vmconf
};
110 $conf->{lock} = 'migrate';
111 PVE
::LXC
::Config-
>write_config($vmid, $conf);
113 if ($self->{running
}) {
114 $self->log('info', "container is running - using online migration");
117 $self->{volumes
} = []; # list of already migrated volumes
118 my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
119 my $volhash_errors = {};
122 my $log_error = sub {
123 my ($msg, $volid) = @_;
125 $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid});
129 my $test_volid = sub {
130 my ($volid, $snapname) = @_;
134 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
136 # check if storage is available on both nodes
137 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
138 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
140 if ($scfg->{shared
}) {
141 $self->log('info', "volume '$volid' is on shared storage '$sid'")
146 $volhash->{$volid}->{ref} = defined($snapname) ?
'snapshot' : 'config';
147 $volhash->{$volid}->{snapshots
} = defined($snapname);
149 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
151 die "owned by other guest (owner = $owner)\n"
152 if !$owner || ($owner != $self->{vmid
});
154 if (defined($snapname)) {
155 # we cannot migrate shapshots on local storage
156 # exceptions: 'zfspool'
157 if (($scfg->{type
} eq 'zfspool')) {
160 die "non-migratable snapshot exists\n";
165 my ($ms, $mountpoint, $snapname) = @_;
167 my $volid = $mountpoint->{volume
};
168 # already checked in prepare
169 if ($mountpoint->{type
} ne 'volume') {
170 $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')")
176 &$test_volid($volid, $snapname);
179 &$log_error($@, $volid) if $@;
182 # first unused / lost volumes owned by this container
183 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
184 foreach my $storeid (@sids) {
185 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
186 next if $scfg->{shared
};
187 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
189 # get list from PVE::Storage (for unused volumes)
190 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
192 next if @{$dl->{$storeid}} == 0;
194 # check if storage is available on target node
195 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $storeid, $self->{node
});
197 PVE
::Storage
::foreach_volid
($dl, sub {
198 my ($volid, $sid, $volname) = @_;
200 $volhash->{$volid}->{ref} = 'storage';
204 # then all volumes referenced in snapshots
205 foreach my $snapname (keys %{$conf->{snapshots
}}) {
206 &$test_volid($conf->{snapshots
}->{$snapname}->{'vmstate'}, 0, undef)
207 if defined($conf->{snapshots
}->{$snapname}->{'vmstate'});
208 PVE
::LXC
::Config-
>foreach_volume($conf->{snapshots
}->{$snapname}, $test_mp, $snapname);
211 # finally all currently used volumes
212 PVE
::LXC
::Config-
>foreach_volume($conf, $test_mp);
215 # additional checks for local storage
216 foreach my $volid (keys %$volhash) {
218 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
219 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
221 my $migratable = ($scfg->{type
} eq 'dir') || ($scfg->{type
} eq 'zfspool') ||
222 ($scfg->{type
} eq 'lvmthin') || ($scfg->{type
} eq 'lvm');
224 die "storage type '$scfg->{type}' not supported\n"
227 # image is a linked clone on local storage, se we can't migrate.
228 if (my $basename = (PVE
::Storage
::parse_volname
($self->{storecfg
}, $volid))[3]) {
229 die "clone of '$basename'";
232 &$log_error($@, $volid) if $@;
235 foreach my $volid (sort keys %$volhash) {
236 my $ref = $volhash->{$volid}->{ref};
237 if ($ref eq 'storage') {
238 $self->log('info', "found local volume '$volid' (via storage)\n");
239 } elsif ($ref eq 'config') {
240 $self->log('info', "found local volume '$volid' (in current VM config)\n");
241 } elsif ($ref eq 'snapshot') {
242 $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n");
244 $self->log('info', "found local volume '$volid'\n");
248 foreach my $volid (sort keys %$volhash_errors) {
249 $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}");
253 die "can't migrate CT - check log\n";
258 my $rep_cfg = PVE
::ReplicationConfig-
>new();
260 if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node
})) {
261 die "can't live migrate VM with replicated volumes\n" if $self->{running
};
262 my $start_time = time();
263 my $logfunc = sub { my ($msg) = @_; $self->log('info', $msg); };
264 $rep_volumes = PVE
::Replication
::run_replication
(
265 'PVE::LXC::Config', $jobcfg, $start_time, $start_time, $logfunc);
268 my $opts = $self->{opts
};
269 foreach my $volid (keys %$volhash) {
270 next if $rep_volumes->{$volid};
271 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
272 push @{$self->{volumes
}}, $volid;
273 my $bwlimit = PVE
::Storage
::get_bandwidth_limit
('migration', [$sid], $opts->{bwlimit
});
274 # JSONSchema and get_bandwidth_limit use kbps - storage_migrate bps
275 $bwlimit = $bwlimit * 1024 if defined($bwlimit);
277 my $storage_migrate_opts = {
278 'ratelimit_bps' => $bwlimit,
279 'insecure' => $opts->{migration_type
} eq 'insecure',
280 'with_snapshots' => $volhash->{$volid}->{snapshots
},
283 my $logfunc = sub { $self->log('info', $_[0]); };
285 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{ssh_info
},
286 $sid, $storage_migrate_opts, $logfunc);
289 die "storage migration for '$volid' to storage '$sid' failed - $err\n";
292 eval { PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, [$volid]); };
294 $self->log('warn', $err);
298 if ($self->{running
}) {
302 # make sure everything on (shared) storage is unmounted
303 # Note: we must be 100% sure, else we get data corruption because
304 # non-shared file system could be mounted twice (on shared storage)
306 PVE
::LXC
::umount_all
($vmid, $self->{storecfg
}, $conf);
308 #to be sure there are no active volumes
309 my $vollist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
310 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
312 # transfer replication state before moving config
313 $self->transfer_replication_state() if $rep_volumes;
314 PVE
::LXC
::Config-
>move_config_to_node($vmid, $self->{node
});
315 $self->{conf_migrated
} = 1;
316 $self->switch_replication_job_target() if $rep_volumes;
320 my ($self, $vmid, $err) = @_;
322 $self->log('info', "aborting phase 1 - cleanup resources");
324 if ($self->{volumes
}) {
325 foreach my $volid (@{$self->{volumes
}}) {
326 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
327 # fixme: try to remove ?
333 my ($self, $vmid) = @_;
335 my $volids = $self->{volumes
};
337 # destroy local copies
338 foreach my $volid (@$volids) {
339 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
341 $self->log('err', "removing local copy of '$volid' failed - $err");
343 last if $err =~ /^interrupted by signal$/;
349 my ($self, $vmid) = @_;
351 $self->log('info', "start final cleanup");
353 if (!$self->{conf_migrated
}) {
354 eval { PVE
::LXC
::Config-
>remove_lock($vmid, 'migrate'); };
356 $self->log('err', $err);
358 # in restart mode, we start the container on the source node
360 if ($self->{opts
}->{restart
} && $self->{was_running
}) {
361 $self->log('info', "start container on source node");
363 PVE
::LXC
::vm_start
($vmid, $self->{vmconf
}, $skiplock);
366 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'unlock', $vmid ];
367 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
369 # in restart mode, we start the container on the target node
371 if ($self->{opts
}->{restart
} && $self->{was_running
}) {
372 $self->log('info', "start container on target node");
373 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'start', $vmid];