]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Migrate.pm
64bdd3841c4a75f8f4029fb69fa92b06813a3870
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_full($conf, { include_unused
=> 1 }, 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_enabled
($self->{storecfg
}, $storage);
68 my $targetsid = $storage;
70 die "content type 'rootdir' is not available on storage '$storage'\n"
71 if !$scfg->{content
}->{rootdir
};
73 if ($scfg->{shared
}) {
74 # PVE::Storage::activate_storage checks this for non-shared storages
75 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
76 warn "Used shared storage '$storage' is not online on source node!\n"
77 if !$plugin->check_connection($storage, $scfg);
79 # unless in restart mode because we shut the container down
80 die "unable to migrate local mount point '$volid' while CT is running"
81 if $running && !$restart;
83 $targetsid = PVE
::JSONSchema
::map_id
($self->{opts
}->{storagemap
}, $storage);
86 my $target_scfg = PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $targetsid, $self->{node
});
88 die "$volid: content type 'rootdir' is not available on storage '$targetsid'\n"
89 if !$target_scfg->{content
}->{rootdir
};
92 # todo: test if VM uses local resources
95 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
96 eval { $self->cmd_quiet($cmd); };
97 die "Can't connect to destination address using public key\n" if $@;
99 # in restart mode, we shutdown the container before migrating
100 if ($restart && $running) {
101 my $timeout = $self->{opts
}->{timeout
} // 180;
103 $self->log('info', "shutdown CT $vmid\n");
105 PVE
::LXC
::vm_stop
($vmid, 0, $timeout);
114 my ($self, $vmid) = @_;
116 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
118 my $conf = $self->{vmconf
};
119 $conf->{lock} = 'migrate';
120 PVE
::LXC
::Config-
>write_config($vmid, $conf);
122 if ($self->{running
}) {
123 $self->log('info', "container is running - using online migration");
126 $self->{volumes
} = []; # list of already migrated volumes
127 my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
128 my $volhash_errors = {};
131 my $log_error = sub {
132 my ($msg, $volid) = @_;
134 $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid});
138 my $test_volid = sub {
139 my ($volid, $snapname) = @_;
143 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
145 # check if storage is available on source node
146 my $scfg = PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $sid);
148 my $targetsid = $sid;
150 if ($scfg->{shared
}) {
151 $self->log('info', "volume '$volid' is on shared storage '$sid'")
155 $targetsid = PVE
::JSONSchema
::map_id
($self->{opts
}->{storagemap
}, $sid);
158 PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $targetsid, $self->{node
});
160 my $bwlimit = $self->get_bwlimit($sid, $targetsid);
162 $volhash->{$volid}->{ref} = defined($snapname) ?
'snapshot' : 'config';
163 $volhash->{$volid}->{snapshots
} = 1 if defined($snapname);
164 $volhash->{$volid}->{targetsid
} = $targetsid;
165 $volhash->{$volid}->{bwlimit
} = $bwlimit;
167 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
169 die "owned by other guest (owner = $owner)\n"
170 if !$owner || ($owner != $self->{vmid
});
172 if (defined($snapname)) {
173 # we cannot migrate shapshots on local storage
174 # exceptions: 'zfspool', 'btrfs'
175 if ($scfg->{type
} eq 'zfspool' || $scfg->{type
} eq 'btrfs') {
178 die "non-migratable snapshot exists\n";
183 my ($ms, $mountpoint, $snapname) = @_;
185 my $volid = $mountpoint->{volume
};
186 # already checked in prepare
187 if ($mountpoint->{type
} ne 'volume') {
188 $self->log('info', "ignoring shared '$mountpoint->{type}' mount point '$ms' ('$volid')")
194 &$test_volid($volid, $snapname);
197 &$log_error($@, $volid) if $@;
200 # first unused / lost volumes owned by this container
201 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
202 foreach my $storeid (@sids) {
203 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
204 next if $scfg->{shared
};
205 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
207 # get list from PVE::Storage (for unreferenced volumes)
208 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid, undef, 'rootdir');
210 next if @{$dl->{$storeid}} == 0;
212 # check if storage is available on target node
213 my $targetsid = PVE
::JSONSchema
::map_id
($self->{opts
}->{storagemap
}, $storeid);
214 my $target_scfg = PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $targetsid, $self->{node
});
216 die "content type 'rootdir' is not available on storage '$targetsid'\n"
217 if !$target_scfg->{content
}->{rootdir
};
219 PVE
::Storage
::foreach_volid
($dl, sub {
220 my ($volid, $sid, $volname) = @_;
222 $volhash->{$volid}->{ref} = 'storage';
226 # then all volumes referenced in snapshots
227 foreach my $snapname (keys %{$conf->{snapshots
}}) {
228 &$test_volid($conf->{snapshots
}->{$snapname}->{'vmstate'}, 0, undef)
229 if defined($conf->{snapshots
}->{$snapname}->{'vmstate'});
230 PVE
::LXC
::Config-
>foreach_volume($conf->{snapshots
}->{$snapname}, $test_mp, $snapname);
233 # finally all current volumes
234 PVE
::LXC
::Config-
>foreach_volume_full($conf, { include_unused
=> 1 }, $test_mp);
236 # additional checks for local storage
237 foreach my $volid (keys %$volhash) {
239 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
240 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
242 my $migratable = ($scfg->{type
} eq 'dir') || ($scfg->{type
} eq 'zfspool')
243 || ($scfg->{type
} eq 'lvmthin') || ($scfg->{type
} eq 'lvm')
244 || ($scfg->{type
} eq 'btrfs');
246 die "storage type '$scfg->{type}' not supported\n"
249 # image is a linked clone on local storage, se we can't migrate.
250 if (my $basename = (PVE
::Storage
::parse_volname
($self->{storecfg
}, $volid))[3]) {
251 die "clone of '$basename'";
254 &$log_error($@, $volid) if $@;
257 foreach my $volid (sort keys %$volhash) {
258 my $ref = $volhash->{$volid}->{ref};
259 if ($ref eq 'storage') {
260 $self->log('info', "found local volume '$volid' (via storage)\n");
261 } elsif ($ref eq 'config') {
262 $self->log('info', "found local volume '$volid' (in current VM config)\n");
263 } elsif ($ref eq 'snapshot') {
264 $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n");
266 $self->log('info', "found local volume '$volid'\n");
270 foreach my $volid (sort keys %$volhash_errors) {
271 $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}");
275 die "can't migrate CT - check log\n";
280 my $rep_cfg = PVE
::ReplicationConfig-
>new();
282 if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node
})) {
283 die "can't live migrate VM with replicated volumes\n" if $self->{running
};
284 my $start_time = time();
285 my $logfunc = sub { my ($msg) = @_; $self->log('info', $msg); };
286 $rep_volumes = PVE
::Replication
::run_replication
(
287 'PVE::LXC::Config', $jobcfg, $start_time, $start_time, $logfunc);
290 my $opts = $self->{opts
};
291 foreach my $volid (keys %$volhash) {
292 next if $rep_volumes->{$volid};
293 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
294 push @{$self->{volumes
}}, $volid;
296 # JSONSchema and get_bandwidth_limit use kbps - storage_migrate bps
297 my $bwlimit = $volhash->{$volid}->{bwlimit
};
298 $bwlimit = $bwlimit * 1024 if defined($bwlimit);
300 my $targetsid = $volhash->{$volid}->{targetsid
};
302 my $new_volid = eval {
303 my $storage_migrate_opts = {
304 'ratelimit_bps' => $bwlimit,
305 'insecure' => $opts->{migration_type
} eq 'insecure',
306 'with_snapshots' => $volhash->{$volid}->{snapshots
},
310 my $logfunc = sub { $self->log('info', $_[0]); };
311 return PVE
::Storage
::storage_migrate
(
316 $storage_migrate_opts,
322 die "storage migration for '$volid' to storage '$targetsid' failed - $err\n";
325 $self->{volume_map
}->{$volid} = $new_volid;
326 $self->log('info', "volume '$volid' is '$new_volid' on the target\n");
328 eval { PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, [$volid]); };
330 $self->log('warn', $err);
334 if ($self->{running
}) {
338 # make sure everything on (shared) storage is unmounted
339 # Note: we must be 100% sure, else we get data corruption because
340 # non-shared file system could be mounted twice (on shared storage)
342 PVE
::LXC
::umount_all
($vmid, $self->{storecfg
}, $conf);
344 #to be sure there are no active volumes
345 my $vollist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
346 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
348 # transfer replication state before moving config
349 $self->transfer_replication_state() if $rep_volumes;
350 PVE
::LXC
::Config-
>update_volume_ids($conf, $self->{volume_map
});
351 PVE
::LXC
::Config-
>write_config($vmid, $conf);
352 PVE
::LXC
::Config-
>move_config_to_node($vmid, $self->{node
});
353 $self->{conf_migrated
} = 1;
354 $self->switch_replication_job_target() if $rep_volumes;
358 my ($self, $vmid, $err) = @_;
360 $self->log('info', "aborting phase 1 - cleanup resources");
362 if ($self->{volumes
}) {
363 foreach my $volid (@{$self->{volumes
}}) {
364 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
365 # fixme: try to remove ?
371 my ($self, $vmid) = @_;
373 my $volids = $self->{volumes
};
375 # destroy local copies
376 foreach my $volid (@$volids) {
377 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
379 $self->log('err', "removing local copy of '$volid' failed - $err");
381 last if $err =~ /^interrupted by signal$/;
387 my ($self, $vmid) = @_;
389 $self->log('info', "start final cleanup");
391 if (!$self->{conf_migrated
}) {
392 eval { PVE
::LXC
::Config-
>remove_lock($vmid, 'migrate'); };
394 $self->log('err', $err);
396 # in restart mode, we start the container on the source node
398 if ($self->{opts
}->{restart
} && $self->{was_running
}) {
399 $self->log('info', "start container on source node");
401 PVE
::LXC
::vm_start
($vmid, $self->{vmconf
}, $skiplock);
404 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'unlock', $vmid ];
405 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
407 # in restart mode, we start the container on the target node
409 if ($self->{opts
}->{restart
} && $self->{was_running
}) {
410 $self->log('info', "start container on target node");
411 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'start', $vmid];