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