]> git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu.pm
remove wrong permission check
[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 die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
651
652 if ($isDisk) {
653 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
654 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
655 } else {
656 delete $conf->{$opt};
657 }
658
659 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
660 };
661
662 my $safe_num_ne = sub {
663 my ($a, $b) = @_;
664
665 return 0 if !defined($a) && !defined($b);
666 return 1 if !defined($a);
667 return 1 if !defined($b);
668
669 return $a != $b;
670 };
671
672 my $vmconfig_update_disk = sub {
673 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
674
675 my $drive = PVE::QemuServer::parse_drive($opt, $value);
676
677 if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom
678 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
679 } else {
680 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
681 }
682
683 if ($conf->{$opt}) {
684
685 if (my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt})) {
686
687 my $media = $drive->{media} || 'disk';
688 my $oldmedia = $old_drive->{media} || 'disk';
689 die "unable to change media type\n" if $media ne $oldmedia;
690
691 if (!PVE::QemuServer::drive_is_cdrom($old_drive) &&
692 ($drive->{file} ne $old_drive->{file})) { # delete old disks
693
694 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
695 $conf = PVE::QemuServer::load_config($vmid); # update/reload
696 }
697
698 if(&$safe_num_ne($drive->{mbps}, $old_drive->{mbps}) ||
699 &$safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) ||
700 &$safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) ||
701 &$safe_num_ne($drive->{iops}, $old_drive->{iops}) ||
702 &$safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) ||
703 &$safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr})) {
704 PVE::QemuServer::qemu_block_set_io_throttle($vmid,"drive-$opt", $drive->{mbps}*1024*1024,
705 $drive->{mbps_rd}*1024*1024, $drive->{mbps_wr}*1024*1024,
706 $drive->{iops}, $drive->{iops_rd}, $drive->{iops_wr})
707 if !PVE::QemuServer::drive_is_cdrom($drive);
708 }
709 }
710 }
711
712 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
713 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
714
715 $conf = PVE::QemuServer::load_config($vmid); # update/reload
716 $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
717
718 if (PVE::QemuServer::drive_is_cdrom($drive)) { # cdrom
719
720 if (PVE::QemuServer::check_running($vmid)) {
721 if ($drive->{file} eq 'none') {
722 PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
723 } else {
724 my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
725 PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt"); #force eject if locked
726 PVE::QemuServer::vm_mon_cmd($vmid, "change",device => "drive-$opt",target => "$path") if $path;
727 }
728 }
729
730 } else { # hotplug new disks
731
732 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
733 }
734 };
735
736 my $vmconfig_update_net = sub {
737 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
738
739 if ($conf->{$opt}) {
740 #if online update, then unplug first
741 die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
742 }
743
744 $conf->{$opt} = $value;
745 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
746 $conf = PVE::QemuServer::load_config($vmid); # update/reload
747
748 my $net = PVE::QemuServer::parse_net($conf->{$opt});
749
750 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net);
751 };
752
753 my $vm_config_perm_list = [
754 'VM.Config.Disk',
755 'VM.Config.CDROM',
756 'VM.Config.CPU',
757 'VM.Config.Memory',
758 'VM.Config.Network',
759 'VM.Config.HWType',
760 'VM.Config.Options',
761 ];
762
763 __PACKAGE__->register_method({
764 name => 'update_vm',
765 path => '{vmid}/config',
766 method => 'PUT',
767 protected => 1,
768 proxyto => 'node',
769 description => "Set virtual machine options.",
770 permissions => {
771 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
772 },
773 parameters => {
774 additionalProperties => 0,
775 properties => PVE::QemuServer::json_config_properties(
776 {
777 node => get_standard_option('pve-node'),
778 vmid => get_standard_option('pve-vmid'),
779 skiplock => get_standard_option('skiplock'),
780 delete => {
781 type => 'string', format => 'pve-configid-list',
782 description => "A list of settings you want to delete.",
783 optional => 1,
784 },
785 force => {
786 type => 'boolean',
787 description => $opt_force_description,
788 optional => 1,
789 requires => 'delete',
790 },
791 digest => {
792 type => 'string',
793 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
794 maxLength => 40,
795 optional => 1,
796 }
797 }),
798 },
799 returns => { type => 'null'},
800 code => sub {
801 my ($param) = @_;
802
803 my $rpcenv = PVE::RPCEnvironment::get();
804
805 my $authuser = $rpcenv->get_user();
806
807 my $node = extract_param($param, 'node');
808
809 my $vmid = extract_param($param, 'vmid');
810
811 my $digest = extract_param($param, 'digest');
812
813 my @paramarr = (); # used for log message
814 foreach my $key (keys %$param) {
815 push @paramarr, "-$key", $param->{$key};
816 }
817
818 my $skiplock = extract_param($param, 'skiplock');
819 raise_param_exc({ skiplock => "Only root may use this option." })
820 if $skiplock && $authuser ne 'root@pam';
821
822 my $delete_str = extract_param($param, 'delete');
823
824 my $force = extract_param($param, 'force');
825
826 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
827
828 my $storecfg = PVE::Storage::config();
829
830 my $defaults = PVE::QemuServer::load_defaults();
831
832 &$resolve_cdrom_alias($param);
833
834 # now try to verify all parameters
835
836 my @delete = ();
837 foreach my $opt (PVE::Tools::split_list($delete_str)) {
838 $opt = 'ide2' if $opt eq 'cdrom';
839 raise_param_exc({ delete => "you can't use '-$opt' and " .
840 "-delete $opt' at the same time" })
841 if defined($param->{$opt});
842
843 if (!PVE::QemuServer::option_exists($opt)) {
844 raise_param_exc({ delete => "unknown option '$opt'" });
845 }
846
847 push @delete, $opt;
848 }
849
850 foreach my $opt (keys %$param) {
851 if (PVE::QemuServer::valid_drivename($opt)) {
852 # cleanup drive path
853 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
854 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
855 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
856 } elsif ($opt =~ m/^net(\d+)$/) {
857 # add macaddr
858 my $net = PVE::QemuServer::parse_net($param->{$opt});
859 $param->{$opt} = PVE::QemuServer::print_net($net);
860 }
861 }
862
863 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
864
865 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
866
867 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
868
869 my $updatefn = sub {
870
871 my $conf = PVE::QemuServer::load_config($vmid);
872
873 die "checksum missmatch (file change by other user?)\n"
874 if $digest && $digest ne $conf->{digest};
875
876 PVE::QemuServer::check_lock($conf) if !$skiplock;
877
878 if ($param->{memory} || defined($param->{balloon})) {
879 my $maxmem = $param->{memory} || $conf->{memory} || $defaults->{memory};
880 my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{balloon};
881
882 die "balloon value too large (must be smaller than assigned memory)\n"
883 if $balloon > $maxmem;
884 }
885
886 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
887
888 foreach my $opt (@delete) { # delete
889 $conf = PVE::QemuServer::load_config($vmid); # update/reload
890 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
891 }
892
893 my $running = PVE::QemuServer::check_running($vmid);
894
895 foreach my $opt (keys %$param) { # add/change
896
897 $conf = PVE::QemuServer::load_config($vmid); # update/reload
898
899 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
900
901 if (PVE::QemuServer::valid_drivename($opt)) {
902
903 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
904 $opt, $param->{$opt}, $force);
905
906 } elsif ($opt =~ m/^net(\d+)$/) { #nics
907
908 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
909 $opt, $param->{$opt});
910
911 } else {
912
913 $conf->{$opt} = $param->{$opt};
914 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
915 }
916 }
917
918 # allow manual ballooning if shares is set to zero
919 if ($running && defined($param->{balloon}) &&
920 defined($conf->{shares}) && ($conf->{shares} == 0)) {
921 my $balloon = $param->{'balloon'} || $conf->{memory} || $defaults->{memory};
922 PVE::QemuServer::vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
923 }
924
925 };
926
927 PVE::QemuServer::lock_config($vmid, $updatefn);
928
929 return undef;
930 }});
931
932
933 __PACKAGE__->register_method({
934 name => 'destroy_vm',
935 path => '{vmid}',
936 method => 'DELETE',
937 protected => 1,
938 proxyto => 'node',
939 description => "Destroy the vm (also delete all used/owned volumes).",
940 permissions => {
941 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
942 },
943 parameters => {
944 additionalProperties => 0,
945 properties => {
946 node => get_standard_option('pve-node'),
947 vmid => get_standard_option('pve-vmid'),
948 skiplock => get_standard_option('skiplock'),
949 },
950 },
951 returns => {
952 type => 'string',
953 },
954 code => sub {
955 my ($param) = @_;
956
957 my $rpcenv = PVE::RPCEnvironment::get();
958
959 my $authuser = $rpcenv->get_user();
960
961 my $vmid = $param->{vmid};
962
963 my $skiplock = $param->{skiplock};
964 raise_param_exc({ skiplock => "Only root may use this option." })
965 if $skiplock && $authuser ne 'root@pam';
966
967 # test if VM exists
968 my $conf = PVE::QemuServer::load_config($vmid);
969
970 my $storecfg = PVE::Storage::config();
971
972 my $delVMfromPoolFn = sub {
973 my $usercfg = cfs_read_file("user.cfg");
974 if (my $pool = $usercfg->{vms}->{$vmid}) {
975 if (my $data = $usercfg->{pools}->{$pool}) {
976 delete $data->{vms}->{$vmid};
977 delete $usercfg->{vms}->{$vmid};
978 cfs_write_file("user.cfg", $usercfg);
979 }
980 }
981 };
982
983 my $realcmd = sub {
984 my $upid = shift;
985
986 syslog('info', "destroy VM $vmid: $upid\n");
987
988 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
989
990 PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed");
991 };
992
993 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
994 }});
995
996 __PACKAGE__->register_method({
997 name => 'unlink',
998 path => '{vmid}/unlink',
999 method => 'PUT',
1000 protected => 1,
1001 proxyto => 'node',
1002 description => "Unlink/delete disk images.",
1003 permissions => {
1004 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
1005 },
1006 parameters => {
1007 additionalProperties => 0,
1008 properties => {
1009 node => get_standard_option('pve-node'),
1010 vmid => get_standard_option('pve-vmid'),
1011 idlist => {
1012 type => 'string', format => 'pve-configid-list',
1013 description => "A list of disk IDs you want to delete.",
1014 },
1015 force => {
1016 type => 'boolean',
1017 description => $opt_force_description,
1018 optional => 1,
1019 },
1020 },
1021 },
1022 returns => { type => 'null'},
1023 code => sub {
1024 my ($param) = @_;
1025
1026 $param->{delete} = extract_param($param, 'idlist');
1027
1028 __PACKAGE__->update_vm($param);
1029
1030 return undef;
1031 }});
1032
1033 my $sslcert;
1034
1035 __PACKAGE__->register_method({
1036 name => 'vncproxy',
1037 path => '{vmid}/vncproxy',
1038 method => 'POST',
1039 protected => 1,
1040 permissions => {
1041 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1042 },
1043 description => "Creates a TCP VNC proxy connections.",
1044 parameters => {
1045 additionalProperties => 0,
1046 properties => {
1047 node => get_standard_option('pve-node'),
1048 vmid => get_standard_option('pve-vmid'),
1049 },
1050 },
1051 returns => {
1052 additionalProperties => 0,
1053 properties => {
1054 user => { type => 'string' },
1055 ticket => { type => 'string' },
1056 cert => { type => 'string' },
1057 port => { type => 'integer' },
1058 upid => { type => 'string' },
1059 },
1060 },
1061 code => sub {
1062 my ($param) = @_;
1063
1064 my $rpcenv = PVE::RPCEnvironment::get();
1065
1066 my $authuser = $rpcenv->get_user();
1067
1068 my $vmid = $param->{vmid};
1069 my $node = $param->{node};
1070
1071 my $authpath = "/vms/$vmid";
1072
1073 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1074
1075 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
1076 if !$sslcert;
1077
1078 my $port = PVE::Tools::next_vnc_port();
1079
1080 my $remip;
1081
1082 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1083 $remip = PVE::Cluster::remote_node_ip($node);
1084 }
1085
1086 # NOTE: kvm VNC traffic is already TLS encrypted
1087 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', $remip] : [];
1088
1089 my $timeout = 10;
1090
1091 my $realcmd = sub {
1092 my $upid = shift;
1093
1094 syslog('info', "starting vnc proxy $upid\n");
1095
1096 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1097
1098 my $qmstr = join(' ', @$qmcmd);
1099
1100 # also redirect stderr (else we get RFB protocol errors)
1101 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
1102
1103 PVE::Tools::run_command($cmd);
1104
1105 return;
1106 };
1107
1108 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1109
1110 PVE::Tools::wait_for_vnc_port($port);
1111
1112 return {
1113 user => $authuser,
1114 ticket => $ticket,
1115 port => $port,
1116 upid => $upid,
1117 cert => $sslcert,
1118 };
1119 }});
1120
1121 __PACKAGE__->register_method({
1122 name => 'vmcmdidx',
1123 path => '{vmid}/status',
1124 method => 'GET',
1125 proxyto => 'node',
1126 description => "Directory index",
1127 permissions => {
1128 user => 'all',
1129 },
1130 parameters => {
1131 additionalProperties => 0,
1132 properties => {
1133 node => get_standard_option('pve-node'),
1134 vmid => get_standard_option('pve-vmid'),
1135 },
1136 },
1137 returns => {
1138 type => 'array',
1139 items => {
1140 type => "object",
1141 properties => {
1142 subdir => { type => 'string' },
1143 },
1144 },
1145 links => [ { rel => 'child', href => "{subdir}" } ],
1146 },
1147 code => sub {
1148 my ($param) = @_;
1149
1150 # test if VM exists
1151 my $conf = PVE::QemuServer::load_config($param->{vmid});
1152
1153 my $res = [
1154 { subdir => 'current' },
1155 { subdir => 'start' },
1156 { subdir => 'stop' },
1157 ];
1158
1159 return $res;
1160 }});
1161
1162 my $vm_is_ha_managed = sub {
1163 my ($vmid) = @_;
1164
1165 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1166 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $vmid, 1)) {
1167 return 1;
1168 }
1169 return 0;
1170 };
1171
1172 __PACKAGE__->register_method({
1173 name => 'vm_status',
1174 path => '{vmid}/status/current',
1175 method => 'GET',
1176 proxyto => 'node',
1177 protected => 1, # qemu pid files are only readable by root
1178 description => "Get virtual machine status.",
1179 permissions => {
1180 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1181 },
1182 parameters => {
1183 additionalProperties => 0,
1184 properties => {
1185 node => get_standard_option('pve-node'),
1186 vmid => get_standard_option('pve-vmid'),
1187 },
1188 },
1189 returns => { type => 'object' },
1190 code => sub {
1191 my ($param) = @_;
1192
1193 # test if VM exists
1194 my $conf = PVE::QemuServer::load_config($param->{vmid});
1195
1196 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
1197 my $status = $vmstatus->{$param->{vmid}};
1198
1199 $status->{ha} = &$vm_is_ha_managed($param->{vmid});
1200
1201 return $status;
1202 }});
1203
1204 __PACKAGE__->register_method({
1205 name => 'vm_start',
1206 path => '{vmid}/status/start',
1207 method => 'POST',
1208 protected => 1,
1209 proxyto => 'node',
1210 description => "Start virtual machine.",
1211 permissions => {
1212 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1213 },
1214 parameters => {
1215 additionalProperties => 0,
1216 properties => {
1217 node => get_standard_option('pve-node'),
1218 vmid => get_standard_option('pve-vmid'),
1219 skiplock => get_standard_option('skiplock'),
1220 stateuri => get_standard_option('pve-qm-stateuri'),
1221 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
1222
1223 },
1224 },
1225 returns => {
1226 type => 'string',
1227 },
1228 code => sub {
1229 my ($param) = @_;
1230
1231 my $rpcenv = PVE::RPCEnvironment::get();
1232
1233 my $authuser = $rpcenv->get_user();
1234
1235 my $node = extract_param($param, 'node');
1236
1237 my $vmid = extract_param($param, 'vmid');
1238
1239 my $stateuri = extract_param($param, 'stateuri');
1240 raise_param_exc({ stateuri => "Only root may use this option." })
1241 if $stateuri && $authuser ne 'root@pam';
1242
1243 my $skiplock = extract_param($param, 'skiplock');
1244 raise_param_exc({ skiplock => "Only root may use this option." })
1245 if $skiplock && $authuser ne 'root@pam';
1246
1247 my $migratedfrom = extract_param($param, 'migratedfrom');
1248 raise_param_exc({ migratedfrom => "Only root may use this option." })
1249 if $migratedfrom && $authuser ne 'root@pam';
1250
1251 my $storecfg = PVE::Storage::config();
1252
1253 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1254 $rpcenv->{type} ne 'ha') {
1255
1256 my $hacmd = sub {
1257 my $upid = shift;
1258
1259 my $service = "pvevm:$vmid";
1260
1261 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1262
1263 print "Executing HA start for VM $vmid\n";
1264
1265 PVE::Tools::run_command($cmd);
1266
1267 return;
1268 };
1269
1270 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1271
1272 } else {
1273
1274 my $realcmd = sub {
1275 my $upid = shift;
1276
1277 syslog('info', "start VM $vmid: $upid\n");
1278
1279 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom);
1280
1281 return;
1282 };
1283
1284 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1285 }
1286 }});
1287
1288 __PACKAGE__->register_method({
1289 name => 'vm_stop',
1290 path => '{vmid}/status/stop',
1291 method => 'POST',
1292 protected => 1,
1293 proxyto => 'node',
1294 description => "Stop virtual machine.",
1295 permissions => {
1296 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1297 },
1298 parameters => {
1299 additionalProperties => 0,
1300 properties => {
1301 node => get_standard_option('pve-node'),
1302 vmid => get_standard_option('pve-vmid'),
1303 skiplock => get_standard_option('skiplock'),
1304 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
1305 timeout => {
1306 description => "Wait maximal timeout seconds.",
1307 type => 'integer',
1308 minimum => 0,
1309 optional => 1,
1310 },
1311 keepActive => {
1312 description => "Do not decativate storage volumes.",
1313 type => 'boolean',
1314 optional => 1,
1315 default => 0,
1316 }
1317 },
1318 },
1319 returns => {
1320 type => 'string',
1321 },
1322 code => sub {
1323 my ($param) = @_;
1324
1325 my $rpcenv = PVE::RPCEnvironment::get();
1326
1327 my $authuser = $rpcenv->get_user();
1328
1329 my $node = extract_param($param, 'node');
1330
1331 my $vmid = extract_param($param, 'vmid');
1332
1333 my $skiplock = extract_param($param, 'skiplock');
1334 raise_param_exc({ skiplock => "Only root may use this option." })
1335 if $skiplock && $authuser ne 'root@pam';
1336
1337 my $keepActive = extract_param($param, 'keepActive');
1338 raise_param_exc({ keepActive => "Only root may use this option." })
1339 if $keepActive && $authuser ne 'root@pam';
1340
1341 my $migratedfrom = extract_param($param, 'migratedfrom');
1342 raise_param_exc({ migratedfrom => "Only root may use this option." })
1343 if $migratedfrom && $authuser ne 'root@pam';
1344
1345
1346 my $storecfg = PVE::Storage::config();
1347
1348 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
1349
1350 my $hacmd = sub {
1351 my $upid = shift;
1352
1353 my $service = "pvevm:$vmid";
1354
1355 my $cmd = ['clusvcadm', '-d', $service];
1356
1357 print "Executing HA stop for VM $vmid\n";
1358
1359 PVE::Tools::run_command($cmd);
1360
1361 return;
1362 };
1363
1364 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1365
1366 } else {
1367 my $realcmd = sub {
1368 my $upid = shift;
1369
1370 syslog('info', "stop VM $vmid: $upid\n");
1371
1372 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1373 $param->{timeout}, 0, 1, $keepActive, $migratedfrom);
1374
1375 return;
1376 };
1377
1378 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1379 }
1380 }});
1381
1382 __PACKAGE__->register_method({
1383 name => 'vm_reset',
1384 path => '{vmid}/status/reset',
1385 method => 'POST',
1386 protected => 1,
1387 proxyto => 'node',
1388 description => "Reset virtual machine.",
1389 permissions => {
1390 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1391 },
1392 parameters => {
1393 additionalProperties => 0,
1394 properties => {
1395 node => get_standard_option('pve-node'),
1396 vmid => get_standard_option('pve-vmid'),
1397 skiplock => get_standard_option('skiplock'),
1398 },
1399 },
1400 returns => {
1401 type => 'string',
1402 },
1403 code => sub {
1404 my ($param) = @_;
1405
1406 my $rpcenv = PVE::RPCEnvironment::get();
1407
1408 my $authuser = $rpcenv->get_user();
1409
1410 my $node = extract_param($param, 'node');
1411
1412 my $vmid = extract_param($param, 'vmid');
1413
1414 my $skiplock = extract_param($param, 'skiplock');
1415 raise_param_exc({ skiplock => "Only root may use this option." })
1416 if $skiplock && $authuser ne 'root@pam';
1417
1418 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1419
1420 my $realcmd = sub {
1421 my $upid = shift;
1422
1423 PVE::QemuServer::vm_reset($vmid, $skiplock);
1424
1425 return;
1426 };
1427
1428 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1429 }});
1430
1431 __PACKAGE__->register_method({
1432 name => 'vm_shutdown',
1433 path => '{vmid}/status/shutdown',
1434 method => 'POST',
1435 protected => 1,
1436 proxyto => 'node',
1437 description => "Shutdown virtual machine.",
1438 permissions => {
1439 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1440 },
1441 parameters => {
1442 additionalProperties => 0,
1443 properties => {
1444 node => get_standard_option('pve-node'),
1445 vmid => get_standard_option('pve-vmid'),
1446 skiplock => get_standard_option('skiplock'),
1447 timeout => {
1448 description => "Wait maximal timeout seconds.",
1449 type => 'integer',
1450 minimum => 0,
1451 optional => 1,
1452 },
1453 forceStop => {
1454 description => "Make sure the VM stops.",
1455 type => 'boolean',
1456 optional => 1,
1457 default => 0,
1458 },
1459 keepActive => {
1460 description => "Do not decativate storage volumes.",
1461 type => 'boolean',
1462 optional => 1,
1463 default => 0,
1464 }
1465 },
1466 },
1467 returns => {
1468 type => 'string',
1469 },
1470 code => sub {
1471 my ($param) = @_;
1472
1473 my $rpcenv = PVE::RPCEnvironment::get();
1474
1475 my $authuser = $rpcenv->get_user();
1476
1477 my $node = extract_param($param, 'node');
1478
1479 my $vmid = extract_param($param, 'vmid');
1480
1481 my $skiplock = extract_param($param, 'skiplock');
1482 raise_param_exc({ skiplock => "Only root may use this option." })
1483 if $skiplock && $authuser ne 'root@pam';
1484
1485 my $keepActive = extract_param($param, 'keepActive');
1486 raise_param_exc({ keepActive => "Only root may use this option." })
1487 if $keepActive && $authuser ne 'root@pam';
1488
1489 my $storecfg = PVE::Storage::config();
1490
1491 my $realcmd = sub {
1492 my $upid = shift;
1493
1494 syslog('info', "shutdown VM $vmid: $upid\n");
1495
1496 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1497 1, $param->{forceStop}, $keepActive);
1498
1499 return;
1500 };
1501
1502 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1503 }});
1504
1505 __PACKAGE__->register_method({
1506 name => 'vm_suspend',
1507 path => '{vmid}/status/suspend',
1508 method => 'POST',
1509 protected => 1,
1510 proxyto => 'node',
1511 description => "Suspend virtual machine.",
1512 permissions => {
1513 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1514 },
1515 parameters => {
1516 additionalProperties => 0,
1517 properties => {
1518 node => get_standard_option('pve-node'),
1519 vmid => get_standard_option('pve-vmid'),
1520 skiplock => get_standard_option('skiplock'),
1521 },
1522 },
1523 returns => {
1524 type => 'string',
1525 },
1526 code => sub {
1527 my ($param) = @_;
1528
1529 my $rpcenv = PVE::RPCEnvironment::get();
1530
1531 my $authuser = $rpcenv->get_user();
1532
1533 my $node = extract_param($param, 'node');
1534
1535 my $vmid = extract_param($param, 'vmid');
1536
1537 my $skiplock = extract_param($param, 'skiplock');
1538 raise_param_exc({ skiplock => "Only root may use this option." })
1539 if $skiplock && $authuser ne 'root@pam';
1540
1541 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1542
1543 my $realcmd = sub {
1544 my $upid = shift;
1545
1546 syslog('info', "suspend VM $vmid: $upid\n");
1547
1548 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1549
1550 return;
1551 };
1552
1553 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1554 }});
1555
1556 __PACKAGE__->register_method({
1557 name => 'vm_resume',
1558 path => '{vmid}/status/resume',
1559 method => 'POST',
1560 protected => 1,
1561 proxyto => 'node',
1562 description => "Resume virtual machine.",
1563 permissions => {
1564 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1565 },
1566 parameters => {
1567 additionalProperties => 0,
1568 properties => {
1569 node => get_standard_option('pve-node'),
1570 vmid => get_standard_option('pve-vmid'),
1571 skiplock => get_standard_option('skiplock'),
1572 },
1573 },
1574 returns => {
1575 type => 'string',
1576 },
1577 code => sub {
1578 my ($param) = @_;
1579
1580 my $rpcenv = PVE::RPCEnvironment::get();
1581
1582 my $authuser = $rpcenv->get_user();
1583
1584 my $node = extract_param($param, 'node');
1585
1586 my $vmid = extract_param($param, 'vmid');
1587
1588 my $skiplock = extract_param($param, 'skiplock');
1589 raise_param_exc({ skiplock => "Only root may use this option." })
1590 if $skiplock && $authuser ne 'root@pam';
1591
1592 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1593
1594 my $realcmd = sub {
1595 my $upid = shift;
1596
1597 syslog('info', "resume VM $vmid: $upid\n");
1598
1599 PVE::QemuServer::vm_resume($vmid, $skiplock);
1600
1601 return;
1602 };
1603
1604 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1605 }});
1606
1607 __PACKAGE__->register_method({
1608 name => 'vm_sendkey',
1609 path => '{vmid}/sendkey',
1610 method => 'PUT',
1611 protected => 1,
1612 proxyto => 'node',
1613 description => "Send key event to virtual machine.",
1614 permissions => {
1615 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1616 },
1617 parameters => {
1618 additionalProperties => 0,
1619 properties => {
1620 node => get_standard_option('pve-node'),
1621 vmid => get_standard_option('pve-vmid'),
1622 skiplock => get_standard_option('skiplock'),
1623 key => {
1624 description => "The key (qemu monitor encoding).",
1625 type => 'string'
1626 }
1627 },
1628 },
1629 returns => { type => 'null'},
1630 code => sub {
1631 my ($param) = @_;
1632
1633 my $rpcenv = PVE::RPCEnvironment::get();
1634
1635 my $authuser = $rpcenv->get_user();
1636
1637 my $node = extract_param($param, 'node');
1638
1639 my $vmid = extract_param($param, 'vmid');
1640
1641 my $skiplock = extract_param($param, 'skiplock');
1642 raise_param_exc({ skiplock => "Only root may use this option." })
1643 if $skiplock && $authuser ne 'root@pam';
1644
1645 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1646
1647 return;
1648 }});
1649
1650 __PACKAGE__->register_method({
1651 name => 'vm_feature',
1652 path => '{vmid}/feature',
1653 method => 'GET',
1654 proxyto => 'node',
1655 protected => 1,
1656 description => "Check if feature for virtual machine is available.",
1657 permissions => {
1658 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1659 },
1660 parameters => {
1661 additionalProperties => 0,
1662 properties => {
1663 node => get_standard_option('pve-node'),
1664 vmid => get_standard_option('pve-vmid'),
1665 feature => {
1666 description => "Feature to check.",
1667 type => 'string',
1668 enum => [ 'snapshot', 'clone' ],
1669 },
1670 snapname => get_standard_option('pve-snapshot-name', {
1671 optional => 1,
1672 }),
1673 },
1674
1675 },
1676 returns => {
1677 type => 'boolean'
1678 },
1679 code => sub {
1680 my ($param) = @_;
1681
1682 my $node = extract_param($param, 'node');
1683
1684 my $vmid = extract_param($param, 'vmid');
1685
1686 my $snapname = extract_param($param, 'snapname');
1687
1688 my $feature = extract_param($param, 'feature');
1689
1690 my $running = PVE::QemuServer::check_running($vmid);
1691
1692 my $conf = PVE::QemuServer::load_config($vmid);
1693
1694 if($snapname){
1695 my $snap = $conf->{snapshots}->{$snapname};
1696 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1697 $conf = $snap;
1698 }
1699 my $storecfg = PVE::Storage::config();
1700
1701 my $hasfeature = PVE::QemuServer::has_feature($feature, $conf, $storecfg, $snapname, $running);
1702 my $res = $hasfeature ? 1 : 0 ;
1703 return $res;
1704 }});
1705
1706 __PACKAGE__->register_method({
1707 name => 'migrate_vm',
1708 path => '{vmid}/migrate',
1709 method => 'POST',
1710 protected => 1,
1711 proxyto => 'node',
1712 description => "Migrate virtual machine. Creates a new migration task.",
1713 permissions => {
1714 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1715 },
1716 parameters => {
1717 additionalProperties => 0,
1718 properties => {
1719 node => get_standard_option('pve-node'),
1720 vmid => get_standard_option('pve-vmid'),
1721 target => get_standard_option('pve-node', { description => "Target node." }),
1722 online => {
1723 type => 'boolean',
1724 description => "Use online/live migration.",
1725 optional => 1,
1726 },
1727 force => {
1728 type => 'boolean',
1729 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
1730 optional => 1,
1731 },
1732 },
1733 },
1734 returns => {
1735 type => 'string',
1736 description => "the task ID.",
1737 },
1738 code => sub {
1739 my ($param) = @_;
1740
1741 my $rpcenv = PVE::RPCEnvironment::get();
1742
1743 my $authuser = $rpcenv->get_user();
1744
1745 my $target = extract_param($param, 'target');
1746
1747 my $localnode = PVE::INotify::nodename();
1748 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
1749
1750 PVE::Cluster::check_cfs_quorum();
1751
1752 PVE::Cluster::check_node_exists($target);
1753
1754 my $targetip = PVE::Cluster::remote_node_ip($target);
1755
1756 my $vmid = extract_param($param, 'vmid');
1757
1758 raise_param_exc({ force => "Only root may use this option." })
1759 if $param->{force} && $authuser ne 'root@pam';
1760
1761 # test if VM exists
1762 my $conf = PVE::QemuServer::load_config($vmid);
1763
1764 # try to detect errors early
1765
1766 PVE::QemuServer::check_lock($conf);
1767
1768 if (PVE::QemuServer::check_running($vmid)) {
1769 die "cant migrate running VM without --online\n"
1770 if !$param->{online};
1771 }
1772
1773 my $storecfg = PVE::Storage::config();
1774 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target);
1775
1776 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
1777
1778 my $hacmd = sub {
1779 my $upid = shift;
1780
1781 my $service = "pvevm:$vmid";
1782
1783 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1784
1785 print "Executing HA migrate for VM $vmid to node $target\n";
1786
1787 PVE::Tools::run_command($cmd);
1788
1789 return;
1790 };
1791
1792 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1793
1794 } else {
1795
1796 my $realcmd = sub {
1797 my $upid = shift;
1798
1799 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1800 };
1801
1802 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1803 }
1804
1805 }});
1806
1807 __PACKAGE__->register_method({
1808 name => 'monitor',
1809 path => '{vmid}/monitor',
1810 method => 'POST',
1811 protected => 1,
1812 proxyto => 'node',
1813 description => "Execute Qemu monitor commands.",
1814 permissions => {
1815 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1816 },
1817 parameters => {
1818 additionalProperties => 0,
1819 properties => {
1820 node => get_standard_option('pve-node'),
1821 vmid => get_standard_option('pve-vmid'),
1822 command => {
1823 type => 'string',
1824 description => "The monitor command.",
1825 }
1826 },
1827 },
1828 returns => { type => 'string'},
1829 code => sub {
1830 my ($param) = @_;
1831
1832 my $vmid = $param->{vmid};
1833
1834 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1835
1836 my $res = '';
1837 eval {
1838 $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command});
1839 };
1840 $res = "ERROR: $@" if $@;
1841
1842 return $res;
1843 }});
1844
1845 __PACKAGE__->register_method({
1846 name => 'resize_vm',
1847 path => '{vmid}/resize',
1848 method => 'PUT',
1849 protected => 1,
1850 proxyto => 'node',
1851 description => "Extend volume size.",
1852 permissions => {
1853 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
1854 },
1855 parameters => {
1856 additionalProperties => 0,
1857 properties => {
1858 node => get_standard_option('pve-node'),
1859 vmid => get_standard_option('pve-vmid'),
1860 skiplock => get_standard_option('skiplock'),
1861 disk => {
1862 type => 'string',
1863 description => "The disk you want to resize.",
1864 enum => [PVE::QemuServer::disknames()],
1865 },
1866 size => {
1867 type => 'string',
1868 pattern => '\+?\d+(\.\d+)?[KMGT]?',
1869 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.",
1870 },
1871 digest => {
1872 type => 'string',
1873 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1874 maxLength => 40,
1875 optional => 1,
1876 },
1877 },
1878 },
1879 returns => { type => 'null'},
1880 code => sub {
1881 my ($param) = @_;
1882
1883 my $rpcenv = PVE::RPCEnvironment::get();
1884
1885 my $authuser = $rpcenv->get_user();
1886
1887 my $node = extract_param($param, 'node');
1888
1889 my $vmid = extract_param($param, 'vmid');
1890
1891 my $digest = extract_param($param, 'digest');
1892
1893 my $disk = extract_param($param, 'disk');
1894
1895 my $sizestr = extract_param($param, 'size');
1896
1897 my $skiplock = extract_param($param, 'skiplock');
1898 raise_param_exc({ skiplock => "Only root may use this option." })
1899 if $skiplock && $authuser ne 'root@pam';
1900
1901 my $storecfg = PVE::Storage::config();
1902
1903 my $updatefn = sub {
1904
1905 my $conf = PVE::QemuServer::load_config($vmid);
1906
1907 die "checksum missmatch (file change by other user?)\n"
1908 if $digest && $digest ne $conf->{digest};
1909 PVE::QemuServer::check_lock($conf) if !$skiplock;
1910
1911 die "disk '$disk' does not exist\n" if !$conf->{$disk};
1912
1913 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
1914
1915 my $volid = $drive->{file};
1916
1917 die "disk '$disk' has no associated volume\n" if !$volid;
1918
1919 die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
1920
1921 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
1922
1923 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
1924
1925 my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
1926
1927 die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
1928 my ($ext, $newsize, $unit) = ($1, $2, $4);
1929 if ($unit) {
1930 if ($unit eq 'K') {
1931 $newsize = $newsize * 1024;
1932 } elsif ($unit eq 'M') {
1933 $newsize = $newsize * 1024 * 1024;
1934 } elsif ($unit eq 'G') {
1935 $newsize = $newsize * 1024 * 1024 * 1024;
1936 } elsif ($unit eq 'T') {
1937 $newsize = $newsize * 1024 * 1024 * 1024 * 1024;
1938 }
1939 }
1940 $newsize += $size if $ext;
1941 $newsize = int($newsize);
1942
1943 die "unable to skrink disk size\n" if $newsize < $size;
1944
1945 return if $size == $newsize;
1946
1947 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr");
1948
1949 PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize);
1950
1951 $drive->{size} = $newsize;
1952 $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive);
1953
1954 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
1955 };
1956
1957 PVE::QemuServer::lock_config($vmid, $updatefn);
1958 return undef;
1959 }});
1960
1961 __PACKAGE__->register_method({
1962 name => 'snapshot_list',
1963 path => '{vmid}/snapshot',
1964 method => 'GET',
1965 description => "List all snapshots.",
1966 permissions => {
1967 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1968 },
1969 proxyto => 'node',
1970 protected => 1, # qemu pid files are only readable by root
1971 parameters => {
1972 additionalProperties => 0,
1973 properties => {
1974 vmid => get_standard_option('pve-vmid'),
1975 node => get_standard_option('pve-node'),
1976 },
1977 },
1978 returns => {
1979 type => 'array',
1980 items => {
1981 type => "object",
1982 properties => {},
1983 },
1984 links => [ { rel => 'child', href => "{name}" } ],
1985 },
1986 code => sub {
1987 my ($param) = @_;
1988
1989 my $vmid = $param->{vmid};
1990
1991 my $conf = PVE::QemuServer::load_config($vmid);
1992 my $snaphash = $conf->{snapshots} || {};
1993
1994 my $res = [];
1995
1996 foreach my $name (keys %$snaphash) {
1997 my $d = $snaphash->{$name};
1998 my $item = {
1999 name => $name,
2000 snaptime => $d->{snaptime} || 0,
2001 vmstate => $d->{vmstate} ? 1 : 0,
2002 description => $d->{description} || '',
2003 };
2004 $item->{parent} = $d->{parent} if $d->{parent};
2005 $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
2006 push @$res, $item;
2007 }
2008
2009 my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
2010 my $current = { name => 'current', digest => $conf->{digest}, running => $running };
2011 $current->{parent} = $conf->{parent} if $conf->{parent};
2012
2013 push @$res, $current;
2014
2015 return $res;
2016 }});
2017
2018 __PACKAGE__->register_method({
2019 name => 'snapshot',
2020 path => '{vmid}/snapshot',
2021 method => 'POST',
2022 protected => 1,
2023 proxyto => 'node',
2024 description => "Snapshot a VM.",
2025 permissions => {
2026 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2027 },
2028 parameters => {
2029 additionalProperties => 0,
2030 properties => {
2031 node => get_standard_option('pve-node'),
2032 vmid => get_standard_option('pve-vmid'),
2033 snapname => get_standard_option('pve-snapshot-name'),
2034 vmstate => {
2035 optional => 1,
2036 type => 'boolean',
2037 description => "Save the vmstate",
2038 },
2039 freezefs => {
2040 optional => 1,
2041 type => 'boolean',
2042 description => "Freeze the filesystem",
2043 },
2044 description => {
2045 optional => 1,
2046 type => 'string',
2047 description => "A textual description or comment.",
2048 },
2049 },
2050 },
2051 returns => {
2052 type => 'string',
2053 description => "the task ID.",
2054 },
2055 code => sub {
2056 my ($param) = @_;
2057
2058 my $rpcenv = PVE::RPCEnvironment::get();
2059
2060 my $authuser = $rpcenv->get_user();
2061
2062 my $node = extract_param($param, 'node');
2063
2064 my $vmid = extract_param($param, 'vmid');
2065
2066 my $snapname = extract_param($param, 'snapname');
2067
2068 die "unable to use snapshot name 'current' (reserved name)\n"
2069 if $snapname eq 'current';
2070
2071 my $realcmd = sub {
2072 PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
2073 PVE::QemuServer::snapshot_create($vmid, $snapname, $param->{vmstate},
2074 $param->{freezefs}, $param->{description});
2075 };
2076
2077 return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd);
2078 }});
2079
2080 __PACKAGE__->register_method({
2081 name => 'snapshot_cmd_idx',
2082 path => '{vmid}/snapshot/{snapname}',
2083 description => '',
2084 method => 'GET',
2085 permissions => {
2086 user => 'all',
2087 },
2088 parameters => {
2089 additionalProperties => 0,
2090 properties => {
2091 vmid => get_standard_option('pve-vmid'),
2092 node => get_standard_option('pve-node'),
2093 snapname => get_standard_option('pve-snapshot-name'),
2094 },
2095 },
2096 returns => {
2097 type => 'array',
2098 items => {
2099 type => "object",
2100 properties => {},
2101 },
2102 links => [ { rel => 'child', href => "{cmd}" } ],
2103 },
2104 code => sub {
2105 my ($param) = @_;
2106
2107 my $res = [];
2108
2109 push @$res, { cmd => 'rollback' };
2110 push @$res, { cmd => 'config' };
2111
2112 return $res;
2113 }});
2114
2115 __PACKAGE__->register_method({
2116 name => 'update_snapshot_config',
2117 path => '{vmid}/snapshot/{snapname}/config',
2118 method => 'PUT',
2119 protected => 1,
2120 proxyto => 'node',
2121 description => "Update snapshot metadata.",
2122 permissions => {
2123 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2124 },
2125 parameters => {
2126 additionalProperties => 0,
2127 properties => {
2128 node => get_standard_option('pve-node'),
2129 vmid => get_standard_option('pve-vmid'),
2130 snapname => get_standard_option('pve-snapshot-name'),
2131 description => {
2132 optional => 1,
2133 type => 'string',
2134 description => "A textual description or comment.",
2135 },
2136 },
2137 },
2138 returns => { type => 'null' },
2139 code => sub {
2140 my ($param) = @_;
2141
2142 my $rpcenv = PVE::RPCEnvironment::get();
2143
2144 my $authuser = $rpcenv->get_user();
2145
2146 my $vmid = extract_param($param, 'vmid');
2147
2148 my $snapname = extract_param($param, 'snapname');
2149
2150 return undef if !defined($param->{description});
2151
2152 my $updatefn = sub {
2153
2154 my $conf = PVE::QemuServer::load_config($vmid);
2155
2156 PVE::QemuServer::check_lock($conf);
2157
2158 my $snap = $conf->{snapshots}->{$snapname};
2159
2160 die "snapshot '$snapname' does not exist\n" if !defined($snap);
2161
2162 $snap->{description} = $param->{description} if defined($param->{description});
2163
2164 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
2165 };
2166
2167 PVE::QemuServer::lock_config($vmid, $updatefn);
2168
2169 return undef;
2170 }});
2171
2172 __PACKAGE__->register_method({
2173 name => 'get_snapshot_config',
2174 path => '{vmid}/snapshot/{snapname}/config',
2175 method => 'GET',
2176 proxyto => 'node',
2177 description => "Get snapshot configuration",
2178 permissions => {
2179 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2180 },
2181 parameters => {
2182 additionalProperties => 0,
2183 properties => {
2184 node => get_standard_option('pve-node'),
2185 vmid => get_standard_option('pve-vmid'),
2186 snapname => get_standard_option('pve-snapshot-name'),
2187 },
2188 },
2189 returns => { type => "object" },
2190 code => sub {
2191 my ($param) = @_;
2192
2193 my $rpcenv = PVE::RPCEnvironment::get();
2194
2195 my $authuser = $rpcenv->get_user();
2196
2197 my $vmid = extract_param($param, 'vmid');
2198
2199 my $snapname = extract_param($param, 'snapname');
2200
2201 my $conf = PVE::QemuServer::load_config($vmid);
2202
2203 my $snap = $conf->{snapshots}->{$snapname};
2204
2205 die "snapshot '$snapname' does not exist\n" if !defined($snap);
2206
2207 return $snap;
2208 }});
2209
2210 __PACKAGE__->register_method({
2211 name => 'rollback',
2212 path => '{vmid}/snapshot/{snapname}/rollback',
2213 method => 'POST',
2214 protected => 1,
2215 proxyto => 'node',
2216 description => "Rollback VM state to specified snapshot.",
2217 permissions => {
2218 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2219 },
2220 parameters => {
2221 additionalProperties => 0,
2222 properties => {
2223 node => get_standard_option('pve-node'),
2224 vmid => get_standard_option('pve-vmid'),
2225 snapname => get_standard_option('pve-snapshot-name'),
2226 },
2227 },
2228 returns => {
2229 type => 'string',
2230 description => "the task ID.",
2231 },
2232 code => sub {
2233 my ($param) = @_;
2234
2235 my $rpcenv = PVE::RPCEnvironment::get();
2236
2237 my $authuser = $rpcenv->get_user();
2238
2239 my $node = extract_param($param, 'node');
2240
2241 my $vmid = extract_param($param, 'vmid');
2242
2243 my $snapname = extract_param($param, 'snapname');
2244
2245 my $realcmd = sub {
2246 PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname");
2247 PVE::QemuServer::snapshot_rollback($vmid, $snapname);
2248 };
2249
2250 return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd);
2251 }});
2252
2253 __PACKAGE__->register_method({
2254 name => 'delsnapshot',
2255 path => '{vmid}/snapshot/{snapname}',
2256 method => 'DELETE',
2257 protected => 1,
2258 proxyto => 'node',
2259 description => "Delete a VM snapshot.",
2260 permissions => {
2261 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2262 },
2263 parameters => {
2264 additionalProperties => 0,
2265 properties => {
2266 node => get_standard_option('pve-node'),
2267 vmid => get_standard_option('pve-vmid'),
2268 snapname => get_standard_option('pve-snapshot-name'),
2269 force => {
2270 optional => 1,
2271 type => 'boolean',
2272 description => "For removal from config file, even if removing disk snapshots fails.",
2273 },
2274 },
2275 },
2276 returns => {
2277 type => 'string',
2278 description => "the task ID.",
2279 },
2280 code => sub {
2281 my ($param) = @_;
2282
2283 my $rpcenv = PVE::RPCEnvironment::get();
2284
2285 my $authuser = $rpcenv->get_user();
2286
2287 my $node = extract_param($param, 'node');
2288
2289 my $vmid = extract_param($param, 'vmid');
2290
2291 my $snapname = extract_param($param, 'snapname');
2292
2293 my $realcmd = sub {
2294 PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
2295 PVE::QemuServer::snapshot_delete($vmid, $snapname, $param->{force});
2296 };
2297
2298 return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);
2299 }});
2300
2301 __PACKAGE__->register_method({
2302 name => 'template',
2303 path => '{vmid}/template',
2304 method => 'POST',
2305 protected => 1,
2306 proxyto => 'node',
2307 description => "Create a Template.",
2308 parameters => {
2309 additionalProperties => 0,
2310 properties => {
2311 node => get_standard_option('pve-node'),
2312 vmid => get_standard_option('pve-vmid'),
2313 disk => {
2314 optional => 1,
2315 type => 'string',
2316 description => "If you want to convert only 1 disk to base image.",
2317 enum => [PVE::QemuServer::disknames()],
2318 },
2319
2320 },
2321 },
2322 returns => { type => 'null'},
2323 code => sub {
2324 my ($param) = @_;
2325
2326 my $rpcenv = PVE::RPCEnvironment::get();
2327
2328 my $authuser = $rpcenv->get_user();
2329
2330 my $node = extract_param($param, 'node');
2331
2332 my $vmid = extract_param($param, 'vmid');
2333
2334 my $disk = extract_param($param, 'disk');
2335
2336 my $updatefn = sub {
2337
2338 my $conf = PVE::QemuServer::load_config($vmid);
2339
2340 PVE::QemuServer::check_lock($conf);
2341
2342 die "you can't convert a template to a template"
2343 if PVE::QemuServer::is_template($conf) && !$disk;
2344 my $realcmd = sub {
2345 PVE::QemuServer::template_create($vmid, $conf, $disk);
2346 };
2347 return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
2348
2349 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
2350 };
2351
2352 PVE::QemuServer::lock_config($vmid, $updatefn);
2353 return undef;
2354 }});
2355
2356
2357
2358 1;