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