]>
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 | ||
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}; | |
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::OpenVZ::config_file($vmid); | |
104 | my $newconffile = PVE::OpenVZ::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 | ||
121 | sub phase1_cleanup { | |
122 | my ($self, $vmid, $err) = @_; | |
123 | ||
124 | $self->log('info', "aborting phase 1 - cleanup resources"); | |
125 | ||
126 | if ($self->{volumes}) { | |
127 | foreach my $volid (@{$self->{volumes}}) { | |
128 | $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'"); | |
129 | # fixme: try to remove ? | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | sub phase3 { | |
135 | my ($self, $vmid) = @_; | |
136 | ||
137 | my $volids = $self->{volumes}; | |
138 | ||
139 | # destroy local copies | |
140 | foreach my $volid (@$volids) { | |
141 | eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); }; | |
142 | if (my $err = $@) { | |
143 | $self->log('err', "removing local copy of '$volid' failed - $err"); | |
144 | $self->{errors} = 1; | |
145 | last if $err =~ /^interrupted by signal$/; | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | sub final_cleanup { | |
151 | my ($self, $vmid) = @_; | |
152 | ||
153 | $self->log('info', "start final cleanup"); | |
154 | ||
155 | my $conf = $self->{vmconf}; | |
156 | delete $conf->{lock}; | |
157 | ||
158 | eval { PVE::LXC::write_config($vmid, $conf); }; | |
159 | if (my $err = $@) { | |
160 | $self->log('err', $err); | |
161 | } | |
162 | } | |
163 | ||
164 | 1; |