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