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