]> git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu.pm
dc44dee18dec8b84ee80c39388065c093bca3fae
[qemu-server.git] / PVE / API2 / Qemu.pm
1 package PVE::API2::Qemu;
2
3 use strict;
4 use warnings;
5 use Cwd 'abs_path';
6 use Net::SSLeay;
7 use IO::Socket::IP;
8 use IO::Socket::UNIX;
9 use IPC::Open3;
10 use JSON;
11 use URI::Escape;
12 use Crypt::OpenSSL::Random;
13 use Socket qw(SOCK_STREAM);
14
15 use PVE::APIClient::LWP;
16 use PVE::CGroup;
17 use PVE::Cluster qw (cfs_read_file cfs_write_file);;
18 use PVE::RRD;
19 use PVE::SafeSyslog;
20 use PVE::Tools qw(extract_param);
21 use PVE::Exception qw(raise raise_param_exc raise_perm_exc);
22 use PVE::Storage;
23 use PVE::JSONSchema qw(get_standard_option);
24 use PVE::RESTHandler;
25 use PVE::ReplicationConfig;
26 use PVE::GuestHelpers qw(assert_tag_permissions);
27 use PVE::QemuConfig;
28 use PVE::QemuServer;
29 use PVE::QemuServer::Cloudinit;
30 use PVE::QemuServer::CPUConfig;
31 use PVE::QemuServer::Drive;
32 use PVE::QemuServer::ImportDisk;
33 use PVE::QemuServer::Monitor qw(mon_cmd);
34 use PVE::QemuServer::Machine;
35 use PVE::QemuServer::Memory qw(get_current_memory);
36 use PVE::QemuServer::PCI;
37 use PVE::QemuServer::USB;
38 use PVE::QemuMigrate;
39 use PVE::RPCEnvironment;
40 use PVE::AccessControl;
41 use PVE::INotify;
42 use PVE::Network;
43 use PVE::Firewall;
44 use PVE::API2::Firewall::VM;
45 use PVE::API2::Qemu::Agent;
46 use PVE::VZDump::Plugin;
47 use PVE::DataCenterConfig;
48 use PVE::SSHInfo;
49 use PVE::Replication;
50 use PVE::StorageTunnel;
51 use PVE::RESTEnvironment qw(log_warn);
52
53 BEGIN {
54 if (!$ENV{PVE_GENERATING_DOCS}) {
55 require PVE::HA::Env::PVE2;
56 import PVE::HA::Env::PVE2;
57 require PVE::HA::Config;
58 import PVE::HA::Config;
59 }
60 }
61
62 use base qw(PVE::RESTHandler);
63
64 my $opt_force_description = "Force physical removal. Without this, we simple remove the disk from the config file and create an additional configuration entry called 'unused[n]', which contains the volume ID. Unlink of unused[n] always cause physical removal.";
65
66 my $resolve_cdrom_alias = sub {
67 my $param = shift;
68
69 if (my $value = $param->{cdrom}) {
70 $value .= ",media=cdrom" if $value !~ m/media=/;
71 $param->{ide2} = $value;
72 delete $param->{cdrom};
73 }
74 };
75
76 # Used in import-enabled API endpoints. Parses drives using the extended '_with_alloc' schema.
77 my $foreach_volume_with_alloc = sub {
78 my ($param, $func) = @_;
79
80 for my $opt (sort keys $param->%*) {
81 next if !PVE::QemuServer::is_valid_drivename($opt);
82
83 my $drive = PVE::QemuServer::Drive::parse_drive($opt, $param->{$opt}, 1);
84 next if !$drive;
85
86 $func->($opt, $drive);
87 }
88 };
89
90 my $check_drive_param = sub {
91 my ($param, $storecfg, $extra_checks) = @_;
92
93 for my $opt (sort keys $param->%*) {
94 next if !PVE::QemuServer::is_valid_drivename($opt);
95
96 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}, 1);
97 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
98
99 if ($drive->{'import-from'}) {
100 if ($drive->{file} !~ $PVE::QemuServer::Drive::NEW_DISK_RE || $3 != 0) {
101 raise_param_exc({
102 $opt => "'import-from' requires special syntax - ".
103 "use <storage ID>:0,import-from=<source>",
104 });
105 }
106
107 if ($opt eq 'efidisk0') {
108 for my $required (qw(efitype pre-enrolled-keys)) {
109 if (!defined($drive->{$required})) {
110 raise_param_exc({
111 $opt => "need to specify '$required' when using 'import-from'",
112 });
113 }
114 }
115 } elsif ($opt eq 'tpmstate0') {
116 raise_param_exc({ $opt => "need to specify 'version' when using 'import-from'" })
117 if !defined($drive->{version});
118 }
119 }
120
121 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
122
123 $extra_checks->($drive) if $extra_checks;
124
125 $param->{$opt} = PVE::QemuServer::print_drive($drive, 1);
126 }
127 };
128
129 my $check_storage_access = sub {
130 my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_;
131
132 $foreach_volume_with_alloc->($settings, sub {
133 my ($ds, $drive) = @_;
134
135 my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive);
136
137 my $volid = $drive->{file};
138 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
139
140 if (!$volid || ($volid eq 'none' || $volid eq 'cloudinit' || (defined($volname) && $volname eq 'cloudinit'))) {
141 # nothing to check
142 } elsif ($isCDROM && ($volid eq 'cdrom')) {
143 $rpcenv->check($authuser, "/", ['Sys.Console']);
144 } elsif (!$isCDROM && ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE)) {
145 my ($storeid, $size) = ($2 || $default_storage, $3);
146 die "no storage ID specified (and no default storage)\n" if !$storeid;
147 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
148 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
149 raise_param_exc({ storage => "storage '$storeid' does not support vm images"})
150 if !$scfg->{content}->{images};
151 } else {
152 PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
153 if ($storeid) {
154 my ($vtype) = PVE::Storage::parse_volname($storecfg, $volid);
155 raise_param_exc({ $ds => "content type needs to be 'images' or 'iso'" })
156 if $vtype ne 'images' && $vtype ne 'iso';
157 }
158 }
159
160 if (my $src_image = $drive->{'import-from'}) {
161 my $src_vmid;
162 if (PVE::Storage::parse_volume_id($src_image, 1)) { # PVE-managed volume
163 (my $vtype, undef, $src_vmid) = PVE::Storage::parse_volname($storecfg, $src_image);
164 raise_param_exc({ $ds => "$src_image has wrong type '$vtype' - not an image" })
165 if $vtype ne 'images';
166 }
167
168 if ($src_vmid) { # might be actively used by VM and will be copied via clone_disk()
169 $rpcenv->check($authuser, "/vms/${src_vmid}", ['VM.Clone']);
170 } else {
171 PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $src_image);
172 }
173 }
174 });
175
176 $rpcenv->check($authuser, "/storage/$settings->{vmstatestorage}", ['Datastore.AllocateSpace'])
177 if defined($settings->{vmstatestorage});
178 };
179
180 my $check_storage_access_clone = sub {
181 my ($rpcenv, $authuser, $storecfg, $conf, $storage) = @_;
182
183 my $sharedvm = 1;
184
185 PVE::QemuConfig->foreach_volume($conf, sub {
186 my ($ds, $drive) = @_;
187
188 my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive);
189
190 my $volid = $drive->{file};
191
192 return if !$volid || $volid eq 'none';
193
194 if ($isCDROM) {
195 if ($volid eq 'cdrom') {
196 $rpcenv->check($authuser, "/", ['Sys.Console']);
197 } else {
198 # we simply allow access
199 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
200 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
201 $sharedvm = 0 if !$scfg->{shared};
202
203 }
204 } else {
205 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
206 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
207 $sharedvm = 0 if !$scfg->{shared};
208
209 $sid = $storage if $storage;
210 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']);
211 }
212 });
213
214 $rpcenv->check($authuser, "/storage/$conf->{vmstatestorage}", ['Datastore.AllocateSpace'])
215 if defined($conf->{vmstatestorage});
216
217 return $sharedvm;
218 };
219
220 my $check_storage_access_migrate = sub {
221 my ($rpcenv, $authuser, $storecfg, $storage, $node) = @_;
222
223 PVE::Storage::storage_check_enabled($storecfg, $storage, $node);
224
225 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']);
226
227 my $scfg = PVE::Storage::storage_config($storecfg, $storage);
228 die "storage '$storage' does not support vm images\n"
229 if !$scfg->{content}->{images};
230 };
231
232 my $import_from_volid = sub {
233 my ($storecfg, $src_volid, $dest_info, $vollist) = @_;
234
235 die "could not get size of $src_volid\n"
236 if !PVE::Storage::volume_size_info($storecfg, $src_volid, 10);
237
238 die "cannot import from cloudinit disk\n"
239 if PVE::QemuServer::Drive::drive_is_cloudinit({ file => $src_volid });
240
241 my $src_vmid = (PVE::Storage::parse_volname($storecfg, $src_volid))[2];
242
243 my $src_vm_state = sub {
244 my $exists = $src_vmid && PVE::Cluster::get_vmlist()->{ids}->{$src_vmid} ? 1 : 0;
245
246 my $runs = 0;
247 if ($exists) {
248 eval { PVE::QemuConfig::assert_config_exists_on_node($src_vmid); };
249 die "owner VM $src_vmid not on local node\n" if $@;
250 $runs = PVE::QemuServer::Helpers::vm_running_locally($src_vmid) || 0;
251 }
252
253 return ($exists, $runs);
254 };
255
256 my ($src_vm_exists, $running) = $src_vm_state->();
257
258 die "cannot import from '$src_volid' - full clone feature is not supported\n"
259 if !PVE::Storage::volume_has_feature($storecfg, 'copy', $src_volid, undef, $running);
260
261 my $clonefn = sub {
262 my ($src_vm_exists_now, $running_now) = $src_vm_state->();
263
264 die "owner VM $src_vmid changed state unexpectedly\n"
265 if $src_vm_exists_now != $src_vm_exists || $running_now != $running;
266
267 my $src_conf = $src_vm_exists_now ? PVE::QemuConfig->load_config($src_vmid) : {};
268
269 my $src_drive = { file => $src_volid };
270 my $src_drivename;
271 PVE::QemuConfig->foreach_volume($src_conf, sub {
272 my ($ds, $drive) = @_;
273
274 return if $src_drivename;
275
276 if ($drive->{file} eq $src_volid) {
277 $src_drive = $drive;
278 $src_drivename = $ds;
279 }
280 });
281
282 my $source_info = {
283 vmid => $src_vmid,
284 running => $running_now,
285 drivename => $src_drivename,
286 drive => $src_drive,
287 snapname => undef,
288 };
289
290 my ($src_storeid) = PVE::Storage::parse_volume_id($src_volid);
291
292 return PVE::QemuServer::clone_disk(
293 $storecfg,
294 $source_info,
295 $dest_info,
296 1,
297 $vollist,
298 undef,
299 undef,
300 $src_conf->{agent},
301 PVE::Storage::get_bandwidth_limit('clone', [$src_storeid, $dest_info->{storage}]),
302 );
303 };
304
305 my $cloned;
306 if ($running) {
307 $cloned = PVE::QemuConfig->lock_config_full($src_vmid, 30, $clonefn);
308 } elsif ($src_vmid) {
309 $cloned = PVE::QemuConfig->lock_config_shared($src_vmid, 30, $clonefn);
310 } else {
311 $cloned = $clonefn->();
312 }
313
314 return $cloned->@{qw(file size)};
315 };
316
317 # Note: $pool is only needed when creating a VM, because pool permissions
318 # are automatically inherited if VM already exists inside a pool.
319 my sub create_disks : prototype($$$$$$$$$$) {
320 my (
321 $rpcenv,
322 $authuser,
323 $conf,
324 $arch,
325 $storecfg,
326 $vmid,
327 $pool,
328 $settings,
329 $default_storage,
330 $is_live_import,
331 ) = @_;
332
333 my $vollist = [];
334
335 my $res = {};
336
337 my $live_import_mapping = {};
338
339 my $code = sub {
340 my ($ds, $disk) = @_;
341
342 my $volid = $disk->{file};
343 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
344
345 if (!$volid || $volid eq 'none' || $volid eq 'cdrom') {
346 delete $disk->{size};
347 $res->{$ds} = PVE::QemuServer::print_drive($disk);
348 } elsif (defined($volname) && $volname eq 'cloudinit') {
349 $storeid = $storeid // $default_storage;
350 die "no storage ID specified (and no default storage)\n" if !$storeid;
351
352 if (
353 my $ci_key = PVE::QemuConfig->has_cloudinit($conf, $ds)
354 || PVE::QemuConfig->has_cloudinit($conf->{pending} || {}, $ds)
355 || PVE::QemuConfig->has_cloudinit($res, $ds)
356 ) {
357 die "$ds - cloud-init drive is already attached at '$ci_key'\n";
358 }
359
360 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
361 my $name = "vm-$vmid-cloudinit";
362
363 my $fmt = undef;
364 if ($scfg->{path}) {
365 $fmt = $disk->{format} // "qcow2";
366 $name .= ".$fmt";
367 } else {
368 $fmt = $disk->{format} // "raw";
369 }
370
371 # Initial disk created with 4 MB and aligned to 4MB on regeneration
372 my $ci_size = PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE;
373 my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $name, $ci_size/1024);
374 $disk->{file} = $volid;
375 $disk->{media} = 'cdrom';
376 push @$vollist, $volid;
377 delete $disk->{format}; # no longer needed
378 $res->{$ds} = PVE::QemuServer::print_drive($disk);
379 print "$ds: successfully created disk '$res->{$ds}'\n";
380 } elsif ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE) {
381 my ($storeid, $size) = ($2 || $default_storage, $3);
382 die "no storage ID specified (and no default storage)\n" if !$storeid;
383
384 $size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # vdisk_alloc uses kb
385
386 my $live_import = $is_live_import && $ds ne 'efidisk0';
387 my $needs_creation = 1;
388
389 if (my $source = delete $disk->{'import-from'}) {
390 my $dst_volid;
391
392 $needs_creation = $live_import;
393
394 if (PVE::Storage::parse_volume_id($source, 1)) { # PVE-managed volume
395 if ($live_import && $ds ne 'efidisk0') {
396 my $path = PVE::Storage::path($storecfg, $source)
397 or die "failed to get a path for '$source'\n";
398 $source = $path;
399 ($size, my $source_format) = PVE::Storage::file_size_info($source);
400 die "could not get file size of $source\n" if !$size;
401 $live_import_mapping->{$ds} = {
402 path => $source,
403 format => $source_format,
404 };
405 } else {
406 my $dest_info = {
407 vmid => $vmid,
408 drivename => $ds,
409 storage => $storeid,
410 format => $disk->{format},
411 };
412
413 $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($conf, $disk)
414 if $ds eq 'efidisk0';
415
416 ($dst_volid, $size) = eval {
417 $import_from_volid->($storecfg, $source, $dest_info, $vollist);
418 };
419 die "cannot import from '$source' - $@" if $@;
420 }
421 } else {
422 $source = PVE::Storage::abs_filesystem_path($storecfg, $source, 1);
423 ($size, my $source_format) = PVE::Storage::file_size_info($source);
424 die "could not get file size of $source\n" if !$size;
425
426 if ($live_import && $ds ne 'efidisk0') {
427 $live_import_mapping->{$ds} = {
428 path => $source,
429 format => $source_format,
430 };
431 } else {
432 (undef, $dst_volid) = PVE::QemuServer::ImportDisk::do_import(
433 $source,
434 $vmid,
435 $storeid,
436 {
437 drive_name => $ds,
438 format => $disk->{format},
439 'skip-config-update' => 1,
440 },
441 );
442 push @$vollist, $dst_volid;
443 }
444 }
445
446 if ($needs_creation) {
447 $size = PVE::Tools::convert_size($size, 'b' => 'kb'); # vdisk_alloc uses kb
448 } else {
449 $disk->{file} = $dst_volid;
450 $disk->{size} = $size;
451 delete $disk->{format}; # no longer needed
452 $res->{$ds} = PVE::QemuServer::print_drive($disk);
453 }
454 }
455
456 if ($needs_creation) {
457 my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
458 my $fmt = $disk->{format} || $defformat;
459
460 my $volid;
461 if ($ds eq 'efidisk0') {
462 my $smm = PVE::QemuServer::Machine::machine_type_is_q35($conf);
463 ($volid, $size) = PVE::QemuServer::create_efidisk(
464 $storecfg, $storeid, $vmid, $fmt, $arch, $disk, $smm);
465 } elsif ($ds eq 'tpmstate0') {
466 # swtpm can only use raw volumes, and uses a fixed size
467 $size = PVE::Tools::convert_size(PVE::QemuServer::Drive::TPMSTATE_DISK_SIZE, 'b' => 'kb');
468 $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, "raw", undef, $size);
469 } else {
470 $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $size);
471 }
472 push @$vollist, $volid;
473 $disk->{file} = $volid;
474 $disk->{size} = PVE::Tools::convert_size($size, 'kb' => 'b');
475 delete $disk->{format}; # no longer needed
476 $res->{$ds} = PVE::QemuServer::print_drive($disk);
477 }
478
479 print "$ds: successfully created disk '$res->{$ds}'\n";
480 } else {
481 PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
482 if ($storeid) {
483 my ($vtype) = PVE::Storage::parse_volname($storecfg, $volid);
484 die "cannot use volume $volid - content type needs to be 'images' or 'iso'"
485 if $vtype ne 'images' && $vtype ne 'iso';
486
487 if (PVE::QemuServer::Drive::drive_is_cloudinit($disk)) {
488 if (
489 my $ci_key = PVE::QemuConfig->has_cloudinit($conf, $ds)
490 || PVE::QemuConfig->has_cloudinit($conf->{pending} || {}, $ds)
491 || PVE::QemuConfig->has_cloudinit($res, $ds)
492 ) {
493 die "$ds - cloud-init drive is already attached at '$ci_key'\n";
494 }
495 }
496 }
497
498 PVE::Storage::activate_volumes($storecfg, [ $volid ]) if $storeid;
499
500 my $size = PVE::Storage::volume_size_info($storecfg, $volid);
501 die "volume $volid does not exist\n" if !$size;
502 $disk->{size} = $size;
503
504 $res->{$ds} = PVE::QemuServer::print_drive($disk);
505 }
506 };
507
508 eval { $foreach_volume_with_alloc->($settings, $code); };
509
510 # free allocated images on error
511 if (my $err = $@) {
512 syslog('err', "VM $vmid creating disks failed");
513 foreach my $volid (@$vollist) {
514 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
515 warn $@ if $@;
516 }
517 die $err;
518 }
519
520 # don't return empty import mappings
521 $live_import_mapping = undef if !%$live_import_mapping;
522
523 return ($vollist, $res, $live_import_mapping);
524 };
525
526 my $check_cpu_model_access = sub {
527 my ($rpcenv, $authuser, $new, $existing) = @_;
528
529 return if !defined($new->{cpu});
530
531 my $cpu = PVE::JSONSchema::check_format('pve-vm-cpu-conf', $new->{cpu});
532 return if !$cpu || !$cpu->{cputype}; # always allow default
533 my $cputype = $cpu->{cputype};
534
535 if ($existing && $existing->{cpu}) {
536 # changing only other settings doesn't require permissions for CPU model
537 my $existingCpu = PVE::JSONSchema::check_format('pve-vm-cpu-conf', $existing->{cpu});
538 return if $existingCpu->{cputype} eq $cputype;
539 }
540
541 if (PVE::QemuServer::CPUConfig::is_custom_model($cputype)) {
542 $rpcenv->check($authuser, "/nodes", ['Sys.Audit']);
543 }
544 };
545
546 my $cpuoptions = {
547 'cores' => 1,
548 'cpu' => 1,
549 'cpulimit' => 1,
550 'cpuunits' => 1,
551 'numa' => 1,
552 'smp' => 1,
553 'sockets' => 1,
554 'vcpus' => 1,
555 };
556
557 my $memoryoptions = {
558 'memory' => 1,
559 'balloon' => 1,
560 'shares' => 1,
561 };
562
563 my $hwtypeoptions = {
564 'acpi' => 1,
565 'hotplug' => 1,
566 'kvm' => 1,
567 'machine' => 1,
568 'scsihw' => 1,
569 'smbios1' => 1,
570 'tablet' => 1,
571 'vga' => 1,
572 'watchdog' => 1,
573 'audio0' => 1,
574 };
575
576 my $generaloptions = {
577 'agent' => 1,
578 'autostart' => 1,
579 'bios' => 1,
580 'description' => 1,
581 'keyboard' => 1,
582 'localtime' => 1,
583 'migrate_downtime' => 1,
584 'migrate_speed' => 1,
585 'name' => 1,
586 'onboot' => 1,
587 'ostype' => 1,
588 'protection' => 1,
589 'reboot' => 1,
590 'startdate' => 1,
591 'startup' => 1,
592 'tdf' => 1,
593 'template' => 1,
594 };
595
596 my $vmpoweroptions = {
597 'freeze' => 1,
598 };
599
600 my $diskoptions = {
601 'boot' => 1,
602 'bootdisk' => 1,
603 'vmstatestorage' => 1,
604 };
605
606 my $cloudinitoptions = {
607 cicustom => 1,
608 cipassword => 1,
609 citype => 1,
610 ciuser => 1,
611 ciupgrade => 1,
612 nameserver => 1,
613 searchdomain => 1,
614 sshkeys => 1,
615 };
616
617 my $check_vm_create_serial_perm = sub {
618 my ($rpcenv, $authuser, $vmid, $pool, $param) = @_;
619
620 return 1 if $authuser eq 'root@pam';
621
622 foreach my $opt (keys %{$param}) {
623 next if $opt !~ m/^serial\d+$/;
624
625 if ($param->{$opt} eq 'socket') {
626 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
627 } else {
628 die "only root can set '$opt' config for real devices\n";
629 }
630 }
631
632 return 1;
633 };
634
635 my sub check_usb_perm {
636 my ($rpcenv, $authuser, $vmid, $pool, $opt, $value) = @_;
637
638 return 1 if $authuser eq 'root@pam';
639
640 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
641
642 my $device = PVE::JSONSchema::parse_property_string('pve-qm-usb', $value);
643 if ($device->{host} && $device->{host} !~ m/^spice$/i) {
644 die "only root can set '$opt' config for real devices\n";
645 } elsif ($device->{mapping}) {
646 $rpcenv->check_full($authuser, "/mapping/usb/$device->{mapping}", ['Mapping.Use']);
647 } else {
648 die "either 'host' or 'mapping' must be set.\n";
649 }
650
651 return 1;
652 }
653
654 my sub check_vm_create_usb_perm {
655 my ($rpcenv, $authuser, $vmid, $pool, $param) = @_;
656
657 return 1 if $authuser eq 'root@pam';
658
659 foreach my $opt (keys %{$param}) {
660 next if $opt !~ m/^usb\d+$/;
661 check_usb_perm($rpcenv, $authuser, $vmid, $pool, $opt, $param->{$opt});
662 }
663
664 return 1;
665 };
666
667 my sub check_hostpci_perm {
668 my ($rpcenv, $authuser, $vmid, $pool, $opt, $value) = @_;
669
670 return 1 if $authuser eq 'root@pam';
671
672 my $device = PVE::JSONSchema::parse_property_string('pve-qm-hostpci', $value);
673 if ($device->{host}) {
674 die "only root can set '$opt' config for non-mapped devices\n";
675 } elsif ($device->{mapping}) {
676 $rpcenv->check_full($authuser, "/mapping/pci/$device->{mapping}", ['Mapping.Use']);
677 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
678 } else {
679 die "either 'host' or 'mapping' must be set.\n";
680 }
681
682 return 1;
683 }
684
685 my sub check_vm_create_hostpci_perm {
686 my ($rpcenv, $authuser, $vmid, $pool, $param) = @_;
687
688 return 1 if $authuser eq 'root@pam';
689
690 foreach my $opt (keys %{$param}) {
691 next if $opt !~ m/^hostpci\d+$/;
692 check_hostpci_perm($rpcenv, $authuser, $vmid, $pool, $opt, $param->{$opt});
693 }
694
695 return 1;
696 };
697
698 my $check_vm_modify_config_perm = sub {
699 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
700
701 return 1 if $authuser eq 'root@pam';
702
703 foreach my $opt (@$key_list) {
704 # some checks (e.g., disk, serial port, usb) need to be done somewhere
705 # else, as there the permission can be value dependend
706 next if PVE::QemuServer::is_valid_drivename($opt);
707 next if $opt eq 'cdrom';
708 next if $opt =~ m/^(?:unused|serial|usb|hostpci)\d+$/;
709 next if $opt eq 'tags';
710
711
712 if ($cpuoptions->{$opt} || $opt =~ m/^numa\d+$/) {
713 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
714 } elsif ($memoryoptions->{$opt}) {
715 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
716 } elsif ($hwtypeoptions->{$opt}) {
717 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
718 } elsif ($generaloptions->{$opt}) {
719 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
720 # special case for startup since it changes host behaviour
721 if ($opt eq 'startup') {
722 $rpcenv->check_full($authuser, "/", ['Sys.Modify']);
723 }
724 } elsif ($vmpoweroptions->{$opt}) {
725 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.PowerMgmt']);
726 } elsif ($diskoptions->{$opt}) {
727 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
728 } elsif ($opt =~ m/^net\d+$/) {
729 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
730 } elsif ($cloudinitoptions->{$opt} || $opt =~ m/^ipconfig\d+$/) {
731 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Cloudinit', 'VM.Config.Network'], 1);
732 } elsif ($opt eq 'vmstate') {
733 # the user needs Disk and PowerMgmt privileges to change the vmstate
734 # also needs privileges on the storage, that will be checked later
735 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk', 'VM.PowerMgmt' ]);
736 } else {
737 # catches args, lock, etc.
738 # new options will be checked here
739 die "only root can set '$opt' config\n";
740 }
741 }
742
743 return 1;
744 };
745
746 sub assert_scsi_feature_compatibility {
747 my ($opt, $conf, $storecfg, $drive_attributes) = @_;
748
749 my $drive = PVE::QemuServer::Drive::parse_drive($opt, $drive_attributes, 1);
750
751 my $machine_type = PVE::QemuServer::get_vm_machine($conf, undef, $conf->{arch});
752 my $machine_version = PVE::QemuServer::Machine::extract_version(
753 $machine_type, PVE::QemuServer::kvm_user_version());
754 my $drivetype = PVE::QemuServer::Drive::get_scsi_device_type(
755 $drive, $storecfg, $machine_version);
756
757 if ($drivetype ne 'hd' && $drivetype ne 'cd') {
758 if ($drive->{product}) {
759 raise_param_exc({
760 $opt => "Passing of product information is only supported for 'scsi-hd' and "
761 ."'scsi-cd' devices (e.g. not pass-through).",
762 });
763 }
764 if ($drive->{vendor}) {
765 raise_param_exc({
766 $opt => "Passing of vendor information is only supported for 'scsi-hd' and "
767 ."'scsi-cd' devices (e.g. not pass-through).",
768 });
769 }
770 }
771 }
772
773 __PACKAGE__->register_method({
774 name => 'vmlist',
775 path => '',
776 method => 'GET',
777 description => "Virtual machine index (per node).",
778 permissions => {
779 description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
780 user => 'all',
781 },
782 proxyto => 'node',
783 protected => 1, # qemu pid files are only readable by root
784 parameters => {
785 additionalProperties => 0,
786 properties => {
787 node => get_standard_option('pve-node'),
788 full => {
789 type => 'boolean',
790 optional => 1,
791 description => "Determine the full status of active VMs.",
792 },
793 },
794 },
795 returns => {
796 type => 'array',
797 items => {
798 type => "object",
799 properties => $PVE::QemuServer::vmstatus_return_properties,
800 },
801 links => [ { rel => 'child', href => "{vmid}" } ],
802 },
803 code => sub {
804 my ($param) = @_;
805
806 my $rpcenv = PVE::RPCEnvironment::get();
807 my $authuser = $rpcenv->get_user();
808
809 my $vmstatus = PVE::QemuServer::vmstatus(undef, $param->{full});
810
811 my $res = [];
812 foreach my $vmid (keys %$vmstatus) {
813 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
814
815 my $data = $vmstatus->{$vmid};
816 push @$res, $data;
817 }
818
819 return $res;
820 }});
821
822 my $parse_restore_archive = sub {
823 my ($storecfg, $archive) = @_;
824
825 my ($archive_storeid, $archive_volname) = PVE::Storage::parse_volume_id($archive, 1);
826
827 my $res = {};
828
829 if (defined($archive_storeid)) {
830 my $scfg = PVE::Storage::storage_config($storecfg, $archive_storeid);
831 $res->{volid} = $archive;
832 if ($scfg->{type} eq 'pbs') {
833 $res->{type} = 'pbs';
834 return $res;
835 }
836 }
837 my $path = PVE::Storage::abs_filesystem_path($storecfg, $archive);
838 $res->{type} = 'file';
839 $res->{path} = $path;
840 return $res;
841 };
842
843 __PACKAGE__->register_method({
844 name => 'create_vm',
845 path => '',
846 method => 'POST',
847 description => "Create or restore a virtual machine.",
848 permissions => {
849 description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
850 "For restore (option 'archive'), it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
851 "If you create disks you need 'Datastore.AllocateSpace' on any used storage." .
852 "If you use a bridge/vlan, you need 'SDN.Use' on any used bridge/vlan.",
853 user => 'all', # check inside
854 },
855 protected => 1,
856 proxyto => 'node',
857 parameters => {
858 additionalProperties => 0,
859 properties => PVE::QemuServer::json_config_properties(
860 {
861 node => get_standard_option('pve-node'),
862 vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
863 archive => {
864 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.",
865 type => 'string',
866 optional => 1,
867 maxLength => 255,
868 completion => \&PVE::QemuServer::complete_backup_archives,
869 },
870 storage => get_standard_option('pve-storage-id', {
871 description => "Default storage.",
872 optional => 1,
873 completion => \&PVE::QemuServer::complete_storage,
874 }),
875 force => {
876 optional => 1,
877 type => 'boolean',
878 description => "Allow to overwrite existing VM.",
879 requires => 'archive',
880 },
881 unique => {
882 optional => 1,
883 type => 'boolean',
884 description => "Assign a unique random ethernet address.",
885 requires => 'archive',
886 },
887 'live-restore' => {
888 optional => 1,
889 type => 'boolean',
890 description => "Start the VM immediately while importing or restoring in the background.",
891 },
892 pool => {
893 optional => 1,
894 type => 'string', format => 'pve-poolid',
895 description => "Add the VM to the specified pool.",
896 },
897 bwlimit => {
898 description => "Override I/O bandwidth limit (in KiB/s).",
899 optional => 1,
900 type => 'integer',
901 minimum => '0',
902 default => 'restore limit from datacenter or storage config',
903 },
904 start => {
905 optional => 1,
906 type => 'boolean',
907 default => 0,
908 description => "Start VM after it was created successfully.",
909 },
910 },
911 1, # with_disk_alloc
912 ),
913 },
914 returns => {
915 type => 'string',
916 },
917 code => sub {
918 my ($param) = @_;
919
920 my $rpcenv = PVE::RPCEnvironment::get();
921 my $authuser = $rpcenv->get_user();
922
923 my $node = extract_param($param, 'node');
924 my $vmid = extract_param($param, 'vmid');
925
926 my $archive = extract_param($param, 'archive');
927 my $is_restore = !!$archive;
928
929 my $bwlimit = extract_param($param, 'bwlimit');
930 my $force = extract_param($param, 'force');
931 my $pool = extract_param($param, 'pool');
932 my $start_after_create = extract_param($param, 'start');
933 my $storage = extract_param($param, 'storage');
934 my $unique = extract_param($param, 'unique');
935 my $live_restore = extract_param($param, 'live-restore');
936
937 if (defined(my $ssh_keys = $param->{sshkeys})) {
938 $ssh_keys = URI::Escape::uri_unescape($ssh_keys);
939 PVE::Tools::validate_ssh_public_keys($ssh_keys);
940 }
941
942 $param->{cpuunits} = PVE::CGroup::clamp_cpu_shares($param->{cpuunits})
943 if defined($param->{cpuunits}); # clamp value depending on cgroup version
944
945 PVE::Cluster::check_cfs_quorum();
946
947 my $filename = PVE::QemuConfig->config_file($vmid);
948 my $storecfg = PVE::Storage::config();
949
950 if (defined($pool)) {
951 $rpcenv->check_pool_exist($pool);
952 }
953
954 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
955 if defined($storage);
956
957 if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) {
958 # OK
959 } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) {
960 # OK
961 } elsif ($archive && $force && (-f $filename) &&
962 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
963 # OK: user has VM.Backup permissions and wants to restore an existing VM
964 } else {
965 raise_perm_exc();
966 }
967
968 if ($archive) {
969 for my $opt (sort keys $param->%*) {
970 if (PVE::QemuServer::Drive::is_valid_drivename($opt)) {
971 raise_param_exc({ $opt => "option conflicts with option 'archive'" });
972 }
973 }
974
975 if ($archive eq '-') {
976 die "pipe requires cli environment\n" if $rpcenv->{type} ne 'cli';
977 $archive = { type => 'pipe' };
978 } else {
979 PVE::Storage::check_volume_access(
980 $rpcenv,
981 $authuser,
982 $storecfg,
983 $vmid,
984 $archive,
985 'backup',
986 );
987
988 $archive = $parse_restore_archive->($storecfg, $archive);
989 }
990 }
991
992 if (scalar(keys $param->%*) > 0) {
993 &$resolve_cdrom_alias($param);
994
995 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
996
997 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
998
999 &$check_vm_create_serial_perm($rpcenv, $authuser, $vmid, $pool, $param);
1000 check_vm_create_usb_perm($rpcenv, $authuser, $vmid, $pool, $param);
1001 check_vm_create_hostpci_perm($rpcenv, $authuser, $vmid, $pool, $param);
1002
1003 PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $param);
1004 &$check_cpu_model_access($rpcenv, $authuser, $param);
1005
1006 $check_drive_param->($param, $storecfg);
1007
1008 PVE::QemuServer::add_random_macs($param);
1009 }
1010
1011 my $emsg = $is_restore ? "unable to restore VM $vmid -" : "unable to create VM $vmid -";
1012
1013 eval { PVE::QemuConfig->create_and_lock_config($vmid, $force) };
1014 die "$emsg $@" if $@;
1015
1016 my $restored_data = 0;
1017 my $restorefn = sub {
1018 my $conf = PVE::QemuConfig->load_config($vmid);
1019
1020 PVE::QemuConfig->check_protection($conf, $emsg);
1021
1022 die "$emsg vm is running\n" if PVE::QemuServer::check_running($vmid);
1023
1024 my $realcmd = sub {
1025 my $restore_options = {
1026 storage => $storage,
1027 pool => $pool,
1028 unique => $unique,
1029 bwlimit => $bwlimit,
1030 live => $live_restore,
1031 override_conf => $param,
1032 };
1033 if (my $volid = $archive->{volid}) {
1034 # best effort, real check is after restoring!
1035 my $merged = eval {
1036 my $old_conf = PVE::Storage::extract_vzdump_config($storecfg, $volid);
1037 PVE::QemuServer::restore_merge_config("backup/qemu-server/$vmid.conf", $old_conf, $param);
1038 };
1039 if ($@) {
1040 warn "Could not extract backed up config: $@\n";
1041 warn "Skipping early checks!\n";
1042 } else {
1043 PVE::QemuServer::check_restore_permissions($rpcenv, $authuser, $merged);
1044 }
1045 }
1046 if ($archive->{type} eq 'file' || $archive->{type} eq 'pipe') {
1047 die "live-restore is only compatible with backup images from a Proxmox Backup Server\n"
1048 if $live_restore;
1049 PVE::QemuServer::restore_file_archive($archive->{path} // '-', $vmid, $authuser, $restore_options);
1050 } elsif ($archive->{type} eq 'pbs') {
1051 PVE::QemuServer::restore_proxmox_backup_archive($archive->{volid}, $vmid, $authuser, $restore_options);
1052 } else {
1053 die "unknown backup archive type\n";
1054 }
1055 $restored_data = 1;
1056
1057 my $restored_conf = PVE::QemuConfig->load_config($vmid);
1058 # Convert restored VM to template if backup was VM template
1059 if (PVE::QemuConfig->is_template($restored_conf)) {
1060 warn "Convert to template.\n";
1061 eval { PVE::QemuServer::template_create($vmid, $restored_conf) };
1062 warn $@ if $@;
1063 }
1064
1065 PVE::QemuServer::create_ifaces_ipams_ips($restored_conf, $vmid) if $unique;
1066 };
1067
1068 # ensure no old replication state are exists
1069 PVE::ReplicationState::delete_guest_states($vmid);
1070
1071 PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd);
1072
1073 if ($start_after_create && !$live_restore) {
1074 print "Execute autostart\n";
1075 eval { PVE::API2::Qemu->vm_start({ vmid => $vmid, node => $node }) };
1076 warn $@ if $@;
1077 }
1078 };
1079
1080 my $createfn = sub {
1081 my $live_import_mapping = {};
1082
1083 # ensure no old replication state are exists
1084 PVE::ReplicationState::delete_guest_states($vmid);
1085
1086 my $realcmd = sub {
1087 my $conf = $param;
1088 my $arch = PVE::QemuServer::get_vm_arch($conf);
1089
1090 for my $opt (sort keys $param->%*) {
1091 next if $opt !~ m/^scsi\d+$/;
1092 assert_scsi_feature_compatibility($opt, $conf, $storecfg, $param->{$opt});
1093 }
1094
1095 $conf->{meta} = PVE::QemuServer::new_meta_info_string();
1096
1097 my $vollist = [];
1098 eval {
1099 ($vollist, my $created_opts, $live_import_mapping) = create_disks(
1100 $rpcenv,
1101 $authuser,
1102 $conf,
1103 $arch,
1104 $storecfg,
1105 $vmid,
1106 $pool,
1107 $param,
1108 $storage,
1109 $live_restore,
1110 );
1111 $conf->{$_} = $created_opts->{$_} for keys $created_opts->%*;
1112
1113 if (!$conf->{boot}) {
1114 my $devs = PVE::QemuServer::get_default_bootdevices($conf);
1115 $conf->{boot} = PVE::QemuServer::print_bootorder($devs);
1116 }
1117
1118 my $vga = PVE::QemuServer::parse_vga($conf->{vga});
1119 PVE::QemuServer::assert_clipboard_config($vga);
1120
1121 # auto generate uuid if user did not specify smbios1 option
1122 if (!$conf->{smbios1}) {
1123 $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid();
1124 }
1125
1126 if ((!defined($conf->{vmgenid}) || $conf->{vmgenid} eq '1') && $arch ne 'aarch64') {
1127 $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
1128 }
1129
1130 my $machine = $conf->{machine};
1131 if (!$machine || $machine =~ m/^(?:pc|q35|virt)$/) {
1132 # always pin Windows' machine version on create, they get to easily confused
1133 if (PVE::QemuServer::Helpers::windows_version($conf->{ostype})) {
1134 $conf->{machine} = PVE::QemuServer::windows_get_pinned_machine_version($machine);
1135 }
1136 }
1137
1138 $conf->{lock} = 'import' if $live_import_mapping;
1139
1140 PVE::QemuConfig->write_config($vmid, $conf);
1141 };
1142 my $err = $@;
1143
1144 if ($err) {
1145 foreach my $volid (@$vollist) {
1146 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
1147 warn $@ if $@;
1148 }
1149 die "$emsg $err";
1150 }
1151
1152 PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
1153
1154 PVE::QemuServer::create_ifaces_ipams_ips($conf, $vmid);
1155 };
1156
1157 PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd);
1158
1159 if ($start_after_create && !$live_restore) {
1160 print "Execute autostart\n";
1161 eval { PVE::API2::Qemu->vm_start({vmid => $vmid, node => $node}) };
1162 warn $@ if $@;
1163 return;
1164 } else {
1165 return $live_import_mapping;
1166 }
1167 };
1168
1169 my ($code, $worker_name);
1170 if ($is_restore) {
1171 $worker_name = 'qmrestore';
1172 $code = sub {
1173 eval { $restorefn->() };
1174 if (my $err = $@) {
1175 eval { PVE::QemuConfig->remove_lock($vmid, 'create') };
1176 warn $@ if $@;
1177 if ($restored_data) {
1178 warn "error after data was restored, VM disks should be OK but config may "
1179 ."require adaptions. VM $vmid state is NOT cleaned up.\n";
1180 } else {
1181 warn "error before or during data restore, some or all disks were not "
1182 ."completely restored. VM $vmid state is NOT cleaned up.\n";
1183 }
1184 die $err;
1185 }
1186 };
1187 } else {
1188 $worker_name = 'qmcreate';
1189 $code = sub {
1190 # If a live import was requested the create function returns
1191 # the mapping for the startup.
1192 my $live_import_mapping = eval { $createfn->() };
1193 if (my $err = $@) {
1194 eval {
1195 my $conffile = PVE::QemuConfig->config_file($vmid);
1196 unlink($conffile) or die "failed to remove config file: $!\n";
1197 };
1198 warn $@ if $@;
1199 die $err;
1200 }
1201
1202 if ($live_import_mapping) {
1203 my $import_options = {
1204 bwlimit => $bwlimit,
1205 live => 1,
1206 };
1207
1208 my $conf = PVE::QemuConfig->load_config($vmid);
1209 PVE::QemuServer::live_import_from_files(
1210 $live_import_mapping,
1211 $vmid,
1212 $conf,
1213 $import_options,
1214 );
1215 }
1216 };
1217 }
1218
1219 return $rpcenv->fork_worker($worker_name, $vmid, $authuser, $code);
1220 }});
1221
1222 __PACKAGE__->register_method({
1223 name => 'vmdiridx',
1224 path => '{vmid}',
1225 method => 'GET',
1226 proxyto => 'node',
1227 description => "Directory index",
1228 permissions => {
1229 user => 'all',
1230 },
1231 parameters => {
1232 additionalProperties => 0,
1233 properties => {
1234 node => get_standard_option('pve-node'),
1235 vmid => get_standard_option('pve-vmid'),
1236 },
1237 },
1238 returns => {
1239 type => 'array',
1240 items => {
1241 type => "object",
1242 properties => {
1243 subdir => { type => 'string' },
1244 },
1245 },
1246 links => [ { rel => 'child', href => "{subdir}" } ],
1247 },
1248 code => sub {
1249 my ($param) = @_;
1250
1251 my $res = [
1252 { subdir => 'config' },
1253 { subdir => 'cloudinit' },
1254 { subdir => 'pending' },
1255 { subdir => 'status' },
1256 { subdir => 'unlink' },
1257 { subdir => 'vncproxy' },
1258 { subdir => 'termproxy' },
1259 { subdir => 'migrate' },
1260 { subdir => 'resize' },
1261 { subdir => 'move' },
1262 { subdir => 'rrd' },
1263 { subdir => 'rrddata' },
1264 { subdir => 'monitor' },
1265 { subdir => 'agent' },
1266 { subdir => 'snapshot' },
1267 { subdir => 'spiceproxy' },
1268 { subdir => 'sendkey' },
1269 { subdir => 'firewall' },
1270 { subdir => 'mtunnel' },
1271 { subdir => 'remote_migrate' },
1272 ];
1273
1274 return $res;
1275 }});
1276
1277 __PACKAGE__->register_method ({
1278 subclass => "PVE::API2::Firewall::VM",
1279 path => '{vmid}/firewall',
1280 });
1281
1282 __PACKAGE__->register_method ({
1283 subclass => "PVE::API2::Qemu::Agent",
1284 path => '{vmid}/agent',
1285 });
1286
1287 __PACKAGE__->register_method({
1288 name => 'rrd',
1289 path => '{vmid}/rrd',
1290 method => 'GET',
1291 protected => 1, # fixme: can we avoid that?
1292 permissions => {
1293 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1294 },
1295 description => "Read VM RRD statistics (returns PNG)",
1296 parameters => {
1297 additionalProperties => 0,
1298 properties => {
1299 node => get_standard_option('pve-node'),
1300 vmid => get_standard_option('pve-vmid'),
1301 timeframe => {
1302 description => "Specify the time frame you are interested in.",
1303 type => 'string',
1304 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
1305 },
1306 ds => {
1307 description => "The list of datasources you want to display.",
1308 type => 'string', format => 'pve-configid-list',
1309 },
1310 cf => {
1311 description => "The RRD consolidation function",
1312 type => 'string',
1313 enum => [ 'AVERAGE', 'MAX' ],
1314 optional => 1,
1315 },
1316 },
1317 },
1318 returns => {
1319 type => "object",
1320 properties => {
1321 filename => { type => 'string' },
1322 },
1323 },
1324 code => sub {
1325 my ($param) = @_;
1326
1327 return PVE::RRD::create_rrd_graph(
1328 "pve2-vm/$param->{vmid}", $param->{timeframe},
1329 $param->{ds}, $param->{cf});
1330
1331 }});
1332
1333 __PACKAGE__->register_method({
1334 name => 'rrddata',
1335 path => '{vmid}/rrddata',
1336 method => 'GET',
1337 protected => 1, # fixme: can we avoid that?
1338 permissions => {
1339 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1340 },
1341 description => "Read VM RRD statistics",
1342 parameters => {
1343 additionalProperties => 0,
1344 properties => {
1345 node => get_standard_option('pve-node'),
1346 vmid => get_standard_option('pve-vmid'),
1347 timeframe => {
1348 description => "Specify the time frame you are interested in.",
1349 type => 'string',
1350 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
1351 },
1352 cf => {
1353 description => "The RRD consolidation function",
1354 type => 'string',
1355 enum => [ 'AVERAGE', 'MAX' ],
1356 optional => 1,
1357 },
1358 },
1359 },
1360 returns => {
1361 type => "array",
1362 items => {
1363 type => "object",
1364 properties => {},
1365 },
1366 },
1367 code => sub {
1368 my ($param) = @_;
1369
1370 return PVE::RRD::create_rrd_data(
1371 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
1372 }});
1373
1374
1375 __PACKAGE__->register_method({
1376 name => 'vm_config',
1377 path => '{vmid}/config',
1378 method => 'GET',
1379 proxyto => 'node',
1380 description => "Get the virtual machine configuration with pending configuration " .
1381 "changes applied. Set the 'current' parameter to get the current configuration instead.",
1382 permissions => {
1383 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1384 },
1385 parameters => {
1386 additionalProperties => 0,
1387 properties => {
1388 node => get_standard_option('pve-node'),
1389 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
1390 current => {
1391 description => "Get current values (instead of pending values).",
1392 optional => 1,
1393 default => 0,
1394 type => 'boolean',
1395 },
1396 snapshot => get_standard_option('pve-snapshot-name', {
1397 description => "Fetch config values from given snapshot.",
1398 optional => 1,
1399 completion => sub {
1400 my ($cmd, $pname, $cur, $args) = @_;
1401 PVE::QemuConfig->snapshot_list($args->[0]);
1402 },
1403 }),
1404 },
1405 },
1406 returns => {
1407 description => "The VM configuration.",
1408 type => "object",
1409 properties => PVE::QemuServer::json_config_properties({
1410 digest => {
1411 type => 'string',
1412 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
1413 }
1414 }),
1415 },
1416 code => sub {
1417 my ($param) = @_;
1418
1419 raise_param_exc({ snapshot => "cannot use 'snapshot' parameter with 'current'",
1420 current => "cannot use 'snapshot' parameter with 'current'"})
1421 if ($param->{snapshot} && $param->{current});
1422
1423 my $conf;
1424 if ($param->{snapshot}) {
1425 $conf = PVE::QemuConfig->load_snapshot_config($param->{vmid}, $param->{snapshot});
1426 } else {
1427 $conf = PVE::QemuConfig->load_current_config($param->{vmid}, $param->{current});
1428 }
1429 $conf->{cipassword} = '**********' if $conf->{cipassword};
1430 return $conf;
1431
1432 }});
1433
1434 __PACKAGE__->register_method({
1435 name => 'vm_pending',
1436 path => '{vmid}/pending',
1437 method => 'GET',
1438 proxyto => 'node',
1439 description => "Get the virtual machine configuration with both current and pending values.",
1440 permissions => {
1441 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1442 },
1443 parameters => {
1444 additionalProperties => 0,
1445 properties => {
1446 node => get_standard_option('pve-node'),
1447 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
1448 },
1449 },
1450 returns => {
1451 type => "array",
1452 items => {
1453 type => "object",
1454 properties => {
1455 key => {
1456 description => "Configuration option name.",
1457 type => 'string',
1458 },
1459 value => {
1460 description => "Current value.",
1461 type => 'string',
1462 optional => 1,
1463 },
1464 pending => {
1465 description => "Pending value.",
1466 type => 'string',
1467 optional => 1,
1468 },
1469 delete => {
1470 description => "Indicates a pending delete request if present and not 0. " .
1471 "The value 2 indicates a force-delete request.",
1472 type => 'integer',
1473 minimum => 0,
1474 maximum => 2,
1475 optional => 1,
1476 },
1477 },
1478 },
1479 },
1480 code => sub {
1481 my ($param) = @_;
1482
1483 my $conf = PVE::QemuConfig->load_config($param->{vmid});
1484
1485 my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
1486
1487 $conf->{cipassword} = '**********' if defined($conf->{cipassword});
1488 $conf->{pending}->{cipassword} = '********** ' if defined($conf->{pending}->{cipassword});
1489
1490 return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash);
1491 }});
1492
1493 __PACKAGE__->register_method({
1494 name => 'cloudinit_pending',
1495 path => '{vmid}/cloudinit',
1496 method => 'GET',
1497 proxyto => 'node',
1498 description => "Get the cloudinit configuration with both current and pending values.",
1499 permissions => {
1500 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1501 },
1502 parameters => {
1503 additionalProperties => 0,
1504 properties => {
1505 node => get_standard_option('pve-node'),
1506 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
1507 },
1508 },
1509 returns => {
1510 type => "array",
1511 items => {
1512 type => "object",
1513 properties => {
1514 key => {
1515 description => "Configuration option name.",
1516 type => 'string',
1517 },
1518 value => {
1519 description => "Value as it was used to generate the current cloudinit image.",
1520 type => 'string',
1521 optional => 1,
1522 },
1523 pending => {
1524 description => "The new pending value.",
1525 type => 'string',
1526 optional => 1,
1527 },
1528 delete => {
1529 description => "Indicates a pending delete request if present and not 0. ",
1530 type => 'integer',
1531 minimum => 0,
1532 maximum => 1,
1533 optional => 1,
1534 },
1535 },
1536 },
1537 },
1538 code => sub {
1539 my ($param) = @_;
1540
1541 my $vmid = $param->{vmid};
1542 my $conf = PVE::QemuConfig->load_config($vmid);
1543
1544 my $ci = $conf->{cloudinit};
1545
1546 $conf->{cipassword} = '**********' if exists $conf->{cipassword};
1547 $ci->{cipassword} = '**********' if exists $ci->{cipassword};
1548
1549 my $res = [];
1550
1551 # All the values that got added
1552 my $added = delete($ci->{added}) // '';
1553 for my $key (PVE::Tools::split_list($added)) {
1554 push @$res, { key => $key, pending => $conf->{$key} };
1555 }
1556
1557 # All already existing values (+ their new value, if it exists)
1558 for my $opt (keys %$cloudinitoptions) {
1559 next if !$conf->{$opt};
1560 next if $added =~ m/$opt/;
1561 my $item = {
1562 key => $opt,
1563 };
1564
1565 if (my $pending = $ci->{$opt}) {
1566 $item->{value} = $pending;
1567 $item->{pending} = $conf->{$opt};
1568 } else {
1569 $item->{value} = $conf->{$opt},
1570 }
1571
1572 push @$res, $item;
1573 }
1574
1575 # Now, we'll find the deleted ones
1576 for my $opt (keys %$ci) {
1577 next if $conf->{$opt};
1578 push @$res, { key => $opt, delete => 1 };
1579 }
1580
1581 return $res;
1582 }});
1583
1584 __PACKAGE__->register_method({
1585 name => 'cloudinit_update',
1586 path => '{vmid}/cloudinit',
1587 method => 'PUT',
1588 protected => 1,
1589 proxyto => 'node',
1590 description => "Regenerate and change cloudinit config drive.",
1591 permissions => {
1592 check => ['perm', '/vms/{vmid}', ['VM.Config.Cloudinit']],
1593 },
1594 parameters => {
1595 additionalProperties => 0,
1596 properties => {
1597 node => get_standard_option('pve-node'),
1598 vmid => get_standard_option('pve-vmid'),
1599 },
1600 },
1601 returns => { type => 'null' },
1602 code => sub {
1603 my ($param) = @_;
1604
1605 my $rpcenv = PVE::RPCEnvironment::get();
1606 my $authuser = $rpcenv->get_user();
1607
1608 my $vmid = extract_param($param, 'vmid');
1609
1610 PVE::QemuConfig->lock_config($vmid, sub {
1611 my $conf = PVE::QemuConfig->load_config($vmid);
1612 PVE::QemuConfig->check_lock($conf);
1613
1614 my $storecfg = PVE::Storage::config();
1615 PVE::QemuServer::vmconfig_update_cloudinit_drive($storecfg, $conf, $vmid);
1616 });
1617 return;
1618 }});
1619
1620 # POST/PUT {vmid}/config implementation
1621 #
1622 # The original API used PUT (idempotent) an we assumed that all operations
1623 # are fast. But it turned out that almost any configuration change can
1624 # involve hot-plug actions, or disk alloc/free. Such actions can take long
1625 # time to complete and have side effects (not idempotent).
1626 #
1627 # The new implementation uses POST and forks a worker process. We added
1628 # a new option 'background_delay'. If specified we wait up to
1629 # 'background_delay' second for the worker task to complete. It returns null
1630 # if the task is finished within that time, else we return the UPID.
1631
1632 my $update_vm_api = sub {
1633 my ($param, $sync) = @_;
1634
1635 my $rpcenv = PVE::RPCEnvironment::get();
1636
1637 my $authuser = $rpcenv->get_user();
1638
1639 my $node = extract_param($param, 'node');
1640
1641 my $vmid = extract_param($param, 'vmid');
1642
1643 my $digest = extract_param($param, 'digest');
1644
1645 my $background_delay = extract_param($param, 'background_delay');
1646
1647 my $skip_cloud_init = extract_param($param, 'skip_cloud_init');
1648
1649 if (defined(my $cipassword = $param->{cipassword})) {
1650 # Same logic as in cloud-init (but with the regex fixed...)
1651 $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword)
1652 if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/;
1653 }
1654
1655 my @paramarr = (); # used for log message
1656 foreach my $key (sort keys %$param) {
1657 my $value = $key eq 'cipassword' ? '<hidden>' : $param->{$key};
1658 push @paramarr, "-$key", $value;
1659 }
1660
1661 my $skiplock = extract_param($param, 'skiplock');
1662 raise_param_exc({ skiplock => "Only root may use this option." })
1663 if $skiplock && $authuser ne 'root@pam';
1664
1665 my $delete_str = extract_param($param, 'delete');
1666
1667 my $revert_str = extract_param($param, 'revert');
1668
1669 my $force = extract_param($param, 'force');
1670
1671 if (defined(my $ssh_keys = $param->{sshkeys})) {
1672 $ssh_keys = URI::Escape::uri_unescape($ssh_keys);
1673 PVE::Tools::validate_ssh_public_keys($ssh_keys);
1674 }
1675
1676 $param->{cpuunits} = PVE::CGroup::clamp_cpu_shares($param->{cpuunits})
1677 if defined($param->{cpuunits}); # clamp value depending on cgroup version
1678
1679 die "no options specified\n" if !$delete_str && !$revert_str && !scalar(keys %$param);
1680
1681 my $storecfg = PVE::Storage::config();
1682
1683 &$resolve_cdrom_alias($param);
1684
1685 # now try to verify all parameters
1686
1687 my $revert = {};
1688 foreach my $opt (PVE::Tools::split_list($revert_str)) {
1689 if (!PVE::QemuServer::option_exists($opt)) {
1690 raise_param_exc({ revert => "unknown option '$opt'" });
1691 }
1692
1693 raise_param_exc({ delete => "you can't use '-$opt' and " .
1694 "-revert $opt' at the same time" })
1695 if defined($param->{$opt});
1696
1697 $revert->{$opt} = 1;
1698 }
1699
1700 my @delete = ();
1701 foreach my $opt (PVE::Tools::split_list($delete_str)) {
1702 $opt = 'ide2' if $opt eq 'cdrom';
1703
1704 raise_param_exc({ delete => "you can't use '-$opt' and " .
1705 "-delete $opt' at the same time" })
1706 if defined($param->{$opt});
1707
1708 raise_param_exc({ revert => "you can't use '-delete $opt' and " .
1709 "-revert $opt' at the same time" })
1710 if $revert->{$opt};
1711
1712 if (!PVE::QemuServer::option_exists($opt)) {
1713 raise_param_exc({ delete => "unknown option '$opt'" });
1714 }
1715
1716 push @delete, $opt;
1717 }
1718
1719 my $repl_conf = PVE::ReplicationConfig->new();
1720 my $is_replicated = $repl_conf->check_for_existing_jobs($vmid, 1);
1721 my $check_replication = sub {
1722 my ($drive) = @_;
1723 return if !$is_replicated;
1724 my $volid = $drive->{file};
1725 return if !$volid || !($drive->{replicate}//1);
1726 return if PVE::QemuServer::drive_is_cdrom($drive);
1727
1728 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
1729 die "cannot add non-managed/pass-through volume to a replicated VM\n"
1730 if !defined($storeid);
1731
1732 return if defined($volname) && $volname eq 'cloudinit';
1733
1734 my $format;
1735 if ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE) {
1736 $storeid = $2;
1737 $format = $drive->{format} || PVE::Storage::storage_default_format($storecfg, $storeid);
1738 } else {
1739 $format = (PVE::Storage::parse_volname($storecfg, $volid))[6];
1740 }
1741 return if PVE::Storage::storage_can_replicate($storecfg, $storeid, $format);
1742 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
1743 return if $scfg->{shared};
1744 die "cannot add non-replicatable volume to a replicated VM\n";
1745 };
1746
1747 $check_drive_param->($param, $storecfg, $check_replication);
1748
1749 foreach my $opt (keys %$param) {
1750 if ($opt =~ m/^net(\d+)$/) {
1751 # add macaddr
1752 my $net = PVE::QemuServer::parse_net($param->{$opt});
1753 $param->{$opt} = PVE::QemuServer::print_net($net);
1754 } elsif ($opt eq 'vmgenid') {
1755 if ($param->{$opt} eq '1') {
1756 $param->{$opt} = PVE::QemuServer::generate_uuid();
1757 }
1758 } elsif ($opt eq 'hookscript') {
1759 eval { PVE::GuestHelpers::check_hookscript($param->{$opt}, $storecfg); };
1760 raise_param_exc({ $opt => $@ }) if $@;
1761 }
1762 }
1763
1764 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
1765
1766 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
1767
1768 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
1769
1770 PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $param);
1771
1772 my $updatefn = sub {
1773
1774 my $conf = PVE::QemuConfig->load_config($vmid);
1775
1776 die "checksum missmatch (file change by other user?)\n"
1777 if $digest && $digest ne $conf->{digest};
1778
1779 &$check_cpu_model_access($rpcenv, $authuser, $param, $conf);
1780
1781 # FIXME: 'suspended' lock should probabyl be a state or "weak" lock?!
1782 if (scalar(@delete) && grep { $_ eq 'vmstate'} @delete) {
1783 if (defined($conf->{lock}) && $conf->{lock} eq 'suspended') {
1784 delete $conf->{lock}; # for check lock check, not written out
1785 push @delete, 'lock'; # this is the real deal to write it out
1786 }
1787 push @delete, 'runningmachine' if $conf->{runningmachine};
1788 push @delete, 'runningcpu' if $conf->{runningcpu};
1789 }
1790
1791 PVE::QemuConfig->check_lock($conf) if !$skiplock;
1792
1793 foreach my $opt (keys %$revert) {
1794 if (defined($conf->{$opt})) {
1795 $param->{$opt} = $conf->{$opt};
1796 } elsif (defined($conf->{pending}->{$opt})) {
1797 push @delete, $opt;
1798 }
1799 }
1800
1801 if ($param->{memory} || defined($param->{balloon})) {
1802
1803 my $memory = $param->{memory} || $conf->{pending}->{memory} || $conf->{memory};
1804 my $maxmem = get_current_memory($memory);
1805 my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{pending}->{balloon} || $conf->{balloon};
1806
1807 die "balloon value too large (must be smaller than assigned memory)\n"
1808 if $balloon && $balloon > $maxmem;
1809 }
1810
1811 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
1812
1813 my $worker = sub {
1814
1815 print "update VM $vmid: " . join (' ', @paramarr) . "\n";
1816
1817 # write updates to pending section
1818
1819 my $modified = {}; # record what $option we modify
1820
1821 my @bootorder;
1822 if (my $boot = $conf->{boot}) {
1823 my $bootcfg = PVE::JSONSchema::parse_property_string('pve-qm-boot', $boot);
1824 @bootorder = PVE::Tools::split_list($bootcfg->{order}) if $bootcfg && $bootcfg->{order};
1825 }
1826 my $bootorder_deleted = grep {$_ eq 'bootorder'} @delete;
1827
1828 my $check_drive_perms = sub {
1829 my ($opt, $val) = @_;
1830 my $drive = PVE::QemuServer::parse_drive($opt, $val, 1);
1831 if (PVE::QemuServer::drive_is_cloudinit($drive)) {
1832 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Cloudinit', 'VM.Config.CDROM']);
1833 } elsif (PVE::QemuServer::drive_is_cdrom($drive, 1)) { # CDROM
1834 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
1835 } else {
1836 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
1837
1838 }
1839 };
1840
1841 foreach my $opt (@delete) {
1842 $modified->{$opt} = 1;
1843 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1844
1845 # value of what we want to delete, independent if pending or not
1846 my $val = $conf->{$opt} // $conf->{pending}->{$opt};
1847 if (!defined($val)) {
1848 warn "cannot delete '$opt' - not set in current configuration!\n";
1849 $modified->{$opt} = 0;
1850 next;
1851 }
1852 my $is_pending_val = defined($conf->{pending}->{$opt});
1853 delete $conf->{pending}->{$opt};
1854
1855 # remove from bootorder if necessary
1856 if (!$bootorder_deleted && @bootorder && grep {$_ eq $opt} @bootorder) {
1857 @bootorder = grep {$_ ne $opt} @bootorder;
1858 $conf->{pending}->{boot} = PVE::QemuServer::print_bootorder(\@bootorder);
1859 $modified->{boot} = 1;
1860 }
1861
1862 if ($opt =~ m/^unused/) {
1863 my $drive = PVE::QemuServer::parse_drive($opt, $val);
1864 PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'");
1865 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
1866 if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) {
1867 delete $conf->{$opt};
1868 PVE::QemuConfig->write_config($vmid, $conf);
1869 }
1870 } elsif ($opt eq 'vmstate') {
1871 PVE::QemuConfig->check_protection($conf, "can't remove vmstate '$val'");
1872 if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, { file => $val }, $rpcenv, $authuser, 1)) {
1873 delete $conf->{$opt};
1874 PVE::QemuConfig->write_config($vmid, $conf);
1875 }
1876 } elsif (PVE::QemuServer::is_valid_drivename($opt)) {
1877 PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'");
1878 $check_drive_perms->($opt, $val);
1879 PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $val))
1880 if $is_pending_val;
1881 PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
1882 PVE::QemuConfig->write_config($vmid, $conf);
1883 } elsif ($opt =~ m/^serial\d+$/) {
1884 if ($val eq 'socket') {
1885 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
1886 } elsif ($authuser ne 'root@pam') {
1887 die "only root can delete '$opt' config for real devices\n";
1888 }
1889 PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
1890 PVE::QemuConfig->write_config($vmid, $conf);
1891 } elsif ($opt =~ m/^usb\d+$/) {
1892 check_usb_perm($rpcenv, $authuser, $vmid, undef, $opt, $val);
1893 PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
1894 PVE::QemuConfig->write_config($vmid, $conf);
1895 } elsif ($opt =~ m/^hostpci\d+$/) {
1896 check_hostpci_perm($rpcenv, $authuser, $vmid, undef, $opt, $val);
1897 PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
1898 PVE::QemuConfig->write_config($vmid, $conf);
1899 } elsif ($opt eq 'tags') {
1900 assert_tag_permissions($vmid, $val, '', $rpcenv, $authuser);
1901 delete $conf->{$opt};
1902 PVE::QemuConfig->write_config($vmid, $conf);
1903 } elsif ($opt =~ m/^net\d+$/) {
1904 if ($conf->{$opt}) {
1905 PVE::QemuServer::check_bridge_access(
1906 $rpcenv,
1907 $authuser,
1908 { $opt => $conf->{$opt} },
1909 );
1910 }
1911 PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
1912 PVE::QemuConfig->write_config($vmid, $conf);
1913 } else {
1914 PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
1915 PVE::QemuConfig->write_config($vmid, $conf);
1916 }
1917 }
1918
1919 foreach my $opt (keys %$param) { # add/change
1920 $modified->{$opt} = 1;
1921 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1922 next if defined($conf->{pending}->{$opt}) && ($param->{$opt} eq $conf->{pending}->{$opt}); # skip if nothing changed
1923
1924 my $arch = PVE::QemuServer::get_vm_arch($conf);
1925
1926 if (PVE::QemuServer::is_valid_drivename($opt)) {
1927 # old drive
1928 if ($conf->{$opt}) {
1929 $check_drive_perms->($opt, $conf->{$opt});
1930 }
1931
1932 # new drive
1933 $check_drive_perms->($opt, $param->{$opt});
1934 PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
1935 if defined($conf->{pending}->{$opt});
1936
1937 assert_scsi_feature_compatibility($opt, $conf, $storecfg, $param->{$opt})
1938 if $opt =~ m/^scsi\d+$/;
1939
1940 my (undef, $created_opts) = create_disks(
1941 $rpcenv,
1942 $authuser,
1943 $conf,
1944 $arch,
1945 $storecfg,
1946 $vmid,
1947 undef,
1948 {$opt => $param->{$opt}},
1949 undef,
1950 undef,
1951 );
1952 $conf->{pending}->{$_} = $created_opts->{$_} for keys $created_opts->%*;
1953
1954 # default legacy boot order implies all cdroms anyway
1955 if (@bootorder) {
1956 # append new CD drives to bootorder to mark them bootable
1957 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}, 1);
1958 if (PVE::QemuServer::drive_is_cdrom($drive, 1) && !grep(/^$opt$/, @bootorder)) {
1959 push @bootorder, $opt;
1960 $conf->{pending}->{boot} = PVE::QemuServer::print_bootorder(\@bootorder);
1961 $modified->{boot} = 1;
1962 }
1963 }
1964 } elsif ($opt =~ m/^serial\d+/) {
1965 if ((!defined($conf->{$opt}) || $conf->{$opt} eq 'socket') && $param->{$opt} eq 'socket') {
1966 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
1967 } elsif ($authuser ne 'root@pam') {
1968 die "only root can modify '$opt' config for real devices\n";
1969 }
1970 $conf->{pending}->{$opt} = $param->{$opt};
1971 } elsif ($opt eq 'vga') {
1972 my $vga = PVE::QemuServer::parse_vga($param->{$opt});
1973 PVE::QemuServer::assert_clipboard_config($vga);
1974 $conf->{pending}->{$opt} = $param->{$opt};
1975 } elsif ($opt =~ m/^usb\d+/) {
1976 if (my $olddevice = $conf->{$opt}) {
1977 check_usb_perm($rpcenv, $authuser, $vmid, undef, $opt, $conf->{$opt});
1978 }
1979 check_usb_perm($rpcenv, $authuser, $vmid, undef, $opt, $param->{$opt});
1980 $conf->{pending}->{$opt} = $param->{$opt};
1981 } elsif ($opt =~ m/^hostpci\d+$/) {
1982 if (my $oldvalue = $conf->{$opt}) {
1983 check_hostpci_perm($rpcenv, $authuser, $vmid, undef, $opt, $oldvalue);
1984 }
1985 check_hostpci_perm($rpcenv, $authuser, $vmid, undef, $opt, $param->{$opt});
1986 $conf->{pending}->{$opt} = $param->{$opt};
1987 } elsif ($opt eq 'tags') {
1988 assert_tag_permissions($vmid, $conf->{$opt}, $param->{$opt}, $rpcenv, $authuser);
1989 $conf->{pending}->{$opt} = PVE::GuestHelpers::get_unique_tags($param->{$opt});
1990 } elsif ($opt =~ m/^net\d+$/) {
1991 if ($conf->{$opt}) {
1992 PVE::QemuServer::check_bridge_access(
1993 $rpcenv,
1994 $authuser,
1995 { $opt => $conf->{$opt} },
1996 );
1997 }
1998 $conf->{pending}->{$opt} = $param->{$opt};
1999 } else {
2000 $conf->{pending}->{$opt} = $param->{$opt};
2001
2002 if ($opt eq 'boot') {
2003 my $new_bootcfg = PVE::JSONSchema::parse_property_string('pve-qm-boot', $param->{$opt});
2004 if ($new_bootcfg->{order}) {
2005 my @devs = PVE::Tools::split_list($new_bootcfg->{order});
2006 for my $dev (@devs) {
2007 my $exists = $conf->{$dev} || $conf->{pending}->{$dev} || $param->{$dev};
2008 my $deleted = grep {$_ eq $dev} @delete;
2009 die "invalid bootorder: device '$dev' does not exist'\n"
2010 if !$exists || $deleted;
2011 }
2012
2013 # remove legacy boot order settings if new one set
2014 $conf->{pending}->{$opt} = PVE::QemuServer::print_bootorder(\@devs);
2015 PVE::QemuConfig->add_to_pending_delete($conf, "bootdisk")
2016 if $conf->{bootdisk};
2017 }
2018 }
2019 }
2020 PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
2021 PVE::QemuConfig->write_config($vmid, $conf);
2022 }
2023
2024 # remove pending changes when nothing changed
2025 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
2026 my $changes = PVE::QemuConfig->cleanup_pending($conf);
2027 PVE::QemuConfig->write_config($vmid, $conf) if $changes;
2028
2029 return if !scalar(keys %{$conf->{pending}});
2030
2031 my $running = PVE::QemuServer::check_running($vmid);
2032
2033 # apply pending changes
2034
2035 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
2036
2037 my $errors = {};
2038 if ($running) {
2039 PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors);
2040 } else {
2041 # cloud_init must be skipped if we are in an incoming, remote live migration
2042 PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $errors, $skip_cloud_init);
2043 }
2044 raise_param_exc($errors) if scalar(keys %$errors);
2045
2046 return;
2047 };
2048
2049 if ($sync) {
2050 &$worker();
2051 return;
2052 } else {
2053 my $upid = $rpcenv->fork_worker('qmconfig', $vmid, $authuser, $worker);
2054
2055 if ($background_delay) {
2056
2057 # Note: It would be better to do that in the Event based HTTPServer
2058 # to avoid blocking call to sleep.
2059
2060 my $end_time = time() + $background_delay;
2061
2062 my $task = PVE::Tools::upid_decode($upid);
2063
2064 my $running = 1;
2065 while (time() < $end_time) {
2066 $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart});
2067 last if !$running;
2068 sleep(1); # this gets interrupted when child process ends
2069 }
2070
2071 if (!$running) {
2072 my $status = PVE::Tools::upid_read_status($upid);
2073 return if !PVE::Tools::upid_status_is_error($status);
2074 die "failed to update VM $vmid: $status\n";
2075 }
2076 }
2077
2078 return $upid;
2079 }
2080 };
2081
2082 return PVE::QemuConfig->lock_config($vmid, $updatefn);
2083 };
2084
2085 my $vm_config_perm_list = [
2086 'VM.Config.Disk',
2087 'VM.Config.CDROM',
2088 'VM.Config.CPU',
2089 'VM.Config.Memory',
2090 'VM.Config.Network',
2091 'VM.Config.HWType',
2092 'VM.Config.Options',
2093 'VM.Config.Cloudinit',
2094 ];
2095
2096 __PACKAGE__->register_method({
2097 name => 'update_vm_async',
2098 path => '{vmid}/config',
2099 method => 'POST',
2100 protected => 1,
2101 proxyto => 'node',
2102 description => "Set virtual machine options (asynchrounous API).",
2103 permissions => {
2104 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
2105 },
2106 parameters => {
2107 additionalProperties => 0,
2108 properties => PVE::QemuServer::json_config_properties(
2109 {
2110 node => get_standard_option('pve-node'),
2111 vmid => get_standard_option('pve-vmid'),
2112 skiplock => get_standard_option('skiplock'),
2113 delete => {
2114 type => 'string', format => 'pve-configid-list',
2115 description => "A list of settings you want to delete.",
2116 optional => 1,
2117 },
2118 revert => {
2119 type => 'string', format => 'pve-configid-list',
2120 description => "Revert a pending change.",
2121 optional => 1,
2122 },
2123 force => {
2124 type => 'boolean',
2125 description => $opt_force_description,
2126 optional => 1,
2127 requires => 'delete',
2128 },
2129 digest => {
2130 type => 'string',
2131 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
2132 maxLength => 40,
2133 optional => 1,
2134 },
2135 background_delay => {
2136 type => 'integer',
2137 description => "Time to wait for the task to finish. We return 'null' if the task finish within that time.",
2138 minimum => 1,
2139 maximum => 30,
2140 optional => 1,
2141 },
2142 },
2143 1, # with_disk_alloc
2144 ),
2145 },
2146 returns => {
2147 type => 'string',
2148 optional => 1,
2149 },
2150 code => $update_vm_api,
2151 });
2152
2153 __PACKAGE__->register_method({
2154 name => 'update_vm',
2155 path => '{vmid}/config',
2156 method => 'PUT',
2157 protected => 1,
2158 proxyto => 'node',
2159 description => "Set virtual machine options (synchrounous API) - You should consider using the POST method instead for any actions involving hotplug or storage allocation.",
2160 permissions => {
2161 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
2162 },
2163 parameters => {
2164 additionalProperties => 0,
2165 properties => PVE::QemuServer::json_config_properties(
2166 {
2167 node => get_standard_option('pve-node'),
2168 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
2169 skiplock => get_standard_option('skiplock'),
2170 delete => {
2171 type => 'string', format => 'pve-configid-list',
2172 description => "A list of settings you want to delete.",
2173 optional => 1,
2174 },
2175 revert => {
2176 type => 'string', format => 'pve-configid-list',
2177 description => "Revert a pending change.",
2178 optional => 1,
2179 },
2180 force => {
2181 type => 'boolean',
2182 description => $opt_force_description,
2183 optional => 1,
2184 requires => 'delete',
2185 },
2186 digest => {
2187 type => 'string',
2188 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
2189 maxLength => 40,
2190 optional => 1,
2191 },
2192 },
2193 1, # with_disk_alloc
2194 ),
2195 },
2196 returns => { type => 'null' },
2197 code => sub {
2198 my ($param) = @_;
2199 &$update_vm_api($param, 1);
2200 return;
2201 }
2202 });
2203
2204 __PACKAGE__->register_method({
2205 name => 'destroy_vm',
2206 path => '{vmid}',
2207 method => 'DELETE',
2208 protected => 1,
2209 proxyto => 'node',
2210 description => "Destroy the VM and all used/owned volumes. Removes any VM specific permissions"
2211 ." and firewall rules",
2212 permissions => {
2213 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
2214 },
2215 parameters => {
2216 additionalProperties => 0,
2217 properties => {
2218 node => get_standard_option('pve-node'),
2219 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }),
2220 skiplock => get_standard_option('skiplock'),
2221 purge => {
2222 type => 'boolean',
2223 description => "Remove VMID from configurations, like backup & replication jobs and HA.",
2224 optional => 1,
2225 },
2226 'destroy-unreferenced-disks' => {
2227 type => 'boolean',
2228 description => "If set, destroy additionally all disks not referenced in the config"
2229 ." but with a matching VMID from all enabled storages.",
2230 optional => 1,
2231 default => 0,
2232 },
2233 },
2234 },
2235 returns => {
2236 type => 'string',
2237 },
2238 code => sub {
2239 my ($param) = @_;
2240
2241 my $rpcenv = PVE::RPCEnvironment::get();
2242 my $authuser = $rpcenv->get_user();
2243 my $vmid = $param->{vmid};
2244
2245 my $skiplock = $param->{skiplock};
2246 raise_param_exc({ skiplock => "Only root may use this option." })
2247 if $skiplock && $authuser ne 'root@pam';
2248
2249 my $early_checks = sub {
2250 # test if VM exists
2251 my $conf = PVE::QemuConfig->load_config($vmid);
2252 PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid");
2253
2254 my $ha_managed = PVE::HA::Config::service_is_configured("vm:$vmid");
2255
2256 if (!$param->{purge}) {
2257 die "unable to remove VM $vmid - used in HA resources and purge parameter not set.\n"
2258 if $ha_managed;
2259 # don't allow destroy if with replication jobs but no purge param
2260 my $repl_conf = PVE::ReplicationConfig->new();
2261 $repl_conf->check_for_existing_jobs($vmid);
2262 }
2263
2264 die "VM $vmid is running - destroy failed\n"
2265 if PVE::QemuServer::check_running($vmid);
2266
2267 return $ha_managed;
2268 };
2269
2270 $early_checks->();
2271
2272 my $realcmd = sub {
2273 my $upid = shift;
2274
2275 my $storecfg = PVE::Storage::config();
2276
2277 syslog('info', "destroy VM $vmid: $upid\n");
2278 PVE::QemuConfig->lock_config($vmid, sub {
2279 # repeat, config might have changed
2280 my $ha_managed = $early_checks->();
2281
2282 my $purge_unreferenced = $param->{'destroy-unreferenced-disks'};
2283
2284 PVE::QemuServer::destroy_vm(
2285 $storecfg,
2286 $vmid,
2287 $skiplock, { lock => 'destroyed' },
2288 $purge_unreferenced,
2289 );
2290
2291 PVE::AccessControl::remove_vm_access($vmid);
2292 PVE::Firewall::remove_vmfw_conf($vmid);
2293 if ($param->{purge}) {
2294 print "purging VM $vmid from related configurations..\n";
2295 PVE::ReplicationConfig::remove_vmid_jobs($vmid);
2296 PVE::VZDump::Plugin::remove_vmid_from_backup_jobs($vmid);
2297
2298 if ($ha_managed) {
2299 PVE::HA::Config::delete_service_from_config("vm:$vmid");
2300 print "NOTE: removed VM $vmid from HA resource configuration.\n";
2301 }
2302 }
2303
2304 # only now remove the zombie config, else we can have reuse race
2305 PVE::QemuConfig->destroy_config($vmid);
2306 });
2307 };
2308
2309 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
2310 }});
2311
2312 __PACKAGE__->register_method({
2313 name => 'unlink',
2314 path => '{vmid}/unlink',
2315 method => 'PUT',
2316 protected => 1,
2317 proxyto => 'node',
2318 description => "Unlink/delete disk images.",
2319 permissions => {
2320 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
2321 },
2322 parameters => {
2323 additionalProperties => 0,
2324 properties => {
2325 node => get_standard_option('pve-node'),
2326 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
2327 idlist => {
2328 type => 'string', format => 'pve-configid-list',
2329 description => "A list of disk IDs you want to delete.",
2330 },
2331 force => {
2332 type => 'boolean',
2333 description => $opt_force_description,
2334 optional => 1,
2335 },
2336 },
2337 },
2338 returns => { type => 'null'},
2339 code => sub {
2340 my ($param) = @_;
2341
2342 $param->{delete} = extract_param($param, 'idlist');
2343
2344 __PACKAGE__->update_vm($param);
2345
2346 return;
2347 }});
2348
2349 # uses good entropy, each char is limited to 6 bit to get printable chars simply
2350 my $gen_rand_chars = sub {
2351 my ($length) = @_;
2352
2353 die "invalid length $length" if $length < 1;
2354
2355 my $min = ord('!'); # first printable ascii
2356
2357 my $rand_bytes = Crypt::OpenSSL::Random::random_bytes($length);
2358 die "failed to generate random bytes!\n"
2359 if !$rand_bytes;
2360
2361 my $str = join('', map { chr((ord($_) & 0x3F) + $min) } split('', $rand_bytes));
2362
2363 return $str;
2364 };
2365
2366 my $sslcert;
2367
2368 __PACKAGE__->register_method({
2369 name => 'vncproxy',
2370 path => '{vmid}/vncproxy',
2371 method => 'POST',
2372 protected => 1,
2373 permissions => {
2374 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
2375 },
2376 description => "Creates a TCP VNC proxy connections.",
2377 parameters => {
2378 additionalProperties => 0,
2379 properties => {
2380 node => get_standard_option('pve-node'),
2381 vmid => get_standard_option('pve-vmid'),
2382 websocket => {
2383 optional => 1,
2384 type => 'boolean',
2385 description => "Prepare for websocket upgrade (only required when using "
2386 ."serial terminal, otherwise upgrade is always possible).",
2387 },
2388 'generate-password' => {
2389 optional => 1,
2390 type => 'boolean',
2391 default => 0,
2392 description => "Generates a random password to be used as ticket instead of the API ticket.",
2393 },
2394 },
2395 },
2396 returns => {
2397 additionalProperties => 0,
2398 properties => {
2399 user => { type => 'string' },
2400 ticket => { type => 'string' },
2401 password => {
2402 optional => 1,
2403 description => "Returned if requested with 'generate-password' param."
2404 ." Consists of printable ASCII characters ('!' .. '~').",
2405 type => 'string',
2406 },
2407 cert => { type => 'string' },
2408 port => { type => 'integer' },
2409 upid => { type => 'string' },
2410 },
2411 },
2412 code => sub {
2413 my ($param) = @_;
2414
2415 my $rpcenv = PVE::RPCEnvironment::get();
2416
2417 my $authuser = $rpcenv->get_user();
2418
2419 my $vmid = $param->{vmid};
2420 my $node = $param->{node};
2421 my $websocket = $param->{websocket};
2422
2423 my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists
2424
2425 my $serial;
2426 if ($conf->{vga}) {
2427 my $vga = PVE::QemuServer::parse_vga($conf->{vga});
2428 $serial = $vga->{type} if $vga->{type} =~ m/^serial\d+$/;
2429 }
2430
2431 my $authpath = "/vms/$vmid";
2432
2433 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
2434 my $password = $ticket;
2435 if ($param->{'generate-password'}) {
2436 $password = $gen_rand_chars->(8);
2437 }
2438
2439 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
2440 if !$sslcert;
2441
2442 my $family;
2443 my $remcmd = [];
2444
2445 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
2446 (undef, $family) = PVE::Cluster::remote_node_ip($node);
2447 my $sshinfo = PVE::SSHInfo::get_ssh_info($node);
2448 # NOTE: kvm VNC traffic is already TLS encrypted or is known unsecure
2449 $remcmd = PVE::SSHInfo::ssh_info_to_command($sshinfo, defined($serial) ? '-t' : '-T');
2450 } else {
2451 $family = PVE::Tools::get_host_address_family($node);
2452 }
2453
2454 my $port = PVE::Tools::next_vnc_port($family);
2455
2456 my $timeout = 10;
2457
2458 my $realcmd = sub {
2459 my $upid = shift;
2460
2461 syslog('info', "starting vnc proxy $upid\n");
2462
2463 my $cmd;
2464
2465 if (defined($serial)) {
2466
2467 my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-iface', $serial, '-escape', '0' ];
2468
2469 $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
2470 '-timeout', $timeout, '-authpath', $authpath,
2471 '-perm', 'Sys.Console'];
2472
2473 if ($param->{websocket}) {
2474 $ENV{PVE_VNC_TICKET} = $password; # pass ticket to vncterm
2475 push @$cmd, '-notls', '-listen', 'localhost';
2476 }
2477
2478 push @$cmd, '-c', @$remcmd, @$termcmd;
2479
2480 PVE::Tools::run_command($cmd);
2481
2482 } else {
2483
2484 $ENV{LC_PVE_TICKET} = $password; # set ticket with "qm vncproxy"
2485
2486 $cmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
2487
2488 my $sock = IO::Socket::IP->new(
2489 ReuseAddr => 1,
2490 Listen => 1,
2491 LocalPort => $port,
2492 Proto => 'tcp',
2493 GetAddrInfoFlags => 0,
2494 ) or die "failed to create socket: $!\n";
2495 # Inside the worker we shouldn't have any previous alarms
2496 # running anyway...:
2497 alarm(0);
2498 local $SIG{ALRM} = sub { die "connection timed out\n" };
2499 alarm $timeout;
2500 accept(my $cli, $sock) or die "connection failed: $!\n";
2501 alarm(0);
2502 close($sock);
2503 if (PVE::Tools::run_command($cmd,
2504 output => '>&'.fileno($cli),
2505 input => '<&'.fileno($cli),
2506 noerr => 1) != 0)
2507 {
2508 die "Failed to run vncproxy.\n";
2509 }
2510 }
2511
2512 return;
2513 };
2514
2515 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1);
2516
2517 PVE::Tools::wait_for_vnc_port($port);
2518
2519 my $res = {
2520 user => $authuser,
2521 ticket => $ticket,
2522 port => $port,
2523 upid => $upid,
2524 cert => $sslcert,
2525 };
2526 $res->{password} = $password if $param->{'generate-password'};
2527
2528 return $res;
2529 }});
2530
2531 __PACKAGE__->register_method({
2532 name => 'termproxy',
2533 path => '{vmid}/termproxy',
2534 method => 'POST',
2535 protected => 1,
2536 permissions => {
2537 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
2538 },
2539 description => "Creates a TCP proxy connections.",
2540 parameters => {
2541 additionalProperties => 0,
2542 properties => {
2543 node => get_standard_option('pve-node'),
2544 vmid => get_standard_option('pve-vmid'),
2545 serial=> {
2546 optional => 1,
2547 type => 'string',
2548 enum => [qw(serial0 serial1 serial2 serial3)],
2549 description => "opens a serial terminal (defaults to display)",
2550 },
2551 },
2552 },
2553 returns => {
2554 additionalProperties => 0,
2555 properties => {
2556 user => { type => 'string' },
2557 ticket => { type => 'string' },
2558 port => { type => 'integer' },
2559 upid => { type => 'string' },
2560 },
2561 },
2562 code => sub {
2563 my ($param) = @_;
2564
2565 my $rpcenv = PVE::RPCEnvironment::get();
2566
2567 my $authuser = $rpcenv->get_user();
2568
2569 my $vmid = $param->{vmid};
2570 my $node = $param->{node};
2571 my $serial = $param->{serial};
2572
2573 my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists
2574
2575 if (!defined($serial)) {
2576 if ($conf->{vga}) {
2577 my $vga = PVE::QemuServer::parse_vga($conf->{vga});
2578 $serial = $vga->{type} if $vga->{type} =~ m/^serial\d+$/;
2579 }
2580 }
2581
2582 my $authpath = "/vms/$vmid";
2583
2584 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
2585
2586 my $family;
2587 my $remcmd = [];
2588
2589 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
2590 (undef, $family) = PVE::Cluster::remote_node_ip($node);
2591 my $sshinfo = PVE::SSHInfo::get_ssh_info($node);
2592 $remcmd = PVE::SSHInfo::ssh_info_to_command($sshinfo, '-t');
2593 push @$remcmd, '--';
2594 } else {
2595 $family = PVE::Tools::get_host_address_family($node);
2596 }
2597
2598 my $port = PVE::Tools::next_vnc_port($family);
2599
2600 my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-escape', '0'];
2601 push @$termcmd, '-iface', $serial if $serial;
2602
2603 my $realcmd = sub {
2604 my $upid = shift;
2605
2606 syslog('info', "starting qemu termproxy $upid\n");
2607
2608 my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
2609 '--perm', 'VM.Console', '--'];
2610 push @$cmd, @$remcmd, @$termcmd;
2611
2612 PVE::Tools::run_command($cmd);
2613 };
2614
2615 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1);
2616
2617 PVE::Tools::wait_for_vnc_port($port);
2618
2619 return {
2620 user => $authuser,
2621 ticket => $ticket,
2622 port => $port,
2623 upid => $upid,
2624 };
2625 }});
2626
2627 __PACKAGE__->register_method({
2628 name => 'vncwebsocket',
2629 path => '{vmid}/vncwebsocket',
2630 method => 'GET',
2631 permissions => {
2632 description => "You also need to pass a valid ticket (vncticket).",
2633 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
2634 },
2635 description => "Opens a weksocket for VNC traffic.",
2636 parameters => {
2637 additionalProperties => 0,
2638 properties => {
2639 node => get_standard_option('pve-node'),
2640 vmid => get_standard_option('pve-vmid'),
2641 vncticket => {
2642 description => "Ticket from previous call to vncproxy.",
2643 type => 'string',
2644 maxLength => 512,
2645 },
2646 port => {
2647 description => "Port number returned by previous vncproxy call.",
2648 type => 'integer',
2649 minimum => 5900,
2650 maximum => 5999,
2651 },
2652 },
2653 },
2654 returns => {
2655 type => "object",
2656 properties => {
2657 port => { type => 'string' },
2658 },
2659 },
2660 code => sub {
2661 my ($param) = @_;
2662
2663 my $rpcenv = PVE::RPCEnvironment::get();
2664
2665 my $authuser = $rpcenv->get_user();
2666
2667 my $vmid = $param->{vmid};
2668 my $node = $param->{node};
2669
2670 my $authpath = "/vms/$vmid";
2671
2672 PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath);
2673
2674 my $conf = PVE::QemuConfig->load_config($vmid, $node); # VM exists ?
2675
2676 # Note: VNC ports are acessible from outside, so we do not gain any
2677 # security if we verify that $param->{port} belongs to VM $vmid. This
2678 # check is done by verifying the VNC ticket (inside VNC protocol).
2679
2680 my $port = $param->{port};
2681
2682 return { port => $port };
2683 }});
2684
2685 __PACKAGE__->register_method({
2686 name => 'spiceproxy',
2687 path => '{vmid}/spiceproxy',
2688 method => 'POST',
2689 protected => 1,
2690 proxyto => 'node',
2691 permissions => {
2692 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
2693 },
2694 description => "Returns a SPICE configuration to connect to the VM.",
2695 parameters => {
2696 additionalProperties => 0,
2697 properties => {
2698 node => get_standard_option('pve-node'),
2699 vmid => get_standard_option('pve-vmid'),
2700 proxy => get_standard_option('spice-proxy', { optional => 1 }),
2701 },
2702 },
2703 returns => get_standard_option('remote-viewer-config'),
2704 code => sub {
2705 my ($param) = @_;
2706
2707 my $rpcenv = PVE::RPCEnvironment::get();
2708
2709 my $authuser = $rpcenv->get_user();
2710
2711 my $vmid = $param->{vmid};
2712 my $node = $param->{node};
2713 my $proxy = $param->{proxy};
2714
2715 my $conf = PVE::QemuConfig->load_config($vmid, $node);
2716 my $title = "VM $vmid";
2717 $title .= " - ". $conf->{name} if $conf->{name};
2718
2719 my $port = PVE::QemuServer::spice_port($vmid);
2720
2721 my ($ticket, undef, $remote_viewer_config) =
2722 PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port);
2723
2724 mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket);
2725 mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30");
2726
2727 return $remote_viewer_config;
2728 }});
2729
2730 __PACKAGE__->register_method({
2731 name => 'vmcmdidx',
2732 path => '{vmid}/status',
2733 method => 'GET',
2734 proxyto => 'node',
2735 description => "Directory index",
2736 permissions => {
2737 user => 'all',
2738 },
2739 parameters => {
2740 additionalProperties => 0,
2741 properties => {
2742 node => get_standard_option('pve-node'),
2743 vmid => get_standard_option('pve-vmid'),
2744 },
2745 },
2746 returns => {
2747 type => 'array',
2748 items => {
2749 type => "object",
2750 properties => {
2751 subdir => { type => 'string' },
2752 },
2753 },
2754 links => [ { rel => 'child', href => "{subdir}" } ],
2755 },
2756 code => sub {
2757 my ($param) = @_;
2758
2759 # test if VM exists
2760 my $conf = PVE::QemuConfig->load_config($param->{vmid});
2761
2762 my $res = [
2763 { subdir => 'current' },
2764 { subdir => 'start' },
2765 { subdir => 'stop' },
2766 { subdir => 'reset' },
2767 { subdir => 'shutdown' },
2768 { subdir => 'suspend' },
2769 { subdir => 'reboot' },
2770 ];
2771
2772 return $res;
2773 }});
2774
2775 __PACKAGE__->register_method({
2776 name => 'vm_status',
2777 path => '{vmid}/status/current',
2778 method => 'GET',
2779 proxyto => 'node',
2780 protected => 1, # qemu pid files are only readable by root
2781 description => "Get virtual machine status.",
2782 permissions => {
2783 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
2784 },
2785 parameters => {
2786 additionalProperties => 0,
2787 properties => {
2788 node => get_standard_option('pve-node'),
2789 vmid => get_standard_option('pve-vmid'),
2790 },
2791 },
2792 returns => {
2793 type => 'object',
2794 properties => {
2795 %$PVE::QemuServer::vmstatus_return_properties,
2796 ha => {
2797 description => "HA manager service status.",
2798 type => 'object',
2799 },
2800 spice => {
2801 description => "QEMU VGA configuration supports spice.",
2802 type => 'boolean',
2803 optional => 1,
2804 },
2805 agent => {
2806 description => "QEMU Guest Agent is enabled in config.",
2807 type => 'boolean',
2808 optional => 1,
2809 },
2810 clipboard => {
2811 description => 'Enable a specific clipboard. If not set, depending on'
2812 .' the display type the SPICE one will be added.',
2813 type => 'string',
2814 enum => ['vnc'],
2815 optional => 1,
2816 },
2817 },
2818 },
2819 code => sub {
2820 my ($param) = @_;
2821
2822 # test if VM exists
2823 my $conf = PVE::QemuConfig->load_config($param->{vmid});
2824
2825 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
2826 my $status = $vmstatus->{$param->{vmid}};
2827
2828 $status->{ha} = PVE::HA::Config::get_service_status("vm:$param->{vmid}");
2829
2830 if ($conf->{vga}) {
2831 my $vga = PVE::QemuServer::parse_vga($conf->{vga});
2832 my $spice = defined($vga->{type}) && $vga->{type} =~ /^virtio/;
2833 $spice ||= PVE::QemuServer::vga_conf_has_spice($conf->{vga});
2834 $status->{spice} = 1 if $spice;
2835 $status->{clipboard} = $vga->{clipboard};
2836 }
2837 $status->{agent} = 1 if PVE::QemuServer::get_qga_key($conf, 'enabled');
2838
2839 return $status;
2840 }});
2841
2842 __PACKAGE__->register_method({
2843 name => 'vm_start',
2844 path => '{vmid}/status/start',
2845 method => 'POST',
2846 protected => 1,
2847 proxyto => 'node',
2848 description => "Start virtual machine.",
2849 permissions => {
2850 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2851 },
2852 parameters => {
2853 additionalProperties => 0,
2854 properties => {
2855 node => get_standard_option('pve-node'),
2856 vmid => get_standard_option('pve-vmid',
2857 { completion => \&PVE::QemuServer::complete_vmid_stopped }),
2858 skiplock => get_standard_option('skiplock'),
2859 stateuri => get_standard_option('pve-qm-stateuri'),
2860 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
2861 migration_type => {
2862 type => 'string',
2863 enum => ['secure', 'insecure'],
2864 description => "Migration traffic is encrypted using an SSH " .
2865 "tunnel by default. On secure, completely private networks " .
2866 "this can be disabled to increase performance.",
2867 optional => 1,
2868 },
2869 migration_network => {
2870 type => 'string', format => 'CIDR',
2871 description => "CIDR of the (sub) network that is used for migration.",
2872 optional => 1,
2873 },
2874 machine => get_standard_option('pve-qemu-machine'),
2875 'force-cpu' => {
2876 description => "Override QEMU's -cpu argument with the given string.",
2877 type => 'string',
2878 optional => 1,
2879 },
2880 targetstorage => get_standard_option('pve-targetstorage'),
2881 timeout => {
2882 description => "Wait maximal timeout seconds.",
2883 type => 'integer',
2884 minimum => 0,
2885 default => 'max(30, vm memory in GiB)',
2886 optional => 1,
2887 },
2888 },
2889 },
2890 returns => {
2891 type => 'string',
2892 },
2893 code => sub {
2894 my ($param) = @_;
2895
2896 my $rpcenv = PVE::RPCEnvironment::get();
2897 my $authuser = $rpcenv->get_user();
2898
2899 my $node = extract_param($param, 'node');
2900 my $vmid = extract_param($param, 'vmid');
2901 my $timeout = extract_param($param, 'timeout');
2902 my $machine = extract_param($param, 'machine');
2903
2904 my $get_root_param = sub {
2905 my $value = extract_param($param, $_[0]);
2906 raise_param_exc({ "$_[0]" => "Only root may use this option." })
2907 if $value && $authuser ne 'root@pam';
2908 return $value;
2909 };
2910
2911 my $stateuri = $get_root_param->('stateuri');
2912 my $skiplock = $get_root_param->('skiplock');
2913 my $migratedfrom = $get_root_param->('migratedfrom');
2914 my $migration_type = $get_root_param->('migration_type');
2915 my $migration_network = $get_root_param->('migration_network');
2916 my $targetstorage = $get_root_param->('targetstorage');
2917 my $force_cpu = $get_root_param->('force-cpu');
2918
2919 my $storagemap;
2920
2921 if ($targetstorage) {
2922 raise_param_exc({ targetstorage => "targetstorage can only by used with migratedfrom." })
2923 if !$migratedfrom;
2924 $storagemap = eval { PVE::JSONSchema::parse_idmap($targetstorage, 'pve-storage-id') };
2925 raise_param_exc({ targetstorage => "failed to parse storage map: $@" })
2926 if $@;
2927 }
2928
2929 # read spice ticket from STDIN
2930 my $spice_ticket;
2931 my $nbd_protocol_version = 0;
2932 my $replicated_volumes = {};
2933 my $offline_volumes = {};
2934 if ($stateuri && ($stateuri eq 'tcp' || $stateuri eq 'unix') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
2935 while (defined(my $line = <STDIN>)) {
2936 chomp $line;
2937 if ($line =~ m/^spice_ticket: (.+)$/) {
2938 $spice_ticket = $1;
2939 } elsif ($line =~ m/^nbd_protocol_version: (\d+)$/) {
2940 $nbd_protocol_version = $1;
2941 } elsif ($line =~ m/^replicated_volume: (.*)$/) {
2942 $replicated_volumes->{$1} = 1;
2943 } elsif ($line =~ m/^tpmstate0: (.*)$/) { # Deprecated, use offline_volume instead
2944 $offline_volumes->{tpmstate0} = $1;
2945 } elsif ($line =~ m/^offline_volume: ([^:]+): (.*)$/) {
2946 $offline_volumes->{$1} = $2;
2947 } elsif (!$spice_ticket) {
2948 # fallback for old source node
2949 $spice_ticket = $line;
2950 } else {
2951 warn "unknown 'start' parameter on STDIN: '$line'\n";
2952 }
2953 }
2954 }
2955
2956 PVE::Cluster::check_cfs_quorum();
2957
2958 my $storecfg = PVE::Storage::config();
2959
2960 if (PVE::HA::Config::vm_is_ha_managed($vmid) && !$stateuri && $rpcenv->{type} ne 'ha') {
2961 my $hacmd = sub {
2962 my $upid = shift;
2963
2964 print "Requesting HA start for VM $vmid\n";
2965
2966 my $cmd = ['ha-manager', 'set', "vm:$vmid", '--state', 'started'];
2967 PVE::Tools::run_command($cmd);
2968 return;
2969 };
2970
2971 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
2972
2973 } else {
2974
2975 my $realcmd = sub {
2976 my $upid = shift;
2977
2978 syslog('info', "start VM $vmid: $upid\n");
2979
2980 my $migrate_opts = {
2981 migratedfrom => $migratedfrom,
2982 spice_ticket => $spice_ticket,
2983 network => $migration_network,
2984 type => $migration_type,
2985 storagemap => $storagemap,
2986 nbd_proto_version => $nbd_protocol_version,
2987 replicated_volumes => $replicated_volumes,
2988 offline_volumes => $offline_volumes,
2989 };
2990
2991 my $params = {
2992 statefile => $stateuri,
2993 skiplock => $skiplock,
2994 forcemachine => $machine,
2995 timeout => $timeout,
2996 forcecpu => $force_cpu,
2997 };
2998
2999 PVE::QemuServer::vm_start($storecfg, $vmid, $params, $migrate_opts);
3000 return;
3001 };
3002
3003 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
3004 }
3005 }});
3006
3007 __PACKAGE__->register_method({
3008 name => 'vm_stop',
3009 path => '{vmid}/status/stop',
3010 method => 'POST',
3011 protected => 1,
3012 proxyto => 'node',
3013 description => "Stop virtual machine. The qemu process will exit immediately. This" .
3014 "is akin to pulling the power plug of a running computer and may damage the VM data",
3015 permissions => {
3016 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
3017 },
3018 parameters => {
3019 additionalProperties => 0,
3020 properties => {
3021 node => get_standard_option('pve-node'),
3022 vmid => get_standard_option('pve-vmid',
3023 { completion => \&PVE::QemuServer::complete_vmid_running }),
3024 skiplock => get_standard_option('skiplock'),
3025 migratedfrom => get_standard_option('pve-node', { optional => 1 }),
3026 timeout => {
3027 description => "Wait maximal timeout seconds.",
3028 type => 'integer',
3029 minimum => 0,
3030 optional => 1,
3031 },
3032 keepActive => {
3033 description => "Do not deactivate storage volumes.",
3034 type => 'boolean',
3035 optional => 1,
3036 default => 0,
3037 }
3038 },
3039 },
3040 returns => {
3041 type => 'string',
3042 },
3043 code => sub {
3044 my ($param) = @_;
3045
3046 my $rpcenv = PVE::RPCEnvironment::get();
3047 my $authuser = $rpcenv->get_user();
3048
3049 my $node = extract_param($param, 'node');
3050 my $vmid = extract_param($param, 'vmid');
3051
3052 my $skiplock = extract_param($param, 'skiplock');
3053 raise_param_exc({ skiplock => "Only root may use this option." })
3054 if $skiplock && $authuser ne 'root@pam';
3055
3056 my $keepActive = extract_param($param, 'keepActive');
3057 raise_param_exc({ keepActive => "Only root may use this option." })
3058 if $keepActive && $authuser ne 'root@pam';
3059
3060 my $migratedfrom = extract_param($param, 'migratedfrom');
3061 raise_param_exc({ migratedfrom => "Only root may use this option." })
3062 if $migratedfrom && $authuser ne 'root@pam';
3063
3064
3065 my $storecfg = PVE::Storage::config();
3066
3067 if (PVE::HA::Config::vm_is_ha_managed($vmid) && ($rpcenv->{type} ne 'ha') && !defined($migratedfrom)) {
3068
3069 my $hacmd = sub {
3070 my $upid = shift;
3071
3072 print "Requesting HA stop for VM $vmid\n";
3073
3074 my $cmd = ['ha-manager', 'crm-command', 'stop', "vm:$vmid", '0'];
3075 PVE::Tools::run_command($cmd);
3076 return;
3077 };
3078
3079 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
3080
3081 } else {
3082 my $realcmd = sub {
3083 my $upid = shift;
3084
3085 syslog('info', "stop VM $vmid: $upid\n");
3086
3087 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
3088 $param->{timeout}, 0, 1, $keepActive, $migratedfrom);
3089 return;
3090 };
3091
3092 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
3093 }
3094 }});
3095
3096 __PACKAGE__->register_method({
3097 name => 'vm_reset',
3098 path => '{vmid}/status/reset',
3099 method => 'POST',
3100 protected => 1,
3101 proxyto => 'node',
3102 description => "Reset virtual machine.",
3103 permissions => {
3104 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
3105 },
3106 parameters => {
3107 additionalProperties => 0,
3108 properties => {
3109 node => get_standard_option('pve-node'),
3110 vmid => get_standard_option('pve-vmid',
3111 { completion => \&PVE::QemuServer::complete_vmid_running }),
3112 skiplock => get_standard_option('skiplock'),
3113 },
3114 },
3115 returns => {
3116 type => 'string',
3117 },
3118 code => sub {
3119 my ($param) = @_;
3120
3121 my $rpcenv = PVE::RPCEnvironment::get();
3122
3123 my $authuser = $rpcenv->get_user();
3124
3125 my $node = extract_param($param, 'node');
3126
3127 my $vmid = extract_param($param, 'vmid');
3128
3129 my $skiplock = extract_param($param, 'skiplock');
3130 raise_param_exc({ skiplock => "Only root may use this option." })
3131 if $skiplock && $authuser ne 'root@pam';
3132
3133 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
3134
3135 my $realcmd = sub {
3136 my $upid = shift;
3137
3138 PVE::QemuServer::vm_reset($vmid, $skiplock);
3139
3140 return;
3141 };
3142
3143 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
3144 }});
3145
3146 __PACKAGE__->register_method({
3147 name => 'vm_shutdown',
3148 path => '{vmid}/status/shutdown',
3149 method => 'POST',
3150 protected => 1,
3151 proxyto => 'node',
3152 description => "Shutdown virtual machine. This is similar to pressing the power button on a physical machine." .
3153 "This will send an ACPI event for the guest OS, which should then proceed to a clean shutdown.",
3154 permissions => {
3155 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
3156 },
3157 parameters => {
3158 additionalProperties => 0,
3159 properties => {
3160 node => get_standard_option('pve-node'),
3161 vmid => get_standard_option('pve-vmid',
3162 { completion => \&PVE::QemuServer::complete_vmid_running }),
3163 skiplock => get_standard_option('skiplock'),
3164 timeout => {
3165 description => "Wait maximal timeout seconds.",
3166 type => 'integer',
3167 minimum => 0,
3168 optional => 1,
3169 },
3170 forceStop => {
3171 description => "Make sure the VM stops.",
3172 type => 'boolean',
3173 optional => 1,
3174 default => 0,
3175 },
3176 keepActive => {
3177 description => "Do not deactivate storage volumes.",
3178 type => 'boolean',
3179 optional => 1,
3180 default => 0,
3181 }
3182 },
3183 },
3184 returns => {
3185 type => 'string',
3186 },
3187 code => sub {
3188 my ($param) = @_;
3189
3190 my $rpcenv = PVE::RPCEnvironment::get();
3191 my $authuser = $rpcenv->get_user();
3192
3193 my $node = extract_param($param, 'node');
3194 my $vmid = extract_param($param, 'vmid');
3195
3196 my $skiplock = extract_param($param, 'skiplock');
3197 raise_param_exc({ skiplock => "Only root may use this option." })
3198 if $skiplock && $authuser ne 'root@pam';
3199
3200 my $keepActive = extract_param($param, 'keepActive');
3201 raise_param_exc({ keepActive => "Only root may use this option." })
3202 if $keepActive && $authuser ne 'root@pam';
3203
3204 my $storecfg = PVE::Storage::config();
3205
3206 my $shutdown = 1;
3207
3208 # sending a graceful shutdown command to paused VMs runs into timeouts, and even worse, when
3209 # the VM gets resumed later, it still gets the request delivered and powers off
3210 if (PVE::QemuServer::vm_is_paused($vmid, 1)) {
3211 if ($param->{forceStop}) {
3212 warn "VM is paused - stop instead of shutdown\n";
3213 $shutdown = 0;
3214 } else {
3215 die "VM is paused - cannot shutdown\n";
3216 }
3217 }
3218
3219 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
3220
3221 my $timeout = $param->{timeout} // 60;
3222 my $hacmd = sub {
3223 my $upid = shift;
3224
3225 print "Requesting HA stop for VM $vmid\n";
3226
3227 my $cmd = ['ha-manager', 'crm-command', 'stop', "vm:$vmid", "$timeout"];
3228 PVE::Tools::run_command($cmd);
3229 return;
3230 };
3231
3232 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
3233
3234 } else {
3235
3236 my $realcmd = sub {
3237 my $upid = shift;
3238
3239 syslog('info', "shutdown VM $vmid: $upid\n");
3240
3241 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
3242 $shutdown, $param->{forceStop}, $keepActive);
3243 return;
3244 };
3245
3246 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
3247 }
3248 }});
3249
3250 __PACKAGE__->register_method({
3251 name => 'vm_reboot',
3252 path => '{vmid}/status/reboot',
3253 method => 'POST',
3254 protected => 1,
3255 proxyto => 'node',
3256 description => "Reboot the VM by shutting it down, and starting it again. Applies pending changes.",
3257 permissions => {
3258 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
3259 },
3260 parameters => {
3261 additionalProperties => 0,
3262 properties => {
3263 node => get_standard_option('pve-node'),
3264 vmid => get_standard_option('pve-vmid',
3265 { completion => \&PVE::QemuServer::complete_vmid_running }),
3266 timeout => {
3267 description => "Wait maximal timeout seconds for the shutdown.",
3268 type => 'integer',
3269 minimum => 0,
3270 optional => 1,
3271 },
3272 },
3273 },
3274 returns => {
3275 type => 'string',
3276 },
3277 code => sub {
3278 my ($param) = @_;
3279
3280 my $rpcenv = PVE::RPCEnvironment::get();
3281 my $authuser = $rpcenv->get_user();
3282
3283 my $node = extract_param($param, 'node');
3284 my $vmid = extract_param($param, 'vmid');
3285
3286 die "VM is paused - cannot shutdown\n" if PVE::QemuServer::vm_is_paused($vmid, 1);
3287
3288 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
3289
3290 my $realcmd = sub {
3291 my $upid = shift;
3292
3293 syslog('info', "requesting reboot of VM $vmid: $upid\n");
3294 PVE::QemuServer::vm_reboot($vmid, $param->{timeout});
3295 return;
3296 };
3297
3298 return $rpcenv->fork_worker('qmreboot', $vmid, $authuser, $realcmd);
3299 }});
3300
3301 __PACKAGE__->register_method({
3302 name => 'vm_suspend',
3303 path => '{vmid}/status/suspend',
3304 method => 'POST',
3305 protected => 1,
3306 proxyto => 'node',
3307 description => "Suspend virtual machine.",
3308 permissions => {
3309 description => "You need 'VM.PowerMgmt' on /vms/{vmid}, and if you have set 'todisk',".
3310 " you need also 'VM.Config.Disk' on /vms/{vmid} and 'Datastore.AllocateSpace'".
3311 " on the storage for the vmstate.",
3312 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
3313 },
3314 parameters => {
3315 additionalProperties => 0,
3316 properties => {
3317 node => get_standard_option('pve-node'),
3318 vmid => get_standard_option('pve-vmid',
3319 { completion => \&PVE::QemuServer::complete_vmid_running }),
3320 skiplock => get_standard_option('skiplock'),
3321 todisk => {
3322 type => 'boolean',
3323 default => 0,
3324 optional => 1,
3325 description => 'If set, suspends the VM to disk. Will be resumed on next VM start.',
3326 },
3327 statestorage => get_standard_option('pve-storage-id', {
3328 description => "The storage for the VM state",
3329 requires => 'todisk',
3330 optional => 1,
3331 completion => \&PVE::Storage::complete_storage_enabled,
3332 }),
3333 },
3334 },
3335 returns => {
3336 type => 'string',
3337 },
3338 code => sub {
3339 my ($param) = @_;
3340
3341 my $rpcenv = PVE::RPCEnvironment::get();
3342 my $authuser = $rpcenv->get_user();
3343
3344 my $node = extract_param($param, 'node');
3345 my $vmid = extract_param($param, 'vmid');
3346
3347 my $todisk = extract_param($param, 'todisk') // 0;
3348
3349 my $statestorage = extract_param($param, 'statestorage');
3350
3351 my $skiplock = extract_param($param, 'skiplock');
3352 raise_param_exc({ skiplock => "Only root may use this option." })
3353 if $skiplock && $authuser ne 'root@pam';
3354
3355 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
3356
3357 die "Cannot suspend HA managed VM to disk\n"
3358 if $todisk && PVE::HA::Config::vm_is_ha_managed($vmid);
3359
3360 # early check for storage permission, for better user feedback
3361 if ($todisk) {
3362 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
3363 my $conf = PVE::QemuConfig->load_config($vmid);
3364
3365 # cannot save the state of a non-virtualized PCIe device, so resume cannot really work
3366 for my $key (keys %$conf) {
3367 next if $key !~ /^hostpci\d+/;
3368 die "cannot suspend VM to disk due to passed-through PCI device(s), which lack the"
3369 ." possibility to save/restore their internal state\n";
3370 }
3371
3372 if (!$statestorage) {
3373 # get statestorage from config if none is given
3374 my $storecfg = PVE::Storage::config();
3375 $statestorage = PVE::QemuServer::find_vmstate_storage($conf, $storecfg);
3376 }
3377
3378 $rpcenv->check($authuser, "/storage/$statestorage", ['Datastore.AllocateSpace']);
3379 }
3380
3381 my $realcmd = sub {
3382 my $upid = shift;
3383
3384 syslog('info', "suspend VM $vmid: $upid\n");
3385
3386 PVE::QemuServer::vm_suspend($vmid, $skiplock, $todisk, $statestorage);
3387
3388 return;
3389 };
3390
3391 my $taskname = $todisk ? 'qmsuspend' : 'qmpause';
3392 return $rpcenv->fork_worker($taskname, $vmid, $authuser, $realcmd);
3393 }});
3394
3395 __PACKAGE__->register_method({
3396 name => 'vm_resume',
3397 path => '{vmid}/status/resume',
3398 method => 'POST',
3399 protected => 1,
3400 proxyto => 'node',
3401 description => "Resume virtual machine.",
3402 permissions => {
3403 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
3404 },
3405 parameters => {
3406 additionalProperties => 0,
3407 properties => {
3408 node => get_standard_option('pve-node'),
3409 vmid => get_standard_option('pve-vmid',
3410 { completion => \&PVE::QemuServer::complete_vmid_running }),
3411 skiplock => get_standard_option('skiplock'),
3412 nocheck => { type => 'boolean', optional => 1 },
3413
3414 },
3415 },
3416 returns => {
3417 type => 'string',
3418 },
3419 code => sub {
3420 my ($param) = @_;
3421
3422 my $rpcenv = PVE::RPCEnvironment::get();
3423
3424 my $authuser = $rpcenv->get_user();
3425
3426 my $node = extract_param($param, 'node');
3427
3428 my $vmid = extract_param($param, 'vmid');
3429
3430 my $skiplock = extract_param($param, 'skiplock');
3431 raise_param_exc({ skiplock => "Only root may use this option." })
3432 if $skiplock && $authuser ne 'root@pam';
3433
3434 # nocheck is used as part of migration when config file might be still
3435 # be on source node
3436 my $nocheck = extract_param($param, 'nocheck');
3437 raise_param_exc({ nocheck => "Only root may use this option." })
3438 if $nocheck && $authuser ne 'root@pam';
3439
3440 my $to_disk_suspended;
3441 eval {
3442 PVE::QemuConfig->lock_config($vmid, sub {
3443 my $conf = PVE::QemuConfig->load_config($vmid);
3444 $to_disk_suspended = PVE::QemuConfig->has_lock($conf, 'suspended');
3445 });
3446 };
3447
3448 die "VM $vmid not running\n"
3449 if !$to_disk_suspended && !PVE::QemuServer::check_running($vmid, $nocheck);
3450
3451 my $realcmd = sub {
3452 my $upid = shift;
3453
3454 syslog('info', "resume VM $vmid: $upid\n");
3455
3456 if (!$to_disk_suspended) {
3457 PVE::QemuServer::vm_resume($vmid, $skiplock, $nocheck);
3458 } else {
3459 my $storecfg = PVE::Storage::config();
3460 PVE::QemuServer::vm_start($storecfg, $vmid, { skiplock => $skiplock });
3461 }
3462
3463 return;
3464 };
3465
3466 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
3467 }});
3468
3469 __PACKAGE__->register_method({
3470 name => 'vm_sendkey',
3471 path => '{vmid}/sendkey',
3472 method => 'PUT',
3473 protected => 1,
3474 proxyto => 'node',
3475 description => "Send key event to virtual machine.",
3476 permissions => {
3477 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
3478 },
3479 parameters => {
3480 additionalProperties => 0,
3481 properties => {
3482 node => get_standard_option('pve-node'),
3483 vmid => get_standard_option('pve-vmid',
3484 { completion => \&PVE::QemuServer::complete_vmid_running }),
3485 skiplock => get_standard_option('skiplock'),
3486 key => {
3487 description => "The key (qemu monitor encoding).",
3488 type => 'string'
3489 }
3490 },
3491 },
3492 returns => { type => 'null'},
3493 code => sub {
3494 my ($param) = @_;
3495
3496 my $rpcenv = PVE::RPCEnvironment::get();
3497
3498 my $authuser = $rpcenv->get_user();
3499
3500 my $node = extract_param($param, 'node');
3501
3502 my $vmid = extract_param($param, 'vmid');
3503
3504 my $skiplock = extract_param($param, 'skiplock');
3505 raise_param_exc({ skiplock => "Only root may use this option." })
3506 if $skiplock && $authuser ne 'root@pam';
3507
3508 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
3509
3510 return;
3511 }});
3512
3513 __PACKAGE__->register_method({
3514 name => 'vm_feature',
3515 path => '{vmid}/feature',
3516 method => 'GET',
3517 proxyto => 'node',
3518 protected => 1,
3519 description => "Check if feature for virtual machine is available.",
3520 permissions => {
3521 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
3522 },
3523 parameters => {
3524 additionalProperties => 0,
3525 properties => {
3526 node => get_standard_option('pve-node'),
3527 vmid => get_standard_option('pve-vmid'),
3528 feature => {
3529 description => "Feature to check.",
3530 type => 'string',
3531 enum => [ 'snapshot', 'clone', 'copy' ],
3532 },
3533 snapname => get_standard_option('pve-snapshot-name', {
3534 optional => 1,
3535 }),
3536 },
3537 },
3538 returns => {
3539 type => "object",
3540 properties => {
3541 hasFeature => { type => 'boolean' },
3542 nodes => {
3543 type => 'array',
3544 items => { type => 'string' },
3545 }
3546 },
3547 },
3548 code => sub {
3549 my ($param) = @_;
3550
3551 my $node = extract_param($param, 'node');
3552
3553 my $vmid = extract_param($param, 'vmid');
3554
3555 my $snapname = extract_param($param, 'snapname');
3556
3557 my $feature = extract_param($param, 'feature');
3558
3559 my $running = PVE::QemuServer::check_running($vmid);
3560
3561 my $conf = PVE::QemuConfig->load_config($vmid);
3562
3563 if($snapname){
3564 my $snap = $conf->{snapshots}->{$snapname};
3565 die "snapshot '$snapname' does not exist\n" if !defined($snap);
3566 $conf = $snap;
3567 }
3568 my $storecfg = PVE::Storage::config();
3569
3570 my $nodelist = PVE::QemuServer::shared_nodes($conf, $storecfg);
3571 my $hasFeature = PVE::QemuConfig->has_feature($feature, $conf, $storecfg, $snapname, $running);
3572
3573 return {
3574 hasFeature => $hasFeature,
3575 nodes => [ keys %$nodelist ],
3576 };
3577 }});
3578
3579 __PACKAGE__->register_method({
3580 name => 'clone_vm',
3581 path => '{vmid}/clone',
3582 method => 'POST',
3583 protected => 1,
3584 proxyto => 'node',
3585 description => "Create a copy of virtual machine/template.",
3586 permissions => {
3587 description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " .
3588 "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " .
3589 "'Datastore.AllocateSpace' on any used storage and 'SDN.Use' on any used bridge/vnet",
3590 check =>
3591 [ 'and',
3592 ['perm', '/vms/{vmid}', [ 'VM.Clone' ]],
3593 [ 'or',
3594 [ 'perm', '/vms/{newid}', ['VM.Allocate']],
3595 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
3596 ],
3597 ]
3598 },
3599 parameters => {
3600 additionalProperties => 0,
3601 properties => {
3602 node => get_standard_option('pve-node'),
3603 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3604 newid => get_standard_option('pve-vmid', {
3605 completion => \&PVE::Cluster::complete_next_vmid,
3606 description => 'VMID for the clone.' }),
3607 name => {
3608 optional => 1,
3609 type => 'string', format => 'dns-name',
3610 description => "Set a name for the new VM.",
3611 },
3612 description => {
3613 optional => 1,
3614 type => 'string',
3615 description => "Description for the new VM.",
3616 },
3617 pool => {
3618 optional => 1,
3619 type => 'string', format => 'pve-poolid',
3620 description => "Add the new VM to the specified pool.",
3621 },
3622 snapname => get_standard_option('pve-snapshot-name', {
3623 optional => 1,
3624 }),
3625 storage => get_standard_option('pve-storage-id', {
3626 description => "Target storage for full clone.",
3627 optional => 1,
3628 }),
3629 'format' => {
3630 description => "Target format for file storage. Only valid for full clone.",
3631 type => 'string',
3632 optional => 1,
3633 enum => [ 'raw', 'qcow2', 'vmdk'],
3634 },
3635 full => {
3636 optional => 1,
3637 type => 'boolean',
3638 description => "Create a full copy of all disks. This is always done when " .
3639 "you clone a normal VM. For VM templates, we try to create a linked clone by default.",
3640 },
3641 target => get_standard_option('pve-node', {
3642 description => "Target node. Only allowed if the original VM is on shared storage.",
3643 optional => 1,
3644 }),
3645 bwlimit => {
3646 description => "Override I/O bandwidth limit (in KiB/s).",
3647 optional => 1,
3648 type => 'integer',
3649 minimum => '0',
3650 default => 'clone limit from datacenter or storage config',
3651 },
3652 },
3653 },
3654 returns => {
3655 type => 'string',
3656 },
3657 code => sub {
3658 my ($param) = @_;
3659
3660 my $rpcenv = PVE::RPCEnvironment::get();
3661 my $authuser = $rpcenv->get_user();
3662
3663 my $node = extract_param($param, 'node');
3664 my $vmid = extract_param($param, 'vmid');
3665 my $newid = extract_param($param, 'newid');
3666 my $pool = extract_param($param, 'pool');
3667
3668 my $snapname = extract_param($param, 'snapname');
3669 my $storage = extract_param($param, 'storage');
3670 my $format = extract_param($param, 'format');
3671 my $target = extract_param($param, 'target');
3672
3673 my $localnode = PVE::INotify::nodename();
3674
3675 if ($target && ($target eq $localnode || $target eq 'localhost')) {
3676 undef $target;
3677 }
3678
3679 my $running = PVE::QemuServer::check_running($vmid) || 0;
3680
3681 my $load_and_check = sub {
3682 $rpcenv->check_pool_exist($pool) if defined($pool);
3683 PVE::Cluster::check_node_exists($target) if $target;
3684
3685 my $storecfg = PVE::Storage::config();
3686
3687 if ($storage) {
3688 # check if storage is enabled on local node
3689 PVE::Storage::storage_check_enabled($storecfg, $storage);
3690 if ($target) {
3691 # check if storage is available on target node
3692 PVE::Storage::storage_check_enabled($storecfg, $storage, $target);
3693 # clone only works if target storage is shared
3694 my $scfg = PVE::Storage::storage_config($storecfg, $storage);
3695 die "can't clone to non-shared storage '$storage'\n"
3696 if !$scfg->{shared};
3697 }
3698 }
3699
3700 PVE::Cluster::check_cfs_quorum();
3701
3702 my $conf = PVE::QemuConfig->load_config($vmid);
3703 PVE::QemuConfig->check_lock($conf);
3704
3705 my $verify_running = PVE::QemuServer::check_running($vmid) || 0;
3706 die "unexpected state change\n" if $verify_running != $running;
3707
3708 die "snapshot '$snapname' does not exist\n"
3709 if $snapname && !defined( $conf->{snapshots}->{$snapname});
3710
3711 my $full = $param->{full} // !PVE::QemuConfig->is_template($conf);
3712
3713 die "parameter 'storage' not allowed for linked clones\n"
3714 if defined($storage) && !$full;
3715
3716 die "parameter 'format' not allowed for linked clones\n"
3717 if defined($format) && !$full;
3718
3719 my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
3720
3721 my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage);
3722 PVE::QemuServer::check_mapping_access($rpcenv, $authuser, $oldconf);
3723
3724 PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $oldconf);
3725
3726 die "can't clone VM to node '$target' (VM uses local storage)\n"
3727 if $target && !$sharedvm;
3728
3729 my $conffile = PVE::QemuConfig->config_file($newid);
3730 die "unable to create VM $newid: config file already exists\n"
3731 if -f $conffile;
3732
3733 my $newconf = { lock => 'clone' };
3734 my $drives = {};
3735 my $fullclone = {};
3736 my $vollist = [];
3737
3738 foreach my $opt (keys %$oldconf) {
3739 my $value = $oldconf->{$opt};
3740
3741 # do not copy snapshot related info
3742 next if $opt eq 'snapshots' || $opt eq 'parent' || $opt eq 'snaptime' ||
3743 $opt eq 'vmstate' || $opt eq 'snapstate';
3744
3745 # no need to copy unused images, because VMID(owner) changes anyways
3746 next if $opt =~ m/^unused\d+$/;
3747
3748 die "cannot clone TPM state while VM is running\n"
3749 if $full && $running && !$snapname && $opt eq 'tpmstate0';
3750
3751 # always change MAC! address
3752 if ($opt =~ m/^net(\d+)$/) {
3753 my $net = PVE::QemuServer::parse_net($value);
3754 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
3755 $net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
3756 $newconf->{$opt} = PVE::QemuServer::print_net($net);
3757 } elsif (PVE::QemuServer::is_valid_drivename($opt)) {
3758 my $drive = PVE::QemuServer::parse_drive($opt, $value);
3759 die "unable to parse drive options for '$opt'\n" if !$drive;
3760 if (PVE::QemuServer::drive_is_cdrom($drive, 1)) {
3761 $newconf->{$opt} = $value; # simply copy configuration
3762 } else {
3763 if ($full || PVE::QemuServer::drive_is_cloudinit($drive)) {
3764 die "Full clone feature is not supported for drive '$opt'\n"
3765 if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running);
3766 $fullclone->{$opt} = 1;
3767 } else {
3768 # not full means clone instead of copy
3769 die "Linked clone feature is not supported for drive '$opt'\n"
3770 if !PVE::Storage::volume_has_feature($storecfg, 'clone', $drive->{file}, $snapname, $running);
3771 }
3772 $drives->{$opt} = $drive;
3773 next if PVE::QemuServer::drive_is_cloudinit($drive);
3774 push @$vollist, $drive->{file};
3775 }
3776 } else {
3777 # copy everything else
3778 $newconf->{$opt} = $value;
3779 }
3780 }
3781
3782 return ($conffile, $newconf, $oldconf, $vollist, $drives, $fullclone);
3783 };
3784
3785 my $clonefn = sub {
3786 my ($conffile, $newconf, $oldconf, $vollist, $drives, $fullclone) = $load_and_check->();
3787 my $storecfg = PVE::Storage::config();
3788
3789 # auto generate a new uuid
3790 my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || '');
3791 $smbios1->{uuid} = PVE::QemuServer::generate_uuid();
3792 $newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1);
3793 # auto generate a new vmgenid only if the option was set for template
3794 if ($newconf->{vmgenid}) {
3795 $newconf->{vmgenid} = PVE::QemuServer::generate_uuid();
3796 }
3797
3798 delete $newconf->{template};
3799
3800 if ($param->{name}) {
3801 $newconf->{name} = $param->{name};
3802 } else {
3803 $newconf->{name} = "Copy-of-VM-" . ($oldconf->{name} // $vmid);
3804 }
3805
3806 if ($param->{description}) {
3807 $newconf->{description} = $param->{description};
3808 }
3809
3810 # create empty/temp config - this fails if VM already exists on other node
3811 # FIXME use PVE::QemuConfig->create_and_lock_config and adapt code
3812 PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n");
3813
3814 PVE::Firewall::clone_vmfw_conf($vmid, $newid);
3815
3816 my $newvollist = [];
3817 my $jobs = {};
3818
3819 eval {
3820 local $SIG{INT} =
3821 local $SIG{TERM} =
3822 local $SIG{QUIT} =
3823 local $SIG{HUP} = sub { die "interrupted by signal\n"; };
3824
3825 PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
3826
3827 my $bwlimit = extract_param($param, 'bwlimit');
3828
3829 my $total_jobs = scalar(keys %{$drives});
3830 my $i = 1;
3831
3832 foreach my $opt (sort keys %$drives) {
3833 my $drive = $drives->{$opt};
3834 my $skipcomplete = ($total_jobs != $i); # finish after last drive
3835 my $completion = $skipcomplete ? 'skip' : 'complete';
3836
3837 my $src_sid = PVE::Storage::parse_volume_id($drive->{file});
3838 my $storage_list = [ $src_sid ];
3839 push @$storage_list, $storage if defined($storage);
3840 my $clonelimit = PVE::Storage::get_bandwidth_limit('clone', $storage_list, $bwlimit);
3841
3842 my $source_info = {
3843 vmid => $vmid,
3844 running => $running,
3845 drivename => $opt,
3846 drive => $drive,
3847 snapname => $snapname,
3848 };
3849
3850 my $dest_info = {
3851 vmid => $newid,
3852 drivename => $opt,
3853 storage => $storage,
3854 format => $format,
3855 };
3856
3857 $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($oldconf)
3858 if $opt eq 'efidisk0';
3859
3860 my $newdrive = PVE::QemuServer::clone_disk(
3861 $storecfg,
3862 $source_info,
3863 $dest_info,
3864 $fullclone->{$opt},
3865 $newvollist,
3866 $jobs,
3867 $completion,
3868 $oldconf->{agent},
3869 $clonelimit,
3870 );
3871
3872 $newconf->{$opt} = PVE::QemuServer::print_drive($newdrive);
3873
3874 PVE::QemuConfig->write_config($newid, $newconf);
3875 $i++;
3876 }
3877
3878 delete $newconf->{lock};
3879
3880 # do not write pending changes
3881 if (my @changes = keys %{$newconf->{pending}}) {
3882 my $pending = join(',', @changes);
3883 warn "found pending changes for '$pending', discarding for clone\n";
3884 delete $newconf->{pending};
3885 }
3886
3887 PVE::QemuConfig->write_config($newid, $newconf);
3888
3889 PVE::QemuServer::create_ifaces_ipams_ips($newconf, $newid);
3890
3891 if ($target) {
3892 if (!$running) {
3893 # always deactivate volumes – avoids that LVM LVs are active on several nodes
3894 eval { PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) };
3895 # but only warn when that fails (e.g., parallel clones keeping them active)
3896 log_warn($@) if $@;
3897 }
3898
3899 PVE::Storage::deactivate_volumes($storecfg, $newvollist);
3900
3901 my $newconffile = PVE::QemuConfig->config_file($newid, $target);
3902 die "Failed to move config to node '$target' - rename failed: $!\n"
3903 if !rename($conffile, $newconffile);
3904 }
3905
3906 PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
3907 };
3908 if (my $err = $@) {
3909 eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs) };
3910 sleep 1; # some storage like rbd need to wait before release volume - really?
3911
3912 foreach my $volid (@$newvollist) {
3913 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
3914 warn $@ if $@;
3915 }
3916
3917 PVE::Firewall::remove_vmfw_conf($newid);
3918
3919 unlink $conffile; # avoid races -> last thing before die
3920
3921 die "clone failed: $err";
3922 }
3923
3924 return;
3925 };
3926
3927 # Aquire exclusive lock lock for $newid
3928 my $lock_target_vm = sub {
3929 return PVE::QemuConfig->lock_config_full($newid, 1, $clonefn);
3930 };
3931
3932 my $lock_source_vm = sub {
3933 # exclusive lock if VM is running - else shared lock is enough;
3934 if ($running) {
3935 return PVE::QemuConfig->lock_config_full($vmid, 1, $lock_target_vm);
3936 } else {
3937 return PVE::QemuConfig->lock_config_shared($vmid, 1, $lock_target_vm);
3938 }
3939 };
3940
3941 $load_and_check->(); # early checks before forking/locking
3942
3943 return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $lock_source_vm);
3944 }});
3945
3946 __PACKAGE__->register_method({
3947 name => 'move_vm_disk',
3948 path => '{vmid}/move_disk',
3949 method => 'POST',
3950 protected => 1,
3951 proxyto => 'node',
3952 description => "Move volume to different storage or to a different VM.",
3953 permissions => {
3954 description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " .
3955 "and 'Datastore.AllocateSpace' permissions on the storage. To move ".
3956 "a disk to another VM, you need the permissions on the target VM as well.",
3957 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
3958 },
3959 parameters => {
3960 additionalProperties => 0,
3961 properties => {
3962 node => get_standard_option('pve-node'),
3963 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3964 'target-vmid' => get_standard_option('pve-vmid', {
3965 completion => \&PVE::QemuServer::complete_vmid,
3966 optional => 1,
3967 }),
3968 disk => {
3969 type => 'string',
3970 description => "The disk you want to move.",
3971 enum => [PVE::QemuServer::Drive::valid_drive_names_with_unused()],
3972 },
3973 storage => get_standard_option('pve-storage-id', {
3974 description => "Target storage.",
3975 completion => \&PVE::QemuServer::complete_storage,
3976 optional => 1,
3977 }),
3978 'format' => {
3979 type => 'string',
3980 description => "Target Format.",
3981 enum => [ 'raw', 'qcow2', 'vmdk' ],
3982 optional => 1,
3983 },
3984 delete => {
3985 type => 'boolean',
3986 description => "Delete the original disk after successful copy. By default the"
3987 ." original disk is kept as unused disk.",
3988 optional => 1,
3989 default => 0,
3990 },
3991 digest => {
3992 type => 'string',
3993 description => 'Prevent changes if current configuration file has different SHA1"
3994 ." digest. This can be used to prevent concurrent modifications.',
3995 maxLength => 40,
3996 optional => 1,
3997 },
3998 bwlimit => {
3999 description => "Override I/O bandwidth limit (in KiB/s).",
4000 optional => 1,
4001 type => 'integer',
4002 minimum => '0',
4003 default => 'move limit from datacenter or storage config',
4004 },
4005 'target-disk' => {
4006 type => 'string',
4007 description => "The config key the disk will be moved to on the target VM"
4008 ." (for example, ide0 or scsi1). Default is the source disk key.",
4009 enum => [PVE::QemuServer::Drive::valid_drive_names_with_unused()],
4010 optional => 1,
4011 },
4012 'target-digest' => {
4013 type => 'string',
4014 description => 'Prevent changes if the current config file of the target VM has a"
4015 ." different SHA1 digest. This can be used to detect concurrent modifications.',
4016 maxLength => 40,
4017 optional => 1,
4018 },
4019 },
4020 },
4021 returns => {
4022 type => 'string',
4023 description => "the task ID.",
4024 },
4025 code => sub {
4026 my ($param) = @_;
4027
4028 my $rpcenv = PVE::RPCEnvironment::get();
4029 my $authuser = $rpcenv->get_user();
4030
4031 my $node = extract_param($param, 'node');
4032 my $vmid = extract_param($param, 'vmid');
4033 my $target_vmid = extract_param($param, 'target-vmid');
4034 my $digest = extract_param($param, 'digest');
4035 my $target_digest = extract_param($param, 'target-digest');
4036 my $disk = extract_param($param, 'disk');
4037 my $target_disk = extract_param($param, 'target-disk') // $disk;
4038 my $storeid = extract_param($param, 'storage');
4039 my $format = extract_param($param, 'format');
4040
4041 my $storecfg = PVE::Storage::config();
4042
4043 my $load_and_check_move = sub {
4044 my $conf = PVE::QemuConfig->load_config($vmid);
4045 PVE::QemuConfig->check_lock($conf);
4046
4047 PVE::Tools::assert_if_modified($digest, $conf->{digest});
4048
4049 die "disk '$disk' does not exist\n" if !$conf->{$disk};
4050
4051 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
4052
4053 die "disk '$disk' has no associated volume\n" if !$drive->{file};
4054 die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive, 1);
4055
4056 my $old_volid = $drive->{file};
4057 my $oldfmt;
4058 my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($old_volid);
4059 if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){
4060 $oldfmt = $1;
4061 }
4062
4063 die "you can't move to the same storage with same format\n"
4064 if $oldstoreid eq $storeid && (!$format || !$oldfmt || $oldfmt eq $format);
4065
4066 # this only checks snapshots because $disk is passed!
4067 my $snapshotted = PVE::QemuServer::Drive::is_volume_in_use(
4068 $storecfg,
4069 $conf,
4070 $disk,
4071 $old_volid
4072 );
4073 die "you can't move a disk with snapshots and delete the source\n"
4074 if $snapshotted && $param->{delete};
4075
4076 return ($conf, $drive, $oldstoreid, $snapshotted);
4077 };
4078
4079 my $move_updatefn = sub {
4080 my ($conf, $drive, $oldstoreid, $snapshotted) = $load_and_check_move->();
4081 my $old_volid = $drive->{file};
4082
4083 PVE::Cluster::log_msg(
4084 'info',
4085 $authuser,
4086 "move disk VM $vmid: move --disk $disk --storage $storeid"
4087 );
4088
4089 my $running = PVE::QemuServer::check_running($vmid);
4090
4091 PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]);
4092
4093 my $newvollist = [];
4094
4095 eval {
4096 local $SIG{INT} =
4097 local $SIG{TERM} =
4098 local $SIG{QUIT} =
4099 local $SIG{HUP} = sub { die "interrupted by signal\n"; };
4100
4101 warn "moving disk with snapshots, snapshots will not be moved!\n"
4102 if $snapshotted;
4103
4104 my $bwlimit = extract_param($param, 'bwlimit');
4105 my $movelimit = PVE::Storage::get_bandwidth_limit(
4106 'move',
4107 [$oldstoreid, $storeid],
4108 $bwlimit
4109 );
4110
4111 my $source_info = {
4112 vmid => $vmid,
4113 running => $running,
4114 drivename => $disk,
4115 drive => $drive,
4116 snapname => undef,
4117 };
4118
4119 my $dest_info = {
4120 vmid => $vmid,
4121 drivename => $disk,
4122 storage => $storeid,
4123 format => $format,
4124 };
4125
4126 $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($conf)
4127 if $disk eq 'efidisk0';
4128
4129 my $newdrive = PVE::QemuServer::clone_disk(
4130 $storecfg,
4131 $source_info,
4132 $dest_info,
4133 1,
4134 $newvollist,
4135 undef,
4136 undef,
4137 undef,
4138 $movelimit,
4139 );
4140 $conf->{$disk} = PVE::QemuServer::print_drive($newdrive);
4141
4142 PVE::QemuConfig->add_unused_volume($conf, $old_volid) if !$param->{delete};
4143
4144 # convert moved disk to base if part of template
4145 PVE::QemuServer::template_create($vmid, $conf, $disk)
4146 if PVE::QemuConfig->is_template($conf);
4147
4148 PVE::QemuConfig->write_config($vmid, $conf);
4149
4150 my $do_trim = PVE::QemuServer::get_qga_key($conf, 'fstrim_cloned_disks');
4151 if ($running && $do_trim && PVE::QemuServer::qga_check_running($vmid)) {
4152 eval { mon_cmd($vmid, "guest-fstrim") };
4153 }
4154
4155 eval {
4156 # try to deactivate volumes - avoid lvm LVs to be active on several nodes
4157 PVE::Storage::deactivate_volumes($storecfg, [ $newdrive->{file} ])
4158 if !$running;
4159 };
4160 warn $@ if $@;
4161 };
4162 if (my $err = $@) {
4163 foreach my $volid (@$newvollist) {
4164 eval { PVE::Storage::vdisk_free($storecfg, $volid) };
4165 warn $@ if $@;
4166 }
4167 die "storage migration failed: $err";
4168 }
4169
4170 if ($param->{delete}) {
4171 eval {
4172 PVE::Storage::deactivate_volumes($storecfg, [$old_volid]);
4173 PVE::Storage::vdisk_free($storecfg, $old_volid);
4174 };
4175 warn $@ if $@;
4176 }
4177 };
4178
4179 my $load_and_check_reassign_configs = sub {
4180 my $vmlist = PVE::Cluster::get_vmlist()->{ids};
4181
4182 die "could not find VM ${vmid}\n" if !exists($vmlist->{$vmid});
4183 die "could not find target VM ${target_vmid}\n" if !exists($vmlist->{$target_vmid});
4184
4185 my $source_node = $vmlist->{$vmid}->{node};
4186 my $target_node = $vmlist->{$target_vmid}->{node};
4187
4188 die "Both VMs need to be on the same node ($source_node != $target_node)\n"
4189 if $source_node ne $target_node;
4190
4191 my $source_conf = PVE::QemuConfig->load_config($vmid);
4192 PVE::QemuConfig->check_lock($source_conf);
4193 my $target_conf = PVE::QemuConfig->load_config($target_vmid);
4194 PVE::QemuConfig->check_lock($target_conf);
4195
4196 die "Can't move disks from or to template VMs\n"
4197 if ($source_conf->{template} || $target_conf->{template});
4198
4199 if ($digest) {
4200 eval { PVE::Tools::assert_if_modified($digest, $source_conf->{digest}) };
4201 die "VM ${vmid}: $@" if $@;
4202 }
4203
4204 if ($target_digest) {
4205 eval { PVE::Tools::assert_if_modified($target_digest, $target_conf->{digest}) };
4206 die "VM ${target_vmid}: $@" if $@;
4207 }
4208
4209 die "Disk '${disk}' for VM '$vmid' does not exist\n" if !defined($source_conf->{$disk});
4210
4211 die "Target disk key '${target_disk}' is already in use for VM '$target_vmid'\n"
4212 if $target_conf->{$target_disk};
4213
4214 my $drive = PVE::QemuServer::parse_drive(
4215 $disk,
4216 $source_conf->{$disk},
4217 );
4218 die "failed to parse source disk - $@\n" if !$drive;
4219
4220 my $source_volid = $drive->{file};
4221
4222 die "disk '${disk}' has no associated volume\n" if !$source_volid;
4223 die "CD drive contents can't be moved to another VM\n"
4224 if PVE::QemuServer::drive_is_cdrom($drive, 1);
4225
4226 my $storeid = PVE::Storage::parse_volume_id($source_volid, 1);
4227 die "Volume '$source_volid' not managed by PVE\n" if !defined($storeid);
4228
4229 die "Can't move disk used by a snapshot to another VM\n"
4230 if PVE::QemuServer::Drive::is_volume_in_use($storecfg, $source_conf, $disk, $source_volid);
4231 die "Storage does not support moving of this disk to another VM\n"
4232 if (!PVE::Storage::volume_has_feature($storecfg, 'rename', $source_volid));
4233 die "Cannot move disk to another VM while the source VM is running - detach first\n"
4234 if PVE::QemuServer::check_running($vmid) && $disk !~ m/^unused\d+$/;
4235
4236 # now re-parse using target disk slot format
4237 if ($target_disk =~ /^unused\d+$/) {
4238 $drive = PVE::QemuServer::parse_drive(
4239 $target_disk,
4240 $source_volid,
4241 );
4242 } else {
4243 $drive = PVE::QemuServer::parse_drive(
4244 $target_disk,
4245 $source_conf->{$disk},
4246 );
4247 }
4248 die "failed to parse source disk for target disk format - $@\n" if !$drive;
4249
4250 my $repl_conf = PVE::ReplicationConfig->new();
4251 if ($repl_conf->check_for_existing_jobs($target_vmid, 1)) {
4252 my $format = (PVE::Storage::parse_volname($storecfg, $source_volid))[6];
4253 die "Cannot move disk to a replicated VM. Storage does not support replication!\n"
4254 if !PVE::Storage::storage_can_replicate($storecfg, $storeid, $format);
4255 }
4256
4257 return ($source_conf, $target_conf, $drive);
4258 };
4259
4260 my $logfunc = sub {
4261 my ($msg) = @_;
4262 print STDERR "$msg\n";
4263 };
4264
4265 my $disk_reassignfn = sub {
4266 return PVE::QemuConfig->lock_config($vmid, sub {
4267 return PVE::QemuConfig->lock_config($target_vmid, sub {
4268 my ($source_conf, $target_conf, $drive) = &$load_and_check_reassign_configs();
4269
4270 my $source_volid = $drive->{file};
4271
4272 print "moving disk '$disk' from VM '$vmid' to '$target_vmid'\n";
4273 my ($storeid, $source_volname) = PVE::Storage::parse_volume_id($source_volid);
4274
4275 my $fmt = (PVE::Storage::parse_volname($storecfg, $source_volid))[6];
4276
4277 my $new_volid = PVE::Storage::rename_volume(
4278 $storecfg,
4279 $source_volid,
4280 $target_vmid,
4281 );
4282
4283 $drive->{file} = $new_volid;
4284
4285 my $boot_order = PVE::QemuServer::device_bootorder($source_conf);
4286 if (defined(delete $boot_order->{$disk})) {
4287 print "removing disk '$disk' from boot order config\n";
4288 my $boot_devs = [ sort { $boot_order->{$a} <=> $boot_order->{$b} } keys %$boot_order ];
4289 $source_conf->{boot} = PVE::QemuServer::print_bootorder($boot_devs);
4290 }
4291
4292 delete $source_conf->{$disk};
4293 print "removing disk '${disk}' from VM '${vmid}' config\n";
4294 PVE::QemuConfig->write_config($vmid, $source_conf);
4295
4296 my $drive_string = PVE::QemuServer::print_drive($drive);
4297
4298 if ($target_disk =~ /^unused\d+$/) {
4299 $target_conf->{$target_disk} = $drive_string;
4300 PVE::QemuConfig->write_config($target_vmid, $target_conf);
4301 } else {
4302 &$update_vm_api(
4303 {
4304 node => $node,
4305 vmid => $target_vmid,
4306 digest => $target_digest,
4307 $target_disk => $drive_string,
4308 },
4309 1,
4310 );
4311 }
4312
4313 # remove possible replication snapshots
4314 if (PVE::Storage::volume_has_feature(
4315 $storecfg,
4316 'replicate',
4317 $source_volid),
4318 ) {
4319 eval {
4320 PVE::Replication::prepare(
4321 $storecfg,
4322 [$new_volid],
4323 undef,
4324 1,
4325 undef,
4326 $logfunc,
4327 )
4328 };
4329 if (my $err = $@) {
4330 print "Failed to remove replication snapshots on moved disk " .
4331 "'$target_disk'. Manual cleanup could be necessary.\n";
4332 }
4333 }
4334 });
4335 });
4336 };
4337
4338 if ($target_vmid && $storeid) {
4339 my $msg = "either set 'storage' or 'target-vmid', but not both";
4340 raise_param_exc({ 'target-vmid' => $msg, 'storage' => $msg });
4341 } elsif ($target_vmid) {
4342 $rpcenv->check_vm_perm($authuser, $target_vmid, undef, ['VM.Config.Disk'])
4343 if $authuser ne 'root@pam';
4344
4345 raise_param_exc({ 'target-vmid' => "must be different than source VMID to reassign disk" })
4346 if $vmid eq $target_vmid;
4347
4348 my (undef, undef, $drive) = &$load_and_check_reassign_configs();
4349 my $storage = PVE::Storage::parse_volume_id($drive->{file});
4350 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']);
4351
4352 return $rpcenv->fork_worker(
4353 'qmmove',
4354 "${vmid}-${disk}>${target_vmid}-${target_disk}",
4355 $authuser,
4356 $disk_reassignfn
4357 );
4358 } elsif ($storeid) {
4359 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
4360
4361 die "cannot move disk '$disk', only configured disks can be moved to another storage\n"
4362 if $disk =~ m/^unused\d+$/;
4363
4364 $load_and_check_move->(); # early checks before forking/locking
4365
4366 my $realcmd = sub {
4367 PVE::QemuConfig->lock_config($vmid, $move_updatefn);
4368 };
4369
4370 return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd);
4371 } else {
4372 my $msg = "both 'storage' and 'target-vmid' missing, either needs to be set";
4373 raise_param_exc({ 'target-vmid' => $msg, 'storage' => $msg });
4374 }
4375 }});
4376
4377 my $check_vm_disks_local = sub {
4378 my ($storecfg, $vmconf, $vmid) = @_;
4379
4380 my $local_disks = {};
4381
4382 # add some more information to the disks e.g. cdrom
4383 PVE::QemuServer::foreach_volid($vmconf, sub {
4384 my ($volid, $attr) = @_;
4385
4386 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
4387 if ($storeid) {
4388 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
4389 return if $scfg->{shared};
4390 }
4391 # The shared attr here is just a special case where the vdisk
4392 # is marked as shared manually
4393 return if $attr->{shared};
4394 return if $attr->{cdrom} and $volid eq "none";
4395
4396 if (exists $local_disks->{$volid}) {
4397 @{$local_disks->{$volid}}{keys %$attr} = values %$attr
4398 } else {
4399 $local_disks->{$volid} = $attr;
4400 # ensure volid is present in case it's needed
4401 $local_disks->{$volid}->{volid} = $volid;
4402 }
4403 });
4404
4405 return $local_disks;
4406 };
4407
4408 __PACKAGE__->register_method({
4409 name => 'migrate_vm_precondition',
4410 path => '{vmid}/migrate',
4411 method => 'GET',
4412 protected => 1,
4413 proxyto => 'node',
4414 description => "Get preconditions for migration.",
4415 permissions => {
4416 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
4417 },
4418 parameters => {
4419 additionalProperties => 0,
4420 properties => {
4421 node => get_standard_option('pve-node'),
4422 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
4423 target => get_standard_option('pve-node', {
4424 description => "Target node.",
4425 completion => \&PVE::Cluster::complete_migration_target,
4426 optional => 1,
4427 }),
4428 },
4429 },
4430 returns => {
4431 type => "object",
4432 properties => {
4433 running => { type => 'boolean' },
4434 allowed_nodes => {
4435 type => 'array',
4436 optional => 1,
4437 description => "List nodes allowed for offline migration, only passed if VM is offline"
4438 },
4439 not_allowed_nodes => {
4440 type => 'object',
4441 optional => 1,
4442 description => "List not allowed nodes with additional informations, only passed if VM is offline"
4443 },
4444 local_disks => {
4445 type => 'array',
4446 description => "List local disks including CD-Rom, unsused and not referenced disks"
4447 },
4448 local_resources => {
4449 type => 'array',
4450 description => "List local resources e.g. pci, usb"
4451 },
4452 'mapped-resources' => {
4453 type => 'array',
4454 description => "List of mapped resources e.g. pci, usb"
4455 },
4456 },
4457 },
4458 code => sub {
4459 my ($param) = @_;
4460
4461 my $rpcenv = PVE::RPCEnvironment::get();
4462
4463 my $authuser = $rpcenv->get_user();
4464
4465 PVE::Cluster::check_cfs_quorum();
4466
4467 my $res = {};
4468
4469 my $vmid = extract_param($param, 'vmid');
4470 my $target = extract_param($param, 'target');
4471 my $localnode = PVE::INotify::nodename();
4472
4473
4474 # test if VM exists
4475 my $vmconf = PVE::QemuConfig->load_config($vmid);
4476 my $storecfg = PVE::Storage::config();
4477
4478
4479 # try to detect errors early
4480 PVE::QemuConfig->check_lock($vmconf);
4481
4482 $res->{running} = PVE::QemuServer::check_running($vmid) ? 1:0;
4483
4484 my ($local_resources, $mapped_resources, $missing_mappings_by_node) =
4485 PVE::QemuServer::check_local_resources($vmconf, 1);
4486 delete $missing_mappings_by_node->{$localnode};
4487
4488 my $vga = PVE::QemuServer::parse_vga($vmconf->{vga});
4489 if ($res->{running} && $vga->{'clipboard'} && $vga->{'clipboard'} eq 'vnc') {
4490 push $local_resources->@*, "clipboard=vnc";
4491 }
4492
4493 # if vm is not running, return target nodes where local storage/mapped devices are available
4494 # for offline migration
4495 if (!$res->{running}) {
4496 $res->{allowed_nodes} = [];
4497 my $checked_nodes = PVE::QemuServer::check_local_storage_availability($vmconf, $storecfg);
4498 delete $checked_nodes->{$localnode};
4499
4500 foreach my $node (keys %$checked_nodes) {
4501 my $missing_mappings = $missing_mappings_by_node->{$node};
4502 if (scalar($missing_mappings->@*)) {
4503 $checked_nodes->{$node}->{'unavailable-resources'} = $missing_mappings;
4504 next;
4505 }
4506
4507 if (!defined($checked_nodes->{$node}->{unavailable_storages})) {
4508 push @{$res->{allowed_nodes}}, $node;
4509 }
4510
4511 }
4512 $res->{not_allowed_nodes} = $checked_nodes;
4513 }
4514
4515 my $local_disks = &$check_vm_disks_local($storecfg, $vmconf, $vmid);
4516 $res->{local_disks} = [ values %$local_disks ];;
4517
4518 $res->{local_resources} = $local_resources;
4519 $res->{'mapped-resources'} = $mapped_resources;
4520
4521 return $res;
4522
4523
4524 }});
4525
4526 __PACKAGE__->register_method({
4527 name => 'migrate_vm',
4528 path => '{vmid}/migrate',
4529 method => 'POST',
4530 protected => 1,
4531 proxyto => 'node',
4532 description => "Migrate virtual machine. Creates a new migration task.",
4533 permissions => {
4534 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
4535 },
4536 parameters => {
4537 additionalProperties => 0,
4538 properties => {
4539 node => get_standard_option('pve-node'),
4540 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
4541 target => get_standard_option('pve-node', {
4542 description => "Target node.",
4543 completion => \&PVE::Cluster::complete_migration_target,
4544 }),
4545 online => {
4546 type => 'boolean',
4547 description => "Use online/live migration if VM is running. Ignored if VM is stopped.",
4548 optional => 1,
4549 },
4550 force => {
4551 type => 'boolean',
4552 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
4553 optional => 1,
4554 },
4555 migration_type => {
4556 type => 'string',
4557 enum => ['secure', 'insecure'],
4558 description => "Migration traffic is encrypted using an SSH tunnel by default. On secure, completely private networks this can be disabled to increase performance.",
4559 optional => 1,
4560 },
4561 migration_network => {
4562 type => 'string', format => 'CIDR',
4563 description => "CIDR of the (sub) network that is used for migration.",
4564 optional => 1,
4565 },
4566 "with-local-disks" => {
4567 type => 'boolean',
4568 description => "Enable live storage migration for local disk",
4569 optional => 1,
4570 },
4571 targetstorage => get_standard_option('pve-targetstorage', {
4572 completion => \&PVE::QemuServer::complete_migration_storage,
4573 }),
4574 bwlimit => {
4575 description => "Override I/O bandwidth limit (in KiB/s).",
4576 optional => 1,
4577 type => 'integer',
4578 minimum => '0',
4579 default => 'migrate limit from datacenter or storage config',
4580 },
4581 },
4582 },
4583 returns => {
4584 type => 'string',
4585 description => "the task ID.",
4586 },
4587 code => sub {
4588 my ($param) = @_;
4589
4590 my $rpcenv = PVE::RPCEnvironment::get();
4591 my $authuser = $rpcenv->get_user();
4592
4593 my $target = extract_param($param, 'target');
4594
4595 my $localnode = PVE::INotify::nodename();
4596 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
4597
4598 PVE::Cluster::check_cfs_quorum();
4599
4600 PVE::Cluster::check_node_exists($target);
4601
4602 my $targetip = PVE::Cluster::remote_node_ip($target);
4603
4604 my $vmid = extract_param($param, 'vmid');
4605
4606 raise_param_exc({ force => "Only root may use this option." })
4607 if $param->{force} && $authuser ne 'root@pam';
4608
4609 raise_param_exc({ migration_type => "Only root may use this option." })
4610 if $param->{migration_type} && $authuser ne 'root@pam';
4611
4612 # allow root only until better network permissions are available
4613 raise_param_exc({ migration_network => "Only root may use this option." })
4614 if $param->{migration_network} && $authuser ne 'root@pam';
4615
4616 # test if VM exists
4617 my $conf = PVE::QemuConfig->load_config($vmid);
4618
4619 # try to detect errors early
4620
4621 PVE::QemuConfig->check_lock($conf);
4622
4623 if (PVE::QemuServer::check_running($vmid)) {
4624 die "can't migrate running VM without --online\n" if !$param->{online};
4625
4626 my $repl_conf = PVE::ReplicationConfig->new();
4627 my $is_replicated = $repl_conf->check_for_existing_jobs($vmid, 1);
4628 my $is_replicated_to_target = defined($repl_conf->find_local_replication_job($vmid, $target));
4629 if (!$param->{force} && $is_replicated && !$is_replicated_to_target) {
4630 die "Cannot live-migrate replicated VM to node '$target' - not a replication " .
4631 "target. Use 'force' to override.\n";
4632 }
4633 } else {
4634 warn "VM isn't running. Doing offline migration instead.\n" if $param->{online};
4635 $param->{online} = 0;
4636 }
4637
4638 my $storecfg = PVE::Storage::config();
4639 if (my $targetstorage = $param->{targetstorage}) {
4640 my $storagemap = eval { PVE::JSONSchema::parse_idmap($targetstorage, 'pve-storage-id') };
4641 raise_param_exc({ targetstorage => "failed to parse storage map: $@" })
4642 if $@;
4643
4644 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk'])
4645 if !defined($storagemap->{identity});
4646
4647 foreach my $target_sid (values %{$storagemap->{entries}}) {
4648 $check_storage_access_migrate->($rpcenv, $authuser, $storecfg, $target_sid, $target);
4649 }
4650
4651 $check_storage_access_migrate->($rpcenv, $authuser, $storecfg, $storagemap->{default}, $target)
4652 if $storagemap->{default};
4653
4654 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target)
4655 if $storagemap->{identity};
4656
4657 $param->{storagemap} = $storagemap;
4658 } else {
4659 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target);
4660 }
4661
4662 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
4663
4664 my $hacmd = sub {
4665 my $upid = shift;
4666
4667 print "Requesting HA migration for VM $vmid to node $target\n";
4668
4669 my $cmd = ['ha-manager', 'migrate', "vm:$vmid", $target];
4670 PVE::Tools::run_command($cmd);
4671 return;
4672 };
4673
4674 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
4675
4676 } else {
4677
4678 my $realcmd = sub {
4679 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
4680 };
4681
4682 my $worker = sub {
4683 return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd);
4684 };
4685
4686 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $worker);
4687 }
4688
4689 }});
4690
4691 __PACKAGE__->register_method({
4692 name => 'remote_migrate_vm',
4693 path => '{vmid}/remote_migrate',
4694 method => 'POST',
4695 protected => 1,
4696 proxyto => 'node',
4697 description => "Migrate virtual machine to a remote cluster. Creates a new migration task. EXPERIMENTAL feature!",
4698 permissions => {
4699 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
4700 },
4701 parameters => {
4702 additionalProperties => 0,
4703 properties => {
4704 node => get_standard_option('pve-node'),
4705 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
4706 'target-vmid' => get_standard_option('pve-vmid', { optional => 1 }),
4707 'target-endpoint' => get_standard_option('proxmox-remote', {
4708 description => "Remote target endpoint",
4709 }),
4710 online => {
4711 type => 'boolean',
4712 description => "Use online/live migration if VM is running. Ignored if VM is stopped.",
4713 optional => 1,
4714 },
4715 delete => {
4716 type => 'boolean',
4717 description => "Delete the original VM and related data after successful migration. By default the original VM is kept on the source cluster in a stopped state.",
4718 optional => 1,
4719 default => 0,
4720 },
4721 'target-storage' => get_standard_option('pve-targetstorage', {
4722 completion => \&PVE::QemuServer::complete_migration_storage,
4723 optional => 0,
4724 }),
4725 'target-bridge' => {
4726 type => 'string',
4727 description => "Mapping from source to target bridges. Providing only a single bridge ID maps all source bridges to that bridge. Providing the special value '1' will map each source bridge to itself.",
4728 format => 'bridge-pair-list',
4729 },
4730 bwlimit => {
4731 description => "Override I/O bandwidth limit (in KiB/s).",
4732 optional => 1,
4733 type => 'integer',
4734 minimum => '0',
4735 default => 'migrate limit from datacenter or storage config',
4736 },
4737 },
4738 },
4739 returns => {
4740 type => 'string',
4741 description => "the task ID.",
4742 },
4743 code => sub {
4744 my ($param) = @_;
4745
4746 my $rpcenv = PVE::RPCEnvironment::get();
4747 my $authuser = $rpcenv->get_user();
4748
4749 my $source_vmid = extract_param($param, 'vmid');
4750 my $target_endpoint = extract_param($param, 'target-endpoint');
4751 my $target_vmid = extract_param($param, 'target-vmid') // $source_vmid;
4752
4753 my $delete = extract_param($param, 'delete') // 0;
4754
4755 PVE::Cluster::check_cfs_quorum();
4756
4757 # test if VM exists
4758 my $conf = PVE::QemuConfig->load_config($source_vmid);
4759
4760 PVE::QemuConfig->check_lock($conf);
4761
4762 raise_param_exc({ vmid => "cannot migrate HA-managed VM to remote cluster" })
4763 if PVE::HA::Config::vm_is_ha_managed($source_vmid);
4764
4765 my $remote = PVE::JSONSchema::parse_property_string('proxmox-remote', $target_endpoint);
4766
4767 # TODO: move this as helper somewhere appropriate?
4768 my $conn_args = {
4769 protocol => 'https',
4770 host => $remote->{host},
4771 port => $remote->{port} // 8006,
4772 apitoken => $remote->{apitoken},
4773 };
4774
4775 my $fp;
4776 if ($fp = $remote->{fingerprint}) {
4777 $conn_args->{cached_fingerprints} = { uc($fp) => 1 };
4778 }
4779
4780 print "Establishing API connection with remote at '$remote->{host}'\n";
4781
4782 my $api_client = PVE::APIClient::LWP->new(%$conn_args);
4783
4784 if (!defined($fp)) {
4785 my $cert_info = $api_client->get("/nodes/localhost/certificates/info");
4786 foreach my $cert (@$cert_info) {
4787 my $filename = $cert->{filename};
4788 next if $filename ne 'pveproxy-ssl.pem' && $filename ne 'pve-ssl.pem';
4789 $fp = $cert->{fingerprint} if !$fp || $filename eq 'pveproxy-ssl.pem';
4790 }
4791 $conn_args->{cached_fingerprints} = { uc($fp) => 1 }
4792 if defined($fp);
4793 }
4794
4795 my $repl_conf = PVE::ReplicationConfig->new();
4796 my $is_replicated = $repl_conf->check_for_existing_jobs($source_vmid, 1);
4797 die "cannot remote-migrate replicated VM\n" if $is_replicated;
4798
4799 if (PVE::QemuServer::check_running($source_vmid)) {
4800 die "can't migrate running VM without --online\n" if !$param->{online};
4801
4802 } else {
4803 warn "VM isn't running. Doing offline migration instead.\n" if $param->{online};
4804 $param->{online} = 0;
4805 }
4806
4807 my $storecfg = PVE::Storage::config();
4808 my $target_storage = extract_param($param, 'target-storage');
4809 my $storagemap = eval { PVE::JSONSchema::parse_idmap($target_storage, 'pve-storage-id') };
4810 raise_param_exc({ 'target-storage' => "failed to parse storage map: $@" })
4811 if $@;
4812
4813 my $target_bridge = extract_param($param, 'target-bridge');
4814 my $bridgemap = eval { PVE::JSONSchema::parse_idmap($target_bridge, 'pve-bridge-id') };
4815 raise_param_exc({ 'target-bridge' => "failed to parse bridge map: $@" })
4816 if $@;
4817
4818 die "remote migration requires explicit storage mapping!\n"
4819 if $storagemap->{identity};
4820
4821 $param->{storagemap} = $storagemap;
4822 $param->{bridgemap} = $bridgemap;
4823 $param->{remote} = {
4824 conn => $conn_args, # re-use fingerprint for tunnel
4825 client => $api_client,
4826 vmid => $target_vmid,
4827 };
4828 $param->{migration_type} = 'websocket';
4829 $param->{'with-local-disks'} = 1;
4830 $param->{delete} = $delete if $delete;
4831
4832 my $cluster_status = $api_client->get("/cluster/status");
4833 my $target_node;
4834 foreach my $entry (@$cluster_status) {
4835 next if $entry->{type} ne 'node';
4836 if ($entry->{local}) {
4837 $target_node = $entry->{name};
4838 last;
4839 }
4840 }
4841
4842 die "couldn't determine endpoint's node name\n"
4843 if !defined($target_node);
4844
4845 my $realcmd = sub {
4846 PVE::QemuMigrate->migrate($target_node, $remote->{host}, $source_vmid, $param);
4847 };
4848
4849 my $worker = sub {
4850 return PVE::GuestHelpers::guest_migration_lock($source_vmid, 10, $realcmd);
4851 };
4852
4853 return $rpcenv->fork_worker('qmigrate', $source_vmid, $authuser, $worker);
4854 }});
4855
4856 __PACKAGE__->register_method({
4857 name => 'monitor',
4858 path => '{vmid}/monitor',
4859 method => 'POST',
4860 protected => 1,
4861 proxyto => 'node',
4862 description => "Execute QEMU monitor commands.",
4863 permissions => {
4864 description => "Sys.Modify is required for (sub)commands which are not read-only ('info *' and 'help')",
4865 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
4866 },
4867 parameters => {
4868 additionalProperties => 0,
4869 properties => {
4870 node => get_standard_option('pve-node'),
4871 vmid => get_standard_option('pve-vmid'),
4872 command => {
4873 type => 'string',
4874 description => "The monitor command.",
4875 }
4876 },
4877 },
4878 returns => { type => 'string'},
4879 code => sub {
4880 my ($param) = @_;
4881
4882 my $rpcenv = PVE::RPCEnvironment::get();
4883 my $authuser = $rpcenv->get_user();
4884
4885 my $is_ro = sub {
4886 my $command = shift;
4887 return $command =~ m/^\s*info(\s+|$)/
4888 || $command =~ m/^\s*help\s*$/;
4889 };
4890
4891 $rpcenv->check_full($authuser, "/", ['Sys.Modify'])
4892 if !&$is_ro($param->{command});
4893
4894 my $vmid = $param->{vmid};
4895
4896 my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
4897
4898 my $res = '';
4899 eval {
4900 $res = PVE::QemuServer::Monitor::hmp_cmd($vmid, $param->{command});
4901 };
4902 $res = "ERROR: $@" if $@;
4903
4904 return $res;
4905 }});
4906
4907 __PACKAGE__->register_method({
4908 name => 'resize_vm',
4909 path => '{vmid}/resize',
4910 method => 'PUT',
4911 protected => 1,
4912 proxyto => 'node',
4913 description => "Extend volume size.",
4914 permissions => {
4915 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
4916 },
4917 parameters => {
4918 additionalProperties => 0,
4919 properties => {
4920 node => get_standard_option('pve-node'),
4921 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
4922 skiplock => get_standard_option('skiplock'),
4923 disk => {
4924 type => 'string',
4925 description => "The disk you want to resize.",
4926 enum => [PVE::QemuServer::Drive::valid_drive_names()],
4927 },
4928 size => {
4929 type => 'string',
4930 pattern => '\+?\d+(\.\d+)?[KMGT]?',
4931 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.",
4932 },
4933 digest => {
4934 type => 'string',
4935 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
4936 maxLength => 40,
4937 optional => 1,
4938 },
4939 },
4940 },
4941 returns => {
4942 type => 'string',
4943 description => "the task ID.",
4944 },
4945 code => sub {
4946 my ($param) = @_;
4947
4948 my $rpcenv = PVE::RPCEnvironment::get();
4949
4950 my $authuser = $rpcenv->get_user();
4951
4952 my $node = extract_param($param, 'node');
4953
4954 my $vmid = extract_param($param, 'vmid');
4955
4956 my $digest = extract_param($param, 'digest');
4957
4958 my $disk = extract_param($param, 'disk');
4959
4960 my $sizestr = extract_param($param, 'size');
4961
4962 my $skiplock = extract_param($param, 'skiplock');
4963 raise_param_exc({ skiplock => "Only root may use this option." })
4964 if $skiplock && $authuser ne 'root@pam';
4965
4966 my $storecfg = PVE::Storage::config();
4967
4968 my $updatefn = sub {
4969
4970 my $conf = PVE::QemuConfig->load_config($vmid);
4971
4972 die "checksum missmatch (file change by other user?)\n"
4973 if $digest && $digest ne $conf->{digest};
4974 PVE::QemuConfig->check_lock($conf) if !$skiplock;
4975
4976 die "disk '$disk' does not exist\n" if !$conf->{$disk};
4977
4978 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
4979
4980 my (undef, undef, undef, undef, undef, undef, $format) =
4981 PVE::Storage::parse_volname($storecfg, $drive->{file});
4982
4983 my $volid = $drive->{file};
4984
4985 die "disk '$disk' has no associated volume\n" if !$volid;
4986
4987 die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
4988
4989 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
4990
4991 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
4992
4993 PVE::Storage::activate_volumes($storecfg, [$volid]);
4994 my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
4995
4996 die "Could not determine current size of volume '$volid'\n" if !defined($size);
4997
4998 die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
4999 my ($ext, $newsize, $unit) = ($1, $2, $4);
5000 if ($unit) {
5001 if ($unit eq 'K') {
5002 $newsize = $newsize * 1024;
5003 } elsif ($unit eq 'M') {
5004 $newsize = $newsize * 1024 * 1024;
5005 } elsif ($unit eq 'G') {
5006 $newsize = $newsize * 1024 * 1024 * 1024;
5007 } elsif ($unit eq 'T') {
5008 $newsize = $newsize * 1024 * 1024 * 1024 * 1024;
5009 }
5010 }
5011 $newsize += $size if $ext;
5012 $newsize = int($newsize);
5013
5014 die "shrinking disks is not supported\n" if $newsize < $size;
5015
5016 return if $size == $newsize;
5017
5018 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr");
5019
5020 PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize);
5021
5022 $drive->{size} = $newsize;
5023 $conf->{$disk} = PVE::QemuServer::print_drive($drive);
5024
5025 PVE::QemuConfig->write_config($vmid, $conf);
5026 };
5027
5028 my $worker = sub {
5029 PVE::QemuConfig->lock_config($vmid, $updatefn);
5030 };
5031
5032 return $rpcenv->fork_worker('resize', $vmid, $authuser, $worker);
5033 }});
5034
5035 __PACKAGE__->register_method({
5036 name => 'snapshot_list',
5037 path => '{vmid}/snapshot',
5038 method => 'GET',
5039 description => "List all snapshots.",
5040 permissions => {
5041 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
5042 },
5043 proxyto => 'node',
5044 protected => 1, # qemu pid files are only readable by root
5045 parameters => {
5046 additionalProperties => 0,
5047 properties => {
5048 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
5049 node => get_standard_option('pve-node'),
5050 },
5051 },
5052 returns => {
5053 type => 'array',
5054 items => {
5055 type => "object",
5056 properties => {
5057 name => {
5058 description => "Snapshot identifier. Value 'current' identifies the current VM.",
5059 type => 'string',
5060 },
5061 vmstate => {
5062 description => "Snapshot includes RAM.",
5063 type => 'boolean',
5064 optional => 1,
5065 },
5066 description => {
5067 description => "Snapshot description.",
5068 type => 'string',
5069 },
5070 snaptime => {
5071 description => "Snapshot creation time",
5072 type => 'integer',
5073 renderer => 'timestamp',
5074 optional => 1,
5075 },
5076 parent => {
5077 description => "Parent snapshot identifier.",
5078 type => 'string',
5079 optional => 1,
5080 },
5081 },
5082 },
5083 links => [ { rel => 'child', href => "{name}" } ],
5084 },
5085 code => sub {
5086 my ($param) = @_;
5087
5088 my $vmid = $param->{vmid};
5089
5090 my $conf = PVE::QemuConfig->load_config($vmid);
5091 my $snaphash = $conf->{snapshots} || {};
5092
5093 my $res = [];
5094
5095 foreach my $name (keys %$snaphash) {
5096 my $d = $snaphash->{$name};
5097 my $item = {
5098 name => $name,
5099 snaptime => $d->{snaptime} || 0,
5100 vmstate => $d->{vmstate} ? 1 : 0,
5101 description => $d->{description} || '',
5102 };
5103 $item->{parent} = $d->{parent} if $d->{parent};
5104 $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
5105 push @$res, $item;
5106 }
5107
5108 my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
5109 my $current = {
5110 name => 'current',
5111 digest => $conf->{digest},
5112 running => $running,
5113 description => "You are here!",
5114 };
5115 $current->{parent} = $conf->{parent} if $conf->{parent};
5116
5117 push @$res, $current;
5118
5119 return $res;
5120 }});
5121
5122 __PACKAGE__->register_method({
5123 name => 'snapshot',
5124 path => '{vmid}/snapshot',
5125 method => 'POST',
5126 protected => 1,
5127 proxyto => 'node',
5128 description => "Snapshot a VM.",
5129 permissions => {
5130 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
5131 },
5132 parameters => {
5133 additionalProperties => 0,
5134 properties => {
5135 node => get_standard_option('pve-node'),
5136 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
5137 snapname => get_standard_option('pve-snapshot-name'),
5138 vmstate => {
5139 optional => 1,
5140 type => 'boolean',
5141 description => "Save the vmstate",
5142 },
5143 description => {
5144 optional => 1,
5145 type => 'string',
5146 description => "A textual description or comment.",
5147 },
5148 },
5149 },
5150 returns => {
5151 type => 'string',
5152 description => "the task ID.",
5153 },
5154 code => sub {
5155 my ($param) = @_;
5156
5157 my $rpcenv = PVE::RPCEnvironment::get();
5158
5159 my $authuser = $rpcenv->get_user();
5160
5161 my $node = extract_param($param, 'node');
5162
5163 my $vmid = extract_param($param, 'vmid');
5164
5165 my $snapname = extract_param($param, 'snapname');
5166
5167 die "unable to use snapshot name 'current' (reserved name)\n"
5168 if $snapname eq 'current';
5169
5170 die "unable to use snapshot name 'pending' (reserved name)\n"
5171 if lc($snapname) eq 'pending';
5172
5173 my $realcmd = sub {
5174 PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
5175 PVE::QemuConfig->snapshot_create($vmid, $snapname, $param->{vmstate},
5176 $param->{description});
5177 };
5178
5179 return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd);
5180 }});
5181
5182 __PACKAGE__->register_method({
5183 name => 'snapshot_cmd_idx',
5184 path => '{vmid}/snapshot/{snapname}',
5185 description => '',
5186 method => 'GET',
5187 permissions => {
5188 user => 'all',
5189 },
5190 parameters => {
5191 additionalProperties => 0,
5192 properties => {
5193 vmid => get_standard_option('pve-vmid'),
5194 node => get_standard_option('pve-node'),
5195 snapname => get_standard_option('pve-snapshot-name'),
5196 },
5197 },
5198 returns => {
5199 type => 'array',
5200 items => {
5201 type => "object",
5202 properties => {},
5203 },
5204 links => [ { rel => 'child', href => "{cmd}" } ],
5205 },
5206 code => sub {
5207 my ($param) = @_;
5208
5209 my $res = [];
5210
5211 push @$res, { cmd => 'rollback' };
5212 push @$res, { cmd => 'config' };
5213
5214 return $res;
5215 }});
5216
5217 __PACKAGE__->register_method({
5218 name => 'update_snapshot_config',
5219 path => '{vmid}/snapshot/{snapname}/config',
5220 method => 'PUT',
5221 protected => 1,
5222 proxyto => 'node',
5223 description => "Update snapshot metadata.",
5224 permissions => {
5225 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
5226 },
5227 parameters => {
5228 additionalProperties => 0,
5229 properties => {
5230 node => get_standard_option('pve-node'),
5231 vmid => get_standard_option('pve-vmid'),
5232 snapname => get_standard_option('pve-snapshot-name'),
5233 description => {
5234 optional => 1,
5235 type => 'string',
5236 description => "A textual description or comment.",
5237 },
5238 },
5239 },
5240 returns => { type => 'null' },
5241 code => sub {
5242 my ($param) = @_;
5243
5244 my $rpcenv = PVE::RPCEnvironment::get();
5245
5246 my $authuser = $rpcenv->get_user();
5247
5248 my $vmid = extract_param($param, 'vmid');
5249
5250 my $snapname = extract_param($param, 'snapname');
5251
5252 return if !defined($param->{description});
5253
5254 my $updatefn = sub {
5255
5256 my $conf = PVE::QemuConfig->load_config($vmid);
5257
5258 PVE::QemuConfig->check_lock($conf);
5259
5260 my $snap = $conf->{snapshots}->{$snapname};
5261
5262 die "snapshot '$snapname' does not exist\n" if !defined($snap);
5263
5264 $snap->{description} = $param->{description} if defined($param->{description});
5265
5266 PVE::QemuConfig->write_config($vmid, $conf);
5267 };
5268
5269 PVE::QemuConfig->lock_config($vmid, $updatefn);
5270
5271 return;
5272 }});
5273
5274 __PACKAGE__->register_method({
5275 name => 'get_snapshot_config',
5276 path => '{vmid}/snapshot/{snapname}/config',
5277 method => 'GET',
5278 proxyto => 'node',
5279 description => "Get snapshot configuration",
5280 permissions => {
5281 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot', 'VM.Snapshot.Rollback', 'VM.Audit' ], any => 1],
5282 },
5283 parameters => {
5284 additionalProperties => 0,
5285 properties => {
5286 node => get_standard_option('pve-node'),
5287 vmid => get_standard_option('pve-vmid'),
5288 snapname => get_standard_option('pve-snapshot-name'),
5289 },
5290 },
5291 returns => { type => "object" },
5292 code => sub {
5293 my ($param) = @_;
5294
5295 my $rpcenv = PVE::RPCEnvironment::get();
5296
5297 my $authuser = $rpcenv->get_user();
5298
5299 my $vmid = extract_param($param, 'vmid');
5300
5301 my $snapname = extract_param($param, 'snapname');
5302
5303 my $conf = PVE::QemuConfig->load_config($vmid);
5304
5305 my $snap = $conf->{snapshots}->{$snapname};
5306
5307 die "snapshot '$snapname' does not exist\n" if !defined($snap);
5308
5309 return $snap;
5310 }});
5311
5312 __PACKAGE__->register_method({
5313 name => 'rollback',
5314 path => '{vmid}/snapshot/{snapname}/rollback',
5315 method => 'POST',
5316 protected => 1,
5317 proxyto => 'node',
5318 description => "Rollback VM state to specified snapshot.",
5319 permissions => {
5320 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot', 'VM.Snapshot.Rollback' ], any => 1],
5321 },
5322 parameters => {
5323 additionalProperties => 0,
5324 properties => {
5325 node => get_standard_option('pve-node'),
5326 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
5327 snapname => get_standard_option('pve-snapshot-name'),
5328 start => {
5329 type => 'boolean',
5330 description => "Whether the VM should get started after rolling back successfully."
5331 . " (Note: VMs will be automatically started if the snapshot includes RAM.)",
5332 optional => 1,
5333 default => 0,
5334 },
5335 },
5336 },
5337 returns => {
5338 type => 'string',
5339 description => "the task ID.",
5340 },
5341 code => sub {
5342 my ($param) = @_;
5343
5344 my $rpcenv = PVE::RPCEnvironment::get();
5345
5346 my $authuser = $rpcenv->get_user();
5347
5348 my $node = extract_param($param, 'node');
5349
5350 my $vmid = extract_param($param, 'vmid');
5351
5352 my $snapname = extract_param($param, 'snapname');
5353
5354 my $realcmd = sub {
5355 PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname");
5356 PVE::QemuConfig->snapshot_rollback($vmid, $snapname);
5357
5358 if ($param->{start} && !PVE::QemuServer::Helpers::vm_running_locally($vmid)) {
5359 PVE::API2::Qemu->vm_start({ vmid => $vmid, node => $node });
5360 }
5361 };
5362
5363 my $worker = sub {
5364 # hold migration lock, this makes sure that nobody create replication snapshots
5365 return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd);
5366 };
5367
5368 return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $worker);
5369 }});
5370
5371 __PACKAGE__->register_method({
5372 name => 'delsnapshot',
5373 path => '{vmid}/snapshot/{snapname}',
5374 method => 'DELETE',
5375 protected => 1,
5376 proxyto => 'node',
5377 description => "Delete a VM snapshot.",
5378 permissions => {
5379 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
5380 },
5381 parameters => {
5382 additionalProperties => 0,
5383 properties => {
5384 node => get_standard_option('pve-node'),
5385 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
5386 snapname => get_standard_option('pve-snapshot-name'),
5387 force => {
5388 optional => 1,
5389 type => 'boolean',
5390 description => "For removal from config file, even if removing disk snapshots fails.",
5391 },
5392 },
5393 },
5394 returns => {
5395 type => 'string',
5396 description => "the task ID.",
5397 },
5398 code => sub {
5399 my ($param) = @_;
5400
5401 my $rpcenv = PVE::RPCEnvironment::get();
5402
5403 my $authuser = $rpcenv->get_user();
5404
5405 my $node = extract_param($param, 'node');
5406
5407 my $vmid = extract_param($param, 'vmid');
5408
5409 my $snapname = extract_param($param, 'snapname');
5410
5411 my $lock_obtained;
5412 my $do_delete = sub {
5413 $lock_obtained = 1;
5414 PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
5415 PVE::QemuConfig->snapshot_delete($vmid, $snapname, $param->{force});
5416 };
5417
5418 my $realcmd = sub {
5419 if ($param->{force}) {
5420 $do_delete->();
5421 } else {
5422 eval { PVE::GuestHelpers::guest_migration_lock($vmid, 10, $do_delete); };
5423 if (my $err = $@) {
5424 die $err if $lock_obtained;
5425 die "Failed to obtain guest migration lock - replication running?\n";
5426 }
5427 }
5428 };
5429
5430 return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);
5431 }});
5432
5433 __PACKAGE__->register_method({
5434 name => 'template',
5435 path => '{vmid}/template',
5436 method => 'POST',
5437 protected => 1,
5438 proxyto => 'node',
5439 description => "Create a Template.",
5440 permissions => {
5441 description => "You need 'VM.Allocate' permissions on /vms/{vmid}",
5442 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
5443 },
5444 parameters => {
5445 additionalProperties => 0,
5446 properties => {
5447 node => get_standard_option('pve-node'),
5448 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }),
5449 disk => {
5450 optional => 1,
5451 type => 'string',
5452 description => "If you want to convert only 1 disk to base image.",
5453 enum => [PVE::QemuServer::Drive::valid_drive_names()],
5454 },
5455
5456 },
5457 },
5458 returns => {
5459 type => 'string',
5460 description => "the task ID.",
5461 },
5462 code => sub {
5463 my ($param) = @_;
5464
5465 my $rpcenv = PVE::RPCEnvironment::get();
5466
5467 my $authuser = $rpcenv->get_user();
5468
5469 my $node = extract_param($param, 'node');
5470
5471 my $vmid = extract_param($param, 'vmid');
5472
5473 my $disk = extract_param($param, 'disk');
5474
5475 my $load_and_check = sub {
5476 my $conf = PVE::QemuConfig->load_config($vmid);
5477
5478 PVE::QemuConfig->check_lock($conf);
5479
5480 die "unable to create template, because VM contains snapshots\n"
5481 if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}});
5482
5483 die "you can't convert a template to a template\n"
5484 if PVE::QemuConfig->is_template($conf) && !$disk;
5485
5486 die "you can't convert a VM to template if VM is running\n"
5487 if PVE::QemuServer::check_running($vmid);
5488
5489 return $conf;
5490 };
5491
5492 $load_and_check->();
5493
5494 my $realcmd = sub {
5495 PVE::QemuConfig->lock_config($vmid, sub {
5496 my $conf = $load_and_check->();
5497
5498 $conf->{template} = 1;
5499 PVE::QemuConfig->write_config($vmid, $conf);
5500
5501 PVE::QemuServer::template_create($vmid, $conf, $disk);
5502 });
5503 };
5504
5505 return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
5506 }});
5507
5508 __PACKAGE__->register_method({
5509 name => 'cloudinit_generated_config_dump',
5510 path => '{vmid}/cloudinit/dump',
5511 method => 'GET',
5512 proxyto => 'node',
5513 description => "Get automatically generated cloudinit config.",
5514 permissions => {
5515 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
5516 },
5517 parameters => {
5518 additionalProperties => 0,
5519 properties => {
5520 node => get_standard_option('pve-node'),
5521 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
5522 type => {
5523 description => 'Config type.',
5524 type => 'string',
5525 enum => ['user', 'network', 'meta'],
5526 },
5527 },
5528 },
5529 returns => {
5530 type => 'string',
5531 },
5532 code => sub {
5533 my ($param) = @_;
5534
5535 my $conf = PVE::QemuConfig->load_config($param->{vmid});
5536
5537 return PVE::QemuServer::Cloudinit::dump_cloudinit_config($conf, $param->{vmid}, $param->{type});
5538 }});
5539
5540 __PACKAGE__->register_method({
5541 name => 'mtunnel',
5542 path => '{vmid}/mtunnel',
5543 method => 'POST',
5544 protected => 1,
5545 description => 'Migration tunnel endpoint - only for internal use by VM migration.',
5546 permissions => {
5547 check =>
5548 [ 'and',
5549 ['perm', '/vms/{vmid}', [ 'VM.Allocate' ]],
5550 ['perm', '/', [ 'Sys.Incoming' ]],
5551 ],
5552 description => "You need 'VM.Allocate' permissions on '/vms/{vmid}' and Sys.Incoming" .
5553 " on '/'. Further permission checks happen during the actual migration.",
5554 },
5555 parameters => {
5556 additionalProperties => 0,
5557 properties => {
5558 node => get_standard_option('pve-node'),
5559 vmid => get_standard_option('pve-vmid'),
5560 storages => {
5561 type => 'string',
5562 format => 'pve-storage-id-list',
5563 optional => 1,
5564 description => 'List of storages to check permission and availability. Will be checked again for all actually used storages during migration.',
5565 },
5566 bridges => {
5567 type => 'string',
5568 format => 'pve-bridge-id-list',
5569 optional => 1,
5570 description => 'List of network bridges to check availability. Will be checked again for actually used bridges during migration.',
5571 },
5572 },
5573 },
5574 returns => {
5575 additionalProperties => 0,
5576 properties => {
5577 upid => { type => 'string' },
5578 ticket => { type => 'string' },
5579 socket => { type => 'string' },
5580 },
5581 },
5582 code => sub {
5583 my ($param) = @_;
5584
5585 my $rpcenv = PVE::RPCEnvironment::get();
5586 my $authuser = $rpcenv->get_user();
5587
5588 my $node = extract_param($param, 'node');
5589 my $vmid = extract_param($param, 'vmid');
5590
5591 my $storages = extract_param($param, 'storages');
5592 my $bridges = extract_param($param, 'bridges');
5593
5594 my $nodename = PVE::INotify::nodename();
5595
5596 raise_param_exc({ node => "node needs to be 'localhost' or local hostname '$nodename'" })
5597 if $node ne 'localhost' && $node ne $nodename;
5598
5599 $node = $nodename;
5600
5601 my $storecfg = PVE::Storage::config();
5602 foreach my $storeid (PVE::Tools::split_list($storages)) {
5603 $check_storage_access_migrate->($rpcenv, $authuser, $storecfg, $storeid, $node);
5604 }
5605
5606 foreach my $bridge (PVE::Tools::split_list($bridges)) {
5607 PVE::Network::read_bridge_mtu($bridge);
5608 }
5609
5610 PVE::Cluster::check_cfs_quorum();
5611
5612 my $lock = 'create';
5613 eval { PVE::QemuConfig->create_and_lock_config($vmid, 0, $lock); };
5614
5615 raise_param_exc({ vmid => "unable to create empty VM config - $@"})
5616 if $@;
5617
5618 my $realcmd = sub {
5619 my $state = {
5620 storecfg => PVE::Storage::config(),
5621 lock => $lock,
5622 vmid => $vmid,
5623 };
5624
5625 my $run_locked = sub {
5626 my ($code, $params) = @_;
5627 return PVE::QemuConfig->lock_config($state->{vmid}, sub {
5628 my $conf = PVE::QemuConfig->load_config($state->{vmid});
5629
5630 $state->{conf} = $conf;
5631
5632 die "Encountered wrong lock - aborting mtunnel command handling.\n"
5633 if $state->{lock} && !PVE::QemuConfig->has_lock($conf, $state->{lock});
5634
5635 return $code->($params);
5636 });
5637 };
5638
5639 my $cmd_desc = {
5640 config => {
5641 conf => {
5642 type => 'string',
5643 description => 'Full VM config, adapted for target cluster/node',
5644 },
5645 'firewall-config' => {
5646 type => 'string',
5647 description => 'VM firewall config',
5648 optional => 1,
5649 },
5650 },
5651 disk => {
5652 format => PVE::JSONSchema::get_standard_option('pve-qm-image-format'),
5653 storage => {
5654 type => 'string',
5655 format => 'pve-storage-id',
5656 },
5657 drive => {
5658 type => 'object',
5659 description => 'parsed drive information without volid and format',
5660 },
5661 },
5662 start => {
5663 start_params => {
5664 type => 'object',
5665 description => 'params passed to vm_start_nolock',
5666 },
5667 migrate_opts => {
5668 type => 'object',
5669 description => 'migrate_opts passed to vm_start_nolock',
5670 },
5671 },
5672 ticket => {
5673 path => {
5674 type => 'string',
5675 description => 'socket path for which the ticket should be valid. must be known to current mtunnel instance.',
5676 },
5677 },
5678 quit => {
5679 cleanup => {
5680 type => 'boolean',
5681 description => 'remove VM config and disks, aborting migration',
5682 default => 0,
5683 },
5684 },
5685 'disk-import' => $PVE::StorageTunnel::cmd_schema->{'disk-import'},
5686 'query-disk-import' => $PVE::StorageTunnel::cmd_schema->{'query-disk-import'},
5687 bwlimit => $PVE::StorageTunnel::cmd_schema->{bwlimit},
5688 };
5689
5690 my $cmd_handlers = {
5691 'version' => sub {
5692 # compared against other end's version
5693 # bump/reset for breaking changes
5694 # bump/bump for opt-in changes
5695 return {
5696 api => $PVE::QemuMigrate::WS_TUNNEL_VERSION,
5697 age => 0,
5698 };
5699 },
5700 'config' => sub {
5701 my ($params) = @_;
5702
5703 # parse and write out VM FW config if given
5704 if (my $fw_conf = $params->{'firewall-config'}) {
5705 my ($path, $fh) = PVE::Tools::tempfile_contents($fw_conf, 700);
5706
5707 my $empty_conf = {
5708 rules => [],
5709 options => {},
5710 aliases => {},
5711 ipset => {} ,
5712 ipset_comments => {},
5713 };
5714 my $cluster_fw_conf = PVE::Firewall::load_clusterfw_conf();
5715
5716 # TODO: add flag for strict parsing?
5717 # TODO: add import sub that does all this given raw content?
5718 my $vmfw_conf = PVE::Firewall::generic_fw_config_parser($path, $cluster_fw_conf, $empty_conf, 'vm');
5719 $vmfw_conf->{vmid} = $state->{vmid};
5720 PVE::Firewall::save_vmfw_conf($state->{vmid}, $vmfw_conf);
5721
5722 $state->{cleanup}->{fw} = 1;
5723 }
5724
5725 my $conf_fn = "incoming/qemu-server/$state->{vmid}.conf";
5726 my $new_conf = PVE::QemuServer::parse_vm_config($conf_fn, $params->{conf}, 1);
5727 delete $new_conf->{lock};
5728 delete $new_conf->{digest};
5729
5730 # TODO handle properly?
5731 delete $new_conf->{snapshots};
5732 delete $new_conf->{parent};
5733 delete $new_conf->{pending};
5734
5735 # not handled by update_vm_api
5736 my $vmgenid = delete $new_conf->{vmgenid};
5737 my $meta = delete $new_conf->{meta};
5738 my $cloudinit = delete $new_conf->{cloudinit}; # this is informational only
5739 $new_conf->{skip_cloud_init} = 1; # re-use image from source side
5740
5741 $new_conf->{vmid} = $state->{vmid};
5742 $new_conf->{node} = $node;
5743
5744 PVE::QemuConfig->remove_lock($state->{vmid}, 'create');
5745
5746 eval {
5747 $update_vm_api->($new_conf, 1);
5748 };
5749 if (my $err = $@) {
5750 # revert to locked previous config
5751 my $conf = PVE::QemuConfig->load_config($state->{vmid});
5752 $conf->{lock} = 'create';
5753 PVE::QemuConfig->write_config($state->{vmid}, $conf);
5754
5755 die $err;
5756 }
5757
5758 my $conf = PVE::QemuConfig->load_config($state->{vmid});
5759 $conf->{lock} = 'migrate';
5760 $conf->{vmgenid} = $vmgenid if defined($vmgenid);
5761 $conf->{meta} = $meta if defined($meta);
5762 $conf->{cloudinit} = $cloudinit if defined($cloudinit);
5763 PVE::QemuConfig->write_config($state->{vmid}, $conf);
5764
5765 $state->{lock} = 'migrate';
5766
5767 return;
5768 },
5769 'bwlimit' => sub {
5770 my ($params) = @_;
5771 return PVE::StorageTunnel::handle_bwlimit($params);
5772 },
5773 'disk' => sub {
5774 my ($params) = @_;
5775
5776 my $format = $params->{format};
5777 my $storeid = $params->{storage};
5778 my $drive = $params->{drive};
5779
5780 $check_storage_access_migrate->($rpcenv, $authuser, $state->{storecfg}, $storeid, $node);
5781
5782 my $storagemap = {
5783 default => $storeid,
5784 };
5785
5786 my $source_volumes = {
5787 'disk' => [
5788 undef,
5789 $storeid,
5790 $drive,
5791 0,
5792 $format,
5793 ],
5794 };
5795
5796 my $res = PVE::QemuServer::vm_migrate_alloc_nbd_disks($state->{storecfg}, $state->{vmid}, $source_volumes, $storagemap);
5797 if (defined($res->{disk})) {
5798 $state->{cleanup}->{volumes}->{$res->{disk}->{volid}} = 1;
5799 return $res->{disk};
5800 } else {
5801 die "failed to allocate NBD disk..\n";
5802 }
5803 },
5804 'disk-import' => sub {
5805 my ($params) = @_;
5806
5807 $check_storage_access_migrate->(
5808 $rpcenv,
5809 $authuser,
5810 $state->{storecfg},
5811 $params->{storage},
5812 $node
5813 );
5814
5815 $params->{unix} = "/run/qemu-server/$state->{vmid}.storage";
5816
5817 return PVE::StorageTunnel::handle_disk_import($state, $params);
5818 },
5819 'query-disk-import' => sub {
5820 my ($params) = @_;
5821
5822 return PVE::StorageTunnel::handle_query_disk_import($state, $params);
5823 },
5824 'start' => sub {
5825 my ($params) = @_;
5826
5827 my $info = PVE::QemuServer::vm_start_nolock(
5828 $state->{storecfg},
5829 $state->{vmid},
5830 $state->{conf},
5831 $params->{start_params},
5832 $params->{migrate_opts},
5833 );
5834
5835
5836 if ($info->{migrate}->{proto} ne 'unix') {
5837 PVE::QemuServer::vm_stop(undef, $state->{vmid}, 1, 1);
5838 die "migration over non-UNIX sockets not possible\n";
5839 }
5840
5841 my $socket = $info->{migrate}->{addr};
5842 chown $state->{socket_uid}, -1, $socket;
5843 $state->{sockets}->{$socket} = 1;
5844
5845 my $unix_sockets = $info->{migrate}->{unix_sockets};
5846 foreach my $socket (@$unix_sockets) {
5847 chown $state->{socket_uid}, -1, $socket;
5848 $state->{sockets}->{$socket} = 1;
5849 }
5850 return $info;
5851 },
5852 'fstrim' => sub {
5853 if (PVE::QemuServer::qga_check_running($state->{vmid})) {
5854 eval { mon_cmd($state->{vmid}, "guest-fstrim") };
5855 warn "fstrim failed: $@\n" if $@;
5856 }
5857 return;
5858 },
5859 'stop' => sub {
5860 PVE::QemuServer::vm_stop(undef, $state->{vmid}, 1, 1);
5861 return;
5862 },
5863 'nbdstop' => sub {
5864 PVE::QemuServer::nbd_stop($state->{vmid});
5865 return;
5866 },
5867 'resume' => sub {
5868 if (PVE::QemuServer::Helpers::vm_running_locally($state->{vmid})) {
5869 PVE::QemuServer::vm_resume($state->{vmid}, 1, 1);
5870 } else {
5871 die "VM $state->{vmid} not running\n";
5872 }
5873 return;
5874 },
5875 'unlock' => sub {
5876 PVE::QemuConfig->remove_lock($state->{vmid}, $state->{lock});
5877 delete $state->{lock};
5878 return;
5879 },
5880 'ticket' => sub {
5881 my ($params) = @_;
5882
5883 my $path = $params->{path};
5884
5885 die "Not allowed to generate ticket for unknown socket '$path'\n"
5886 if !defined($state->{sockets}->{$path});
5887
5888 return { ticket => PVE::AccessControl::assemble_tunnel_ticket($authuser, "/socket/$path") };
5889 },
5890 'quit' => sub {
5891 my ($params) = @_;
5892
5893 if ($params->{cleanup}) {
5894 if ($state->{cleanup}->{fw}) {
5895 PVE::Firewall::remove_vmfw_conf($state->{vmid});
5896 }
5897
5898 for my $volid (keys $state->{cleanup}->{volumes}->%*) {
5899 print "freeing volume '$volid' as part of cleanup\n";
5900 eval { PVE::Storage::vdisk_free($state->{storecfg}, $volid) };
5901 warn $@ if $@;
5902 }
5903
5904 PVE::QemuServer::destroy_vm($state->{storecfg}, $state->{vmid}, 1);
5905 }
5906
5907 print "switching to exit-mode, waiting for client to disconnect\n";
5908 $state->{exit} = 1;
5909 return;
5910 },
5911 };
5912
5913 $run_locked->(sub {
5914 my $socket_addr = "/run/qemu-server/$state->{vmid}.mtunnel";
5915 unlink $socket_addr;
5916
5917 $state->{socket} = IO::Socket::UNIX->new(
5918 Type => SOCK_STREAM(),
5919 Local => $socket_addr,
5920 Listen => 1,
5921 );
5922
5923 $state->{socket_uid} = getpwnam('www-data')
5924 or die "Failed to resolve user 'www-data' to numeric UID\n";
5925 chown $state->{socket_uid}, -1, $socket_addr;
5926 });
5927
5928 print "mtunnel started\n";
5929
5930 my $conn = eval { PVE::Tools::run_with_timeout(300, sub { $state->{socket}->accept() }) };
5931 if ($@) {
5932 warn "Failed to accept tunnel connection - $@\n";
5933
5934 warn "Removing tunnel socket..\n";
5935 unlink $state->{socket};
5936
5937 warn "Removing temporary VM config..\n";
5938 $run_locked->(sub {
5939 PVE::QemuServer::destroy_vm($state->{storecfg}, $state->{vmid}, 1);
5940 });
5941
5942 die "Exiting mtunnel\n";
5943 }
5944
5945 $state->{conn} = $conn;
5946
5947 my $reply_err = sub {
5948 my ($msg) = @_;
5949
5950 my $reply = JSON::encode_json({
5951 success => JSON::false,
5952 msg => $msg,
5953 });
5954 $conn->print("$reply\n");
5955 $conn->flush();
5956 };
5957
5958 my $reply_ok = sub {
5959 my ($res) = @_;
5960
5961 $res->{success} = JSON::true;
5962 my $reply = JSON::encode_json($res);
5963 $conn->print("$reply\n");
5964 $conn->flush();
5965 };
5966
5967 while (my $line = <$conn>) {
5968 chomp $line;
5969
5970 # untaint, we validate below if needed
5971 ($line) = $line =~ /^(.*)$/;
5972 my $parsed = eval { JSON::decode_json($line) };
5973 if ($@) {
5974 $reply_err->("failed to parse command - $@");
5975 next;
5976 }
5977
5978 my $cmd = delete $parsed->{cmd};
5979 if (!defined($cmd)) {
5980 $reply_err->("'cmd' missing");
5981 } elsif ($state->{exit}) {
5982 $reply_err->("tunnel is in exit-mode, processing '$cmd' cmd not possible");
5983 next;
5984 } elsif (my $handler = $cmd_handlers->{$cmd}) {
5985 print "received command '$cmd'\n";
5986 eval {
5987 if ($cmd_desc->{$cmd}) {
5988 PVE::JSONSchema::validate($parsed, $cmd_desc->{$cmd});
5989 } else {
5990 $parsed = {};
5991 }
5992 my $res = $run_locked->($handler, $parsed);
5993 $reply_ok->($res);
5994 };
5995 $reply_err->("failed to handle '$cmd' command - $@")
5996 if $@;
5997 } else {
5998 $reply_err->("unknown command '$cmd' given");
5999 }
6000 }
6001
6002 if ($state->{exit}) {
6003 print "mtunnel exited\n";
6004 } else {
6005 die "mtunnel exited unexpectedly\n";
6006 }
6007 };
6008
6009 my $socket_addr = "/run/qemu-server/$vmid.mtunnel";
6010 my $ticket = PVE::AccessControl::assemble_tunnel_ticket($authuser, "/socket/$socket_addr");
6011 my $upid = $rpcenv->fork_worker('qmtunnel', $vmid, $authuser, $realcmd);
6012
6013 return {
6014 ticket => $ticket,
6015 upid => $upid,
6016 socket => $socket_addr,
6017 };
6018 }});
6019
6020 __PACKAGE__->register_method({
6021 name => 'mtunnelwebsocket',
6022 path => '{vmid}/mtunnelwebsocket',
6023 method => 'GET',
6024 permissions => {
6025 description => "You need to pass a ticket valid for the selected socket. Tickets can be created via the mtunnel API call, which will check permissions accordingly.",
6026 user => 'all', # check inside
6027 },
6028 description => 'Migration tunnel endpoint for websocket upgrade - only for internal use by VM migration.',
6029 parameters => {
6030 additionalProperties => 0,
6031 properties => {
6032 node => get_standard_option('pve-node'),
6033 vmid => get_standard_option('pve-vmid'),
6034 socket => {
6035 type => "string",
6036 description => "unix socket to forward to",
6037 },
6038 ticket => {
6039 type => "string",
6040 description => "ticket return by initial 'mtunnel' API call, or retrieved via 'ticket' tunnel command",
6041 },
6042 },
6043 },
6044 returns => {
6045 type => "object",
6046 properties => {
6047 port => { type => 'string', optional => 1 },
6048 socket => { type => 'string', optional => 1 },
6049 },
6050 },
6051 code => sub {
6052 my ($param) = @_;
6053
6054 my $rpcenv = PVE::RPCEnvironment::get();
6055 my $authuser = $rpcenv->get_user();
6056
6057 my $nodename = PVE::INotify::nodename();
6058 my $node = extract_param($param, 'node');
6059
6060 raise_param_exc({ node => "node needs to be 'localhost' or local hostname '$nodename'" })
6061 if $node ne 'localhost' && $node ne $nodename;
6062
6063 my $vmid = $param->{vmid};
6064 # check VM exists
6065 PVE::QemuConfig->load_config($vmid);
6066
6067 my $socket = $param->{socket};
6068 PVE::AccessControl::verify_tunnel_ticket($param->{ticket}, $authuser, "/socket/$socket");
6069
6070 return { socket => $socket };
6071 }});
6072
6073 1;