]> git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/HA/Resources/PVEVM.pm
fix # 2241: VM resource: allow migration with local device, when not running
[pve-ha-manager.git] / src / PVE / HA / Resources / PVEVM.pm
1 package PVE::HA::Resources::PVEVM;
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::QemuConfig;
11 import PVE::QemuConfig;
12 require PVE::QemuServer;
13 import PVE::QemuServer;
14 require PVE::API2::Qemu;
15 import PVE::API2::Qemu;
16 }
17 }
18
19 use base qw(PVE::HA::Resources);
20
21 sub type {
22 return 'vm';
23 }
24
25 sub verify_name {
26 my ($class, $name) = @_;
27
28 die "invalid VMID\n" if $name !~ m/^[1-9][0-9]+$/;
29 }
30
31 sub options {
32 return {
33 state => { optional => 1 },
34 group => { optional => 1 },
35 comment => { optional => 1 },
36 max_restart => { optional => 1 },
37 max_relocate => { optional => 1 },
38 };
39 }
40
41 sub config_file {
42 my ($class, $vmid, $nodename) = @_;
43
44 return PVE::QemuConfig->config_file($vmid, $nodename);
45 }
46
47 sub exists {
48 my ($class, $vmid, $noerr) = @_;
49
50 my $vmlist = PVE::Cluster::get_vmlist();
51
52 if(!defined($vmlist->{ids}->{$vmid})) {
53 die "resource 'vm:$vmid' does not exists in cluster\n" if !$noerr;
54 return undef;
55 } else {
56 return 1;
57 }
58 }
59
60 sub start {
61 my ($class, $haenv, $id) = @_;
62
63 my $nodename = $haenv->nodename();
64
65 my $params = {
66 node => $nodename,
67 vmid => $id
68 };
69
70 my $upid = PVE::API2::Qemu->vm_start($params);
71 PVE::HA::Tools::upid_wait($upid, $haenv);
72 }
73
74 sub shutdown {
75 my ($class, $haenv, $id) = @_;
76
77 my $nodename = $haenv->nodename();
78 my $shutdown_timeout = 60; # fixme: make this configurable
79
80 my $params = {
81 node => $nodename,
82 vmid => $id,
83 timeout => $shutdown_timeout,
84 forceStop => 1,
85 };
86
87 my $upid = PVE::API2::Qemu->vm_shutdown($params);
88 PVE::HA::Tools::upid_wait($upid, $haenv);
89 }
90
91 sub migrate {
92 my ($class, $haenv, $id, $target, $online) = @_;
93
94 my $nodename = $haenv->nodename();
95
96 my $params = {
97 node => $nodename,
98 vmid => $id,
99 # bug #2241 forces is for local resource only, people can ensure that
100 # different host have the same hardware, so this can be fine, and qemu
101 # knows when not, so can only win here
102 force => 1,
103 target => $target,
104 online => $online,
105 };
106
107 # explicitly shutdown if $online isn't true (relocate)
108 if (!$online && $class->check_running($haenv, $id)) {
109 $class->shutdown($haenv, $id);
110 }
111
112 my $oldconfig = $class->config_file($id, $nodename);
113
114 my $upid = PVE::API2::Qemu->migrate_vm($params);
115 PVE::HA::Tools::upid_wait($upid, $haenv);
116
117 # check if vm really moved
118 return !(-f $oldconfig);
119 }
120
121 sub check_running {
122 my ($class, $haenv, $vmid) = @_;
123
124 my $nodename = $haenv->nodename();
125
126 if (PVE::QemuServer::check_running($vmid, 1, $nodename)) {
127 # do not count VMs which are suspended for a backup job as running
128 my $conf = PVE::QemuConfig->load_config($vmid, $nodename);
129 if (defined($conf->{lock}) && $conf->{lock} eq 'backup') {
130 my $qmpstatus = eval {
131 PVE::QemuServer::vm_qmp_command($vmid, { execute => 'query-status' })
132 };
133 warn "$@\n" if $@;
134
135 return 0 if defined($qmpstatus) && $qmpstatus->{status} eq 'prelaunch';
136 }
137
138 return 1;
139 } else {
140 return 0;
141 }
142 }
143
144 sub remove_locks {
145 my ($self, $haenv, $id, $locks, $service_node) = @_;
146
147 $service_node = $service_node || $haenv->nodename();
148
149 my $conf = PVE::QemuConfig->load_config($id, $service_node);
150
151 return undef if !defined($conf->{lock});
152
153 foreach my $lock (@$locks) {
154 if ($conf->{lock} eq $lock) {
155 delete $conf->{lock};
156
157 my $cfspath = PVE::QemuConfig->cfs_config_path($id, $service_node);
158 PVE::Cluster::cfs_write_file($cfspath, $conf);
159
160 return $lock;
161 }
162 }
163
164 return undef;
165 }
166
167 1;