]> git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu.pm
cleanup update_vm - carefully reload config after changes
[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
7 use PVE::Cluster;
8 use PVE::SafeSyslog;
9 use PVE::Tools qw(extract_param);
10 use PVE::Exception qw(raise raise_param_exc);
11 use PVE::Storage;
12 use PVE::JSONSchema qw(get_standard_option);
13 use PVE::RESTHandler;
14 use PVE::QemuServer;
15 use PVE::QemuMigrate;
16 use PVE::RPCEnvironment;
17 use PVE::AccessControl;
18 use PVE::INotify;
19
20 use Data::Dumper; # fixme: remove
21
22 use base qw(PVE::RESTHandler);
23
24 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.";
25
26 my $resolve_cdrom_alias = sub {
27 my $param = shift;
28
29 if (my $value = $param->{cdrom}) {
30 $value .= ",media=cdrom" if $value !~ m/media=/;
31 $param->{ide2} = $value;
32 delete $param->{cdrom};
33 }
34 };
35
36 my $check_volume_access = sub {
37 my ($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool) = @_;
38
39 my $path;
40 if (my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1)) {
41 my ($ownervm, $vtype);
42 ($path, $ownervm, $vtype) = PVE::Storage::path($storecfg, $volid);
43 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
44 # we simply allow access
45 } elsif (!$ownervm || ($ownervm != $vmid)) {
46 # allow if we are Datastore administrator
47 $rpcenv->check_storage_perm($authuser, $vmid, $pool, $sid, [ 'Datastore.Allocate' ]);
48 }
49 } else {
50 die "Only root can pass arbitrary filesystem paths."
51 if $authuser ne 'root@pam';
52
53 $path = abs_path($volid);
54 }
55 return $path;
56 };
57
58 # Note: $pool is only needed when creating a VM, because pool permissions
59 # are automatically inherited if VM already exists inside a pool.
60 my $create_disks = sub {
61 my ($rpcenv, $authuser, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
62
63 # check permissions first
64
65 my $alloc = [];
66 foreach_drive($settings, sub {
67 my ($ds, $disk) = @_;
68
69 return if drive_is_cdrom($disk);
70
71 my $volid = $disk->{file};
72
73 if ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
74 my ($storeid, $size) = ($2 || $default_storage, $3);
75 die "no storage ID specified (and no default storage)\n" if !$storeid;
76 $rpcenv->check_storage_perm($authuser, $vmid, $pool, $storeid, [ 'Datastore.AllocateSpace' ]);
77 my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
78 my $fmt = $disk->{format} || $defformat;
79 push @$alloc, [$ds, $disk, $storeid, $size, $fmt];
80 } else {
81 my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid, $pool);
82 die "image '$path' does not exists\n" if (!(-f $path || -b $path));
83 }
84 });
85
86 # now try to allocate everything
87
88 my $vollist = [];
89 eval {
90 foreach my $task (@$alloc) {
91 my ($ds, $disk, $storeid, $size, $fmt) = @$task;
92
93 my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid,
94 $fmt, undef, $size*1024*1024);
95
96 $disk->{file} = $volid;
97 push @$vollist, $volid;
98 }
99 };
100
101 # free allocated images on error
102 if (my $err = $@) {
103 syslog('err', "VM $vmid creating disks failed");
104 foreach my $volid (@$vollist) {
105 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
106 warn $@ if $@;
107 }
108 die $err;
109 }
110
111 # modify vm config if everything went well
112 foreach my $task (@$alloc) {
113 my ($ds, $disk) = @$task;
114 delete $disk->{format}; # no longer needed
115 $settings->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
116 }
117
118 return $vollist;
119 };
120
121 my $check_vm_modify_config_perm = sub {
122 my ($rpcenv, $authuser, $vmid, $pool, $param) = @_;
123
124 return 1 if $authuser ne 'root@pam';
125
126 foreach my $opt (keys %$param) {
127 # disk checks need to be done somewhere else
128 next if PVE::QemuServer::valid_drivename($opt);
129
130 if ($opt eq 'sockets' || $opt eq 'cores' ||
131 $opt eq 'cpu' || $opt eq 'smp' ||
132 $opt eq 'cpuimit' || $opt eq 'cpuunits') {
133 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
134 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
135 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
136 } elsif ($opt eq 'memory' || $opt eq 'balloon') {
137 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
138 } elsif ($opt eq 'args' || $opt eq 'lock') {
139 die "only root can set '$opt' config\n";
140 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
141 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
142 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
143 } elsif ($opt =~ m/^net\d+$/) {
144 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
145 } else {
146 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
147 }
148 }
149
150 return 1;
151 };
152
153 __PACKAGE__->register_method({
154 name => 'vmlist',
155 path => '',
156 method => 'GET',
157 description => "Virtual machine index (per node).",
158 permissions => {
159 description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
160 user => 'all',
161 },
162 proxyto => 'node',
163 protected => 1, # qemu pid files are only readable by root
164 parameters => {
165 additionalProperties => 0,
166 properties => {
167 node => get_standard_option('pve-node'),
168 },
169 },
170 returns => {
171 type => 'array',
172 items => {
173 type => "object",
174 properties => {},
175 },
176 links => [ { rel => 'child', href => "{vmid}" } ],
177 },
178 code => sub {
179 my ($param) = @_;
180
181 my $rpcenv = PVE::RPCEnvironment::get();
182 my $authuser = $rpcenv->get_user();
183
184 my $vmstatus = PVE::QemuServer::vmstatus();
185
186 my $res = [];
187 foreach my $vmid (keys %$vmstatus) {
188 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
189
190 my $data = $vmstatus->{$vmid};
191 $data->{vmid} = $vmid;
192 push @$res, $data;
193 }
194
195 return $res;
196 }});
197
198 __PACKAGE__->register_method({
199 name => 'create_vm',
200 path => '',
201 method => 'POST',
202 description => "Create or restore a virtual machine.",
203 permissions => {
204 description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. If you create disks you need 'Datastore.AllocateSpace' on any used storage.",
205 check => [ 'or',
206 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
207 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
208 ],
209 },
210 protected => 1,
211 proxyto => 'node',
212 parameters => {
213 additionalProperties => 0,
214 properties => PVE::QemuServer::json_config_properties(
215 {
216 node => get_standard_option('pve-node'),
217 vmid => get_standard_option('pve-vmid'),
218 archive => {
219 description => "The backup file.",
220 type => 'string',
221 optional => 1,
222 maxLength => 255,
223 },
224 storage => get_standard_option('pve-storage-id', {
225 description => "Default storage.",
226 optional => 1,
227 }),
228 force => {
229 optional => 1,
230 type => 'boolean',
231 description => "Allow to overwrite existing VM.",
232 requires => 'archive',
233 },
234 unique => {
235 optional => 1,
236 type => 'boolean',
237 description => "Assign a unique random ethernet address.",
238 requires => 'archive',
239 },
240 pool => {
241 optional => 1,
242 type => 'string', format => 'pve-poolid',
243 description => "Add the VM to the specified pool.",
244 },
245 }),
246 },
247 returns => {
248 type => 'string',
249 },
250 code => sub {
251 my ($param) = @_;
252
253 my $rpcenv = PVE::RPCEnvironment::get();
254
255 my $authuser = $rpcenv->get_user();
256
257 my $node = extract_param($param, 'node');
258
259 my $vmid = extract_param($param, 'vmid');
260
261 my $archive = extract_param($param, 'archive');
262
263 my $storage = extract_param($param, 'storage');
264
265 my $force = extract_param($param, 'force');
266
267 my $unique = extract_param($param, 'unique');
268
269 my $pool = extract_param($param, 'pool');
270
271 my $filename = PVE::QemuServer::config_file($vmid);
272
273 my $storecfg = PVE::Storage::config();
274
275 PVE::Cluster::check_cfs_quorum();
276
277 if (defined($pool)) {
278 $rpcenv->check_pool_exist($pool);
279 $rpcenv->check_perm_modify($authuser, "/pool/$pool");
280 }
281
282 $rpcenv->check_storage_perm($authuser, $vmid, $pool, $storage, [ 'Datastore.AllocateSpace' ])
283 if defined($storage);
284
285 if (!$archive) {
286 &$resolve_cdrom_alias($param);
287
288 foreach my $opt (keys %$param) {
289 if (PVE::QemuServer::valid_drivename($opt)) {
290 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
291 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
292
293 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
294 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
295 }
296 }
297
298 PVE::QemuServer::add_random_macs($param);
299 } else {
300 my $keystr = join(' ', keys %$param);
301 raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
302
303 if ($archive eq '-') {
304 die "pipe requires cli environment\n"
305 && $rpcenv->{type} ne 'cli';
306 } else {
307 my $path = &$check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $archive, $pool);
308 die "can't find archive file '$archive'\n" if !($path && -f $path);
309 $archive = $path;
310 }
311 }
312
313 my $restorefn = sub {
314
315 if (-f $filename) {
316 die "unable to restore vm $vmid: config file already exists\n"
317 if !$force;
318
319 die "unable to restore vm $vmid: vm is running\n"
320 if PVE::QemuServer::check_running($vmid);
321
322 # destroy existing data - keep empty config
323 PVE::QemuServer::destroy_vm($storecfg, $vmid, 1);
324 }
325
326 my $realcmd = sub {
327 PVE::QemuServer::restore_archive($archive, $vmid, $authuser, {
328 storage => $storage,
329 pool => $pool,
330 unique => $unique });
331 };
332
333 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
334 };
335
336 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, $param);
337
338 my $createfn = sub {
339
340 # second test (after locking test is accurate)
341 die "unable to create vm $vmid: config file already exists\n"
342 if -f $filename;
343
344 my $realcmd = sub {
345
346 my $vollist = [];
347
348 eval {
349 $vollist = &$create_disks($rpcenv, $authuser, $storecfg, $vmid, $pool, $param, $storage);
350
351 # try to be smart about bootdisk
352 my @disks = PVE::QemuServer::disknames();
353 my $firstdisk;
354 foreach my $ds (reverse @disks) {
355 next if !$param->{$ds};
356 my $disk = PVE::QemuServer::parse_drive($ds, $param->{$ds});
357 next if PVE::QemuServer::drive_is_cdrom($disk);
358 $firstdisk = $ds;
359 }
360
361 if (!$param->{bootdisk} && $firstdisk) {
362 $param->{bootdisk} = $firstdisk;
363 }
364
365 PVE::QemuServer::create_conf_nolock($vmid, $param);
366 };
367 my $err = $@;
368
369 if ($err) {
370 foreach my $volid (@$vollist) {
371 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
372 warn $@ if $@;
373 }
374 die "create failed - $err";
375 }
376 };
377
378 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
379 };
380
381 return PVE::QemuServer::lock_config($vmid, $archive ? $restorefn : $createfn);
382 }});
383
384 __PACKAGE__->register_method({
385 name => 'vmdiridx',
386 path => '{vmid}',
387 method => 'GET',
388 proxyto => 'node',
389 description => "Directory index",
390 permissions => {
391 user => 'all',
392 },
393 parameters => {
394 additionalProperties => 0,
395 properties => {
396 node => get_standard_option('pve-node'),
397 vmid => get_standard_option('pve-vmid'),
398 },
399 },
400 returns => {
401 type => 'array',
402 items => {
403 type => "object",
404 properties => {
405 subdir => { type => 'string' },
406 },
407 },
408 links => [ { rel => 'child', href => "{subdir}" } ],
409 },
410 code => sub {
411 my ($param) = @_;
412
413 my $res = [
414 { subdir => 'config' },
415 { subdir => 'status' },
416 { subdir => 'unlink' },
417 { subdir => 'vncproxy' },
418 { subdir => 'migrate' },
419 { subdir => 'rrd' },
420 { subdir => 'rrddata' },
421 { subdir => 'monitor' },
422 ];
423
424 return $res;
425 }});
426
427 __PACKAGE__->register_method({
428 name => 'rrd',
429 path => '{vmid}/rrd',
430 method => 'GET',
431 protected => 1, # fixme: can we avoid that?
432 permissions => {
433 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
434 },
435 description => "Read VM RRD statistics (returns PNG)",
436 parameters => {
437 additionalProperties => 0,
438 properties => {
439 node => get_standard_option('pve-node'),
440 vmid => get_standard_option('pve-vmid'),
441 timeframe => {
442 description => "Specify the time frame you are interested in.",
443 type => 'string',
444 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
445 },
446 ds => {
447 description => "The list of datasources you want to display.",
448 type => 'string', format => 'pve-configid-list',
449 },
450 cf => {
451 description => "The RRD consolidation function",
452 type => 'string',
453 enum => [ 'AVERAGE', 'MAX' ],
454 optional => 1,
455 },
456 },
457 },
458 returns => {
459 type => "object",
460 properties => {
461 filename => { type => 'string' },
462 },
463 },
464 code => sub {
465 my ($param) = @_;
466
467 return PVE::Cluster::create_rrd_graph(
468 "pve2-vm/$param->{vmid}", $param->{timeframe},
469 $param->{ds}, $param->{cf});
470
471 }});
472
473 __PACKAGE__->register_method({
474 name => 'rrddata',
475 path => '{vmid}/rrddata',
476 method => 'GET',
477 protected => 1, # fixme: can we avoid that?
478 permissions => {
479 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
480 },
481 description => "Read VM RRD statistics",
482 parameters => {
483 additionalProperties => 0,
484 properties => {
485 node => get_standard_option('pve-node'),
486 vmid => get_standard_option('pve-vmid'),
487 timeframe => {
488 description => "Specify the time frame you are interested in.",
489 type => 'string',
490 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
491 },
492 cf => {
493 description => "The RRD consolidation function",
494 type => 'string',
495 enum => [ 'AVERAGE', 'MAX' ],
496 optional => 1,
497 },
498 },
499 },
500 returns => {
501 type => "array",
502 items => {
503 type => "object",
504 properties => {},
505 },
506 },
507 code => sub {
508 my ($param) = @_;
509
510 return PVE::Cluster::create_rrd_data(
511 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
512 }});
513
514
515 __PACKAGE__->register_method({
516 name => 'vm_config',
517 path => '{vmid}/config',
518 method => 'GET',
519 proxyto => 'node',
520 description => "Get virtual machine configuration.",
521 permissions => {
522 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
523 },
524 parameters => {
525 additionalProperties => 0,
526 properties => {
527 node => get_standard_option('pve-node'),
528 vmid => get_standard_option('pve-vmid'),
529 },
530 },
531 returns => {
532 type => "object",
533 properties => {
534 digest => {
535 type => 'string',
536 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
537 }
538 },
539 },
540 code => sub {
541 my ($param) = @_;
542
543 my $conf = PVE::QemuServer::load_config($param->{vmid});
544
545 return $conf;
546 }});
547
548 my $delete_drive = sub {
549 my ($conf, $storecfg, $vmid, $drive, $force) = @_;
550
551 my $changes = {};
552
553 if (!PVE::QemuServer::drive_is_cdrom($drive)) {
554 my $volid = $drive->{file};
555
556 if ($volid !~ m|^/|) {
557 my ($path, $owner);
558 eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
559 if ($owner && ($owner == $vmid)) {
560 if ($force) {
561 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
562 warn $@ if $@;
563 } else {
564 PVE::QemuServer::add_unused_volume($conf, $volid, $vmid, $changes);
565 }
566 }
567 }
568 } else {
569
570 }
571
572 return $changes;
573 };
574
575 my $vm_config_perm_list = [
576 'VM.Config.Disk',
577 'VM.Config.CDROM',
578 'VM.Config.CPU',
579 'VM.Config.Memory',
580 'VM.Config.Network',
581 'VM.Config.HWType',
582 'VM.Config.Options',
583 ];
584
585 __PACKAGE__->register_method({
586 name => 'update_vm',
587 path => '{vmid}/config',
588 method => 'PUT',
589 protected => 1,
590 proxyto => 'node',
591 description => "Set virtual machine options.",
592 permissions => {
593 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
594 },
595 parameters => {
596 additionalProperties => 0,
597 properties => PVE::QemuServer::json_config_properties(
598 {
599 node => get_standard_option('pve-node'),
600 vmid => get_standard_option('pve-vmid'),
601 skiplock => get_standard_option('skiplock'),
602 delete => {
603 type => 'string', format => 'pve-configid-list',
604 description => "A list of settings you want to delete.",
605 optional => 1,
606 },
607 force => {
608 type => 'boolean',
609 description => $opt_force_description,
610 optional => 1,
611 requires => 'delete',
612 },
613 digest => {
614 type => 'string',
615 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
616 maxLength => 40,
617 optional => 1,
618 }
619 }),
620 },
621 returns => { type => 'null'},
622 code => sub {
623 my ($param) = @_;
624
625 my $rpcenv = PVE::RPCEnvironment::get();
626
627 my $authuser = $rpcenv->get_user();
628
629 my $node = extract_param($param, 'node');
630
631 my $vmid = extract_param($param, 'vmid');
632
633 my $digest = extract_param($param, 'digest');
634
635 my @paramarr = (); # used for log message
636 foreach my $key (keys %$param) {
637 push @paramarr, "-$key", $param->{$key};
638 }
639
640 my $skiplock = extract_param($param, 'skiplock');
641 raise_param_exc({ skiplock => "Only root may use this option." })
642 if $skiplock && $authuser ne 'root@pam';
643
644 my $delete_str = extract_param($param, 'delete');
645
646 my $force = extract_param($param, 'force');
647
648 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
649
650 my $storecfg = PVE::Storage::config();
651
652 &$resolve_cdrom_alias($param);
653
654 # now try to verify all parameters
655
656 my @delete = ();
657 foreach my $opt (PVE::Tools::split_list($delete_str)) {
658 $opt = 'ide2' if $opt eq 'cdrom';
659 raise_param_exc({ delete => "you can't use '-$opt' and " .
660 "-delete $opt' at the same time" })
661 if defined($param->{$opt});
662
663 if (!PVE::QemuServer::option_exists($opt)) {
664 raise_param_exc({ delete => "unknown option '$opt'" });
665 }
666 push @delete, $opt;
667 }
668
669 my $drive_hash = {};
670 my $net_hash = {};
671 foreach my $opt (keys %$param) {
672 if (PVE::QemuServer::valid_drivename($opt)) {
673 # cleanup drive path
674 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
675 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
676 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
677 $drive_hash->{$opt} = $drive;
678 } elsif ($opt =~ m/^net(\d+)$/) {
679 # add macaddr
680 my $net = PVE::QemuServer::parse_net($param->{$opt});
681 $param->{$opt} = PVE::QemuServer::print_net($net);
682 $net_hash->{$opt} = $net;
683 }
684 }
685
686
687 my $updatefn = sub {
688
689 my $conf = PVE::QemuServer::load_config($vmid);
690
691 die "checksum missmatch (file change by other user?)\n"
692 if $digest && $digest ne $conf->{digest};
693
694 PVE::QemuServer::check_lock($conf) if !$skiplock;
695
696 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
697
698 foreach my $opt (@delete) { # delete
699
700 $conf = PVE::QemuServer::load_config($vmid); # update/reload
701
702 next if !defined($conf->{$opt});
703
704 die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
705
706 my $changes = {};
707
708 if (my $drive = $drive_hash->{$opt}) { # drives
709 $changes = &$delete_drive($conf, $storecfg, $vmid, $drive, $force);
710 } elsif ($opt =~ m/^unused/) {
711 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
712 my $volid = $drive->{file};
713 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
714 warn $@ if $@;
715 }
716
717 PVE::QemuServer::change_config_nolock($vmid, $changes, { $opt => 1 }, 1);
718 }
719
720 foreach my $opt (keys %$param) { # add/change
721
722 $conf = PVE::QemuServer::load_config($vmid); # update/reload
723
724 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
725
726 if (my $drive = $drive_hash->{$opt}) { # drives
727
728 my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}) if $conf->{$opt};
729
730 if ($old_drive && ($drive->{file} ne $old_drive->{file}) &&
731 !PVE::QemuServer::drive_is_cdrom($old_drive)) { # delete old disks
732
733 die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
734
735 my $changes = &$delete_drive($conf, $storecfg, $vmid, $old_drive, 0);
736 PVE::QemuServer::change_config_nolock($vmid, $changes, { $opt => 1 }, 1);
737 $conf = PVE::QemuServer::load_config($vmid); # update/reload
738 }
739
740 if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom
741
742 if (PVE::QemuServer::check_running($vmid)) {
743 if ($drive->{file} eq 'none') {
744 PVE::QemuServer::vm_monitor_command($vmid, "eject -f drive-$opt", 0);
745 } else {
746 my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
747 PVE::QemuServer::vm_monitor_command($vmid, "eject -f drive-$opt", 0); #force eject if locked
748 PVE::QemuServer::vm_monitor_command($vmid, "change drive-$opt \"$path\"", 0) if $path;
749 }
750 }
751
752 PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
753
754 } else { #hdd
755
756 my $settings = { $opt => $param->{$opt} };
757 &$create_disks($rpcenv, $authuser, $storecfg, $vmid, undef, $settings);
758 PVE::QemuServer::change_config_nolock($vmid, { $opt => $settings->{$opt} }, {}, 1);
759 $conf = PVE::QemuServer::load_config($vmid); # update/reload
760
761 my $newdrive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
762
763 #hotplug new disks
764 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $newdrive);
765 }
766
767 } elsif (my $net = $net_hash->{$opt}) { #nics
768
769 #if online update, then unplug first
770 die "error hot-unplug $opt for update" if $conf->{$opt} &&
771 !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
772
773 PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
774 $conf = PVE::QemuServer::load_config($vmid); # update/reload
775
776 my $newnet = PVE::QemuServer::parse_net($conf->{$opt});
777 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet);
778
779 } else {
780
781 PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
782 }
783 }
784 };
785
786 PVE::QemuServer::lock_config($vmid, $updatefn);
787
788 return undef;
789 }});
790
791
792 __PACKAGE__->register_method({
793 name => 'destroy_vm',
794 path => '{vmid}',
795 method => 'DELETE',
796 protected => 1,
797 proxyto => 'node',
798 description => "Destroy the vm (also delete all used/owned volumes).",
799 permissions => {
800 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
801 },
802 parameters => {
803 additionalProperties => 0,
804 properties => {
805 node => get_standard_option('pve-node'),
806 vmid => get_standard_option('pve-vmid'),
807 skiplock => get_standard_option('skiplock'),
808 },
809 },
810 returns => {
811 type => 'string',
812 },
813 code => sub {
814 my ($param) = @_;
815
816 my $rpcenv = PVE::RPCEnvironment::get();
817
818 my $authuser = $rpcenv->get_user();
819
820 my $vmid = $param->{vmid};
821
822 my $skiplock = $param->{skiplock};
823 raise_param_exc({ skiplock => "Only root may use this option." })
824 if $skiplock && $authuser ne 'root@pam';
825
826 # test if VM exists
827 my $conf = PVE::QemuServer::load_config($vmid);
828
829 my $storecfg = PVE::Storage::config();
830
831 my $realcmd = sub {
832 my $upid = shift;
833
834 syslog('info', "destroy VM $vmid: $upid\n");
835
836 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
837 };
838
839 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
840 }});
841
842 __PACKAGE__->register_method({
843 name => 'unlink',
844 path => '{vmid}/unlink',
845 method => 'PUT',
846 protected => 1,
847 proxyto => 'node',
848 description => "Unlink/delete disk images.",
849 permissions => {
850 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
851 },
852 parameters => {
853 additionalProperties => 0,
854 properties => {
855 node => get_standard_option('pve-node'),
856 vmid => get_standard_option('pve-vmid'),
857 idlist => {
858 type => 'string', format => 'pve-configid-list',
859 description => "A list of disk IDs you want to delete.",
860 },
861 force => {
862 type => 'boolean',
863 description => $opt_force_description,
864 optional => 1,
865 },
866 },
867 },
868 returns => { type => 'null'},
869 code => sub {
870 my ($param) = @_;
871
872 $param->{delete} = extract_param($param, 'idlist');
873
874 __PACKAGE__->update_vm($param);
875
876 return undef;
877 }});
878
879 my $sslcert;
880
881 __PACKAGE__->register_method({
882 name => 'vncproxy',
883 path => '{vmid}/vncproxy',
884 method => 'POST',
885 protected => 1,
886 permissions => {
887 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
888 },
889 description => "Creates a TCP VNC proxy connections.",
890 parameters => {
891 additionalProperties => 0,
892 properties => {
893 node => get_standard_option('pve-node'),
894 vmid => get_standard_option('pve-vmid'),
895 },
896 },
897 returns => {
898 additionalProperties => 0,
899 properties => {
900 user => { type => 'string' },
901 ticket => { type => 'string' },
902 cert => { type => 'string' },
903 port => { type => 'integer' },
904 upid => { type => 'string' },
905 },
906 },
907 code => sub {
908 my ($param) = @_;
909
910 my $rpcenv = PVE::RPCEnvironment::get();
911
912 my $authuser = $rpcenv->get_user();
913
914 my $vmid = $param->{vmid};
915 my $node = $param->{node};
916
917 my $authpath = "/vms/$vmid";
918
919 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
920
921 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
922 if !$sslcert;
923
924 my $port = PVE::Tools::next_vnc_port();
925
926 my $remip;
927
928 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
929 $remip = PVE::Cluster::remote_node_ip($node);
930 }
931
932 # NOTE: kvm VNC traffic is already TLS encrypted,
933 # so we select the fastest chipher here (or 'none'?)
934 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
935 '-c', 'blowfish-cbc', $remip] : [];
936
937 my $timeout = 10;
938
939 my $realcmd = sub {
940 my $upid = shift;
941
942 syslog('info', "starting vnc proxy $upid\n");
943
944 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
945
946 my $qmstr = join(' ', @$qmcmd);
947
948 # also redirect stderr (else we get RFB protocol errors)
949 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
950
951 PVE::Tools::run_command($cmd);
952
953 return;
954 };
955
956 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
957
958 return {
959 user => $authuser,
960 ticket => $ticket,
961 port => $port,
962 upid => $upid,
963 cert => $sslcert,
964 };
965 }});
966
967 __PACKAGE__->register_method({
968 name => 'vmcmdidx',
969 path => '{vmid}/status',
970 method => 'GET',
971 proxyto => 'node',
972 description => "Directory index",
973 permissions => {
974 user => 'all',
975 },
976 parameters => {
977 additionalProperties => 0,
978 properties => {
979 node => get_standard_option('pve-node'),
980 vmid => get_standard_option('pve-vmid'),
981 },
982 },
983 returns => {
984 type => 'array',
985 items => {
986 type => "object",
987 properties => {
988 subdir => { type => 'string' },
989 },
990 },
991 links => [ { rel => 'child', href => "{subdir}" } ],
992 },
993 code => sub {
994 my ($param) = @_;
995
996 # test if VM exists
997 my $conf = PVE::QemuServer::load_config($param->{vmid});
998
999 my $res = [
1000 { subdir => 'current' },
1001 { subdir => 'start' },
1002 { subdir => 'stop' },
1003 ];
1004
1005 return $res;
1006 }});
1007
1008 __PACKAGE__->register_method({
1009 name => 'vm_status',
1010 path => '{vmid}/status/current',
1011 method => 'GET',
1012 proxyto => 'node',
1013 protected => 1, # qemu pid files are only readable by root
1014 description => "Get virtual machine status.",
1015 permissions => {
1016 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1017 },
1018 parameters => {
1019 additionalProperties => 0,
1020 properties => {
1021 node => get_standard_option('pve-node'),
1022 vmid => get_standard_option('pve-vmid'),
1023 },
1024 },
1025 returns => { type => 'object' },
1026 code => sub {
1027 my ($param) = @_;
1028
1029 # test if VM exists
1030 my $conf = PVE::QemuServer::load_config($param->{vmid});
1031
1032 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
1033 my $status = $vmstatus->{$param->{vmid}};
1034
1035 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1036 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
1037 $status->{ha} = 1;
1038 } else {
1039 $status->{ha} = 0;
1040 }
1041
1042 return $status;
1043 }});
1044
1045 __PACKAGE__->register_method({
1046 name => 'vm_start',
1047 path => '{vmid}/status/start',
1048 method => 'POST',
1049 protected => 1,
1050 proxyto => 'node',
1051 description => "Start virtual machine.",
1052 permissions => {
1053 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1054 },
1055 parameters => {
1056 additionalProperties => 0,
1057 properties => {
1058 node => get_standard_option('pve-node'),
1059 vmid => get_standard_option('pve-vmid'),
1060 skiplock => get_standard_option('skiplock'),
1061 stateuri => get_standard_option('pve-qm-stateuri'),
1062 },
1063 },
1064 returns => {
1065 type => 'string',
1066 },
1067 code => sub {
1068 my ($param) = @_;
1069
1070 my $rpcenv = PVE::RPCEnvironment::get();
1071
1072 my $authuser = $rpcenv->get_user();
1073
1074 my $node = extract_param($param, 'node');
1075
1076 my $vmid = extract_param($param, 'vmid');
1077
1078 my $stateuri = extract_param($param, 'stateuri');
1079 raise_param_exc({ stateuri => "Only root may use this option." })
1080 if $stateuri && $authuser ne 'root@pam';
1081
1082 my $skiplock = extract_param($param, 'skiplock');
1083 raise_param_exc({ skiplock => "Only root may use this option." })
1084 if $skiplock && $authuser ne 'root@pam';
1085
1086 my $storecfg = PVE::Storage::config();
1087
1088 my $realcmd = sub {
1089 my $upid = shift;
1090
1091 syslog('info', "start VM $vmid: $upid\n");
1092
1093 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
1094
1095 return;
1096 };
1097
1098 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1099 }});
1100
1101 __PACKAGE__->register_method({
1102 name => 'vm_stop',
1103 path => '{vmid}/status/stop',
1104 method => 'POST',
1105 protected => 1,
1106 proxyto => 'node',
1107 description => "Stop virtual machine.",
1108 permissions => {
1109 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1110 },
1111 parameters => {
1112 additionalProperties => 0,
1113 properties => {
1114 node => get_standard_option('pve-node'),
1115 vmid => get_standard_option('pve-vmid'),
1116 skiplock => get_standard_option('skiplock'),
1117 timeout => {
1118 description => "Wait maximal timeout seconds.",
1119 type => 'integer',
1120 minimum => 0,
1121 optional => 1,
1122 },
1123 keepActive => {
1124 description => "Do not decativate storage volumes.",
1125 type => 'boolean',
1126 optional => 1,
1127 default => 0,
1128 }
1129 },
1130 },
1131 returns => {
1132 type => 'string',
1133 },
1134 code => sub {
1135 my ($param) = @_;
1136
1137 my $rpcenv = PVE::RPCEnvironment::get();
1138
1139 my $authuser = $rpcenv->get_user();
1140
1141 my $node = extract_param($param, 'node');
1142
1143 my $vmid = extract_param($param, 'vmid');
1144
1145 my $skiplock = extract_param($param, 'skiplock');
1146 raise_param_exc({ skiplock => "Only root may use this option." })
1147 if $skiplock && $authuser ne 'root@pam';
1148
1149 my $keepActive = extract_param($param, 'keepActive');
1150 raise_param_exc({ keepActive => "Only root may use this option." })
1151 if $keepActive && $authuser ne 'root@pam';
1152
1153 my $storecfg = PVE::Storage::config();
1154
1155 my $realcmd = sub {
1156 my $upid = shift;
1157
1158 syslog('info', "stop VM $vmid: $upid\n");
1159
1160 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
1161 $param->{timeout}, 0, 1, $keepActive);
1162
1163 return;
1164 };
1165
1166 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1167 }});
1168
1169 __PACKAGE__->register_method({
1170 name => 'vm_reset',
1171 path => '{vmid}/status/reset',
1172 method => 'POST',
1173 protected => 1,
1174 proxyto => 'node',
1175 description => "Reset virtual machine.",
1176 permissions => {
1177 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1178 },
1179 parameters => {
1180 additionalProperties => 0,
1181 properties => {
1182 node => get_standard_option('pve-node'),
1183 vmid => get_standard_option('pve-vmid'),
1184 skiplock => get_standard_option('skiplock'),
1185 },
1186 },
1187 returns => {
1188 type => 'string',
1189 },
1190 code => sub {
1191 my ($param) = @_;
1192
1193 my $rpcenv = PVE::RPCEnvironment::get();
1194
1195 my $authuser = $rpcenv->get_user();
1196
1197 my $node = extract_param($param, 'node');
1198
1199 my $vmid = extract_param($param, 'vmid');
1200
1201 my $skiplock = extract_param($param, 'skiplock');
1202 raise_param_exc({ skiplock => "Only root may use this option." })
1203 if $skiplock && $authuser ne 'root@pam';
1204
1205 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1206
1207 my $realcmd = sub {
1208 my $upid = shift;
1209
1210 PVE::QemuServer::vm_reset($vmid, $skiplock);
1211
1212 return;
1213 };
1214
1215 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
1216 }});
1217
1218 __PACKAGE__->register_method({
1219 name => 'vm_shutdown',
1220 path => '{vmid}/status/shutdown',
1221 method => 'POST',
1222 protected => 1,
1223 proxyto => 'node',
1224 description => "Shutdown virtual machine.",
1225 permissions => {
1226 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1227 },
1228 parameters => {
1229 additionalProperties => 0,
1230 properties => {
1231 node => get_standard_option('pve-node'),
1232 vmid => get_standard_option('pve-vmid'),
1233 skiplock => get_standard_option('skiplock'),
1234 timeout => {
1235 description => "Wait maximal timeout seconds.",
1236 type => 'integer',
1237 minimum => 0,
1238 optional => 1,
1239 },
1240 forceStop => {
1241 description => "Make sure the VM stops.",
1242 type => 'boolean',
1243 optional => 1,
1244 default => 0,
1245 },
1246 keepActive => {
1247 description => "Do not decativate storage volumes.",
1248 type => 'boolean',
1249 optional => 1,
1250 default => 0,
1251 }
1252 },
1253 },
1254 returns => {
1255 type => 'string',
1256 },
1257 code => sub {
1258 my ($param) = @_;
1259
1260 my $rpcenv = PVE::RPCEnvironment::get();
1261
1262 my $authuser = $rpcenv->get_user();
1263
1264 my $node = extract_param($param, 'node');
1265
1266 my $vmid = extract_param($param, 'vmid');
1267
1268 my $skiplock = extract_param($param, 'skiplock');
1269 raise_param_exc({ skiplock => "Only root may use this option." })
1270 if $skiplock && $authuser ne 'root@pam';
1271
1272 my $keepActive = extract_param($param, 'keepActive');
1273 raise_param_exc({ keepActive => "Only root may use this option." })
1274 if $keepActive && $authuser ne 'root@pam';
1275
1276 my $storecfg = PVE::Storage::config();
1277
1278 my $realcmd = sub {
1279 my $upid = shift;
1280
1281 syslog('info', "shutdown VM $vmid: $upid\n");
1282
1283 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
1284 1, $param->{forceStop}, $keepActive);
1285
1286 return;
1287 };
1288
1289 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
1290 }});
1291
1292 __PACKAGE__->register_method({
1293 name => 'vm_suspend',
1294 path => '{vmid}/status/suspend',
1295 method => 'POST',
1296 protected => 1,
1297 proxyto => 'node',
1298 description => "Suspend virtual machine.",
1299 permissions => {
1300 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1301 },
1302 parameters => {
1303 additionalProperties => 0,
1304 properties => {
1305 node => get_standard_option('pve-node'),
1306 vmid => get_standard_option('pve-vmid'),
1307 skiplock => get_standard_option('skiplock'),
1308 },
1309 },
1310 returns => {
1311 type => 'string',
1312 },
1313 code => sub {
1314 my ($param) = @_;
1315
1316 my $rpcenv = PVE::RPCEnvironment::get();
1317
1318 my $authuser = $rpcenv->get_user();
1319
1320 my $node = extract_param($param, 'node');
1321
1322 my $vmid = extract_param($param, 'vmid');
1323
1324 my $skiplock = extract_param($param, 'skiplock');
1325 raise_param_exc({ skiplock => "Only root may use this option." })
1326 if $skiplock && $authuser ne 'root@pam';
1327
1328 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1329
1330 my $realcmd = sub {
1331 my $upid = shift;
1332
1333 syslog('info', "suspend VM $vmid: $upid\n");
1334
1335 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1336
1337 return;
1338 };
1339
1340 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
1341 }});
1342
1343 __PACKAGE__->register_method({
1344 name => 'vm_resume',
1345 path => '{vmid}/status/resume',
1346 method => 'POST',
1347 protected => 1,
1348 proxyto => 'node',
1349 description => "Resume virtual machine.",
1350 permissions => {
1351 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1352 },
1353 parameters => {
1354 additionalProperties => 0,
1355 properties => {
1356 node => get_standard_option('pve-node'),
1357 vmid => get_standard_option('pve-vmid'),
1358 skiplock => get_standard_option('skiplock'),
1359 },
1360 },
1361 returns => {
1362 type => 'string',
1363 },
1364 code => sub {
1365 my ($param) = @_;
1366
1367 my $rpcenv = PVE::RPCEnvironment::get();
1368
1369 my $authuser = $rpcenv->get_user();
1370
1371 my $node = extract_param($param, 'node');
1372
1373 my $vmid = extract_param($param, 'vmid');
1374
1375 my $skiplock = extract_param($param, 'skiplock');
1376 raise_param_exc({ skiplock => "Only root may use this option." })
1377 if $skiplock && $authuser ne 'root@pam';
1378
1379 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1380
1381 my $realcmd = sub {
1382 my $upid = shift;
1383
1384 syslog('info', "resume VM $vmid: $upid\n");
1385
1386 PVE::QemuServer::vm_resume($vmid, $skiplock);
1387
1388 return;
1389 };
1390
1391 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
1392 }});
1393
1394 __PACKAGE__->register_method({
1395 name => 'vm_sendkey',
1396 path => '{vmid}/sendkey',
1397 method => 'PUT',
1398 protected => 1,
1399 proxyto => 'node',
1400 description => "Send key event to virtual machine.",
1401 permissions => {
1402 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1403 },
1404 parameters => {
1405 additionalProperties => 0,
1406 properties => {
1407 node => get_standard_option('pve-node'),
1408 vmid => get_standard_option('pve-vmid'),
1409 skiplock => get_standard_option('skiplock'),
1410 key => {
1411 description => "The key (qemu monitor encoding).",
1412 type => 'string'
1413 }
1414 },
1415 },
1416 returns => { type => 'null'},
1417 code => sub {
1418 my ($param) = @_;
1419
1420 my $rpcenv = PVE::RPCEnvironment::get();
1421
1422 my $authuser = $rpcenv->get_user();
1423
1424 my $node = extract_param($param, 'node');
1425
1426 my $vmid = extract_param($param, 'vmid');
1427
1428 my $skiplock = extract_param($param, 'skiplock');
1429 raise_param_exc({ skiplock => "Only root may use this option." })
1430 if $skiplock && $authuser ne 'root@pam';
1431
1432 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1433
1434 return;
1435 }});
1436
1437 __PACKAGE__->register_method({
1438 name => 'migrate_vm',
1439 path => '{vmid}/migrate',
1440 method => 'POST',
1441 protected => 1,
1442 proxyto => 'node',
1443 description => "Migrate virtual machine. Creates a new migration task.",
1444 permissions => {
1445 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1446 },
1447 parameters => {
1448 additionalProperties => 0,
1449 properties => {
1450 node => get_standard_option('pve-node'),
1451 vmid => get_standard_option('pve-vmid'),
1452 target => get_standard_option('pve-node', { description => "Target node." }),
1453 online => {
1454 type => 'boolean',
1455 description => "Use online/live migration.",
1456 optional => 1,
1457 },
1458 force => {
1459 type => 'boolean',
1460 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
1461 optional => 1,
1462 },
1463 },
1464 },
1465 returns => {
1466 type => 'string',
1467 description => "the task ID.",
1468 },
1469 code => sub {
1470 my ($param) = @_;
1471
1472 my $rpcenv = PVE::RPCEnvironment::get();
1473
1474 my $authuser = $rpcenv->get_user();
1475
1476 my $target = extract_param($param, 'target');
1477
1478 my $localnode = PVE::INotify::nodename();
1479 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
1480
1481 PVE::Cluster::check_cfs_quorum();
1482
1483 PVE::Cluster::check_node_exists($target);
1484
1485 my $targetip = PVE::Cluster::remote_node_ip($target);
1486
1487 my $vmid = extract_param($param, 'vmid');
1488
1489 raise_param_exc({ force => "Only root may use this option." })
1490 if $param->{force} && $authuser ne 'root@pam';
1491
1492 # test if VM exists
1493 my $conf = PVE::QemuServer::load_config($vmid);
1494
1495 # try to detect errors early
1496
1497 PVE::QemuServer::check_lock($conf);
1498
1499 if (PVE::QemuServer::check_running($vmid)) {
1500 die "cant migrate running VM without --online\n"
1501 if !$param->{online};
1502 }
1503
1504 my $realcmd = sub {
1505 my $upid = shift;
1506
1507 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1508 };
1509
1510 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1511
1512 return $upid;
1513 }});
1514
1515 __PACKAGE__->register_method({
1516 name => 'monitor',
1517 path => '{vmid}/monitor',
1518 method => 'POST',
1519 protected => 1,
1520 proxyto => 'node',
1521 description => "Execute Qemu monitor commands.",
1522 permissions => {
1523 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1524 },
1525 parameters => {
1526 additionalProperties => 0,
1527 properties => {
1528 node => get_standard_option('pve-node'),
1529 vmid => get_standard_option('pve-vmid'),
1530 command => {
1531 type => 'string',
1532 description => "The monitor command.",
1533 }
1534 },
1535 },
1536 returns => { type => 'string'},
1537 code => sub {
1538 my ($param) = @_;
1539
1540 my $vmid = $param->{vmid};
1541
1542 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1543
1544 my $res = '';
1545 eval {
1546 $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
1547 };
1548 $res = "ERROR: $@" if $@;
1549
1550 return $res;
1551 }});
1552
1553 1;