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