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