]>
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
};
50 # skip dev/bind mps when forced
51 if ($mountpoint->{type
} ne 'volume' && $force) {
54 my ($storage, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1) if $volid;
55 die "can't determine assigned storage for mountpoint '$ms'\n" if !$storage;
57 # check if storage is available on both nodes
58 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $storage);
59 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $storage, $self->{node
});
62 if ($scfg->{shared
}) {
63 # PVE::Storage::activate_storage checks this for non-shared storages
64 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
65 warn "Used shared storage '$storage' is not online on source node!\n"
66 if !$plugin->check_connection($storage, $scfg);
68 # only activate if not shared
69 push @$need_activate, $volid;
71 die "unable to migrate local mountpoint '$volid' while CT is running"
77 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $need_activate);
79 # todo: test if VM uses local resources
82 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
83 eval { $self->cmd_quiet($cmd); };
84 die "Can't connect to destination address using public key\n" if $@;
90 my ($self, $vmid) = @_;
92 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
94 my $conf = $self->{vmconf
};
95 $conf->{lock} = 'migrate';
96 PVE
::LXC
::Config-
>write_config($vmid, $conf);
98 if ($self->{running
}) {
99 $self->log('info', "container is running - using online migration");
102 $self->{volumes
} = []; # list of already migrated volumes
103 my $volhash = {}; # 'config', 'snapshot' or 'storage' for local volumes
104 my $volhash_errors = {};
107 my $log_error = sub {
108 my ($msg, $volid) = @_;
110 $volhash_errors->{$volid} = $msg if !defined($volhash_errors->{$volid});
114 my $test_volid = sub {
115 my ($volid, $snapname) = @_;
119 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
121 # check if storage is available on both nodes
122 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
123 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
125 if ($scfg->{shared
}) {
126 $self->log('info', "volume '$volid' is on shared storage '$sid'")
131 $volhash->{$volid} = defined($snapname) ?
'snapshot' : 'config';
133 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
135 die "owned by other guest (owner = $owner)\n"
136 if !$owner || ($owner != $self->{vmid
});
138 if (defined($snapname)) {
139 # we cannot migrate shapshots on local storage
140 # exceptions: 'zfspool'
141 if (($scfg->{type
} eq 'zfspool')) {
144 die "non-migratable snapshot exists\n";
149 my ($ms, $mountpoint, $snapname) = @_;
151 my $volid = $mountpoint->{volume
};
152 # already checked in prepare
153 if ($mountpoint->{type
} ne 'volume') {
154 $self->log('info', "ignoring mountpoint '$ms' ('$volid') of type " .
155 "'$mountpoint->{type}', migration is forced.")
161 &$test_volid($volid, $snapname);
164 &$log_error($@, $volid) if $@;
167 # first unused / lost volumes owned by this container
168 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
169 foreach my $storeid (@sids) {
170 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
171 next if $scfg->{shared
};
172 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
174 # get list from PVE::Storage (for unused volumes)
175 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
177 next if @{$dl->{$storeid}} == 0;
179 # check if storage is available on target node
180 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $storeid, $self->{node
});
182 PVE
::Storage
::foreach_volid
($dl, sub {
183 my ($volid, $sid, $volname) = @_;
185 $volhash->{$volid} = 'storage';
189 # then all volumes referenced in snapshots
190 foreach my $snapname (keys %{$conf->{snapshots
}}) {
191 &$test_volid($conf->{snapshots
}->{$snapname}->{'vmstate'}, 0, undef)
192 if defined($conf->{snapshots
}->{$snapname}->{'vmstate'});
193 PVE
::LXC
::Config-
>foreach_mountpoint($conf->{snapshots
}->{$snapname}, $test_mp, $snapname);
196 # finally all currently used volumes
197 PVE
::LXC
::Config-
>foreach_mountpoint($conf, $test_mp);
200 # additional checks for local storage
201 foreach my $volid (keys %$volhash) {
203 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
204 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
206 my $migratable = ($scfg->{type
} eq 'dir') || ($scfg->{type
} eq 'zfspool') ||
207 ($scfg->{type
} eq 'lvmthin') || ($scfg->{type
} eq 'lvm');
209 die "storage type '$scfg->{type}' not supported\n"
212 # image is a linked clone on local storage, se we can't migrate.
213 if (my $basename = (PVE
::Storage
::parse_volname
($self->{storecfg
}, $volid))[3]) {
214 die "clone of '$basename'";
217 &$log_error($@, $volid) if $@;
220 foreach my $volid (sort keys %$volhash) {
221 if ($volhash->{$volid} eq 'storage') {
222 $self->log('info', "found local volume '$volid' (via storage)\n");
223 } elsif ($volhash->{$volid} eq 'config') {
224 $self->log('info', "found local volume '$volid' (in current VM config)\n");
225 } elsif ($volhash->{$volid} eq 'snapshot') {
226 $self->log('info', "found local volume '$volid' (referenced by snapshot(s))\n");
228 $self->log('info', "found local volume '$volid'\n");
232 foreach my $volid (sort keys %$volhash_errors) {
233 $self->log('warn', "can't migrate local volume '$volid': $volhash_errors->{$volid}");
237 die "can't migrate CT - check log\n";
240 foreach my $volid (keys %$volhash) {
241 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
242 push @{$self->{volumes
}}, $volid;
243 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{nodeip
}, $sid);
246 my $conffile = PVE
::LXC
::Config-
>config_file($vmid);
247 my $newconffile = PVE
::LXC
::Config-
>config_file($vmid, $self->{node
});
249 if ($self->{running
}) {
253 # make sure everything on (shared) storage is unmounted
254 # Note: we must be 100% sure, else we get data corruption because
255 # non-shared file system could be mounted twice (on shared storage)
257 PVE
::LXC
::umount_all
($vmid, $self->{storecfg
}, $conf);
259 #to be sure there are no active volumes
260 my $vollist = PVE
::LXC
::Config-
>get_vm_volumes($conf);
261 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
264 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
265 if !rename($conffile, $newconffile);
267 $self->{conf_migrated
} = 1;
271 my ($self, $vmid, $err) = @_;
273 $self->log('info', "aborting phase 1 - cleanup resources");
275 if ($self->{volumes
}) {
276 foreach my $volid (@{$self->{volumes
}}) {
277 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
278 # fixme: try to remove ?
284 my ($self, $vmid) = @_;
286 my $volids = $self->{volumes
};
288 # destroy local copies
289 foreach my $volid (@$volids) {
290 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
292 $self->log('err', "removing local copy of '$volid' failed - $err");
294 last if $err =~ /^interrupted by signal$/;
300 my ($self, $vmid) = @_;
302 $self->log('info', "start final cleanup");
304 if (!$self->{conf_migrated
}) {
305 my $conf = $self->{vmconf
};
306 delete $conf->{lock};
308 eval { PVE
::LXC
::Config-
>write_config($vmid, $conf); };
310 $self->log('err', $err);
313 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'unlock', $vmid ];
314 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");