]>
Commit | Line | Data |
---|---|---|
21ce53c3 TL |
1 | package PVE::HA::Resources::PVEVM; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
33783485 TL |
6 | use PVE::HA::Tools; |
7 | ||
a1c88626 WB |
8 | BEGIN { |
9 | if (!$ENV{PVE_GENERATING_DOCS}) { | |
10 | require PVE::QemuConfig; | |
11 | import PVE::QemuConfig; | |
12 | require PVE::QemuServer; | |
13 | import PVE::QemuServer; | |
5aac17c6 SR |
14 | require PVE::QemuServer::Monitor; |
15 | import PVE::QemuServer::Monitor; | |
a1c88626 WB |
16 | require PVE::API2::Qemu; |
17 | import PVE::API2::Qemu; | |
18 | } | |
19 | } | |
21ce53c3 TL |
20 | |
21 | use base qw(PVE::HA::Resources); | |
22 | ||
23 | sub type { | |
24 | return 'vm'; | |
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 | ||
94cedf0f | 46 | return PVE::QemuConfig->config_file($vmid, $nodename); |
21ce53c3 TL |
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})) { | |
fd0548f6 | 55 | die "resource 'vm:$vmid' does not exist in cluster\n" if !$noerr; |
21ce53c3 TL |
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::Qemu->vm_start($params); | |
33783485 | 73 | PVE::HA::Tools::upid_wait($upid, $haenv); |
21ce53c3 TL |
74 | } |
75 | ||
76 | sub shutdown { | |
e4ef317d | 77 | my ($class, $haenv, $id, $timeout) = @_; |
21ce53c3 TL |
78 | |
79 | my $nodename = $haenv->nodename(); | |
e4ef317d | 80 | my $shutdown_timeout = $timeout // 60; |
21ce53c3 | 81 | |
e4ef317d | 82 | my $upid; |
21ce53c3 TL |
83 | my $params = { |
84 | node => $nodename, | |
85 | vmid => $id, | |
21ce53c3 TL |
86 | }; |
87 | ||
e4ef317d FE |
88 | if ($shutdown_timeout) { |
89 | $params->{timeout} = $shutdown_timeout; | |
90 | $params->{forceStop} = 1; | |
91 | $upid = PVE::API2::Qemu->vm_shutdown($params); | |
92 | } else { | |
93 | $upid = PVE::API2::Qemu->vm_stop($params); | |
94 | } | |
95 | ||
33783485 | 96 | PVE::HA::Tools::upid_wait($upid, $haenv); |
21ce53c3 TL |
97 | } |
98 | ||
21ce53c3 TL |
99 | sub migrate { |
100 | my ($class, $haenv, $id, $target, $online) = @_; | |
101 | ||
102 | my $nodename = $haenv->nodename(); | |
103 | ||
104 | my $params = { | |
105 | node => $nodename, | |
106 | vmid => $id, | |
6e8b0c22 TL |
107 | # bug #2241 forces is for local resource only, people can ensure that |
108 | # different host have the same hardware, so this can be fine, and qemu | |
109 | # knows when not, so can only win here | |
110 | force => 1, | |
77e25aea | 111 | 'with-local-disks' => 1, |
21ce53c3 TL |
112 | target => $target, |
113 | online => $online, | |
114 | }; | |
115 | ||
116 | # explicitly shutdown if $online isn't true (relocate) | |
0d5906f3 | 117 | if (!$online && $class->check_running($haenv, $id)) { |
21ce53c3 TL |
118 | $class->shutdown($haenv, $id); |
119 | } | |
120 | ||
ea28f873 TL |
121 | my $oldconfig = $class->config_file($id, $nodename); |
122 | ||
21ce53c3 | 123 | my $upid = PVE::API2::Qemu->migrate_vm($params); |
33783485 | 124 | PVE::HA::Tools::upid_wait($upid, $haenv); |
ea28f873 TL |
125 | |
126 | # check if vm really moved | |
127 | return !(-f $oldconfig); | |
21ce53c3 TL |
128 | } |
129 | ||
130 | sub check_running { | |
0d5906f3 | 131 | my ($class, $haenv, $vmid) = @_; |
21ce53c3 | 132 | |
0d5906f3 TL |
133 | my $nodename = $haenv->nodename(); |
134 | ||
bc2a9d18 TL |
135 | if (PVE::QemuServer::check_running($vmid, 1, $nodename)) { |
136 | # do not count VMs which are suspended for a backup job as running | |
137 | my $conf = PVE::QemuConfig->load_config($vmid, $nodename); | |
138 | if (defined($conf->{lock}) && $conf->{lock} eq 'backup') { | |
a9a49e32 | 139 | my $qmpstatus = eval { |
5aac17c6 | 140 | PVE::QemuServer::Monitor::mon_cmd($vmid, 'query-status') |
a9a49e32 TL |
141 | }; |
142 | warn "$@\n" if $@; | |
143 | ||
144 | return 0 if defined($qmpstatus) && $qmpstatus->{status} eq 'prelaunch'; | |
bc2a9d18 TL |
145 | } |
146 | ||
147 | return 1; | |
148 | } else { | |
149 | return 0; | |
150 | } | |
21ce53c3 TL |
151 | } |
152 | ||
5dd3ed86 TL |
153 | sub remove_locks { |
154 | my ($self, $haenv, $id, $locks, $service_node) = @_; | |
155 | ||
156 | $service_node = $service_node || $haenv->nodename(); | |
157 | ||
158 | my $conf = PVE::QemuConfig->load_config($id, $service_node); | |
159 | ||
160 | return undef if !defined($conf->{lock}); | |
161 | ||
162 | foreach my $lock (@$locks) { | |
163 | if ($conf->{lock} eq $lock) { | |
164 | delete $conf->{lock}; | |
165 | ||
166 | my $cfspath = PVE::QemuConfig->cfs_config_path($id, $service_node); | |
167 | PVE::Cluster::cfs_write_file($cfspath, $conf); | |
168 | ||
169 | return $lock; | |
170 | } | |
171 | } | |
172 | ||
173 | return undef; | |
174 | } | |
175 | ||
eea0c609 FE |
176 | sub get_static_stats { |
177 | my ($class, $haenv, $id, $service_node) = @_; | |
178 | ||
179 | my $conf = PVE::QemuConfig->load_config($id, $service_node); | |
180 | my $defaults = PVE::QemuServer::load_defaults(); | |
181 | ||
182 | my $cpus = ($conf->{sockets} || $defaults->{sockets}) * ($conf->{cores} || $defaults->{cores}); | |
183 | ||
184 | return { | |
185 | maxcpu => $conf->{vcpus} || $cpus, | |
186 | maxmem => ($conf->{memory} || $defaults->{memory}) * 1024 * 1024, | |
187 | }; | |
188 | } | |
189 | ||
21ce53c3 | 190 | 1; |