]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Migrate.pm
fix bug #750: deactivate volumes to be sure there are no volumes active on the source...
[pve-container.git] / src / PVE / LXC / Migrate.pm
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
19 return PVE::LXC::lock_container($vmid, undef, $code, @param);
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
30 my $conf = $self->{vmconf} = PVE::LXC::load_config($vmid);
31
32 PVE::LXC::check_lock($conf);
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
42 PVE::LXC::foreach_mountpoint($conf, sub {
43 my ($ms, $mountpoint) = @_;
44
45 my $volid = $mountpoint->{volume};
46 my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1) if $volid;
47 die "can't determine assigned storage for mountpoint '$ms'\n" if !$storage;
48
49 # check if storage is available on both nodes
50 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $storage);
51 PVE::Storage::storage_check_node($self->{storecfg}, $storage, $self->{node});
52
53 die "unable to migrate local mountpoint '$volid' while CT is running"
54 if !$scfg->{shared} && $running;
55
56 });
57
58 my $volid_list = PVE::LXC::get_vm_volumes($conf);
59 PVE::Storage::activate_volumes($self->{storecfg}, $volid_list);
60
61 # todo: test if VM uses local resources
62
63 # test ssh connection
64 my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ];
65 eval { $self->cmd_quiet($cmd); };
66 die "Can't connect to destination address using public key\n" if $@;
67
68 return $running;
69 }
70
71 sub phase1 {
72 my ($self, $vmid) = @_;
73
74 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
75
76 my $conf = $self->{vmconf};
77 $conf->{lock} = 'migrate';
78 PVE::LXC::write_config($vmid, $conf);
79
80 if ($self->{running}) {
81 $self->log('info', "container is running - using online migration");
82 }
83
84 $self->{volumes} = [];
85
86 PVE::LXC::foreach_mountpoint($conf, sub {
87 my ($ms, $mountpoint) = @_;
88
89 my $volid = $mountpoint->{volume};
90 my ($storage, $volname) = PVE::Storage::parse_volume_id($volid);
91 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $storage);
92
93 if (!$scfg->{shared}) {
94
95 $self->log('info', "copy mointpoint '$ms' ($volid) to node ' $self->{node}'");
96 PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $storage);
97 push @{$self->{volumes}}, $volid;
98 } else {
99 $self->log('info', "mointpoint '$ms' is on shared storage '$storage'");
100 }
101 });
102
103 my $conffile = PVE::LXC::config_file($vmid);
104 my $newconffile = PVE::LXC::config_file($vmid, $self->{node});
105
106 if ($self->{running}) {
107 die "implement me";
108 }
109
110 # make sure everything on (shared) storage is unmounted
111 # Note: we must be 100% sure, else we get data corruption because
112 # non-shared file system could be mounted twice (on shared storage)
113
114 PVE::LXC::umount_all($vmid, $self->{storecfg}, $conf);
115
116 #to be sure there are no active volumes
117 my $vollist = PVE::LXC::get_vm_volumes($conf);
118 PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
119
120 # move config
121 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
122 if !rename($conffile, $newconffile);
123
124 $self->{conf_migrated} = 1;
125 }
126
127 sub phase1_cleanup {
128 my ($self, $vmid, $err) = @_;
129
130 $self->log('info', "aborting phase 1 - cleanup resources");
131
132 if ($self->{volumes}) {
133 foreach my $volid (@{$self->{volumes}}) {
134 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
135 # fixme: try to remove ?
136 }
137 }
138 }
139
140 sub phase3 {
141 my ($self, $vmid) = @_;
142
143 my $volids = $self->{volumes};
144
145 # destroy local copies
146 foreach my $volid (@$volids) {
147 eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
148 if (my $err = $@) {
149 $self->log('err', "removing local copy of '$volid' failed - $err");
150 $self->{errors} = 1;
151 last if $err =~ /^interrupted by signal$/;
152 }
153 }
154 }
155
156 sub final_cleanup {
157 my ($self, $vmid) = @_;
158
159 $self->log('info', "start final cleanup");
160
161 if (!$self->{conf_migrated}) {
162 my $conf = $self->{vmconf};
163 delete $conf->{lock};
164
165 eval { PVE::LXC::write_config($vmid, $conf); };
166 if (my $err = $@) {
167 $self->log('err', $err);
168 }
169 } else {
170 my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ];
171 $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");
172 }
173 }
174
175 1;