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