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