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