]>
Commit | Line | Data |
---|---|---|
1e3baf05 DM |
1 | package PVE::API2::Qemu; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5b9d692a | 5 | use Cwd 'abs_path'; |
1e3baf05 DM |
6 | |
7 | use PVE::Cluster; | |
8 | use PVE::SafeSyslog; | |
9 | use PVE::Tools qw(extract_param); | |
10 | use PVE::Exception qw(raise raise_param_exc); | |
11 | use PVE::Storage; | |
12 | use PVE::JSONSchema qw(get_standard_option); | |
13 | use PVE::RESTHandler; | |
14 | use PVE::QemuServer; | |
3ea94c60 | 15 | use PVE::QemuMigrate; |
1e3baf05 DM |
16 | use PVE::RPCEnvironment; |
17 | use PVE::AccessControl; | |
18 | use PVE::INotify; | |
19 | ||
20 | use Data::Dumper; # fixme: remove | |
21 | ||
22 | use base qw(PVE::RESTHandler); | |
23 | ||
24 | my $opt_force_description = "Force physical removal. Without this, we simple remove the disk from the config file and create an additional configuration entry called 'unused[n]', which contains the volume ID. Unlink of unused[n] always cause physical removal."; | |
25 | ||
26 | my $resolve_cdrom_alias = sub { | |
27 | my $param = shift; | |
28 | ||
29 | if (my $value = $param->{cdrom}) { | |
30 | $value .= ",media=cdrom" if $value !~ m/media=/; | |
31 | $param->{ide2} = $value; | |
32 | delete $param->{cdrom}; | |
33 | } | |
34 | }; | |
35 | ||
a0d1b1a2 DM |
36 | my $check_volume_access = sub { |
37 | my ($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool) = @_; | |
38 | ||
39 | my $path; | |
40 | if (my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1)) { | |
41 | my ($ownervm, $vtype); | |
42 | ($path, $ownervm, $vtype) = PVE::Storage::path($storecfg, $volid); | |
43 | if ($vtype eq 'iso' || $vtype eq 'vztmpl') { | |
44 | # we simply allow access | |
45 | } elsif (!$ownervm || ($ownervm != $vmid)) { | |
46 | # allow if we are Datastore administrator | |
47 | $rpcenv->check_storage_perm($authuser, $vmid, $pool, $sid, [ 'Datastore.Allocate' ]); | |
48 | } | |
49 | } else { | |
50 | die "Only root can pass arbitrary filesystem paths." | |
51 | if $authuser ne 'root@pam'; | |
52 | ||
53 | $path = abs_path($volid); | |
54 | } | |
55 | return $path; | |
56 | }; | |
57 | ||
ae57f6b3 DM |
58 | my $check_storage_access = sub { |
59 | my ($rpcenv, $authuser, $storecfg, $vmid, $pool, $settings, $default_storage) = @_; | |
a0d1b1a2 | 60 | |
ae57f6b3 DM |
61 | PVE::QemuServer::foreach_drive($settings, sub { |
62 | my ($ds, $drive) = @_; | |
a0d1b1a2 | 63 | |
ae57f6b3 | 64 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); |
a0d1b1a2 | 65 | |
ae57f6b3 | 66 | my $volid = $drive->{file}; |
a0d1b1a2 | 67 | |
ae57f6b3 | 68 | if (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) { |
a0d1b1a2 DM |
69 | my ($storeid, $size) = ($2 || $default_storage, $3); |
70 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
71 | $rpcenv->check_storage_perm($authuser, $vmid, $pool, $storeid, [ 'Datastore.AllocateSpace' ]); | |
a0d1b1a2 DM |
72 | } else { |
73 | my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool); | |
74 | die "image '$path' does not exists\n" if (!(-f $path || -b $path)); | |
75 | } | |
76 | }); | |
ae57f6b3 | 77 | }; |
a0d1b1a2 | 78 | |
ae57f6b3 DM |
79 | # Note: $pool is only needed when creating a VM, because pool permissions |
80 | # are automatically inherited if VM already exists inside a pool. | |
81 | my $create_disks = sub { | |
82 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_; | |
a0d1b1a2 DM |
83 | |
84 | my $vollist = []; | |
a0d1b1a2 | 85 | |
ae57f6b3 DM |
86 | my $res = {}; |
87 | PVE::QemuServer::foreach_drive($settings, sub { | |
88 | my ($ds, $disk) = @_; | |
89 | ||
90 | my $volid = $disk->{file}; | |
91 | ||
92 | if ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) { | |
93 | my ($storeid, $size) = ($2 || $default_storage, $3); | |
94 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
95 | my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid); | |
96 | my $fmt = $disk->{format} || $defformat; | |
a0d1b1a2 DM |
97 | my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, |
98 | $fmt, undef, $size*1024*1024); | |
a0d1b1a2 DM |
99 | $disk->{file} = $volid; |
100 | push @$vollist, $volid; | |
ae57f6b3 DM |
101 | delete $disk->{format}; # no longer needed |
102 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
103 | } else { | |
104 | my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool); | |
105 | die "image '$path' does not exists\n" if (!(-f $path || -b $path)); | |
106 | $res->{$ds} = $settings->{ds}; | |
a0d1b1a2 | 107 | } |
ae57f6b3 | 108 | }); |
a0d1b1a2 DM |
109 | |
110 | # free allocated images on error | |
111 | if (my $err = $@) { | |
112 | syslog('err', "VM $vmid creating disks failed"); | |
113 | foreach my $volid (@$vollist) { | |
114 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
115 | warn $@ if $@; | |
116 | } | |
117 | die $err; | |
118 | } | |
119 | ||
120 | # modify vm config if everything went well | |
ae57f6b3 DM |
121 | foreach my $ds (keys %$res) { |
122 | $conf->{$ds} = $res->{$ds}; | |
a0d1b1a2 DM |
123 | } |
124 | ||
125 | return $vollist; | |
126 | }; | |
127 | ||
128 | my $check_vm_modify_config_perm = sub { | |
ae57f6b3 | 129 | my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_; |
a0d1b1a2 DM |
130 | |
131 | return 1 if $authuser ne 'root@pam'; | |
132 | ||
ae57f6b3 | 133 | foreach my $opt (@$key_list) { |
a0d1b1a2 DM |
134 | # disk checks need to be done somewhere else |
135 | next if PVE::QemuServer::valid_drivename($opt); | |
136 | ||
137 | if ($opt eq 'sockets' || $opt eq 'cores' || | |
138 | $opt eq 'cpu' || $opt eq 'smp' || | |
139 | $opt eq 'cpuimit' || $opt eq 'cpuunits') { | |
140 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']); | |
141 | } elsif ($opt eq 'boot' || $opt eq 'bootdisk') { | |
142 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']); | |
143 | } elsif ($opt eq 'memory' || $opt eq 'balloon') { | |
144 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']); | |
145 | } elsif ($opt eq 'args' || $opt eq 'lock') { | |
146 | die "only root can set '$opt' config\n"; | |
147 | } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' || | |
148 | $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') { | |
149 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); | |
150 | } elsif ($opt =~ m/^net\d+$/) { | |
151 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']); | |
152 | } else { | |
153 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']); | |
154 | } | |
155 | } | |
156 | ||
157 | return 1; | |
158 | }; | |
159 | ||
1e3baf05 | 160 | __PACKAGE__->register_method({ |
afdb31d5 DM |
161 | name => 'vmlist', |
162 | path => '', | |
1e3baf05 DM |
163 | method => 'GET', |
164 | description => "Virtual machine index (per node).", | |
a0d1b1a2 DM |
165 | permissions => { |
166 | description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.", | |
167 | user => 'all', | |
168 | }, | |
1e3baf05 DM |
169 | proxyto => 'node', |
170 | protected => 1, # qemu pid files are only readable by root | |
171 | parameters => { | |
172 | additionalProperties => 0, | |
173 | properties => { | |
174 | node => get_standard_option('pve-node'), | |
175 | }, | |
176 | }, | |
177 | returns => { | |
178 | type => 'array', | |
179 | items => { | |
180 | type => "object", | |
181 | properties => {}, | |
182 | }, | |
183 | links => [ { rel => 'child', href => "{vmid}" } ], | |
184 | }, | |
185 | code => sub { | |
186 | my ($param) = @_; | |
187 | ||
a0d1b1a2 DM |
188 | my $rpcenv = PVE::RPCEnvironment::get(); |
189 | my $authuser = $rpcenv->get_user(); | |
190 | ||
1e3baf05 DM |
191 | my $vmstatus = PVE::QemuServer::vmstatus(); |
192 | ||
a0d1b1a2 DM |
193 | my $res = []; |
194 | foreach my $vmid (keys %$vmstatus) { | |
195 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1); | |
196 | ||
197 | my $data = $vmstatus->{$vmid}; | |
198 | $data->{vmid} = $vmid; | |
199 | push @$res, $data; | |
200 | } | |
1e3baf05 | 201 | |
a0d1b1a2 | 202 | return $res; |
1e3baf05 DM |
203 | }}); |
204 | ||
205 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
206 | name => 'create_vm', |
207 | path => '', | |
1e3baf05 | 208 | method => 'POST', |
3e16d5fc | 209 | description => "Create or restore a virtual machine.", |
a0d1b1a2 DM |
210 | permissions => { |
211 | description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. If you create disks you need 'Datastore.AllocateSpace' on any used storage.", | |
212 | check => [ 'or', | |
213 | [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
214 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
215 | ], | |
216 | }, | |
1e3baf05 DM |
217 | protected => 1, |
218 | proxyto => 'node', | |
219 | parameters => { | |
220 | additionalProperties => 0, | |
221 | properties => PVE::QemuServer::json_config_properties( | |
222 | { | |
223 | node => get_standard_option('pve-node'), | |
224 | vmid => get_standard_option('pve-vmid'), | |
3e16d5fc DM |
225 | archive => { |
226 | description => "The backup file.", | |
227 | type => 'string', | |
228 | optional => 1, | |
229 | maxLength => 255, | |
230 | }, | |
231 | storage => get_standard_option('pve-storage-id', { | |
232 | description => "Default storage.", | |
233 | optional => 1, | |
234 | }), | |
235 | force => { | |
afdb31d5 | 236 | optional => 1, |
3e16d5fc DM |
237 | type => 'boolean', |
238 | description => "Allow to overwrite existing VM.", | |
51586c3a DM |
239 | requires => 'archive', |
240 | }, | |
241 | unique => { | |
afdb31d5 | 242 | optional => 1, |
51586c3a DM |
243 | type => 'boolean', |
244 | description => "Assign a unique random ethernet address.", | |
245 | requires => 'archive', | |
3e16d5fc | 246 | }, |
a0d1b1a2 DM |
247 | pool => { |
248 | optional => 1, | |
249 | type => 'string', format => 'pve-poolid', | |
250 | description => "Add the VM to the specified pool.", | |
251 | }, | |
1e3baf05 DM |
252 | }), |
253 | }, | |
afdb31d5 | 254 | returns => { |
5fdbe4f0 DM |
255 | type => 'string', |
256 | }, | |
1e3baf05 DM |
257 | code => sub { |
258 | my ($param) = @_; | |
259 | ||
5fdbe4f0 DM |
260 | my $rpcenv = PVE::RPCEnvironment::get(); |
261 | ||
a0d1b1a2 | 262 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 | 263 | |
1e3baf05 DM |
264 | my $node = extract_param($param, 'node'); |
265 | ||
1e3baf05 DM |
266 | my $vmid = extract_param($param, 'vmid'); |
267 | ||
3e16d5fc DM |
268 | my $archive = extract_param($param, 'archive'); |
269 | ||
270 | my $storage = extract_param($param, 'storage'); | |
271 | ||
51586c3a DM |
272 | my $force = extract_param($param, 'force'); |
273 | ||
274 | my $unique = extract_param($param, 'unique'); | |
a0d1b1a2 DM |
275 | |
276 | my $pool = extract_param($param, 'pool'); | |
51586c3a | 277 | |
1e3baf05 | 278 | my $filename = PVE::QemuServer::config_file($vmid); |
afdb31d5 DM |
279 | |
280 | my $storecfg = PVE::Storage::config(); | |
1e3baf05 | 281 | |
3e16d5fc | 282 | PVE::Cluster::check_cfs_quorum(); |
1e3baf05 | 283 | |
a0d1b1a2 DM |
284 | if (defined($pool)) { |
285 | $rpcenv->check_pool_exist($pool); | |
286 | $rpcenv->check_perm_modify($authuser, "/pool/$pool"); | |
287 | } | |
288 | ||
289 | $rpcenv->check_storage_perm($authuser, $vmid, $pool, $storage, [ 'Datastore.AllocateSpace' ]) | |
290 | if defined($storage); | |
291 | ||
afdb31d5 | 292 | if (!$archive) { |
3e16d5fc | 293 | &$resolve_cdrom_alias($param); |
1e3baf05 | 294 | |
ae57f6b3 DM |
295 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $pool, $param, $storage); |
296 | ||
297 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]); | |
298 | ||
3e16d5fc DM |
299 | foreach my $opt (keys %$param) { |
300 | if (PVE::QemuServer::valid_drivename($opt)) { | |
301 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
302 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; | |
afdb31d5 | 303 | |
3e16d5fc DM |
304 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); |
305 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
306 | } | |
1e3baf05 | 307 | } |
3e16d5fc DM |
308 | |
309 | PVE::QemuServer::add_random_macs($param); | |
51586c3a DM |
310 | } else { |
311 | my $keystr = join(' ', keys %$param); | |
bc4dcb99 DM |
312 | raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr; |
313 | ||
5b9d692a | 314 | if ($archive eq '-') { |
afdb31d5 | 315 | die "pipe requires cli environment\n" |
5b9d692a DM |
316 | && $rpcenv->{type} ne 'cli'; |
317 | } else { | |
a0d1b1a2 | 318 | my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $archive, $pool); |
971f27c4 DM |
319 | die "can't find archive file '$archive'\n" if !($path && -f $path); |
320 | $archive = $path; | |
321 | } | |
1e3baf05 DM |
322 | } |
323 | ||
3e16d5fc DM |
324 | my $restorefn = sub { |
325 | ||
326 | if (-f $filename) { | |
afdb31d5 | 327 | die "unable to restore vm $vmid: config file already exists\n" |
51586c3a | 328 | if !$force; |
3e16d5fc | 329 | |
afdb31d5 | 330 | die "unable to restore vm $vmid: vm is running\n" |
3e16d5fc | 331 | if PVE::QemuServer::check_running($vmid); |
a6af7b3e DM |
332 | |
333 | # destroy existing data - keep empty config | |
334 | PVE::QemuServer::destroy_vm($storecfg, $vmid, 1); | |
3e16d5fc DM |
335 | } |
336 | ||
337 | my $realcmd = sub { | |
a0d1b1a2 | 338 | PVE::QemuServer::restore_archive($archive, $vmid, $authuser, { |
51586c3a | 339 | storage => $storage, |
a0d1b1a2 | 340 | pool => $pool, |
51586c3a | 341 | unique => $unique }); |
3e16d5fc DM |
342 | }; |
343 | ||
a0d1b1a2 | 344 | return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd); |
3e16d5fc | 345 | }; |
1e3baf05 | 346 | |
1e3baf05 DM |
347 | my $createfn = sub { |
348 | ||
349 | # second test (after locking test is accurate) | |
afdb31d5 | 350 | die "unable to create vm $vmid: config file already exists\n" |
1e3baf05 DM |
351 | if -f $filename; |
352 | ||
5fdbe4f0 | 353 | my $realcmd = sub { |
1e3baf05 | 354 | |
5fdbe4f0 | 355 | my $vollist = []; |
1e3baf05 | 356 | |
1858638f DM |
357 | my $conf = $param; |
358 | ||
5fdbe4f0 | 359 | eval { |
ae57f6b3 | 360 | |
1858638f | 361 | $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage); |
1e3baf05 | 362 | |
5fdbe4f0 DM |
363 | # try to be smart about bootdisk |
364 | my @disks = PVE::QemuServer::disknames(); | |
365 | my $firstdisk; | |
366 | foreach my $ds (reverse @disks) { | |
1858638f DM |
367 | next if !$conf->{$ds}; |
368 | my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds}); | |
5fdbe4f0 DM |
369 | next if PVE::QemuServer::drive_is_cdrom($disk); |
370 | $firstdisk = $ds; | |
371 | } | |
1e3baf05 | 372 | |
1858638f DM |
373 | if (!$conf->{bootdisk} && $firstdisk) { |
374 | $conf->{bootdisk} = $firstdisk; | |
5fdbe4f0 | 375 | } |
1e3baf05 | 376 | |
1858638f | 377 | PVE::QemuServer::update_conf_nolock($vmid, $conf); |
5fdbe4f0 DM |
378 | }; |
379 | my $err = $@; | |
1e3baf05 | 380 | |
5fdbe4f0 DM |
381 | if ($err) { |
382 | foreach my $volid (@$vollist) { | |
383 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
384 | warn $@ if $@; | |
385 | } | |
386 | die "create failed - $err"; | |
387 | } | |
388 | }; | |
389 | ||
a0d1b1a2 | 390 | return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
391 | }; |
392 | ||
3e16d5fc | 393 | return PVE::QemuServer::lock_config($vmid, $archive ? $restorefn : $createfn); |
1e3baf05 DM |
394 | }}); |
395 | ||
396 | __PACKAGE__->register_method({ | |
397 | name => 'vmdiridx', | |
afdb31d5 | 398 | path => '{vmid}', |
1e3baf05 DM |
399 | method => 'GET', |
400 | proxyto => 'node', | |
401 | description => "Directory index", | |
a0d1b1a2 DM |
402 | permissions => { |
403 | user => 'all', | |
404 | }, | |
1e3baf05 DM |
405 | parameters => { |
406 | additionalProperties => 0, | |
407 | properties => { | |
408 | node => get_standard_option('pve-node'), | |
409 | vmid => get_standard_option('pve-vmid'), | |
410 | }, | |
411 | }, | |
412 | returns => { | |
413 | type => 'array', | |
414 | items => { | |
415 | type => "object", | |
416 | properties => { | |
417 | subdir => { type => 'string' }, | |
418 | }, | |
419 | }, | |
420 | links => [ { rel => 'child', href => "{subdir}" } ], | |
421 | }, | |
422 | code => sub { | |
423 | my ($param) = @_; | |
424 | ||
425 | my $res = [ | |
426 | { subdir => 'config' }, | |
427 | { subdir => 'status' }, | |
428 | { subdir => 'unlink' }, | |
429 | { subdir => 'vncproxy' }, | |
3ea94c60 | 430 | { subdir => 'migrate' }, |
1e3baf05 DM |
431 | { subdir => 'rrd' }, |
432 | { subdir => 'rrddata' }, | |
91c94f0a | 433 | { subdir => 'monitor' }, |
1e3baf05 | 434 | ]; |
afdb31d5 | 435 | |
1e3baf05 DM |
436 | return $res; |
437 | }}); | |
438 | ||
439 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
440 | name => 'rrd', |
441 | path => '{vmid}/rrd', | |
1e3baf05 DM |
442 | method => 'GET', |
443 | protected => 1, # fixme: can we avoid that? | |
444 | permissions => { | |
378b359e | 445 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], |
1e3baf05 DM |
446 | }, |
447 | description => "Read VM RRD statistics (returns PNG)", | |
448 | parameters => { | |
449 | additionalProperties => 0, | |
450 | properties => { | |
451 | node => get_standard_option('pve-node'), | |
452 | vmid => get_standard_option('pve-vmid'), | |
453 | timeframe => { | |
454 | description => "Specify the time frame you are interested in.", | |
455 | type => 'string', | |
456 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
457 | }, | |
458 | ds => { | |
459 | description => "The list of datasources you want to display.", | |
460 | type => 'string', format => 'pve-configid-list', | |
461 | }, | |
462 | cf => { | |
463 | description => "The RRD consolidation function", | |
464 | type => 'string', | |
465 | enum => [ 'AVERAGE', 'MAX' ], | |
466 | optional => 1, | |
467 | }, | |
468 | }, | |
469 | }, | |
470 | returns => { | |
471 | type => "object", | |
472 | properties => { | |
473 | filename => { type => 'string' }, | |
474 | }, | |
475 | }, | |
476 | code => sub { | |
477 | my ($param) = @_; | |
478 | ||
479 | return PVE::Cluster::create_rrd_graph( | |
afdb31d5 | 480 | "pve2-vm/$param->{vmid}", $param->{timeframe}, |
1e3baf05 | 481 | $param->{ds}, $param->{cf}); |
afdb31d5 | 482 | |
1e3baf05 DM |
483 | }}); |
484 | ||
485 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
486 | name => 'rrddata', |
487 | path => '{vmid}/rrddata', | |
1e3baf05 DM |
488 | method => 'GET', |
489 | protected => 1, # fixme: can we avoid that? | |
490 | permissions => { | |
378b359e | 491 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], |
1e3baf05 DM |
492 | }, |
493 | description => "Read VM RRD statistics", | |
494 | parameters => { | |
495 | additionalProperties => 0, | |
496 | properties => { | |
497 | node => get_standard_option('pve-node'), | |
498 | vmid => get_standard_option('pve-vmid'), | |
499 | timeframe => { | |
500 | description => "Specify the time frame you are interested in.", | |
501 | type => 'string', | |
502 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
503 | }, | |
504 | cf => { | |
505 | description => "The RRD consolidation function", | |
506 | type => 'string', | |
507 | enum => [ 'AVERAGE', 'MAX' ], | |
508 | optional => 1, | |
509 | }, | |
510 | }, | |
511 | }, | |
512 | returns => { | |
513 | type => "array", | |
514 | items => { | |
515 | type => "object", | |
516 | properties => {}, | |
517 | }, | |
518 | }, | |
519 | code => sub { | |
520 | my ($param) = @_; | |
521 | ||
522 | return PVE::Cluster::create_rrd_data( | |
523 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); | |
524 | }}); | |
525 | ||
526 | ||
527 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
528 | name => 'vm_config', |
529 | path => '{vmid}/config', | |
1e3baf05 DM |
530 | method => 'GET', |
531 | proxyto => 'node', | |
532 | description => "Get virtual machine configuration.", | |
a0d1b1a2 DM |
533 | permissions => { |
534 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
535 | }, | |
1e3baf05 DM |
536 | parameters => { |
537 | additionalProperties => 0, | |
538 | properties => { | |
539 | node => get_standard_option('pve-node'), | |
540 | vmid => get_standard_option('pve-vmid'), | |
541 | }, | |
542 | }, | |
afdb31d5 | 543 | returns => { |
1e3baf05 | 544 | type => "object", |
554ac7e7 DM |
545 | properties => { |
546 | digest => { | |
547 | type => 'string', | |
548 | description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', | |
549 | } | |
550 | }, | |
1e3baf05 DM |
551 | }, |
552 | code => sub { | |
553 | my ($param) = @_; | |
554 | ||
555 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
556 | ||
557 | return $conf; | |
558 | }}); | |
559 | ||
ae57f6b3 DM |
560 | my $vm_is_volid_owner = sub { |
561 | my ($storecfg, $vmid, $volid) =@_; | |
562 | ||
563 | if ($volid !~ m|^/|) { | |
564 | my ($path, $owner); | |
565 | eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); }; | |
566 | if ($owner && ($owner == $vmid)) { | |
567 | return 1; | |
568 | } | |
569 | } | |
570 | ||
571 | return undef; | |
572 | }; | |
573 | ||
574 | my $test_deallocate_drive = sub { | |
575 | my ($storecfg, $vmid, $key, $drive, $force) = @_; | |
576 | ||
577 | if (!PVE::QemuServer::drive_is_cdrom($drive)) { | |
578 | my $volid = $drive->{file}; | |
579 | if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) { | |
580 | if ($force || $key =~ m/^unused/) { | |
581 | my $sid = PVE::Storage::parse_volume_id($volid); | |
582 | return $sid; | |
583 | } | |
584 | } | |
585 | } | |
586 | ||
587 | return undef; | |
588 | }; | |
589 | ||
5d7a6767 | 590 | my $delete_drive = sub { |
1858638f | 591 | my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_; |
5d7a6767 DM |
592 | |
593 | if (!PVE::QemuServer::drive_is_cdrom($drive)) { | |
594 | my $volid = $drive->{file}; | |
ae57f6b3 DM |
595 | if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) { |
596 | if ($force || $key =~ m/^unused/) { | |
597 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
598 | warn $@ if $@; | |
599 | } else { | |
600 | PVE::QemuServer::add_unused_volume($conf, $volid, $vmid); | |
5d7a6767 | 601 | } |
ae57f6b3 DM |
602 | delete $conf->{$key}; |
603 | } | |
604 | } | |
605 | }; | |
606 | ||
607 | my $vmconfig_delete_option = sub { | |
608 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_; | |
609 | ||
610 | return if !defined($conf->{$opt}); | |
611 | ||
612 | my $isDisk = PVE::QemuServer::valid_drivename($opt)|| ($opt =~ m/^unused/); | |
613 | ||
614 | if ($isDisk) { | |
615 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
616 | ||
617 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
618 | if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) { | |
619 | $rpcenv->check_storage_perm($authuser, $vmid, undef, $sid, [ 'Datastore.Allocate' ]); | |
5d7a6767 | 620 | } |
ae57f6b3 DM |
621 | } |
622 | ||
623 | die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt); | |
624 | ||
625 | if ($isDisk) { | |
626 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
627 | &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force); | |
5d7a6767 | 628 | } else { |
ae57f6b3 DM |
629 | delete $conf->{$opt}; |
630 | } | |
631 | ||
632 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
633 | }; | |
634 | ||
635 | my $vmconfig_update_disk = sub { | |
636 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_; | |
5d7a6767 | 637 | |
ae57f6b3 DM |
638 | my $drive = PVE::QemuServer::parse_drive($opt, $value); |
639 | ||
640 | if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom | |
641 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']); | |
642 | } else { | |
643 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
644 | } | |
645 | ||
646 | if ($conf->{$opt}) { | |
647 | ||
648 | if (my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt})) { | |
649 | ||
650 | my $media = $drive->{media} || 'disk'; | |
651 | my $oldmedia = $old_drive->{media} || 'disk'; | |
652 | die "unable to change media type\n" if $media ne $oldmedia; | |
653 | ||
654 | if (!PVE::QemuServer::drive_is_cdrom($old_drive) && | |
655 | ($drive->{file} ne $old_drive->{file})) { # delete old disks | |
656 | ||
657 | &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force); | |
658 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
659 | } | |
660 | } | |
661 | } | |
662 | ||
663 | &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value}); | |
664 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
665 | ||
666 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
667 | $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
668 | ||
669 | if (PVE::QemuServer::drive_is_cdrom($drive)) { # cdrom | |
670 | ||
671 | if (PVE::QemuServer::check_running($vmid)) { | |
672 | if ($drive->{file} eq 'none') { | |
673 | PVE::QemuServer::vm_monitor_command($vmid, "eject -f drive-$opt", 0); | |
674 | } else { | |
675 | my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file}); | |
676 | PVE::QemuServer::vm_monitor_command($vmid, "eject -f drive-$opt", 0); #force eject if locked | |
677 | PVE::QemuServer::vm_monitor_command($vmid, "change drive-$opt \"$path\"", 0) if $path; | |
678 | } | |
679 | } | |
680 | ||
681 | } else { # hotplug new disks | |
682 | ||
683 | die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive); | |
5d7a6767 | 684 | } |
5d7a6767 DM |
685 | }; |
686 | ||
ae57f6b3 DM |
687 | my $vmconfig_update_net = sub { |
688 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_; | |
689 | ||
690 | if ($conf->{$opt}) { | |
691 | #if online update, then unplug first | |
692 | die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt); | |
693 | } | |
694 | ||
695 | $conf->{$opt} = $value; | |
696 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
697 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
698 | ||
699 | my $net = PVE::QemuServer::parse_net($conf->{$opt}); | |
700 | ||
701 | die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net); | |
702 | }; | |
703 | ||
a0d1b1a2 DM |
704 | my $vm_config_perm_list = [ |
705 | 'VM.Config.Disk', | |
706 | 'VM.Config.CDROM', | |
707 | 'VM.Config.CPU', | |
708 | 'VM.Config.Memory', | |
709 | 'VM.Config.Network', | |
710 | 'VM.Config.HWType', | |
711 | 'VM.Config.Options', | |
712 | ]; | |
713 | ||
1e3baf05 | 714 | __PACKAGE__->register_method({ |
afdb31d5 DM |
715 | name => 'update_vm', |
716 | path => '{vmid}/config', | |
1e3baf05 DM |
717 | method => 'PUT', |
718 | protected => 1, | |
719 | proxyto => 'node', | |
720 | description => "Set virtual machine options.", | |
a0d1b1a2 DM |
721 | permissions => { |
722 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
723 | }, | |
1e3baf05 DM |
724 | parameters => { |
725 | additionalProperties => 0, | |
726 | properties => PVE::QemuServer::json_config_properties( | |
727 | { | |
728 | node => get_standard_option('pve-node'), | |
729 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 | 730 | skiplock => get_standard_option('skiplock'), |
1e3baf05 DM |
731 | delete => { |
732 | type => 'string', format => 'pve-configid-list', | |
733 | description => "A list of settings you want to delete.", | |
734 | optional => 1, | |
735 | }, | |
736 | force => { | |
737 | type => 'boolean', | |
738 | description => $opt_force_description, | |
739 | optional => 1, | |
740 | requires => 'delete', | |
741 | }, | |
554ac7e7 DM |
742 | digest => { |
743 | type => 'string', | |
744 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
745 | maxLength => 40, | |
afdb31d5 | 746 | optional => 1, |
554ac7e7 | 747 | } |
1e3baf05 DM |
748 | }), |
749 | }, | |
750 | returns => { type => 'null'}, | |
751 | code => sub { | |
752 | my ($param) = @_; | |
753 | ||
754 | my $rpcenv = PVE::RPCEnvironment::get(); | |
755 | ||
a0d1b1a2 | 756 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
757 | |
758 | my $node = extract_param($param, 'node'); | |
759 | ||
1e3baf05 DM |
760 | my $vmid = extract_param($param, 'vmid'); |
761 | ||
5fdbe4f0 DM |
762 | my $digest = extract_param($param, 'digest'); |
763 | ||
764 | my @paramarr = (); # used for log message | |
765 | foreach my $key (keys %$param) { | |
766 | push @paramarr, "-$key", $param->{$key}; | |
767 | } | |
768 | ||
1e3baf05 | 769 | my $skiplock = extract_param($param, 'skiplock'); |
afdb31d5 | 770 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 771 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 772 | |
0532bc63 DM |
773 | my $delete_str = extract_param($param, 'delete'); |
774 | ||
1e3baf05 DM |
775 | my $force = extract_param($param, 'force'); |
776 | ||
0532bc63 DM |
777 | die "no options specified\n" if !$delete_str && !scalar(keys %$param); |
778 | ||
1e68cb19 DM |
779 | my $storecfg = PVE::Storage::config(); |
780 | ||
781 | &$resolve_cdrom_alias($param); | |
782 | ||
783 | # now try to verify all parameters | |
784 | ||
0532bc63 DM |
785 | my @delete = (); |
786 | foreach my $opt (PVE::Tools::split_list($delete_str)) { | |
787 | $opt = 'ide2' if $opt eq 'cdrom'; | |
788 | raise_param_exc({ delete => "you can't use '-$opt' and " . | |
789 | "-delete $opt' at the same time" }) | |
790 | if defined($param->{$opt}); | |
791 | ||
792 | if (!PVE::QemuServer::option_exists($opt)) { | |
793 | raise_param_exc({ delete => "unknown option '$opt'" }); | |
794 | } | |
ae57f6b3 | 795 | |
0532bc63 DM |
796 | push @delete, $opt; |
797 | } | |
1e3baf05 | 798 | |
1e68cb19 DM |
799 | foreach my $opt (keys %$param) { |
800 | if (PVE::QemuServer::valid_drivename($opt)) { | |
801 | # cleanup drive path | |
802 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
803 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); | |
804 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
1e68cb19 DM |
805 | } elsif ($opt =~ m/^net(\d+)$/) { |
806 | # add macaddr | |
807 | my $net = PVE::QemuServer::parse_net($param->{$opt}); | |
808 | $param->{$opt} = PVE::QemuServer::print_net($net); | |
1e68cb19 DM |
809 | } |
810 | } | |
1e3baf05 | 811 | |
ae57f6b3 DM |
812 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]); |
813 | ||
814 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]); | |
815 | ||
816 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, undef, $param); | |
1e3baf05 | 817 | |
5d39a182 | 818 | my $updatefn = sub { |
1e3baf05 | 819 | |
5d39a182 | 820 | my $conf = PVE::QemuServer::load_config($vmid); |
1e3baf05 | 821 | |
5d39a182 DM |
822 | die "checksum missmatch (file change by other user?)\n" |
823 | if $digest && $digest ne $conf->{digest}; | |
1e3baf05 | 824 | |
5d39a182 | 825 | PVE::QemuServer::check_lock($conf) if !$skiplock; |
1e3baf05 | 826 | |
a0d1b1a2 | 827 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr)); |
c2a64aa7 | 828 | |
5d7a6767 | 829 | foreach my $opt (@delete) { # delete |
1e68cb19 | 830 | $conf = PVE::QemuServer::load_config($vmid); # update/reload |
ae57f6b3 | 831 | &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force); |
5d39a182 | 832 | } |
1e3baf05 | 833 | |
5d7a6767 | 834 | foreach my $opt (keys %$param) { # add/change |
1e3baf05 | 835 | |
1e68cb19 | 836 | $conf = PVE::QemuServer::load_config($vmid); # update/reload |
1e3baf05 | 837 | |
5d7a6767 DM |
838 | next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed |
839 | ||
ae57f6b3 | 840 | if (PVE::QemuServer::valid_drivename($opt)) { |
5d7a6767 | 841 | |
ae57f6b3 DM |
842 | &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid, |
843 | $opt, $param->{$opt}, $force); | |
1e68cb19 | 844 | |
ae57f6b3 | 845 | } elsif ($opt =~ m/^net(\d+)$/) { #nics |
1858638f | 846 | |
ae57f6b3 DM |
847 | &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid, |
848 | $opt, $param->{$opt}); | |
1e68cb19 DM |
849 | |
850 | } else { | |
851 | ||
1858638f DM |
852 | $conf->{$opt} = $param->{$opt}; |
853 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
3a1e36bb | 854 | } |
5d39a182 DM |
855 | } |
856 | }; | |
857 | ||
858 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
fcdb0117 | 859 | |
1e3baf05 DM |
860 | return undef; |
861 | }}); | |
862 | ||
863 | ||
864 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
865 | name => 'destroy_vm', |
866 | path => '{vmid}', | |
1e3baf05 DM |
867 | method => 'DELETE', |
868 | protected => 1, | |
869 | proxyto => 'node', | |
870 | description => "Destroy the vm (also delete all used/owned volumes).", | |
a0d1b1a2 DM |
871 | permissions => { |
872 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
873 | }, | |
1e3baf05 DM |
874 | parameters => { |
875 | additionalProperties => 0, | |
876 | properties => { | |
877 | node => get_standard_option('pve-node'), | |
878 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 | 879 | skiplock => get_standard_option('skiplock'), |
1e3baf05 DM |
880 | }, |
881 | }, | |
afdb31d5 | 882 | returns => { |
5fdbe4f0 DM |
883 | type => 'string', |
884 | }, | |
1e3baf05 DM |
885 | code => sub { |
886 | my ($param) = @_; | |
887 | ||
888 | my $rpcenv = PVE::RPCEnvironment::get(); | |
889 | ||
a0d1b1a2 | 890 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
891 | |
892 | my $vmid = $param->{vmid}; | |
893 | ||
894 | my $skiplock = $param->{skiplock}; | |
afdb31d5 | 895 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 896 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 897 | |
5fdbe4f0 DM |
898 | # test if VM exists |
899 | my $conf = PVE::QemuServer::load_config($vmid); | |
900 | ||
afdb31d5 | 901 | my $storecfg = PVE::Storage::config(); |
1e3baf05 | 902 | |
5fdbe4f0 | 903 | my $realcmd = sub { |
ff1a2432 DM |
904 | my $upid = shift; |
905 | ||
906 | syslog('info', "destroy VM $vmid: $upid\n"); | |
907 | ||
5fdbe4f0 DM |
908 | PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); |
909 | }; | |
1e3baf05 | 910 | |
a0d1b1a2 | 911 | return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd); |
1e3baf05 DM |
912 | }}); |
913 | ||
914 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
915 | name => 'unlink', |
916 | path => '{vmid}/unlink', | |
1e3baf05 DM |
917 | method => 'PUT', |
918 | protected => 1, | |
919 | proxyto => 'node', | |
920 | description => "Unlink/delete disk images.", | |
a0d1b1a2 DM |
921 | permissions => { |
922 | check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']], | |
923 | }, | |
1e3baf05 DM |
924 | parameters => { |
925 | additionalProperties => 0, | |
926 | properties => { | |
927 | node => get_standard_option('pve-node'), | |
928 | vmid => get_standard_option('pve-vmid'), | |
929 | idlist => { | |
930 | type => 'string', format => 'pve-configid-list', | |
931 | description => "A list of disk IDs you want to delete.", | |
932 | }, | |
933 | force => { | |
934 | type => 'boolean', | |
935 | description => $opt_force_description, | |
936 | optional => 1, | |
937 | }, | |
938 | }, | |
939 | }, | |
940 | returns => { type => 'null'}, | |
941 | code => sub { | |
942 | my ($param) = @_; | |
943 | ||
944 | $param->{delete} = extract_param($param, 'idlist'); | |
945 | ||
946 | __PACKAGE__->update_vm($param); | |
947 | ||
948 | return undef; | |
949 | }}); | |
950 | ||
951 | my $sslcert; | |
952 | ||
953 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
954 | name => 'vncproxy', |
955 | path => '{vmid}/vncproxy', | |
1e3baf05 DM |
956 | method => 'POST', |
957 | protected => 1, | |
958 | permissions => { | |
378b359e | 959 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], |
1e3baf05 DM |
960 | }, |
961 | description => "Creates a TCP VNC proxy connections.", | |
962 | parameters => { | |
963 | additionalProperties => 0, | |
964 | properties => { | |
965 | node => get_standard_option('pve-node'), | |
966 | vmid => get_standard_option('pve-vmid'), | |
967 | }, | |
968 | }, | |
afdb31d5 | 969 | returns => { |
1e3baf05 DM |
970 | additionalProperties => 0, |
971 | properties => { | |
972 | user => { type => 'string' }, | |
973 | ticket => { type => 'string' }, | |
974 | cert => { type => 'string' }, | |
975 | port => { type => 'integer' }, | |
976 | upid => { type => 'string' }, | |
977 | }, | |
978 | }, | |
979 | code => sub { | |
980 | my ($param) = @_; | |
981 | ||
982 | my $rpcenv = PVE::RPCEnvironment::get(); | |
983 | ||
a0d1b1a2 | 984 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
985 | |
986 | my $vmid = $param->{vmid}; | |
987 | my $node = $param->{node}; | |
988 | ||
b6f39da2 DM |
989 | my $authpath = "/vms/$vmid"; |
990 | ||
a0d1b1a2 | 991 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); |
b6f39da2 | 992 | |
1e3baf05 DM |
993 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) |
994 | if !$sslcert; | |
995 | ||
996 | my $port = PVE::Tools::next_vnc_port(); | |
997 | ||
998 | my $remip; | |
afdb31d5 | 999 | |
4f1be36c | 1000 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { |
1e3baf05 DM |
1001 | $remip = PVE::Cluster::remote_node_ip($node); |
1002 | } | |
1003 | ||
1004 | # NOTE: kvm VNC traffic is already TLS encrypted, | |
1005 | # so we select the fastest chipher here (or 'none'?) | |
1006 | my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', | |
1007 | '-c', 'blowfish-cbc', $remip] : []; | |
1008 | ||
afdb31d5 | 1009 | my $timeout = 10; |
1e3baf05 DM |
1010 | |
1011 | my $realcmd = sub { | |
1012 | my $upid = shift; | |
1013 | ||
1014 | syslog('info', "starting vnc proxy $upid\n"); | |
1015 | ||
1016 | my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid]; | |
1017 | ||
1018 | my $qmstr = join(' ', @$qmcmd); | |
1019 | ||
1020 | # also redirect stderr (else we get RFB protocol errors) | |
be62c45c | 1021 | my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"]; |
1e3baf05 | 1022 | |
be62c45c | 1023 | PVE::Tools::run_command($cmd); |
1e3baf05 DM |
1024 | |
1025 | return; | |
1026 | }; | |
1027 | ||
a0d1b1a2 | 1028 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); |
1e3baf05 DM |
1029 | |
1030 | return { | |
a0d1b1a2 | 1031 | user => $authuser, |
1e3baf05 | 1032 | ticket => $ticket, |
afdb31d5 DM |
1033 | port => $port, |
1034 | upid => $upid, | |
1035 | cert => $sslcert, | |
1e3baf05 DM |
1036 | }; |
1037 | }}); | |
1038 | ||
5fdbe4f0 DM |
1039 | __PACKAGE__->register_method({ |
1040 | name => 'vmcmdidx', | |
afdb31d5 | 1041 | path => '{vmid}/status', |
5fdbe4f0 DM |
1042 | method => 'GET', |
1043 | proxyto => 'node', | |
1044 | description => "Directory index", | |
a0d1b1a2 DM |
1045 | permissions => { |
1046 | user => 'all', | |
1047 | }, | |
5fdbe4f0 DM |
1048 | parameters => { |
1049 | additionalProperties => 0, | |
1050 | properties => { | |
1051 | node => get_standard_option('pve-node'), | |
1052 | vmid => get_standard_option('pve-vmid'), | |
1053 | }, | |
1054 | }, | |
1055 | returns => { | |
1056 | type => 'array', | |
1057 | items => { | |
1058 | type => "object", | |
1059 | properties => { | |
1060 | subdir => { type => 'string' }, | |
1061 | }, | |
1062 | }, | |
1063 | links => [ { rel => 'child', href => "{subdir}" } ], | |
1064 | }, | |
1065 | code => sub { | |
1066 | my ($param) = @_; | |
1067 | ||
1068 | # test if VM exists | |
1069 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
1070 | ||
1071 | my $res = [ | |
1072 | { subdir => 'current' }, | |
1073 | { subdir => 'start' }, | |
1074 | { subdir => 'stop' }, | |
1075 | ]; | |
afdb31d5 | 1076 | |
5fdbe4f0 DM |
1077 | return $res; |
1078 | }}); | |
1079 | ||
1e3baf05 | 1080 | __PACKAGE__->register_method({ |
afdb31d5 | 1081 | name => 'vm_status', |
5fdbe4f0 | 1082 | path => '{vmid}/status/current', |
1e3baf05 DM |
1083 | method => 'GET', |
1084 | proxyto => 'node', | |
1085 | protected => 1, # qemu pid files are only readable by root | |
1086 | description => "Get virtual machine status.", | |
a0d1b1a2 DM |
1087 | permissions => { |
1088 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1089 | }, | |
1e3baf05 DM |
1090 | parameters => { |
1091 | additionalProperties => 0, | |
1092 | properties => { | |
1093 | node => get_standard_option('pve-node'), | |
1094 | vmid => get_standard_option('pve-vmid'), | |
1095 | }, | |
1096 | }, | |
1097 | returns => { type => 'object' }, | |
1098 | code => sub { | |
1099 | my ($param) = @_; | |
1100 | ||
1101 | # test if VM exists | |
1102 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
1103 | ||
ff1a2432 | 1104 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}); |
8610701a | 1105 | my $status = $vmstatus->{$param->{vmid}}; |
1e3baf05 | 1106 | |
8610701a DM |
1107 | my $cc = PVE::Cluster::cfs_read_file('cluster.conf'); |
1108 | if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) { | |
1109 | $status->{ha} = 1; | |
1110 | } else { | |
1111 | $status->{ha} = 0; | |
1112 | } | |
1113 | ||
1114 | return $status; | |
1e3baf05 DM |
1115 | }}); |
1116 | ||
1117 | __PACKAGE__->register_method({ | |
afdb31d5 | 1118 | name => 'vm_start', |
5fdbe4f0 DM |
1119 | path => '{vmid}/status/start', |
1120 | method => 'POST', | |
1e3baf05 DM |
1121 | protected => 1, |
1122 | proxyto => 'node', | |
5fdbe4f0 | 1123 | description => "Start virtual machine.", |
a0d1b1a2 DM |
1124 | permissions => { |
1125 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1126 | }, | |
1e3baf05 DM |
1127 | parameters => { |
1128 | additionalProperties => 0, | |
1129 | properties => { | |
1130 | node => get_standard_option('pve-node'), | |
1131 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 DM |
1132 | skiplock => get_standard_option('skiplock'), |
1133 | stateuri => get_standard_option('pve-qm-stateuri'), | |
1e3baf05 DM |
1134 | }, |
1135 | }, | |
afdb31d5 | 1136 | returns => { |
5fdbe4f0 DM |
1137 | type => 'string', |
1138 | }, | |
1e3baf05 DM |
1139 | code => sub { |
1140 | my ($param) = @_; | |
1141 | ||
1142 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1143 | ||
a0d1b1a2 | 1144 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1145 | |
1146 | my $node = extract_param($param, 'node'); | |
1147 | ||
1e3baf05 DM |
1148 | my $vmid = extract_param($param, 'vmid'); |
1149 | ||
3ea94c60 | 1150 | my $stateuri = extract_param($param, 'stateuri'); |
afdb31d5 | 1151 | raise_param_exc({ stateuri => "Only root may use this option." }) |
a0d1b1a2 | 1152 | if $stateuri && $authuser ne 'root@pam'; |
3ea94c60 | 1153 | |
1e3baf05 | 1154 | my $skiplock = extract_param($param, 'skiplock'); |
afdb31d5 | 1155 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1156 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 1157 | |
afdb31d5 | 1158 | my $storecfg = PVE::Storage::config(); |
5fdbe4f0 DM |
1159 | |
1160 | my $realcmd = sub { | |
1161 | my $upid = shift; | |
1162 | ||
1163 | syslog('info', "start VM $vmid: $upid\n"); | |
1164 | ||
3ea94c60 | 1165 | PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock); |
5fdbe4f0 DM |
1166 | |
1167 | return; | |
1168 | }; | |
1169 | ||
a0d1b1a2 | 1170 | return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1171 | }}); |
1172 | ||
1173 | __PACKAGE__->register_method({ | |
afdb31d5 | 1174 | name => 'vm_stop', |
5fdbe4f0 DM |
1175 | path => '{vmid}/status/stop', |
1176 | method => 'POST', | |
1177 | protected => 1, | |
1178 | proxyto => 'node', | |
1179 | description => "Stop virtual machine.", | |
a0d1b1a2 DM |
1180 | permissions => { |
1181 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1182 | }, | |
5fdbe4f0 DM |
1183 | parameters => { |
1184 | additionalProperties => 0, | |
1185 | properties => { | |
1186 | node => get_standard_option('pve-node'), | |
1187 | vmid => get_standard_option('pve-vmid'), | |
1188 | skiplock => get_standard_option('skiplock'), | |
c6bb9502 DM |
1189 | timeout => { |
1190 | description => "Wait maximal timeout seconds.", | |
1191 | type => 'integer', | |
1192 | minimum => 0, | |
1193 | optional => 1, | |
254575e9 DM |
1194 | }, |
1195 | keepActive => { | |
1196 | description => "Do not decativate storage volumes.", | |
1197 | type => 'boolean', | |
1198 | optional => 1, | |
1199 | default => 0, | |
c6bb9502 | 1200 | } |
5fdbe4f0 DM |
1201 | }, |
1202 | }, | |
afdb31d5 | 1203 | returns => { |
5fdbe4f0 DM |
1204 | type => 'string', |
1205 | }, | |
1206 | code => sub { | |
1207 | my ($param) = @_; | |
1208 | ||
1209 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1210 | ||
a0d1b1a2 | 1211 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1212 | |
1213 | my $node = extract_param($param, 'node'); | |
1214 | ||
1215 | my $vmid = extract_param($param, 'vmid'); | |
1216 | ||
1217 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1218 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1219 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1220 | |
254575e9 | 1221 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 1222 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 1223 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 1224 | |
ff1a2432 DM |
1225 | my $storecfg = PVE::Storage::config(); |
1226 | ||
5fdbe4f0 DM |
1227 | my $realcmd = sub { |
1228 | my $upid = shift; | |
1229 | ||
1230 | syslog('info', "stop VM $vmid: $upid\n"); | |
1231 | ||
afdb31d5 | 1232 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, |
254575e9 | 1233 | $param->{timeout}, 0, 1, $keepActive); |
c6bb9502 | 1234 | |
5fdbe4f0 DM |
1235 | return; |
1236 | }; | |
1237 | ||
a0d1b1a2 | 1238 | return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1239 | }}); |
1240 | ||
1241 | __PACKAGE__->register_method({ | |
afdb31d5 | 1242 | name => 'vm_reset', |
5fdbe4f0 DM |
1243 | path => '{vmid}/status/reset', |
1244 | method => 'POST', | |
1245 | protected => 1, | |
1246 | proxyto => 'node', | |
1247 | description => "Reset virtual machine.", | |
a0d1b1a2 DM |
1248 | permissions => { |
1249 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1250 | }, | |
5fdbe4f0 DM |
1251 | parameters => { |
1252 | additionalProperties => 0, | |
1253 | properties => { | |
1254 | node => get_standard_option('pve-node'), | |
1255 | vmid => get_standard_option('pve-vmid'), | |
1256 | skiplock => get_standard_option('skiplock'), | |
1257 | }, | |
1258 | }, | |
afdb31d5 | 1259 | returns => { |
5fdbe4f0 DM |
1260 | type => 'string', |
1261 | }, | |
1262 | code => sub { | |
1263 | my ($param) = @_; | |
1264 | ||
1265 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1266 | ||
a0d1b1a2 | 1267 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1268 | |
1269 | my $node = extract_param($param, 'node'); | |
1270 | ||
1271 | my $vmid = extract_param($param, 'vmid'); | |
1272 | ||
1273 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1274 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1275 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1276 | |
ff1a2432 DM |
1277 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
1278 | ||
5fdbe4f0 DM |
1279 | my $realcmd = sub { |
1280 | my $upid = shift; | |
1281 | ||
1e3baf05 | 1282 | PVE::QemuServer::vm_reset($vmid, $skiplock); |
5fdbe4f0 DM |
1283 | |
1284 | return; | |
1285 | }; | |
1286 | ||
a0d1b1a2 | 1287 | return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1288 | }}); |
1289 | ||
1290 | __PACKAGE__->register_method({ | |
afdb31d5 | 1291 | name => 'vm_shutdown', |
5fdbe4f0 DM |
1292 | path => '{vmid}/status/shutdown', |
1293 | method => 'POST', | |
1294 | protected => 1, | |
1295 | proxyto => 'node', | |
1296 | description => "Shutdown virtual machine.", | |
a0d1b1a2 DM |
1297 | permissions => { |
1298 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1299 | }, | |
5fdbe4f0 DM |
1300 | parameters => { |
1301 | additionalProperties => 0, | |
1302 | properties => { | |
1303 | node => get_standard_option('pve-node'), | |
1304 | vmid => get_standard_option('pve-vmid'), | |
1305 | skiplock => get_standard_option('skiplock'), | |
c6bb9502 DM |
1306 | timeout => { |
1307 | description => "Wait maximal timeout seconds.", | |
1308 | type => 'integer', | |
1309 | minimum => 0, | |
1310 | optional => 1, | |
9269013a DM |
1311 | }, |
1312 | forceStop => { | |
1313 | description => "Make sure the VM stops.", | |
1314 | type => 'boolean', | |
1315 | optional => 1, | |
1316 | default => 0, | |
254575e9 DM |
1317 | }, |
1318 | keepActive => { | |
1319 | description => "Do not decativate storage volumes.", | |
1320 | type => 'boolean', | |
1321 | optional => 1, | |
1322 | default => 0, | |
c6bb9502 | 1323 | } |
5fdbe4f0 DM |
1324 | }, |
1325 | }, | |
afdb31d5 | 1326 | returns => { |
5fdbe4f0 DM |
1327 | type => 'string', |
1328 | }, | |
1329 | code => sub { | |
1330 | my ($param) = @_; | |
1331 | ||
1332 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1333 | ||
a0d1b1a2 | 1334 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1335 | |
1336 | my $node = extract_param($param, 'node'); | |
1337 | ||
1338 | my $vmid = extract_param($param, 'vmid'); | |
1339 | ||
1340 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1341 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1342 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1343 | |
254575e9 | 1344 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 1345 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 1346 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 1347 | |
02d07cf5 DM |
1348 | my $storecfg = PVE::Storage::config(); |
1349 | ||
5fdbe4f0 DM |
1350 | my $realcmd = sub { |
1351 | my $upid = shift; | |
1352 | ||
1353 | syslog('info', "shutdown VM $vmid: $upid\n"); | |
1354 | ||
afdb31d5 | 1355 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout}, |
254575e9 | 1356 | 1, $param->{forceStop}, $keepActive); |
c6bb9502 | 1357 | |
5fdbe4f0 DM |
1358 | return; |
1359 | }; | |
1360 | ||
a0d1b1a2 | 1361 | return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1362 | }}); |
1363 | ||
1364 | __PACKAGE__->register_method({ | |
afdb31d5 | 1365 | name => 'vm_suspend', |
5fdbe4f0 DM |
1366 | path => '{vmid}/status/suspend', |
1367 | method => 'POST', | |
1368 | protected => 1, | |
1369 | proxyto => 'node', | |
1370 | description => "Suspend virtual machine.", | |
a0d1b1a2 DM |
1371 | permissions => { |
1372 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1373 | }, | |
5fdbe4f0 DM |
1374 | parameters => { |
1375 | additionalProperties => 0, | |
1376 | properties => { | |
1377 | node => get_standard_option('pve-node'), | |
1378 | vmid => get_standard_option('pve-vmid'), | |
1379 | skiplock => get_standard_option('skiplock'), | |
1380 | }, | |
1381 | }, | |
afdb31d5 | 1382 | returns => { |
5fdbe4f0 DM |
1383 | type => 'string', |
1384 | }, | |
1385 | code => sub { | |
1386 | my ($param) = @_; | |
1387 | ||
1388 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1389 | ||
a0d1b1a2 | 1390 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1391 | |
1392 | my $node = extract_param($param, 'node'); | |
1393 | ||
1394 | my $vmid = extract_param($param, 'vmid'); | |
1395 | ||
1396 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1397 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1398 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1399 | |
ff1a2432 DM |
1400 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
1401 | ||
5fdbe4f0 DM |
1402 | my $realcmd = sub { |
1403 | my $upid = shift; | |
1404 | ||
1405 | syslog('info', "suspend VM $vmid: $upid\n"); | |
1406 | ||
1e3baf05 | 1407 | PVE::QemuServer::vm_suspend($vmid, $skiplock); |
5fdbe4f0 DM |
1408 | |
1409 | return; | |
1410 | }; | |
1411 | ||
a0d1b1a2 | 1412 | return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1413 | }}); |
1414 | ||
1415 | __PACKAGE__->register_method({ | |
afdb31d5 | 1416 | name => 'vm_resume', |
5fdbe4f0 DM |
1417 | path => '{vmid}/status/resume', |
1418 | method => 'POST', | |
1419 | protected => 1, | |
1420 | proxyto => 'node', | |
1421 | description => "Resume virtual machine.", | |
a0d1b1a2 DM |
1422 | permissions => { |
1423 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1424 | }, | |
5fdbe4f0 DM |
1425 | parameters => { |
1426 | additionalProperties => 0, | |
1427 | properties => { | |
1428 | node => get_standard_option('pve-node'), | |
1429 | vmid => get_standard_option('pve-vmid'), | |
1430 | skiplock => get_standard_option('skiplock'), | |
1431 | }, | |
1432 | }, | |
afdb31d5 | 1433 | returns => { |
5fdbe4f0 DM |
1434 | type => 'string', |
1435 | }, | |
1436 | code => sub { | |
1437 | my ($param) = @_; | |
1438 | ||
1439 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1440 | ||
a0d1b1a2 | 1441 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1442 | |
1443 | my $node = extract_param($param, 'node'); | |
1444 | ||
1445 | my $vmid = extract_param($param, 'vmid'); | |
1446 | ||
1447 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1448 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1449 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1450 | |
b7eeab21 | 1451 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
ff1a2432 | 1452 | |
5fdbe4f0 DM |
1453 | my $realcmd = sub { |
1454 | my $upid = shift; | |
1455 | ||
1456 | syslog('info', "resume VM $vmid: $upid\n"); | |
1457 | ||
1e3baf05 | 1458 | PVE::QemuServer::vm_resume($vmid, $skiplock); |
1e3baf05 | 1459 | |
5fdbe4f0 DM |
1460 | return; |
1461 | }; | |
1462 | ||
a0d1b1a2 | 1463 | return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1464 | }}); |
1465 | ||
1466 | __PACKAGE__->register_method({ | |
afdb31d5 | 1467 | name => 'vm_sendkey', |
5fdbe4f0 DM |
1468 | path => '{vmid}/sendkey', |
1469 | method => 'PUT', | |
1470 | protected => 1, | |
1471 | proxyto => 'node', | |
1472 | description => "Send key event to virtual machine.", | |
a0d1b1a2 DM |
1473 | permissions => { |
1474 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1475 | }, | |
5fdbe4f0 DM |
1476 | parameters => { |
1477 | additionalProperties => 0, | |
1478 | properties => { | |
1479 | node => get_standard_option('pve-node'), | |
1480 | vmid => get_standard_option('pve-vmid'), | |
1481 | skiplock => get_standard_option('skiplock'), | |
1482 | key => { | |
1483 | description => "The key (qemu monitor encoding).", | |
1484 | type => 'string' | |
1485 | } | |
1486 | }, | |
1487 | }, | |
1488 | returns => { type => 'null'}, | |
1489 | code => sub { | |
1490 | my ($param) = @_; | |
1491 | ||
1492 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1493 | ||
a0d1b1a2 | 1494 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1495 | |
1496 | my $node = extract_param($param, 'node'); | |
1497 | ||
1498 | my $vmid = extract_param($param, 'vmid'); | |
1499 | ||
1500 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1501 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1502 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 DM |
1503 | |
1504 | PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); | |
1505 | ||
1506 | return; | |
1e3baf05 DM |
1507 | }}); |
1508 | ||
3ea94c60 | 1509 | __PACKAGE__->register_method({ |
afdb31d5 | 1510 | name => 'migrate_vm', |
3ea94c60 DM |
1511 | path => '{vmid}/migrate', |
1512 | method => 'POST', | |
1513 | protected => 1, | |
1514 | proxyto => 'node', | |
1515 | description => "Migrate virtual machine. Creates a new migration task.", | |
a0d1b1a2 DM |
1516 | permissions => { |
1517 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], | |
1518 | }, | |
3ea94c60 DM |
1519 | parameters => { |
1520 | additionalProperties => 0, | |
1521 | properties => { | |
1522 | node => get_standard_option('pve-node'), | |
1523 | vmid => get_standard_option('pve-vmid'), | |
1524 | target => get_standard_option('pve-node', { description => "Target node." }), | |
1525 | online => { | |
1526 | type => 'boolean', | |
1527 | description => "Use online/live migration.", | |
1528 | optional => 1, | |
1529 | }, | |
1530 | force => { | |
1531 | type => 'boolean', | |
1532 | description => "Allow to migrate VMs which use local devices. Only root may use this option.", | |
1533 | optional => 1, | |
1534 | }, | |
1535 | }, | |
1536 | }, | |
afdb31d5 | 1537 | returns => { |
3ea94c60 DM |
1538 | type => 'string', |
1539 | description => "the task ID.", | |
1540 | }, | |
1541 | code => sub { | |
1542 | my ($param) = @_; | |
1543 | ||
1544 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1545 | ||
a0d1b1a2 | 1546 | my $authuser = $rpcenv->get_user(); |
3ea94c60 DM |
1547 | |
1548 | my $target = extract_param($param, 'target'); | |
1549 | ||
1550 | my $localnode = PVE::INotify::nodename(); | |
1551 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
1552 | ||
1553 | PVE::Cluster::check_cfs_quorum(); | |
1554 | ||
1555 | PVE::Cluster::check_node_exists($target); | |
1556 | ||
1557 | my $targetip = PVE::Cluster::remote_node_ip($target); | |
1558 | ||
1559 | my $vmid = extract_param($param, 'vmid'); | |
1560 | ||
afdb31d5 | 1561 | raise_param_exc({ force => "Only root may use this option." }) |
a0d1b1a2 | 1562 | if $param->{force} && $authuser ne 'root@pam'; |
3ea94c60 DM |
1563 | |
1564 | # test if VM exists | |
a5ed42d3 | 1565 | my $conf = PVE::QemuServer::load_config($vmid); |
3ea94c60 DM |
1566 | |
1567 | # try to detect errors early | |
a5ed42d3 DM |
1568 | |
1569 | PVE::QemuServer::check_lock($conf); | |
1570 | ||
3ea94c60 | 1571 | if (PVE::QemuServer::check_running($vmid)) { |
afdb31d5 | 1572 | die "cant migrate running VM without --online\n" |
3ea94c60 DM |
1573 | if !$param->{online}; |
1574 | } | |
1575 | ||
1576 | my $realcmd = sub { | |
1577 | my $upid = shift; | |
1578 | ||
16e903f2 | 1579 | PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param); |
3ea94c60 DM |
1580 | }; |
1581 | ||
a0d1b1a2 | 1582 | my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd); |
3ea94c60 DM |
1583 | |
1584 | return $upid; | |
1585 | }}); | |
1e3baf05 | 1586 | |
91c94f0a | 1587 | __PACKAGE__->register_method({ |
afdb31d5 DM |
1588 | name => 'monitor', |
1589 | path => '{vmid}/monitor', | |
91c94f0a DM |
1590 | method => 'POST', |
1591 | protected => 1, | |
1592 | proxyto => 'node', | |
1593 | description => "Execute Qemu monitor commands.", | |
a0d1b1a2 DM |
1594 | permissions => { |
1595 | check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]], | |
1596 | }, | |
91c94f0a DM |
1597 | parameters => { |
1598 | additionalProperties => 0, | |
1599 | properties => { | |
1600 | node => get_standard_option('pve-node'), | |
1601 | vmid => get_standard_option('pve-vmid'), | |
1602 | command => { | |
1603 | type => 'string', | |
1604 | description => "The monitor command.", | |
1605 | } | |
1606 | }, | |
1607 | }, | |
1608 | returns => { type => 'string'}, | |
1609 | code => sub { | |
1610 | my ($param) = @_; | |
1611 | ||
1612 | my $vmid = $param->{vmid}; | |
1613 | ||
1614 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
1615 | ||
1616 | my $res = ''; | |
1617 | eval { | |
1618 | $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command}); | |
1619 | }; | |
1620 | $res = "ERROR: $@" if $@; | |
1621 | ||
1622 | return $res; | |
1623 | }}); | |
1624 | ||
1e3baf05 | 1625 | 1; |