]> git.proxmox.com Git - pve-container.git/blame - src/PVE/LXC/Migrate.pm
fix #971: don't activate shared storage in offline migration
[pve-container.git] / src / PVE / LXC / Migrate.pm
CommitLineData
6f42807e
DM
1package PVE::LXC::Migrate;
2
3use strict;
4use warnings;
5use PVE::AbstractMigrate;
6use File::Basename;
7use File::Copy; # fixme: remove
8use PVE::Tools;
9use PVE::INotify;
10use PVE::Cluster;
11use PVE::Storage;
12use PVE::LXC;
13
14use base qw(PVE::AbstractMigrate);
15
16sub lock_vm {
17 my ($self, $vmid, $code, @param) = @_;
18
67afe46e 19 return PVE::LXC::Config->lock_config($vmid, $code, @param);
6f42807e
DM
20}
21
22sub prepare {
23 my ($self, $vmid) = @_;
24
25 my $online = $self->{opts}->{online};
26
27 $self->{storecfg} = PVE::Storage::config();
28
29 # test is VM exist
67afe46e 30 my $conf = $self->{vmconf} = PVE::LXC::Config->load_config($vmid);
6f42807e 31
67afe46e 32 PVE::LXC::Config->check_lock($conf);
6f42807e
DM
33
34 my $running = 0;
35 if (PVE::LXC::check_running($vmid)) {
36 die "lxc live migration is currently not implemented\n";
37
fc181735 38 die "can't migrate running container without --online\n" if !$online;
6f42807e
DM
39 $running = 1;
40 }
41
9746c095 42 my $force = $self->{opts}->{force} // 0;
20ab40f3 43 my $need_activate = [];
9746c095 44
d250604f 45 PVE::LXC::Config->foreach_mountpoint($conf, sub {
6f42807e
DM
46 my ($ms, $mountpoint) = @_;
47
48 my $volid = $mountpoint->{volume};
9746c095
FG
49
50 # skip dev/bind mps when forced
51 if ($mountpoint->{type} ne 'volume' && $force) {
52 return;
53 }
6f42807e
DM
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;
56
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});
60
20ab40f3
FG
61
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);
67 } else {
68 # only activate if not shared
69 push @$need_activate, $volid;
70
71 die "unable to migrate local mountpoint '$volid' while CT is running"
72 if $running;
73 }
6f42807e
DM
74
75 });
76
20ab40f3 77 PVE::Storage::activate_volumes($self->{storecfg}, $need_activate);
6f42807e
DM
78
79 # todo: test if VM uses local resources
80
81 # test ssh connection
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 $@;
85
86 return $running;
87}
88
89sub phase1 {
90 my ($self, $vmid) = @_;
91
92 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
93
94 my $conf = $self->{vmconf};
95 $conf->{lock} = 'migrate';
67afe46e 96 PVE::LXC::Config->write_config($vmid, $conf);
6f42807e
DM
97
98 if ($self->{running}) {
99 $self->log('info', "container is running - using online migration");
100 }
101
102 $self->{volumes} = [];
103
d250604f 104 PVE::LXC::Config->foreach_mountpoint($conf, sub {
6f42807e
DM
105 my ($ms, $mountpoint) = @_;
106
107 my $volid = $mountpoint->{volume};
9746c095
FG
108
109 # already checked in prepare
110 if ($mountpoint->{type} ne 'volume') {
111 $self->log('info', "ignoring mountpoint '$ms' ('$volid') of type " .
112 "'$mountpoint->{type}', migration is forced.");
113 return;
114 }
115
6f42807e
DM
116 my ($storage, $volname) = PVE::Storage::parse_volume_id($volid);
117 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $storage);
118
119 if (!$scfg->{shared}) {
120
fc181735 121 $self->log('info', "copy mountpoint '$ms' ($volid) to node ' $self->{node}'");
6f42807e
DM
122 PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $storage);
123 push @{$self->{volumes}}, $volid;
124 } else {
fc181735 125 $self->log('info', "mountpoint '$ms' is on shared storage '$storage'");
6f42807e
DM
126 }
127 });
128
67afe46e
FG
129 my $conffile = PVE::LXC::Config->config_file($vmid);
130 my $newconffile = PVE::LXC::Config->config_file($vmid, $self->{node});
6f42807e
DM
131
132 if ($self->{running}) {
133 die "implement me";
134 }
135
136 # make sure everything on (shared) storage is unmounted
137 # Note: we must be 100% sure, else we get data corruption because
138 # non-shared file system could be mounted twice (on shared storage)
139
140 PVE::LXC::umount_all($vmid, $self->{storecfg}, $conf);
141
c9bc5018 142 #to be sure there are no active volumes
d250604f 143 my $vollist = PVE::LXC::Config->get_vm_volumes($conf);
c9bc5018
WL
144 PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
145
6f42807e
DM
146 # move config
147 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
148 if !rename($conffile, $newconffile);
1d1c1b4f
WL
149
150 $self->{conf_migrated} = 1;
6f42807e
DM
151}
152
153sub phase1_cleanup {
154 my ($self, $vmid, $err) = @_;
155
156 $self->log('info', "aborting phase 1 - cleanup resources");
157
158 if ($self->{volumes}) {
159 foreach my $volid (@{$self->{volumes}}) {
160 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
161 # fixme: try to remove ?
162 }
163 }
164}
165
166sub phase3 {
167 my ($self, $vmid) = @_;
168
169 my $volids = $self->{volumes};
170
171 # destroy local copies
172 foreach my $volid (@$volids) {
173 eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
174 if (my $err = $@) {
175 $self->log('err', "removing local copy of '$volid' failed - $err");
176 $self->{errors} = 1;
177 last if $err =~ /^interrupted by signal$/;
178 }
179 }
180}
181
182sub final_cleanup {
183 my ($self, $vmid) = @_;
184
185 $self->log('info', "start final cleanup");
186
1d1c1b4f
WL
187 if (!$self->{conf_migrated}) {
188 my $conf = $self->{vmconf};
189 delete $conf->{lock};
6f42807e 190
67afe46e 191 eval { PVE::LXC::Config->write_config($vmid, $conf); };
1d1c1b4f
WL
192 if (my $err = $@) {
193 $self->log('err', $err);
194 }
195 } else {
196 my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ];
197 $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");
6f42807e
DM
198 }
199}
200
2011;