]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Migrate.pm
1 package PVE
::LXC
::Migrate
;
5 use PVE
::AbstractMigrate
;
7 use File
::Copy
; # fixme: remove
14 use base
qw(PVE::AbstractMigrate);
17 my ($self, $vmid, $code, @param) = @_;
19 return PVE
::LXC
::lock_container
($vmid, undef, $code, @param);
23 my ($self, $vmid) = @_;
25 my $online = $self->{opts
}->{online
};
27 $self->{storecfg
} = PVE
::Storage
::config
();
30 my $conf = $self->{vmconf
} = PVE
::LXC
::load_config
($vmid);
32 PVE
::LXC
::check_lock
($conf);
35 if (PVE
::LXC
::check_running
($vmid)) {
36 die "lxc live migration is currently not implemented\n";
38 die "cant migrate running container without --online\n" if !$online;
42 PVE
::LXC
::foreach_mountpoint
($conf, sub {
43 my ($ms, $mountpoint) = @_;
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;
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
});
53 die "unable to migrate local mountpoint '$volid' while CT is running"
54 if !$scfg->{shared
} && $running;
58 my $volid_list = PVE
::LXC
::get_vm_volumes
($conf);
59 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $volid_list);
61 # todo: test if VM uses local resources
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 $@;
72 my ($self, $vmid) = @_;
74 $self->log('info', "starting migration of CT $self->{vmid} to node '$self->{node}' ($self->{nodeip})");
76 my $conf = $self->{vmconf
};
77 $conf->{lock} = 'migrate';
78 PVE
::LXC
::write_config
($vmid, $conf);
80 if ($self->{running
}) {
81 $self->log('info', "container is running - using online migration");
84 $self->{volumes
} = [];
86 PVE
::LXC
::foreach_mountpoint
($conf, sub {
87 my ($ms, $mountpoint) = @_;
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);
93 if (!$scfg->{shared
}) {
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;
99 $self->log('info', "mointpoint '$ms' is on shared storage '$storage'");
103 my $conffile = PVE
::LXC
::config_file
($vmid);
104 my $newconffile = PVE
::LXC
::config_file
($vmid, $self->{node
});
106 if ($self->{running
}) {
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)
114 PVE
::LXC
::umount_all
($vmid, $self->{storecfg
}, $conf);
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);
121 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
122 if !rename($conffile, $newconffile);
124 $self->{conf_migrated
} = 1;
128 my ($self, $vmid, $err) = @_;
130 $self->log('info', "aborting phase 1 - cleanup resources");
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 ?
141 my ($self, $vmid) = @_;
143 my $volids = $self->{volumes
};
145 # destroy local copies
146 foreach my $volid (@$volids) {
147 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
149 $self->log('err', "removing local copy of '$volid' failed - $err");
151 last if $err =~ /^interrupted by signal$/;
157 my ($self, $vmid) = @_;
159 $self->log('info', "start final cleanup");
161 if (!$self->{conf_migrated
}) {
162 my $conf = $self->{vmconf
};
163 delete $conf->{lock};
165 eval { PVE
::LXC
::write_config
($vmid, $conf); };
167 $self->log('err', $err);
170 my $cmd = [ @{$self->{rem_ssh
}}, 'pct', 'unlock', $vmid ];
171 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");