]> git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu.pm
daada76ceea0881e946e035dc9a0f2aba79a652e
[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
7 use PVE::Cluster qw (cfs_read_file cfs_write_file);;
8 use PVE::SafeSyslog;
9 use PVE::Tools qw(extract_param);
10 use PVE::Exception qw(raise raise_param_exc);
11 use PVE::Storage;
12 use PVE::JSONSchema qw(get_standard_option);
13 use PVE::RESTHandler;
14 use PVE::QemuServer;
15 use PVE::QemuMigrate;
16 use PVE::RPCEnvironment;
17 use PVE::AccessControl;
18 use PVE::INotify;
19
20 use Data::Dumper; # fixme: remove
21
22 use base qw(PVE::RESTHandler);
23
24 my $opt_force_description = "Force physical removal. Without this, we simple remove the disk from the config file and create an additional configuration entry called 'unused[n]', which contains the volume ID. Unlink of unused[n] always cause physical removal.";
25
26 my $resolve_cdrom_alias = sub {
27 my $param = shift;
28
29 if (my $value = $param->{cdrom}) {
30 $value .= ",media=cdrom" if $value !~ m/media=/;
31 $param->{ide2} = $value;
32 delete $param->{cdrom};
33 }
34 };
35
36
37 my $check_storage_access = sub {
38 my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_;
39
40 PVE::QemuServer::foreach_drive($settings, sub {
41 my ($ds, $drive) = @_;
42
43 my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive);
44
45 my $volid = $drive->{file};
46
47 if (!$volid || $volid eq 'none') {
48 # nothing to check
49 } elsif ($isCDROM && ($volid eq 'cdrom')) {
50 $rpcenv->check($authuser, "/", ['Sys.Console']);
51 } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) {
52 my ($storeid, $size) = ($2 || $default_storage, $3);
53 die "no storage ID specified (and no default storage)\n" if !$storeid;
54 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
55 } else {
56 $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
57 }
58 });
59 };
60
61 # Note: $pool is only needed when creating a VM, because pool permissions
62 # are automatically inherited if VM already exists inside a pool.
63 my $create_disks = sub {
64 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
65
66 my $vollist = [];
67
68 my $res = {};
69 PVE::QemuServer::foreach_drive($settings, sub {
70 my ($ds, $disk) = @_;
71
72 my $volid = $disk->{file};
73
74 if (!$volid || $volid eq 'none' || $volid eq 'cdrom') {
75 delete $disk->{size};
76 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
77 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
78 my ($storeid, $size) = ($2 || $default_storage, $3);
79 die "no storage ID specified (and no default storage)\n" if !$storeid;
80 my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
81 my $fmt = $disk->{format} || $defformat;
82 my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid,
83 $fmt, undef, $size*1024*1024);
84 $disk->{file} = $volid;
85 $disk->{size} = $size*1024*1024*1024;
86 push @$vollist, $volid;
87 delete $disk->{format}; # no longer needed
88 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
89 } else {
90
91 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
92
93 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
94
95 my $foundvolid = undef;
96
97 if ($storeid) {
98 PVE::Storage::activate_volumes($storecfg, [ $volid ]);
99 my $dl = PVE::Storage::vdisk_list($storecfg, $storeid, undef);
100
101 PVE::Storage::foreach_volid($dl, sub {
102 my ($volumeid) = @_;
103 if($volumeid eq $volid) {
104 $foundvolid = 1;
105 return;
106 }
107 });
108 }
109
110 die "image '$path' does not exists\n" if (!(-f $path || -b $path || $foundvolid));
111
112 my ($size) = PVE::Storage::volume_size_info($storecfg, $volid, 1);
113 $disk->{size} = $size;
114 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
115 }
116 });
117
118 # free allocated images on error
119 if (my $err = $@) {
120 syslog('err', "VM $vmid creating disks failed");
121 foreach my $volid (@$vollist) {
122 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
123 warn $@ if $@;
124 }
125 die $err;
126 }
127
128 # modify vm config if everything went well
129 foreach my $ds (keys %$res) {
130 $conf->{$ds} = $res->{$ds};
131 }
132
133 return $vollist;
134 };
135
136 my $check_vm_modify_config_perm = sub {
137 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
138
139 return 1 if $authuser eq 'root@pam';
140
141 foreach my $opt (@$key_list) {
142 # disk checks need to be done somewhere else
143 next if PVE::QemuServer::valid_drivename($opt);
144
145 if ($opt eq 'sockets' || $opt eq 'cores' ||
146 $opt eq 'cpu' || $opt eq 'smp' ||
147 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
148 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
149 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
150 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
151 } elsif ($opt eq 'memory' || $opt eq 'balloon' || $opt eq 'shares') {
152 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
153 } elsif ($opt eq 'args' || $opt eq 'lock') {
154 die "only root can set '$opt' config\n";
155 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
156 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
157 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
158 } elsif ($opt =~ m/^net\d+$/) {
159 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
160 } else {
161 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
162 }
163 }
164
165 return 1;
166 };
167
168 __PACKAGE__->register_method({
169 name => 'vmlist',
170 path => '',
171 method => 'GET',
172 description => "Virtual machine index (per node).",
173 permissions => {
174 description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
175 user => 'all',
176 },
177 proxyto => 'node',
178 protected => 1, # qemu pid files are only readable by root
179 parameters => {
180 additionalProperties => 0,
181 properties => {
182 node => get_standard_option('pve-node'),
183 },
184 },
185 returns => {
186 type => 'array',
187 items => {
188 type => "object",
189 properties => {},
190 },
191 links => [ { rel => 'child', href => "{vmid}" } ],
192 },
193 code => sub {
194 my ($param) = @_;
195
196 my $rpcenv = PVE::RPCEnvironment::get();
197 my $authuser = $rpcenv->get_user();
198
199 my $vmstatus = PVE::QemuServer::vmstatus();
200
201 my $res = [];
202 foreach my $vmid (keys %$vmstatus) {
203 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
204
205 my $data = $vmstatus->{$vmid};
206 $data->{vmid} = $vmid;
207 push @$res, $data;
208 }
209
210 return $res;
211 }});
212
213 __PACKAGE__->register_method({
214 name => 'create_vm',
215 path => '',
216 method => 'POST',
217 description => "Create or restore a virtual machine.",
218 permissions => {
219 description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. If you create disks you need 'Datastore.AllocateSpace' on any used storage.",
220 check => [ 'or',
221 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
222 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
223 ],
224 },
225 protected => 1,
226 proxyto => 'node',
227 parameters => {
228 additionalProperties => 0,
229 properties => PVE::QemuServer::json_config_properties(
230 {
231 node => get_standard_option('pve-node'),
232 vmid => get_standard_option('pve-vmid'),
233 archive => {
234 description => "The backup file.",
235 type => 'string',
236 optional => 1,
237 maxLength => 255,
238 },
239 storage => get_standard_option('pve-storage-id', {
240 description => "Default storage.",
241 optional => 1,
242 }),
243 force => {
244 optional => 1,
245 type => 'boolean',
246 description => "Allow to overwrite existing VM.",
247 requires => 'archive',
248 },
249 unique => {
250 optional => 1,
251 type => 'boolean',
252 description => "Assign a unique random ethernet address.",
253 requires => 'archive',
254 },
255 pool => {
256 optional => 1,
257 type => 'string', format => 'pve-poolid',
258 description => "Add the VM to the specified pool.",
259 },
260 }),
261 },
262 returns => {
263 type => 'string',
264 },
265 code => sub {
266 my ($param) = @_;
267
268 my $rpcenv = PVE::RPCEnvironment::get();
269
270 my $authuser = $rpcenv->get_user();
271
272 my $node = extract_param($param, 'node');
273
274 my $vmid = extract_param($param, 'vmid');
275
276 my $archive = extract_param($param, 'archive');
277
278 my $storage = extract_param($param, 'storage');
279
280 my $force = extract_param($param, 'force');
281
282 my $unique = extract_param($param, 'unique');
283
284 my $pool = extract_param($param, 'pool');
285
286 my $filename = PVE::QemuServer::config_file($vmid);
287
288 my $storecfg = PVE::Storage::config();
289
290 PVE::Cluster::check_cfs_quorum();
291
292 if (defined($pool)) {
293 $rpcenv->check_pool_exist($pool);
294 }
295
296 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
297 if defined($storage);
298
299 if (!$archive) {
300 &$resolve_cdrom_alias($param);
301
302 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
303
304 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
305
306 foreach my $opt (keys %$param) {
307 if (PVE::QemuServer::valid_drivename($opt)) {
308 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
309 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
310
311 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
312 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
313 }
314 }
315
316 PVE::QemuServer::add_random_macs($param);
317 } else {
318 my $keystr = join(' ', keys %$param);
319 raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
320
321 if ($archive eq '-') {
322 die "pipe requires cli environment\n"
323 if $rpcenv->{type} ne 'cli';
324 } else {
325 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
326
327 PVE::Storage::activate_volumes($storecfg, [ $archive ])
328 if PVE::Storage::parse_volume_id ($archive, 1);
329
330 die "can't find archive file '$archive'\n" if !($path && -f $path);
331 $archive = $path;
332 }
333 }
334
335 my $addVMtoPoolFn = sub {
336 my $usercfg = cfs_read_file("user.cfg");
337 if (my $data = $usercfg->{pools}->{$pool}) {
338 $data->{vms}->{$vmid} = 1;
339 $usercfg->{vms}->{$vmid} = $pool;
340 cfs_write_file("user.cfg", $usercfg);
341 }
342 };
343
344 my $restorefn = sub {
345
346 if (-f $filename) {
347 die "unable to restore vm $vmid: config file already exists\n"
348 if !$force;
349
350 die "unable to restore vm $vmid: vm is running\n"
351 if PVE::QemuServer::check_running($vmid);
352 }
353
354 my $realcmd = sub {
355 PVE::QemuServer::restore_archive($archive, $vmid, $authuser, {
356 storage => $storage,
357 pool => $pool,
358 unique => $unique });
359
360 PVE::AccessControl::lock_user_config($addVMtoPoolFn, "can't add VM to pool") if $pool;
361 };
362
363 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
364 };
365
366 my $createfn = sub {
367
368 # test after locking
369 die "unable to create vm $vmid: config file already exists\n"
370 if -f $filename;
371
372 my $realcmd = sub {
373
374 my $vollist = [];
375
376 my $conf = $param;
377
378 eval {
379
380 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
381
382 # try to be smart about bootdisk
383 my @disks = PVE::QemuServer::disknames();
384 my $firstdisk;
385 foreach my $ds (reverse @disks) {
386 next if !$conf->{$ds};
387 my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds});
388 next if PVE::QemuServer::drive_is_cdrom($disk);
389 $firstdisk = $ds;
390 }
391
392 if (!$conf->{bootdisk} && $firstdisk) {
393 $conf->{bootdisk} = $firstdisk;
394 }
395
396 PVE::QemuServer::update_config_nolock($vmid, $conf);
397
398 };
399 my $err = $@;
400
401 if ($err) {
402 foreach my $volid (@$vollist) {
403 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
404 warn $@ if $@;
405 }
406 die "create failed - $err";
407 }
408
409 PVE::AccessControl::lock_user_config($addVMtoPoolFn, "can't add VM to pool") if $pool;
410 };
411
412 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
413 };
414
415 return PVE::QemuServer::lock_config_full($vmid, 1, $archive ? $restorefn : $createfn);
416 }});
417
418 __PACKAGE__->register_method({
419 name => 'vmdiridx',
420 path => '{vmid}',
421 method => 'GET',
422 proxyto => 'node',
423 description => "Directory index",
424 permissions => {
425 user => 'all',
426 },
427 parameters => {
428 additionalProperties => 0,
429 properties => {
430 node => get_standard_option('pve-node'),
431 vmid => get_standard_option('pve-vmid'),
432 },
433 },
434 returns => {
435 type => 'array',
436 items => {
437 type => "object",
438 properties => {
439 subdir => { type => 'string' },
440 },
441 },
442 links => [ { rel => 'child', href => "{subdir}" } ],
443 },
444 code => sub {
445 my ($param) = @_;
446
447 my $res = [
448 { subdir => 'config' },
449 { subdir => 'status' },
450 { subdir => 'unlink' },
451 { subdir => 'vncproxy' },
452 { subdir => 'migrate' },
453 { subdir => 'resize' },
454 { subdir => 'rrd' },
455 { subdir => 'rrddata' },
456 { subdir => 'monitor' },
457 { subdir => 'snapshot' },
458 ];
459
460 return $res;
461 }});
462
463 __PACKAGE__->register_method({
464 name => 'rrd',
465 path => '{vmid}/rrd',
466 method => 'GET',
467 protected => 1, # fixme: can we avoid that?
468 permissions => {
469 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
470 },
471 description => "Read VM RRD statistics (returns PNG)",
472 parameters => {
473 additionalProperties => 0,
474 properties => {
475 node => get_standard_option('pve-node'),
476 vmid => get_standard_option('pve-vmid'),
477 timeframe => {
478 description => "Specify the time frame you are interested in.",
479 type => 'string',
480 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
481 },
482 ds => {
483 description => "The list of datasources you want to display.",
484 type => 'string', format => 'pve-configid-list',
485 },
486 cf => {
487 description => "The RRD consolidation function",
488 type => 'string',
489 enum => [ 'AVERAGE', 'MAX' ],
490 optional => 1,
491 },
492 },
493 },
494 returns => {
495 type => "object",
496 properties => {
497 filename => { type => 'string' },
498 },
499 },
500 code => sub {
501 my ($param) = @_;
502
503 return PVE::Cluster::create_rrd_graph(
504 "pve2-vm/$param->{vmid}", $param->{timeframe},
505 $param->{ds}, $param->{cf});
506
507 }});
508
509 __PACKAGE__->register_method({
510 name => 'rrddata',
511 path => '{vmid}/rrddata',
512 method => 'GET',
513 protected => 1, # fixme: can we avoid that?
514 permissions => {
515 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
516 },
517 description => "Read VM RRD statistics",
518 parameters => {
519 additionalProperties => 0,
520 properties => {
521 node => get_standard_option('pve-node'),
522 vmid => get_standard_option('pve-vmid'),
523 timeframe => {
524 description => "Specify the time frame you are interested in.",
525 type => 'string',
526 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
527 },
528 cf => {
529 description => "The RRD consolidation function",
530 type => 'string',
531 enum => [ 'AVERAGE', 'MAX' ],
532 optional => 1,
533 },
534 },
535 },
536 returns => {
537 type => "array",
538 items => {
539 type => "object",
540 properties => {},
541 },
542 },
543 code => sub {
544 my ($param) = @_;
545
546 return PVE::Cluster::create_rrd_data(
547 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
548 }});
549
550
551 __PACKAGE__->register_method({
552 name => 'vm_config',
553 path => '{vmid}/config',
554 method => 'GET',
555 proxyto => 'node',
556 description => "Get virtual machine configuration.",
557 permissions => {
558 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
559 },
560 parameters => {
561 additionalProperties => 0,
562 properties => {
563 node => get_standard_option('pve-node'),
564 vmid => get_standard_option('pve-vmid'),
565 },
566 },
567 returns => {
568 type => "object",
569 properties => {
570 digest => {
571 type => 'string',
572 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
573 }
574 },
575 },
576 code => sub {
577 my ($param) = @_;
578
579 my $conf = PVE::QemuServer::load_config($param->{vmid});
580
581 delete $conf->{snapshots};
582
583 return $conf;
584 }});
585
586 my $vm_is_volid_owner = sub {
587 my ($storecfg, $vmid, $volid) =@_;
588
589 if ($volid !~ m|^/|) {
590 my ($path, $owner);
591 eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
592 if ($owner && ($owner == $vmid)) {
593 return 1;
594 }
595 }
596
597 return undef;
598 };
599
600 my $test_deallocate_drive = sub {
601 my ($storecfg, $vmid, $key, $drive, $force) = @_;
602
603 if (!PVE::QemuServer::drive_is_cdrom($drive)) {
604 my $volid = $drive->{file};
605 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
606 if ($force || $key =~ m/^unused/) {
607 my $sid = PVE::Storage::parse_volume_id($volid);
608 return $sid;
609 }
610 }
611 }
612
613 return undef;
614 };
615
616 my $delete_drive = sub {
617 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
618
619 if (!PVE::QemuServer::drive_is_cdrom($drive)) {
620 my $volid = $drive->{file};
621 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
622 if ($force || $key =~ m/^unused/) {
623 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
624 die $@ if $@;
625 } else {
626 PVE::QemuServer::add_unused_volume($conf, $volid, $vmid);
627 }
628 }
629 }
630
631 delete $conf->{$key};
632 };
633
634 my $vmconfig_delete_option = sub {
635 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
636
637 return if !defined($conf->{$opt});
638
639 my $isDisk = PVE::QemuServer::valid_drivename($opt)|| ($opt =~ m/^unused/);
640
641 if ($isDisk) {
642 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
643
644 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
645 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
646 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
647 }
648 }
649
650 my $unplugwarning = "";
651 if($conf->{ostype} && $conf->{ostype} eq 'l26'){
652 $unplugwarning = "<br>verify that you have acpiphp && pci_hotplug modules loaded in your guest VM";
653 }elsif($conf->{ostype} && $conf->{ostype} eq 'l24'){
654 $unplugwarning = "<br>kernel 2.4 don't support hotplug, please disable hotplug in options";
655 }elsif(!$conf->{ostype} || ($conf->{ostype} && $conf->{ostype} eq 'other')){
656 $unplugwarning = "<br>verify that your guest support acpi hotplug";
657 }
658
659 die "error hot-unplug $opt $unplugwarning" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
660
661 PVE::QemuServer::vm_deviceplug(undef, $conf, $vmid, $opt) if $opt eq 'tablet';
662
663 if ($isDisk) {
664 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
665 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
666 } else {
667 delete $conf->{$opt};
668 }
669
670 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
671 };
672
673 my $safe_num_ne = sub {
674 my ($a, $b) = @_;
675
676 return 0 if !defined($a) && !defined($b);
677 return 1 if !defined($a);
678 return 1 if !defined($b);
679
680 return $a != $b;
681 };
682
683 my $vmconfig_update_disk = sub {
684 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
685
686 my $drive = PVE::QemuServer::parse_drive($opt, $value);
687
688 if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom
689 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
690 } else {
691 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
692 }
693
694 if ($conf->{$opt}) {
695
696 if (my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt})) {
697
698 my $media = $drive->{media} || 'disk';
699 my $oldmedia = $old_drive->{media} || 'disk';
700 die "unable to change media type\n" if $media ne $oldmedia;
701
702 if (!PVE::QemuServer::drive_is_cdrom($old_drive) &&
703 ($drive->{file} ne $old_drive->{file})) { # delete old disks
704
705 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
706 $conf = PVE::QemuServer::load_config($vmid); # update/reload
707 }
708
709 if(&$safe_num_ne($drive->{mbps}, $old_drive->{mbps}) ||
710 &$safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) ||
711 &$safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) ||
712 &$safe_num_ne($drive->{iops}, $old_drive->{iops}) ||
713 &$safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) ||
714 &$safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr})) {
715 PVE::QemuServer::qemu_block_set_io_throttle($vmid,"drive-$opt", $drive->{mbps}*1024*1024,
716 $drive->{mbps_rd}*1024*1024, $drive->{mbps_wr}*1024*1024,
717 $drive->{iops}, $drive->{iops_rd}, $drive->{iops_wr})
718 if !PVE::QemuServer::drive_is_cdrom($drive);
719 }
720 }
721 }
722
723 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
724 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
725
726 $conf = PVE::QemuServer::load_config($vmid); # update/reload
727 $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
728
729 if (PVE::QemuServer::drive_is_cdrom($drive)) { # cdrom
730
731 if (PVE::QemuServer::check_running($vmid)) {
732 if ($drive->{file} eq 'none') {
733 PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
734 } else {
735 my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
736 PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt"); #force eject if locked
737 PVE::QemuServer::vm_mon_cmd($vmid, "change",device => "drive-$opt",target => "$path") if $path;
738 }
739 }
740
741 } else { # hotplug new disks
742
743 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
744 }
745 };
746
747 my $vmconfig_update_net = sub {
748 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
749
750 if ($conf->{$opt}) {
751 #if online update, then unplug first
752 die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
753 }
754
755 $conf->{$opt} = $value;
756 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
757 $conf = PVE::QemuServer::load_config($vmid); # update/reload
758
759 my $net = PVE::QemuServer::parse_net($conf->{$opt});
760
761 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net);
762 };
763
764 my $vm_config_perm_list = [
765 'VM.Config.Disk',
766 'VM.Config.CDROM',
767 'VM.Config.CPU',
768 'VM.Config.Memory',
769 'VM.Config.Network',
770 'VM.Config.HWType',
771 'VM.Config.Options',
772 ];
773
774 __PACKAGE__->register_method({
775 name => 'update_vm',
776 path => '{vmid}/config',
777 method => 'PUT',
778 protected => 1,
779 proxyto => 'node',
780 description => "Set virtual machine options.",
781 permissions => {
782 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
783 },
784 parameters => {
785 additionalProperties => 0,
786 properties => PVE::QemuServer::json_config_properties(
787 {
788 node => get_standard_option('pve-node'),
789 vmid => get_standard_option('pve-vmid'),
790 skiplock => get_standard_option('skiplock'),
791 delete => {
792 type => 'string', format => 'pve-configid-list',
793 description => "A list of settings you want to delete.",
794 optional => 1,
795 },
796 force => {
797 type => 'boolean',
798 description => $opt_force_description,
799 optional => 1,
800 requires => 'delete',
801 },
802 digest => {
803 type => 'string',
804 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
805 maxLength => 40,
806 optional => 1,
807 }
808 }),
809 },
810 returns => { type => 'null'},
811 code => sub {
812 my ($param) = @_;
813
814 my $rpcenv = PVE::RPCEnvironment::get();
815
816 my $authuser = $rpcenv->get_user();
817
818 my $node = extract_param($param, 'node');
819
820 my $vmid = extract_param($param, 'vmid');
821
822 my $digest = extract_param($param, 'digest');
823
824 my @paramarr = (); # used for log message
825 foreach my $key (keys %$param) {
826 push @paramarr, "-$key", $param->{$key};
827 }
828
829 my $skiplock = extract_param($param, 'skiplock');
830 raise_param_exc({ skiplock => "Only root may use this option." })
831 if $skiplock && $authuser ne 'root@pam';
832
833 my $delete_str = extract_param($param, 'delete');
834
835 my $force = extract_param($param, 'force');
836
837 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
838
839 my $storecfg = PVE::Storage::config();
840
841 my $defaults = PVE::QemuServer::load_defaults();
842
843 &$resolve_cdrom_alias($param);
844
845 # now try to verify all parameters
846
847 my @delete = ();
848 foreach my $opt (PVE::Tools::split_list($delete_str)) {
849 $opt = 'ide2' if $opt eq 'cdrom';
850 raise_param_exc({ delete => "you can't use '-$opt' and " .
851 "-delete $opt' at the same time" })
852 if defined($param->{$opt});
853
854 if (!PVE::QemuServer::option_exists($opt)) {
855 raise_param_exc({ delete => "unknown option '$opt'" });
856 }
857
858 push @delete, $opt;
859 }
860
861 foreach my $opt (keys %$param) {
862 if (PVE::QemuServer::valid_drivename($opt)) {
863 # cleanup drive path
864 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
865 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
866 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
867 } elsif ($opt =~ m/^net(\d+)$/) {
868 # add macaddr
869 my $net = PVE::QemuServer::parse_net($param->{$opt});
870 $param->{$opt} = PVE::QemuServer::print_net($net);
871 }
872 }
873
874 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
875
876 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
877
878 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
879
880 my $updatefn = sub {
881
882 my $conf = PVE::QemuServer::load_config($vmid);
883
884 die "checksum missmatch (file change by other user?)\n"
885 if $digest && $digest ne $conf->{digest};
886
887 PVE::QemuServer::check_lock($conf) if !$skiplock;
888
889 if ($param->{memory} || defined($param->{balloon})) {
890 my $maxmem = $param->{memory} || $conf->{memory} || $defaults->{memory};
891 my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{balloon};
892
893 die "balloon value too large (must be smaller than assigned memory)\n"
894 if $balloon > $maxmem;
895 }
896
897 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
898
899 foreach my $opt (@delete) { # delete
900 $conf = PVE::QemuServer::load_config($vmid); # update/reload
901 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
902 }
903
904 my $running = PVE::QemuServer::check_running($vmid);
905
906 foreach my $opt (keys %$param) { # add/change
907
908 $conf = PVE::QemuServer::load_config($vmid); # update/reload
909
910 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
911
912 if (PVE::QemuServer::valid_drivename($opt)) {
913
914 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
915 $opt, $param->{$opt}, $force);
916
917 } elsif ($opt =~ m/^net(\d+)$/) { #nics
918
919 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
920 $opt, $param->{$opt});
921
922 } else {
923
924 if($opt eq 'tablet' && $param->{$opt} == 1){
925 PVE::QemuServer::vm_deviceplug(undef, $conf, $vmid, $opt);
926 }elsif($opt eq 'tablet' && $param->{$opt} == 0){
927 PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
928 }
929
930 $conf->{$opt} = $param->{$opt};
931 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
932 }
933 }
934
935 # allow manual ballooning if shares is set to zero
936 if ($running && defined($param->{balloon}) &&
937 defined($conf->{shares}) && ($conf->{shares} == 0)) {
938 my $balloon = $param->{'balloon'} || $conf->{memory} || $defaults->{memory};
939 PVE::QemuServer::vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
940 }
941
942 };
943
944 PVE::QemuServer::lock_config($vmid, $updatefn);
945
946 return undef;
947 }});
948
949
950 __PACKAGE__->register_method({
951 name => 'destroy_vm',
952 path => '{vmid}',
953 method => 'DELETE',
954 protected => 1,
955 proxyto => 'node',
956 description => "Destroy the vm (also delete all used/owned volumes).",
957 permissions => {
958 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
959 },
960 parameters => {
961 additionalProperties => 0,
962 properties => {
963 node => get_standard_option('pve-node'),
964 vmid => get_standard_option('pve-vmid'),
965 skiplock => get_standard_option('skiplock'),
966 },
967 },
968 returns => {
969 type => 'string',
970 },
971 code => sub {
972 my ($param) = @_;
973
974 my $rpcenv = PVE::RPCEnvironment::get();
975
976 my $authuser = $rpcenv->get_user();
977
978 my $vmid = $param->{vmid};
979
980 my $skiplock = $param->{skiplock};
981 raise_param_exc({ skiplock => "Only root may use this option." })
982 if $skiplock && $authuser ne 'root@pam';
983
984 # test if VM exists
985 my $conf = PVE::QemuServer::load_config($vmid);
986
987 my $storecfg = PVE::Storage::config();
988
989 my $delVMfromPoolFn = sub {
990 my $usercfg = cfs_read_file("user.cfg");
991 if (my $pool = $usercfg->{vms}->{$vmid}) {
992 if (my $data = $usercfg->{pools}->{$pool}) {
993 delete $data->{vms}->{$vmid};
994 delete $usercfg->{vms}->{$vmid};
995 cfs_write_file("user.cfg", $usercfg);
996 }
997 }
998 };
999
1000 my $realcmd = sub {
1001 my $upid = shift;
1002
1003 syslog('info', "destroy VM $vmid: $upid\n");
1004
1005 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
1006
1007 PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed");
1008 };
1009
1010 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
1011 }});
1012
1013 __PACKAGE__->register_method({
1014 name => 'unlink',
1015 path => '{vmid}/unlink',
1016 method => 'PUT',
1017 protected => 1,
1018 proxyto => 'node',
1019 description => "Unlink/delete disk images.",
1020 permissions => {
1021 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
1022 },
1023 parameters => {
1024 additionalProperties => 0,
1025 properties => {
1026 node => get_standard_option('pve-node'),
1027 vmid => get_standard_option('pve-vmid'),
1028 idlist => {
1029 type => 'string', format => 'pve-configid-list',
1030 description => "A list of disk IDs you want to delete.",
1031 },
1032 force => {
1033 type => 'boolean',
1034 description => $opt_force_description,
1035 optional => 1,
1036 },
1037 },
1038 },
1039 returns => { type => 'null'},
1040 code => sub {
1041 my ($param) = @_;
1042
1043 $param->{delete} = extract_param($param, 'idlist');
1044
1045 __PACKAGE__->update_vm($param);
1046
1047 return undef;
1048 }});
1049
1050 my $sslcert;
1051
1052 __PACKAGE__->register_method({
1053 name => 'vncproxy',
1054 path => '{vmid}/vncproxy',
1055 method => 'POST',
1056 protected => 1,
1057 permissions => {
1058 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1059 },
1060 description => "Creates a TCP VNC proxy connections.",
1061 parameters => {
1062 additionalProperties => 0,
1063 properties => {
1064 node => get_standard_option('pve-node'),
1065 vmid => get_standard_option('pve-vmid'),
1066 },
1067 },
1068 returns => {
1069 additionalProperties => 0,
1070 properties => {
1071 user => { type => 'string' },
1072 ticket => { type => 'string' },
1073 cert => { type => 'string' },
1074 port => { type => 'integer' },
1075 upid => { type => 'string' },
1076 },
1077 },
1078 code => sub {
1079 my ($param) = @_;
1080
1081 my $rpcenv = PVE::RPCEnvironment::get();
1082
1083 my $authuser = $rpcenv->get_user();
1084
1085 my $vmid = $param->{vmid};
1086 my $node = $param->{node};
1087
1088 my $authpath = "/vms/$vmid";
1089
1090 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1091
1092 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
1093 if !$sslcert;
1094
1095 my $port = PVE::Tools::next_vnc_port();
1096
1097 my $remip;
1098
1099 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1100 $remip = PVE::Cluster::remote_node_ip($node);
1101 }
1102
1103 # NOTE: kvm VNC traffic is already TLS encrypted
1104 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', $remip] : [];
1105
1106 my $timeout = 10;
1107
1108 my $realcmd = sub {
1109 my $upid = shift;
1110
1111 syslog('info', "starting vnc proxy $upid\n");
1112
1113 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1114
1115 my $qmstr = join(' ', @$qmcmd);
1116
1117 # also redirect stderr (else we get RFB protocol errors)
1118 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
1119
1120 PVE::Tools::run_command($cmd);
1121
1122 return;
1123 };
1124
1125 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1126
1127 PVE::Tools::wait_for_vnc_port($port);
1128
1129 return {
1130 user => $authuser,
1131 ticket => $ticket,
1132 port => $port,
1133 upid => $upid,
1134 cert => $sslcert,
1135 };
1136 }});
1137
1138 __PACKAGE__->register_method({
1139 name => 'vmcmdidx',
1140 path => '{vmid}/status',
1141 method => 'GET',
1142 proxyto => 'node',
1143 description => "Directory index",
1144 permissions => {
1145 user => 'all',
1146 },
1147 parameters => {
1148 additionalProperties => 0,
1149 properties => {
1150 node => get_standard_option('pve-node'),
1151 vmid => get_standard_option('pve-vmid'),
1152 },
1153 },
1154 returns => {
1155 type => 'array',
1156 items => {
1157 type => "object",
1158 properties => {
1159 subdir => { type => 'string' },
1160 },
1161 },
1162 links => [ { rel => 'child', href => "{subdir}" } ],
1163 },
1164 code => sub {
1165 my ($param) = @_;
1166
1167 # test if VM exists
1168 my $conf = PVE::QemuServer::load_config($param->{vmid});
1169
1170 my $res = [
1171 { subdir => 'current' },
1172 { subdir => 'start' },
1173 { subdir => 'stop' },
1174 ];
1175
1176 return $res;
1177 }});
1178
1179 my $vm_is_ha_managed = sub {
1180 my ($vmid) = @_;
1181
1182 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1183 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $vmid, 1)) {
1184 return 1;
1185 }
1186 return 0;
1187 };
1188
1189 __PACKAGE__->register_method({
1190 name => 'vm_status',
1191 path => '{vmid}/status/current',
1192 method => 'GET',
1193 proxyto => 'node',
1194 protected => 1, # qemu pid files are only readable by root
1195 description => "Get virtual machine status.",
1196 permissions => {
1197 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1198 },
1199 parameters => {
1200 additionalProperties => 0,
1201 properties => {
1202 node => get_standard_option('pve-node'),
1203 vmid => get_standard_option('pve-vmid'),
1204 },
1205 },
1206 returns => { type => 'object' },
1207 code => sub {
1208 my ($param) = @_;
1209
1210 # test if VM exists
1211 my $conf = PVE::QemuServer::load_config($param->{vmid});
1212
1213 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
1214 my $status = $vmstatus->{$param->{vmid}};
1215
1216 $status->{ha} = &$vm_is_ha_managed($param->{vmid});
1217
1218 return $status;
1219 }});
1220
1221 __PACKAGE__->register_method({
1222 name => 'vm_start',
1223 path => '{vmid}/status/start',
1224 method => 'POST',
1225 protected => 1,
1226 proxyto => 'node',
1227 description => "Start virtual machine.",
1228 permissions => {
1229 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1230 },
1231 parameters => {
1232 additionalProperties => 0,
1233 properties => {
1234 node => get_standard_option('pve-node'),
1235 vmid => get_standard_option('pve-vmid'),
1236 skiplock => get_standard_option('skiplock'),
1237 stateuri => get_standard_option('pve-qm-stateuri'),
1238 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
1239
1240 },
1241 },
1242 returns => {
1243 type => 'string',
1244 },
1245 code => sub {
1246 my ($param) = @_;
1247
1248 my $rpcenv = PVE::RPCEnvironment::get();
1249
1250 my $authuser = $rpcenv->get_user();
1251
1252 my $node = extract_param($param, 'node');
1253
1254 my $vmid = extract_param($param, 'vmid');
1255
1256 my $stateuri = extract_param($param, 'stateuri');
1257 raise_param_exc({ stateuri => "Only root may use this option." })
1258 if $stateuri && $authuser ne 'root@pam';
1259
1260 my $skiplock = extract_param($param, 'skiplock');
1261 raise_param_exc({ skiplock => "Only root may use this option." })
1262 if $skiplock && $authuser ne 'root@pam';
1263
1264 my $migratedfrom = extract_param($param, 'migratedfrom');
1265 raise_param_exc({ migratedfrom => "Only root may use this option." })
1266 if $migratedfrom && $authuser ne 'root@pam';
1267
1268 my $storecfg = PVE::Storage::config();
1269
1270 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1271 $rpcenv->{type} ne 'ha') {
1272
1273 my $hacmd = sub {
1274 my $upid = shift;
1275
1276 my $service = "pvevm:$vmid";
1277
1278 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1279
1280 print "Executing HA start for VM $vmid\n";
1281
1282 PVE::Tools::run_command($cmd);
1283
1284 return;
1285 };
1286
1287 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1288
1289 } else {
1290
1291 my $realcmd = sub {
1292 my $upid = shift;
1293
1294 syslog('info', "start VM $vmid: $upid\n");
1295
1296 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom);
1297
1298 return;
1299 };
1300
1301 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1302 }
1303 }});
1304
1305 __PACKAGE__->register_method({
1306 name => 'vm_stop',
1307 path => '{vmid}/status/stop',
1308 method => 'POST',
1309 protected => 1,
1310 proxyto => 'node',
1311 description => "Stop virtual machine.",
1312 permissions => {
1313 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1314 },
1315 parameters => {
1316 additionalProperties => 0,
1317 properties => {
1318 node => get_standard_option('pve-node'),
1319 vmid => get_standard_option('pve-vmid'),
1320 skiplock => get_standard_option('skiplock'),
1321 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
1322 timeout => {
1323 description => "Wait maximal timeout seconds.",
1324 type => 'integer',
1325 minimum => 0,
1326 optional => 1,
1327 },
1328 keepActive => {
1329 description => "Do not decativate storage volumes.",
1330 type => 'boolean',
1331 optional => 1,
1332 default => 0,
1333 }
1334 },
1335 },
1336 returns => {
1337 type => 'string',
1338 },
1339 code => sub {
1340 my ($param) = @_;
1341
1342 my $rpcenv = PVE::RPCEnvironment::get();
1343
1344 my $authuser = $rpcenv->get_user();
1345
1346 my $node = extract_param($param, 'node');
1347
1348 my $vmid = extract_param($param, 'vmid');
1349
1350 my $skiplock = extract_param($param, 'skiplock');
1351 raise_param_exc({ skiplock => "Only root may use this option." })
1352 if $skiplock && $authuser ne 'root@pam';
1353
1354 my $keepActive = extract_param($param, 'keepActive');
1355 raise_param_exc({ keepActive => "Only root may use this option." })
1356 if $keepActive && $authuser ne 'root@pam';
1357
1358 my $migratedfrom = extract_param($param, 'migratedfrom');
1359 raise_param_exc({ migratedfrom => "Only root may use this option." })
1360 if $migratedfrom && $authuser ne 'root@pam';
1361
1362
1363 my $storecfg = PVE::Storage::config();
1364
1365 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
1366
1367 my $hacmd = sub {
1368 my $upid = shift;
1369
1370 my $service = "pvevm:$vmid";
1371
1372 my $cmd = ['clusvcadm', '-d', $service];
1373
1374 print "Executing HA stop for VM $vmid\n";
1375
1376 PVE::Tools::run_command($cmd);
1377
1378 return;
1379 };
1380
1381 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1382
1383 } else {
1384 my $realcmd = sub {
1385 my $upid = shift;
1386
1387 syslog('info', "stop VM $vmid: $upid\n");
1388
1389 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1390 $param->{timeout}, 0, 1, $keepActive, $migratedfrom);
1391
1392 return;
1393 };
1394
1395 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1396 }
1397 }});
1398
1399 __PACKAGE__->register_method({
1400 name => 'vm_reset',
1401 path => '{vmid}/status/reset',
1402 method => 'POST',
1403 protected => 1,
1404 proxyto => 'node',
1405 description => "Reset virtual machine.",
1406 permissions => {
1407 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1408 },
1409 parameters => {
1410 additionalProperties => 0,
1411 properties => {
1412 node => get_standard_option('pve-node'),
1413 vmid => get_standard_option('pve-vmid'),
1414 skiplock => get_standard_option('skiplock'),
1415 },
1416 },
1417 returns => {
1418 type => 'string',
1419 },
1420 code => sub {
1421 my ($param) = @_;
1422
1423 my $rpcenv = PVE::RPCEnvironment::get();
1424
1425 my $authuser = $rpcenv->get_user();
1426
1427 my $node = extract_param($param, 'node');
1428
1429 my $vmid = extract_param($param, 'vmid');
1430
1431 my $skiplock = extract_param($param, 'skiplock');
1432 raise_param_exc({ skiplock => "Only root may use this option." })
1433 if $skiplock && $authuser ne 'root@pam';
1434
1435 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1436
1437 my $realcmd = sub {
1438 my $upid = shift;
1439
1440 PVE::QemuServer::vm_reset($vmid, $skiplock);
1441
1442 return;
1443 };
1444
1445 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1446 }});
1447
1448 __PACKAGE__->register_method({
1449 name => 'vm_shutdown',
1450 path => '{vmid}/status/shutdown',
1451 method => 'POST',
1452 protected => 1,
1453 proxyto => 'node',
1454 description => "Shutdown virtual machine.",
1455 permissions => {
1456 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1457 },
1458 parameters => {
1459 additionalProperties => 0,
1460 properties => {
1461 node => get_standard_option('pve-node'),
1462 vmid => get_standard_option('pve-vmid'),
1463 skiplock => get_standard_option('skiplock'),
1464 timeout => {
1465 description => "Wait maximal timeout seconds.",
1466 type => 'integer',
1467 minimum => 0,
1468 optional => 1,
1469 },
1470 forceStop => {
1471 description => "Make sure the VM stops.",
1472 type => 'boolean',
1473 optional => 1,
1474 default => 0,
1475 },
1476 keepActive => {
1477 description => "Do not decativate storage volumes.",
1478 type => 'boolean',
1479 optional => 1,
1480 default => 0,
1481 }
1482 },
1483 },
1484 returns => {
1485 type => 'string',
1486 },
1487 code => sub {
1488 my ($param) = @_;
1489
1490 my $rpcenv = PVE::RPCEnvironment::get();
1491
1492 my $authuser = $rpcenv->get_user();
1493
1494 my $node = extract_param($param, 'node');
1495
1496 my $vmid = extract_param($param, 'vmid');
1497
1498 my $skiplock = extract_param($param, 'skiplock');
1499 raise_param_exc({ skiplock => "Only root may use this option." })
1500 if $skiplock && $authuser ne 'root@pam';
1501
1502 my $keepActive = extract_param($param, 'keepActive');
1503 raise_param_exc({ keepActive => "Only root may use this option." })
1504 if $keepActive && $authuser ne 'root@pam';
1505
1506 my $storecfg = PVE::Storage::config();
1507
1508 my $realcmd = sub {
1509 my $upid = shift;
1510
1511 syslog('info', "shutdown VM $vmid: $upid\n");
1512
1513 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1514 1, $param->{forceStop}, $keepActive);
1515
1516 return;
1517 };
1518
1519 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1520 }});
1521
1522 __PACKAGE__->register_method({
1523 name => 'vm_suspend',
1524 path => '{vmid}/status/suspend',
1525 method => 'POST',
1526 protected => 1,
1527 proxyto => 'node',
1528 description => "Suspend virtual machine.",
1529 permissions => {
1530 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1531 },
1532 parameters => {
1533 additionalProperties => 0,
1534 properties => {
1535 node => get_standard_option('pve-node'),
1536 vmid => get_standard_option('pve-vmid'),
1537 skiplock => get_standard_option('skiplock'),
1538 },
1539 },
1540 returns => {
1541 type => 'string',
1542 },
1543 code => sub {
1544 my ($param) = @_;
1545
1546 my $rpcenv = PVE::RPCEnvironment::get();
1547
1548 my $authuser = $rpcenv->get_user();
1549
1550 my $node = extract_param($param, 'node');
1551
1552 my $vmid = extract_param($param, 'vmid');
1553
1554 my $skiplock = extract_param($param, 'skiplock');
1555 raise_param_exc({ skiplock => "Only root may use this option." })
1556 if $skiplock && $authuser ne 'root@pam';
1557
1558 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1559
1560 my $realcmd = sub {
1561 my $upid = shift;
1562
1563 syslog('info', "suspend VM $vmid: $upid\n");
1564
1565 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1566
1567 return;
1568 };
1569
1570 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1571 }});
1572
1573 __PACKAGE__->register_method({
1574 name => 'vm_resume',
1575 path => '{vmid}/status/resume',
1576 method => 'POST',
1577 protected => 1,
1578 proxyto => 'node',
1579 description => "Resume virtual machine.",
1580 permissions => {
1581 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1582 },
1583 parameters => {
1584 additionalProperties => 0,
1585 properties => {
1586 node => get_standard_option('pve-node'),
1587 vmid => get_standard_option('pve-vmid'),
1588 skiplock => get_standard_option('skiplock'),
1589 },
1590 },
1591 returns => {
1592 type => 'string',
1593 },
1594 code => sub {
1595 my ($param) = @_;
1596
1597 my $rpcenv = PVE::RPCEnvironment::get();
1598
1599 my $authuser = $rpcenv->get_user();
1600
1601 my $node = extract_param($param, 'node');
1602
1603 my $vmid = extract_param($param, 'vmid');
1604
1605 my $skiplock = extract_param($param, 'skiplock');
1606 raise_param_exc({ skiplock => "Only root may use this option." })
1607 if $skiplock && $authuser ne 'root@pam';
1608
1609 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1610
1611 my $realcmd = sub {
1612 my $upid = shift;
1613
1614 syslog('info', "resume VM $vmid: $upid\n");
1615
1616 PVE::QemuServer::vm_resume($vmid, $skiplock);
1617
1618 return;
1619 };
1620
1621 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1622 }});
1623
1624 __PACKAGE__->register_method({
1625 name => 'vm_sendkey',
1626 path => '{vmid}/sendkey',
1627 method => 'PUT',
1628 protected => 1,
1629 proxyto => 'node',
1630 description => "Send key event to virtual machine.",
1631 permissions => {
1632 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1633 },
1634 parameters => {
1635 additionalProperties => 0,
1636 properties => {
1637 node => get_standard_option('pve-node'),
1638 vmid => get_standard_option('pve-vmid'),
1639 skiplock => get_standard_option('skiplock'),
1640 key => {
1641 description => "The key (qemu monitor encoding).",
1642 type => 'string'
1643 }
1644 },
1645 },
1646 returns => { type => 'null'},
1647 code => sub {
1648 my ($param) = @_;
1649
1650 my $rpcenv = PVE::RPCEnvironment::get();
1651
1652 my $authuser = $rpcenv->get_user();
1653
1654 my $node = extract_param($param, 'node');
1655
1656 my $vmid = extract_param($param, 'vmid');
1657
1658 my $skiplock = extract_param($param, 'skiplock');
1659 raise_param_exc({ skiplock => "Only root may use this option." })
1660 if $skiplock && $authuser ne 'root@pam';
1661
1662 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1663
1664 return;
1665 }});
1666
1667 __PACKAGE__->register_method({
1668 name => 'vm_feature',
1669 path => '{vmid}/feature',
1670 method => 'GET',
1671 proxyto => 'node',
1672 protected => 1,
1673 description => "Check if feature for virtual machine is available.",
1674 permissions => {
1675 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1676 },
1677 parameters => {
1678 additionalProperties => 0,
1679 properties => {
1680 node => get_standard_option('pve-node'),
1681 vmid => get_standard_option('pve-vmid'),
1682 feature => {
1683 description => "Feature to check.",
1684 type => 'string',
1685 enum => [ 'snapshot', 'clone' ],
1686 },
1687 snapname => get_standard_option('pve-snapshot-name', {
1688 optional => 1,
1689 }),
1690 },
1691
1692 },
1693 returns => {
1694 type => 'boolean'
1695 },
1696 code => sub {
1697 my ($param) = @_;
1698
1699 my $node = extract_param($param, 'node');
1700
1701 my $vmid = extract_param($param, 'vmid');
1702
1703 my $snapname = extract_param($param, 'snapname');
1704
1705 my $feature = extract_param($param, 'feature');
1706
1707 my $running = PVE::QemuServer::check_running($vmid);
1708
1709 my $conf = PVE::QemuServer::load_config($vmid);
1710
1711 if($snapname){
1712 my $snap = $conf->{snapshots}->{$snapname};
1713 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1714 $conf = $snap;
1715 }
1716 my $storecfg = PVE::Storage::config();
1717
1718 my $hasfeature = PVE::QemuServer::has_feature($feature, $conf, $storecfg, $snapname, $running);
1719 my $res = $hasfeature ? 1 : 0 ;
1720 return $res;
1721 }});
1722
1723 __PACKAGE__->register_method({
1724 name => 'migrate_vm',
1725 path => '{vmid}/migrate',
1726 method => 'POST',
1727 protected => 1,
1728 proxyto => 'node',
1729 description => "Migrate virtual machine. Creates a new migration task.",
1730 permissions => {
1731 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1732 },
1733 parameters => {
1734 additionalProperties => 0,
1735 properties => {
1736 node => get_standard_option('pve-node'),
1737 vmid => get_standard_option('pve-vmid'),
1738 target => get_standard_option('pve-node', { description => "Target node." }),
1739 online => {
1740 type => 'boolean',
1741 description => "Use online/live migration.",
1742 optional => 1,
1743 },
1744 force => {
1745 type => 'boolean',
1746 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
1747 optional => 1,
1748 },
1749 },
1750 },
1751 returns => {
1752 type => 'string',
1753 description => "the task ID.",
1754 },
1755 code => sub {
1756 my ($param) = @_;
1757
1758 my $rpcenv = PVE::RPCEnvironment::get();
1759
1760 my $authuser = $rpcenv->get_user();
1761
1762 my $target = extract_param($param, 'target');
1763
1764 my $localnode = PVE::INotify::nodename();
1765 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
1766
1767 PVE::Cluster::check_cfs_quorum();
1768
1769 PVE::Cluster::check_node_exists($target);
1770
1771 my $targetip = PVE::Cluster::remote_node_ip($target);
1772
1773 my $vmid = extract_param($param, 'vmid');
1774
1775 raise_param_exc({ force => "Only root may use this option." })
1776 if $param->{force} && $authuser ne 'root@pam';
1777
1778 # test if VM exists
1779 my $conf = PVE::QemuServer::load_config($vmid);
1780
1781 # try to detect errors early
1782
1783 PVE::QemuServer::check_lock($conf);
1784
1785 if (PVE::QemuServer::check_running($vmid)) {
1786 die "cant migrate running VM without --online\n"
1787 if !$param->{online};
1788 }
1789
1790 my $storecfg = PVE::Storage::config();
1791 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target);
1792
1793 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
1794
1795 my $hacmd = sub {
1796 my $upid = shift;
1797
1798 my $service = "pvevm:$vmid";
1799
1800 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1801
1802 print "Executing HA migrate for VM $vmid to node $target\n";
1803
1804 PVE::Tools::run_command($cmd);
1805
1806 return;
1807 };
1808
1809 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1810
1811 } else {
1812
1813 my $realcmd = sub {
1814 my $upid = shift;
1815
1816 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1817 };
1818
1819 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1820 }
1821
1822 }});
1823
1824 __PACKAGE__->register_method({
1825 name => 'monitor',
1826 path => '{vmid}/monitor',
1827 method => 'POST',
1828 protected => 1,
1829 proxyto => 'node',
1830 description => "Execute Qemu monitor commands.",
1831 permissions => {
1832 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1833 },
1834 parameters => {
1835 additionalProperties => 0,
1836 properties => {
1837 node => get_standard_option('pve-node'),
1838 vmid => get_standard_option('pve-vmid'),
1839 command => {
1840 type => 'string',
1841 description => "The monitor command.",
1842 }
1843 },
1844 },
1845 returns => { type => 'string'},
1846 code => sub {
1847 my ($param) = @_;
1848
1849 my $vmid = $param->{vmid};
1850
1851 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1852
1853 my $res = '';
1854 eval {
1855 $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command});
1856 };
1857 $res = "ERROR: $@" if $@;
1858
1859 return $res;
1860 }});
1861
1862 __PACKAGE__->register_method({
1863 name => 'resize_vm',
1864 path => '{vmid}/resize',
1865 method => 'PUT',
1866 protected => 1,
1867 proxyto => 'node',
1868 description => "Extend volume size.",
1869 permissions => {
1870 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
1871 },
1872 parameters => {
1873 additionalProperties => 0,
1874 properties => {
1875 node => get_standard_option('pve-node'),
1876 vmid => get_standard_option('pve-vmid'),
1877 skiplock => get_standard_option('skiplock'),
1878 disk => {
1879 type => 'string',
1880 description => "The disk you want to resize.",
1881 enum => [PVE::QemuServer::disknames()],
1882 },
1883 size => {
1884 type => 'string',
1885 pattern => '\+?\d+(\.\d+)?[KMGT]?',
1886 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.",
1887 },
1888 digest => {
1889 type => 'string',
1890 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1891 maxLength => 40,
1892 optional => 1,
1893 },
1894 },
1895 },
1896 returns => { type => 'null'},
1897 code => sub {
1898 my ($param) = @_;
1899
1900 my $rpcenv = PVE::RPCEnvironment::get();
1901
1902 my $authuser = $rpcenv->get_user();
1903
1904 my $node = extract_param($param, 'node');
1905
1906 my $vmid = extract_param($param, 'vmid');
1907
1908 my $digest = extract_param($param, 'digest');
1909
1910 my $disk = extract_param($param, 'disk');
1911
1912 my $sizestr = extract_param($param, 'size');
1913
1914 my $skiplock = extract_param($param, 'skiplock');
1915 raise_param_exc({ skiplock => "Only root may use this option." })
1916 if $skiplock && $authuser ne 'root@pam';
1917
1918 my $storecfg = PVE::Storage::config();
1919
1920 my $updatefn = sub {
1921
1922 my $conf = PVE::QemuServer::load_config($vmid);
1923
1924 die "checksum missmatch (file change by other user?)\n"
1925 if $digest && $digest ne $conf->{digest};
1926 PVE::QemuServer::check_lock($conf) if !$skiplock;
1927
1928 die "disk '$disk' does not exist\n" if !$conf->{$disk};
1929
1930 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
1931
1932 my $volid = $drive->{file};
1933
1934 die "disk '$disk' has no associated volume\n" if !$volid;
1935
1936 die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
1937
1938 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
1939
1940 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
1941
1942 my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
1943
1944 die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
1945 my ($ext, $newsize, $unit) = ($1, $2, $4);
1946 if ($unit) {
1947 if ($unit eq 'K') {
1948 $newsize = $newsize * 1024;
1949 } elsif ($unit eq 'M') {
1950 $newsize = $newsize * 1024 * 1024;
1951 } elsif ($unit eq 'G') {
1952 $newsize = $newsize * 1024 * 1024 * 1024;
1953 } elsif ($unit eq 'T') {
1954 $newsize = $newsize * 1024 * 1024 * 1024 * 1024;
1955 }
1956 }
1957 $newsize += $size if $ext;
1958 $newsize = int($newsize);
1959
1960 die "unable to skrink disk size\n" if $newsize < $size;
1961
1962 return if $size == $newsize;
1963
1964 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr");
1965
1966 PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize);
1967
1968 $drive->{size} = $newsize;
1969 $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive);
1970
1971 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
1972 };
1973
1974 PVE::QemuServer::lock_config($vmid, $updatefn);
1975 return undef;
1976 }});
1977
1978 __PACKAGE__->register_method({
1979 name => 'snapshot_list',
1980 path => '{vmid}/snapshot',
1981 method => 'GET',
1982 description => "List all snapshots.",
1983 permissions => {
1984 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1985 },
1986 proxyto => 'node',
1987 protected => 1, # qemu pid files are only readable by root
1988 parameters => {
1989 additionalProperties => 0,
1990 properties => {
1991 vmid => get_standard_option('pve-vmid'),
1992 node => get_standard_option('pve-node'),
1993 },
1994 },
1995 returns => {
1996 type => 'array',
1997 items => {
1998 type => "object",
1999 properties => {},
2000 },
2001 links => [ { rel => 'child', href => "{name}" } ],
2002 },
2003 code => sub {
2004 my ($param) = @_;
2005
2006 my $vmid = $param->{vmid};
2007
2008 my $conf = PVE::QemuServer::load_config($vmid);
2009 my $snaphash = $conf->{snapshots} || {};
2010
2011 my $res = [];
2012
2013 foreach my $name (keys %$snaphash) {
2014 my $d = $snaphash->{$name};
2015 my $item = {
2016 name => $name,
2017 snaptime => $d->{snaptime} || 0,
2018 vmstate => $d->{vmstate} ? 1 : 0,
2019 description => $d->{description} || '',
2020 };
2021 $item->{parent} = $d->{parent} if $d->{parent};
2022 $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
2023 push @$res, $item;
2024 }
2025
2026 my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
2027 my $current = { name => 'current', digest => $conf->{digest}, running => $running };
2028 $current->{parent} = $conf->{parent} if $conf->{parent};
2029
2030 push @$res, $current;
2031
2032 return $res;
2033 }});
2034
2035 __PACKAGE__->register_method({
2036 name => 'snapshot',
2037 path => '{vmid}/snapshot',
2038 method => 'POST',
2039 protected => 1,
2040 proxyto => 'node',
2041 description => "Snapshot a VM.",
2042 permissions => {
2043 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2044 },
2045 parameters => {
2046 additionalProperties => 0,
2047 properties => {
2048 node => get_standard_option('pve-node'),
2049 vmid => get_standard_option('pve-vmid'),
2050 snapname => get_standard_option('pve-snapshot-name'),
2051 vmstate => {
2052 optional => 1,
2053 type => 'boolean',
2054 description => "Save the vmstate",
2055 },
2056 freezefs => {
2057 optional => 1,
2058 type => 'boolean',
2059 description => "Freeze the filesystem",
2060 },
2061 description => {
2062 optional => 1,
2063 type => 'string',
2064 description => "A textual description or comment.",
2065 },
2066 },
2067 },
2068 returns => {
2069 type => 'string',
2070 description => "the task ID.",
2071 },
2072 code => sub {
2073 my ($param) = @_;
2074
2075 my $rpcenv = PVE::RPCEnvironment::get();
2076
2077 my $authuser = $rpcenv->get_user();
2078
2079 my $node = extract_param($param, 'node');
2080
2081 my $vmid = extract_param($param, 'vmid');
2082
2083 my $snapname = extract_param($param, 'snapname');
2084
2085 die "unable to use snapshot name 'current' (reserved name)\n"
2086 if $snapname eq 'current';
2087
2088 my $realcmd = sub {
2089 PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
2090 PVE::QemuServer::snapshot_create($vmid, $snapname, $param->{vmstate},
2091 $param->{freezefs}, $param->{description});
2092 };
2093
2094 return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd);
2095 }});
2096
2097 __PACKAGE__->register_method({
2098 name => 'snapshot_cmd_idx',
2099 path => '{vmid}/snapshot/{snapname}',
2100 description => '',
2101 method => 'GET',
2102 permissions => {
2103 user => 'all',
2104 },
2105 parameters => {
2106 additionalProperties => 0,
2107 properties => {
2108 vmid => get_standard_option('pve-vmid'),
2109 node => get_standard_option('pve-node'),
2110 snapname => get_standard_option('pve-snapshot-name'),
2111 },
2112 },
2113 returns => {
2114 type => 'array',
2115 items => {
2116 type => "object",
2117 properties => {},
2118 },
2119 links => [ { rel => 'child', href => "{cmd}" } ],
2120 },
2121 code => sub {
2122 my ($param) = @_;
2123
2124 my $res = [];
2125
2126 push @$res, { cmd => 'rollback' };
2127 push @$res, { cmd => 'config' };
2128
2129 return $res;
2130 }});
2131
2132 __PACKAGE__->register_method({
2133 name => 'update_snapshot_config',
2134 path => '{vmid}/snapshot/{snapname}/config',
2135 method => 'PUT',
2136 protected => 1,
2137 proxyto => 'node',
2138 description => "Update snapshot metadata.",
2139 permissions => {
2140 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2141 },
2142 parameters => {
2143 additionalProperties => 0,
2144 properties => {
2145 node => get_standard_option('pve-node'),
2146 vmid => get_standard_option('pve-vmid'),
2147 snapname => get_standard_option('pve-snapshot-name'),
2148 description => {
2149 optional => 1,
2150 type => 'string',
2151 description => "A textual description or comment.",
2152 },
2153 },
2154 },
2155 returns => { type => 'null' },
2156 code => sub {
2157 my ($param) = @_;
2158
2159 my $rpcenv = PVE::RPCEnvironment::get();
2160
2161 my $authuser = $rpcenv->get_user();
2162
2163 my $vmid = extract_param($param, 'vmid');
2164
2165 my $snapname = extract_param($param, 'snapname');
2166
2167 return undef if !defined($param->{description});
2168
2169 my $updatefn = sub {
2170
2171 my $conf = PVE::QemuServer::load_config($vmid);
2172
2173 PVE::QemuServer::check_lock($conf);
2174
2175 my $snap = $conf->{snapshots}->{$snapname};
2176
2177 die "snapshot '$snapname' does not exist\n" if !defined($snap);
2178
2179 $snap->{description} = $param->{description} if defined($param->{description});
2180
2181 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
2182 };
2183
2184 PVE::QemuServer::lock_config($vmid, $updatefn);
2185
2186 return undef;
2187 }});
2188
2189 __PACKAGE__->register_method({
2190 name => 'get_snapshot_config',
2191 path => '{vmid}/snapshot/{snapname}/config',
2192 method => 'GET',
2193 proxyto => 'node',
2194 description => "Get snapshot configuration",
2195 permissions => {
2196 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2197 },
2198 parameters => {
2199 additionalProperties => 0,
2200 properties => {
2201 node => get_standard_option('pve-node'),
2202 vmid => get_standard_option('pve-vmid'),
2203 snapname => get_standard_option('pve-snapshot-name'),
2204 },
2205 },
2206 returns => { type => "object" },
2207 code => sub {
2208 my ($param) = @_;
2209
2210 my $rpcenv = PVE::RPCEnvironment::get();
2211
2212 my $authuser = $rpcenv->get_user();
2213
2214 my $vmid = extract_param($param, 'vmid');
2215
2216 my $snapname = extract_param($param, 'snapname');
2217
2218 my $conf = PVE::QemuServer::load_config($vmid);
2219
2220 my $snap = $conf->{snapshots}->{$snapname};
2221
2222 die "snapshot '$snapname' does not exist\n" if !defined($snap);
2223
2224 return $snap;
2225 }});
2226
2227 __PACKAGE__->register_method({
2228 name => 'rollback',
2229 path => '{vmid}/snapshot/{snapname}/rollback',
2230 method => 'POST',
2231 protected => 1,
2232 proxyto => 'node',
2233 description => "Rollback VM state to specified snapshot.",
2234 permissions => {
2235 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2236 },
2237 parameters => {
2238 additionalProperties => 0,
2239 properties => {
2240 node => get_standard_option('pve-node'),
2241 vmid => get_standard_option('pve-vmid'),
2242 snapname => get_standard_option('pve-snapshot-name'),
2243 },
2244 },
2245 returns => {
2246 type => 'string',
2247 description => "the task ID.",
2248 },
2249 code => sub {
2250 my ($param) = @_;
2251
2252 my $rpcenv = PVE::RPCEnvironment::get();
2253
2254 my $authuser = $rpcenv->get_user();
2255
2256 my $node = extract_param($param, 'node');
2257
2258 my $vmid = extract_param($param, 'vmid');
2259
2260 my $snapname = extract_param($param, 'snapname');
2261
2262 my $realcmd = sub {
2263 PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname");
2264 PVE::QemuServer::snapshot_rollback($vmid, $snapname);
2265 };
2266
2267 return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd);
2268 }});
2269
2270 __PACKAGE__->register_method({
2271 name => 'delsnapshot',
2272 path => '{vmid}/snapshot/{snapname}',
2273 method => 'DELETE',
2274 protected => 1,
2275 proxyto => 'node',
2276 description => "Delete a VM snapshot.",
2277 permissions => {
2278 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2279 },
2280 parameters => {
2281 additionalProperties => 0,
2282 properties => {
2283 node => get_standard_option('pve-node'),
2284 vmid => get_standard_option('pve-vmid'),
2285 snapname => get_standard_option('pve-snapshot-name'),
2286 force => {
2287 optional => 1,
2288 type => 'boolean',
2289 description => "For removal from config file, even if removing disk snapshots fails.",
2290 },
2291 },
2292 },
2293 returns => {
2294 type => 'string',
2295 description => "the task ID.",
2296 },
2297 code => sub {
2298 my ($param) = @_;
2299
2300 my $rpcenv = PVE::RPCEnvironment::get();
2301
2302 my $authuser = $rpcenv->get_user();
2303
2304 my $node = extract_param($param, 'node');
2305
2306 my $vmid = extract_param($param, 'vmid');
2307
2308 my $snapname = extract_param($param, 'snapname');
2309
2310 my $realcmd = sub {
2311 PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
2312 PVE::QemuServer::snapshot_delete($vmid, $snapname, $param->{force});
2313 };
2314
2315 return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);
2316 }});
2317
2318 __PACKAGE__->register_method({
2319 name => 'template',
2320 path => '{vmid}/template',
2321 method => 'POST',
2322 protected => 1,
2323 proxyto => 'node',
2324 description => "Create a Template.",
2325 parameters => {
2326 additionalProperties => 0,
2327 properties => {
2328 node => get_standard_option('pve-node'),
2329 vmid => get_standard_option('pve-vmid'),
2330 disk => {
2331 optional => 1,
2332 type => 'string',
2333 description => "If you want to convert only 1 disk to base image.",
2334 enum => [PVE::QemuServer::disknames()],
2335 },
2336
2337 },
2338 },
2339 returns => { type => 'null'},
2340 code => sub {
2341 my ($param) = @_;
2342
2343 my $rpcenv = PVE::RPCEnvironment::get();
2344
2345 my $authuser = $rpcenv->get_user();
2346
2347 my $node = extract_param($param, 'node');
2348
2349 my $vmid = extract_param($param, 'vmid');
2350
2351 my $disk = extract_param($param, 'disk');
2352
2353 my $updatefn = sub {
2354
2355 my $conf = PVE::QemuServer::load_config($vmid);
2356
2357 PVE::QemuServer::check_lock($conf);
2358
2359 die "you can't convert a template to a template"
2360 if PVE::QemuServer::is_template($conf) && !$disk;
2361 my $realcmd = sub {
2362 PVE::QemuServer::template_create($vmid, $conf, $disk);
2363 };
2364 return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
2365
2366 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
2367 };
2368
2369 PVE::QemuServer::lock_config($vmid, $updatefn);
2370 return undef;
2371 }});
2372
2373
2374
2375 1;