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