]> git.proxmox.com Git - qemu-server.git/blame - PVE/API2/Qemu.pm
do not set cache=none for .raw files
[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 6
502d18a2 7use PVE::Cluster qw (cfs_read_file cfs_write_file);;
1e3baf05
DM
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 36
ae57f6b3 37my $check_storage_access = sub {
fcbb753e 38 my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_;
a0d1b1a2 39
ae57f6b3
DM
40 PVE::QemuServer::foreach_drive($settings, sub {
41 my ($ds, $drive) = @_;
a0d1b1a2 42
ae57f6b3 43 my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive);
a0d1b1a2 44
ae57f6b3 45 my $volid = $drive->{file};
a0d1b1a2 46
09d0ee64
DM
47 if (!$volid || $volid eq 'none') {
48 # nothing to check
f5782fd0
DM
49 } elsif ($isCDROM && ($volid eq 'cdrom')) {
50 $rpcenv->check($authuser, "/", ['Sys.Console']);
09d0ee64 51 } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) {
a0d1b1a2
DM
52 my ($storeid, $size) = ($2 || $default_storage, $3);
53 die "no storage ID specified (and no default storage)\n" if !$storeid;
fcbb753e 54 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
a0d1b1a2 55 } else {
8b192abf 56 $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
a0d1b1a2
DM
57 }
58 });
ae57f6b3 59};
a0d1b1a2 60
ae57f6b3
DM
61# Note: $pool is only needed when creating a VM, because pool permissions
62# are automatically inherited if VM already exists inside a pool.
63my $create_disks = sub {
64 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
a0d1b1a2
DM
65
66 my $vollist = [];
a0d1b1a2 67
ae57f6b3
DM
68 my $res = {};
69 PVE::QemuServer::foreach_drive($settings, sub {
70 my ($ds, $disk) = @_;
71
72 my $volid = $disk->{file};
73
f5782fd0 74 if (!$volid || $volid eq 'none' || $volid eq 'cdrom') {
628e9a2b
AD
75 delete $disk->{size};
76 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
09d0ee64 77 } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) {
ae57f6b3
DM
78 my ($storeid, $size) = ($2 || $default_storage, $3);
79 die "no storage ID specified (and no default storage)\n" if !$storeid;
80 my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
81 my $fmt = $disk->{format} || $defformat;
a0d1b1a2
DM
82 my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid,
83 $fmt, undef, $size*1024*1024);
a0d1b1a2 84 $disk->{file} = $volid;
24afaca0 85 $disk->{size} = $size*1024*1024*1024;
a0d1b1a2 86 push @$vollist, $volid;
ae57f6b3
DM
87 delete $disk->{format}; # no longer needed
88 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
89 } else {
eabe0da0 90
ba68cf09 91 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid);
eabe0da0
DM
92
93 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
35cb731c 94
35cb731c
AD
95 my $foundvolid = undef;
96
eabe0da0
DM
97 if ($storeid) {
98 PVE::Storage::activate_volumes($storecfg, [ $volid ]);
99 my $dl = PVE::Storage::vdisk_list($storecfg, $storeid, undef);
100
101 PVE::Storage::foreach_volid($dl, sub {
102 my ($volumeid) = @_;
103 if($volumeid eq $volid) {
104 $foundvolid = 1;
105 return;
106 }
107 });
108 }
35cb731c
AD
109
110 die "image '$path' does not exists\n" if (!(-f $path || -b $path || $foundvolid));
24afaca0
DM
111
112 my ($size) = PVE::Storage::volume_size_info($storecfg, $volid, 1);
113 $disk->{size} = $size;
114 $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
a0d1b1a2 115 }
ae57f6b3 116 });
a0d1b1a2
DM
117
118 # free allocated images on error
119 if (my $err = $@) {
120 syslog('err', "VM $vmid creating disks failed");
121 foreach my $volid (@$vollist) {
122 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
123 warn $@ if $@;
124 }
125 die $err;
126 }
127
128 # modify vm config if everything went well
ae57f6b3
DM
129 foreach my $ds (keys %$res) {
130 $conf->{$ds} = $res->{$ds};
a0d1b1a2
DM
131 }
132
133 return $vollist;
134};
135
136my $check_vm_modify_config_perm = sub {
ae57f6b3 137 my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_;
a0d1b1a2 138
6e5c4da7 139 return 1 if $authuser eq 'root@pam';
a0d1b1a2 140
ae57f6b3 141 foreach my $opt (@$key_list) {
a0d1b1a2
DM
142 # disk checks need to be done somewhere else
143 next if PVE::QemuServer::valid_drivename($opt);
144
145 if ($opt eq 'sockets' || $opt eq 'cores' ||
146 $opt eq 'cpu' || $opt eq 'smp' ||
ab6b35df 147 $opt eq 'cpulimit' || $opt eq 'cpuunits') {
a0d1b1a2
DM
148 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
149 } elsif ($opt eq 'boot' || $opt eq 'bootdisk') {
150 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
ccd5438f 151 } elsif ($opt eq 'memory' || $opt eq 'balloon' || $opt eq 'shares') {
a0d1b1a2
DM
152 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']);
153 } elsif ($opt eq 'args' || $opt eq 'lock') {
154 die "only root can set '$opt' config\n";
155 } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' ||
156 $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') {
157 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
158 } elsif ($opt =~ m/^net\d+$/) {
159 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
160 } else {
161 $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']);
162 }
163 }
164
165 return 1;
166};
167
1e3baf05 168__PACKAGE__->register_method({
afdb31d5
DM
169 name => 'vmlist',
170 path => '',
1e3baf05
DM
171 method => 'GET',
172 description => "Virtual machine index (per node).",
a0d1b1a2
DM
173 permissions => {
174 description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.",
175 user => 'all',
176 },
1e3baf05
DM
177 proxyto => 'node',
178 protected => 1, # qemu pid files are only readable by root
179 parameters => {
180 additionalProperties => 0,
181 properties => {
182 node => get_standard_option('pve-node'),
183 },
184 },
185 returns => {
186 type => 'array',
187 items => {
188 type => "object",
189 properties => {},
190 },
191 links => [ { rel => 'child', href => "{vmid}" } ],
192 },
193 code => sub {
194 my ($param) = @_;
195
a0d1b1a2
DM
196 my $rpcenv = PVE::RPCEnvironment::get();
197 my $authuser = $rpcenv->get_user();
198
1e3baf05
DM
199 my $vmstatus = PVE::QemuServer::vmstatus();
200
a0d1b1a2
DM
201 my $res = [];
202 foreach my $vmid (keys %$vmstatus) {
203 next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1);
204
205 my $data = $vmstatus->{$vmid};
206 $data->{vmid} = $vmid;
207 push @$res, $data;
208 }
1e3baf05 209
a0d1b1a2 210 return $res;
1e3baf05
DM
211 }});
212
213__PACKAGE__->register_method({
afdb31d5
DM
214 name => 'create_vm',
215 path => '',
1e3baf05 216 method => 'POST',
3e16d5fc 217 description => "Create or restore a virtual machine.",
a0d1b1a2
DM
218 permissions => {
219 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.",
220 check => [ 'or',
221 [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
222 [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'],
223 ],
224 },
1e3baf05
DM
225 protected => 1,
226 proxyto => 'node',
227 parameters => {
228 additionalProperties => 0,
229 properties => PVE::QemuServer::json_config_properties(
230 {
231 node => get_standard_option('pve-node'),
232 vmid => get_standard_option('pve-vmid'),
3e16d5fc
DM
233 archive => {
234 description => "The backup file.",
235 type => 'string',
236 optional => 1,
237 maxLength => 255,
238 },
239 storage => get_standard_option('pve-storage-id', {
240 description => "Default storage.",
241 optional => 1,
242 }),
243 force => {
afdb31d5 244 optional => 1,
3e16d5fc
DM
245 type => 'boolean',
246 description => "Allow to overwrite existing VM.",
51586c3a
DM
247 requires => 'archive',
248 },
249 unique => {
afdb31d5 250 optional => 1,
51586c3a
DM
251 type => 'boolean',
252 description => "Assign a unique random ethernet address.",
253 requires => 'archive',
3e16d5fc 254 },
a0d1b1a2
DM
255 pool => {
256 optional => 1,
257 type => 'string', format => 'pve-poolid',
258 description => "Add the VM to the specified pool.",
259 },
1e3baf05
DM
260 }),
261 },
afdb31d5 262 returns => {
5fdbe4f0
DM
263 type => 'string',
264 },
1e3baf05
DM
265 code => sub {
266 my ($param) = @_;
267
5fdbe4f0
DM
268 my $rpcenv = PVE::RPCEnvironment::get();
269
a0d1b1a2 270 my $authuser = $rpcenv->get_user();
5fdbe4f0 271
1e3baf05
DM
272 my $node = extract_param($param, 'node');
273
1e3baf05
DM
274 my $vmid = extract_param($param, 'vmid');
275
3e16d5fc
DM
276 my $archive = extract_param($param, 'archive');
277
278 my $storage = extract_param($param, 'storage');
279
51586c3a
DM
280 my $force = extract_param($param, 'force');
281
282 my $unique = extract_param($param, 'unique');
a0d1b1a2
DM
283
284 my $pool = extract_param($param, 'pool');
51586c3a 285
1e3baf05 286 my $filename = PVE::QemuServer::config_file($vmid);
afdb31d5
DM
287
288 my $storecfg = PVE::Storage::config();
1e3baf05 289
3e16d5fc 290 PVE::Cluster::check_cfs_quorum();
1e3baf05 291
a0d1b1a2
DM
292 if (defined($pool)) {
293 $rpcenv->check_pool_exist($pool);
a0d1b1a2
DM
294 }
295
fcbb753e 296 $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace'])
a0d1b1a2
DM
297 if defined($storage);
298
afdb31d5 299 if (!$archive) {
3e16d5fc 300 &$resolve_cdrom_alias($param);
1e3baf05 301
fcbb753e 302 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
ae57f6b3
DM
303
304 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
305
3e16d5fc
DM
306 foreach my $opt (keys %$param) {
307 if (PVE::QemuServer::valid_drivename($opt)) {
308 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
309 raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
afdb31d5 310
3e16d5fc
DM
311 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
312 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
313 }
1e3baf05 314 }
3e16d5fc
DM
315
316 PVE::QemuServer::add_random_macs($param);
51586c3a
DM
317 } else {
318 my $keystr = join(' ', keys %$param);
bc4dcb99
DM
319 raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
320
5b9d692a 321 if ($archive eq '-') {
afdb31d5 322 die "pipe requires cli environment\n"
d7810bc1 323 if $rpcenv->{type} ne 'cli';
5b9d692a 324 } else {
ba68cf09 325 my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive);
d7810bc1
DM
326
327 PVE::Storage::activate_volumes($storecfg, [ $archive ])
328 if PVE::Storage::parse_volume_id ($archive, 1);
329
971f27c4
DM
330 die "can't find archive file '$archive'\n" if !($path && -f $path);
331 $archive = $path;
332 }
1e3baf05
DM
333 }
334
502d18a2
DM
335 my $addVMtoPoolFn = sub {
336 my $usercfg = cfs_read_file("user.cfg");
337 if (my $data = $usercfg->{pools}->{$pool}) {
338 $data->{vms}->{$vmid} = 1;
339 $usercfg->{vms}->{$vmid} = $pool;
340 cfs_write_file("user.cfg", $usercfg);
341 }
342 };
343
3e16d5fc
DM
344 my $restorefn = sub {
345
346 if (-f $filename) {
afdb31d5 347 die "unable to restore vm $vmid: config file already exists\n"
51586c3a 348 if !$force;
3e16d5fc 349
afdb31d5 350 die "unable to restore vm $vmid: vm is running\n"
3e16d5fc
DM
351 if PVE::QemuServer::check_running($vmid);
352 }
353
354 my $realcmd = sub {
a0d1b1a2 355 PVE::QemuServer::restore_archive($archive, $vmid, $authuser, {
51586c3a 356 storage => $storage,
a0d1b1a2 357 pool => $pool,
51586c3a 358 unique => $unique });
502d18a2
DM
359
360 PVE::AccessControl::lock_user_config($addVMtoPoolFn, "can't add VM to pool") if $pool;
3e16d5fc
DM
361 };
362
a0d1b1a2 363 return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd);
3e16d5fc 364 };
1e3baf05 365
1e3baf05
DM
366 my $createfn = sub {
367
191435c6 368 # test after locking
afdb31d5 369 die "unable to create vm $vmid: config file already exists\n"
1e3baf05
DM
370 if -f $filename;
371
5fdbe4f0 372 my $realcmd = sub {
1e3baf05 373
5fdbe4f0 374 my $vollist = [];
1e3baf05 375
1858638f
DM
376 my $conf = $param;
377
5fdbe4f0 378 eval {
ae57f6b3 379
1858638f 380 $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage);
1e3baf05 381
5fdbe4f0
DM
382 # try to be smart about bootdisk
383 my @disks = PVE::QemuServer::disknames();
384 my $firstdisk;
385 foreach my $ds (reverse @disks) {
1858638f
DM
386 next if !$conf->{$ds};
387 my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds});
5fdbe4f0
DM
388 next if PVE::QemuServer::drive_is_cdrom($disk);
389 $firstdisk = $ds;
390 }
1e3baf05 391
1858638f
DM
392 if (!$conf->{bootdisk} && $firstdisk) {
393 $conf->{bootdisk} = $firstdisk;
5fdbe4f0 394 }
1e3baf05 395
ae9ca91d
DM
396 PVE::QemuServer::update_config_nolock($vmid, $conf);
397
5fdbe4f0
DM
398 };
399 my $err = $@;
1e3baf05 400
5fdbe4f0
DM
401 if ($err) {
402 foreach my $volid (@$vollist) {
403 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
404 warn $@ if $@;
405 }
406 die "create failed - $err";
407 }
502d18a2
DM
408
409 PVE::AccessControl::lock_user_config($addVMtoPoolFn, "can't add VM to pool") if $pool;
5fdbe4f0
DM
410 };
411
a0d1b1a2 412 return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
413 };
414
191435c6 415 return PVE::QemuServer::lock_config_full($vmid, 1, $archive ? $restorefn : $createfn);
1e3baf05
DM
416 }});
417
418__PACKAGE__->register_method({
419 name => 'vmdiridx',
afdb31d5 420 path => '{vmid}',
1e3baf05
DM
421 method => 'GET',
422 proxyto => 'node',
423 description => "Directory index",
a0d1b1a2
DM
424 permissions => {
425 user => 'all',
426 },
1e3baf05
DM
427 parameters => {
428 additionalProperties => 0,
429 properties => {
430 node => get_standard_option('pve-node'),
431 vmid => get_standard_option('pve-vmid'),
432 },
433 },
434 returns => {
435 type => 'array',
436 items => {
437 type => "object",
438 properties => {
439 subdir => { type => 'string' },
440 },
441 },
442 links => [ { rel => 'child', href => "{subdir}" } ],
443 },
444 code => sub {
445 my ($param) = @_;
446
447 my $res = [
448 { subdir => 'config' },
449 { subdir => 'status' },
450 { subdir => 'unlink' },
451 { subdir => 'vncproxy' },
3ea94c60 452 { subdir => 'migrate' },
2f48a4f5 453 { subdir => 'resize' },
1e3baf05
DM
454 { subdir => 'rrd' },
455 { subdir => 'rrddata' },
91c94f0a 456 { subdir => 'monitor' },
7e7d7b61 457 { subdir => 'snapshot' },
1e3baf05 458 ];
afdb31d5 459
1e3baf05
DM
460 return $res;
461 }});
462
463__PACKAGE__->register_method({
afdb31d5
DM
464 name => 'rrd',
465 path => '{vmid}/rrd',
1e3baf05
DM
466 method => 'GET',
467 protected => 1, # fixme: can we avoid that?
468 permissions => {
378b359e 469 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1e3baf05
DM
470 },
471 description => "Read VM RRD statistics (returns PNG)",
472 parameters => {
473 additionalProperties => 0,
474 properties => {
475 node => get_standard_option('pve-node'),
476 vmid => get_standard_option('pve-vmid'),
477 timeframe => {
478 description => "Specify the time frame you are interested in.",
479 type => 'string',
480 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
481 },
482 ds => {
483 description => "The list of datasources you want to display.",
484 type => 'string', format => 'pve-configid-list',
485 },
486 cf => {
487 description => "The RRD consolidation function",
488 type => 'string',
489 enum => [ 'AVERAGE', 'MAX' ],
490 optional => 1,
491 },
492 },
493 },
494 returns => {
495 type => "object",
496 properties => {
497 filename => { type => 'string' },
498 },
499 },
500 code => sub {
501 my ($param) = @_;
502
503 return PVE::Cluster::create_rrd_graph(
afdb31d5 504 "pve2-vm/$param->{vmid}", $param->{timeframe},
1e3baf05 505 $param->{ds}, $param->{cf});
afdb31d5 506
1e3baf05
DM
507 }});
508
509__PACKAGE__->register_method({
afdb31d5
DM
510 name => 'rrddata',
511 path => '{vmid}/rrddata',
1e3baf05
DM
512 method => 'GET',
513 protected => 1, # fixme: can we avoid that?
514 permissions => {
378b359e 515 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1e3baf05
DM
516 },
517 description => "Read VM RRD statistics",
518 parameters => {
519 additionalProperties => 0,
520 properties => {
521 node => get_standard_option('pve-node'),
522 vmid => get_standard_option('pve-vmid'),
523 timeframe => {
524 description => "Specify the time frame you are interested in.",
525 type => 'string',
526 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
527 },
528 cf => {
529 description => "The RRD consolidation function",
530 type => 'string',
531 enum => [ 'AVERAGE', 'MAX' ],
532 optional => 1,
533 },
534 },
535 },
536 returns => {
537 type => "array",
538 items => {
539 type => "object",
540 properties => {},
541 },
542 },
543 code => sub {
544 my ($param) = @_;
545
546 return PVE::Cluster::create_rrd_data(
547 "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
548 }});
549
550
551__PACKAGE__->register_method({
afdb31d5
DM
552 name => 'vm_config',
553 path => '{vmid}/config',
1e3baf05
DM
554 method => 'GET',
555 proxyto => 'node',
556 description => "Get virtual machine configuration.",
a0d1b1a2
DM
557 permissions => {
558 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
559 },
1e3baf05
DM
560 parameters => {
561 additionalProperties => 0,
562 properties => {
563 node => get_standard_option('pve-node'),
564 vmid => get_standard_option('pve-vmid'),
565 },
566 },
afdb31d5 567 returns => {
1e3baf05 568 type => "object",
554ac7e7
DM
569 properties => {
570 digest => {
571 type => 'string',
572 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
573 }
574 },
1e3baf05
DM
575 },
576 code => sub {
577 my ($param) = @_;
578
579 my $conf = PVE::QemuServer::load_config($param->{vmid});
580
22c377f0
DM
581 delete $conf->{snapshots};
582
1e3baf05
DM
583 return $conf;
584 }});
585
ae57f6b3
DM
586my $vm_is_volid_owner = sub {
587 my ($storecfg, $vmid, $volid) =@_;
588
589 if ($volid !~ m|^/|) {
590 my ($path, $owner);
591 eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
592 if ($owner && ($owner == $vmid)) {
593 return 1;
594 }
595 }
596
597 return undef;
598};
599
600my $test_deallocate_drive = sub {
601 my ($storecfg, $vmid, $key, $drive, $force) = @_;
602
603 if (!PVE::QemuServer::drive_is_cdrom($drive)) {
604 my $volid = $drive->{file};
605 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
606 if ($force || $key =~ m/^unused/) {
607 my $sid = PVE::Storage::parse_volume_id($volid);
608 return $sid;
609 }
610 }
611 }
612
613 return undef;
614};
615
5d7a6767 616my $delete_drive = sub {
1858638f 617 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
5d7a6767
DM
618
619 if (!PVE::QemuServer::drive_is_cdrom($drive)) {
620 my $volid = $drive->{file};
ae57f6b3
DM
621 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
622 if ($force || $key =~ m/^unused/) {
623 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
7e4e69a6 624 die $@ if $@;
ae57f6b3
DM
625 } else {
626 PVE::QemuServer::add_unused_volume($conf, $volid, $vmid);
5d7a6767 627 }
ae57f6b3
DM
628 }
629 }
49f9db93
DM
630
631 delete $conf->{$key};
ae57f6b3
DM
632};
633
634my $vmconfig_delete_option = sub {
635 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
636
637 return if !defined($conf->{$opt});
638
639 my $isDisk = PVE::QemuServer::valid_drivename($opt)|| ($opt =~ m/^unused/);
640
641 if ($isDisk) {
642 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
643
644 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
645 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
fcbb753e 646 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
5d7a6767 647 }
ae57f6b3
DM
648 }
649
650 die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
651
652 if ($isDisk) {
653 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
654 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
5d7a6767 655 } else {
ae57f6b3
DM
656 delete $conf->{$opt};
657 }
658
659 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
660};
661
9bf371a6 662my $safe_num_ne = sub {
93ae06e1
DM
663 my ($a, $b) = @_;
664
665 return 0 if !defined($a) && !defined($b);
666 return 1 if !defined($a);
667 return 1 if !defined($b);
668
669 return $a != $b;
670};
671
ae57f6b3
DM
672my $vmconfig_update_disk = sub {
673 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
5d7a6767 674
ae57f6b3
DM
675 my $drive = PVE::QemuServer::parse_drive($opt, $value);
676
677 if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom
678 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
679 } else {
680 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
681 }
682
683 if ($conf->{$opt}) {
684
685 if (my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt})) {
686
687 my $media = $drive->{media} || 'disk';
688 my $oldmedia = $old_drive->{media} || 'disk';
689 die "unable to change media type\n" if $media ne $oldmedia;
690
691 if (!PVE::QemuServer::drive_is_cdrom($old_drive) &&
692 ($drive->{file} ne $old_drive->{file})) { # delete old disks
693
694 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
695 $conf = PVE::QemuServer::load_config($vmid); # update/reload
696 }
0f56d571 697
9bf371a6
DM
698 if(&$safe_num_ne($drive->{mbps}, $old_drive->{mbps}) ||
699 &$safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) ||
700 &$safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) ||
701 &$safe_num_ne($drive->{iops}, $old_drive->{iops}) ||
702 &$safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) ||
703 &$safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr})) {
704 PVE::QemuServer::qemu_block_set_io_throttle($vmid,"drive-$opt", $drive->{mbps}*1024*1024,
705 $drive->{mbps_rd}*1024*1024, $drive->{mbps_wr}*1024*1024,
706 $drive->{iops}, $drive->{iops_rd}, $drive->{iops_wr})
707 if !PVE::QemuServer::drive_is_cdrom($drive);
0f56d571 708 }
ae57f6b3
DM
709 }
710 }
711
712 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
713 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
714
715 $conf = PVE::QemuServer::load_config($vmid); # update/reload
716 $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
717
718 if (PVE::QemuServer::drive_is_cdrom($drive)) { # cdrom
719
720 if (PVE::QemuServer::check_running($vmid)) {
721 if ($drive->{file} eq 'none') {
ce156282 722 PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
ae57f6b3
DM
723 } else {
724 my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
ce156282
AD
725 PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt"); #force eject if locked
726 PVE::QemuServer::vm_mon_cmd($vmid, "change",device => "drive-$opt",target => "$path") if $path;
ae57f6b3
DM
727 }
728 }
729
730 } else { # hotplug new disks
731
732 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
5d7a6767 733 }
5d7a6767
DM
734};
735
ae57f6b3
DM
736my $vmconfig_update_net = sub {
737 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
738
739 if ($conf->{$opt}) {
740 #if online update, then unplug first
741 die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
742 }
743
744 $conf->{$opt} = $value;
745 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
746 $conf = PVE::QemuServer::load_config($vmid); # update/reload
747
748 my $net = PVE::QemuServer::parse_net($conf->{$opt});
749
750 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net);
751};
752
a0d1b1a2
DM
753my $vm_config_perm_list = [
754 'VM.Config.Disk',
755 'VM.Config.CDROM',
756 'VM.Config.CPU',
757 'VM.Config.Memory',
758 'VM.Config.Network',
759 'VM.Config.HWType',
760 'VM.Config.Options',
761 ];
762
1e3baf05 763__PACKAGE__->register_method({
afdb31d5
DM
764 name => 'update_vm',
765 path => '{vmid}/config',
1e3baf05
DM
766 method => 'PUT',
767 protected => 1,
768 proxyto => 'node',
769 description => "Set virtual machine options.",
a0d1b1a2
DM
770 permissions => {
771 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
772 },
1e3baf05
DM
773 parameters => {
774 additionalProperties => 0,
775 properties => PVE::QemuServer::json_config_properties(
776 {
777 node => get_standard_option('pve-node'),
778 vmid => get_standard_option('pve-vmid'),
3ea94c60 779 skiplock => get_standard_option('skiplock'),
1e3baf05
DM
780 delete => {
781 type => 'string', format => 'pve-configid-list',
782 description => "A list of settings you want to delete.",
783 optional => 1,
784 },
785 force => {
786 type => 'boolean',
787 description => $opt_force_description,
788 optional => 1,
789 requires => 'delete',
790 },
554ac7e7
DM
791 digest => {
792 type => 'string',
793 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
794 maxLength => 40,
afdb31d5 795 optional => 1,
554ac7e7 796 }
1e3baf05
DM
797 }),
798 },
799 returns => { type => 'null'},
800 code => sub {
801 my ($param) = @_;
802
803 my $rpcenv = PVE::RPCEnvironment::get();
804
a0d1b1a2 805 my $authuser = $rpcenv->get_user();
1e3baf05
DM
806
807 my $node = extract_param($param, 'node');
808
1e3baf05
DM
809 my $vmid = extract_param($param, 'vmid');
810
5fdbe4f0
DM
811 my $digest = extract_param($param, 'digest');
812
813 my @paramarr = (); # used for log message
814 foreach my $key (keys %$param) {
815 push @paramarr, "-$key", $param->{$key};
816 }
817
1e3baf05 818 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 819 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 820 if $skiplock && $authuser ne 'root@pam';
1e3baf05 821
0532bc63
DM
822 my $delete_str = extract_param($param, 'delete');
823
1e3baf05
DM
824 my $force = extract_param($param, 'force');
825
0532bc63
DM
826 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
827
1e68cb19
DM
828 my $storecfg = PVE::Storage::config();
829
7bfdeb5f
DM
830 my $defaults = PVE::QemuServer::load_defaults();
831
1e68cb19
DM
832 &$resolve_cdrom_alias($param);
833
834 # now try to verify all parameters
835
0532bc63
DM
836 my @delete = ();
837 foreach my $opt (PVE::Tools::split_list($delete_str)) {
838 $opt = 'ide2' if $opt eq 'cdrom';
839 raise_param_exc({ delete => "you can't use '-$opt' and " .
840 "-delete $opt' at the same time" })
841 if defined($param->{$opt});
842
843 if (!PVE::QemuServer::option_exists($opt)) {
844 raise_param_exc({ delete => "unknown option '$opt'" });
845 }
ae57f6b3 846
0532bc63
DM
847 push @delete, $opt;
848 }
1e3baf05 849
1e68cb19
DM
850 foreach my $opt (keys %$param) {
851 if (PVE::QemuServer::valid_drivename($opt)) {
852 # cleanup drive path
853 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
854 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
855 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
1e68cb19
DM
856 } elsif ($opt =~ m/^net(\d+)$/) {
857 # add macaddr
858 my $net = PVE::QemuServer::parse_net($param->{$opt});
859 $param->{$opt} = PVE::QemuServer::print_net($net);
1e68cb19
DM
860 }
861 }
1e3baf05 862
ae57f6b3
DM
863 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
864
865 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
866
fcbb753e 867 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
1e3baf05 868
5d39a182 869 my $updatefn = sub {
1e3baf05 870
5d39a182 871 my $conf = PVE::QemuServer::load_config($vmid);
1e3baf05 872
5d39a182
DM
873 die "checksum missmatch (file change by other user?)\n"
874 if $digest && $digest ne $conf->{digest};
1e3baf05 875
5d39a182 876 PVE::QemuServer::check_lock($conf) if !$skiplock;
1e3baf05 877
7bfdeb5f
DM
878 if ($param->{memory} || defined($param->{balloon})) {
879 my $maxmem = $param->{memory} || $conf->{memory} || $defaults->{memory};
880 my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{balloon};
881
882 die "balloon value too large (must be smaller than assigned memory)\n"
883 if $balloon > $maxmem;
884 }
885
a0d1b1a2 886 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
c2a64aa7 887
5d7a6767 888 foreach my $opt (@delete) { # delete
1e68cb19 889 $conf = PVE::QemuServer::load_config($vmid); # update/reload
ae57f6b3 890 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
5d39a182 891 }
1e3baf05 892
7bfdeb5f
DM
893 my $running = PVE::QemuServer::check_running($vmid);
894
5d7a6767 895 foreach my $opt (keys %$param) { # add/change
1e3baf05 896
1e68cb19 897 $conf = PVE::QemuServer::load_config($vmid); # update/reload
1e3baf05 898
5d7a6767
DM
899 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
900
ae57f6b3 901 if (PVE::QemuServer::valid_drivename($opt)) {
5d7a6767 902
ae57f6b3
DM
903 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
904 $opt, $param->{$opt}, $force);
1e68cb19 905
ae57f6b3 906 } elsif ($opt =~ m/^net(\d+)$/) { #nics
1858638f 907
ae57f6b3
DM
908 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
909 $opt, $param->{$opt});
1e68cb19
DM
910
911 } else {
912
1858638f
DM
913 $conf->{$opt} = $param->{$opt};
914 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
3a1e36bb 915 }
5d39a182 916 }
7bfdeb5f
DM
917
918 # allow manual ballooning if shares is set to zero
919 if ($running && defined($param->{balloon}) &&
920 defined($conf->{shares}) && ($conf->{shares} == 0)) {
921 my $balloon = $param->{'balloon'} || $conf->{memory} || $defaults->{memory};
922 PVE::QemuServer::vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
923 }
924
5d39a182
DM
925 };
926
927 PVE::QemuServer::lock_config($vmid, $updatefn);
fcdb0117 928
1e3baf05
DM
929 return undef;
930 }});
931
932
933__PACKAGE__->register_method({
afdb31d5
DM
934 name => 'destroy_vm',
935 path => '{vmid}',
1e3baf05
DM
936 method => 'DELETE',
937 protected => 1,
938 proxyto => 'node',
939 description => "Destroy the vm (also delete all used/owned volumes).",
a0d1b1a2
DM
940 permissions => {
941 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
942 },
1e3baf05
DM
943 parameters => {
944 additionalProperties => 0,
945 properties => {
946 node => get_standard_option('pve-node'),
947 vmid => get_standard_option('pve-vmid'),
3ea94c60 948 skiplock => get_standard_option('skiplock'),
1e3baf05
DM
949 },
950 },
afdb31d5 951 returns => {
5fdbe4f0
DM
952 type => 'string',
953 },
1e3baf05
DM
954 code => sub {
955 my ($param) = @_;
956
957 my $rpcenv = PVE::RPCEnvironment::get();
958
a0d1b1a2 959 my $authuser = $rpcenv->get_user();
1e3baf05
DM
960
961 my $vmid = $param->{vmid};
962
963 my $skiplock = $param->{skiplock};
afdb31d5 964 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 965 if $skiplock && $authuser ne 'root@pam';
1e3baf05 966
5fdbe4f0
DM
967 # test if VM exists
968 my $conf = PVE::QemuServer::load_config($vmid);
969
afdb31d5 970 my $storecfg = PVE::Storage::config();
1e3baf05 971
502d18a2
DM
972 my $delVMfromPoolFn = sub {
973 my $usercfg = cfs_read_file("user.cfg");
5d0094ea
DM
974 if (my $pool = $usercfg->{vms}->{$vmid}) {
975 if (my $data = $usercfg->{pools}->{$pool}) {
976 delete $data->{vms}->{$vmid};
977 delete $usercfg->{vms}->{$vmid};
978 cfs_write_file("user.cfg", $usercfg);
979 }
502d18a2
DM
980 }
981 };
982
5fdbe4f0 983 my $realcmd = sub {
ff1a2432
DM
984 my $upid = shift;
985
986 syslog('info', "destroy VM $vmid: $upid\n");
987
5fdbe4f0 988 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
502d18a2
DM
989
990 PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed");
5fdbe4f0 991 };
1e3baf05 992
a0d1b1a2 993 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
1e3baf05
DM
994 }});
995
996__PACKAGE__->register_method({
afdb31d5
DM
997 name => 'unlink',
998 path => '{vmid}/unlink',
1e3baf05
DM
999 method => 'PUT',
1000 protected => 1,
1001 proxyto => 'node',
1002 description => "Unlink/delete disk images.",
a0d1b1a2
DM
1003 permissions => {
1004 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
1005 },
1e3baf05
DM
1006 parameters => {
1007 additionalProperties => 0,
1008 properties => {
1009 node => get_standard_option('pve-node'),
1010 vmid => get_standard_option('pve-vmid'),
1011 idlist => {
1012 type => 'string', format => 'pve-configid-list',
1013 description => "A list of disk IDs you want to delete.",
1014 },
1015 force => {
1016 type => 'boolean',
1017 description => $opt_force_description,
1018 optional => 1,
1019 },
1020 },
1021 },
1022 returns => { type => 'null'},
1023 code => sub {
1024 my ($param) = @_;
1025
1026 $param->{delete} = extract_param($param, 'idlist');
1027
1028 __PACKAGE__->update_vm($param);
1029
1030 return undef;
1031 }});
1032
1033my $sslcert;
1034
1035__PACKAGE__->register_method({
afdb31d5
DM
1036 name => 'vncproxy',
1037 path => '{vmid}/vncproxy',
1e3baf05
DM
1038 method => 'POST',
1039 protected => 1,
1040 permissions => {
378b359e 1041 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1e3baf05
DM
1042 },
1043 description => "Creates a TCP VNC proxy connections.",
1044 parameters => {
1045 additionalProperties => 0,
1046 properties => {
1047 node => get_standard_option('pve-node'),
1048 vmid => get_standard_option('pve-vmid'),
1049 },
1050 },
afdb31d5 1051 returns => {
1e3baf05
DM
1052 additionalProperties => 0,
1053 properties => {
1054 user => { type => 'string' },
1055 ticket => { type => 'string' },
1056 cert => { type => 'string' },
1057 port => { type => 'integer' },
1058 upid => { type => 'string' },
1059 },
1060 },
1061 code => sub {
1062 my ($param) = @_;
1063
1064 my $rpcenv = PVE::RPCEnvironment::get();
1065
a0d1b1a2 1066 my $authuser = $rpcenv->get_user();
1e3baf05
DM
1067
1068 my $vmid = $param->{vmid};
1069 my $node = $param->{node};
1070
b6f39da2
DM
1071 my $authpath = "/vms/$vmid";
1072
a0d1b1a2 1073 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
b6f39da2 1074
1e3baf05
DM
1075 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
1076 if !$sslcert;
1077
1078 my $port = PVE::Tools::next_vnc_port();
1079
1080 my $remip;
afdb31d5 1081
4f1be36c 1082 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1e3baf05
DM
1083 $remip = PVE::Cluster::remote_node_ip($node);
1084 }
1085
6bb726c9
SP
1086 # NOTE: kvm VNC traffic is already TLS encrypted
1087 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', $remip] : [];
1e3baf05 1088
afdb31d5 1089 my $timeout = 10;
1e3baf05
DM
1090
1091 my $realcmd = sub {
1092 my $upid = shift;
1093
1094 syslog('info', "starting vnc proxy $upid\n");
1095
1096 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1097
1098 my $qmstr = join(' ', @$qmcmd);
1099
1100 # also redirect stderr (else we get RFB protocol errors)
be62c45c 1101 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
1e3baf05 1102
be62c45c 1103 PVE::Tools::run_command($cmd);
1e3baf05
DM
1104
1105 return;
1106 };
1107
a0d1b1a2 1108 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1e3baf05 1109
3da85107
DM
1110 PVE::Tools::wait_for_vnc_port($port);
1111
1e3baf05 1112 return {
a0d1b1a2 1113 user => $authuser,
1e3baf05 1114 ticket => $ticket,
afdb31d5
DM
1115 port => $port,
1116 upid => $upid,
1117 cert => $sslcert,
1e3baf05
DM
1118 };
1119 }});
1120
5fdbe4f0
DM
1121__PACKAGE__->register_method({
1122 name => 'vmcmdidx',
afdb31d5 1123 path => '{vmid}/status',
5fdbe4f0
DM
1124 method => 'GET',
1125 proxyto => 'node',
1126 description => "Directory index",
a0d1b1a2
DM
1127 permissions => {
1128 user => 'all',
1129 },
5fdbe4f0
DM
1130 parameters => {
1131 additionalProperties => 0,
1132 properties => {
1133 node => get_standard_option('pve-node'),
1134 vmid => get_standard_option('pve-vmid'),
1135 },
1136 },
1137 returns => {
1138 type => 'array',
1139 items => {
1140 type => "object",
1141 properties => {
1142 subdir => { type => 'string' },
1143 },
1144 },
1145 links => [ { rel => 'child', href => "{subdir}" } ],
1146 },
1147 code => sub {
1148 my ($param) = @_;
1149
1150 # test if VM exists
1151 my $conf = PVE::QemuServer::load_config($param->{vmid});
1152
1153 my $res = [
1154 { subdir => 'current' },
1155 { subdir => 'start' },
1156 { subdir => 'stop' },
1157 ];
afdb31d5 1158
5fdbe4f0
DM
1159 return $res;
1160 }});
1161
88fc87b4
DM
1162my $vm_is_ha_managed = sub {
1163 my ($vmid) = @_;
1164
1165 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1166 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $vmid, 1)) {
1167 return 1;
1168 }
1169 return 0;
1170};
1171
1e3baf05 1172__PACKAGE__->register_method({
afdb31d5 1173 name => 'vm_status',
5fdbe4f0 1174 path => '{vmid}/status/current',
1e3baf05
DM
1175 method => 'GET',
1176 proxyto => 'node',
1177 protected => 1, # qemu pid files are only readable by root
1178 description => "Get virtual machine status.",
a0d1b1a2
DM
1179 permissions => {
1180 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1181 },
1e3baf05
DM
1182 parameters => {
1183 additionalProperties => 0,
1184 properties => {
1185 node => get_standard_option('pve-node'),
1186 vmid => get_standard_option('pve-vmid'),
1187 },
1188 },
1189 returns => { type => 'object' },
1190 code => sub {
1191 my ($param) = @_;
1192
1193 # test if VM exists
1194 my $conf = PVE::QemuServer::load_config($param->{vmid});
1195
03a33f30 1196 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
8610701a 1197 my $status = $vmstatus->{$param->{vmid}};
1e3baf05 1198
88fc87b4 1199 $status->{ha} = &$vm_is_ha_managed($param->{vmid});
8610701a
DM
1200
1201 return $status;
1e3baf05
DM
1202 }});
1203
1204__PACKAGE__->register_method({
afdb31d5 1205 name => 'vm_start',
5fdbe4f0
DM
1206 path => '{vmid}/status/start',
1207 method => 'POST',
1e3baf05
DM
1208 protected => 1,
1209 proxyto => 'node',
5fdbe4f0 1210 description => "Start virtual machine.",
a0d1b1a2
DM
1211 permissions => {
1212 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1213 },
1e3baf05
DM
1214 parameters => {
1215 additionalProperties => 0,
1216 properties => {
1217 node => get_standard_option('pve-node'),
1218 vmid => get_standard_option('pve-vmid'),
3ea94c60
DM
1219 skiplock => get_standard_option('skiplock'),
1220 stateuri => get_standard_option('pve-qm-stateuri'),
7e8dcf2c
AD
1221 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
1222
1e3baf05
DM
1223 },
1224 },
afdb31d5 1225 returns => {
5fdbe4f0
DM
1226 type => 'string',
1227 },
1e3baf05
DM
1228 code => sub {
1229 my ($param) = @_;
1230
1231 my $rpcenv = PVE::RPCEnvironment::get();
1232
a0d1b1a2 1233 my $authuser = $rpcenv->get_user();
1e3baf05
DM
1234
1235 my $node = extract_param($param, 'node');
1236
1e3baf05
DM
1237 my $vmid = extract_param($param, 'vmid');
1238
3ea94c60 1239 my $stateuri = extract_param($param, 'stateuri');
afdb31d5 1240 raise_param_exc({ stateuri => "Only root may use this option." })
a0d1b1a2 1241 if $stateuri && $authuser ne 'root@pam';
3ea94c60 1242
1e3baf05 1243 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1244 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1245 if $skiplock && $authuser ne 'root@pam';
1e3baf05 1246
7e8dcf2c
AD
1247 my $migratedfrom = extract_param($param, 'migratedfrom');
1248 raise_param_exc({ migratedfrom => "Only root may use this option." })
1249 if $migratedfrom && $authuser ne 'root@pam';
1250
afdb31d5 1251 my $storecfg = PVE::Storage::config();
5fdbe4f0 1252
cce37749
DM
1253 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1254 $rpcenv->{type} ne 'ha') {
5fdbe4f0 1255
88fc87b4
DM
1256 my $hacmd = sub {
1257 my $upid = shift;
5fdbe4f0 1258
88fc87b4 1259 my $service = "pvevm:$vmid";
5fdbe4f0 1260
88fc87b4
DM
1261 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1262
1263 print "Executing HA start for VM $vmid\n";
1264
1265 PVE::Tools::run_command($cmd);
1266
1267 return;
1268 };
1269
1270 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1271
1272 } else {
1273
1274 my $realcmd = sub {
1275 my $upid = shift;
1276
1277 syslog('info', "start VM $vmid: $upid\n");
1278
7e8dcf2c 1279 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom);
88fc87b4
DM
1280
1281 return;
1282 };
5fdbe4f0 1283
88fc87b4
DM
1284 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1285 }
5fdbe4f0
DM
1286 }});
1287
1288__PACKAGE__->register_method({
afdb31d5 1289 name => 'vm_stop',
5fdbe4f0
DM
1290 path => '{vmid}/status/stop',
1291 method => 'POST',
1292 protected => 1,
1293 proxyto => 'node',
1294 description => "Stop virtual machine.",
a0d1b1a2
DM
1295 permissions => {
1296 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1297 },
5fdbe4f0
DM
1298 parameters => {
1299 additionalProperties => 0,
1300 properties => {
1301 node => get_standard_option('pve-node'),
1302 vmid => get_standard_option('pve-vmid'),
1303 skiplock => get_standard_option('skiplock'),
af30308f 1304 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
c6bb9502
DM
1305 timeout => {
1306 description => "Wait maximal timeout seconds.",
1307 type => 'integer',
1308 minimum => 0,
1309 optional => 1,
254575e9
DM
1310 },
1311 keepActive => {
1312 description => "Do not decativate storage volumes.",
1313 type => 'boolean',
1314 optional => 1,
1315 default => 0,
c6bb9502 1316 }
5fdbe4f0
DM
1317 },
1318 },
afdb31d5 1319 returns => {
5fdbe4f0
DM
1320 type => 'string',
1321 },
1322 code => sub {
1323 my ($param) = @_;
1324
1325 my $rpcenv = PVE::RPCEnvironment::get();
1326
a0d1b1a2 1327 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1328
1329 my $node = extract_param($param, 'node');
1330
1331 my $vmid = extract_param($param, 'vmid');
1332
1333 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1334 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1335 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1336
254575e9 1337 my $keepActive = extract_param($param, 'keepActive');
afdb31d5 1338 raise_param_exc({ keepActive => "Only root may use this option." })
a0d1b1a2 1339 if $keepActive && $authuser ne 'root@pam';
254575e9 1340
af30308f
DM
1341 my $migratedfrom = extract_param($param, 'migratedfrom');
1342 raise_param_exc({ migratedfrom => "Only root may use this option." })
1343 if $migratedfrom && $authuser ne 'root@pam';
1344
1345
ff1a2432
DM
1346 my $storecfg = PVE::Storage::config();
1347
3be30d63 1348 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
5fdbe4f0 1349
88fc87b4
DM
1350 my $hacmd = sub {
1351 my $upid = shift;
5fdbe4f0 1352
88fc87b4 1353 my $service = "pvevm:$vmid";
c6bb9502 1354
88fc87b4
DM
1355 my $cmd = ['clusvcadm', '-d', $service];
1356
1357 print "Executing HA stop for VM $vmid\n";
1358
1359 PVE::Tools::run_command($cmd);
5fdbe4f0 1360
88fc87b4
DM
1361 return;
1362 };
1363
1364 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1365
1366 } else {
1367 my $realcmd = sub {
1368 my $upid = shift;
1369
1370 syslog('info', "stop VM $vmid: $upid\n");
1371
1372 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
af30308f 1373 $param->{timeout}, 0, 1, $keepActive, $migratedfrom);
88fc87b4
DM
1374
1375 return;
1376 };
1377
1378 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1379 }
5fdbe4f0
DM
1380 }});
1381
1382__PACKAGE__->register_method({
afdb31d5 1383 name => 'vm_reset',
5fdbe4f0
DM
1384 path => '{vmid}/status/reset',
1385 method => 'POST',
1386 protected => 1,
1387 proxyto => 'node',
1388 description => "Reset virtual machine.",
a0d1b1a2
DM
1389 permissions => {
1390 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1391 },
5fdbe4f0
DM
1392 parameters => {
1393 additionalProperties => 0,
1394 properties => {
1395 node => get_standard_option('pve-node'),
1396 vmid => get_standard_option('pve-vmid'),
1397 skiplock => get_standard_option('skiplock'),
1398 },
1399 },
afdb31d5 1400 returns => {
5fdbe4f0
DM
1401 type => 'string',
1402 },
1403 code => sub {
1404 my ($param) = @_;
1405
1406 my $rpcenv = PVE::RPCEnvironment::get();
1407
a0d1b1a2 1408 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1409
1410 my $node = extract_param($param, 'node');
1411
1412 my $vmid = extract_param($param, 'vmid');
1413
1414 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1415 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1416 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1417
ff1a2432
DM
1418 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1419
5fdbe4f0
DM
1420 my $realcmd = sub {
1421 my $upid = shift;
1422
1e3baf05 1423 PVE::QemuServer::vm_reset($vmid, $skiplock);
5fdbe4f0
DM
1424
1425 return;
1426 };
1427
a0d1b1a2 1428 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
1429 }});
1430
1431__PACKAGE__->register_method({
afdb31d5 1432 name => 'vm_shutdown',
5fdbe4f0
DM
1433 path => '{vmid}/status/shutdown',
1434 method => 'POST',
1435 protected => 1,
1436 proxyto => 'node',
1437 description => "Shutdown virtual machine.",
a0d1b1a2
DM
1438 permissions => {
1439 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1440 },
5fdbe4f0
DM
1441 parameters => {
1442 additionalProperties => 0,
1443 properties => {
1444 node => get_standard_option('pve-node'),
1445 vmid => get_standard_option('pve-vmid'),
1446 skiplock => get_standard_option('skiplock'),
c6bb9502
DM
1447 timeout => {
1448 description => "Wait maximal timeout seconds.",
1449 type => 'integer',
1450 minimum => 0,
1451 optional => 1,
9269013a
DM
1452 },
1453 forceStop => {
1454 description => "Make sure the VM stops.",
1455 type => 'boolean',
1456 optional => 1,
1457 default => 0,
254575e9
DM
1458 },
1459 keepActive => {
1460 description => "Do not decativate storage volumes.",
1461 type => 'boolean',
1462 optional => 1,
1463 default => 0,
c6bb9502 1464 }
5fdbe4f0
DM
1465 },
1466 },
afdb31d5 1467 returns => {
5fdbe4f0
DM
1468 type => 'string',
1469 },
1470 code => sub {
1471 my ($param) = @_;
1472
1473 my $rpcenv = PVE::RPCEnvironment::get();
1474
a0d1b1a2 1475 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1476
1477 my $node = extract_param($param, 'node');
1478
1479 my $vmid = extract_param($param, 'vmid');
1480
1481 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1482 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1483 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1484
254575e9 1485 my $keepActive = extract_param($param, 'keepActive');
afdb31d5 1486 raise_param_exc({ keepActive => "Only root may use this option." })
a0d1b1a2 1487 if $keepActive && $authuser ne 'root@pam';
254575e9 1488
02d07cf5
DM
1489 my $storecfg = PVE::Storage::config();
1490
5fdbe4f0
DM
1491 my $realcmd = sub {
1492 my $upid = shift;
1493
1494 syslog('info', "shutdown VM $vmid: $upid\n");
1495
afdb31d5 1496 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
254575e9 1497 1, $param->{forceStop}, $keepActive);
c6bb9502 1498
5fdbe4f0
DM
1499 return;
1500 };
1501
a0d1b1a2 1502 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
1503 }});
1504
1505__PACKAGE__->register_method({
afdb31d5 1506 name => 'vm_suspend',
5fdbe4f0
DM
1507 path => '{vmid}/status/suspend',
1508 method => 'POST',
1509 protected => 1,
1510 proxyto => 'node',
1511 description => "Suspend virtual machine.",
a0d1b1a2
DM
1512 permissions => {
1513 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1514 },
5fdbe4f0
DM
1515 parameters => {
1516 additionalProperties => 0,
1517 properties => {
1518 node => get_standard_option('pve-node'),
1519 vmid => get_standard_option('pve-vmid'),
1520 skiplock => get_standard_option('skiplock'),
1521 },
1522 },
afdb31d5 1523 returns => {
5fdbe4f0
DM
1524 type => 'string',
1525 },
1526 code => sub {
1527 my ($param) = @_;
1528
1529 my $rpcenv = PVE::RPCEnvironment::get();
1530
a0d1b1a2 1531 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1532
1533 my $node = extract_param($param, 'node');
1534
1535 my $vmid = extract_param($param, 'vmid');
1536
1537 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1538 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1539 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1540
ff1a2432
DM
1541 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1542
5fdbe4f0
DM
1543 my $realcmd = sub {
1544 my $upid = shift;
1545
1546 syslog('info', "suspend VM $vmid: $upid\n");
1547
1e3baf05 1548 PVE::QemuServer::vm_suspend($vmid, $skiplock);
5fdbe4f0
DM
1549
1550 return;
1551 };
1552
a0d1b1a2 1553 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
1554 }});
1555
1556__PACKAGE__->register_method({
afdb31d5 1557 name => 'vm_resume',
5fdbe4f0
DM
1558 path => '{vmid}/status/resume',
1559 method => 'POST',
1560 protected => 1,
1561 proxyto => 'node',
1562 description => "Resume virtual machine.",
a0d1b1a2
DM
1563 permissions => {
1564 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1565 },
5fdbe4f0
DM
1566 parameters => {
1567 additionalProperties => 0,
1568 properties => {
1569 node => get_standard_option('pve-node'),
1570 vmid => get_standard_option('pve-vmid'),
1571 skiplock => get_standard_option('skiplock'),
1572 },
1573 },
afdb31d5 1574 returns => {
5fdbe4f0
DM
1575 type => 'string',
1576 },
1577 code => sub {
1578 my ($param) = @_;
1579
1580 my $rpcenv = PVE::RPCEnvironment::get();
1581
a0d1b1a2 1582 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1583
1584 my $node = extract_param($param, 'node');
1585
1586 my $vmid = extract_param($param, 'vmid');
1587
1588 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1589 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1590 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1591
b7eeab21 1592 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
ff1a2432 1593
5fdbe4f0
DM
1594 my $realcmd = sub {
1595 my $upid = shift;
1596
1597 syslog('info', "resume VM $vmid: $upid\n");
1598
1e3baf05 1599 PVE::QemuServer::vm_resume($vmid, $skiplock);
1e3baf05 1600
5fdbe4f0
DM
1601 return;
1602 };
1603
a0d1b1a2 1604 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
1605 }});
1606
1607__PACKAGE__->register_method({
afdb31d5 1608 name => 'vm_sendkey',
5fdbe4f0
DM
1609 path => '{vmid}/sendkey',
1610 method => 'PUT',
1611 protected => 1,
1612 proxyto => 'node',
1613 description => "Send key event to virtual machine.",
a0d1b1a2
DM
1614 permissions => {
1615 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1616 },
5fdbe4f0
DM
1617 parameters => {
1618 additionalProperties => 0,
1619 properties => {
1620 node => get_standard_option('pve-node'),
1621 vmid => get_standard_option('pve-vmid'),
1622 skiplock => get_standard_option('skiplock'),
1623 key => {
1624 description => "The key (qemu monitor encoding).",
1625 type => 'string'
1626 }
1627 },
1628 },
1629 returns => { type => 'null'},
1630 code => sub {
1631 my ($param) = @_;
1632
1633 my $rpcenv = PVE::RPCEnvironment::get();
1634
a0d1b1a2 1635 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1636
1637 my $node = extract_param($param, 'node');
1638
1639 my $vmid = extract_param($param, 'vmid');
1640
1641 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1642 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1643 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0
DM
1644
1645 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1646
1647 return;
1e3baf05
DM
1648 }});
1649
1ac0d2ee
AD
1650__PACKAGE__->register_method({
1651 name => 'vm_feature',
1652 path => '{vmid}/feature',
1653 method => 'GET',
1654 proxyto => 'node',
1655 protected => 1,
1656 description => "Check if feature for virtual machine is available.",
1657 permissions => {
1658 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1659 },
1660 parameters => {
1661 additionalProperties => 0,
1662 properties => {
1663 node => get_standard_option('pve-node'),
1664 vmid => get_standard_option('pve-vmid'),
1665 feature => {
1666 description => "Feature to check.",
1667 type => 'string',
1668 enum => [ 'snapshot', 'clone' ],
1669 },
1670 snapname => get_standard_option('pve-snapshot-name', {
1671 optional => 1,
1672 }),
1673 },
1674
1675 },
1676 returns => {
1677 type => 'boolean'
1678 },
1679 code => sub {
1680 my ($param) = @_;
1681
1682 my $node = extract_param($param, 'node');
1683
1684 my $vmid = extract_param($param, 'vmid');
1685
1686 my $snapname = extract_param($param, 'snapname');
1687
1688 my $feature = extract_param($param, 'feature');
1689
1690 my $running = PVE::QemuServer::check_running($vmid);
1691
1692 my $conf = PVE::QemuServer::load_config($vmid);
1693
1694 if($snapname){
1695 my $snap = $conf->{snapshots}->{$snapname};
1696 die "snapshot '$snapname' does not exist\n" if !defined($snap);
1697 $conf = $snap;
1698 }
1699 my $storecfg = PVE::Storage::config();
1700
1701 my $hasfeature = PVE::QemuServer::has_feature($feature, $conf, $storecfg, $snapname, $running);
1702 my $res = $hasfeature ? 1 : 0 ;
1703 return $res;
1704 }});
1705
3ea94c60 1706__PACKAGE__->register_method({
afdb31d5 1707 name => 'migrate_vm',
3ea94c60
DM
1708 path => '{vmid}/migrate',
1709 method => 'POST',
1710 protected => 1,
1711 proxyto => 'node',
1712 description => "Migrate virtual machine. Creates a new migration task.",
a0d1b1a2
DM
1713 permissions => {
1714 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1715 },
3ea94c60
DM
1716 parameters => {
1717 additionalProperties => 0,
1718 properties => {
1719 node => get_standard_option('pve-node'),
1720 vmid => get_standard_option('pve-vmid'),
1721 target => get_standard_option('pve-node', { description => "Target node." }),
1722 online => {
1723 type => 'boolean',
1724 description => "Use online/live migration.",
1725 optional => 1,
1726 },
1727 force => {
1728 type => 'boolean',
1729 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
1730 optional => 1,
1731 },
1732 },
1733 },
afdb31d5 1734 returns => {
3ea94c60
DM
1735 type => 'string',
1736 description => "the task ID.",
1737 },
1738 code => sub {
1739 my ($param) = @_;
1740
1741 my $rpcenv = PVE::RPCEnvironment::get();
1742
a0d1b1a2 1743 my $authuser = $rpcenv->get_user();
3ea94c60
DM
1744
1745 my $target = extract_param($param, 'target');
1746
1747 my $localnode = PVE::INotify::nodename();
1748 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
1749
1750 PVE::Cluster::check_cfs_quorum();
1751
1752 PVE::Cluster::check_node_exists($target);
1753
1754 my $targetip = PVE::Cluster::remote_node_ip($target);
1755
1756 my $vmid = extract_param($param, 'vmid');
1757
afdb31d5 1758 raise_param_exc({ force => "Only root may use this option." })
a0d1b1a2 1759 if $param->{force} && $authuser ne 'root@pam';
3ea94c60
DM
1760
1761 # test if VM exists
a5ed42d3 1762 my $conf = PVE::QemuServer::load_config($vmid);
3ea94c60
DM
1763
1764 # try to detect errors early
a5ed42d3
DM
1765
1766 PVE::QemuServer::check_lock($conf);
1767
3ea94c60 1768 if (PVE::QemuServer::check_running($vmid)) {
afdb31d5 1769 die "cant migrate running VM without --online\n"
3ea94c60
DM
1770 if !$param->{online};
1771 }
1772
47152e2e 1773 my $storecfg = PVE::Storage::config();
22d646a7 1774 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target);
47152e2e 1775
3be30d63 1776 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
3ea94c60 1777
88fc87b4
DM
1778 my $hacmd = sub {
1779 my $upid = shift;
3ea94c60 1780
88fc87b4
DM
1781 my $service = "pvevm:$vmid";
1782
1783 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1784
1785 print "Executing HA migrate for VM $vmid to node $target\n";
1786
1787 PVE::Tools::run_command($cmd);
1788
1789 return;
1790 };
1791
1792 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1793
1794 } else {
1795
1796 my $realcmd = sub {
1797 my $upid = shift;
1798
1799 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1800 };
1801
1802 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1803 }
3ea94c60 1804
3ea94c60 1805 }});
1e3baf05 1806
91c94f0a 1807__PACKAGE__->register_method({
afdb31d5
DM
1808 name => 'monitor',
1809 path => '{vmid}/monitor',
91c94f0a
DM
1810 method => 'POST',
1811 protected => 1,
1812 proxyto => 'node',
1813 description => "Execute Qemu monitor commands.",
a0d1b1a2
DM
1814 permissions => {
1815 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1816 },
91c94f0a
DM
1817 parameters => {
1818 additionalProperties => 0,
1819 properties => {
1820 node => get_standard_option('pve-node'),
1821 vmid => get_standard_option('pve-vmid'),
1822 command => {
1823 type => 'string',
1824 description => "The monitor command.",
1825 }
1826 },
1827 },
1828 returns => { type => 'string'},
1829 code => sub {
1830 my ($param) = @_;
1831
1832 my $vmid = $param->{vmid};
1833
1834 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1835
1836 my $res = '';
1837 eval {
7b7c6d1b 1838 $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command});
91c94f0a
DM
1839 };
1840 $res = "ERROR: $@" if $@;
1841
1842 return $res;
1843 }});
1844
0d02881c
AD
1845__PACKAGE__->register_method({
1846 name => 'resize_vm',
614e3941 1847 path => '{vmid}/resize',
0d02881c
AD
1848 method => 'PUT',
1849 protected => 1,
1850 proxyto => 'node',
2f48a4f5 1851 description => "Extend volume size.",
0d02881c 1852 permissions => {
3b2773f6 1853 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
0d02881c
AD
1854 },
1855 parameters => {
1856 additionalProperties => 0,
2f48a4f5
DM
1857 properties => {
1858 node => get_standard_option('pve-node'),
1859 vmid => get_standard_option('pve-vmid'),
1860 skiplock => get_standard_option('skiplock'),
1861 disk => {
1862 type => 'string',
1863 description => "The disk you want to resize.",
1864 enum => [PVE::QemuServer::disknames()],
1865 },
1866 size => {
1867 type => 'string',
f91b2e45 1868 pattern => '\+?\d+(\.\d+)?[KMGT]?',
2f48a4f5
DM
1869 description => "The new size. With the '+' sign the value is added to the actual size of the volume and without it, the value is taken as an absolute one. Shrinking disk size is not supported.",
1870 },
1871 digest => {
1872 type => 'string',
1873 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1874 maxLength => 40,
1875 optional => 1,
1876 },
1877 },
0d02881c
AD
1878 },
1879 returns => { type => 'null'},
1880 code => sub {
1881 my ($param) = @_;
1882
1883 my $rpcenv = PVE::RPCEnvironment::get();
1884
1885 my $authuser = $rpcenv->get_user();
1886
1887 my $node = extract_param($param, 'node');
1888
1889 my $vmid = extract_param($param, 'vmid');
1890
1891 my $digest = extract_param($param, 'digest');
1892
2f48a4f5
DM
1893 my $disk = extract_param($param, 'disk');
1894
1895 my $sizestr = extract_param($param, 'size');
0d02881c 1896
f91b2e45 1897 my $skiplock = extract_param($param, 'skiplock');
0d02881c
AD
1898 raise_param_exc({ skiplock => "Only root may use this option." })
1899 if $skiplock && $authuser ne 'root@pam';
1900
0d02881c
AD
1901 my $storecfg = PVE::Storage::config();
1902
0d02881c
AD
1903 my $updatefn = sub {
1904
1905 my $conf = PVE::QemuServer::load_config($vmid);
1906
1907 die "checksum missmatch (file change by other user?)\n"
1908 if $digest && $digest ne $conf->{digest};
1909 PVE::QemuServer::check_lock($conf) if !$skiplock;
1910
f91b2e45
DM
1911 die "disk '$disk' does not exist\n" if !$conf->{$disk};
1912
1913 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
1914
1915 my $volid = $drive->{file};
1916
1917 die "disk '$disk' has no associated volume\n" if !$volid;
1918
1919 die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
1920
1921 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
1922
1923 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
1924
1925 my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
1926
1927 die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
1928 my ($ext, $newsize, $unit) = ($1, $2, $4);
1929 if ($unit) {
1930 if ($unit eq 'K') {
1931 $newsize = $newsize * 1024;
1932 } elsif ($unit eq 'M') {
1933 $newsize = $newsize * 1024 * 1024;
1934 } elsif ($unit eq 'G') {
1935 $newsize = $newsize * 1024 * 1024 * 1024;
1936 } elsif ($unit eq 'T') {
1937 $newsize = $newsize * 1024 * 1024 * 1024 * 1024;
1938 }
1939 }
1940 $newsize += $size if $ext;
1941 $newsize = int($newsize);
1942
1943 die "unable to skrink disk size\n" if $newsize < $size;
1944
1945 return if $size == $newsize;
1946
2f48a4f5 1947 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr");
0d02881c 1948
f91b2e45
DM
1949 PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize);
1950
1951 $drive->{size} = $newsize;
1952 $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive);
1953
1954 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
1955 };
0d02881c
AD
1956
1957 PVE::QemuServer::lock_config($vmid, $updatefn);
1958 return undef;
1959 }});
1960
9dbd1ee4 1961__PACKAGE__->register_method({
7e7d7b61 1962 name => 'snapshot_list',
9dbd1ee4 1963 path => '{vmid}/snapshot',
7e7d7b61
DM
1964 method => 'GET',
1965 description => "List all snapshots.",
1966 permissions => {
1967 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1968 },
1969 proxyto => 'node',
1970 protected => 1, # qemu pid files are only readable by root
1971 parameters => {
1972 additionalProperties => 0,
1973 properties => {
1974 vmid => get_standard_option('pve-vmid'),
1975 node => get_standard_option('pve-node'),
1976 },
1977 },
1978 returns => {
1979 type => 'array',
1980 items => {
1981 type => "object",
1982 properties => {},
1983 },
1984 links => [ { rel => 'child', href => "{name}" } ],
1985 },
1986 code => sub {
1987 my ($param) = @_;
1988
6aa4651b
DM
1989 my $vmid = $param->{vmid};
1990
1991 my $conf = PVE::QemuServer::load_config($vmid);
7e7d7b61
DM
1992 my $snaphash = $conf->{snapshots} || {};
1993
1994 my $res = [];
1995
1996 foreach my $name (keys %$snaphash) {
0ea6bc69 1997 my $d = $snaphash->{$name};
982c7f12
DM
1998 my $item = {
1999 name => $name,
2000 snaptime => $d->{snaptime} || 0,
6aa4651b 2001 vmstate => $d->{vmstate} ? 1 : 0,
982c7f12
DM
2002 description => $d->{description} || '',
2003 };
0ea6bc69 2004 $item->{parent} = $d->{parent} if $d->{parent};
3ee28e38 2005 $item->{snapstate} = $d->{snapstate} if $d->{snapstate};
0ea6bc69
DM
2006 push @$res, $item;
2007 }
2008
6aa4651b
DM
2009 my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0;
2010 my $current = { name => 'current', digest => $conf->{digest}, running => $running };
d1914468
DM
2011 $current->{parent} = $conf->{parent} if $conf->{parent};
2012
2013 push @$res, $current;
7e7d7b61
DM
2014
2015 return $res;
2016 }});
2017
2018__PACKAGE__->register_method({
2019 name => 'snapshot',
2020 path => '{vmid}/snapshot',
2021 method => 'POST',
9dbd1ee4
AD
2022 protected => 1,
2023 proxyto => 'node',
2024 description => "Snapshot a VM.",
2025 permissions => {
f1baf1df 2026 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
9dbd1ee4
AD
2027 },
2028 parameters => {
2029 additionalProperties => 0,
2030 properties => {
2031 node => get_standard_option('pve-node'),
2032 vmid => get_standard_option('pve-vmid'),
8abd398b 2033 snapname => get_standard_option('pve-snapshot-name'),
9dbd1ee4
AD
2034 vmstate => {
2035 optional => 1,
2036 type => 'boolean',
2037 description => "Save the vmstate",
2038 },
2039 freezefs => {
2040 optional => 1,
2041 type => 'boolean',
2042 description => "Freeze the filesystem",
2043 },
782f4f75
DM
2044 description => {
2045 optional => 1,
2046 type => 'string',
2047 description => "A textual description or comment.",
2048 },
9dbd1ee4
AD
2049 },
2050 },
7e7d7b61
DM
2051 returns => {
2052 type => 'string',
2053 description => "the task ID.",
2054 },
9dbd1ee4
AD
2055 code => sub {
2056 my ($param) = @_;
2057
2058 my $rpcenv = PVE::RPCEnvironment::get();
2059
2060 my $authuser = $rpcenv->get_user();
2061
2062 my $node = extract_param($param, 'node');
2063
2064 my $vmid = extract_param($param, 'vmid');
2065
9dbd1ee4
AD
2066 my $snapname = extract_param($param, 'snapname');
2067
d1914468
DM
2068 die "unable to use snapshot name 'current' (reserved name)\n"
2069 if $snapname eq 'current';
2070
7e7d7b61 2071 my $realcmd = sub {
22c377f0 2072 PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
782f4f75
DM
2073 PVE::QemuServer::snapshot_create($vmid, $snapname, $param->{vmstate},
2074 $param->{freezefs}, $param->{description});
7e7d7b61
DM
2075 };
2076
2077 return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd);
2078 }});
2079
154ccdcd
DM
2080__PACKAGE__->register_method({
2081 name => 'snapshot_cmd_idx',
2082 path => '{vmid}/snapshot/{snapname}',
2083 description => '',
2084 method => 'GET',
2085 permissions => {
2086 user => 'all',
2087 },
2088 parameters => {
2089 additionalProperties => 0,
2090 properties => {
2091 vmid => get_standard_option('pve-vmid'),
2092 node => get_standard_option('pve-node'),
8abd398b 2093 snapname => get_standard_option('pve-snapshot-name'),
154ccdcd
DM
2094 },
2095 },
2096 returns => {
2097 type => 'array',
2098 items => {
2099 type => "object",
2100 properties => {},
2101 },
2102 links => [ { rel => 'child', href => "{cmd}" } ],
2103 },
2104 code => sub {
2105 my ($param) = @_;
2106
2107 my $res = [];
2108
2109 push @$res, { cmd => 'rollback' };
d788cea6 2110 push @$res, { cmd => 'config' };
154ccdcd
DM
2111
2112 return $res;
2113 }});
2114
d788cea6
DM
2115__PACKAGE__->register_method({
2116 name => 'update_snapshot_config',
2117 path => '{vmid}/snapshot/{snapname}/config',
2118 method => 'PUT',
2119 protected => 1,
2120 proxyto => 'node',
2121 description => "Update snapshot metadata.",
2122 permissions => {
2123 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2124 },
2125 parameters => {
2126 additionalProperties => 0,
2127 properties => {
2128 node => get_standard_option('pve-node'),
2129 vmid => get_standard_option('pve-vmid'),
2130 snapname => get_standard_option('pve-snapshot-name'),
2131 description => {
2132 optional => 1,
2133 type => 'string',
2134 description => "A textual description or comment.",
2135 },
2136 },
2137 },
2138 returns => { type => 'null' },
2139 code => sub {
2140 my ($param) = @_;
2141
2142 my $rpcenv = PVE::RPCEnvironment::get();
2143
2144 my $authuser = $rpcenv->get_user();
2145
2146 my $vmid = extract_param($param, 'vmid');
2147
2148 my $snapname = extract_param($param, 'snapname');
2149
2150 return undef if !defined($param->{description});
2151
2152 my $updatefn = sub {
2153
2154 my $conf = PVE::QemuServer::load_config($vmid);
2155
2156 PVE::QemuServer::check_lock($conf);
2157
2158 my $snap = $conf->{snapshots}->{$snapname};
2159
2160 die "snapshot '$snapname' does not exist\n" if !defined($snap);
2161
2162 $snap->{description} = $param->{description} if defined($param->{description});
2163
2164 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
2165 };
2166
2167 PVE::QemuServer::lock_config($vmid, $updatefn);
2168
2169 return undef;
2170 }});
2171
2172__PACKAGE__->register_method({
2173 name => 'get_snapshot_config',
2174 path => '{vmid}/snapshot/{snapname}/config',
2175 method => 'GET',
2176 proxyto => 'node',
2177 description => "Get snapshot configuration",
2178 permissions => {
2179 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
2180 },
2181 parameters => {
2182 additionalProperties => 0,
2183 properties => {
2184 node => get_standard_option('pve-node'),
2185 vmid => get_standard_option('pve-vmid'),
2186 snapname => get_standard_option('pve-snapshot-name'),
2187 },
2188 },
2189 returns => { type => "object" },
2190 code => sub {
2191 my ($param) = @_;
2192
2193 my $rpcenv = PVE::RPCEnvironment::get();
2194
2195 my $authuser = $rpcenv->get_user();
2196
2197 my $vmid = extract_param($param, 'vmid');
2198
2199 my $snapname = extract_param($param, 'snapname');
2200
2201 my $conf = PVE::QemuServer::load_config($vmid);
2202
2203 my $snap = $conf->{snapshots}->{$snapname};
2204
2205 die "snapshot '$snapname' does not exist\n" if !defined($snap);
2206
2207 return $snap;
2208 }});
2209
7e7d7b61
DM
2210__PACKAGE__->register_method({
2211 name => 'rollback',
154ccdcd 2212 path => '{vmid}/snapshot/{snapname}/rollback',
7e7d7b61
DM
2213 method => 'POST',
2214 protected => 1,
2215 proxyto => 'node',
2216 description => "Rollback VM state to specified snapshot.",
2217 permissions => {
f1baf1df 2218 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
7e7d7b61
DM
2219 },
2220 parameters => {
2221 additionalProperties => 0,
2222 properties => {
2223 node => get_standard_option('pve-node'),
2224 vmid => get_standard_option('pve-vmid'),
8abd398b 2225 snapname => get_standard_option('pve-snapshot-name'),
7e7d7b61
DM
2226 },
2227 },
2228 returns => {
2229 type => 'string',
2230 description => "the task ID.",
2231 },
2232 code => sub {
2233 my ($param) = @_;
2234
2235 my $rpcenv = PVE::RPCEnvironment::get();
2236
2237 my $authuser = $rpcenv->get_user();
2238
2239 my $node = extract_param($param, 'node');
2240
2241 my $vmid = extract_param($param, 'vmid');
2242
2243 my $snapname = extract_param($param, 'snapname');
2244
7e7d7b61 2245 my $realcmd = sub {
22c377f0
DM
2246 PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname");
2247 PVE::QemuServer::snapshot_rollback($vmid, $snapname);
7e7d7b61
DM
2248 };
2249
2250 return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd);
2251 }});
2252
2253__PACKAGE__->register_method({
2254 name => 'delsnapshot',
2255 path => '{vmid}/snapshot/{snapname}',
2256 method => 'DELETE',
2257 protected => 1,
2258 proxyto => 'node',
2259 description => "Delete a VM snapshot.",
2260 permissions => {
f1baf1df 2261 check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]],
7e7d7b61
DM
2262 },
2263 parameters => {
2264 additionalProperties => 0,
2265 properties => {
2266 node => get_standard_option('pve-node'),
2267 vmid => get_standard_option('pve-vmid'),
8abd398b 2268 snapname => get_standard_option('pve-snapshot-name'),
3ee28e38
DM
2269 force => {
2270 optional => 1,
2271 type => 'boolean',
2272 description => "For removal from config file, even if removing disk snapshots fails.",
2273 },
7e7d7b61
DM
2274 },
2275 },
2276 returns => {
2277 type => 'string',
2278 description => "the task ID.",
2279 },
2280 code => sub {
2281 my ($param) = @_;
2282
2283 my $rpcenv = PVE::RPCEnvironment::get();
2284
2285 my $authuser = $rpcenv->get_user();
2286
2287 my $node = extract_param($param, 'node');
2288
2289 my $vmid = extract_param($param, 'vmid');
2290
2291 my $snapname = extract_param($param, 'snapname');
2292
7e7d7b61 2293 my $realcmd = sub {
22c377f0 2294 PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
3ee28e38 2295 PVE::QemuServer::snapshot_delete($vmid, $snapname, $param->{force});
7e7d7b61 2296 };
9dbd1ee4 2297
7b2257a8 2298 return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd);
9dbd1ee4
AD
2299 }});
2300
1e3baf05 23011;