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