]> git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu.pm
bump version to 8.2.1
[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 my $conf = PVE::QemuConfig->load_config($param->{vmid});
869
870 if (my $snapname = $param->{snapshot}) {
871 my $snapshot = $conf->{snapshots}->{$snapname};
872 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
873
874 $snapshot->{digest} = $conf->{digest}; # keep file digest for API
875
876 $conf = $snapshot;
877 }
878
879 delete $conf->{snapshots};
880
881 if (!$param->{current}) {
882 foreach my $opt (keys %{$conf->{pending}}) {
883 next if $opt eq 'delete';
884 my $value = $conf->{pending}->{$opt};
885 next if ref($value); # just to be sure
886 $conf->{$opt} = $value;
887 }
888 my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete});
889 foreach my $opt (keys %$pending_delete_hash) {
890 delete $conf->{$opt} if $conf->{$opt};
891 }
892 }
893
894 delete $conf->{pending};
895
896 # hide cloudinit password
897 if ($conf->{cipassword}) {
898 $conf->{cipassword} = '**********';
899 }
900
901 return $conf;
902 }});
903
904 __PACKAGE__->register_method({
905 name => 'vm_pending',
906 path => '{vmid}/pending',
907 method => 'GET',
908 proxyto => 'node',
909 description => "Get virtual machine configuration, including pending changes.",
910 permissions => {
911 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
912 },
913 parameters => {
914 additionalProperties => 0,
915 properties => {
916 node => get_standard_option('pve-node'),
917 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
918 },
919 },
920 returns => {
921 type => "array",
922 items => {
923 type => "object",
924 properties => {
925 key => {
926 description => "Configuration option name.",
927 type => 'string',
928 },
929 value => {
930 description => "Current value.",
931 type => 'string',
932 optional => 1,
933 },
934 pending => {
935 description => "Pending value.",
936 type => 'string',
937 optional => 1,
938 },
939 delete => {
940 description => "Indicates a pending delete request if present and not 0. " .
941 "The value 2 indicates a force-delete request.",
942 type => 'integer',
943 minimum => 0,
944 maximum => 2,
945 optional => 1,
946 },
947 },
948 },
949 },
950 code => sub {
951 my ($param) = @_;
952
953 my $conf = PVE::QemuConfig->load_config($param->{vmid});
954
955 my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete});
956
957 my $res = [];
958
959 foreach my $opt (keys %$conf) {
960 next if ref($conf->{$opt});
961 my $item = { key => $opt };
962 $item->{value} = $conf->{$opt} if defined($conf->{$opt});
963 $item->{pending} = $conf->{pending}->{$opt} if defined($conf->{pending}->{$opt});
964 $item->{delete} = ($pending_delete_hash->{$opt} ? 2 : 1) if exists $pending_delete_hash->{$opt};
965
966 # hide cloudinit password
967 if ($opt eq 'cipassword') {
968 $item->{value} = '**********' if defined($item->{value});
969 # the trailing space so that the pending string is different
970 $item->{pending} = '********** ' if defined($item->{pending});
971 }
972 push @$res, $item;
973 }
974
975 foreach my $opt (keys %{$conf->{pending}}) {
976 next if $opt eq 'delete';
977 next if ref($conf->{pending}->{$opt}); # just to be sure
978 next if defined($conf->{$opt});
979 my $item = { key => $opt };
980 $item->{pending} = $conf->{pending}->{$opt};
981
982 # hide cloudinit password
983 if ($opt eq 'cipassword') {
984 $item->{pending} = '**********' if defined($item->{pending});
985 }
986 push @$res, $item;
987 }
988
989 while (my ($opt, $force) = each %$pending_delete_hash) {
990 next if $conf->{pending}->{$opt}; # just to be sure
991 next if $conf->{$opt};
992 my $item = { key => $opt, delete => ($force ? 2 : 1)};
993 push @$res, $item;
994 }
995
996 return $res;
997 }});
998
999 # POST/PUT {vmid}/config implementation
1000 #
1001 # The original API used PUT (idempotent) an we assumed that all operations
1002 # are fast. But it turned out that almost any configuration change can
1003 # involve hot-plug actions, or disk alloc/free. Such actions can take long
1004 # time to complete and have side effects (not idempotent).
1005 #
1006 # The new implementation uses POST and forks a worker process. We added
1007 # a new option 'background_delay'. If specified we wait up to
1008 # 'background_delay' second for the worker task to complete. It returns null
1009 # if the task is finished within that time, else we return the UPID.
1010
1011 my $update_vm_api = sub {
1012 my ($param, $sync) = @_;
1013
1014 my $rpcenv = PVE::RPCEnvironment::get();
1015
1016 my $authuser = $rpcenv->get_user();
1017
1018 my $node = extract_param($param, 'node');
1019
1020 my $vmid = extract_param($param, 'vmid');
1021
1022 my $digest = extract_param($param, 'digest');
1023
1024 my $background_delay = extract_param($param, 'background_delay');
1025
1026 if (defined(my $cipassword = $param->{cipassword})) {
1027 # Same logic as in cloud-init (but with the regex fixed...)
1028 $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword)
1029 if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/;
1030 }
1031
1032 my @paramarr = (); # used for log message
1033 foreach my $key (sort keys %$param) {
1034 my $value = $key eq 'cipassword' ? '<hidden>' : $param->{$key};
1035 push @paramarr, "-$key", $value;
1036 }
1037
1038 my $skiplock = extract_param($param, 'skiplock');
1039 raise_param_exc({ skiplock => "Only root may use this option." })
1040 if $skiplock && $authuser ne 'root@pam';
1041
1042 my $delete_str = extract_param($param, 'delete');
1043
1044 my $revert_str = extract_param($param, 'revert');
1045
1046 my $force = extract_param($param, 'force');
1047
1048 if (defined(my $ssh_keys = $param->{sshkeys})) {
1049 $ssh_keys = URI::Escape::uri_unescape($ssh_keys);
1050 PVE::Tools::validate_ssh_public_keys($ssh_keys);
1051 }
1052
1053 die "no options specified\n" if !$delete_str && !$revert_str && !scalar(keys %$param);
1054
1055 my $storecfg = PVE::Storage::config();
1056
1057 my $defaults = PVE::QemuServer::load_defaults();
1058
1059 &$resolve_cdrom_alias($param);
1060
1061 # now try to verify all parameters
1062
1063 my $revert = {};
1064 foreach my $opt (PVE::Tools::split_list($revert_str)) {
1065 if (!PVE::QemuServer::option_exists($opt)) {
1066 raise_param_exc({ revert => "unknown option '$opt'" });
1067 }
1068
1069 raise_param_exc({ delete => "you can't use '-$opt' and " .
1070 "-revert $opt' at the same time" })
1071 if defined($param->{$opt});
1072
1073 $revert->{$opt} = 1;
1074 }
1075
1076 my @delete = ();
1077 foreach my $opt (PVE::Tools::split_list($delete_str)) {
1078 $opt = 'ide2' if $opt eq 'cdrom';
1079
1080 raise_param_exc({ delete => "you can't use '-$opt' and " .
1081 "-delete $opt' at the same time" })
1082 if defined($param->{$opt});
1083
1084 raise_param_exc({ revert => "you can't use '-delete $opt' and " .
1085 "-revert $opt' at the same time" })
1086 if $revert->{$opt};
1087
1088 if (!PVE::QemuServer::option_exists($opt)) {
1089 raise_param_exc({ delete => "unknown option '$opt'" });
1090 }
1091
1092 push @delete, $opt;
1093 }
1094
1095 my $repl_conf = PVE::ReplicationConfig->new();
1096 my $is_replicated = $repl_conf->check_for_existing_jobs($vmid, 1);
1097 my $check_replication = sub {
1098 my ($drive) = @_;
1099 return if !$is_replicated;
1100 my $volid = $drive->{file};
1101 return if !$volid || !($drive->{replicate}//1);
1102 return if PVE::QemuServer::drive_is_cdrom($drive);
1103 my ($storeid, $format);
1104 if ($volid =~ $NEW_DISK_RE) {
1105 $storeid = $2;
1106 $format = $drive->{format} || PVE::Storage::storage_default_format($storecfg, $storeid);
1107 } else {
1108 ($storeid, undef) = PVE::Storage::parse_volume_id($volid, 1);
1109 $format = (PVE::Storage::parse_volname($storecfg, $volid))[6];
1110 }
1111 return if PVE::Storage::storage_can_replicate($storecfg, $storeid, $format);
1112 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
1113 return if $scfg->{shared};
1114 die "cannot add non-replicatable volume to a replicated VM\n";
1115 };
1116
1117 foreach my $opt (keys %$param) {
1118 if (PVE::QemuServer::is_valid_drivename($opt)) {
1119 # cleanup drive path
1120 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
1121 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
1122 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
1123 $check_replication->($drive);
1124 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
1125 } elsif ($opt =~ m/^net(\d+)$/) {
1126 # add macaddr
1127 my $net = PVE::QemuServer::parse_net($param->{$opt});
1128 $param->{$opt} = PVE::QemuServer::print_net($net);
1129 } elsif ($opt eq 'vmgenid') {
1130 if ($param->{$opt} eq '1') {
1131 $param->{$opt} = PVE::QemuServer::generate_uuid();
1132 }
1133 } elsif ($opt eq 'hookscript') {
1134 eval { PVE::GuestHelpers::check_hookscript($param->{$opt}, $storecfg); };
1135 raise_param_exc({ $opt => $@ }) if $@;
1136 }
1137 }
1138
1139 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
1140
1141 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
1142
1143 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
1144
1145 my $updatefn = sub {
1146
1147 my $conf = PVE::QemuConfig->load_config($vmid);
1148
1149 die "checksum missmatch (file change by other user?)\n"
1150 if $digest && $digest ne $conf->{digest};
1151
1152 PVE::QemuConfig->check_lock($conf) if !$skiplock;
1153
1154 foreach my $opt (keys %$revert) {
1155 if (defined($conf->{$opt})) {
1156 $param->{$opt} = $conf->{$opt};
1157 } elsif (defined($conf->{pending}->{$opt})) {
1158 push @delete, $opt;
1159 }
1160 }
1161
1162 if ($param->{memory} || defined($param->{balloon})) {
1163 my $maxmem = $param->{memory} || $conf->{pending}->{memory} || $conf->{memory} || $defaults->{memory};
1164 my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{pending}->{balloon} || $conf->{balloon};
1165
1166 die "balloon value too large (must be smaller than assigned memory)\n"
1167 if $balloon && $balloon > $maxmem;
1168 }
1169
1170 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
1171
1172 my $worker = sub {
1173
1174 print "update VM $vmid: " . join (' ', @paramarr) . "\n";
1175
1176 # write updates to pending section
1177
1178 my $modified = {}; # record what $option we modify
1179
1180 foreach my $opt (@delete) {
1181 $modified->{$opt} = 1;
1182 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1183 if (!defined($conf->{$opt}) && !defined($conf->{pending}->{$opt})) {
1184 warn "cannot delete '$opt' - not set in current configuration!\n";
1185 $modified->{$opt} = 0;
1186 next;
1187 }
1188
1189 if ($opt =~ m/^unused/) {
1190 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
1191 PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'");
1192 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
1193 if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) {
1194 delete $conf->{$opt};
1195 PVE::QemuConfig->write_config($vmid, $conf);
1196 }
1197 } elsif (PVE::QemuServer::is_valid_drivename($opt)) {
1198 PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'");
1199 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
1200 PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
1201 if defined($conf->{pending}->{$opt});
1202 PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
1203 PVE::QemuConfig->write_config($vmid, $conf);
1204 } elsif ($opt =~ m/^serial\d+$/) {
1205 if ($conf->{$opt} eq 'socket') {
1206 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
1207 } elsif ($authuser ne 'root@pam') {
1208 die "only root can delete '$opt' config for real devices\n";
1209 }
1210 PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
1211 PVE::QemuConfig->write_config($vmid, $conf);
1212 } elsif ($opt =~ m/^usb\d+$/) {
1213 if ($conf->{$opt} =~ m/spice/) {
1214 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
1215 } elsif ($authuser ne 'root@pam') {
1216 die "only root can delete '$opt' config for real devices\n";
1217 }
1218 PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
1219 PVE::QemuConfig->write_config($vmid, $conf);
1220 } else {
1221 PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
1222 PVE::QemuConfig->write_config($vmid, $conf);
1223 }
1224 }
1225
1226 foreach my $opt (keys %$param) { # add/change
1227 $modified->{$opt} = 1;
1228 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1229 next if defined($conf->{pending}->{$opt}) && ($param->{$opt} eq $conf->{pending}->{$opt}); # skip if nothing changed
1230
1231 my ($arch, undef) = PVE::QemuServer::get_basic_machine_info($conf);
1232
1233 if (PVE::QemuServer::is_valid_drivename($opt)) {
1234 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
1235 # FIXME: cloudinit: CDROM or Disk?
1236 if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM
1237 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
1238 } else {
1239 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
1240 }
1241 PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
1242 if defined($conf->{pending}->{$opt});
1243
1244 &$create_disks($rpcenv, $authuser, $conf->{pending}, $arch, $storecfg, $vmid, undef, {$opt => $param->{$opt}});
1245 } elsif ($opt =~ m/^serial\d+/) {
1246 if ((!defined($conf->{$opt}) || $conf->{$opt} eq 'socket') && $param->{$opt} eq 'socket') {
1247 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
1248 } elsif ($authuser ne 'root@pam') {
1249 die "only root can modify '$opt' config for real devices\n";
1250 }
1251 $conf->{pending}->{$opt} = $param->{$opt};
1252 } elsif ($opt =~ m/^usb\d+/) {
1253 if ((!defined($conf->{$opt}) || $conf->{$opt} =~ m/spice/) && $param->{$opt} =~ m/spice/) {
1254 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
1255 } elsif ($authuser ne 'root@pam') {
1256 die "only root can modify '$opt' config for real devices\n";
1257 }
1258 $conf->{pending}->{$opt} = $param->{$opt};
1259 } else {
1260 $conf->{pending}->{$opt} = $param->{$opt};
1261 }
1262 PVE::QemuServer::vmconfig_undelete_pending_option($conf, $opt);
1263 PVE::QemuConfig->write_config($vmid, $conf);
1264 }
1265
1266 # remove pending changes when nothing changed
1267 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1268 my $changes = PVE::QemuServer::vmconfig_cleanup_pending($conf);
1269 PVE::QemuConfig->write_config($vmid, $conf) if $changes;
1270
1271 return if !scalar(keys %{$conf->{pending}});
1272
1273 my $running = PVE::QemuServer::check_running($vmid);
1274
1275 # apply pending changes
1276
1277 $conf = PVE::QemuConfig->load_config($vmid); # update/reload
1278
1279 if ($running) {
1280 my $errors = {};
1281 PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors);
1282 raise_param_exc($errors) if scalar(keys %$errors);
1283 } else {
1284 PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $running);
1285 }
1286
1287 return;
1288 };
1289
1290 if ($sync) {
1291 &$worker();
1292 return undef;
1293 } else {
1294 my $upid = $rpcenv->fork_worker('qmconfig', $vmid, $authuser, $worker);
1295
1296 if ($background_delay) {
1297
1298 # Note: It would be better to do that in the Event based HTTPServer
1299 # to avoid blocking call to sleep.
1300
1301 my $end_time = time() + $background_delay;
1302
1303 my $task = PVE::Tools::upid_decode($upid);
1304
1305 my $running = 1;
1306 while (time() < $end_time) {
1307 $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart});
1308 last if !$running;
1309 sleep(1); # this gets interrupted when child process ends
1310 }
1311
1312 if (!$running) {
1313 my $status = PVE::Tools::upid_read_status($upid);
1314 return undef if $status eq 'OK';
1315 die $status;
1316 }
1317 }
1318
1319 return $upid;
1320 }
1321 };
1322
1323 return PVE::QemuConfig->lock_config($vmid, $updatefn);
1324 };
1325
1326 my $vm_config_perm_list = [
1327 'VM.Config.Disk',
1328 'VM.Config.CDROM',
1329 'VM.Config.CPU',
1330 'VM.Config.Memory',
1331 'VM.Config.Network',
1332 'VM.Config.HWType',
1333 'VM.Config.Options',
1334 ];
1335
1336 __PACKAGE__->register_method({
1337 name => 'update_vm_async',
1338 path => '{vmid}/config',
1339 method => 'POST',
1340 protected => 1,
1341 proxyto => 'node',
1342 description => "Set virtual machine options (asynchrounous API).",
1343 permissions => {
1344 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
1345 },
1346 parameters => {
1347 additionalProperties => 0,
1348 properties => PVE::QemuServer::json_config_properties(
1349 {
1350 node => get_standard_option('pve-node'),
1351 vmid => get_standard_option('pve-vmid'),
1352 skiplock => get_standard_option('skiplock'),
1353 delete => {
1354 type => 'string', format => 'pve-configid-list',
1355 description => "A list of settings you want to delete.",
1356 optional => 1,
1357 },
1358 revert => {
1359 type => 'string', format => 'pve-configid-list',
1360 description => "Revert a pending change.",
1361 optional => 1,
1362 },
1363 force => {
1364 type => 'boolean',
1365 description => $opt_force_description,
1366 optional => 1,
1367 requires => 'delete',
1368 },
1369 digest => {
1370 type => 'string',
1371 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1372 maxLength => 40,
1373 optional => 1,
1374 },
1375 background_delay => {
1376 type => 'integer',
1377 description => "Time to wait for the task to finish. We return 'null' if the task finish within that time.",
1378 minimum => 1,
1379 maximum => 30,
1380 optional => 1,
1381 },
1382 }),
1383 },
1384 returns => {
1385 type => 'string',
1386 optional => 1,
1387 },
1388 code => $update_vm_api,
1389 });
1390
1391 __PACKAGE__->register_method({
1392 name => 'update_vm',
1393 path => '{vmid}/config',
1394 method => 'PUT',
1395 protected => 1,
1396 proxyto => 'node',
1397 description => "Set virtual machine options (synchrounous API) - You should consider using the POST method instead for any actions involving hotplug or storage allocation.",
1398 permissions => {
1399 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
1400 },
1401 parameters => {
1402 additionalProperties => 0,
1403 properties => PVE::QemuServer::json_config_properties(
1404 {
1405 node => get_standard_option('pve-node'),
1406 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
1407 skiplock => get_standard_option('skiplock'),
1408 delete => {
1409 type => 'string', format => 'pve-configid-list',
1410 description => "A list of settings you want to delete.",
1411 optional => 1,
1412 },
1413 revert => {
1414 type => 'string', format => 'pve-configid-list',
1415 description => "Revert a pending change.",
1416 optional => 1,
1417 },
1418 force => {
1419 type => 'boolean',
1420 description => $opt_force_description,
1421 optional => 1,
1422 requires => 'delete',
1423 },
1424 digest => {
1425 type => 'string',
1426 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1427 maxLength => 40,
1428 optional => 1,
1429 },
1430 }),
1431 },
1432 returns => { type => 'null' },
1433 code => sub {
1434 my ($param) = @_;
1435 &$update_vm_api($param, 1);
1436 return undef;
1437 }
1438 });
1439
1440 __PACKAGE__->register_method({
1441 name => 'destroy_vm',
1442 path => '{vmid}',
1443 method => 'DELETE',
1444 protected => 1,
1445 proxyto => 'node',
1446 description => "Destroy the vm (also delete all used/owned volumes).",
1447 permissions => {
1448 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
1449 },
1450 parameters => {
1451 additionalProperties => 0,
1452 properties => {
1453 node => get_standard_option('pve-node'),
1454 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }),
1455 skiplock => get_standard_option('skiplock'),
1456 },
1457 },
1458 returns => {
1459 type => 'string',
1460 },
1461 code => sub {
1462 my ($param) = @_;
1463
1464 my $rpcenv = PVE::RPCEnvironment::get();
1465 my $authuser = $rpcenv->get_user();
1466 my $vmid = $param->{vmid};
1467
1468 my $skiplock = $param->{skiplock};
1469 raise_param_exc({ skiplock => "Only root may use this option." })
1470 if $skiplock && $authuser ne 'root@pam';
1471
1472 # test if VM exists
1473 my $conf = PVE::QemuConfig->load_config($vmid);
1474 my $storecfg = PVE::Storage::config();
1475 PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid");
1476 die "unable to remove VM $vmid - used in HA resources\n"
1477 if PVE::HA::Config::vm_is_ha_managed($vmid);
1478
1479 # do not allow destroy if there are replication jobs
1480 my $repl_conf = PVE::ReplicationConfig->new();
1481 $repl_conf->check_for_existing_jobs($vmid);
1482
1483 # early tests (repeat after locking)
1484 die "VM $vmid is running - destroy failed\n"
1485 if PVE::QemuServer::check_running($vmid);
1486
1487 my $realcmd = sub {
1488 my $upid = shift;
1489
1490 syslog('info', "destroy VM $vmid: $upid\n");
1491 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
1492 PVE::AccessControl::remove_vm_access($vmid);
1493 PVE::Firewall::remove_vmfw_conf($vmid);
1494 };
1495
1496 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
1497 }});
1498
1499 __PACKAGE__->register_method({
1500 name => 'unlink',
1501 path => '{vmid}/unlink',
1502 method => 'PUT',
1503 protected => 1,
1504 proxyto => 'node',
1505 description => "Unlink/delete disk images.",
1506 permissions => {
1507 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
1508 },
1509 parameters => {
1510 additionalProperties => 0,
1511 properties => {
1512 node => get_standard_option('pve-node'),
1513 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
1514 idlist => {
1515 type => 'string', format => 'pve-configid-list',
1516 description => "A list of disk IDs you want to delete.",
1517 },
1518 force => {
1519 type => 'boolean',
1520 description => $opt_force_description,
1521 optional => 1,
1522 },
1523 },
1524 },
1525 returns => { type => 'null'},
1526 code => sub {
1527 my ($param) = @_;
1528
1529 $param->{delete} = extract_param($param, 'idlist');
1530
1531 __PACKAGE__->update_vm($param);
1532
1533 return undef;
1534 }});
1535
1536 my $sslcert;
1537
1538 __PACKAGE__->register_method({
1539 name => 'vncproxy',
1540 path => '{vmid}/vncproxy',
1541 method => 'POST',
1542 protected => 1,
1543 permissions => {
1544 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1545 },
1546 description => "Creates a TCP VNC proxy connections.",
1547 parameters => {
1548 additionalProperties => 0,
1549 properties => {
1550 node => get_standard_option('pve-node'),
1551 vmid => get_standard_option('pve-vmid'),
1552 websocket => {
1553 optional => 1,
1554 type => 'boolean',
1555 description => "starts websockify instead of vncproxy",
1556 },
1557 },
1558 },
1559 returns => {
1560 additionalProperties => 0,
1561 properties => {
1562 user => { type => 'string' },
1563 ticket => { type => 'string' },
1564 cert => { type => 'string' },
1565 port => { type => 'integer' },
1566 upid => { type => 'string' },
1567 },
1568 },
1569 code => sub {
1570 my ($param) = @_;
1571
1572 my $rpcenv = PVE::RPCEnvironment::get();
1573
1574 my $authuser = $rpcenv->get_user();
1575
1576 my $vmid = $param->{vmid};
1577 my $node = $param->{node};
1578 my $websocket = $param->{websocket};
1579
1580 my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists
1581 my $use_serial = ($conf->{vga} && ($conf->{vga} =~ m/^serial\d+$/));
1582
1583 my $authpath = "/vms/$vmid";
1584
1585 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1586
1587 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
1588 if !$sslcert;
1589
1590 my $family;
1591 my $remcmd = [];
1592
1593 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1594 (undef, $family) = PVE::Cluster::remote_node_ip($node);
1595 my $sshinfo = PVE::Cluster::get_ssh_info($node);
1596 # NOTE: kvm VNC traffic is already TLS encrypted or is known unsecure
1597 $remcmd = PVE::Cluster::ssh_info_to_command($sshinfo, $use_serial ? '-t' : '-T');
1598 } else {
1599 $family = PVE::Tools::get_host_address_family($node);
1600 }
1601
1602 my $port = PVE::Tools::next_vnc_port($family);
1603
1604 my $timeout = 10;
1605
1606 my $realcmd = sub {
1607 my $upid = shift;
1608
1609 syslog('info', "starting vnc proxy $upid\n");
1610
1611 my $cmd;
1612
1613 if ($use_serial) {
1614
1615 my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-iface', $conf->{vga}, '-escape', '0' ];
1616
1617 $cmd = ['/usr/bin/vncterm', '-rfbport', $port,
1618 '-timeout', $timeout, '-authpath', $authpath,
1619 '-perm', 'Sys.Console'];
1620
1621 if ($param->{websocket}) {
1622 $ENV{PVE_VNC_TICKET} = $ticket; # pass ticket to vncterm
1623 push @$cmd, '-notls', '-listen', 'localhost';
1624 }
1625
1626 push @$cmd, '-c', @$remcmd, @$termcmd;
1627
1628 PVE::Tools::run_command($cmd);
1629
1630 } else {
1631
1632 $ENV{LC_PVE_TICKET} = $ticket if $websocket; # set ticket with "qm vncproxy"
1633
1634 $cmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1635
1636 my $sock = IO::Socket::IP->new(
1637 ReuseAddr => 1,
1638 Listen => 1,
1639 LocalPort => $port,
1640 Proto => 'tcp',
1641 GetAddrInfoFlags => 0,
1642 ) or die "failed to create socket: $!\n";
1643 # Inside the worker we shouldn't have any previous alarms
1644 # running anyway...:
1645 alarm(0);
1646 local $SIG{ALRM} = sub { die "connection timed out\n" };
1647 alarm $timeout;
1648 accept(my $cli, $sock) or die "connection failed: $!\n";
1649 alarm(0);
1650 close($sock);
1651 if (PVE::Tools::run_command($cmd,
1652 output => '>&'.fileno($cli),
1653 input => '<&'.fileno($cli),
1654 noerr => 1) != 0)
1655 {
1656 die "Failed to run vncproxy.\n";
1657 }
1658 }
1659
1660 return;
1661 };
1662
1663 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1);
1664
1665 PVE::Tools::wait_for_vnc_port($port);
1666
1667 return {
1668 user => $authuser,
1669 ticket => $ticket,
1670 port => $port,
1671 upid => $upid,
1672 cert => $sslcert,
1673 };
1674 }});
1675
1676 __PACKAGE__->register_method({
1677 name => 'termproxy',
1678 path => '{vmid}/termproxy',
1679 method => 'POST',
1680 protected => 1,
1681 permissions => {
1682 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1683 },
1684 description => "Creates a TCP proxy connections.",
1685 parameters => {
1686 additionalProperties => 0,
1687 properties => {
1688 node => get_standard_option('pve-node'),
1689 vmid => get_standard_option('pve-vmid'),
1690 serial=> {
1691 optional => 1,
1692 type => 'string',
1693 enum => [qw(serial0 serial1 serial2 serial3)],
1694 description => "opens a serial terminal (defaults to display)",
1695 },
1696 },
1697 },
1698 returns => {
1699 additionalProperties => 0,
1700 properties => {
1701 user => { type => 'string' },
1702 ticket => { type => 'string' },
1703 port => { type => 'integer' },
1704 upid => { type => 'string' },
1705 },
1706 },
1707 code => sub {
1708 my ($param) = @_;
1709
1710 my $rpcenv = PVE::RPCEnvironment::get();
1711
1712 my $authuser = $rpcenv->get_user();
1713
1714 my $vmid = $param->{vmid};
1715 my $node = $param->{node};
1716 my $serial = $param->{serial};
1717
1718 my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists
1719
1720 if (!defined($serial)) {
1721 if ($conf->{vga} && $conf->{vga} =~ m/^serial\d+$/) {
1722 $serial = $conf->{vga};
1723 }
1724 }
1725
1726 my $authpath = "/vms/$vmid";
1727
1728 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
1729
1730 my $family;
1731 my $remcmd = [];
1732
1733 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1734 (undef, $family) = PVE::Cluster::remote_node_ip($node);
1735 my $sshinfo = PVE::Cluster::get_ssh_info($node);
1736 $remcmd = PVE::Cluster::ssh_info_to_command($sshinfo, '-t');
1737 push @$remcmd, '--';
1738 } else {
1739 $family = PVE::Tools::get_host_address_family($node);
1740 }
1741
1742 my $port = PVE::Tools::next_vnc_port($family);
1743
1744 my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-escape', '0'];
1745 push @$termcmd, '-iface', $serial if $serial;
1746
1747 my $realcmd = sub {
1748 my $upid = shift;
1749
1750 syslog('info', "starting qemu termproxy $upid\n");
1751
1752 my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath,
1753 '--perm', 'VM.Console', '--'];
1754 push @$cmd, @$remcmd, @$termcmd;
1755
1756 PVE::Tools::run_command($cmd);
1757 };
1758
1759 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1);
1760
1761 PVE::Tools::wait_for_vnc_port($port);
1762
1763 return {
1764 user => $authuser,
1765 ticket => $ticket,
1766 port => $port,
1767 upid => $upid,
1768 };
1769 }});
1770
1771 __PACKAGE__->register_method({
1772 name => 'vncwebsocket',
1773 path => '{vmid}/vncwebsocket',
1774 method => 'GET',
1775 permissions => {
1776 description => "You also need to pass a valid ticket (vncticket).",
1777 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1778 },
1779 description => "Opens a weksocket for VNC traffic.",
1780 parameters => {
1781 additionalProperties => 0,
1782 properties => {
1783 node => get_standard_option('pve-node'),
1784 vmid => get_standard_option('pve-vmid'),
1785 vncticket => {
1786 description => "Ticket from previous call to vncproxy.",
1787 type => 'string',
1788 maxLength => 512,
1789 },
1790 port => {
1791 description => "Port number returned by previous vncproxy call.",
1792 type => 'integer',
1793 minimum => 5900,
1794 maximum => 5999,
1795 },
1796 },
1797 },
1798 returns => {
1799 type => "object",
1800 properties => {
1801 port => { type => 'string' },
1802 },
1803 },
1804 code => sub {
1805 my ($param) = @_;
1806
1807 my $rpcenv = PVE::RPCEnvironment::get();
1808
1809 my $authuser = $rpcenv->get_user();
1810
1811 my $vmid = $param->{vmid};
1812 my $node = $param->{node};
1813
1814 my $authpath = "/vms/$vmid";
1815
1816 PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath);
1817
1818 my $conf = PVE::QemuConfig->load_config($vmid, $node); # VM exists ?
1819
1820 # Note: VNC ports are acessible from outside, so we do not gain any
1821 # security if we verify that $param->{port} belongs to VM $vmid. This
1822 # check is done by verifying the VNC ticket (inside VNC protocol).
1823
1824 my $port = $param->{port};
1825
1826 return { port => $port };
1827 }});
1828
1829 __PACKAGE__->register_method({
1830 name => 'spiceproxy',
1831 path => '{vmid}/spiceproxy',
1832 method => 'POST',
1833 protected => 1,
1834 proxyto => 'node',
1835 permissions => {
1836 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1837 },
1838 description => "Returns a SPICE configuration to connect to the VM.",
1839 parameters => {
1840 additionalProperties => 0,
1841 properties => {
1842 node => get_standard_option('pve-node'),
1843 vmid => get_standard_option('pve-vmid'),
1844 proxy => get_standard_option('spice-proxy', { optional => 1 }),
1845 },
1846 },
1847 returns => get_standard_option('remote-viewer-config'),
1848 code => sub {
1849 my ($param) = @_;
1850
1851 my $rpcenv = PVE::RPCEnvironment::get();
1852
1853 my $authuser = $rpcenv->get_user();
1854
1855 my $vmid = $param->{vmid};
1856 my $node = $param->{node};
1857 my $proxy = $param->{proxy};
1858
1859 my $conf = PVE::QemuConfig->load_config($vmid, $node);
1860 my $title = "VM $vmid";
1861 $title .= " - ". $conf->{name} if $conf->{name};
1862
1863 my $port = PVE::QemuServer::spice_port($vmid);
1864
1865 my ($ticket, undef, $remote_viewer_config) =
1866 PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port);
1867
1868 PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket);
1869 PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30");
1870
1871 return $remote_viewer_config;
1872 }});
1873
1874 __PACKAGE__->register_method({
1875 name => 'vmcmdidx',
1876 path => '{vmid}/status',
1877 method => 'GET',
1878 proxyto => 'node',
1879 description => "Directory index",
1880 permissions => {
1881 user => 'all',
1882 },
1883 parameters => {
1884 additionalProperties => 0,
1885 properties => {
1886 node => get_standard_option('pve-node'),
1887 vmid => get_standard_option('pve-vmid'),
1888 },
1889 },
1890 returns => {
1891 type => 'array',
1892 items => {
1893 type => "object",
1894 properties => {
1895 subdir => { type => 'string' },
1896 },
1897 },
1898 links => [ { rel => 'child', href => "{subdir}" } ],
1899 },
1900 code => sub {
1901 my ($param) = @_;
1902
1903 # test if VM exists
1904 my $conf = PVE::QemuConfig->load_config($param->{vmid});
1905
1906 my $res = [
1907 { subdir => 'current' },
1908 { subdir => 'start' },
1909 { subdir => 'stop' },
1910 { subdir => 'reset' },
1911 { subdir => 'shutdown' },
1912 { subdir => 'suspend' },
1913 { subdir => 'reboot' },
1914 ];
1915
1916 return $res;
1917 }});
1918
1919 __PACKAGE__->register_method({
1920 name => 'vm_status',
1921 path => '{vmid}/status/current',
1922 method => 'GET',
1923 proxyto => 'node',
1924 protected => 1, # qemu pid files are only readable by root
1925 description => "Get virtual machine status.",
1926 permissions => {
1927 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1928 },
1929 parameters => {
1930 additionalProperties => 0,
1931 properties => {
1932 node => get_standard_option('pve-node'),
1933 vmid => get_standard_option('pve-vmid'),
1934 },
1935 },
1936 returns => {
1937 type => 'object',
1938 properties => {
1939 %$PVE::QemuServer::vmstatus_return_properties,
1940 ha => {
1941 description => "HA manager service status.",
1942 type => 'object',
1943 },
1944 spice => {
1945 description => "Qemu VGA configuration supports spice.",
1946 type => 'boolean',
1947 optional => 1,
1948 },
1949 agent => {
1950 description => "Qemu GuestAgent enabled in config.",
1951 type => 'boolean',
1952 optional => 1,
1953 },
1954 },
1955 },
1956 code => sub {
1957 my ($param) = @_;
1958
1959 # test if VM exists
1960 my $conf = PVE::QemuConfig->load_config($param->{vmid});
1961
1962 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
1963 my $status = $vmstatus->{$param->{vmid}};
1964
1965 $status->{ha} = PVE::HA::Config::get_service_status("vm:$param->{vmid}");
1966
1967 $status->{spice} = 1 if PVE::QemuServer::vga_conf_has_spice($conf->{vga});
1968 $status->{agent} = 1 if (PVE::QemuServer::parse_guest_agent($conf)->{enabled});
1969
1970 return $status;
1971 }});
1972
1973 __PACKAGE__->register_method({
1974 name => 'vm_start',
1975 path => '{vmid}/status/start',
1976 method => 'POST',
1977 protected => 1,
1978 proxyto => 'node',
1979 description => "Start virtual machine.",
1980 permissions => {
1981 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1982 },
1983 parameters => {
1984 additionalProperties => 0,
1985 properties => {
1986 node => get_standard_option('pve-node'),
1987 vmid => get_standard_option('pve-vmid',
1988 { completion => \&PVE::QemuServer::complete_vmid_stopped }),
1989 skiplock => get_standard_option('skiplock'),
1990 stateuri => get_standard_option('pve-qm-stateuri'),
1991 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
1992 migration_type => {
1993 type => 'string',
1994 enum => ['secure', 'insecure'],
1995 description => "Migration traffic is encrypted using an SSH " .
1996 "tunnel by default. On secure, completely private networks " .
1997 "this can be disabled to increase performance.",
1998 optional => 1,
1999 },
2000 migration_network => {
2001 type => 'string', format => 'CIDR',
2002 description => "CIDR of the (sub) network that is used for migration.",
2003 optional => 1,
2004 },
2005 machine => get_standard_option('pve-qm-machine'),
2006 targetstorage => {
2007 description => "Target storage for the migration. (Can be '1' to use the same storage id as on the source node.)",
2008 type => 'string',
2009 optional => 1
2010 }
2011 },
2012 },
2013 returns => {
2014 type => 'string',
2015 },
2016 code => sub {
2017 my ($param) = @_;
2018
2019 my $rpcenv = PVE::RPCEnvironment::get();
2020 my $authuser = $rpcenv->get_user();
2021
2022 my $node = extract_param($param, 'node');
2023 my $vmid = extract_param($param, 'vmid');
2024
2025 my $machine = extract_param($param, 'machine');
2026
2027 my $get_root_param = sub {
2028 my $value = extract_param($param, $_[0]);
2029 raise_param_exc({ "$_[0]" => "Only root may use this option." })
2030 if $value && $authuser ne 'root@pam';
2031 return $value;
2032 };
2033
2034 my $stateuri = $get_root_param->('stateuri');
2035 my $skiplock = $get_root_param->('skiplock');
2036 my $migratedfrom = $get_root_param->('migratedfrom');
2037 my $migration_type = $get_root_param->('migration_type');
2038 my $migration_network = $get_root_param->('migration_network');
2039 my $targetstorage = $get_root_param->('targetstorage');
2040
2041 raise_param_exc({ targetstorage => "targetstorage can only by used with migratedfrom." })
2042 if $targetstorage && !$migratedfrom;
2043
2044 # read spice ticket from STDIN
2045 my $spice_ticket;
2046 if ($stateuri && ($stateuri eq 'tcp' || $stateuri eq 'unix') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
2047 if (defined(my $line = <STDIN>)) {
2048 chomp $line;
2049 $spice_ticket = $line;
2050 }
2051 }
2052
2053 PVE::Cluster::check_cfs_quorum();
2054
2055 my $storecfg = PVE::Storage::config();
2056
2057 if (PVE::HA::Config::vm_is_ha_managed($vmid) && !$stateuri && $rpcenv->{type} ne 'ha') {
2058 my $hacmd = sub {
2059 my $upid = shift;
2060
2061 print "Requesting HA start for VM $vmid\n";
2062
2063 my $cmd = ['ha-manager', 'set', "vm:$vmid", '--state', 'started'];
2064 PVE::Tools::run_command($cmd);
2065 return;
2066 };
2067
2068 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
2069
2070 } else {
2071
2072 my $realcmd = sub {
2073 my $upid = shift;
2074
2075 syslog('info', "start VM $vmid: $upid\n");
2076
2077 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom, undef,
2078 $machine, $spice_ticket, $migration_network, $migration_type, $targetstorage);
2079 return;
2080 };
2081
2082 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
2083 }
2084 }});
2085
2086 __PACKAGE__->register_method({
2087 name => 'vm_stop',
2088 path => '{vmid}/status/stop',
2089 method => 'POST',
2090 protected => 1,
2091 proxyto => 'node',
2092 description => "Stop virtual machine. The qemu process will exit immediately. This" .
2093 "is akin to pulling the power plug of a running computer and may damage the VM data",
2094 permissions => {
2095 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2096 },
2097 parameters => {
2098 additionalProperties => 0,
2099 properties => {
2100 node => get_standard_option('pve-node'),
2101 vmid => get_standard_option('pve-vmid',
2102 { completion => \&PVE::QemuServer::complete_vmid_running }),
2103 skiplock => get_standard_option('skiplock'),
2104 migratedfrom => get_standard_option('pve-node', { optional => 1 }),
2105 timeout => {
2106 description => "Wait maximal timeout seconds.",
2107 type => 'integer',
2108 minimum => 0,
2109 optional => 1,
2110 },
2111 keepActive => {
2112 description => "Do not deactivate storage volumes.",
2113 type => 'boolean',
2114 optional => 1,
2115 default => 0,
2116 }
2117 },
2118 },
2119 returns => {
2120 type => 'string',
2121 },
2122 code => sub {
2123 my ($param) = @_;
2124
2125 my $rpcenv = PVE::RPCEnvironment::get();
2126 my $authuser = $rpcenv->get_user();
2127
2128 my $node = extract_param($param, 'node');
2129 my $vmid = extract_param($param, 'vmid');
2130
2131 my $skiplock = extract_param($param, 'skiplock');
2132 raise_param_exc({ skiplock => "Only root may use this option." })
2133 if $skiplock && $authuser ne 'root@pam';
2134
2135 my $keepActive = extract_param($param, 'keepActive');
2136 raise_param_exc({ keepActive => "Only root may use this option." })
2137 if $keepActive && $authuser ne 'root@pam';
2138
2139 my $migratedfrom = extract_param($param, 'migratedfrom');
2140 raise_param_exc({ migratedfrom => "Only root may use this option." })
2141 if $migratedfrom && $authuser ne 'root@pam';
2142
2143
2144 my $storecfg = PVE::Storage::config();
2145
2146 if (PVE::HA::Config::vm_is_ha_managed($vmid) && ($rpcenv->{type} ne 'ha') && !defined($migratedfrom)) {
2147
2148 my $hacmd = sub {
2149 my $upid = shift;
2150
2151 print "Requesting HA stop for VM $vmid\n";
2152
2153 my $cmd = ['ha-manager', 'set', "vm:$vmid", '--state', 'stopped'];
2154 PVE::Tools::run_command($cmd);
2155 return;
2156 };
2157
2158 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
2159
2160 } else {
2161 my $realcmd = sub {
2162 my $upid = shift;
2163
2164 syslog('info', "stop VM $vmid: $upid\n");
2165
2166 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
2167 $param->{timeout}, 0, 1, $keepActive, $migratedfrom);
2168 return;
2169 };
2170
2171 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
2172 }
2173 }});
2174
2175 __PACKAGE__->register_method({
2176 name => 'vm_reset',
2177 path => '{vmid}/status/reset',
2178 method => 'POST',
2179 protected => 1,
2180 proxyto => 'node',
2181 description => "Reset virtual machine.",
2182 permissions => {
2183 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2184 },
2185 parameters => {
2186 additionalProperties => 0,
2187 properties => {
2188 node => get_standard_option('pve-node'),
2189 vmid => get_standard_option('pve-vmid',
2190 { completion => \&PVE::QemuServer::complete_vmid_running }),
2191 skiplock => get_standard_option('skiplock'),
2192 },
2193 },
2194 returns => {
2195 type => 'string',
2196 },
2197 code => sub {
2198 my ($param) = @_;
2199
2200 my $rpcenv = PVE::RPCEnvironment::get();
2201
2202 my $authuser = $rpcenv->get_user();
2203
2204 my $node = extract_param($param, 'node');
2205
2206 my $vmid = extract_param($param, 'vmid');
2207
2208 my $skiplock = extract_param($param, 'skiplock');
2209 raise_param_exc({ skiplock => "Only root may use this option." })
2210 if $skiplock && $authuser ne 'root@pam';
2211
2212 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
2213
2214 my $realcmd = sub {
2215 my $upid = shift;
2216
2217 PVE::QemuServer::vm_reset($vmid, $skiplock);
2218
2219 return;
2220 };
2221
2222 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
2223 }});
2224
2225 __PACKAGE__->register_method({
2226 name => 'vm_shutdown',
2227 path => '{vmid}/status/shutdown',
2228 method => 'POST',
2229 protected => 1,
2230 proxyto => 'node',
2231 description => "Shutdown virtual machine. This is similar to pressing the power button on a physical machine." .
2232 "This will send an ACPI event for the guest OS, which should then proceed to a clean shutdown.",
2233 permissions => {
2234 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2235 },
2236 parameters => {
2237 additionalProperties => 0,
2238 properties => {
2239 node => get_standard_option('pve-node'),
2240 vmid => get_standard_option('pve-vmid',
2241 { completion => \&PVE::QemuServer::complete_vmid_running }),
2242 skiplock => get_standard_option('skiplock'),
2243 timeout => {
2244 description => "Wait maximal timeout seconds.",
2245 type => 'integer',
2246 minimum => 0,
2247 optional => 1,
2248 },
2249 forceStop => {
2250 description => "Make sure the VM stops.",
2251 type => 'boolean',
2252 optional => 1,
2253 default => 0,
2254 },
2255 keepActive => {
2256 description => "Do not deactivate storage volumes.",
2257 type => 'boolean',
2258 optional => 1,
2259 default => 0,
2260 }
2261 },
2262 },
2263 returns => {
2264 type => 'string',
2265 },
2266 code => sub {
2267 my ($param) = @_;
2268
2269 my $rpcenv = PVE::RPCEnvironment::get();
2270 my $authuser = $rpcenv->get_user();
2271
2272 my $node = extract_param($param, 'node');
2273 my $vmid = extract_param($param, 'vmid');
2274
2275 my $skiplock = extract_param($param, 'skiplock');
2276 raise_param_exc({ skiplock => "Only root may use this option." })
2277 if $skiplock && $authuser ne 'root@pam';
2278
2279 my $keepActive = extract_param($param, 'keepActive');
2280 raise_param_exc({ keepActive => "Only root may use this option." })
2281 if $keepActive && $authuser ne 'root@pam';
2282
2283 my $storecfg = PVE::Storage::config();
2284
2285 my $shutdown = 1;
2286
2287 # if vm is paused, do not shutdown (but stop if forceStop = 1)
2288 # otherwise, we will infer a shutdown command, but run into the timeout,
2289 # then when the vm is resumed, it will instantly shutdown
2290 #
2291 # checking the qmp status here to get feedback to the gui/cli/api
2292 # and the status query should not take too long
2293 my $qmpstatus = eval {
2294 PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0);
2295 };
2296 my $err = $@ if $@;
2297
2298 if (!$err && $qmpstatus->{status} eq "paused") {
2299 if ($param->{forceStop}) {
2300 warn "VM is paused - stop instead of shutdown\n";
2301 $shutdown = 0;
2302 } else {
2303 die "VM is paused - cannot shutdown\n";
2304 }
2305 }
2306
2307 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
2308
2309 my $hacmd = sub {
2310 my $upid = shift;
2311
2312 print "Requesting HA stop for VM $vmid\n";
2313
2314 my $cmd = ['ha-manager', 'set', "vm:$vmid", '--state', 'stopped'];
2315 PVE::Tools::run_command($cmd);
2316 return;
2317 };
2318
2319 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
2320
2321 } else {
2322
2323 my $realcmd = sub {
2324 my $upid = shift;
2325
2326 syslog('info', "shutdown VM $vmid: $upid\n");
2327
2328 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
2329 $shutdown, $param->{forceStop}, $keepActive);
2330 return;
2331 };
2332
2333 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
2334 }
2335 }});
2336
2337 __PACKAGE__->register_method({
2338 name => 'vm_reboot',
2339 path => '{vmid}/status/reboot',
2340 method => 'POST',
2341 protected => 1,
2342 proxyto => 'node',
2343 description => "Reboot the VM by shutting it down, and starting it again. Applies pending changes.",
2344 permissions => {
2345 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2346 },
2347 parameters => {
2348 additionalProperties => 0,
2349 properties => {
2350 node => get_standard_option('pve-node'),
2351 vmid => get_standard_option('pve-vmid',
2352 { completion => \&PVE::QemuServer::complete_vmid_running }),
2353 timeout => {
2354 description => "Wait maximal timeout seconds for the shutdown.",
2355 type => 'integer',
2356 minimum => 0,
2357 optional => 1,
2358 },
2359 },
2360 },
2361 returns => {
2362 type => 'string',
2363 },
2364 code => sub {
2365 my ($param) = @_;
2366
2367 my $rpcenv = PVE::RPCEnvironment::get();
2368 my $authuser = $rpcenv->get_user();
2369
2370 my $node = extract_param($param, 'node');
2371 my $vmid = extract_param($param, 'vmid');
2372
2373 my $qmpstatus = eval {
2374 PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0);
2375 };
2376 my $err = $@ if $@;
2377
2378 if (!$err && $qmpstatus->{status} eq "paused") {
2379 die "VM is paused - cannot shutdown\n";
2380 }
2381
2382 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
2383
2384 my $realcmd = sub {
2385 my $upid = shift;
2386
2387 syslog('info', "requesting reboot of VM $vmid: $upid\n");
2388 PVE::QemuServer::vm_reboot($vmid, $param->{timeout});
2389 return;
2390 };
2391
2392 return $rpcenv->fork_worker('qmreboot', $vmid, $authuser, $realcmd);
2393 }});
2394
2395 __PACKAGE__->register_method({
2396 name => 'vm_suspend',
2397 path => '{vmid}/status/suspend',
2398 method => 'POST',
2399 protected => 1,
2400 proxyto => 'node',
2401 description => "Suspend virtual machine.",
2402 permissions => {
2403 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2404 },
2405 parameters => {
2406 additionalProperties => 0,
2407 properties => {
2408 node => get_standard_option('pve-node'),
2409 vmid => get_standard_option('pve-vmid',
2410 { completion => \&PVE::QemuServer::complete_vmid_running }),
2411 skiplock => get_standard_option('skiplock'),
2412 todisk => {
2413 type => 'boolean',
2414 default => 0,
2415 optional => 1,
2416 description => 'If set, suspends the VM to disk. Will be resumed on next VM start.',
2417 },
2418 statestorage => get_standard_option('pve-storage-id', {
2419 description => "The storage for the VM state",
2420 requires => 'todisk',
2421 optional => 1,
2422 completion => \&PVE::Storage::complete_storage_enabled,
2423 }),
2424 },
2425 },
2426 returns => {
2427 type => 'string',
2428 },
2429 code => sub {
2430 my ($param) = @_;
2431
2432 my $rpcenv = PVE::RPCEnvironment::get();
2433 my $authuser = $rpcenv->get_user();
2434
2435 my $node = extract_param($param, 'node');
2436 my $vmid = extract_param($param, 'vmid');
2437
2438 my $todisk = extract_param($param, 'todisk') // 0;
2439
2440 my $statestorage = extract_param($param, 'statestorage');
2441
2442 my $skiplock = extract_param($param, 'skiplock');
2443 raise_param_exc({ skiplock => "Only root may use this option." })
2444 if $skiplock && $authuser ne 'root@pam';
2445
2446 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
2447
2448 die "Cannot suspend HA managed VM to disk\n"
2449 if $todisk && PVE::HA::Config::vm_is_ha_managed($vmid);
2450
2451 my $realcmd = sub {
2452 my $upid = shift;
2453
2454 syslog('info', "suspend VM $vmid: $upid\n");
2455
2456 PVE::QemuServer::vm_suspend($vmid, $skiplock, $todisk, $statestorage);
2457
2458 return;
2459 };
2460
2461 my $taskname = $todisk ? 'qmsuspend' : 'qmpause';
2462 return $rpcenv->fork_worker($taskname, $vmid, $authuser, $realcmd);
2463 }});
2464
2465 __PACKAGE__->register_method({
2466 name => 'vm_resume',
2467 path => '{vmid}/status/resume',
2468 method => 'POST',
2469 protected => 1,
2470 proxyto => 'node',
2471 description => "Resume virtual machine.",
2472 permissions => {
2473 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
2474 },
2475 parameters => {
2476 additionalProperties => 0,
2477 properties => {
2478 node => get_standard_option('pve-node'),
2479 vmid => get_standard_option('pve-vmid',
2480 { completion => \&PVE::QemuServer::complete_vmid_running }),
2481 skiplock => get_standard_option('skiplock'),
2482 nocheck => { type => 'boolean', optional => 1 },
2483
2484 },
2485 },
2486 returns => {
2487 type => 'string',
2488 },
2489 code => sub {
2490 my ($param) = @_;
2491
2492 my $rpcenv = PVE::RPCEnvironment::get();
2493
2494 my $authuser = $rpcenv->get_user();
2495
2496 my $node = extract_param($param, 'node');
2497
2498 my $vmid = extract_param($param, 'vmid');
2499
2500 my $skiplock = extract_param($param, 'skiplock');
2501 raise_param_exc({ skiplock => "Only root may use this option." })
2502 if $skiplock && $authuser ne 'root@pam';
2503
2504 my $nocheck = extract_param($param, 'nocheck');
2505
2506 my $to_disk_suspended;
2507 eval {
2508 PVE::QemuConfig->lock_config($vmid, sub {
2509 my $conf = PVE::QemuConfig->load_config($vmid);
2510 $to_disk_suspended = PVE::QemuConfig->has_lock($conf, 'suspended');
2511 });
2512 };
2513
2514 die "VM $vmid not running\n"
2515 if !$to_disk_suspended && !PVE::QemuServer::check_running($vmid, $nocheck);
2516
2517 my $realcmd = sub {
2518 my $upid = shift;
2519
2520 syslog('info', "resume VM $vmid: $upid\n");
2521
2522 if (!$to_disk_suspended) {
2523 PVE::QemuServer::vm_resume($vmid, $skiplock, $nocheck);
2524 } else {
2525 my $storecfg = PVE::Storage::config();
2526 PVE::QemuServer::vm_start($storecfg, $vmid, undef, $skiplock);
2527 }
2528
2529 return;
2530 };
2531
2532 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
2533 }});
2534
2535 __PACKAGE__->register_method({
2536 name => 'vm_sendkey',
2537 path => '{vmid}/sendkey',
2538 method => 'PUT',
2539 protected => 1,
2540 proxyto => 'node',
2541 description => "Send key event to virtual machine.",
2542 permissions => {
2543 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
2544 },
2545 parameters => {
2546 additionalProperties => 0,
2547 properties => {
2548 node => get_standard_option('pve-node'),
2549 vmid => get_standard_option('pve-vmid',
2550 { completion => \&PVE::QemuServer::complete_vmid_running }),
2551 skiplock => get_standard_option('skiplock'),
2552 key => {
2553 description => "The key (qemu monitor encoding).",
2554 type => 'string'
2555 }
2556 },
2557 },
2558 returns => { type => 'null'},
2559 code => sub {
2560 my ($param) = @_;
2561
2562 my $rpcenv = PVE::RPCEnvironment::get();
2563
2564 my $authuser = $rpcenv->get_user();
2565
2566 my $node = extract_param($param, 'node');
2567
2568 my $vmid = extract_param($param, 'vmid');
2569
2570 my $skiplock = extract_param($param, 'skiplock');
2571 raise_param_exc({ skiplock => "Only root may use this option." })
2572 if $skiplock && $authuser ne 'root@pam';
2573
2574 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
2575
2576 return;
2577 }});
2578
2579 __PACKAGE__->register_method({
2580 name => 'vm_feature',
2581 path => '{vmid}/feature',
2582 method => 'GET',
2583 proxyto => 'node',
2584 protected => 1,
2585 description => "Check if feature for virtual machine is available.",
2586 permissions => {
2587 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
2588 },
2589 parameters => {
2590 additionalProperties => 0,
2591 properties => {
2592 node => get_standard_option('pve-node'),
2593 vmid => get_standard_option('pve-vmid'),
2594 feature => {
2595 description => "Feature to check.",
2596 type => 'string',
2597 enum => [ 'snapshot', 'clone', 'copy' ],
2598 },
2599 snapname => get_standard_option('pve-snapshot-name', {
2600 optional => 1,
2601 }),
2602 },
2603 },
2604 returns => {
2605 type => "object",
2606 properties => {
2607 hasFeature => { type => 'boolean' },
2608 nodes => {
2609 type => 'array',
2610 items => { type => 'string' },
2611 }
2612 },
2613 },
2614 code => sub {
2615 my ($param) = @_;
2616
2617 my $node = extract_param($param, 'node');
2618
2619 my $vmid = extract_param($param, 'vmid');
2620
2621 my $snapname = extract_param($param, 'snapname');
2622
2623 my $feature = extract_param($param, 'feature');
2624
2625 my $running = PVE::QemuServer::check_running($vmid);
2626
2627 my $conf = PVE::QemuConfig->load_config($vmid);
2628
2629 if($snapname){
2630 my $snap = $conf->{snapshots}->{$snapname};
2631 die "snapshot '$snapname' does not exist\n" if !defined($snap);
2632 $conf = $snap;
2633 }
2634 my $storecfg = PVE::Storage::config();
2635
2636 my $nodelist = PVE::QemuServer::shared_nodes($conf, $storecfg);
2637 my $hasFeature = PVE::QemuConfig->has_feature($feature, $conf, $storecfg, $snapname, $running);
2638
2639 return {
2640 hasFeature => $hasFeature,
2641 nodes => [ keys %$nodelist ],
2642 };
2643 }});
2644
2645 __PACKAGE__->register_method({
2646 name => 'clone_vm',
2647 path => '{vmid}/clone',
2648 method => 'POST',
2649 protected => 1,
2650 proxyto => 'node',
2651 description => "Create a copy of virtual machine/template.",
2652 permissions => {
2653 description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " .
2654 "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " .
2655 "'Datastore.AllocateSpace' on any used storage.",
2656 check =>
2657 [ 'and',
2658 ['perm', '/vms/{vmid}', [ 'VM.Clone' ]],
2659 [ 'or',
2660 [ 'perm', '/vms/{newid}', ['VM.Allocate']],
2661 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
2662 ],
2663 ]
2664 },
2665 parameters => {
2666 additionalProperties => 0,
2667 properties => {
2668 node => get_standard_option('pve-node'),
2669 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
2670 newid => get_standard_option('pve-vmid', {
2671 completion => \&PVE::Cluster::complete_next_vmid,
2672 description => 'VMID for the clone.' }),
2673 name => {
2674 optional => 1,
2675 type => 'string', format => 'dns-name',
2676 description => "Set a name for the new VM.",
2677 },
2678 description => {
2679 optional => 1,
2680 type => 'string',
2681 description => "Description for the new VM.",
2682 },
2683 pool => {
2684 optional => 1,
2685 type => 'string', format => 'pve-poolid',
2686 description => "Add the new VM to the specified pool.",
2687 },
2688 snapname => get_standard_option('pve-snapshot-name', {
2689 optional => 1,
2690 }),
2691 storage => get_standard_option('pve-storage-id', {
2692 description => "Target storage for full clone.",
2693 optional => 1,
2694 }),
2695 'format' => {
2696 description => "Target format for file storage. Only valid for full clone.",
2697 type => 'string',
2698 optional => 1,
2699 enum => [ 'raw', 'qcow2', 'vmdk'],
2700 },
2701 full => {
2702 optional => 1,
2703 type => 'boolean',
2704 description => "Create a full copy of all disks. This is always done when " .
2705 "you clone a normal VM. For VM templates, we try to create a linked clone by default.",
2706 },
2707 target => get_standard_option('pve-node', {
2708 description => "Target node. Only allowed if the original VM is on shared storage.",
2709 optional => 1,
2710 }),
2711 bwlimit => {
2712 description => "Override I/O bandwidth limit (in KiB/s).",
2713 optional => 1,
2714 type => 'integer',
2715 minimum => '0',
2716 default => 'clone limit from datacenter or storage config',
2717 },
2718 },
2719 },
2720 returns => {
2721 type => 'string',
2722 },
2723 code => sub {
2724 my ($param) = @_;
2725
2726 my $rpcenv = PVE::RPCEnvironment::get();
2727
2728 my $authuser = $rpcenv->get_user();
2729
2730 my $node = extract_param($param, 'node');
2731
2732 my $vmid = extract_param($param, 'vmid');
2733
2734 my $newid = extract_param($param, 'newid');
2735
2736 my $pool = extract_param($param, 'pool');
2737
2738 if (defined($pool)) {
2739 $rpcenv->check_pool_exist($pool);
2740 }
2741
2742 my $snapname = extract_param($param, 'snapname');
2743
2744 my $storage = extract_param($param, 'storage');
2745
2746 my $format = extract_param($param, 'format');
2747
2748 my $target = extract_param($param, 'target');
2749
2750 my $localnode = PVE::INotify::nodename();
2751
2752 undef $target if $target && ($target eq $localnode || $target eq 'localhost');
2753
2754 PVE::Cluster::check_node_exists($target) if $target;
2755
2756 my $storecfg = PVE::Storage::config();
2757
2758 if ($storage) {
2759 # check if storage is enabled on local node
2760 PVE::Storage::storage_check_enabled($storecfg, $storage);
2761 if ($target) {
2762 # check if storage is available on target node
2763 PVE::Storage::storage_check_node($storecfg, $storage, $target);
2764 # clone only works if target storage is shared
2765 my $scfg = PVE::Storage::storage_config($storecfg, $storage);
2766 die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared};
2767 }
2768 }
2769
2770 PVE::Cluster::check_cfs_quorum();
2771
2772 my $running = PVE::QemuServer::check_running($vmid) || 0;
2773
2774 # exclusive lock if VM is running - else shared lock is enough;
2775 my $shared_lock = $running ? 0 : 1;
2776
2777 my $clonefn = sub {
2778
2779 # do all tests after lock
2780 # we also try to do all tests before we fork the worker
2781
2782 my $conf = PVE::QemuConfig->load_config($vmid);
2783
2784 PVE::QemuConfig->check_lock($conf);
2785
2786 my $verify_running = PVE::QemuServer::check_running($vmid) || 0;
2787
2788 die "unexpected state change\n" if $verify_running != $running;
2789
2790 die "snapshot '$snapname' does not exist\n"
2791 if $snapname && !defined( $conf->{snapshots}->{$snapname});
2792
2793 my $full = extract_param($param, 'full');
2794 if (!defined($full)) {
2795 $full = !PVE::QemuConfig->is_template($conf);
2796 }
2797
2798 die "parameter 'storage' not allowed for linked clones\n"
2799 if defined($storage) && !$full;
2800
2801 die "parameter 'format' not allowed for linked clones\n"
2802 if defined($format) && !$full;
2803
2804 my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
2805
2806 my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage);
2807
2808 die "can't clone VM to node '$target' (VM uses local storage)\n" if $target && !$sharedvm;
2809
2810 my $conffile = PVE::QemuConfig->config_file($newid);
2811
2812 die "unable to create VM $newid: config file already exists\n"
2813 if -f $conffile;
2814
2815 my $newconf = { lock => 'clone' };
2816 my $drives = {};
2817 my $fullclone = {};
2818 my $vollist = [];
2819
2820 foreach my $opt (keys %$oldconf) {
2821 my $value = $oldconf->{$opt};
2822
2823 # do not copy snapshot related info
2824 next if $opt eq 'snapshots' || $opt eq 'parent' || $opt eq 'snaptime' ||
2825 $opt eq 'vmstate' || $opt eq 'snapstate';
2826
2827 # no need to copy unused images, because VMID(owner) changes anyways
2828 next if $opt =~ m/^unused\d+$/;
2829
2830 # always change MAC! address
2831 if ($opt =~ m/^net(\d+)$/) {
2832 my $net = PVE::QemuServer::parse_net($value);
2833 my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
2834 $net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
2835 $newconf->{$opt} = PVE::QemuServer::print_net($net);
2836 } elsif (PVE::QemuServer::is_valid_drivename($opt)) {
2837 my $drive = PVE::QemuServer::parse_drive($opt, $value);
2838 die "unable to parse drive options for '$opt'\n" if !$drive;
2839 if (PVE::QemuServer::drive_is_cdrom($drive, 1)) {
2840 $newconf->{$opt} = $value; # simply copy configuration
2841 } else {
2842 if ($full || PVE::QemuServer::drive_is_cloudinit($drive)) {
2843 die "Full clone feature is not supported for drive '$opt'\n"
2844 if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running);
2845 $fullclone->{$opt} = 1;
2846 } else {
2847 # not full means clone instead of copy
2848 die "Linked clone feature is not supported for drive '$opt'\n"
2849 if !PVE::Storage::volume_has_feature($storecfg, 'clone', $drive->{file}, $snapname, $running);
2850 }
2851 $drives->{$opt} = $drive;
2852 push @$vollist, $drive->{file};
2853 }
2854 } else {
2855 # copy everything else
2856 $newconf->{$opt} = $value;
2857 }
2858 }
2859
2860 # auto generate a new uuid
2861 my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || '');
2862 $smbios1->{uuid} = PVE::QemuServer::generate_uuid();
2863 $newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1);
2864
2865 # auto generate a new vmgenid if the option was set
2866 if ($newconf->{vmgenid}) {
2867 $newconf->{vmgenid} = PVE::QemuServer::generate_uuid();
2868 }
2869
2870 delete $newconf->{template};
2871
2872 if ($param->{name}) {
2873 $newconf->{name} = $param->{name};
2874 } else {
2875 if ($oldconf->{name}) {
2876 $newconf->{name} = "Copy-of-$oldconf->{name}";
2877 } else {
2878 $newconf->{name} = "Copy-of-VM-$vmid";
2879 }
2880 }
2881
2882 if ($param->{description}) {
2883 $newconf->{description} = $param->{description};
2884 }
2885
2886 # create empty/temp config - this fails if VM already exists on other node
2887 PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n");
2888
2889 my $realcmd = sub {
2890 my $upid = shift;
2891
2892 my $newvollist = [];
2893 my $jobs = {};
2894
2895 eval {
2896 local $SIG{INT} =
2897 local $SIG{TERM} =
2898 local $SIG{QUIT} =
2899 local $SIG{HUP} = sub { die "interrupted by signal\n"; };
2900
2901 PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
2902
2903 my $bwlimit = extract_param($param, 'bwlimit');
2904
2905 my $total_jobs = scalar(keys %{$drives});
2906 my $i = 1;
2907
2908 foreach my $opt (keys %$drives) {
2909 my $drive = $drives->{$opt};
2910 my $skipcomplete = ($total_jobs != $i); # finish after last drive
2911
2912 my $src_sid = PVE::Storage::parse_volume_id($drive->{file});
2913 my $storage_list = [ $src_sid ];
2914 push @$storage_list, $storage if defined($storage);
2915 my $clonelimit = PVE::Storage::get_bandwidth_limit('clone', $storage_list, $bwlimit);
2916
2917 my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $opt, $drive, $snapname,
2918 $newid, $storage, $format, $fullclone->{$opt}, $newvollist,
2919 $jobs, $skipcomplete, $oldconf->{agent}, $clonelimit);
2920
2921 $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive);
2922
2923 PVE::QemuConfig->write_config($newid, $newconf);
2924 $i++;
2925 }
2926
2927 delete $newconf->{lock};
2928
2929 # do not write pending changes
2930 if (my @changes = keys %{$newconf->{pending}}) {
2931 my $pending = join(',', @changes);
2932 warn "found pending changes for '$pending', discarding for clone\n";
2933 delete $newconf->{pending};
2934 }
2935
2936 PVE::QemuConfig->write_config($newid, $newconf);
2937
2938 if ($target) {
2939 # always deactivate volumes - avoid lvm LVs to be active on several nodes
2940 PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running;
2941 PVE::Storage::deactivate_volumes($storecfg, $newvollist);
2942
2943 my $newconffile = PVE::QemuConfig->config_file($newid, $target);
2944 die "Failed to move config to node '$target' - rename failed: $!\n"
2945 if !rename($conffile, $newconffile);
2946 }
2947
2948 PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool;
2949 };
2950 if (my $err = $@) {
2951 unlink $conffile;
2952
2953 eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs) };
2954
2955 sleep 1; # some storage like rbd need to wait before release volume - really?
2956
2957 foreach my $volid (@$newvollist) {
2958 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
2959 warn $@ if $@;
2960 }
2961 die "clone failed: $err";
2962 }
2963
2964 return;
2965 };
2966
2967 PVE::Firewall::clone_vmfw_conf($vmid, $newid);
2968
2969 return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $realcmd);
2970 };
2971
2972 return PVE::QemuConfig->lock_config_mode($vmid, 1, $shared_lock, sub {
2973 # Aquire exclusive lock lock for $newid
2974 return PVE::QemuConfig->lock_config_full($newid, 1, $clonefn);
2975 });
2976
2977 }});
2978
2979 __PACKAGE__->register_method({
2980 name => 'move_vm_disk',
2981 path => '{vmid}/move_disk',
2982 method => 'POST',
2983 protected => 1,
2984 proxyto => 'node',
2985 description => "Move volume to different storage.",
2986 permissions => {
2987 description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, and 'Datastore.AllocateSpace' permissions on the storage.",
2988 check => [ 'and',
2989 ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
2990 ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]],
2991 ],
2992 },
2993 parameters => {
2994 additionalProperties => 0,
2995 properties => {
2996 node => get_standard_option('pve-node'),
2997 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
2998 disk => {
2999 type => 'string',
3000 description => "The disk you want to move.",
3001 enum => [ PVE::QemuServer::valid_drive_names() ],
3002 },
3003 storage => get_standard_option('pve-storage-id', {
3004 description => "Target storage.",
3005 completion => \&PVE::QemuServer::complete_storage,
3006 }),
3007 'format' => {
3008 type => 'string',
3009 description => "Target Format.",
3010 enum => [ 'raw', 'qcow2', 'vmdk' ],
3011 optional => 1,
3012 },
3013 delete => {
3014 type => 'boolean',
3015 description => "Delete the original disk after successful copy. By default the original disk is kept as unused disk.",
3016 optional => 1,
3017 default => 0,
3018 },
3019 digest => {
3020 type => 'string',
3021 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
3022 maxLength => 40,
3023 optional => 1,
3024 },
3025 bwlimit => {
3026 description => "Override I/O bandwidth limit (in KiB/s).",
3027 optional => 1,
3028 type => 'integer',
3029 minimum => '0',
3030 default => 'move limit from datacenter or storage config',
3031 },
3032 },
3033 },
3034 returns => {
3035 type => 'string',
3036 description => "the task ID.",
3037 },
3038 code => sub {
3039 my ($param) = @_;
3040
3041 my $rpcenv = PVE::RPCEnvironment::get();
3042
3043 my $authuser = $rpcenv->get_user();
3044
3045 my $node = extract_param($param, 'node');
3046
3047 my $vmid = extract_param($param, 'vmid');
3048
3049 my $digest = extract_param($param, 'digest');
3050
3051 my $disk = extract_param($param, 'disk');
3052
3053 my $storeid = extract_param($param, 'storage');
3054
3055 my $format = extract_param($param, 'format');
3056
3057 my $storecfg = PVE::Storage::config();
3058
3059 my $updatefn = sub {
3060
3061 my $conf = PVE::QemuConfig->load_config($vmid);
3062
3063 PVE::QemuConfig->check_lock($conf);
3064
3065 die "checksum missmatch (file change by other user?)\n"
3066 if $digest && $digest ne $conf->{digest};
3067
3068 die "disk '$disk' does not exist\n" if !$conf->{$disk};
3069
3070 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
3071
3072 my $old_volid = $drive->{file} || die "disk '$disk' has no associated volume\n";
3073
3074 die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive, 1);
3075
3076 my $oldfmt;
3077 my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($old_volid);
3078 if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){
3079 $oldfmt = $1;
3080 }
3081
3082 die "you can't move on the same storage with same format\n" if $oldstoreid eq $storeid &&
3083 (!$format || !$oldfmt || $oldfmt eq $format);
3084
3085 # this only checks snapshots because $disk is passed!
3086 my $snapshotted = PVE::QemuServer::is_volume_in_use($storecfg, $conf, $disk, $old_volid);
3087 die "you can't move a disk with snapshots and delete the source\n"
3088 if $snapshotted && $param->{delete};
3089
3090 PVE::Cluster::log_msg('info', $authuser, "move disk VM $vmid: move --disk $disk --storage $storeid");
3091
3092 my $running = PVE::QemuServer::check_running($vmid);
3093
3094 PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]);
3095
3096 my $realcmd = sub {
3097
3098 my $newvollist = [];
3099
3100 eval {
3101 local $SIG{INT} =
3102 local $SIG{TERM} =
3103 local $SIG{QUIT} =
3104 local $SIG{HUP} = sub { die "interrupted by signal\n"; };
3105
3106 warn "moving disk with snapshots, snapshots will not be moved!\n"
3107 if $snapshotted;
3108
3109 my $bwlimit = extract_param($param, 'bwlimit');
3110 my $movelimit = PVE::Storage::get_bandwidth_limit('move', [$oldstoreid, $storeid], $bwlimit);
3111
3112 my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $disk, $drive, undef,
3113 $vmid, $storeid, $format, 1, $newvollist, undef, undef, undef, $movelimit);
3114
3115 $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $newdrive);
3116
3117 PVE::QemuConfig->add_unused_volume($conf, $old_volid) if !$param->{delete};
3118
3119 # convert moved disk to base if part of template
3120 PVE::QemuServer::template_create($vmid, $conf, $disk)
3121 if PVE::QemuConfig->is_template($conf);
3122
3123 PVE::QemuConfig->write_config($vmid, $conf);
3124
3125 if ($running && PVE::QemuServer::parse_guest_agent($conf)->{fstrim_cloned_disks} && PVE::QemuServer::qga_check_running($vmid)) {
3126 eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fstrim"); };
3127 }
3128
3129 eval {
3130 # try to deactivate volumes - avoid lvm LVs to be active on several nodes
3131 PVE::Storage::deactivate_volumes($storecfg, [ $newdrive->{file} ])
3132 if !$running;
3133 };
3134 warn $@ if $@;
3135 };
3136 if (my $err = $@) {
3137
3138 foreach my $volid (@$newvollist) {
3139 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
3140 warn $@ if $@;
3141 }
3142 die "storage migration failed: $err";
3143 }
3144
3145 if ($param->{delete}) {
3146 eval {
3147 PVE::Storage::deactivate_volumes($storecfg, [$old_volid]);
3148 PVE::Storage::vdisk_free($storecfg, $old_volid);
3149 };
3150 warn $@ if $@;
3151 }
3152 };
3153
3154 return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd);
3155 };
3156
3157 return PVE::QemuConfig->lock_config($vmid, $updatefn);
3158 }});
3159
3160 my $check_vm_disks_local = sub {
3161 my ($storecfg, $vmconf, $vmid) = @_;
3162
3163 my $local_disks = {};
3164
3165 # add some more information to the disks e.g. cdrom
3166 PVE::QemuServer::foreach_volid($vmconf, sub {
3167 my ($volid, $attr) = @_;
3168
3169 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
3170 if ($storeid) {
3171 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
3172 return if $scfg->{shared};
3173 }
3174 # The shared attr here is just a special case where the vdisk
3175 # is marked as shared manually
3176 return if $attr->{shared};
3177 return if $attr->{cdrom} and $volid eq "none";
3178
3179 if (exists $local_disks->{$volid}) {
3180 @{$local_disks->{$volid}}{keys %$attr} = values %$attr
3181 } else {
3182 $local_disks->{$volid} = $attr;
3183 # ensure volid is present in case it's needed
3184 $local_disks->{$volid}->{volid} = $volid;
3185 }
3186 });
3187
3188 return $local_disks;
3189 };
3190
3191 __PACKAGE__->register_method({
3192 name => 'migrate_vm_precondition',
3193 path => '{vmid}/migrate',
3194 method => 'GET',
3195 protected => 1,
3196 proxyto => 'node',
3197 description => "Get preconditions for migration.",
3198 permissions => {
3199 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
3200 },
3201 parameters => {
3202 additionalProperties => 0,
3203 properties => {
3204 node => get_standard_option('pve-node'),
3205 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3206 target => get_standard_option('pve-node', {
3207 description => "Target node.",
3208 completion => \&PVE::Cluster::complete_migration_target,
3209 optional => 1,
3210 }),
3211 },
3212 },
3213 returns => {
3214 type => "object",
3215 properties => {
3216 running => { type => 'boolean' },
3217 allowed_nodes => {
3218 type => 'array',
3219 optional => 1,
3220 description => "List nodes allowed for offline migration, only passed if VM is offline"
3221 },
3222 not_allowed_nodes => {
3223 type => 'object',
3224 optional => 1,
3225 description => "List not allowed nodes with additional informations, only passed if VM is offline"
3226 },
3227 local_disks => {
3228 type => 'array',
3229 description => "List local disks including CD-Rom, unsused and not referenced disks"
3230 },
3231 local_resources => {
3232 type => 'array',
3233 description => "List local resources e.g. pci, usb"
3234 }
3235 },
3236 },
3237 code => sub {
3238 my ($param) = @_;
3239
3240 my $rpcenv = PVE::RPCEnvironment::get();
3241
3242 my $authuser = $rpcenv->get_user();
3243
3244 PVE::Cluster::check_cfs_quorum();
3245
3246 my $res = {};
3247
3248 my $vmid = extract_param($param, 'vmid');
3249 my $target = extract_param($param, 'target');
3250 my $localnode = PVE::INotify::nodename();
3251
3252
3253 # test if VM exists
3254 my $vmconf = PVE::QemuConfig->load_config($vmid);
3255 my $storecfg = PVE::Storage::config();
3256
3257
3258 # try to detect errors early
3259 PVE::QemuConfig->check_lock($vmconf);
3260
3261 $res->{running} = PVE::QemuServer::check_running($vmid) ? 1:0;
3262
3263 # if vm is not running, return target nodes where local storage is available
3264 # for offline migration
3265 if (!$res->{running}) {
3266 $res->{allowed_nodes} = [];
3267 my $checked_nodes = PVE::QemuServer::check_local_storage_availability($vmconf, $storecfg);
3268 delete $checked_nodes->{$localnode};
3269
3270 foreach my $node (keys %$checked_nodes) {
3271 if (!defined $checked_nodes->{$node}->{unavailable_storages}) {
3272 push @{$res->{allowed_nodes}}, $node;
3273 }
3274
3275 }
3276 $res->{not_allowed_nodes} = $checked_nodes;
3277 }
3278
3279
3280 my $local_disks = &$check_vm_disks_local($storecfg, $vmconf, $vmid);
3281 $res->{local_disks} = [ values %$local_disks ];;
3282
3283 my $local_resources = PVE::QemuServer::check_local_resources($vmconf, 1);
3284
3285 $res->{local_resources} = $local_resources;
3286
3287 return $res;
3288
3289
3290 }});
3291
3292 __PACKAGE__->register_method({
3293 name => 'migrate_vm',
3294 path => '{vmid}/migrate',
3295 method => 'POST',
3296 protected => 1,
3297 proxyto => 'node',
3298 description => "Migrate virtual machine. Creates a new migration task.",
3299 permissions => {
3300 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
3301 },
3302 parameters => {
3303 additionalProperties => 0,
3304 properties => {
3305 node => get_standard_option('pve-node'),
3306 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3307 target => get_standard_option('pve-node', {
3308 description => "Target node.",
3309 completion => \&PVE::Cluster::complete_migration_target,
3310 }),
3311 online => {
3312 type => 'boolean',
3313 description => "Use online/live migration.",
3314 optional => 1,
3315 },
3316 force => {
3317 type => 'boolean',
3318 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
3319 optional => 1,
3320 },
3321 migration_type => {
3322 type => 'string',
3323 enum => ['secure', 'insecure'],
3324 description => "Migration traffic is encrypted using an SSH tunnel by default. On secure, completely private networks this can be disabled to increase performance.",
3325 optional => 1,
3326 },
3327 migration_network => {
3328 type => 'string', format => 'CIDR',
3329 description => "CIDR of the (sub) network that is used for migration.",
3330 optional => 1,
3331 },
3332 "with-local-disks" => {
3333 type => 'boolean',
3334 description => "Enable live storage migration for local disk",
3335 optional => 1,
3336 },
3337 targetstorage => get_standard_option('pve-storage-id', {
3338 description => "Default target storage.",
3339 optional => 1,
3340 completion => \&PVE::QemuServer::complete_storage,
3341 }),
3342 bwlimit => {
3343 description => "Override I/O bandwidth limit (in KiB/s).",
3344 optional => 1,
3345 type => 'integer',
3346 minimum => '0',
3347 default => 'migrate limit from datacenter or storage config',
3348 },
3349 },
3350 },
3351 returns => {
3352 type => 'string',
3353 description => "the task ID.",
3354 },
3355 code => sub {
3356 my ($param) = @_;
3357
3358 my $rpcenv = PVE::RPCEnvironment::get();
3359 my $authuser = $rpcenv->get_user();
3360
3361 my $target = extract_param($param, 'target');
3362
3363 my $localnode = PVE::INotify::nodename();
3364 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
3365
3366 PVE::Cluster::check_cfs_quorum();
3367
3368 PVE::Cluster::check_node_exists($target);
3369
3370 my $targetip = PVE::Cluster::remote_node_ip($target);
3371
3372 my $vmid = extract_param($param, 'vmid');
3373
3374 raise_param_exc({ targetstorage => "Live storage migration can only be done online." })
3375 if !$param->{online} && $param->{targetstorage};
3376
3377 raise_param_exc({ force => "Only root may use this option." })
3378 if $param->{force} && $authuser ne 'root@pam';
3379
3380 raise_param_exc({ migration_type => "Only root may use this option." })
3381 if $param->{migration_type} && $authuser ne 'root@pam';
3382
3383 # allow root only until better network permissions are available
3384 raise_param_exc({ migration_network => "Only root may use this option." })
3385 if $param->{migration_network} && $authuser ne 'root@pam';
3386
3387 # test if VM exists
3388 my $conf = PVE::QemuConfig->load_config($vmid);
3389
3390 # try to detect errors early
3391
3392 PVE::QemuConfig->check_lock($conf);
3393
3394 if (PVE::QemuServer::check_running($vmid)) {
3395 die "cant migrate running VM without --online\n"
3396 if !$param->{online};
3397 }
3398
3399 my $storecfg = PVE::Storage::config();
3400
3401 if( $param->{targetstorage}) {
3402 PVE::Storage::storage_check_node($storecfg, $param->{targetstorage}, $target);
3403 } else {
3404 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target);
3405 }
3406
3407 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
3408
3409 my $hacmd = sub {
3410 my $upid = shift;
3411
3412 print "Requesting HA migration for VM $vmid to node $target\n";
3413
3414 my $cmd = ['ha-manager', 'migrate', "vm:$vmid", $target];
3415 PVE::Tools::run_command($cmd);
3416 return;
3417 };
3418
3419 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
3420
3421 } else {
3422
3423 my $realcmd = sub {
3424 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
3425 };
3426
3427 my $worker = sub {
3428 return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd);
3429 };
3430
3431 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $worker);
3432 }
3433
3434 }});
3435
3436 __PACKAGE__->register_method({
3437 name => 'monitor',
3438 path => '{vmid}/monitor',
3439 method => 'POST',
3440 protected => 1,
3441 proxyto => 'node',
3442 description => "Execute Qemu monitor commands.",
3443 permissions => {
3444 description => "Sys.Modify is required for (sub)commands which are not read-only ('info *' and 'help')",
3445 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
3446 },
3447 parameters => {
3448 additionalProperties => 0,
3449 properties => {
3450 node => get_standard_option('pve-node'),
3451 vmid => get_standard_option('pve-vmid'),
3452 command => {
3453 type => 'string',
3454 description => "The monitor command.",
3455 }
3456 },
3457 },
3458 returns => { type => 'string'},
3459 code => sub {
3460 my ($param) = @_;
3461
3462 my $rpcenv = PVE::RPCEnvironment::get();
3463 my $authuser = $rpcenv->get_user();
3464
3465 my $is_ro = sub {
3466 my $command = shift;
3467 return $command =~ m/^\s*info(\s+|$)/
3468 || $command =~ m/^\s*help\s*$/;
3469 };
3470
3471 $rpcenv->check_full($authuser, "/", ['Sys.Modify'])
3472 if !&$is_ro($param->{command});
3473
3474 my $vmid = $param->{vmid};
3475
3476 my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
3477
3478 my $res = '';
3479 eval {
3480 $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command});
3481 };
3482 $res = "ERROR: $@" if $@;
3483
3484 return $res;
3485 }});
3486
3487 __PACKAGE__->register_method({
3488 name => 'resize_vm',
3489 path => '{vmid}/resize',
3490 method => 'PUT',
3491 protected => 1,
3492 proxyto => 'node',
3493 description => "Extend volume size.",
3494 permissions => {
3495 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
3496 },
3497 parameters => {
3498 additionalProperties => 0,
3499 properties => {
3500 node => get_standard_option('pve-node'),
3501 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3502 skiplock => get_standard_option('skiplock'),
3503 disk => {
3504 type => 'string',
3505 description => "The disk you want to resize.",
3506 enum => [PVE::QemuServer::valid_drive_names()],
3507 },
3508 size => {
3509 type => 'string',
3510 pattern => '\+?\d+(\.\d+)?[KMGT]?',
3511 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.",
3512 },
3513 digest => {
3514 type => 'string',
3515 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
3516 maxLength => 40,
3517 optional => 1,
3518 },
3519 },
3520 },
3521 returns => { type => 'null'},
3522 code => sub {
3523 my ($param) = @_;
3524
3525 my $rpcenv = PVE::RPCEnvironment::get();
3526
3527 my $authuser = $rpcenv->get_user();
3528
3529 my $node = extract_param($param, 'node');
3530
3531 my $vmid = extract_param($param, 'vmid');
3532
3533 my $digest = extract_param($param, 'digest');
3534
3535 my $disk = extract_param($param, 'disk');
3536
3537 my $sizestr = extract_param($param, 'size');
3538
3539 my $skiplock = extract_param($param, 'skiplock');
3540 raise_param_exc({ skiplock => "Only root may use this option." })
3541 if $skiplock && $authuser ne 'root@pam';
3542
3543 my $storecfg = PVE::Storage::config();
3544
3545 my $updatefn = sub {
3546
3547 my $conf = PVE::QemuConfig->load_config($vmid);
3548
3549 die "checksum missmatch (file change by other user?)\n"
3550 if $digest && $digest ne $conf->{digest};
3551 PVE::QemuConfig->check_lock($conf) if !$skiplock;
3552
3553 die "disk '$disk' does not exist\n" if !$conf->{$disk};
3554
3555 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
3556
3557 my (undef, undef, undef, undef, undef, undef, $format) =
3558 PVE::Storage::parse_volname($storecfg, $drive->{file});
3559
3560 die "can't resize volume: $disk if snapshot exists\n"
3561 if %{$conf->{snapshots}} && $format eq 'qcow2';
3562
3563 my $volid = $drive->{file};
3564
3565 die "disk '$disk' has no associated volume\n" if !$volid;
3566
3567 die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
3568
3569 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
3570
3571 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
3572
3573 PVE::Storage::activate_volumes($storecfg, [$volid]);
3574 my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
3575
3576 die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
3577 my ($ext, $newsize, $unit) = ($1, $2, $4);
3578 if ($unit) {
3579 if ($unit eq 'K') {
3580 $newsize = $newsize * 1024;
3581 } elsif ($unit eq 'M') {
3582 $newsize = $newsize * 1024 * 1024;
3583 } elsif ($unit eq 'G') {
3584 $newsize = $newsize * 1024 * 1024 * 1024;
3585 } elsif ($unit eq 'T') {
3586 $newsize = $newsize * 1024 * 1024 * 1024 * 1024;
3587 }
3588 }
3589 $newsize += $size if $ext;
3590 $newsize = int($newsize);
3591
3592 die "shrinking disks is not supported\n" if $newsize < $size;
3593
3594 return if $size == $newsize;
3595
3596 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr");
3597
3598 PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize);
3599
3600 $drive->{size} = $newsize;
3601 $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive);
3602
3603 PVE::QemuConfig->write_config($vmid, $conf);
3604 };
3605
3606 PVE::QemuConfig->lock_config($vmid, $updatefn);
3607 return undef;
3608 }});
3609
3610 __PACKAGE__->register_method({
3611 name => 'snapshot_list',
3612 path => '{vmid}/snapshot',
3613 method => 'GET',
3614 description => "List all snapshots.",
3615 permissions => {
3616 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
3617 },
3618 proxyto => 'node',
3619 protected => 1, # qemu pid files are only readable by root
3620 parameters => {
3621 additionalProperties => 0,
3622 properties => {
3623 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3624 node => get_standard_option('pve-node'),
3625 },
3626 },
3627 returns => {
3628 type => 'array',
3629 items => {
3630 type => "object",
3631 properties => {
3632 name => {
3633 description => "Snapshot identifier. Value 'current' identifies the current VM.",
3634 type => 'string',
3635 },
3636 vmstate => {
3637 description => "Snapshot includes RAM.",
3638 type => 'boolean',
3639 optional => 1,
3640 },
3641 description => {
3642 description => "Snapshot description.",
3643 type => 'string',
3644 },
3645 snaptime => {
3646 description => "Snapshot creation time",
3647 type => 'integer',
3648 renderer => 'timestamp',
3649 optional => 1,
3650 },
3651 parent => {
3652 description => "Parent snapshot identifier.",
3653 type => 'string',
3654 optional => 1,
3655 },
3656 },
3657 },
3658 links => [ { rel => 'child', href => "{name}" } ],
3659 },
3660 code => sub {
3661 my ($param) = @_;
3662
3663 my $vmid = $param->{vmid};
3664
3665 my $conf = PVE::QemuConfig->load_config($vmid);
3666 my $snaphash = $conf->{snapshots} || {};
3667
3668 my $res = [];
3669
3670 foreach my $name (keys %$snaphash) {
3671 my $d = $snaphash->{$name};
3672 my $item = {
3673 name => $name,
3674 snaptime => $d->{snaptime} || 0,
3675 vmstate => $d->{vmstate} ? 1 : 0,
3676 description => $d->{description} || '',
3677 };
3678 $item->{parent} = $d->{parent} if $d->{parent};
3679 $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
3680 push @$res, $item;
3681 }
3682
3683 my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
3684 my $current = {
3685 name => 'current',
3686 digest => $conf->{digest},
3687 running => $running,
3688 description => "You are here!",
3689 };
3690 $current->{parent} = $conf->{parent} if $conf->{parent};
3691
3692 push @$res, $current;
3693
3694 return $res;
3695 }});
3696
3697 __PACKAGE__->register_method({
3698 name => 'snapshot',
3699 path => '{vmid}/snapshot',
3700 method => 'POST',
3701 protected => 1,
3702 proxyto => 'node',
3703 description => "Snapshot a VM.",
3704 permissions => {
3705 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
3706 },
3707 parameters => {
3708 additionalProperties => 0,
3709 properties => {
3710 node => get_standard_option('pve-node'),
3711 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3712 snapname => get_standard_option('pve-snapshot-name'),
3713 vmstate => {
3714 optional => 1,
3715 type => 'boolean',
3716 description => "Save the vmstate",
3717 },
3718 description => {
3719 optional => 1,
3720 type => 'string',
3721 description => "A textual description or comment.",
3722 },
3723 },
3724 },
3725 returns => {
3726 type => 'string',
3727 description => "the task ID.",
3728 },
3729 code => sub {
3730 my ($param) = @_;
3731
3732 my $rpcenv = PVE::RPCEnvironment::get();
3733
3734 my $authuser = $rpcenv->get_user();
3735
3736 my $node = extract_param($param, 'node');
3737
3738 my $vmid = extract_param($param, 'vmid');
3739
3740 my $snapname = extract_param($param, 'snapname');
3741
3742 die "unable to use snapshot name 'current' (reserved name)\n"
3743 if $snapname eq 'current';
3744
3745 my $realcmd = sub {
3746 PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
3747 PVE::QemuConfig->snapshot_create($vmid, $snapname, $param->{vmstate},
3748 $param->{description});
3749 };
3750
3751 return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd);
3752 }});
3753
3754 __PACKAGE__->register_method({
3755 name => 'snapshot_cmd_idx',
3756 path => '{vmid}/snapshot/{snapname}',
3757 description => '',
3758 method => 'GET',
3759 permissions => {
3760 user => 'all',
3761 },
3762 parameters => {
3763 additionalProperties => 0,
3764 properties => {
3765 vmid => get_standard_option('pve-vmid'),
3766 node => get_standard_option('pve-node'),
3767 snapname => get_standard_option('pve-snapshot-name'),
3768 },
3769 },
3770 returns => {
3771 type => 'array',
3772 items => {
3773 type => "object",
3774 properties => {},
3775 },
3776 links => [ { rel => 'child', href => "{cmd}" } ],
3777 },
3778 code => sub {
3779 my ($param) = @_;
3780
3781 my $res = [];
3782
3783 push @$res, { cmd => 'rollback' };
3784 push @$res, { cmd => 'config' };
3785
3786 return $res;
3787 }});
3788
3789 __PACKAGE__->register_method({
3790 name => 'update_snapshot_config',
3791 path => '{vmid}/snapshot/{snapname}/config',
3792 method => 'PUT',
3793 protected => 1,
3794 proxyto => 'node',
3795 description => "Update snapshot metadata.",
3796 permissions => {
3797 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
3798 },
3799 parameters => {
3800 additionalProperties => 0,
3801 properties => {
3802 node => get_standard_option('pve-node'),
3803 vmid => get_standard_option('pve-vmid'),
3804 snapname => get_standard_option('pve-snapshot-name'),
3805 description => {
3806 optional => 1,
3807 type => 'string',
3808 description => "A textual description or comment.",
3809 },
3810 },
3811 },
3812 returns => { type => 'null' },
3813 code => sub {
3814 my ($param) = @_;
3815
3816 my $rpcenv = PVE::RPCEnvironment::get();
3817
3818 my $authuser = $rpcenv->get_user();
3819
3820 my $vmid = extract_param($param, 'vmid');
3821
3822 my $snapname = extract_param($param, 'snapname');
3823
3824 return undef if !defined($param->{description});
3825
3826 my $updatefn = sub {
3827
3828 my $conf = PVE::QemuConfig->load_config($vmid);
3829
3830 PVE::QemuConfig->check_lock($conf);
3831
3832 my $snap = $conf->{snapshots}->{$snapname};
3833
3834 die "snapshot '$snapname' does not exist\n" if !defined($snap);
3835
3836 $snap->{description} = $param->{description} if defined($param->{description});
3837
3838 PVE::QemuConfig->write_config($vmid, $conf);
3839 };
3840
3841 PVE::QemuConfig->lock_config($vmid, $updatefn);
3842
3843 return undef;
3844 }});
3845
3846 __PACKAGE__->register_method({
3847 name => 'get_snapshot_config',
3848 path => '{vmid}/snapshot/{snapname}/config',
3849 method => 'GET',
3850 proxyto => 'node',
3851 description => "Get snapshot configuration",
3852 permissions => {
3853 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot', 'VM.Snapshot.Rollback' ], any => 1],
3854 },
3855 parameters => {
3856 additionalProperties => 0,
3857 properties => {
3858 node => get_standard_option('pve-node'),
3859 vmid => get_standard_option('pve-vmid'),
3860 snapname => get_standard_option('pve-snapshot-name'),
3861 },
3862 },
3863 returns => { type => "object" },
3864 code => sub {
3865 my ($param) = @_;
3866
3867 my $rpcenv = PVE::RPCEnvironment::get();
3868
3869 my $authuser = $rpcenv->get_user();
3870
3871 my $vmid = extract_param($param, 'vmid');
3872
3873 my $snapname = extract_param($param, 'snapname');
3874
3875 my $conf = PVE::QemuConfig->load_config($vmid);
3876
3877 my $snap = $conf->{snapshots}->{$snapname};
3878
3879 die "snapshot '$snapname' does not exist\n" if !defined($snap);
3880
3881 return $snap;
3882 }});
3883
3884 __PACKAGE__->register_method({
3885 name => 'rollback',
3886 path => '{vmid}/snapshot/{snapname}/rollback',
3887 method => 'POST',
3888 protected => 1,
3889 proxyto => 'node',
3890 description => "Rollback VM state to specified snapshot.",
3891 permissions => {
3892 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot', 'VM.Snapshot.Rollback' ], any => 1],
3893 },
3894 parameters => {
3895 additionalProperties => 0,
3896 properties => {
3897 node => get_standard_option('pve-node'),
3898 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3899 snapname => get_standard_option('pve-snapshot-name'),
3900 },
3901 },
3902 returns => {
3903 type => 'string',
3904 description => "the task ID.",
3905 },
3906 code => sub {
3907 my ($param) = @_;
3908
3909 my $rpcenv = PVE::RPCEnvironment::get();
3910
3911 my $authuser = $rpcenv->get_user();
3912
3913 my $node = extract_param($param, 'node');
3914
3915 my $vmid = extract_param($param, 'vmid');
3916
3917 my $snapname = extract_param($param, 'snapname');
3918
3919 my $realcmd = sub {
3920 PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname");
3921 PVE::QemuConfig->snapshot_rollback($vmid, $snapname);
3922 };
3923
3924 my $worker = sub {
3925 # hold migration lock, this makes sure that nobody create replication snapshots
3926 return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd);
3927 };
3928
3929 return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $worker);
3930 }});
3931
3932 __PACKAGE__->register_method({
3933 name => 'delsnapshot',
3934 path => '{vmid}/snapshot/{snapname}',
3935 method => 'DELETE',
3936 protected => 1,
3937 proxyto => 'node',
3938 description => "Delete a VM snapshot.",
3939 permissions => {
3940 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
3941 },
3942 parameters => {
3943 additionalProperties => 0,
3944 properties => {
3945 node => get_standard_option('pve-node'),
3946 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
3947 snapname => get_standard_option('pve-snapshot-name'),
3948 force => {
3949 optional => 1,
3950 type => 'boolean',
3951 description => "For removal from config file, even if removing disk snapshots fails.",
3952 },
3953 },
3954 },
3955 returns => {
3956 type => 'string',
3957 description => "the task ID.",
3958 },
3959 code => sub {
3960 my ($param) = @_;
3961
3962 my $rpcenv = PVE::RPCEnvironment::get();
3963
3964 my $authuser = $rpcenv->get_user();
3965
3966 my $node = extract_param($param, 'node');
3967
3968 my $vmid = extract_param($param, 'vmid');
3969
3970 my $snapname = extract_param($param, 'snapname');
3971
3972 my $realcmd = sub {
3973 PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
3974 PVE::QemuConfig->snapshot_delete($vmid, $snapname, $param->{force});
3975 };
3976
3977 return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);
3978 }});
3979
3980 __PACKAGE__->register_method({
3981 name => 'template',
3982 path => '{vmid}/template',
3983 method => 'POST',
3984 protected => 1,
3985 proxyto => 'node',
3986 description => "Create a Template.",
3987 permissions => {
3988 description => "You need 'VM.Allocate' permissions on /vms/{vmid}",
3989 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
3990 },
3991 parameters => {
3992 additionalProperties => 0,
3993 properties => {
3994 node => get_standard_option('pve-node'),
3995 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }),
3996 disk => {
3997 optional => 1,
3998 type => 'string',
3999 description => "If you want to convert only 1 disk to base image.",
4000 enum => [PVE::QemuServer::valid_drive_names()],
4001 },
4002
4003 },
4004 },
4005 returns => { type => 'null'},
4006 code => sub {
4007 my ($param) = @_;
4008
4009 my $rpcenv = PVE::RPCEnvironment::get();
4010
4011 my $authuser = $rpcenv->get_user();
4012
4013 my $node = extract_param($param, 'node');
4014
4015 my $vmid = extract_param($param, 'vmid');
4016
4017 my $disk = extract_param($param, 'disk');
4018
4019 my $updatefn = sub {
4020
4021 my $conf = PVE::QemuConfig->load_config($vmid);
4022
4023 PVE::QemuConfig->check_lock($conf);
4024
4025 die "unable to create template, because VM contains snapshots\n"
4026 if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}});
4027
4028 die "you can't convert a template to a template\n"
4029 if PVE::QemuConfig->is_template($conf) && !$disk;
4030
4031 die "you can't convert a VM to template if VM is running\n"
4032 if PVE::QemuServer::check_running($vmid);
4033
4034 my $realcmd = sub {
4035 PVE::QemuServer::template_create($vmid, $conf, $disk);
4036 };
4037
4038 $conf->{template} = 1;
4039 PVE::QemuConfig->write_config($vmid, $conf);
4040
4041 return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
4042 };
4043
4044 PVE::QemuConfig->lock_config($vmid, $updatefn);
4045 return undef;
4046 }});
4047
4048 __PACKAGE__->register_method({
4049 name => 'cloudinit_generated_config_dump',
4050 path => '{vmid}/cloudinit/dump',
4051 method => 'GET',
4052 proxyto => 'node',
4053 description => "Get automatically generated cloudinit config.",
4054 permissions => {
4055 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
4056 },
4057 parameters => {
4058 additionalProperties => 0,
4059 properties => {
4060 node => get_standard_option('pve-node'),
4061 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
4062 type => {
4063 description => 'Config type.',
4064 type => 'string',
4065 enum => ['user', 'network', 'meta'],
4066 },
4067 },
4068 },
4069 returns => {
4070 type => 'string',
4071 },
4072 code => sub {
4073 my ($param) = @_;
4074
4075 my $conf = PVE::QemuConfig->load_config($param->{vmid});
4076
4077 return PVE::QemuServer::Cloudinit::dump_cloudinit_config($conf, $param->{vmid}, $param->{type});
4078 }});
4079
4080 1;