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