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