]> git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/HA/Resources/PVECT.pm
buildsys: don't pull qemu/lxc during doc-generation
[pve-ha-manager.git] / src / PVE / HA / Resources / PVECT.pm
1 package PVE::HA::Resources::PVECT;
2
3 use strict;
4 use warnings;
5
6 use PVE::HA::Tools;
7
8 BEGIN {
9 if (!$ENV{PVE_GENERATING_DOCS}) {
10 require PVE::LXC;
11 import PVE::LXC;
12 require PVE::LXC::Config;
13 import PVE::LXC::Config;
14 require PVE::API2::LXC;
15 import PVE::API2::LXC;
16 require PVE::API2::LXC::Status;
17 import PVE::API2::LXC::Status;
18 }
19 }
20
21 use base qw(PVE::HA::Resources);
22
23 sub type {
24 return 'ct';
25 }
26
27 sub verify_name {
28 my ($class, $name) = @_;
29
30 die "invalid VMID\n" if $name !~ m/^[1-9][0-9]+$/;
31 }
32
33 sub options {
34 return {
35 state => { optional => 1 },
36 group => { optional => 1 },
37 comment => { optional => 1 },
38 max_restart => { optional => 1 },
39 max_relocate => { optional => 1 },
40 };
41 }
42
43 sub config_file {
44 my ($class, $vmid, $nodename) = @_;
45
46 return PVE::LXC::Config->config_file($vmid, $nodename);
47 }
48
49 sub exists {
50 my ($class, $vmid, $noerr) = @_;
51
52 my $vmlist = PVE::Cluster::get_vmlist();
53
54 if(!defined($vmlist->{ids}->{$vmid})) {
55 die "resource 'ct:$vmid' does not exists in cluster\n" if !$noerr;
56 return undef;
57 } else {
58 return 1;
59 }
60 }
61
62 sub start {
63 my ($class, $haenv, $id) = @_;
64
65 my $nodename = $haenv->nodename();
66
67 my $params = {
68 node => $nodename,
69 vmid => $id
70 };
71
72 my $upid = PVE::API2::LXC::Status->vm_start($params);
73 PVE::HA::Tools::upid_wait($upid, $haenv);
74 }
75
76 sub shutdown {
77 my ($class, $haenv, $id) = @_;
78
79 my $nodename = $haenv->nodename();
80 my $shutdown_timeout = 60; # fixme: make this configurable
81
82 my $params = {
83 node => $nodename,
84 vmid => $id,
85 timeout => $shutdown_timeout,
86 forceStop => 1,
87 };
88
89 my $upid = PVE::API2::LXC::Status->vm_shutdown($params);
90 PVE::HA::Tools::upid_wait($upid, $haenv);
91 }
92
93 sub migrate {
94 my ($class, $haenv, $id, $target, $online) = @_;
95
96 my $nodename = $haenv->nodename();
97
98 my $params = {
99 node => $nodename,
100 vmid => $id,
101 target => $target,
102 online => 0, # we cannot migrate CT (yet) online, only relocate
103 };
104
105 # always relocate container for now
106 if ($class->check_running($haenv, $id)) {
107 $class->shutdown($haenv, $id);
108 }
109
110 my $oldconfig = $class->config_file($id, $nodename);
111
112 my $upid = PVE::API2::LXC->migrate_vm($params);
113 PVE::HA::Tools::upid_wait($upid, $haenv);
114
115 # check if vm really moved
116 return !(-f $oldconfig);
117 }
118
119 sub check_running {
120 my ($class, $haenv, $vmid) = @_;
121
122 return PVE::LXC::check_running($vmid);
123 }
124
125 sub remove_locks {
126 my ($self, $haenv, $id, $locks, $service_node) = @_;
127
128 $service_node = $service_node || $haenv->nodename();
129
130 my $conf = PVE::LXC::Config->load_config($id, $service_node);
131
132 return undef if !defined($conf->{lock});
133
134 foreach my $lock (@$locks) {
135 if ($conf->{lock} eq $lock) {
136 delete $conf->{lock};
137
138 my $cfspath = PVE::LXC::Config->cfs_config_path($id, $service_node);
139 PVE::Cluster::cfs_write_file($cfspath, $conf);
140
141 return $lock;
142 }
143 }
144
145 return undef;
146 }
147
148 1;