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