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