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