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