]>
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 base
qw(PVE::AbstractMigrate);
17 my ($self, $vmid, $code, @param) = @_;
19 return PVE
::LXC
::Config-
>lock_config($vmid, $code, @param);
23 my ($self, $vmid) = @_;
25 my $online = $self->{opts
}->{online
};
27 $self->{storecfg
} = PVE
::Storage
::config
();
30 my $conf = $self->{vmconf
} = PVE
::LXC
::Config-
>load_config($vmid);
32 PVE
::LXC
::Config-
>check_lock($conf);
35 if (PVE
::LXC
::check_running
($vmid)) {
36 die "lxc live migration is currently not implemented\n";
38 die "can't migrate running container without --online\n" if !$online;
42 my $force = $self->{opts
}->{force
} // 0;
43 my $need_activate = [];
45 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
46 my ($ms, $mountpoint) = @_;
48 my $volid = $mountpoint->{volume
};
49 my $type = $mountpoint->{type
};
51 # skip dev/bind mps when forced / shared
52 if ($type ne 'volume') {
54 warn "-force is deprecated, please use the 'shared' property on individual non-volume mount points instead!\n";
57 if ($mountpoint->{shared
}) {
60 die "cannot migrate local $type mount point '$ms'\n";
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;
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
});
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);
78 # only activate if not shared
79 push @$need_activate, $volid;
81 die "unable to migrate local mountpoint '$volid' while CT is running"
87 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $need_activate);
89 # todo: test if VM uses local resources
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 $@;
100 my ($self, $vmid) = @_;
102 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
104 my $conf = $self->{vmconf
};
105 $conf->{lock} = 'migrate';
106 PVE
::LXC
::Config-
>write_config($vmid, $conf);
108 if ($self->{running
}) {
109 $self->log('info', "container is running - using online migration");
112 $self->{volumes
} = []; # list of already migrated volumes
113 my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
114 my $volhash_errors = {};
117 my $log_error = sub {
118 my ($msg, $volid) = @_;
120 $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid});
124 my $test_volid = sub {
125 my ($volid, $snapname) = @_;
129 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
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
});
135 if ($scfg->{shared
}) {
136 $self->log('info', "volume '$volid' is on shared storage '$sid'")
141 $volhash->{$volid} = defined($snapname) ?
'snapshot' : 'config';
143 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
145 die "owned by other guest (owner = $owner)\n"
146 if !$owner || ($owner != $self->{vmid
});
148 if (defined($snapname)) {
149 # we cannot migrate shapshots on local storage
150 # exceptions: 'zfspool'
151 if (($scfg->{type
} eq 'zfspool')) {
154 die "non-migratable snapshot exists\n";
159 my ($ms, $mountpoint, $snapname) = @_;
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')")
170 &$test_volid($volid, $snapname);
173 &$log_error($@, $volid) if $@;
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);
183 # get list from PVE::Storage (for unused volumes)
184 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
186 next if @{$dl->{$storeid}} == 0;
188 # check if storage is available on target node
189 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $storeid, $self->{node
});
191 PVE
::Storage
::foreach_volid
($dl, sub {
192 my ($volid, $sid, $volname) = @_;
194 $volhash->{$volid} = 'storage';
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);
205 # finally all currently used volumes
206 PVE
::LXC
::Config-
>foreach_mountpoint($conf, $test_mp);
209 # additional checks for local storage
210 foreach my $volid (keys %$volhash) {
212 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
213 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
215 my $migratable = ($scfg->{type
} eq 'dir') || ($scfg->{type
} eq 'zfspool') ||
216 ($scfg->{type
} eq 'lvmthin') || ($scfg->{type
} eq 'lvm');
218 die "storage type '$scfg->{type}' not supported\n"
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'";
226 &$log_error($@, $volid) if $@;
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");
237 $self->log('info', "found local volume '$volid'\n");
241 foreach my $volid (sort keys %$volhash_errors) {
242 $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}");
246 die "can't migrate CT - check log\n";
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);
255 my $conffile = PVE
::LXC
::Config-
>config_file($vmid);
256 my $newconffile = PVE
::LXC
::Config-
>config_file($vmid, $self->{node
});
258 if ($self->{running
}) {
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)
266 PVE
::LXC
::umount_all
($vmid, $self->{storecfg
}, $conf);
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);
273 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
274 if !rename($conffile, $newconffile);
276 $self->{conf_migrated
} = 1;
280 my ($self, $vmid, $err) = @_;
282 $self->log('info', "aborting phase 1 - cleanup resources");
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 ?
293 my ($self, $vmid) = @_;
295 my $volids = $self->{volumes
};
297 # destroy local copies
298 foreach my $volid (@$volids) {
299 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
301 $self->log('err', "removing local copy of '$volid' failed - $err");
303 last if $err =~ /^interrupted by signal$/;
309 my ($self, $vmid) = @_;
311 $self->log('info', "start final cleanup");
313 if (!$self->{conf_migrated
}) {
314 my $conf = $self->{vmconf
};
315 delete $conf->{lock};
317 eval { PVE
::LXC
::Config-
>write_config($vmid, $conf); };
319 $self->log('err', $err);
322 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'unlock', $vmid ];
323 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");