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