]> git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu.pm
implement unique option for restore
[qemu-server.git] / PVE / API2 / Qemu.pm
1 package PVE::API2::Qemu;
2
3 use strict;
4 use warnings;
5
6 use PVE::Cluster;
7 use PVE::SafeSyslog;
8 use PVE::Tools qw(extract_param);
9 use PVE::Exception qw(raise raise_param_exc);
10 use PVE::Storage;
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::RESTHandler;
13 use PVE::QemuServer;
14 use PVE::QemuMigrate;
15 use PVE::RPCEnvironment;
16 use PVE::AccessControl;
17 use PVE::INotify;
18
19 use Data::Dumper; # fixme: remove
20
21 use base qw(PVE::RESTHandler);
22
23 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.";
24
25 my $resolve_cdrom_alias = sub {
26 my $param = shift;
27
28 if (my $value = $param->{cdrom}) {
29 $value .= ",media=cdrom" if $value !~ m/media=/;
30 $param->{ide2} = $value;
31 delete $param->{cdrom};
32 }
33 };
34
35 __PACKAGE__->register_method({
36 name => 'vmlist',
37 path => '',
38 method => 'GET',
39 description => "Virtual machine index (per node).",
40 proxyto => 'node',
41 protected => 1, # qemu pid files are only readable by root
42 parameters => {
43 additionalProperties => 0,
44 properties => {
45 node => get_standard_option('pve-node'),
46 },
47 },
48 returns => {
49 type => 'array',
50 items => {
51 type => "object",
52 properties => {},
53 },
54 links => [ { rel => 'child', href => "{vmid}" } ],
55 },
56 code => sub {
57 my ($param) = @_;
58
59 my $vmstatus = PVE::QemuServer::vmstatus();
60
61 return PVE::RESTHandler::hash_to_array($vmstatus, 'vmid');
62
63 }});
64
65 __PACKAGE__->register_method({
66 name => 'create_vm',
67 path => '',
68 method => 'POST',
69 description => "Create or restore a virtual machine.",
70 protected => 1,
71 proxyto => 'node',
72 parameters => {
73 additionalProperties => 0,
74 properties => PVE::QemuServer::json_config_properties(
75 {
76 node => get_standard_option('pve-node'),
77 vmid => get_standard_option('pve-vmid'),
78 archive => {
79 description => "The backup file.",
80 type => 'string',
81 optional => 1,
82 maxLength => 255,
83 },
84 storage => get_standard_option('pve-storage-id', {
85 description => "Default storage.",
86 optional => 1,
87 }),
88 force => {
89 optional => 1,
90 type => 'boolean',
91 description => "Allow to overwrite existing VM.",
92 requires => 'archive',
93 },
94 unique => {
95 optional => 1,
96 type => 'boolean',
97 description => "Assign a unique random ethernet address.",
98 requires => 'archive',
99 },
100 }),
101 },
102 returns => {
103 type => 'string',
104 },
105 code => sub {
106 my ($param) = @_;
107
108 my $rpcenv = PVE::RPCEnvironment::get();
109
110 my $user = $rpcenv->get_user();
111
112 my $node = extract_param($param, 'node');
113
114 my $vmid = extract_param($param, 'vmid');
115
116 my $archive = extract_param($param, 'archive');
117
118 my $storage = extract_param($param, 'storage');
119
120 my $force = extract_param($param, 'force');
121
122 my $unique = extract_param($param, 'unique');
123
124 my $filename = PVE::QemuServer::config_file($vmid);
125
126 my $storecfg = PVE::Storage::config();
127
128 PVE::Cluster::check_cfs_quorum();
129
130 if (!$archive) {
131 &$resolve_cdrom_alias($param);
132
133 foreach my $opt (keys %$param) {
134 if (PVE::QemuServer::valid_drivename($opt)) {
135 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
136 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
137
138 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
139 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
140 }
141 }
142
143 PVE::QemuServer::add_random_macs($param);
144 } else {
145 my $keystr = join(' ', keys %$param);
146 raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
147 }
148
149 # fixme: archive eq '-' (read from stdin)
150
151 my $restorefn = sub {
152
153 if (-f $filename) {
154 die "unable to restore vm $vmid: config file already exists\n"
155 if !$force;
156
157 die "unable to restore vm $vmid: vm is running\n"
158 if PVE::QemuServer::check_running($vmid);
159
160 # destroy existing data - keep empty config
161 PVE::QemuServer::destroy_vm($storecfg, $vmid, 1);
162 }
163
164 my $realcmd = sub {
165 PVE::QemuServer::restore_archive($archive, $vmid, {
166 storage => $storage,
167 unique => $unique });
168 };
169
170 return $rpcenv->fork_worker('qmrestore', $vmid, $user, $realcmd);
171 };
172
173 my $createfn = sub {
174
175 # second test (after locking test is accurate)
176 die "unable to create vm $vmid: config file already exists\n"
177 if -f $filename;
178
179 my $realcmd = sub {
180
181 my $vollist = [];
182
183 eval {
184 $vollist = PVE::QemuServer::create_disks($storecfg, $vmid, $param, $storage);
185
186 # try to be smart about bootdisk
187 my @disks = PVE::QemuServer::disknames();
188 my $firstdisk;
189 foreach my $ds (reverse @disks) {
190 next if !$param->{$ds};
191 my $disk = PVE::QemuServer::parse_drive($ds, $param->{$ds});
192 next if PVE::QemuServer::drive_is_cdrom($disk);
193 $firstdisk = $ds;
194 }
195
196 if (!$param->{bootdisk} && $firstdisk) {
197 $param->{bootdisk} = $firstdisk;
198 }
199
200 PVE::QemuServer::create_conf_nolock($vmid, $param);
201 };
202 my $err = $@;
203
204 if ($err) {
205 foreach my $volid (@$vollist) {
206 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
207 warn $@ if $@;
208 }
209 die "create failed - $err";
210 }
211 };
212
213 return $rpcenv->fork_worker('qmcreate', $vmid, $user, $realcmd);
214 };
215
216 return PVE::QemuServer::lock_config($vmid, $archive ? $restorefn : $createfn);
217 }});
218
219 __PACKAGE__->register_method({
220 name => 'vmdiridx',
221 path => '{vmid}',
222 method => 'GET',
223 proxyto => 'node',
224 description => "Directory index",
225 parameters => {
226 additionalProperties => 0,
227 properties => {
228 node => get_standard_option('pve-node'),
229 vmid => get_standard_option('pve-vmid'),
230 },
231 },
232 returns => {
233 type => 'array',
234 items => {
235 type => "object",
236 properties => {
237 subdir => { type => 'string' },
238 },
239 },
240 links => [ { rel => 'child', href => "{subdir}" } ],
241 },
242 code => sub {
243 my ($param) = @_;
244
245 my $res = [
246 { subdir => 'config' },
247 { subdir => 'status' },
248 { subdir => 'unlink' },
249 { subdir => 'vncproxy' },
250 { subdir => 'migrate' },
251 { subdir => 'rrd' },
252 { subdir => 'rrddata' },
253 ];
254
255 return $res;
256 }});
257
258 __PACKAGE__->register_method({
259 name => 'rrd',
260 path => '{vmid}/rrd',
261 method => 'GET',
262 protected => 1, # fixme: can we avoid that?
263 permissions => {
264 path => '/vms/{vmid}',
265 privs => [ 'VM.Audit' ],
266 },
267 description => "Read VM RRD statistics (returns PNG)",
268 parameters => {
269 additionalProperties => 0,
270 properties => {
271 node => get_standard_option('pve-node'),
272 vmid => get_standard_option('pve-vmid'),
273 timeframe => {
274 description => "Specify the time frame you are interested in.",
275 type => 'string',
276 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
277 },
278 ds => {
279 description => "The list of datasources you want to display.",
280 type => 'string', format => 'pve-configid-list',
281 },
282 cf => {
283 description => "The RRD consolidation function",
284 type => 'string',
285 enum => [ 'AVERAGE', 'MAX' ],
286 optional => 1,
287 },
288 },
289 },
290 returns => {
291 type => "object",
292 properties => {
293 filename => { type => 'string' },
294 },
295 },
296 code => sub {
297 my ($param) = @_;
298
299 return PVE::Cluster::create_rrd_graph(
300 "pve2-vm/$param->{vmid}", $param->{timeframe},
301 $param->{ds}, $param->{cf});
302
303 }});
304
305 __PACKAGE__->register_method({
306 name => 'rrddata',
307 path => '{vmid}/rrddata',
308 method => 'GET',
309 protected => 1, # fixme: can we avoid that?
310 permissions => {
311 path => '/vms/{vmid}',
312 privs => [ 'VM.Audit' ],
313 },
314 description => "Read VM RRD statistics",
315 parameters => {
316 additionalProperties => 0,
317 properties => {
318 node => get_standard_option('pve-node'),
319 vmid => get_standard_option('pve-vmid'),
320 timeframe => {
321 description => "Specify the time frame you are interested in.",
322 type => 'string',
323 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
324 },
325 cf => {
326 description => "The RRD consolidation function",
327 type => 'string',
328 enum => [ 'AVERAGE', 'MAX' ],
329 optional => 1,
330 },
331 },
332 },
333 returns => {
334 type => "array",
335 items => {
336 type => "object",
337 properties => {},
338 },
339 },
340 code => sub {
341 my ($param) = @_;
342
343 return PVE::Cluster::create_rrd_data(
344 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
345 }});
346
347
348 __PACKAGE__->register_method({
349 name => 'vm_config',
350 path => '{vmid}/config',
351 method => 'GET',
352 proxyto => 'node',
353 description => "Get virtual machine configuration.",
354 parameters => {
355 additionalProperties => 0,
356 properties => {
357 node => get_standard_option('pve-node'),
358 vmid => get_standard_option('pve-vmid'),
359 },
360 },
361 returns => {
362 type => "object",
363 properties => {
364 digest => {
365 type => 'string',
366 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
367 }
368 },
369 },
370 code => sub {
371 my ($param) = @_;
372
373 my $conf = PVE::QemuServer::load_config($param->{vmid});
374
375 return $conf;
376 }});
377
378 __PACKAGE__->register_method({
379 name => 'update_vm',
380 path => '{vmid}/config',
381 method => 'PUT',
382 protected => 1,
383 proxyto => 'node',
384 description => "Set virtual machine options.",
385 parameters => {
386 additionalProperties => 0,
387 properties => PVE::QemuServer::json_config_properties(
388 {
389 node => get_standard_option('pve-node'),
390 vmid => get_standard_option('pve-vmid'),
391 skiplock => get_standard_option('skiplock'),
392 delete => {
393 type => 'string', format => 'pve-configid-list',
394 description => "A list of settings you want to delete.",
395 optional => 1,
396 },
397 force => {
398 type => 'boolean',
399 description => $opt_force_description,
400 optional => 1,
401 requires => 'delete',
402 },
403 digest => {
404 type => 'string',
405 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
406 maxLength => 40,
407 optional => 1,
408 }
409 }),
410 },
411 returns => { type => 'null'},
412 code => sub {
413 my ($param) = @_;
414
415 my $rpcenv = PVE::RPCEnvironment::get();
416
417 my $user = $rpcenv->get_user();
418
419 my $node = extract_param($param, 'node');
420
421 my $vmid = extract_param($param, 'vmid');
422
423 my $digest = extract_param($param, 'digest');
424
425 my @paramarr = (); # used for log message
426 foreach my $key (keys %$param) {
427 push @paramarr, "-$key", $param->{$key};
428 }
429
430 my $skiplock = extract_param($param, 'skiplock');
431 raise_param_exc({ skiplock => "Only root may use this option." })
432 if $skiplock && $user ne 'root@pam';
433
434 my $delete = extract_param($param, 'delete');
435 my $force = extract_param($param, 'force');
436
437 die "no options specified\n" if !$delete && !scalar(keys %$param);
438
439 my $storecfg = PVE::Storage::config();
440
441 &$resolve_cdrom_alias($param);
442
443 my $eject = {};
444 my $cdchange = {};
445
446 foreach my $opt (keys %$param) {
447 if (PVE::QemuServer::valid_drivename($opt)) {
448 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
449 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
450 if ($drive->{file} eq 'eject') {
451 $eject->{$opt} = 1;
452 delete $param->{$opt};
453 next;
454 }
455
456 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
457 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
458
459 if (PVE::QemuServer::drive_is_cdrom($drive)) {
460 $cdchange->{$opt} = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
461 }
462 }
463 }
464
465 foreach my $opt (PVE::Tools::split_list($delete)) {
466 $opt = 'ide2' if $opt eq 'cdrom';
467 die "you can't use '-$opt' and '-delete $opt' at the same time\n"
468 if defined($param->{$opt});
469 }
470
471 PVE::QemuServer::add_random_macs($param);
472
473 my $vollist = [];
474
475 my $updatefn = sub {
476
477 my $conf = PVE::QemuServer::load_config($vmid);
478
479 die "checksum missmatch (file change by other user?)\n"
480 if $digest && $digest ne $conf->{digest};
481
482 PVE::QemuServer::check_lock($conf) if !$skiplock;
483
484 PVE::Cluster::log_msg('info', $user, "update VM $vmid: " . join (' ', @paramarr));
485
486 foreach my $opt (keys %$eject) {
487 if ($conf->{$opt}) {
488 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
489 $cdchange->{$opt} = undef if PVE::QemuServer::drive_is_cdrom($drive);
490 } else {
491 raise_param_exc({ $opt => "eject failed - drive does not exist." });
492 }
493 }
494
495 foreach my $opt (keys %$param) {
496 next if !PVE::QemuServer::valid_drivename($opt);
497 next if !$conf->{$opt};
498 my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
499 next if PVE::QemuServer::drive_is_cdrom($old_drive);
500 my $new_drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
501 if ($new_drive->{file} ne $old_drive->{file}) {
502 my ($path, $owner);
503 eval { ($path, $owner) = PVE::Storage::path($storecfg, $old_drive->{file}); };
504 if ($owner && ($owner == $vmid)) {
505 PVE::QemuServer::add_unused_volume($conf, $param, $old_drive->{file});
506 }
507 }
508 }
509
510 my $unset = {};
511
512 foreach my $opt (PVE::Tools::split_list($delete)) {
513 $opt = 'ide2' if $opt eq 'cdrom';
514 if (!PVE::QemuServer::option_exists($opt)) {
515 raise_param_exc({ delete => "unknown option '$opt'" });
516 }
517 next if !defined($conf->{$opt});
518 if (PVE::QemuServer::valid_drivename($opt)) {
519 PVE::QemuServer::vm_devicedel($vmid, $conf, $opt);
520 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
521 if (PVE::QemuServer::drive_is_cdrom($drive)) {
522 $cdchange->{$opt} = undef;
523 } else {
524 my $volid = $drive->{file};
525
526 if ($volid !~ m|^/|) {
527 my ($path, $owner);
528 eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
529 if ($owner && ($owner == $vmid)) {
530 if ($force) {
531 push @$vollist, $volid;
532 } else {
533 PVE::QemuServer::add_unused_volume($conf, $param, $volid);
534 }
535 }
536 }
537 }
538 } elsif ($opt =~ m/^unused/) {
539 push @$vollist, $conf->{$opt};
540 }
541
542 $unset->{$opt} = 1;
543 }
544
545 PVE::QemuServer::create_disks($storecfg, $vmid, $param, $conf);
546
547 PVE::QemuServer::change_config_nolock($vmid, $param, $unset, 1);
548
549 return if !PVE::QemuServer::check_running($vmid);
550
551 foreach my $opt (keys %$cdchange) {
552 my $qdn = PVE::QemuServer::qemu_drive_name($opt, 'cdrom');
553 my $path = $cdchange->{$opt};
554 PVE::QemuServer::vm_monitor_command($vmid, "eject $qdn", 0);
555 PVE::QemuServer::vm_monitor_command($vmid, "change $qdn \"$path\"", 0) if $path;
556 }
557 };
558
559 PVE::QemuServer::lock_config($vmid, $updatefn);
560
561 foreach my $volid (@$vollist) {
562 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
563 # fixme: log ?
564 warn $@ if $@;
565 }
566
567 return undef;
568 }});
569
570
571 __PACKAGE__->register_method({
572 name => 'destroy_vm',
573 path => '{vmid}',
574 method => 'DELETE',
575 protected => 1,
576 proxyto => 'node',
577 description => "Destroy the vm (also delete all used/owned volumes).",
578 parameters => {
579 additionalProperties => 0,
580 properties => {
581 node => get_standard_option('pve-node'),
582 vmid => get_standard_option('pve-vmid'),
583 skiplock => get_standard_option('skiplock'),
584 },
585 },
586 returns => {
587 type => 'string',
588 },
589 code => sub {
590 my ($param) = @_;
591
592 my $rpcenv = PVE::RPCEnvironment::get();
593
594 my $user = $rpcenv->get_user();
595
596 my $vmid = $param->{vmid};
597
598 my $skiplock = $param->{skiplock};
599 raise_param_exc({ skiplock => "Only root may use this option." })
600 if $skiplock && $user ne 'root@pam';
601
602 # test if VM exists
603 my $conf = PVE::QemuServer::load_config($vmid);
604
605 my $storecfg = PVE::Storage::config();
606
607 my $realcmd = sub {
608 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
609 };
610
611 return $rpcenv->fork_worker('qmdestroy', $vmid, $user, $realcmd);
612 }});
613
614 __PACKAGE__->register_method({
615 name => 'unlink',
616 path => '{vmid}/unlink',
617 method => 'PUT',
618 protected => 1,
619 proxyto => 'node',
620 description => "Unlink/delete disk images.",
621 parameters => {
622 additionalProperties => 0,
623 properties => {
624 node => get_standard_option('pve-node'),
625 vmid => get_standard_option('pve-vmid'),
626 idlist => {
627 type => 'string', format => 'pve-configid-list',
628 description => "A list of disk IDs you want to delete.",
629 },
630 force => {
631 type => 'boolean',
632 description => $opt_force_description,
633 optional => 1,
634 },
635 },
636 },
637 returns => { type => 'null'},
638 code => sub {
639 my ($param) = @_;
640
641 $param->{delete} = extract_param($param, 'idlist');
642
643 __PACKAGE__->update_vm($param);
644
645 return undef;
646 }});
647
648 my $sslcert;
649
650 __PACKAGE__->register_method({
651 name => 'vncproxy',
652 path => '{vmid}/vncproxy',
653 method => 'POST',
654 protected => 1,
655 permissions => {
656 path => '/vms/{vmid}',
657 privs => [ 'VM.Console' ],
658 },
659 description => "Creates a TCP VNC proxy connections.",
660 parameters => {
661 additionalProperties => 0,
662 properties => {
663 node => get_standard_option('pve-node'),
664 vmid => get_standard_option('pve-vmid'),
665 },
666 },
667 returns => {
668 additionalProperties => 0,
669 properties => {
670 user => { type => 'string' },
671 ticket => { type => 'string' },
672 cert => { type => 'string' },
673 port => { type => 'integer' },
674 upid => { type => 'string' },
675 },
676 },
677 code => sub {
678 my ($param) = @_;
679
680 my $rpcenv = PVE::RPCEnvironment::get();
681
682 my $user = $rpcenv->get_user();
683 my $ticket = PVE::AccessControl::assemble_ticket($user);
684
685 my $vmid = $param->{vmid};
686 my $node = $param->{node};
687
688 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
689 if !$sslcert;
690
691 my $port = PVE::Tools::next_vnc_port();
692
693 my $remip;
694
695 if ($node ne PVE::INotify::nodename()) {
696 $remip = PVE::Cluster::remote_node_ip($node);
697 }
698
699 # NOTE: kvm VNC traffic is already TLS encrypted,
700 # so we select the fastest chipher here (or 'none'?)
701 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
702 '-c', 'blowfish-cbc', $remip] : [];
703
704 my $timeout = 10;
705
706 my $realcmd = sub {
707 my $upid = shift;
708
709 syslog('info', "starting vnc proxy $upid\n");
710
711 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
712
713 my $qmstr = join(' ', @$qmcmd);
714
715 # also redirect stderr (else we get RFB protocol errors)
716 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
717
718 PVE::Tools::run_command($cmd);
719
720 return;
721 };
722
723 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $user, $realcmd);
724
725 return {
726 user => $user,
727 ticket => $ticket,
728 port => $port,
729 upid => $upid,
730 cert => $sslcert,
731 };
732 }});
733
734 __PACKAGE__->register_method({
735 name => 'vmcmdidx',
736 path => '{vmid}/status',
737 method => 'GET',
738 proxyto => 'node',
739 description => "Directory index",
740 parameters => {
741 additionalProperties => 0,
742 properties => {
743 node => get_standard_option('pve-node'),
744 vmid => get_standard_option('pve-vmid'),
745 },
746 },
747 returns => {
748 type => 'array',
749 items => {
750 type => "object",
751 properties => {
752 subdir => { type => 'string' },
753 },
754 },
755 links => [ { rel => 'child', href => "{subdir}" } ],
756 },
757 code => sub {
758 my ($param) = @_;
759
760 # test if VM exists
761 my $conf = PVE::QemuServer::load_config($param->{vmid});
762
763 my $res = [
764 { subdir => 'current' },
765 { subdir => 'start' },
766 { subdir => 'stop' },
767 ];
768
769 return $res;
770 }});
771
772 __PACKAGE__->register_method({
773 name => 'vm_status',
774 path => '{vmid}/status/current',
775 method => 'GET',
776 proxyto => 'node',
777 protected => 1, # qemu pid files are only readable by root
778 description => "Get virtual machine status.",
779 parameters => {
780 additionalProperties => 0,
781 properties => {
782 node => get_standard_option('pve-node'),
783 vmid => get_standard_option('pve-vmid'),
784 },
785 },
786 returns => { type => 'object' },
787 code => sub {
788 my ($param) = @_;
789
790 # test if VM exists
791 my $conf = PVE::QemuServer::load_config($param->{vmid});
792
793 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
794
795 return $vmstatus->{$param->{vmid}};
796 }});
797
798 __PACKAGE__->register_method({
799 name => 'vm_start',
800 path => '{vmid}/status/start',
801 method => 'POST',
802 protected => 1,
803 proxyto => 'node',
804 description => "Start virtual machine.",
805 parameters => {
806 additionalProperties => 0,
807 properties => {
808 node => get_standard_option('pve-node'),
809 vmid => get_standard_option('pve-vmid'),
810 skiplock => get_standard_option('skiplock'),
811 stateuri => get_standard_option('pve-qm-stateuri'),
812 },
813 },
814 returns => {
815 type => 'string',
816 },
817 code => sub {
818 my ($param) = @_;
819
820 my $rpcenv = PVE::RPCEnvironment::get();
821
822 my $user = $rpcenv->get_user();
823
824 my $node = extract_param($param, 'node');
825
826 my $vmid = extract_param($param, 'vmid');
827
828 my $stateuri = extract_param($param, 'stateuri');
829 raise_param_exc({ stateuri => "Only root may use this option." })
830 if $stateuri && $user ne 'root@pam';
831
832 my $skiplock = extract_param($param, 'skiplock');
833 raise_param_exc({ skiplock => "Only root may use this option." })
834 if $skiplock && $user ne 'root@pam';
835
836 my $storecfg = PVE::Storage::config();
837
838 my $realcmd = sub {
839 my $upid = shift;
840
841 syslog('info', "start VM $vmid: $upid\n");
842
843 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock);
844
845 return;
846 };
847
848 return $rpcenv->fork_worker('qmstart', $vmid, $user, $realcmd);
849 }});
850
851 __PACKAGE__->register_method({
852 name => 'vm_stop',
853 path => '{vmid}/status/stop',
854 method => 'POST',
855 protected => 1,
856 proxyto => 'node',
857 description => "Stop virtual machine.",
858 parameters => {
859 additionalProperties => 0,
860 properties => {
861 node => get_standard_option('pve-node'),
862 vmid => get_standard_option('pve-vmid'),
863 skiplock => get_standard_option('skiplock'),
864 timeout => {
865 description => "Wait maximal timeout seconds.",
866 type => 'integer',
867 minimum => 0,
868 optional => 1,
869 }
870 },
871 },
872 returns => {
873 type => 'string',
874 },
875 code => sub {
876 my ($param) = @_;
877
878 my $rpcenv = PVE::RPCEnvironment::get();
879
880 my $user = $rpcenv->get_user();
881
882 my $node = extract_param($param, 'node');
883
884 my $vmid = extract_param($param, 'vmid');
885
886 my $skiplock = extract_param($param, 'skiplock');
887 raise_param_exc({ skiplock => "Only root may use this option." })
888 if $skiplock && $user ne 'root@pam';
889
890 my $realcmd = sub {
891 my $upid = shift;
892
893 syslog('info', "stop VM $vmid: $upid\n");
894
895 PVE::QemuServer::vm_stop($vmid, $skiplock);
896
897 my $pid = PVE::QemuServer::check_running ($vmid);
898
899 if ($pid && $param->{timeout}) {
900 print "waiting until VM $vmid stopps (PID $pid)\n";
901
902 my $count = 0;
903 while (($count < $param->{timeout}) &&
904 PVE::QemuServer::check_running($vmid)) {
905 $count++;
906 sleep 1;
907 }
908
909 die "wait failed - got timeout\n" if PVE::QemuServer::check_running($vmid);
910 }
911
912 return;
913 };
914
915 return $rpcenv->fork_worker('qmstop', $vmid, $user, $realcmd);
916 }});
917
918 __PACKAGE__->register_method({
919 name => 'vm_reset',
920 path => '{vmid}/status/reset',
921 method => 'POST',
922 protected => 1,
923 proxyto => 'node',
924 description => "Reset virtual machine.",
925 parameters => {
926 additionalProperties => 0,
927 properties => {
928 node => get_standard_option('pve-node'),
929 vmid => get_standard_option('pve-vmid'),
930 skiplock => get_standard_option('skiplock'),
931 },
932 },
933 returns => {
934 type => 'string',
935 },
936 code => sub {
937 my ($param) = @_;
938
939 my $rpcenv = PVE::RPCEnvironment::get();
940
941 my $user = $rpcenv->get_user();
942
943 my $node = extract_param($param, 'node');
944
945 my $vmid = extract_param($param, 'vmid');
946
947 my $skiplock = extract_param($param, 'skiplock');
948 raise_param_exc({ skiplock => "Only root may use this option." })
949 if $skiplock && $user ne 'root@pam';
950
951 my $realcmd = sub {
952 my $upid = shift;
953
954 syslog('info', "reset VM $vmid: $upid\n");
955
956 PVE::QemuServer::vm_reset($vmid, $skiplock);
957
958 return;
959 };
960
961 return $rpcenv->fork_worker('qmreset', $vmid, $user, $realcmd);
962 }});
963
964 __PACKAGE__->register_method({
965 name => 'vm_shutdown',
966 path => '{vmid}/status/shutdown',
967 method => 'POST',
968 protected => 1,
969 proxyto => 'node',
970 description => "Shutdown virtual machine.",
971 parameters => {
972 additionalProperties => 0,
973 properties => {
974 node => get_standard_option('pve-node'),
975 vmid => get_standard_option('pve-vmid'),
976 skiplock => get_standard_option('skiplock'),
977 timeout => {
978 description => "Wait maximal timeout seconds.",
979 type => 'integer',
980 minimum => 0,
981 optional => 1,
982 }
983 },
984 },
985 returns => {
986 type => 'string',
987 },
988 code => sub {
989 my ($param) = @_;
990
991 my $rpcenv = PVE::RPCEnvironment::get();
992
993 my $user = $rpcenv->get_user();
994
995 my $node = extract_param($param, 'node');
996
997 my $vmid = extract_param($param, 'vmid');
998
999 my $skiplock = extract_param($param, 'skiplock');
1000 raise_param_exc({ skiplock => "Only root may use this option." })
1001 if $skiplock && $user ne 'root@pam';
1002
1003 my $realcmd = sub {
1004 my $upid = shift;
1005
1006 syslog('info', "shutdown VM $vmid: $upid\n");
1007
1008 PVE::QemuServer::vm_shutdown($vmid, $skiplock);
1009
1010 my $pid = PVE::QemuServer::check_running ($vmid);
1011
1012 if ($pid && $param->{timeout}) {
1013 print "waiting until VM $vmid stopps (PID $pid)\n";
1014
1015 my $count = 0;
1016 while (($count < $param->{timeout}) &&
1017 PVE::QemuServer::check_running($vmid)) {
1018 $count++;
1019 sleep 1;
1020 }
1021
1022 die "wait failed - got timeout\n" if PVE::QemuServer::check_running($vmid);
1023 }
1024
1025 return;
1026 };
1027
1028 return $rpcenv->fork_worker('qmshutdown', $vmid, $user, $realcmd);
1029 }});
1030
1031 __PACKAGE__->register_method({
1032 name => 'vm_suspend',
1033 path => '{vmid}/status/suspend',
1034 method => 'POST',
1035 protected => 1,
1036 proxyto => 'node',
1037 description => "Suspend virtual machine.",
1038 parameters => {
1039 additionalProperties => 0,
1040 properties => {
1041 node => get_standard_option('pve-node'),
1042 vmid => get_standard_option('pve-vmid'),
1043 skiplock => get_standard_option('skiplock'),
1044 },
1045 },
1046 returns => {
1047 type => 'string',
1048 },
1049 code => sub {
1050 my ($param) = @_;
1051
1052 my $rpcenv = PVE::RPCEnvironment::get();
1053
1054 my $user = $rpcenv->get_user();
1055
1056 my $node = extract_param($param, 'node');
1057
1058 my $vmid = extract_param($param, 'vmid');
1059
1060 my $skiplock = extract_param($param, 'skiplock');
1061 raise_param_exc({ skiplock => "Only root may use this option." })
1062 if $skiplock && $user ne 'root@pam';
1063
1064 my $realcmd = sub {
1065 my $upid = shift;
1066
1067 syslog('info', "suspend VM $vmid: $upid\n");
1068
1069 PVE::QemuServer::vm_suspend($vmid, $skiplock);
1070
1071 return;
1072 };
1073
1074 return $rpcenv->fork_worker('qmsuspend', $vmid, $user, $realcmd);
1075 }});
1076
1077 __PACKAGE__->register_method({
1078 name => 'vm_resume',
1079 path => '{vmid}/status/resume',
1080 method => 'POST',
1081 protected => 1,
1082 proxyto => 'node',
1083 description => "Resume virtual machine.",
1084 parameters => {
1085 additionalProperties => 0,
1086 properties => {
1087 node => get_standard_option('pve-node'),
1088 vmid => get_standard_option('pve-vmid'),
1089 skiplock => get_standard_option('skiplock'),
1090 },
1091 },
1092 returns => {
1093 type => 'string',
1094 },
1095 code => sub {
1096 my ($param) = @_;
1097
1098 my $rpcenv = PVE::RPCEnvironment::get();
1099
1100 my $user = $rpcenv->get_user();
1101
1102 my $node = extract_param($param, 'node');
1103
1104 my $vmid = extract_param($param, 'vmid');
1105
1106 my $skiplock = extract_param($param, 'skiplock');
1107 raise_param_exc({ skiplock => "Only root may use this option." })
1108 if $skiplock && $user ne 'root@pam';
1109
1110 my $realcmd = sub {
1111 my $upid = shift;
1112
1113 syslog('info', "resume VM $vmid: $upid\n");
1114
1115 PVE::QemuServer::vm_resume($vmid, $skiplock);
1116
1117 return;
1118 };
1119
1120 return $rpcenv->fork_worker('qmresume', $vmid, $user, $realcmd);
1121 }});
1122
1123 __PACKAGE__->register_method({
1124 name => 'vm_sendkey',
1125 path => '{vmid}/sendkey',
1126 method => 'PUT',
1127 protected => 1,
1128 proxyto => 'node',
1129 description => "Send key event to virtual machine.",
1130 parameters => {
1131 additionalProperties => 0,
1132 properties => {
1133 node => get_standard_option('pve-node'),
1134 vmid => get_standard_option('pve-vmid'),
1135 skiplock => get_standard_option('skiplock'),
1136 key => {
1137 description => "The key (qemu monitor encoding).",
1138 type => 'string'
1139 }
1140 },
1141 },
1142 returns => { type => 'null'},
1143 code => sub {
1144 my ($param) = @_;
1145
1146 my $rpcenv = PVE::RPCEnvironment::get();
1147
1148 my $user = $rpcenv->get_user();
1149
1150 my $node = extract_param($param, 'node');
1151
1152 my $vmid = extract_param($param, 'vmid');
1153
1154 my $skiplock = extract_param($param, 'skiplock');
1155 raise_param_exc({ skiplock => "Only root may use this option." })
1156 if $skiplock && $user ne 'root@pam';
1157
1158 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1159
1160 return;
1161 }});
1162
1163 __PACKAGE__->register_method({
1164 name => 'migrate_vm',
1165 path => '{vmid}/migrate',
1166 method => 'POST',
1167 protected => 1,
1168 proxyto => 'node',
1169 description => "Migrate virtual machine. Creates a new migration task.",
1170 parameters => {
1171 additionalProperties => 0,
1172 properties => {
1173 node => get_standard_option('pve-node'),
1174 vmid => get_standard_option('pve-vmid'),
1175 target => get_standard_option('pve-node', { description => "Target node." }),
1176 online => {
1177 type => 'boolean',
1178 description => "Use online/live migration.",
1179 optional => 1,
1180 },
1181 force => {
1182 type => 'boolean',
1183 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
1184 optional => 1,
1185 },
1186 },
1187 },
1188 returns => {
1189 type => 'string',
1190 description => "the task ID.",
1191 },
1192 code => sub {
1193 my ($param) = @_;
1194
1195 my $rpcenv = PVE::RPCEnvironment::get();
1196
1197 my $user = $rpcenv->get_user();
1198
1199 my $target = extract_param($param, 'target');
1200
1201 my $localnode = PVE::INotify::nodename();
1202 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
1203
1204 PVE::Cluster::check_cfs_quorum();
1205
1206 PVE::Cluster::check_node_exists($target);
1207
1208 my $targetip = PVE::Cluster::remote_node_ip($target);
1209
1210 my $vmid = extract_param($param, 'vmid');
1211
1212 raise_param_exc({ force => "Only root may use this option." }) if $user ne 'root@pam';
1213
1214 # test if VM exists
1215 PVE::QemuServer::load_config($vmid);
1216
1217 # try to detect errors early
1218 if (PVE::QemuServer::check_running($vmid)) {
1219 die "cant migrate running VM without --online\n"
1220 if !$param->{online};
1221 }
1222
1223 my $realcmd = sub {
1224 my $upid = shift;
1225
1226 PVE::QemuMigrate::migrate($target, $targetip, $vmid, $param->{online}, $param->{force});
1227 };
1228
1229 my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $user, $realcmd);
1230
1231 return $upid;
1232 }});
1233
1234 1;