]> git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu.pm
move get_replicatable_volumes from QemuServer.pm to QemuConfig.pm
[qemu-server.git] / PVE / API2 / Qemu.pm
1 package PVE::API2::Qemu;
2
3 use strict;
4 use warnings;
5 use Cwd 'abs_path';
6 use Net::SSLeay;
7 use UUID;
8 use POSIX;
9 use IO::Socket::IP;
10
11 use PVE::Cluster qw (cfs_read_file cfs_write_file);;
12 use PVE::SafeSyslog;
13 use PVE::Tools qw(extract_param);
14 use PVE::Exception qw(raise raise_param_exc raise_perm_exc);
15 use PVE::Storage;
16 use PVE::JSONSchema qw(get_standard_option);
17 use PVE::RESTHandler;
18 use PVE::QemuConfig;
19 use PVE::QemuServer;
20 use PVE::QemuMigrate;
21 use PVE::RPCEnvironment;
22 use PVE::AccessControl;
23 use PVE::INotify;
24 use PVE::Network;
25 use PVE::Firewall;
26 use PVE::API2::Firewall::VM;
27
28 BEGIN {
29 if (!$ENV{PVE_GENERATING_DOCS}) {
30 require PVE::HA::Env::PVE2;
31 import PVE::HA::Env::PVE2;
32 require PVE::HA::Config;
33 import PVE::HA::Config;
34 }
35 }
36
37 use Data::Dumper; # fixme: remove
38
39 use base qw(PVE::RESTHandler);
40
41 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.";
42
43 my $resolve_cdrom_alias = sub {
44 my $param = shift;
45
46 if (my $value = $param->{cdrom}) {
47 $value .= ",media=cdrom" if $value !~ m/media=/;
48 $param->{ide2} = $value;
49 delete $param->{cdrom};
50 }
51 };
52
53 my $check_storage_access = sub {
54 my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_;
55
56 PVE::QemuServer::foreach_drive($settings, sub {
57 my ($ds, $drive) = @_;
58
59 my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive);
60
61 my $volid = $drive->{file};
62
63 if (!$volid || $volid eq 'none') {
64 # nothing to check
65 } elsif ($isCDROM && ($volid eq 'cdrom')) {
66 $rpcenv->check($authuser, "/", ['Sys.Console']);
67 } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) {
68 my ($storeid, $size) = ($2 || $default_storage, $3);
69 die "no storage ID specified (and no default storage)\n" if !$storeid;
70 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
71 } else {
72 PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
73 }
74 });
75 };
76
77 my $check_storage_access_clone = sub {
78 my ($rpcenv, $authuser, $storecfg, $conf, $storage) = @_;
79
80 my $sharedvm = 1;
81
82 PVE::QemuServer::foreach_drive($conf, sub {
83 my ($ds, $drive) = @_;
84
85 my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive);
86
87 my $volid = $drive->{file};
88
89 return if !$volid || $volid eq 'none';
90
91 if ($isCDROM) {
92 if ($volid eq 'cdrom') {
93 $rpcenv->check($authuser, "/", ['Sys.Console']);
94 } else {
95 # we simply allow access
96 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
97 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
98 $sharedvm = 0 if !$scfg->{shared};
99
100 }
101 } else {
102 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
103 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
104 $sharedvm = 0 if !$scfg->{shared};
105
106 $sid = $storage if $storage;
107 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
108 }
109 });
110
111 return $sharedvm;
112 };
113
114 # Note: $pool is only needed when creating a VM, because pool permissions
115 # are automatically inherited if VM already exists inside a pool.
116 my $create_disks = sub {
117 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
118
119 my $vollist = [];
120
121 my $res = {};
122
123 my $code = sub {
124 my ($ds, $disk) = @_;
125
126 my $volid = $disk->{file};
127
128 if (!$volid || $volid eq 'none' || $volid eq 'cdrom') {
129 delete $disk->{size};
130 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
131 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
132 my ($storeid, $size) = ($2 || $default_storage, $3);
133 die "no storage ID specified (and no default storage)\n" if !$storeid;
134 my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
135 my $fmt = $disk->{format} || $defformat;
136
137 my $volid;
138 if ($ds eq 'efidisk0') {
139 # handle efidisk
140 my $ovmfvars = '/usr/share/kvm/OVMF_VARS-pure-efi.fd';
141 die "uefi vars image not found\n" if ! -f $ovmfvars;
142 $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid,
143 $fmt, undef, 128);
144 $disk->{file} = $volid;
145 $disk->{size} = 128*1024;
146 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
147 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
148 my $qemufmt = PVE::QemuServer::qemu_img_format($scfg, $volname);
149 my $path = PVE::Storage::path($storecfg, $volid);
150 my $efidiskcmd = ['/usr/bin/qemu-img', 'convert', '-n', '-f', 'raw', '-O', $qemufmt];
151 push @$efidiskcmd, $ovmfvars;
152 push @$efidiskcmd, $path;
153
154 PVE::Storage::activate_volumes($storecfg, [$volid]);
155
156 eval { PVE::Tools::run_command($efidiskcmd); };
157 my $err = $@;
158 die "Copying of EFI Vars image failed: $err" if $err;
159 } else {
160 $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid,
161 $fmt, undef, $size*1024*1024);
162 $disk->{file} = $volid;
163 $disk->{size} = $size*1024*1024*1024;
164 }
165 push @$vollist, $volid;
166 delete $disk->{format}; # no longer needed
167 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
168 } else {
169
170 PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
171
172 my $volid_is_new = 1;
173
174 if ($conf->{$ds}) {
175 my $olddrive = PVE::QemuServer::parse_drive($ds, $conf->{$ds});
176 $volid_is_new = undef if $olddrive->{file} && $olddrive->{file} eq $volid;
177 }
178
179 if ($volid_is_new) {
180
181 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
182
183 PVE::Storage::activate_volumes($storecfg, [ $volid ]) if $storeid;
184
185 my $size = PVE::Storage::volume_size_info($storecfg, $volid);
186
187 die "volume $volid does not exists\n" if !$size;
188
189 $disk->{size} = $size;
190 }
191
192 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
193 }
194 };
195
196 eval { PVE::QemuServer::foreach_drive($settings, $code); };
197
198 # free allocated images on error
199 if (my $err = $@) {
200 syslog('err', "VM $vmid creating disks failed");
201 foreach my $volid (@$vollist) {
202 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
203 warn $@ if $@;
204 }
205 die $err;
206 }
207
208 # modify vm config if everything went well
209 foreach my $ds (keys %$res) {
210 $conf->{$ds} = $res->{$ds};
211 }
212
213 return $vollist;
214 };
215
216 my $cpuoptions = {
217 'cores' => 1,
218 'cpu' => 1,
219 'cpulimit' => 1,
220 'cpuunits' => 1,
221 'numa' => 1,
222 'smp' => 1,
223 'sockets' => 1,
224 'vcpus' => 1,
225 };
226
227 my $memoryoptions = {
228 'memory' => 1,
229 'balloon' => 1,
230 'shares' => 1,
231 };
232
233 my $hwtypeoptions = {
234 'acpi' => 1,
235 'hotplug' => 1,
236 'kvm' => 1,
237 'machine' => 1,
238 'scsihw' => 1,
239 'smbios1' => 1,
240 'tablet' => 1,
241 'vga' => 1,
242 'watchdog' => 1,
243 };
244
245 my $generaloptions = {
246 'agent' => 1,
247 'autostart' => 1,
248 'bios' => 1,
249 'description' => 1,
250 'keyboard' => 1,
251 'localtime' => 1,
252 'migrate_downtime' => 1,
253 'migrate_speed' => 1,
254 'name' => 1,
255 'onboot' => 1,
256 'ostype' => 1,
257 'protection' => 1,
258 'reboot' => 1,
259 'startdate' => 1,
260 'startup' => 1,
261 'tdf' => 1,
262 'template' => 1,
263 };
264
265 my $vmpoweroptions = {
266 'freeze' => 1,
267 };
268
269 my $diskoptions = {
270 'boot' => 1,
271 'bootdisk' => 1,
272 };
273
274 my $check_vm_modify_config_perm = sub {
275 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
276
277 return 1 if $authuser eq 'root@pam';
278
279 foreach my $opt (@$key_list) {
280 # disk checks need to be done somewhere else
281 next if PVE::QemuServer::is_valid_drivename($opt);
282 next if $opt eq 'cdrom';
283 next if $opt =~ m/^unused\d+$/;
284
285 if ($cpuoptions->{$opt} || $opt =~ m/^numa\d+$/) {
286 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
287 } elsif ($memoryoptions->{$opt}) {
288 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
289 } elsif ($hwtypeoptions->{$opt}) {
290 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
291 } elsif ($generaloptions->{$opt}) {
292 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
293 # special case for startup since it changes host behaviour
294 if ($opt eq 'startup') {
295 $rpcenv->check_full($authuser, "/", ['Sys.Modify']);
296 }
297 } elsif ($vmpoweroptions->{$opt}) {
298 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.PowerMgmt']);
299 } elsif ($diskoptions->{$opt}) {
300 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
301 } elsif ($opt =~ m/^net\d+$/) {
302 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
303 } else {
304 # catches usb\d+, hostpci\d+, args, lock, etc.
305 # new options will be checked here
306 die "only root can set '$opt' config\n";
307 }
308 }
309
310 return 1;
311 };
312
313 __PACKAGE__->register_method({
314 name => 'vmlist',
315 path => '',
316 method => 'GET',
317 description => "Virtual machine index (per node).",
318 permissions => {
319 description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
320 user => 'all',
321 },
322 proxyto => 'node',
323 protected => 1, # qemu pid files are only readable by root
324 parameters => {
325 additionalProperties => 0,
326 properties => {
327 node => get_standard_option('pve-node'),
328 full => {
329 type => 'boolean',
330 optional => 1,
331 description => "Determine the full status of active VMs.",
332 },
333 },
334 },
335 returns => {
336 type => 'array',
337 items => {
338 type => "object",
339 properties => {},
340 },
341 links => [ { rel => 'child', href => "{vmid}" } ],
342 },
343 code => sub {
344 my ($param) = @_;
345
346 my $rpcenv = PVE::RPCEnvironment::get();
347 my $authuser = $rpcenv->get_user();
348
349 my $vmstatus = PVE::QemuServer::vmstatus(undef, $param->{full});
350
351 my $res = [];
352 foreach my $vmid (keys %$vmstatus) {
353 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
354
355 my $data = $vmstatus->{$vmid};
356 $data->{vmid} = int($vmid);
357 push @$res, $data;
358 }
359
360 return $res;
361 }});
362
363
364
365 __PACKAGE__->register_method({
366 name => 'create_vm',
367 path => '',
368 method => 'POST',
369 description => "Create or restore a virtual machine.",
370 permissions => {
371 description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
372 "For restore (option 'archive'), it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
373 "If you create disks you need 'Datastore.AllocateSpace' on any used storage.",
374 user => 'all', # check inside
375 },
376 protected => 1,
377 proxyto => 'node',
378 parameters => {
379 additionalProperties => 0,
380 properties => PVE::QemuServer::json_config_properties(
381 {
382 node => get_standard_option('pve-node'),
383 vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
384 archive => {
385 description => "The backup file.",
386 type => 'string',
387 optional => 1,
388 maxLength => 255,
389 completion => \&PVE::QemuServer::complete_backup_archives,
390 },
391 storage => get_standard_option('pve-storage-id', {
392 description => "Default storage.",
393 optional => 1,
394 completion => \&PVE::QemuServer::complete_storage,
395 }),
396 force => {
397 optional => 1,
398 type => 'boolean',
399 description => "Allow to overwrite existing VM.",
400 requires => 'archive',
401 },
402 unique => {
403 optional => 1,
404 type => 'boolean',
405 description => "Assign a unique random ethernet address.",
406 requires => 'archive',
407 },
408 pool => {
409 optional => 1,
410 type => 'string', format => 'pve-poolid',
411 description => "Add the VM to the specified pool.",
412 },
413 }),
414 },
415 returns => {
416 type => 'string',
417 },
418 code => sub {
419 my ($param) = @_;
420
421 my $rpcenv = PVE::RPCEnvironment::get();
422
423 my $authuser = $rpcenv->get_user();
424
425 my $node = extract_param($param, 'node');
426
427 my $vmid = extract_param($param, 'vmid');
428
429 my $archive = extract_param($param, 'archive');
430
431 my $storage = extract_param($param, 'storage');
432
433 my $force = extract_param($param, 'force');
434
435 my $unique = extract_param($param, 'unique');
436
437 my $pool = extract_param($param, 'pool');
438
439 my $filename = PVE::QemuConfig->config_file($vmid);
440
441 my $storecfg = PVE::Storage::config();
442
443 PVE::Cluster::check_cfs_quorum();
444
445 if (defined($pool)) {
446 $rpcenv->check_pool_exist($pool);
447 }
448
449 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
450 if defined($storage);
451
452 if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
453 # OK
454 } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
455 # OK
456 } elsif ($archive && $force && (-f $filename) &&
457 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
458 # OK: user has VM.Backup permissions, and want to restore an existing VM
459 } else {
460 raise_perm_exc();
461 }
462
463 if (!$archive) {
464 &$resolve_cdrom_alias($param);
465
466 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
467
468 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
469
470 foreach my $opt (keys %$param) {
471 if (PVE::QemuServer::is_valid_drivename($opt)) {
472 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
473 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
474
475 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
476 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
477 }
478 }
479
480 PVE::QemuServer::add_random_macs($param);
481 } else {
482 my $keystr = join(' ', keys %$param);
483 raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
484
485 if ($archive eq '-') {
486 die "pipe requires cli environment\n"
487 if $rpcenv->{type} ne 'cli';
488 } else {
489 PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $archive);
490 $archive = PVE::Storage::abs_filesystem_path($storecfg, $archive);
491 }
492 }
493
494 my $restorefn = sub {
495 my $vmlist = PVE::Cluster::get_vmlist();
496 if ($vmlist->{ids}->{$vmid}) {
497 my $current_node = $vmlist->{ids}->{$vmid}->{node};
498 if ($current_node eq $node) {
499 my $conf = PVE::QemuConfig->load_config($vmid);
500
501 PVE::QemuConfig->check_protection($conf, "unable to restore VM $vmid");
502
503 die "unable to restore vm $vmid - config file already exists\n"
504 if !$force;
505
506 die "unable to restore vm $vmid - vm is running\n"
507 if PVE::QemuServer::check_running($vmid);
508
509 die "unable to restore vm $vmid - vm is a template\n"
510 if PVE::QemuConfig->is_template($conf);
511
512 } else {
513 die "unable to restore vm $vmid - already existing on cluster node '$current_node'\n";
514 }
515 }
516
517 my $realcmd = sub {
518 PVE::QemuServer::restore_archive($archive, $vmid, $authuser, {
519 storage => $storage,
520 pool => $pool,
521 unique => $unique });
522
523 PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
524 };
525
526 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
527 };
528
529 my $createfn = sub {
530
531 # test after locking
532 PVE::Cluster::check_vmid_unused($vmid);
533
534 my $realcmd = sub {
535
536 my $vollist = [];
537
538 my $conf = $param;
539
540 eval {
541
542 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
543
544 # try to be smart about bootdisk
545 my @disks = PVE::QemuServer::valid_drive_names();
546 my $firstdisk;
547 foreach my $ds (reverse @disks) {
548 next if !$conf->{$ds};
549 my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds});
550 next if PVE::QemuServer::drive_is_cdrom($disk);
551 $firstdisk = $ds;
552 }
553
554 if (!$conf->{bootdisk} && $firstdisk) {
555 $conf->{bootdisk} = $firstdisk;
556 }
557
558 # auto generate uuid if user did not specify smbios1 option
559 if (!$conf->{smbios1}) {
560 my ($uuid, $uuid_str);
561 UUID::generate($uuid);
562 UUID::unparse($uuid, $uuid_str);
563 $conf->{smbios1} = "uuid=$uuid_str";
564 }
565
566 PVE::QemuConfig->write_config($vmid, $conf);
567
568 };
569 my $err = $@;
570
571 if ($err) {
572 foreach my $volid (@$vollist) {
573 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
574 warn $@ if $@;
575 }
576 die "create failed - $err";
577 }
578
579 PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
580 };
581
582 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
583 };
584
585 return PVE::QemuConfig->lock_config_full($vmid, 1, $archive ? $restorefn : $createfn);
586 }});
587
588 __PACKAGE__->register_method({
589 name => 'vmdiridx',
590 path => '{vmid}',
591 method => 'GET',
592 proxyto => 'node',
593 description => "Directory index",
594 permissions => {
595 user => 'all',
596 },
597 parameters => {
598 additionalProperties => 0,
599 properties => {
600 node => get_standard_option('pve-node'),
601 vmid => get_standard_option('pve-vmid'),
602 },
603 },
604 returns => {
605 type => 'array',
606 items => {
607 type => "object",
608 properties => {
609 subdir => { type => 'string' },
610 },
611 },
612 links => [ { rel => 'child', href => "{subdir}" } ],
613 },
614 code => sub {
615 my ($param) = @_;
616
617 my $res = [
618 { subdir => 'config' },
619 { subdir => 'pending' },
620 { subdir => 'status' },
621 { subdir => 'unlink' },
622 { subdir => 'vncproxy' },
623 { subdir => 'migrate' },
624 { subdir => 'resize' },
625 { subdir => 'move' },
626 { subdir => 'rrd' },
627 { subdir => 'rrddata' },
628 { subdir => 'monitor' },
629 { subdir => 'agent' },
630 { subdir => 'snapshot' },
631 { subdir => 'spiceproxy' },
632 { subdir => 'sendkey' },
633 { subdir => 'firewall' },
634 ];
635
636 return $res;
637 }});
638
639 __PACKAGE__->register_method ({
640 subclass => "PVE::API2::Firewall::VM",
641 path => '{vmid}/firewall',
642 });
643
644 __PACKAGE__->register_method({
645 name => 'rrd',
646 path => '{vmid}/rrd',
647 method => 'GET',
648 protected => 1, # fixme: can we avoid that?
649 permissions => {
650 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
651 },
652 description => "Read VM RRD statistics (returns PNG)",
653 parameters => {
654 additionalProperties => 0,
655 properties => {
656 node => get_standard_option('pve-node'),
657 vmid => get_standard_option('pve-vmid'),
658 timeframe => {
659 description => "Specify the time frame you are interested in.",
660 type => 'string',
661 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
662 },
663 ds => {
664 description => "The list of datasources you want to display.",
665 type => 'string', format => 'pve-configid-list',
666 },
667 cf => {
668 description => "The RRD consolidation function",
669 type => 'string',
670 enum => [ 'AVERAGE', 'MAX' ],
671 optional => 1,
672 },
673 },
674 },
675 returns => {
676 type => "object",
677 properties => {
678 filename => { type => 'string' },
679 },
680 },
681 code => sub {
682 my ($param) = @_;
683
684 return PVE::Cluster::create_rrd_graph(
685 "pve2-vm/$param->{vmid}", $param->{timeframe},
686 $param->{ds}, $param->{cf});
687
688 }});
689
690 __PACKAGE__->register_method({
691 name => 'rrddata',
692 path => '{vmid}/rrddata',
693 method => 'GET',
694 protected => 1, # fixme: can we avoid that?
695 permissions => {
696 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
697 },
698 description => "Read VM RRD statistics",
699 parameters => {
700 additionalProperties => 0,
701 properties => {
702 node => get_standard_option('pve-node'),
703 vmid => get_standard_option('pve-vmid'),
704 timeframe => {
705 description => "Specify the time frame you are interested in.",
706 type => 'string',
707 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
708 },
709 cf => {
710 description => "The RRD consolidation function",
711 type => 'string',
712 enum => [ 'AVERAGE', 'MAX' ],
713 optional => 1,
714 },
715 },
716 },
717 returns => {
718 type => "array",
719 items => {
720 type => "object",
721 properties => {},
722 },
723 },
724 code => sub {
725 my ($param) = @_;
726
727 return PVE::Cluster::create_rrd_data(
728 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
729 }});
730
731
732 __PACKAGE__->register_method({
733 name => 'vm_config',
734 path => '{vmid}/config',
735 method => 'GET',
736 proxyto => 'node',
737 description => "Get current virtual machine configuration. This does not include pending configuration changes (see 'pending' API).",
738 permissions => {
739 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
740 },
741 parameters => {
742 additionalProperties => 0,
743 properties => {
744 node => get_standard_option('pve-node'),
745 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
746 current => {
747 description => "Get current values (instead of pending values).",
748 optional => 1,
749 default => 0,
750 type => 'boolean',
751 },
752 },
753 },
754 returns => {
755 type => "object",
756 properties => {
757 digest => {
758 type => 'string',
759 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
760 }
761 },
762 },
763 code => sub {
764 my ($param) = @_;
765
766 my $conf = PVE::QemuConfig->load_config($param->{vmid});
767
768 delete $conf->{snapshots};
769
770 if (!$param->{current}) {
771 foreach my $opt (keys %{$conf->{pending}}) {
772 next if $opt eq 'delete';
773 my $value = $conf->{pending}->{$opt};
774 next if ref($value); # just to be sure
775 $conf->{$opt} = $value;
776 }
777 my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete});
778 foreach my $opt (keys %$pending_delete_hash) {
779 delete $conf->{$opt} if $conf->{$opt};
780 }
781 }
782
783 delete $conf->{pending};
784
785 return $conf;
786 }});
787
788 __PACKAGE__->register_method({
789 name => 'vm_pending',
790 path => '{vmid}/pending',
791 method => 'GET',
792 proxyto => 'node',
793 description => "Get virtual machine configuration, including pending changes.",
794 permissions => {
795 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
796 },
797 parameters => {
798 additionalProperties => 0,
799 properties => {
800 node => get_standard_option('pve-node'),
801 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
802 },
803 },
804 returns => {
805 type => "array",
806 items => {
807 type => "object",
808 properties => {
809 key => {
810 description => "Configuration option name.",
811 type => 'string',
812 },
813 value => {
814 description => "Current value.",
815 type => 'string',
816 optional => 1,
817 },
818 pending => {
819 description => "Pending value.",
820 type => 'string',
821 optional => 1,
822 },
823 delete => {
824 description => "Indicates a pending delete request if present and not 0. " .
825 "The value 2 indicates a force-delete request.",
826 type => 'integer',
827 minimum => 0,
828 maximum => 2,
829 optional => 1,
830 },
831 },
832 },
833 },
834 code => sub {
835 my ($param) = @_;
836
837 my $conf = PVE::QemuConfig->load_config($param->{vmid});
838
839 my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete});
840
841 my $res = [];
842
843 foreach my $opt (keys %$conf) {
844 next if ref($conf->{$opt});
845 my $item = { key => $opt };
846 $item->{value} = $conf->{$opt} if defined($conf->{$opt});
847 $item->{pending} = $conf->{pending}->{$opt} if defined($conf->{pending}->{$opt});
848 $item->{delete} = ($pending_delete_hash->{$opt} ? 2 : 1) if exists $pending_delete_hash->{$opt};
849 push @$res, $item;
850 }
851
852 foreach my $opt (keys %{$conf->{pending}}) {
853 next if $opt eq 'delete';
854 next if ref($conf->{pending}->{$opt}); # just to be sure
855 next if defined($conf->{$opt});
856 my $item = { key => $opt };
857 $item->{pending} = $conf->{pending}->{$opt};
858 push @$res, $item;
859 }
860
861 while (my ($opt, $force) = each %$pending_delete_hash) {
862 next if $conf->{pending}->{$opt}; # just to be sure
863 next if $conf->{$opt};
864 my $item = { key => $opt, delete => ($force ? 2 : 1)};
865 push @$res, $item;
866 }
867
868 return $res;
869 }});
870
871 # POST/PUT {vmid}/config implementation
872 #
873 # The original API used PUT (idempotent) an we assumed that all operations
874 # are fast. But it turned out that almost any configuration change can
875 # involve hot-plug actions, or disk alloc/free. Such actions can take long
876 # time to complete and have side effects (not idempotent).
877 #
878 # The new implementation uses POST and forks a worker process. We added
879 # a new option 'background_delay'. If specified we wait up to
880 # 'background_delay' second for the worker task to complete. It returns null
881 # if the task is finished within that time, else we return the UPID.
882
883 my $update_vm_api = sub {
884 my ($param, $sync) = @_;
885
886 my $rpcenv = PVE::RPCEnvironment::get();
887
888 my $authuser = $rpcenv->get_user();
889
890 my $node = extract_param($param, 'node');
891
892 my $vmid = extract_param($param, 'vmid');
893
894 my $digest = extract_param($param, 'digest');
895
896 my $background_delay = extract_param($param, 'background_delay');
897
898 my @paramarr = (); # used for log message
899 foreach my $key (keys %$param) {
900 push @paramarr, "-$key", $param->{$key};
901 }
902
903 my $skiplock = extract_param($param, 'skiplock');
904 raise_param_exc({ skiplock => "Only root may use this option." })
905 if $skiplock && $authuser ne 'root@pam';
906
907 my $delete_str = extract_param($param, 'delete');
908
909 my $revert_str = extract_param($param, 'revert');
910
911 my $force = extract_param($param, 'force');
912
913 die "no options specified\n" if !$delete_str && !$revert_str && !scalar(keys %$param);
914
915 my $storecfg = PVE::Storage::config();
916
917 my $defaults = PVE::QemuServer::load_defaults();
918
919 &$resolve_cdrom_alias($param);
920
921 # now try to verify all parameters
922
923 my $revert = {};
924 foreach my $opt (PVE::Tools::split_list($revert_str)) {
925 if (!PVE::QemuServer::option_exists($opt)) {
926 raise_param_exc({ revert => "unknown option '$opt'" });
927 }
928
929 raise_param_exc({ delete => "you can't use '-$opt' and " .
930 "-revert $opt' at the same time" })
931 if defined($param->{$opt});
932
933 $revert->{$opt} = 1;
934 }
935
936 my @delete = ();
937 foreach my $opt (PVE::Tools::split_list($delete_str)) {
938 $opt = 'ide2' if $opt eq 'cdrom';
939
940 raise_param_exc({ delete => "you can't use '-$opt' and " .
941 "-delete $opt' at the same time" })
942 if defined($param->{$opt});
943
944 raise_param_exc({ revert => "you can't use '-delete $opt' and " .
945 "-revert $opt' at the same time" })
946 if $revert->{$opt};
947
948 if (!PVE::QemuServer::option_exists($opt)) {
949 raise_param_exc({ delete => "unknown option '$opt'" });
950 }
951
952 push @delete, $opt;
953 }
954
955 foreach my $opt (keys %$param) {
956 if (PVE::QemuServer::is_valid_drivename($opt)) {
957 # cleanup drive path
958 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
959 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
960 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
961 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
962 } elsif ($opt =~ m/^net(\d+)$/) {
963 # add macaddr
964 my $net = PVE::QemuServer::parse_net($param->{$opt});
965 $param->{$opt} = PVE::QemuServer::print_net($net);
966 }
967 }
968
969 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
970
971 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
972
973 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
974
975 my $updatefn = sub {
976
977 my $conf = PVE::QemuConfig->load_config($vmid);
978
979 die "checksum missmatch (file change by other user?)\n"
980 if $digest && $digest ne $conf->{digest};
981
982 PVE::QemuConfig->check_lock($conf) if !$skiplock;
983
984 foreach my $opt (keys %$revert) {
985 if (defined($conf->{$opt})) {
986 $param->{$opt} = $conf->{$opt};
987 } elsif (defined($conf->{pending}->{$opt})) {
988 push @delete, $opt;
989 }
990 }
991
992 if ($param->{memory} || defined($param->{balloon})) {
993 my $maxmem = $param->{memory} || $conf->{pending}->{memory} || $conf->{memory} || $defaults->{memory};
994 my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{pending}->{balloon} || $conf->{balloon};
995
996 die "balloon value too large (must be smaller than assigned memory)\n"
997 if $balloon && $balloon > $maxmem;
998 }
999
1000 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
1001
1002 my $worker = sub {
1003
1004 print "update VM $vmid: " . join (' ', @paramarr) . "\n";
1005
1006 # write updates to pending section
1007
1008 my $modified = {}; # record what $option we modify
1009
1010 foreach my $opt (@delete) {
1011 $modified->{$opt} = 1;
1012 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1013 if (!defined($conf->{$opt})) {
1014 warn "cannot delete '$opt' - not set in current configuration!\n";
1015 $modified->{$opt} = 0;
1016 next;
1017 }
1018
1019 if ($opt =~ m/^unused/) {
1020 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
1021 PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'");
1022 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
1023 if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) {
1024 delete $conf->{$opt};
1025 PVE::QemuConfig->write_config($vmid, $conf);
1026 }
1027 } elsif (PVE::QemuServer::is_valid_drivename($opt)) {
1028 PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'");
1029 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
1030 PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
1031 if defined($conf->{pending}->{$opt});
1032 PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
1033 PVE::QemuConfig->write_config($vmid, $conf);
1034 } else {
1035 PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
1036 PVE::QemuConfig->write_config($vmid, $conf);
1037 }
1038 }
1039
1040 foreach my $opt (keys %$param) { # add/change
1041 $modified->{$opt} = 1;
1042 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1043 next if defined($conf->{pending}->{$opt}) && ($param->{$opt} eq $conf->{pending}->{$opt}); # skip if nothing changed
1044
1045 if (PVE::QemuServer::is_valid_drivename($opt)) {
1046 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
1047 if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM
1048 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
1049 } else {
1050 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
1051 }
1052 PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
1053 if defined($conf->{pending}->{$opt});
1054
1055 &$create_disks($rpcenv, $authuser, $conf->{pending}, $storecfg, $vmid, undef, {$opt => $param->{$opt}});
1056 } elsif ($opt eq "replicate") {
1057 # check if all volumes have replicate feature
1058 PVE::QemuConfig->get_replicatable_volumes($storecfg, $conf);
1059 my $repl = PVE::JSONSchema::check_format('pve-replicate', $param->{opt});
1060 PVE::Cluster::check_node_exists($repl->{target});
1061 $conf->{$opt} = $param->{$opt};
1062 } else {
1063 $conf->{pending}->{$opt} = $param->{$opt};
1064 }
1065 PVE::QemuServer::vmconfig_undelete_pending_option($conf, $opt);
1066 PVE::QemuConfig->write_config($vmid, $conf);
1067 }
1068
1069 # remove pending changes when nothing changed
1070 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1071 my $changes = PVE::QemuServer::vmconfig_cleanup_pending($conf);
1072 PVE::QemuConfig->write_config($vmid, $conf) if $changes;
1073
1074 return if !scalar(keys %{$conf->{pending}});
1075
1076 my $running = PVE::QemuServer::check_running($vmid);
1077
1078 # apply pending changes
1079
1080 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1081
1082 if ($running) {
1083 my $errors = {};
1084 PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors);
1085 raise_param_exc($errors) if scalar(keys %$errors);
1086 } else {
1087 PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $running);
1088 }
1089
1090 return;
1091 };
1092
1093 if ($sync) {
1094 &$worker();
1095 return undef;
1096 } else {
1097 my $upid = $rpcenv->fork_worker('qmconfig', $vmid, $authuser, $worker);
1098
1099 if ($background_delay) {
1100
1101 # Note: It would be better to do that in the Event based HTTPServer
1102 # to avoid blocking call to sleep.
1103
1104 my $end_time = time() + $background_delay;
1105
1106 my $task = PVE::Tools::upid_decode($upid);
1107
1108 my $running = 1;
1109 while (time() < $end_time) {
1110 $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart});
1111 last if !$running;
1112 sleep(1); # this gets interrupted when child process ends
1113 }
1114
1115 if (!$running) {
1116 my $status = PVE::Tools::upid_read_status($upid);
1117 return undef if $status eq 'OK';
1118 die $status;
1119 }
1120 }
1121
1122 return $upid;
1123 }
1124 };
1125
1126 return PVE::QemuConfig->lock_config($vmid, $updatefn);
1127 };
1128
1129 my $vm_config_perm_list = [
1130 'VM.Config.Disk',
1131 'VM.Config.CDROM',
1132 'VM.Config.CPU',
1133 'VM.Config.Memory',
1134 'VM.Config.Network',
1135 'VM.Config.HWType',
1136 'VM.Config.Options',
1137 ];
1138
1139 __PACKAGE__->register_method({
1140 name => 'update_vm_async',
1141 path => '{vmid}/config',
1142 method => 'POST',
1143 protected => 1,
1144 proxyto => 'node',
1145 description => "Set virtual machine options (asynchrounous API).",
1146 permissions => {
1147 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
1148 },
1149 parameters => {
1150 additionalProperties => 0,
1151 properties => PVE::QemuServer::json_config_properties(
1152 {
1153 node => get_standard_option('pve-node'),
1154 vmid => get_standard_option('pve-vmid'),
1155 skiplock => get_standard_option('skiplock'),
1156 delete => {
1157 type => 'string', format => 'pve-configid-list',
1158 description => "A list of settings you want to delete.",
1159 optional => 1,
1160 },
1161 revert => {
1162 type => 'string', format => 'pve-configid-list',
1163 description => "Revert a pending change.",
1164 optional => 1,
1165 },
1166 force => {
1167 type => 'boolean',
1168 description => $opt_force_description,
1169 optional => 1,
1170 requires => 'delete',
1171 },
1172 digest => {
1173 type => 'string',
1174 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1175 maxLength => 40,
1176 optional => 1,
1177 },
1178 background_delay => {
1179 type => 'integer',
1180 description => "Time to wait for the task to finish. We return 'null' if the task finish within that time.",
1181 minimum => 1,
1182 maximum => 30,
1183 optional => 1,
1184 },
1185 }),
1186 },
1187 returns => {
1188 type => 'string',
1189 optional => 1,
1190 },
1191 code => $update_vm_api,
1192 });
1193
1194 __PACKAGE__->register_method({
1195 name => 'update_vm',
1196 path => '{vmid}/config',
1197 method => 'PUT',
1198 protected => 1,
1199 proxyto => 'node',
1200 description => "Set virtual machine options (synchrounous API) - You should consider using the POST method instead for any actions involving hotplug or storage allocation.",
1201 permissions => {
1202 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
1203 },
1204 parameters => {
1205 additionalProperties => 0,
1206 properties => PVE::QemuServer::json_config_properties(
1207 {
1208 node => get_standard_option('pve-node'),
1209 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
1210 skiplock => get_standard_option('skiplock'),
1211 delete => {
1212 type => 'string', format => 'pve-configid-list',
1213 description => "A list of settings you want to delete.",
1214 optional => 1,
1215 },
1216 revert => {
1217 type => 'string', format => 'pve-configid-list',
1218 description => "Revert a pending change.",
1219 optional => 1,
1220 },
1221 force => {
1222 type => 'boolean',
1223 description => $opt_force_description,
1224 optional => 1,
1225 requires => 'delete',
1226 },
1227 digest => {
1228 type => 'string',
1229 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1230 maxLength => 40,
1231 optional => 1,
1232 },
1233 }),
1234 },
1235 returns => { type => 'null' },
1236 code => sub {
1237 my ($param) = @_;
1238 &$update_vm_api($param, 1);
1239 return undef;
1240 }
1241 });
1242
1243
1244 __PACKAGE__->register_method({
1245 name => 'destroy_vm',
1246 path => '{vmid}',
1247 method => 'DELETE',
1248 protected => 1,
1249 proxyto => 'node',
1250 description => "Destroy the vm (also delete all used/owned volumes).",
1251 permissions => {
1252 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
1253 },
1254 parameters => {
1255 additionalProperties => 0,
1256 properties => {
1257 node => get_standard_option('pve-node'),
1258 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }),
1259 skiplock => get_standard_option('skiplock'),
1260 },
1261 },
1262 returns => {
1263 type => 'string',
1264 },
1265 code => sub {
1266 my ($param) = @_;
1267
1268 my $rpcenv = PVE::RPCEnvironment::get();
1269
1270 my $authuser = $rpcenv->get_user();
1271
1272 my $vmid = $param->{vmid};
1273
1274 my $skiplock = $param->{skiplock};
1275 raise_param_exc({ skiplock => "Only root may use this option." })
1276 if $skiplock && $authuser ne 'root@pam';
1277
1278 # test if VM exists
1279 my $conf = PVE::QemuConfig->load_config($vmid);
1280
1281 my $storecfg = PVE::Storage::config();
1282
1283 PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid");
1284
1285 die "unable to remove VM $vmid - used in HA resources\n"
1286 if PVE::HA::Config::vm_is_ha_managed($vmid);
1287
1288 # early tests (repeat after locking)
1289 die "VM $vmid is running - destroy failed\n"
1290 if PVE::QemuServer::check_running($vmid);
1291
1292 my $realcmd = sub {
1293 my $upid = shift;
1294
1295 syslog('info', "destroy VM $vmid: $upid\n");
1296
1297 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
1298
1299 PVE::AccessControl::remove_vm_access($vmid);
1300
1301 PVE::Firewall::remove_vmfw_conf($vmid);
1302 };
1303
1304 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
1305 }});
1306
1307 __PACKAGE__->register_method({
1308 name => 'unlink',
1309 path => '{vmid}/unlink',
1310 method => 'PUT',
1311 protected => 1,
1312 proxyto => 'node',
1313 description => "Unlink/delete disk images.",
1314 permissions => {
1315 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
1316 },
1317 parameters => {
1318 additionalProperties => 0,
1319 properties => {
1320 node => get_standard_option('pve-node'),
1321 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
1322 idlist => {
1323 type => 'string', format => 'pve-configid-list',
1324 description => "A list of disk IDs you want to delete.",
1325 },
1326 force => {
1327 type => 'boolean',
1328 description => $opt_force_description,
1329 optional => 1,
1330 },
1331 },
1332 },
1333 returns => { type => 'null'},
1334 code => sub {
1335 my ($param) = @_;
1336
1337 $param->{delete} = extract_param($param, 'idlist');
1338
1339 __PACKAGE__->update_vm($param);
1340
1341 return undef;
1342 }});
1343
1344 my $sslcert;
1345
1346 __PACKAGE__->register_method({
1347 name => 'vncproxy',
1348 path => '{vmid}/vncproxy',
1349 method => 'POST',
1350 protected => 1,
1351 permissions => {
1352 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1353 },
1354 description => "Creates a TCP VNC proxy connections.",
1355 parameters => {
1356 additionalProperties => 0,
1357 properties => {
1358 node => get_standard_option('pve-node'),
1359 vmid => get_standard_option('pve-vmid'),
1360 websocket => {
1361 optional => 1,
1362 type => 'boolean',
1363 description => "starts websockify instead of vncproxy",
1364 },
1365 },
1366 },
1367 returns => {
1368 additionalProperties => 0,
1369 properties => {
1370 user => { type => 'string' },
1371 ticket => { type => 'string' },
1372 cert => { type => 'string' },
1373 port => { type => 'integer' },
1374 upid => { type => 'string' },
1375 },
1376 },
1377 code => sub {
1378 my ($param) = @_;
1379
1380 my $rpcenv = PVE::RPCEnvironment::get();
1381
1382 my $authuser = $rpcenv->get_user();
1383
1384 my $vmid = $param->{vmid};
1385 my $node = $param->{node};
1386 my $websocket = $param->{websocket};
1387
1388 my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists
1389
1390 my $authpath = "/vms/$vmid";
1391
1392 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1393
1394 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
1395 if !$sslcert;
1396
1397 my ($remip, $family);
1398 my $remcmd = [];
1399
1400 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1401 ($remip, $family) = PVE::Cluster::remote_node_ip($node);
1402 # NOTE: kvm VNC traffic is already TLS encrypted or is known unsecure
1403 $remcmd = ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', $remip];
1404 } else {
1405 $family = PVE::Tools::get_host_address_family($node);
1406 }
1407
1408 my $port = PVE::Tools::next_vnc_port($family);
1409
1410 my $timeout = 10;
1411
1412 my $realcmd = sub {
1413 my $upid = shift;
1414
1415 syslog('info', "starting vnc proxy $upid\n");
1416
1417 my $cmd;
1418
1419 if ($conf->{vga} && ($conf->{vga} =~ m/^serial\d+$/)) {
1420
1421 die "Websocket mode is not supported in vga serial mode!" if $websocket;
1422
1423 my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-iface', $conf->{vga} ];
1424 #my $termcmd = "/usr/bin/qm terminal -iface $conf->{vga}";
1425 $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
1426 '-timeout', $timeout, '-authpath', $authpath,
1427 '-perm', 'Sys.Console', '-c', @$remcmd, @$termcmd];
1428 PVE::Tools::run_command($cmd);
1429 } else {
1430
1431 $ENV{LC_PVE_TICKET} = $ticket if $websocket; # set ticket with "qm vncproxy"
1432
1433 $cmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1434
1435 my $sock = IO::Socket::IP->new(
1436 Listen => 1,
1437 LocalPort => $port,
1438 Proto => 'tcp',
1439 GetAddrInfoFlags => 0,
1440 ) or die "failed to create socket: $!\n";
1441 # Inside the worker we shouldn't have any previous alarms
1442 # running anyway...:
1443 alarm(0);
1444 local $SIG{ALRM} = sub { die "connection timed out\n" };
1445 alarm $timeout;
1446 accept(my $cli, $sock) or die "connection failed: $!\n";
1447 alarm(0);
1448 close($sock);
1449 if (PVE::Tools::run_command($cmd,
1450 output => '>&'.fileno($cli),
1451 input => '<&'.fileno($cli),
1452 noerr => 1) != 0)
1453 {
1454 die "Failed to run vncproxy.\n";
1455 }
1456 }
1457
1458 return;
1459 };
1460
1461 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1);
1462
1463 PVE::Tools::wait_for_vnc_port($port);
1464
1465 return {
1466 user => $authuser,
1467 ticket => $ticket,
1468 port => $port,
1469 upid => $upid,
1470 cert => $sslcert,
1471 };
1472 }});
1473
1474 __PACKAGE__->register_method({
1475 name => 'vncwebsocket',
1476 path => '{vmid}/vncwebsocket',
1477 method => 'GET',
1478 permissions => {
1479 description => "You also need to pass a valid ticket (vncticket).",
1480 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1481 },
1482 description => "Opens a weksocket for VNC traffic.",
1483 parameters => {
1484 additionalProperties => 0,
1485 properties => {
1486 node => get_standard_option('pve-node'),
1487 vmid => get_standard_option('pve-vmid'),
1488 vncticket => {
1489 description => "Ticket from previous call to vncproxy.",
1490 type => 'string',
1491 maxLength => 512,
1492 },
1493 port => {
1494 description => "Port number returned by previous vncproxy call.",
1495 type => 'integer',
1496 minimum => 5900,
1497 maximum => 5999,
1498 },
1499 },
1500 },
1501 returns => {
1502 type => "object",
1503 properties => {
1504 port => { type => 'string' },
1505 },
1506 },
1507 code => sub {
1508 my ($param) = @_;
1509
1510 my $rpcenv = PVE::RPCEnvironment::get();
1511
1512 my $authuser = $rpcenv->get_user();
1513
1514 my $vmid = $param->{vmid};
1515 my $node = $param->{node};
1516
1517 my $authpath = "/vms/$vmid";
1518
1519 PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath);
1520
1521 my $conf = PVE::QemuConfig->load_config($vmid, $node); # VM exists ?
1522
1523 # Note: VNC ports are acessible from outside, so we do not gain any
1524 # security if we verify that $param->{port} belongs to VM $vmid. This
1525 # check is done by verifying the VNC ticket (inside VNC protocol).
1526
1527 my $port = $param->{port};
1528
1529 return { port => $port };
1530 }});
1531
1532 __PACKAGE__->register_method({
1533 name => 'spiceproxy',
1534 path => '{vmid}/spiceproxy',
1535 method => 'POST',
1536 protected => 1,
1537 proxyto => 'node',
1538 permissions => {
1539 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1540 },
1541 description => "Returns a SPICE configuration to connect to the VM.",
1542 parameters => {
1543 additionalProperties => 0,
1544 properties => {
1545 node => get_standard_option('pve-node'),
1546 vmid => get_standard_option('pve-vmid'),
1547 proxy => get_standard_option('spice-proxy', { optional => 1 }),
1548 },
1549 },
1550 returns => get_standard_option('remote-viewer-config'),
1551 code => sub {
1552 my ($param) = @_;
1553
1554 my $rpcenv = PVE::RPCEnvironment::get();
1555
1556 my $authuser = $rpcenv->get_user();
1557
1558 my $vmid = $param->{vmid};
1559 my $node = $param->{node};
1560 my $proxy = $param->{proxy};
1561
1562 my $conf = PVE::QemuConfig->load_config($vmid, $node);
1563 my $title = "VM $vmid";
1564 $title .= " - ". $conf->{name} if $conf->{name};
1565
1566 my $port = PVE::QemuServer::spice_port($vmid);
1567
1568 my ($ticket, undef, $remote_viewer_config) =
1569 PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port);
1570
1571 PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket);
1572 PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30");
1573
1574 return $remote_viewer_config;
1575 }});
1576
1577 __PACKAGE__->register_method({
1578 name => 'vmcmdidx',
1579 path => '{vmid}/status',
1580 method => 'GET',
1581 proxyto => 'node',
1582 description => "Directory index",
1583 permissions => {
1584 user => 'all',
1585 },
1586 parameters => {
1587 additionalProperties => 0,
1588 properties => {
1589 node => get_standard_option('pve-node'),
1590 vmid => get_standard_option('pve-vmid'),
1591 },
1592 },
1593 returns => {
1594 type => 'array',
1595 items => {
1596 type => "object",
1597 properties => {
1598 subdir => { type => 'string' },
1599 },
1600 },
1601 links => [ { rel => 'child', href => "{subdir}" } ],
1602 },
1603 code => sub {
1604 my ($param) = @_;
1605
1606 # test if VM exists
1607 my $conf = PVE::QemuConfig->load_config($param->{vmid});
1608
1609 my $res = [
1610 { subdir => 'current' },
1611 { subdir => 'start' },
1612 { subdir => 'stop' },
1613 ];
1614
1615 return $res;
1616 }});
1617
1618 __PACKAGE__->register_method({
1619 name => 'vm_status',
1620 path => '{vmid}/status/current',
1621 method => 'GET',
1622 proxyto => 'node',
1623 protected => 1, # qemu pid files are only readable by root
1624 description => "Get virtual machine status.",
1625 permissions => {
1626 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1627 },
1628 parameters => {
1629 additionalProperties => 0,
1630 properties => {
1631 node => get_standard_option('pve-node'),
1632 vmid => get_standard_option('pve-vmid'),
1633 },
1634 },
1635 returns => { type => 'object' },
1636 code => sub {
1637 my ($param) = @_;
1638
1639 # test if VM exists
1640 my $conf = PVE::QemuConfig->load_config($param->{vmid});
1641
1642 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
1643 my $status = $vmstatus->{$param->{vmid}};
1644
1645 $status->{ha} = PVE::HA::Config::get_service_status("vm:$param->{vmid}");
1646
1647 $status->{spice} = 1 if PVE::QemuServer::vga_conf_has_spice($conf->{vga});
1648
1649 return $status;
1650 }});
1651
1652 __PACKAGE__->register_method({
1653 name => 'vm_start',
1654 path => '{vmid}/status/start',
1655 method => 'POST',
1656 protected => 1,
1657 proxyto => 'node',
1658 description => "Start virtual machine.",
1659 permissions => {
1660 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1661 },
1662 parameters => {
1663 additionalProperties => 0,
1664 properties => {
1665 node => get_standard_option('pve-node'),
1666 vmid => get_standard_option('pve-vmid',
1667 { completion => \&PVE::QemuServer::complete_vmid_stopped }),
1668 skiplock => get_standard_option('skiplock'),
1669 stateuri => get_standard_option('pve-qm-stateuri'),
1670 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
1671 migration_type => {
1672 type => 'string',
1673 enum => ['secure', 'insecure'],
1674 description => "Migration traffic is encrypted using an SSH " .
1675 "tunnel by default. On secure, completely private networks " .
1676 "this can be disabled to increase performance.",
1677 optional => 1,
1678 },
1679 migration_network => {
1680 type => 'string', format => 'CIDR',
1681 description => "CIDR of the (sub) network that is used for migration.",
1682 optional => 1,
1683 },
1684 machine => get_standard_option('pve-qm-machine'),
1685 targetstorage => {
1686 description => "Target storage for the migration. (Can be '1' to use the same storage id as on the source node.)",
1687 type => 'string',
1688 optional => 1
1689 }
1690 },
1691 },
1692 returns => {
1693 type => 'string',
1694 },
1695 code => sub {
1696 my ($param) = @_;
1697
1698 my $rpcenv = PVE::RPCEnvironment::get();
1699
1700 my $authuser = $rpcenv->get_user();
1701
1702 my $node = extract_param($param, 'node');
1703
1704 my $vmid = extract_param($param, 'vmid');
1705
1706 my $machine = extract_param($param, 'machine');
1707
1708 my $stateuri = extract_param($param, 'stateuri');
1709 raise_param_exc({ stateuri => "Only root may use this option." })
1710 if $stateuri && $authuser ne 'root@pam';
1711
1712 my $skiplock = extract_param($param, 'skiplock');
1713 raise_param_exc({ skiplock => "Only root may use this option." })
1714 if $skiplock && $authuser ne 'root@pam';
1715
1716 my $migratedfrom = extract_param($param, 'migratedfrom');
1717 raise_param_exc({ migratedfrom => "Only root may use this option." })
1718 if $migratedfrom && $authuser ne 'root@pam';
1719
1720 my $migration_type = extract_param($param, 'migration_type');
1721 raise_param_exc({ migration_type => "Only root may use this option." })
1722 if $migration_type && $authuser ne 'root@pam';
1723
1724 my $migration_network = extract_param($param, 'migration_network');
1725 raise_param_exc({ migration_network => "Only root may use this option." })
1726 if $migration_network && $authuser ne 'root@pam';
1727
1728 my $targetstorage = extract_param($param, 'targetstorage');
1729 raise_param_exc({ targetstorage => "Only root may use this option." })
1730 if $targetstorage && $authuser ne 'root@pam';
1731
1732 raise_param_exc({ targetstorage => "targetstorage can only by used with migratedfrom." })
1733 if $targetstorage && !$migratedfrom;
1734
1735 # read spice ticket from STDIN
1736 my $spice_ticket;
1737 if ($stateuri && ($stateuri eq 'tcp') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
1738 if (defined(my $line = <>)) {
1739 chomp $line;
1740 $spice_ticket = $line;
1741 }
1742 }
1743
1744 PVE::Cluster::check_cfs_quorum();
1745
1746 my $storecfg = PVE::Storage::config();
1747
1748 if (PVE::HA::Config::vm_is_ha_managed($vmid) && !$stateuri &&
1749 $rpcenv->{type} ne 'ha') {
1750
1751 my $hacmd = sub {
1752 my $upid = shift;
1753
1754 my $service = "vm:$vmid";
1755
1756 my $cmd = ['ha-manager', 'set', $service, '--state', 'started'];
1757
1758 print "Executing HA start for VM $vmid\n";
1759
1760 PVE::Tools::run_command($cmd);
1761
1762 return;
1763 };
1764
1765 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1766
1767 } else {
1768
1769 my $realcmd = sub {
1770 my $upid = shift;
1771
1772 syslog('info', "start VM $vmid: $upid\n");
1773
1774 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom, undef,
1775 $machine, $spice_ticket, $migration_network, $migration_type, $targetstorage);
1776
1777 return;
1778 };
1779
1780 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1781 }
1782 }});
1783
1784 __PACKAGE__->register_method({
1785 name => 'vm_stop',
1786 path => '{vmid}/status/stop',
1787 method => 'POST',
1788 protected => 1,
1789 proxyto => 'node',
1790 description => "Stop virtual machine. The qemu process will exit immediately. This" .
1791 "is akin to pulling the power plug of a running computer and may damage the VM data",
1792 permissions => {
1793 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1794 },
1795 parameters => {
1796 additionalProperties => 0,
1797 properties => {
1798 node => get_standard_option('pve-node'),
1799 vmid => get_standard_option('pve-vmid',
1800 { completion => \&PVE::QemuServer::complete_vmid_running }),
1801 skiplock => get_standard_option('skiplock'),
1802 migratedfrom => get_standard_option('pve-node', { optional => 1 }),
1803 timeout => {
1804 description => "Wait maximal timeout seconds.",
1805 type => 'integer',
1806 minimum => 0,
1807 optional => 1,
1808 },
1809 keepActive => {
1810 description => "Do not deactivate storage volumes.",
1811 type => 'boolean',
1812 optional => 1,
1813 default => 0,
1814 }
1815 },
1816 },
1817 returns => {
1818 type => 'string',
1819 },
1820 code => sub {
1821 my ($param) = @_;
1822
1823 my $rpcenv = PVE::RPCEnvironment::get();
1824
1825 my $authuser = $rpcenv->get_user();
1826
1827 my $node = extract_param($param, 'node');
1828
1829 my $vmid = extract_param($param, 'vmid');
1830
1831 my $skiplock = extract_param($param, 'skiplock');
1832 raise_param_exc({ skiplock => "Only root may use this option." })
1833 if $skiplock && $authuser ne 'root@pam';
1834
1835 my $keepActive = extract_param($param, 'keepActive');
1836 raise_param_exc({ keepActive => "Only root may use this option." })
1837 if $keepActive && $authuser ne 'root@pam';
1838
1839 my $migratedfrom = extract_param($param, 'migratedfrom');
1840 raise_param_exc({ migratedfrom => "Only root may use this option." })
1841 if $migratedfrom && $authuser ne 'root@pam';
1842
1843
1844 my $storecfg = PVE::Storage::config();
1845
1846 if (PVE::HA::Config::vm_is_ha_managed($vmid) && ($rpcenv->{type} ne 'ha') && !defined($migratedfrom)) {
1847
1848 my $hacmd = sub {
1849 my $upid = shift;
1850
1851 my $service = "vm:$vmid";
1852
1853 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
1854
1855 print "Executing HA stop for VM $vmid\n";
1856
1857 PVE::Tools::run_command($cmd);
1858
1859 return;
1860 };
1861
1862 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1863
1864 } else {
1865 my $realcmd = sub {
1866 my $upid = shift;
1867
1868 syslog('info', "stop VM $vmid: $upid\n");
1869
1870 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1871 $param->{timeout}, 0, 1, $keepActive, $migratedfrom);
1872
1873 return;
1874 };
1875
1876 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1877 }
1878 }});
1879
1880 __PACKAGE__->register_method({
1881 name => 'vm_reset',
1882 path => '{vmid}/status/reset',
1883 method => 'POST',
1884 protected => 1,
1885 proxyto => 'node',
1886 description => "Reset virtual machine.",
1887 permissions => {
1888 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1889 },
1890 parameters => {
1891 additionalProperties => 0,
1892 properties => {
1893 node => get_standard_option('pve-node'),
1894 vmid => get_standard_option('pve-vmid',
1895 { completion => \&PVE::QemuServer::complete_vmid_running }),
1896 skiplock => get_standard_option('skiplock'),
1897 },
1898 },
1899 returns => {
1900 type => 'string',
1901 },
1902 code => sub {
1903 my ($param) = @_;
1904
1905 my $rpcenv = PVE::RPCEnvironment::get();
1906
1907 my $authuser = $rpcenv->get_user();
1908
1909 my $node = extract_param($param, 'node');
1910
1911 my $vmid = extract_param($param, 'vmid');
1912
1913 my $skiplock = extract_param($param, 'skiplock');
1914 raise_param_exc({ skiplock => "Only root may use this option." })
1915 if $skiplock && $authuser ne 'root@pam';
1916
1917 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1918
1919 my $realcmd = sub {
1920 my $upid = shift;
1921
1922 PVE::QemuServer::vm_reset($vmid, $skiplock);
1923
1924 return;
1925 };
1926
1927 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1928 }});
1929
1930 __PACKAGE__->register_method({
1931 name => 'vm_shutdown',
1932 path => '{vmid}/status/shutdown',
1933 method => 'POST',
1934 protected => 1,
1935 proxyto => 'node',
1936 description => "Shutdown virtual machine. This is similar to pressing the power button on a physical machine." .
1937 "This will send an ACPI event for the guest OS, which should then proceed to a clean shutdown.",
1938 permissions => {
1939 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1940 },
1941 parameters => {
1942 additionalProperties => 0,
1943 properties => {
1944 node => get_standard_option('pve-node'),
1945 vmid => get_standard_option('pve-vmid',
1946 { completion => \&PVE::QemuServer::complete_vmid_running }),
1947 skiplock => get_standard_option('skiplock'),
1948 timeout => {
1949 description => "Wait maximal timeout seconds.",
1950 type => 'integer',
1951 minimum => 0,
1952 optional => 1,
1953 },
1954 forceStop => {
1955 description => "Make sure the VM stops.",
1956 type => 'boolean',
1957 optional => 1,
1958 default => 0,
1959 },
1960 keepActive => {
1961 description => "Do not deactivate storage volumes.",
1962 type => 'boolean',
1963 optional => 1,
1964 default => 0,
1965 }
1966 },
1967 },
1968 returns => {
1969 type => 'string',
1970 },
1971 code => sub {
1972 my ($param) = @_;
1973
1974 my $rpcenv = PVE::RPCEnvironment::get();
1975
1976 my $authuser = $rpcenv->get_user();
1977
1978 my $node = extract_param($param, 'node');
1979
1980 my $vmid = extract_param($param, 'vmid');
1981
1982 my $skiplock = extract_param($param, 'skiplock');
1983 raise_param_exc({ skiplock => "Only root may use this option." })
1984 if $skiplock && $authuser ne 'root@pam';
1985
1986 my $keepActive = extract_param($param, 'keepActive');
1987 raise_param_exc({ keepActive => "Only root may use this option." })
1988 if $keepActive && $authuser ne 'root@pam';
1989
1990 my $storecfg = PVE::Storage::config();
1991
1992 my $shutdown = 1;
1993
1994 # if vm is paused, do not shutdown (but stop if forceStop = 1)
1995 # otherwise, we will infer a shutdown command, but run into the timeout,
1996 # then when the vm is resumed, it will instantly shutdown
1997 #
1998 # checking the qmp status here to get feedback to the gui/cli/api
1999 # and the status query should not take too long
2000 my $qmpstatus;
2001 eval {
2002 $qmpstatus = PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0);
2003 };
2004 my $err = $@ if $@;
2005
2006 if (!$err && $qmpstatus->{status} eq "paused") {
2007 if ($param->{forceStop}) {
2008 warn "VM is paused - stop instead of shutdown\n";
2009 $shutdown = 0;
2010 } else {
2011 die "VM is paused - cannot shutdown\n";
2012 }
2013 }
2014
2015 if (PVE::HA::Config::vm_is_ha_managed($vmid) &&
2016 ($rpcenv->{type} ne 'ha')) {
2017
2018 my $hacmd = sub {
2019 my $upid = shift;
2020
2021 my $service = "vm:$vmid";
2022
2023 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
2024
2025 print "Executing HA stop for VM $vmid\n";
2026
2027 PVE::Tools::run_command($cmd);
2028
2029 return;
2030 };
2031
2032 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
2033
2034 } else {
2035
2036 my $realcmd = sub {
2037 my $upid = shift;
2038
2039 syslog('info', "shutdown VM $vmid: $upid\n");
2040
2041 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
2042 $shutdown, $param->{forceStop}, $keepActive);
2043
2044 return;
2045 };
2046
2047 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
2048 }
2049 }});
2050
2051 __PACKAGE__->register_method({
2052 name => 'vm_suspend',
2053 path => '{vmid}/status/suspend',
2054 method => 'POST',
2055 protected => 1,
2056 proxyto => 'node',
2057 description => "Suspend virtual machine.",
2058 permissions => {
2059 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2060 },
2061 parameters => {
2062 additionalProperties => 0,
2063 properties => {
2064 node => get_standard_option('pve-node'),
2065 vmid => get_standard_option('pve-vmid',
2066 { completion => \&PVE::QemuServer::complete_vmid_running }),
2067 skiplock => get_standard_option('skiplock'),
2068 },
2069 },
2070 returns => {
2071 type => 'string',
2072 },
2073 code => sub {
2074 my ($param) = @_;
2075
2076 my $rpcenv = PVE::RPCEnvironment::get();
2077
2078 my $authuser = $rpcenv->get_user();
2079
2080 my $node = extract_param($param, 'node');
2081
2082 my $vmid = extract_param($param, 'vmid');
2083
2084 my $skiplock = extract_param($param, 'skiplock');
2085 raise_param_exc({ skiplock => "Only root may use this option." })
2086 if $skiplock && $authuser ne 'root@pam';
2087
2088 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
2089
2090 my $realcmd = sub {
2091 my $upid = shift;
2092
2093 syslog('info', "suspend VM $vmid: $upid\n");
2094
2095 PVE::QemuServer::vm_suspend($vmid, $skiplock);
2096
2097 return;
2098 };
2099
2100 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
2101 }});
2102
2103 __PACKAGE__->register_method({
2104 name => 'vm_resume',
2105 path => '{vmid}/status/resume',
2106 method => 'POST',
2107 protected => 1,
2108 proxyto => 'node',
2109 description => "Resume virtual machine.",
2110 permissions => {
2111 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2112 },
2113 parameters => {
2114 additionalProperties => 0,
2115 properties => {
2116 node => get_standard_option('pve-node'),
2117 vmid => get_standard_option('pve-vmid',
2118 { completion => \&PVE::QemuServer::complete_vmid_running }),
2119 skiplock => get_standard_option('skiplock'),
2120 nocheck => { type => 'boolean', optional => 1 },
2121
2122 },
2123 },
2124 returns => {
2125 type => 'string',
2126 },
2127 code => sub {
2128 my ($param) = @_;
2129
2130 my $rpcenv = PVE::RPCEnvironment::get();
2131
2132 my $authuser = $rpcenv->get_user();
2133
2134 my $node = extract_param($param, 'node');
2135
2136 my $vmid = extract_param($param, 'vmid');
2137
2138 my $skiplock = extract_param($param, 'skiplock');
2139 raise_param_exc({ skiplock => "Only root may use this option." })
2140 if $skiplock && $authuser ne 'root@pam';
2141
2142 my $nocheck = extract_param($param, 'nocheck');
2143
2144 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid, $nocheck);
2145
2146 my $realcmd = sub {
2147 my $upid = shift;
2148
2149 syslog('info', "resume VM $vmid: $upid\n");
2150
2151 PVE::QemuServer::vm_resume($vmid, $skiplock, $nocheck);
2152
2153 return;
2154 };
2155
2156 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
2157 }});
2158
2159 __PACKAGE__->register_method({
2160 name => 'vm_sendkey',
2161 path => '{vmid}/sendkey',
2162 method => 'PUT',
2163 protected => 1,
2164 proxyto => 'node',
2165 description => "Send key event to virtual machine.",
2166 permissions => {
2167 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
2168 },
2169 parameters => {
2170 additionalProperties => 0,
2171 properties => {
2172 node => get_standard_option('pve-node'),
2173 vmid => get_standard_option('pve-vmid',
2174 { completion => \&PVE::QemuServer::complete_vmid_running }),
2175 skiplock => get_standard_option('skiplock'),
2176 key => {
2177 description => "The key (qemu monitor encoding).",
2178 type => 'string'
2179 }
2180 },
2181 },
2182 returns => { type => 'null'},
2183 code => sub {
2184 my ($param) = @_;
2185
2186 my $rpcenv = PVE::RPCEnvironment::get();
2187
2188 my $authuser = $rpcenv->get_user();
2189
2190 my $node = extract_param($param, 'node');
2191
2192 my $vmid = extract_param($param, 'vmid');
2193
2194 my $skiplock = extract_param($param, 'skiplock');
2195 raise_param_exc({ skiplock => "Only root may use this option." })
2196 if $skiplock && $authuser ne 'root@pam';
2197
2198 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
2199
2200 return;
2201 }});
2202
2203 __PACKAGE__->register_method({
2204 name => 'vm_feature',
2205 path => '{vmid}/feature',
2206 method => 'GET',
2207 proxyto => 'node',
2208 protected => 1,
2209 description => "Check if feature for virtual machine is available.",
2210 permissions => {
2211 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
2212 },
2213 parameters => {
2214 additionalProperties => 0,
2215 properties => {
2216 node => get_standard_option('pve-node'),
2217 vmid => get_standard_option('pve-vmid'),
2218 feature => {
2219 description => "Feature to check.",
2220 type => 'string',
2221 enum => [ 'snapshot', 'clone', 'copy' ],
2222 },
2223 snapname => get_standard_option('pve-snapshot-name', {
2224 optional => 1,
2225 }),
2226 },
2227 },
2228 returns => {
2229 type => "object",
2230 properties => {
2231 hasFeature => { type => 'boolean' },
2232 nodes => {
2233 type => 'array',
2234 items => { type => 'string' },
2235 }
2236 },
2237 },
2238 code => sub {
2239 my ($param) = @_;
2240
2241 my $node = extract_param($param, 'node');
2242
2243 my $vmid = extract_param($param, 'vmid');
2244
2245 my $snapname = extract_param($param, 'snapname');
2246
2247 my $feature = extract_param($param, 'feature');
2248
2249 my $running = PVE::QemuServer::check_running($vmid);
2250
2251 my $conf = PVE::QemuConfig->load_config($vmid);
2252
2253 if($snapname){
2254 my $snap = $conf->{snapshots}->{$snapname};
2255 die "snapshot '$snapname' does not exist\n" if !defined($snap);
2256 $conf = $snap;
2257 }
2258 my $storecfg = PVE::Storage::config();
2259
2260 my $nodelist = PVE::QemuServer::shared_nodes($conf, $storecfg);
2261 my $hasFeature = PVE::QemuConfig->has_feature($feature, $conf, $storecfg, $snapname, $running);
2262
2263 return {
2264 hasFeature => $hasFeature,
2265 nodes => [ keys %$nodelist ],
2266 };
2267 }});
2268
2269 __PACKAGE__->register_method({
2270 name => 'clone_vm',
2271 path => '{vmid}/clone',
2272 method => 'POST',
2273 protected => 1,
2274 proxyto => 'node',
2275 description => "Create a copy of virtual machine/template.",
2276 permissions => {
2277 description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " .
2278 "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " .
2279 "'Datastore.AllocateSpace' on any used storage.",
2280 check =>
2281 [ 'and',
2282 ['perm', '/vms/{vmid}', [ 'VM.Clone' ]],
2283 [ 'or',
2284 [ 'perm', '/vms/{newid}', ['VM.Allocate']],
2285 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
2286 ],
2287 ]
2288 },
2289 parameters => {
2290 additionalProperties => 0,
2291 properties => {
2292 node => get_standard_option('pve-node'),
2293 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
2294 newid => get_standard_option('pve-vmid', { description => 'VMID for the clone.' }),
2295 name => {
2296 optional => 1,
2297 type => 'string', format => 'dns-name',
2298 description => "Set a name for the new VM.",
2299 },
2300 description => {
2301 optional => 1,
2302 type => 'string',
2303 description => "Description for the new VM.",
2304 },
2305 pool => {
2306 optional => 1,
2307 type => 'string', format => 'pve-poolid',
2308 description => "Add the new VM to the specified pool.",
2309 },
2310 snapname => get_standard_option('pve-snapshot-name', {
2311 optional => 1,
2312 }),
2313 storage => get_standard_option('pve-storage-id', {
2314 description => "Target storage for full clone.",
2315 requires => 'full',
2316 optional => 1,
2317 }),
2318 'format' => {
2319 description => "Target format for file storage.",
2320 requires => 'full',
2321 type => 'string',
2322 optional => 1,
2323 enum => [ 'raw', 'qcow2', 'vmdk'],
2324 },
2325 full => {
2326 optional => 1,
2327 type => 'boolean',
2328 description => "Create a full copy of all disk. This is always done when " .
2329 "you clone a normal VM. For VM templates, we try to create a linked clone by default.",
2330 default => 0,
2331 },
2332 target => get_standard_option('pve-node', {
2333 description => "Target node. Only allowed if the original VM is on shared storage.",
2334 optional => 1,
2335 }),
2336 },
2337 },
2338 returns => {
2339 type => 'string',
2340 },
2341 code => sub {
2342 my ($param) = @_;
2343
2344 my $rpcenv = PVE::RPCEnvironment::get();
2345
2346 my $authuser = $rpcenv->get_user();
2347
2348 my $node = extract_param($param, 'node');
2349
2350 my $vmid = extract_param($param, 'vmid');
2351
2352 my $newid = extract_param($param, 'newid');
2353
2354 my $pool = extract_param($param, 'pool');
2355
2356 if (defined($pool)) {
2357 $rpcenv->check_pool_exist($pool);
2358 }
2359
2360 my $snapname = extract_param($param, 'snapname');
2361
2362 my $storage = extract_param($param, 'storage');
2363
2364 my $format = extract_param($param, 'format');
2365
2366 my $target = extract_param($param, 'target');
2367
2368 my $localnode = PVE::INotify::nodename();
2369
2370 undef $target if $target && ($target eq $localnode || $target eq 'localhost');
2371
2372 PVE::Cluster::check_node_exists($target) if $target;
2373
2374 my $storecfg = PVE::Storage::config();
2375
2376 if ($storage) {
2377 # check if storage is enabled on local node
2378 PVE::Storage::storage_check_enabled($storecfg, $storage);
2379 if ($target) {
2380 # check if storage is available on target node
2381 PVE::Storage::storage_check_node($storecfg, $storage, $target);
2382 # clone only works if target storage is shared
2383 my $scfg = PVE::Storage::storage_config($storecfg, $storage);
2384 die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared};
2385 }
2386 }
2387
2388 PVE::Cluster::check_cfs_quorum();
2389
2390 my $running = PVE::QemuServer::check_running($vmid) || 0;
2391
2392 # exclusive lock if VM is running - else shared lock is enough;
2393 my $shared_lock = $running ? 0 : 1;
2394
2395 my $clonefn = sub {
2396
2397 # do all tests after lock
2398 # we also try to do all tests before we fork the worker
2399
2400 my $conf = PVE::QemuConfig->load_config($vmid);
2401
2402 PVE::QemuConfig->check_lock($conf);
2403
2404 my $verify_running = PVE::QemuServer::check_running($vmid) || 0;
2405
2406 die "unexpected state change\n" if $verify_running != $running;
2407
2408 die "snapshot '$snapname' does not exist\n"
2409 if $snapname && !defined( $conf->{snapshots}->{$snapname});
2410
2411 my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
2412
2413 my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage);
2414
2415 die "can't clone VM to node '$target' (VM uses local storage)\n" if $target && !$sharedvm;
2416
2417 my $conffile = PVE::QemuConfig->config_file($newid);
2418
2419 die "unable to create VM $newid: config file already exists\n"
2420 if -f $conffile;
2421
2422 my $newconf = { lock => 'clone' };
2423 my $drives = {};
2424 my $fullclone = {};
2425 my $vollist = [];
2426
2427 foreach my $opt (keys %$oldconf) {
2428 my $value = $oldconf->{$opt};
2429
2430 # do not copy snapshot related info
2431 next if $opt eq 'snapshots' || $opt eq 'parent' || $opt eq 'snaptime' ||
2432 $opt eq 'vmstate' || $opt eq 'snapstate';
2433
2434 # no need to copy unused images, because VMID(owner) changes anyways
2435 next if $opt =~ m/^unused\d+$/;
2436
2437 # always change MAC! address
2438 if ($opt =~ m/^net(\d+)$/) {
2439 my $net = PVE::QemuServer::parse_net($value);
2440 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
2441 $net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
2442 $newconf->{$opt} = PVE::QemuServer::print_net($net);
2443 } elsif (PVE::QemuServer::is_valid_drivename($opt)) {
2444 my $drive = PVE::QemuServer::parse_drive($opt, $value);
2445 die "unable to parse drive options for '$opt'\n" if !$drive;
2446 if (PVE::QemuServer::drive_is_cdrom($drive)) {
2447 $newconf->{$opt} = $value; # simply copy configuration
2448 } else {
2449 if ($param->{full}) {
2450 die "Full clone feature is not available"
2451 if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running);
2452 $fullclone->{$opt} = 1;
2453 } else {
2454 # not full means clone instead of copy
2455 die "Linked clone feature is not available"
2456 if !PVE::Storage::volume_has_feature($storecfg, 'clone', $drive->{file}, $snapname, $running);
2457 }
2458 $drives->{$opt} = $drive;
2459 push @$vollist, $drive->{file};
2460 }
2461 } else {
2462 # copy everything else
2463 $newconf->{$opt} = $value;
2464 }
2465 }
2466
2467 # auto generate a new uuid
2468 my ($uuid, $uuid_str);
2469 UUID::generate($uuid);
2470 UUID::unparse($uuid, $uuid_str);
2471 my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || '');
2472 $smbios1->{uuid} = $uuid_str;
2473 $newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1);
2474
2475 delete $newconf->{template};
2476
2477 if ($param->{name}) {
2478 $newconf->{name} = $param->{name};
2479 } else {
2480 if ($oldconf->{name}) {
2481 $newconf->{name} = "Copy-of-$oldconf->{name}";
2482 } else {
2483 $newconf->{name} = "Copy-of-VM-$vmid";
2484 }
2485 }
2486
2487 if ($param->{description}) {
2488 $newconf->{description} = $param->{description};
2489 }
2490
2491 # create empty/temp config - this fails if VM already exists on other node
2492 PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n");
2493
2494 my $realcmd = sub {
2495 my $upid = shift;
2496
2497 my $newvollist = [];
2498 my $jobs = {};
2499
2500 eval {
2501 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
2502
2503 PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
2504
2505 my $total_jobs = scalar(keys %{$drives});
2506 my $i = 1;
2507
2508 foreach my $opt (keys %$drives) {
2509 my $drive = $drives->{$opt};
2510 my $skipcomplete = ($total_jobs != $i); # finish after last drive
2511
2512 my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $opt, $drive, $snapname,
2513 $newid, $storage, $format, $fullclone->{$opt}, $newvollist,
2514 $jobs, $skipcomplete, $oldconf->{agent});
2515
2516 $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive);
2517
2518 PVE::QemuConfig->write_config($newid, $newconf);
2519 $i++;
2520 }
2521
2522 delete $newconf->{lock};
2523 PVE::QemuConfig->write_config($newid, $newconf);
2524
2525 if ($target) {
2526 # always deactivate volumes - avoid lvm LVs to be active on several nodes
2527 PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running;
2528 PVE::Storage::deactivate_volumes($storecfg, $newvollist);
2529
2530 my $newconffile = PVE::QemuConfig->config_file($newid, $target);
2531 die "Failed to move config to node '$target' - rename failed: $!\n"
2532 if !rename($conffile, $newconffile);
2533 }
2534
2535 PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
2536 };
2537 if (my $err = $@) {
2538 unlink $conffile;
2539
2540 eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs) };
2541
2542 sleep 1; # some storage like rbd need to wait before release volume - really?
2543
2544 foreach my $volid (@$newvollist) {
2545 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
2546 warn $@ if $@;
2547 }
2548 die "clone failed: $err";
2549 }
2550
2551 return;
2552 };
2553
2554 PVE::Firewall::clone_vmfw_conf($vmid, $newid);
2555
2556 return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $realcmd);
2557 };
2558
2559 return PVE::QemuConfig->lock_config_mode($vmid, 1, $shared_lock, sub {
2560 # Aquire exclusive lock lock for $newid
2561 return PVE::QemuConfig->lock_config_full($newid, 1, $clonefn);
2562 });
2563
2564 }});
2565
2566 __PACKAGE__->register_method({
2567 name => 'move_vm_disk',
2568 path => '{vmid}/move_disk',
2569 method => 'POST',
2570 protected => 1,
2571 proxyto => 'node',
2572 description => "Move volume to different storage.",
2573 permissions => {
2574 description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, and 'Datastore.AllocateSpace' permissions on the storage.",
2575 check => [ 'and',
2576 ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
2577 ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]],
2578 ],
2579 },
2580 parameters => {
2581 additionalProperties => 0,
2582 properties => {
2583 node => get_standard_option('pve-node'),
2584 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
2585 disk => {
2586 type => 'string',
2587 description => "The disk you want to move.",
2588 enum => [ PVE::QemuServer::valid_drive_names() ],
2589 },
2590 storage => get_standard_option('pve-storage-id', {
2591 description => "Target storage.",
2592 completion => \&PVE::QemuServer::complete_storage,
2593 }),
2594 'format' => {
2595 type => 'string',
2596 description => "Target Format.",
2597 enum => [ 'raw', 'qcow2', 'vmdk' ],
2598 optional => 1,
2599 },
2600 delete => {
2601 type => 'boolean',
2602 description => "Delete the original disk after successful copy. By default the original disk is kept as unused disk.",
2603 optional => 1,
2604 default => 0,
2605 },
2606 digest => {
2607 type => 'string',
2608 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
2609 maxLength => 40,
2610 optional => 1,
2611 },
2612 },
2613 },
2614 returns => {
2615 type => 'string',
2616 description => "the task ID.",
2617 },
2618 code => sub {
2619 my ($param) = @_;
2620
2621 my $rpcenv = PVE::RPCEnvironment::get();
2622
2623 my $authuser = $rpcenv->get_user();
2624
2625 my $node = extract_param($param, 'node');
2626
2627 my $vmid = extract_param($param, 'vmid');
2628
2629 my $digest = extract_param($param, 'digest');
2630
2631 my $disk = extract_param($param, 'disk');
2632
2633 my $storeid = extract_param($param, 'storage');
2634
2635 my $format = extract_param($param, 'format');
2636
2637 my $storecfg = PVE::Storage::config();
2638
2639 my $updatefn = sub {
2640
2641 my $conf = PVE::QemuConfig->load_config($vmid);
2642
2643 PVE::QemuConfig->check_lock($conf);
2644
2645 die "checksum missmatch (file change by other user?)\n"
2646 if $digest && $digest ne $conf->{digest};
2647
2648 die "disk '$disk' does not exist\n" if !$conf->{$disk};
2649
2650 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
2651
2652 my $old_volid = $drive->{file} || die "disk '$disk' has no associated volume\n";
2653
2654 die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
2655
2656 my $oldfmt;
2657 my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($old_volid);
2658 if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){
2659 $oldfmt = $1;
2660 }
2661
2662 die "you can't move on the same storage with same format\n" if $oldstoreid eq $storeid &&
2663 (!$format || !$oldfmt || $oldfmt eq $format);
2664
2665 # this only checks snapshots because $disk is passed!
2666 my $snapshotted = PVE::QemuServer::is_volume_in_use($storecfg, $conf, $disk, $old_volid);
2667 die "you can't move a disk with snapshots and delete the source\n"
2668 if $snapshotted && $param->{delete};
2669
2670 PVE::Cluster::log_msg('info', $authuser, "move disk VM $vmid: move --disk $disk --storage $storeid");
2671
2672 my $running = PVE::QemuServer::check_running($vmid);
2673
2674 PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]);
2675
2676 my $realcmd = sub {
2677
2678 my $newvollist = [];
2679
2680 eval {
2681 local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
2682
2683 warn "moving disk with snapshots, snapshots will not be moved!\n"
2684 if $snapshotted;
2685
2686 my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $disk, $drive, undef,
2687 $vmid, $storeid, $format, 1, $newvollist);
2688
2689 $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $newdrive);
2690
2691 PVE::QemuConfig->add_unused_volume($conf, $old_volid) if !$param->{delete};
2692
2693 # convert moved disk to base if part of template
2694 PVE::QemuServer::template_create($vmid, $conf, $disk)
2695 if PVE::QemuConfig->is_template($conf);
2696
2697 PVE::QemuConfig->write_config($vmid, $conf);
2698
2699 eval {
2700 # try to deactivate volumes - avoid lvm LVs to be active on several nodes
2701 PVE::Storage::deactivate_volumes($storecfg, [ $newdrive->{file} ])
2702 if !$running;
2703 };
2704 warn $@ if $@;
2705 };
2706 if (my $err = $@) {
2707
2708 foreach my $volid (@$newvollist) {
2709 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
2710 warn $@ if $@;
2711 }
2712 die "storage migration failed: $err";
2713 }
2714
2715 if ($param->{delete}) {
2716 eval {
2717 PVE::Storage::deactivate_volumes($storecfg, [$old_volid]);
2718 PVE::Storage::vdisk_free($storecfg, $old_volid);
2719 };
2720 warn $@ if $@;
2721 }
2722 };
2723
2724 return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd);
2725 };
2726
2727 return PVE::QemuConfig->lock_config($vmid, $updatefn);
2728 }});
2729
2730 __PACKAGE__->register_method({
2731 name => 'migrate_vm',
2732 path => '{vmid}/migrate',
2733 method => 'POST',
2734 protected => 1,
2735 proxyto => 'node',
2736 description => "Migrate virtual machine. Creates a new migration task.",
2737 permissions => {
2738 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
2739 },
2740 parameters => {
2741 additionalProperties => 0,
2742 properties => {
2743 node => get_standard_option('pve-node'),
2744 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
2745 target => get_standard_option('pve-node', {
2746 description => "Target node.",
2747 completion => \&PVE::Cluster::complete_migration_target,
2748 }),
2749 online => {
2750 type => 'boolean',
2751 description => "Use online/live migration.",
2752 optional => 1,
2753 },
2754 force => {
2755 type => 'boolean',
2756 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
2757 optional => 1,
2758 },
2759 migration_type => {
2760 type => 'string',
2761 enum => ['secure', 'insecure'],
2762 description => "Migration traffic is encrypted using an SSH tunnel by default. On secure, completely private networks this can be disabled to increase performance.",
2763 optional => 1,
2764 },
2765 migration_network => {
2766 type => 'string', format => 'CIDR',
2767 description => "CIDR of the (sub) network that is used for migration.",
2768 optional => 1,
2769 },
2770 "with-local-disks" => {
2771 type => 'boolean',
2772 description => "Enable live storage migration for local disk",
2773 optional => 1,
2774 },
2775 targetstorage => get_standard_option('pve-storage-id', {
2776 description => "Default target storage.",
2777 optional => 1,
2778 completion => \&PVE::QemuServer::complete_storage,
2779 }),
2780 },
2781 },
2782 returns => {
2783 type => 'string',
2784 description => "the task ID.",
2785 },
2786 code => sub {
2787 my ($param) = @_;
2788
2789 my $rpcenv = PVE::RPCEnvironment::get();
2790
2791 my $authuser = $rpcenv->get_user();
2792
2793 my $target = extract_param($param, 'target');
2794
2795 my $localnode = PVE::INotify::nodename();
2796 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
2797
2798 PVE::Cluster::check_cfs_quorum();
2799
2800 PVE::Cluster::check_node_exists($target);
2801
2802 my $targetip = PVE::Cluster::remote_node_ip($target);
2803
2804 my $vmid = extract_param($param, 'vmid');
2805
2806 raise_param_exc({ targetstorage => "Live storage migration can only be done online." })
2807 if !$param->{online} && $param->{targetstorage};
2808
2809 raise_param_exc({ force => "Only root may use this option." })
2810 if $param->{force} && $authuser ne 'root@pam';
2811
2812 raise_param_exc({ migration_type => "Only root may use this option." })
2813 if $param->{migration_type} && $authuser ne 'root@pam';
2814
2815 # allow root only until better network permissions are available
2816 raise_param_exc({ migration_network => "Only root may use this option." })
2817 if $param->{migration_network} && $authuser ne 'root@pam';
2818
2819 # test if VM exists
2820 my $conf = PVE::QemuConfig->load_config($vmid);
2821
2822 # try to detect errors early
2823
2824 PVE::QemuConfig->check_lock($conf);
2825
2826 if (PVE::QemuServer::check_running($vmid)) {
2827 die "cant migrate running VM without --online\n"
2828 if !$param->{online};
2829 }
2830
2831 my $storecfg = PVE::Storage::config();
2832
2833 if( $param->{targetstorage}) {
2834 PVE::Storage::storage_check_node($storecfg, $param->{targetstorage}, $target);
2835 } else {
2836 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target);
2837 }
2838
2839 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
2840
2841 my $hacmd = sub {
2842 my $upid = shift;
2843
2844 my $service = "vm:$vmid";
2845
2846 my $cmd = ['ha-manager', 'migrate', $service, $target];
2847
2848 print "Executing HA migrate for VM $vmid to node $target\n";
2849
2850 PVE::Tools::run_command($cmd);
2851
2852 return;
2853 };
2854
2855 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
2856
2857 } else {
2858
2859 my $realcmd = sub {
2860 my $upid = shift;
2861
2862 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
2863 };
2864
2865 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
2866 }
2867
2868 }});
2869
2870 __PACKAGE__->register_method({
2871 name => 'monitor',
2872 path => '{vmid}/monitor',
2873 method => 'POST',
2874 protected => 1,
2875 proxyto => 'node',
2876 description => "Execute Qemu monitor commands.",
2877 permissions => {
2878 description => "Sys.Modify is required for (sub)commands which are not read-only ('info *' and 'help')",
2879 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
2880 },
2881 parameters => {
2882 additionalProperties => 0,
2883 properties => {
2884 node => get_standard_option('pve-node'),
2885 vmid => get_standard_option('pve-vmid'),
2886 command => {
2887 type => 'string',
2888 description => "The monitor command.",
2889 }
2890 },
2891 },
2892 returns => { type => 'string'},
2893 code => sub {
2894 my ($param) = @_;
2895
2896 my $rpcenv = PVE::RPCEnvironment::get();
2897 my $authuser = $rpcenv->get_user();
2898
2899 my $is_ro = sub {
2900 my $command = shift;
2901 return $command =~ m/^\s*info(\s+|$)/
2902 || $command =~ m/^\s*help\s*$/;
2903 };
2904
2905 $rpcenv->check_full($authuser, "/", ['Sys.Modify'])
2906 if !&$is_ro($param->{command});
2907
2908 my $vmid = $param->{vmid};
2909
2910 my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
2911
2912 my $res = '';
2913 eval {
2914 $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command});
2915 };
2916 $res = "ERROR: $@" if $@;
2917
2918 return $res;
2919 }});
2920
2921 my $guest_agent_commands = [
2922 'ping',
2923 'get-time',
2924 'info',
2925 'fsfreeze-status',
2926 'fsfreeze-freeze',
2927 'fsfreeze-thaw',
2928 'fstrim',
2929 'network-get-interfaces',
2930 'get-vcpus',
2931 'get-fsinfo',
2932 'get-memory-blocks',
2933 'get-memory-block-info',
2934 'suspend-hybrid',
2935 'suspend-ram',
2936 'suspend-disk',
2937 'shutdown',
2938 ];
2939
2940 __PACKAGE__->register_method({
2941 name => 'agent',
2942 path => '{vmid}/agent',
2943 method => 'POST',
2944 protected => 1,
2945 proxyto => 'node',
2946 description => "Execute Qemu Guest Agent commands.",
2947 permissions => {
2948 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
2949 },
2950 parameters => {
2951 additionalProperties => 0,
2952 properties => {
2953 node => get_standard_option('pve-node'),
2954 vmid => get_standard_option('pve-vmid', {
2955 completion => \&PVE::QemuServer::complete_vmid_running }),
2956 command => {
2957 type => 'string',
2958 description => "The QGA command.",
2959 enum => $guest_agent_commands,
2960 },
2961 },
2962 },
2963 returns => {
2964 type => 'object',
2965 description => "Returns an object with a single `result` property. The type of that
2966 property depends on the executed command.",
2967 },
2968 code => sub {
2969 my ($param) = @_;
2970
2971 my $vmid = $param->{vmid};
2972
2973 my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
2974
2975 die "No Qemu Guest Agent\n" if !defined($conf->{agent});
2976 die "VM $vmid is not running\n" if !PVE::QemuServer::check_running($vmid);
2977
2978 my $cmd = $param->{command};
2979
2980 my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-$cmd");
2981
2982 return { result => $res };
2983 }});
2984
2985 __PACKAGE__->register_method({
2986 name => 'resize_vm',
2987 path => '{vmid}/resize',
2988 method => 'PUT',
2989 protected => 1,
2990 proxyto => 'node',
2991 description => "Extend volume size.",
2992 permissions => {
2993 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
2994 },
2995 parameters => {
2996 additionalProperties => 0,
2997 properties => {
2998 node => get_standard_option('pve-node'),
2999 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3000 skiplock => get_standard_option('skiplock'),
3001 disk => {
3002 type => 'string',
3003 description => "The disk you want to resize.",
3004 enum => [PVE::QemuServer::valid_drive_names()],
3005 },
3006 size => {
3007 type => 'string',
3008 pattern => '\+?\d+(\.\d+)?[KMGT]?',
3009 description => "The new size. With the `+` sign the value is added to the actual size of the volume and without it, the value is taken as an absolute one. Shrinking disk size is not supported.",
3010 },
3011 digest => {
3012 type => 'string',
3013 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
3014 maxLength => 40,
3015 optional => 1,
3016 },
3017 },
3018 },
3019 returns => { type => 'null'},
3020 code => sub {
3021 my ($param) = @_;
3022
3023 my $rpcenv = PVE::RPCEnvironment::get();
3024
3025 my $authuser = $rpcenv->get_user();
3026
3027 my $node = extract_param($param, 'node');
3028
3029 my $vmid = extract_param($param, 'vmid');
3030
3031 my $digest = extract_param($param, 'digest');
3032
3033 my $disk = extract_param($param, 'disk');
3034
3035 my $sizestr = extract_param($param, 'size');
3036
3037 my $skiplock = extract_param($param, 'skiplock');
3038 raise_param_exc({ skiplock => "Only root may use this option." })
3039 if $skiplock && $authuser ne 'root@pam';
3040
3041 my $storecfg = PVE::Storage::config();
3042
3043 my $updatefn = sub {
3044
3045 my $conf = PVE::QemuConfig->load_config($vmid);
3046
3047 die "checksum missmatch (file change by other user?)\n"
3048 if $digest && $digest ne $conf->{digest};
3049 PVE::QemuConfig->check_lock($conf) if !$skiplock;
3050
3051 die "disk '$disk' does not exist\n" if !$conf->{$disk};
3052
3053 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
3054
3055 my (undef, undef, undef, undef, undef, undef, $format) =
3056 PVE::Storage::parse_volname($storecfg, $drive->{file});
3057
3058 die "can't resize volume: $disk if snapshot exists\n"
3059 if %{$conf->{snapshots}} && $format eq 'qcow2';
3060
3061 my $volid = $drive->{file};
3062
3063 die "disk '$disk' has no associated volume\n" if !$volid;
3064
3065 die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
3066
3067 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
3068
3069 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
3070
3071 PVE::Storage::activate_volumes($storecfg, [$volid]);
3072 my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
3073
3074 die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
3075 my ($ext, $newsize, $unit) = ($1, $2, $4);
3076 if ($unit) {
3077 if ($unit eq 'K') {
3078 $newsize = $newsize * 1024;
3079 } elsif ($unit eq 'M') {
3080 $newsize = $newsize * 1024 * 1024;
3081 } elsif ($unit eq 'G') {
3082 $newsize = $newsize * 1024 * 1024 * 1024;
3083 } elsif ($unit eq 'T') {
3084 $newsize = $newsize * 1024 * 1024 * 1024 * 1024;
3085 }
3086 }
3087 $newsize += $size if $ext;
3088 $newsize = int($newsize);
3089
3090 die "shrinking disks is not supported\n" if $newsize < $size;
3091
3092 return if $size == $newsize;
3093
3094 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr");
3095
3096 PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize);
3097
3098 $drive->{size} = $newsize;
3099 $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive);
3100
3101 PVE::QemuConfig->write_config($vmid, $conf);
3102 };
3103
3104 PVE::QemuConfig->lock_config($vmid, $updatefn);
3105 return undef;
3106 }});
3107
3108 __PACKAGE__->register_method({
3109 name => 'snapshot_list',
3110 path => '{vmid}/snapshot',
3111 method => 'GET',
3112 description => "List all snapshots.",
3113 permissions => {
3114 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
3115 },
3116 proxyto => 'node',
3117 protected => 1, # qemu pid files are only readable by root
3118 parameters => {
3119 additionalProperties => 0,
3120 properties => {
3121 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3122 node => get_standard_option('pve-node'),
3123 },
3124 },
3125 returns => {
3126 type => 'array',
3127 items => {
3128 type => "object",
3129 properties => {},
3130 },
3131 links => [ { rel => 'child', href => "{name}" } ],
3132 },
3133 code => sub {
3134 my ($param) = @_;
3135
3136 my $vmid = $param->{vmid};
3137
3138 my $conf = PVE::QemuConfig->load_config($vmid);
3139 my $snaphash = $conf->{snapshots} || {};
3140
3141 my $res = [];
3142
3143 foreach my $name (keys %$snaphash) {
3144 my $d = $snaphash->{$name};
3145 my $item = {
3146 name => $name,
3147 snaptime => $d->{snaptime} || 0,
3148 vmstate => $d->{vmstate} ? 1 : 0,
3149 description => $d->{description} || '',
3150 };
3151 $item->{parent} = $d->{parent} if $d->{parent};
3152 $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
3153 push @$res, $item;
3154 }
3155
3156 my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
3157 my $current = { name => 'current', digest => $conf->{digest}, running => $running };
3158 $current->{parent} = $conf->{parent} if $conf->{parent};
3159
3160 push @$res, $current;
3161
3162 return $res;
3163 }});
3164
3165 __PACKAGE__->register_method({
3166 name => 'snapshot',
3167 path => '{vmid}/snapshot',
3168 method => 'POST',
3169 protected => 1,
3170 proxyto => 'node',
3171 description => "Snapshot a VM.",
3172 permissions => {
3173 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
3174 },
3175 parameters => {
3176 additionalProperties => 0,
3177 properties => {
3178 node => get_standard_option('pve-node'),
3179 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3180 snapname => get_standard_option('pve-snapshot-name'),
3181 vmstate => {
3182 optional => 1,
3183 type => 'boolean',
3184 description => "Save the vmstate",
3185 },
3186 description => {
3187 optional => 1,
3188 type => 'string',
3189 description => "A textual description or comment.",
3190 },
3191 },
3192 },
3193 returns => {
3194 type => 'string',
3195 description => "the task ID.",
3196 },
3197 code => sub {
3198 my ($param) = @_;
3199
3200 my $rpcenv = PVE::RPCEnvironment::get();
3201
3202 my $authuser = $rpcenv->get_user();
3203
3204 my $node = extract_param($param, 'node');
3205
3206 my $vmid = extract_param($param, 'vmid');
3207
3208 my $snapname = extract_param($param, 'snapname');
3209
3210 die "unable to use snapshot name 'current' (reserved name)\n"
3211 if $snapname eq 'current';
3212
3213 my $realcmd = sub {
3214 PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
3215 PVE::QemuConfig->snapshot_create($vmid, $snapname, $param->{vmstate},
3216 $param->{description});
3217 };
3218
3219 return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd);
3220 }});
3221
3222 __PACKAGE__->register_method({
3223 name => 'snapshot_cmd_idx',
3224 path => '{vmid}/snapshot/{snapname}',
3225 description => '',
3226 method => 'GET',
3227 permissions => {
3228 user => 'all',
3229 },
3230 parameters => {
3231 additionalProperties => 0,
3232 properties => {
3233 vmid => get_standard_option('pve-vmid'),
3234 node => get_standard_option('pve-node'),
3235 snapname => get_standard_option('pve-snapshot-name'),
3236 },
3237 },
3238 returns => {
3239 type => 'array',
3240 items => {
3241 type => "object",
3242 properties => {},
3243 },
3244 links => [ { rel => 'child', href => "{cmd}" } ],
3245 },
3246 code => sub {
3247 my ($param) = @_;
3248
3249 my $res = [];
3250
3251 push @$res, { cmd => 'rollback' };
3252 push @$res, { cmd => 'config' };
3253
3254 return $res;
3255 }});
3256
3257 __PACKAGE__->register_method({
3258 name => 'update_snapshot_config',
3259 path => '{vmid}/snapshot/{snapname}/config',
3260 method => 'PUT',
3261 protected => 1,
3262 proxyto => 'node',
3263 description => "Update snapshot metadata.",
3264 permissions => {
3265 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
3266 },
3267 parameters => {
3268 additionalProperties => 0,
3269 properties => {
3270 node => get_standard_option('pve-node'),
3271 vmid => get_standard_option('pve-vmid'),
3272 snapname => get_standard_option('pve-snapshot-name'),
3273 description => {
3274 optional => 1,
3275 type => 'string',
3276 description => "A textual description or comment.",
3277 },
3278 },
3279 },
3280 returns => { type => 'null' },
3281 code => sub {
3282 my ($param) = @_;
3283
3284 my $rpcenv = PVE::RPCEnvironment::get();
3285
3286 my $authuser = $rpcenv->get_user();
3287
3288 my $vmid = extract_param($param, 'vmid');
3289
3290 my $snapname = extract_param($param, 'snapname');
3291
3292 return undef if !defined($param->{description});
3293
3294 my $updatefn = sub {
3295
3296 my $conf = PVE::QemuConfig->load_config($vmid);
3297
3298 PVE::QemuConfig->check_lock($conf);
3299
3300 my $snap = $conf->{snapshots}->{$snapname};
3301
3302 die "snapshot '$snapname' does not exist\n" if !defined($snap);
3303
3304 $snap->{description} = $param->{description} if defined($param->{description});
3305
3306 PVE::QemuConfig->write_config($vmid, $conf);
3307 };
3308
3309 PVE::QemuConfig->lock_config($vmid, $updatefn);
3310
3311 return undef;
3312 }});
3313
3314 __PACKAGE__->register_method({
3315 name => 'get_snapshot_config',
3316 path => '{vmid}/snapshot/{snapname}/config',
3317 method => 'GET',
3318 proxyto => 'node',
3319 description => "Get snapshot configuration",
3320 permissions => {
3321 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
3322 },
3323 parameters => {
3324 additionalProperties => 0,
3325 properties => {
3326 node => get_standard_option('pve-node'),
3327 vmid => get_standard_option('pve-vmid'),
3328 snapname => get_standard_option('pve-snapshot-name'),
3329 },
3330 },
3331 returns => { type => "object" },
3332 code => sub {
3333 my ($param) = @_;
3334
3335 my $rpcenv = PVE::RPCEnvironment::get();
3336
3337 my $authuser = $rpcenv->get_user();
3338
3339 my $vmid = extract_param($param, 'vmid');
3340
3341 my $snapname = extract_param($param, 'snapname');
3342
3343 my $conf = PVE::QemuConfig->load_config($vmid);
3344
3345 my $snap = $conf->{snapshots}->{$snapname};
3346
3347 die "snapshot '$snapname' does not exist\n" if !defined($snap);
3348
3349 return $snap;
3350 }});
3351
3352 __PACKAGE__->register_method({
3353 name => 'rollback',
3354 path => '{vmid}/snapshot/{snapname}/rollback',
3355 method => 'POST',
3356 protected => 1,
3357 proxyto => 'node',
3358 description => "Rollback VM state to specified snapshot.",
3359 permissions => {
3360 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
3361 },
3362 parameters => {
3363 additionalProperties => 0,
3364 properties => {
3365 node => get_standard_option('pve-node'),
3366 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3367 snapname => get_standard_option('pve-snapshot-name'),
3368 },
3369 },
3370 returns => {
3371 type => 'string',
3372 description => "the task ID.",
3373 },
3374 code => sub {
3375 my ($param) = @_;
3376
3377 my $rpcenv = PVE::RPCEnvironment::get();
3378
3379 my $authuser = $rpcenv->get_user();
3380
3381 my $node = extract_param($param, 'node');
3382
3383 my $vmid = extract_param($param, 'vmid');
3384
3385 my $snapname = extract_param($param, 'snapname');
3386
3387 my $realcmd = sub {
3388 PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname");
3389 PVE::QemuConfig->snapshot_rollback($vmid, $snapname);
3390 };
3391
3392 return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd);
3393 }});
3394
3395 __PACKAGE__->register_method({
3396 name => 'delsnapshot',
3397 path => '{vmid}/snapshot/{snapname}',
3398 method => 'DELETE',
3399 protected => 1,
3400 proxyto => 'node',
3401 description => "Delete a VM snapshot.",
3402 permissions => {
3403 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
3404 },
3405 parameters => {
3406 additionalProperties => 0,
3407 properties => {
3408 node => get_standard_option('pve-node'),
3409 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3410 snapname => get_standard_option('pve-snapshot-name'),
3411 force => {
3412 optional => 1,
3413 type => 'boolean',
3414 description => "For removal from config file, even if removing disk snapshots fails.",
3415 },
3416 },
3417 },
3418 returns => {
3419 type => 'string',
3420 description => "the task ID.",
3421 },
3422 code => sub {
3423 my ($param) = @_;
3424
3425 my $rpcenv = PVE::RPCEnvironment::get();
3426
3427 my $authuser = $rpcenv->get_user();
3428
3429 my $node = extract_param($param, 'node');
3430
3431 my $vmid = extract_param($param, 'vmid');
3432
3433 my $snapname = extract_param($param, 'snapname');
3434
3435 my $realcmd = sub {
3436 PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
3437 PVE::QemuConfig->snapshot_delete($vmid, $snapname, $param->{force});
3438 };
3439
3440 return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);
3441 }});
3442
3443 __PACKAGE__->register_method({
3444 name => 'template',
3445 path => '{vmid}/template',
3446 method => 'POST',
3447 protected => 1,
3448 proxyto => 'node',
3449 description => "Create a Template.",
3450 permissions => {
3451 description => "You need 'VM.Allocate' permissions on /vms/{vmid}",
3452 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
3453 },
3454 parameters => {
3455 additionalProperties => 0,
3456 properties => {
3457 node => get_standard_option('pve-node'),
3458 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }),
3459 disk => {
3460 optional => 1,
3461 type => 'string',
3462 description => "If you want to convert only 1 disk to base image.",
3463 enum => [PVE::QemuServer::valid_drive_names()],
3464 },
3465
3466 },
3467 },
3468 returns => { type => 'null'},
3469 code => sub {
3470 my ($param) = @_;
3471
3472 my $rpcenv = PVE::RPCEnvironment::get();
3473
3474 my $authuser = $rpcenv->get_user();
3475
3476 my $node = extract_param($param, 'node');
3477
3478 my $vmid = extract_param($param, 'vmid');
3479
3480 my $disk = extract_param($param, 'disk');
3481
3482 my $updatefn = sub {
3483
3484 my $conf = PVE::QemuConfig->load_config($vmid);
3485
3486 PVE::QemuConfig->check_lock($conf);
3487
3488 die "unable to create template, because VM contains snapshots\n"
3489 if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}});
3490
3491 die "you can't convert a template to a template\n"
3492 if PVE::QemuConfig->is_template($conf) && !$disk;
3493
3494 die "you can't convert a VM to template if VM is running\n"
3495 if PVE::QemuServer::check_running($vmid);
3496
3497 my $realcmd = sub {
3498 PVE::QemuServer::template_create($vmid, $conf, $disk);
3499 };
3500
3501 $conf->{template} = 1;
3502 PVE::QemuConfig->write_config($vmid, $conf);
3503
3504 return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
3505 };
3506
3507 PVE::QemuConfig->lock_config($vmid, $updatefn);
3508 return undef;
3509 }});
3510
3511 1;