]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Migrate.pm
fix unlock handling after migration.
[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 # move config
117 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
118 if !rename($conffile, $newconffile);
119
120 $self->{conf_migrated} = 1;
121 }
122
123 sub phase1_cleanup {
124 my ($self, $vmid, $err) = @_;
125
126 $self->log('info', "aborting phase 1 - cleanup resources");
127
128 if ($self->{volumes}) {
129 foreach my $volid (@{$self->{volumes}}) {
130 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
131 # fixme: try to remove ?
132 }
133 }
134 }
135
136 sub phase3 {
137 my ($self, $vmid) = @_;
138
139 my $volids = $self->{volumes};
140
141 # destroy local copies
142 foreach my $volid (@$volids) {
143 eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
144 if (my $err = $@) {
145 $self->log('err', "removing local copy of '$volid' failed - $err");
146 $self->{errors} = 1;
147 last if $err =~ /^interrupted by signal$/;
148 }
149 }
150 }
151
152 sub final_cleanup {
153 my ($self, $vmid) = @_;
154
155 $self->log('info', "start final cleanup");
156
157 if (!$self->{conf_migrated}) {
158 my $conf = $self->{vmconf};
159 delete $conf->{lock};
160
161 eval { PVE::LXC::write_config($vmid, $conf); };
162 if (my $err = $@) {
163 $self->log('err', $err);
164 }
165 } else {
166 my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ];
167 $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");
168 }
169 }
170
171 1;