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