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