]> git.proxmox.com Git - qemu-server.git/blame - PVE/API2/Qemu.pm
update config on snapshot commands
[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
22c377f0
DM
582 delete $conf->{snapshots};
583
1e3baf05
DM
584 return $conf;
585 }});
586
ae57f6b3
DM
587my $vm_is_volid_owner = sub {
588 my ($storecfg, $vmid, $volid) =@_;
589
590 if ($volid !~ m|^/|) {
591 my ($path, $owner);
592 eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
593 if ($owner && ($owner == $vmid)) {
594 return 1;
595 }
596 }
597
598 return undef;
599};
600
601my $test_deallocate_drive = sub {
602 my ($storecfg, $vmid, $key, $drive, $force) = @_;
603
604 if (!PVE::QemuServer::drive_is_cdrom($drive)) {
605 my $volid = $drive->{file};
606 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
607 if ($force || $key =~ m/^unused/) {
608 my $sid = PVE::Storage::parse_volume_id($volid);
609 return $sid;
610 }
611 }
612 }
613
614 return undef;
615};
616
5d7a6767 617my $delete_drive = sub {
1858638f 618 my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_;
5d7a6767
DM
619
620 if (!PVE::QemuServer::drive_is_cdrom($drive)) {
621 my $volid = $drive->{file};
ae57f6b3
DM
622 if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) {
623 if ($force || $key =~ m/^unused/) {
624 eval { PVE::Storage::vdisk_free($storecfg, $volid); };
7e4e69a6 625 die $@ if $@;
ae57f6b3
DM
626 } else {
627 PVE::QemuServer::add_unused_volume($conf, $volid, $vmid);
5d7a6767 628 }
ae57f6b3
DM
629 }
630 }
49f9db93
DM
631
632 delete $conf->{$key};
ae57f6b3
DM
633};
634
635my $vmconfig_delete_option = sub {
636 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_;
637
638 return if !defined($conf->{$opt});
639
640 my $isDisk = PVE::QemuServer::valid_drivename($opt)|| ($opt =~ m/^unused/);
641
642 if ($isDisk) {
643 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
644
645 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
646 if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) {
fcbb753e 647 $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']);
5d7a6767 648 }
ae57f6b3
DM
649 }
650
651 die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
652
653 if ($isDisk) {
654 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
655 &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force);
5d7a6767 656 } else {
ae57f6b3
DM
657 delete $conf->{$opt};
658 }
659
660 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
661};
662
9bf371a6 663my $safe_num_ne = sub {
93ae06e1
DM
664 my ($a, $b) = @_;
665
666 return 0 if !defined($a) && !defined($b);
667 return 1 if !defined($a);
668 return 1 if !defined($b);
669
670 return $a != $b;
671};
672
ae57f6b3
DM
673my $vmconfig_update_disk = sub {
674 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_;
5d7a6767 675
ae57f6b3
DM
676 my $drive = PVE::QemuServer::parse_drive($opt, $value);
677
678 if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom
679 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
680 } else {
681 $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
682 }
683
684 if ($conf->{$opt}) {
685
686 if (my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt})) {
687
688 my $media = $drive->{media} || 'disk';
689 my $oldmedia = $old_drive->{media} || 'disk';
690 die "unable to change media type\n" if $media ne $oldmedia;
691
692 if (!PVE::QemuServer::drive_is_cdrom($old_drive) &&
693 ($drive->{file} ne $old_drive->{file})) { # delete old disks
694
695 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
696 $conf = PVE::QemuServer::load_config($vmid); # update/reload
697 }
0f56d571 698
9bf371a6
DM
699 if(&$safe_num_ne($drive->{mbps}, $old_drive->{mbps}) ||
700 &$safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) ||
701 &$safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) ||
702 &$safe_num_ne($drive->{iops}, $old_drive->{iops}) ||
703 &$safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) ||
704 &$safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr})) {
705 PVE::QemuServer::qemu_block_set_io_throttle($vmid,"drive-$opt", $drive->{mbps}*1024*1024,
706 $drive->{mbps_rd}*1024*1024, $drive->{mbps_wr}*1024*1024,
707 $drive->{iops}, $drive->{iops_rd}, $drive->{iops_wr})
708 if !PVE::QemuServer::drive_is_cdrom($drive);
0f56d571 709 }
ae57f6b3
DM
710 }
711 }
712
713 &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value});
714 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
715
716 $conf = PVE::QemuServer::load_config($vmid); # update/reload
717 $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
718
719 if (PVE::QemuServer::drive_is_cdrom($drive)) { # cdrom
720
721 if (PVE::QemuServer::check_running($vmid)) {
722 if ($drive->{file} eq 'none') {
ce156282 723 PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
ae57f6b3
DM
724 } else {
725 my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
ce156282
AD
726 PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt"); #force eject if locked
727 PVE::QemuServer::vm_mon_cmd($vmid, "change",device => "drive-$opt",target => "$path") if $path;
ae57f6b3
DM
728 }
729 }
730
731 } else { # hotplug new disks
732
733 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
5d7a6767 734 }
5d7a6767
DM
735};
736
ae57f6b3
DM
737my $vmconfig_update_net = sub {
738 my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_;
739
740 if ($conf->{$opt}) {
741 #if online update, then unplug first
742 die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
743 }
744
745 $conf->{$opt} = $value;
746 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
747 $conf = PVE::QemuServer::load_config($vmid); # update/reload
748
749 my $net = PVE::QemuServer::parse_net($conf->{$opt});
750
751 die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net);
752};
753
a0d1b1a2
DM
754my $vm_config_perm_list = [
755 'VM.Config.Disk',
756 'VM.Config.CDROM',
757 'VM.Config.CPU',
758 'VM.Config.Memory',
759 'VM.Config.Network',
760 'VM.Config.HWType',
761 'VM.Config.Options',
762 ];
763
1e3baf05 764__PACKAGE__->register_method({
afdb31d5
DM
765 name => 'update_vm',
766 path => '{vmid}/config',
1e3baf05
DM
767 method => 'PUT',
768 protected => 1,
769 proxyto => 'node',
770 description => "Set virtual machine options.",
a0d1b1a2
DM
771 permissions => {
772 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
773 },
1e3baf05
DM
774 parameters => {
775 additionalProperties => 0,
776 properties => PVE::QemuServer::json_config_properties(
777 {
778 node => get_standard_option('pve-node'),
779 vmid => get_standard_option('pve-vmid'),
3ea94c60 780 skiplock => get_standard_option('skiplock'),
1e3baf05
DM
781 delete => {
782 type => 'string', format => 'pve-configid-list',
783 description => "A list of settings you want to delete.",
784 optional => 1,
785 },
786 force => {
787 type => 'boolean',
788 description => $opt_force_description,
789 optional => 1,
790 requires => 'delete',
791 },
554ac7e7
DM
792 digest => {
793 type => 'string',
794 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
795 maxLength => 40,
afdb31d5 796 optional => 1,
554ac7e7 797 }
1e3baf05
DM
798 }),
799 },
800 returns => { type => 'null'},
801 code => sub {
802 my ($param) = @_;
803
804 my $rpcenv = PVE::RPCEnvironment::get();
805
a0d1b1a2 806 my $authuser = $rpcenv->get_user();
1e3baf05
DM
807
808 my $node = extract_param($param, 'node');
809
1e3baf05
DM
810 my $vmid = extract_param($param, 'vmid');
811
5fdbe4f0
DM
812 my $digest = extract_param($param, 'digest');
813
814 my @paramarr = (); # used for log message
815 foreach my $key (keys %$param) {
816 push @paramarr, "-$key", $param->{$key};
817 }
818
1e3baf05 819 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 820 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 821 if $skiplock && $authuser ne 'root@pam';
1e3baf05 822
0532bc63
DM
823 my $delete_str = extract_param($param, 'delete');
824
1e3baf05
DM
825 my $force = extract_param($param, 'force');
826
0532bc63
DM
827 die "no options specified\n" if !$delete_str && !scalar(keys %$param);
828
1e68cb19
DM
829 my $storecfg = PVE::Storage::config();
830
831 &$resolve_cdrom_alias($param);
832
833 # now try to verify all parameters
834
0532bc63
DM
835 my @delete = ();
836 foreach my $opt (PVE::Tools::split_list($delete_str)) {
837 $opt = 'ide2' if $opt eq 'cdrom';
838 raise_param_exc({ delete => "you can't use '-$opt' and " .
839 "-delete $opt' at the same time" })
840 if defined($param->{$opt});
841
842 if (!PVE::QemuServer::option_exists($opt)) {
843 raise_param_exc({ delete => "unknown option '$opt'" });
844 }
ae57f6b3 845
0532bc63
DM
846 push @delete, $opt;
847 }
1e3baf05 848
1e68cb19
DM
849 foreach my $opt (keys %$param) {
850 if (PVE::QemuServer::valid_drivename($opt)) {
851 # cleanup drive path
852 my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
853 PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
854 $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
1e68cb19
DM
855 } elsif ($opt =~ m/^net(\d+)$/) {
856 # add macaddr
857 my $net = PVE::QemuServer::parse_net($param->{$opt});
858 $param->{$opt} = PVE::QemuServer::print_net($net);
1e68cb19
DM
859 }
860 }
1e3baf05 861
ae57f6b3
DM
862 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
863
864 &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
865
fcbb753e 866 &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
1e3baf05 867
5d39a182 868 my $updatefn = sub {
1e3baf05 869
5d39a182 870 my $conf = PVE::QemuServer::load_config($vmid);
1e3baf05 871
5d39a182
DM
872 die "checksum missmatch (file change by other user?)\n"
873 if $digest && $digest ne $conf->{digest};
1e3baf05 874
5d39a182 875 PVE::QemuServer::check_lock($conf) if !$skiplock;
1e3baf05 876
a0d1b1a2 877 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr));
c2a64aa7 878
5d7a6767 879 foreach my $opt (@delete) { # delete
1e68cb19 880 $conf = PVE::QemuServer::load_config($vmid); # update/reload
ae57f6b3 881 &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force);
5d39a182 882 }
1e3baf05 883
5d7a6767 884 foreach my $opt (keys %$param) { # add/change
1e3baf05 885
1e68cb19 886 $conf = PVE::QemuServer::load_config($vmid); # update/reload
1e3baf05 887
5d7a6767
DM
888 next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
889
ae57f6b3 890 if (PVE::QemuServer::valid_drivename($opt)) {
5d7a6767 891
ae57f6b3
DM
892 &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid,
893 $opt, $param->{$opt}, $force);
1e68cb19 894
ae57f6b3 895 } elsif ($opt =~ m/^net(\d+)$/) { #nics
1858638f 896
ae57f6b3
DM
897 &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid,
898 $opt, $param->{$opt});
1e68cb19
DM
899
900 } else {
901
1858638f
DM
902 $conf->{$opt} = $param->{$opt};
903 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
3a1e36bb 904 }
5d39a182
DM
905 }
906 };
907
908 PVE::QemuServer::lock_config($vmid, $updatefn);
fcdb0117 909
1e3baf05
DM
910 return undef;
911 }});
912
913
914__PACKAGE__->register_method({
afdb31d5
DM
915 name => 'destroy_vm',
916 path => '{vmid}',
1e3baf05
DM
917 method => 'DELETE',
918 protected => 1,
919 proxyto => 'node',
920 description => "Destroy the vm (also delete all used/owned volumes).",
a0d1b1a2
DM
921 permissions => {
922 check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']],
923 },
1e3baf05
DM
924 parameters => {
925 additionalProperties => 0,
926 properties => {
927 node => get_standard_option('pve-node'),
928 vmid => get_standard_option('pve-vmid'),
3ea94c60 929 skiplock => get_standard_option('skiplock'),
1e3baf05
DM
930 },
931 },
afdb31d5 932 returns => {
5fdbe4f0
DM
933 type => 'string',
934 },
1e3baf05
DM
935 code => sub {
936 my ($param) = @_;
937
938 my $rpcenv = PVE::RPCEnvironment::get();
939
a0d1b1a2 940 my $authuser = $rpcenv->get_user();
1e3baf05
DM
941
942 my $vmid = $param->{vmid};
943
944 my $skiplock = $param->{skiplock};
afdb31d5 945 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 946 if $skiplock && $authuser ne 'root@pam';
1e3baf05 947
5fdbe4f0
DM
948 # test if VM exists
949 my $conf = PVE::QemuServer::load_config($vmid);
950
afdb31d5 951 my $storecfg = PVE::Storage::config();
1e3baf05 952
502d18a2
DM
953 my $delVMfromPoolFn = sub {
954 my $usercfg = cfs_read_file("user.cfg");
5d0094ea
DM
955 if (my $pool = $usercfg->{vms}->{$vmid}) {
956 if (my $data = $usercfg->{pools}->{$pool}) {
957 delete $data->{vms}->{$vmid};
958 delete $usercfg->{vms}->{$vmid};
959 cfs_write_file("user.cfg", $usercfg);
960 }
502d18a2
DM
961 }
962 };
963
5fdbe4f0 964 my $realcmd = sub {
ff1a2432
DM
965 my $upid = shift;
966
967 syslog('info', "destroy VM $vmid: $upid\n");
968
5fdbe4f0 969 PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
502d18a2
DM
970
971 PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed");
5fdbe4f0 972 };
1e3baf05 973
a0d1b1a2 974 return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
1e3baf05
DM
975 }});
976
977__PACKAGE__->register_method({
afdb31d5
DM
978 name => 'unlink',
979 path => '{vmid}/unlink',
1e3baf05
DM
980 method => 'PUT',
981 protected => 1,
982 proxyto => 'node',
983 description => "Unlink/delete disk images.",
a0d1b1a2
DM
984 permissions => {
985 check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']],
986 },
1e3baf05
DM
987 parameters => {
988 additionalProperties => 0,
989 properties => {
990 node => get_standard_option('pve-node'),
991 vmid => get_standard_option('pve-vmid'),
992 idlist => {
993 type => 'string', format => 'pve-configid-list',
994 description => "A list of disk IDs you want to delete.",
995 },
996 force => {
997 type => 'boolean',
998 description => $opt_force_description,
999 optional => 1,
1000 },
1001 },
1002 },
1003 returns => { type => 'null'},
1004 code => sub {
1005 my ($param) = @_;
1006
1007 $param->{delete} = extract_param($param, 'idlist');
1008
1009 __PACKAGE__->update_vm($param);
1010
1011 return undef;
1012 }});
1013
1014my $sslcert;
1015
1016__PACKAGE__->register_method({
afdb31d5
DM
1017 name => 'vncproxy',
1018 path => '{vmid}/vncproxy',
1e3baf05
DM
1019 method => 'POST',
1020 protected => 1,
1021 permissions => {
378b359e 1022 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1e3baf05
DM
1023 },
1024 description => "Creates a TCP VNC proxy connections.",
1025 parameters => {
1026 additionalProperties => 0,
1027 properties => {
1028 node => get_standard_option('pve-node'),
1029 vmid => get_standard_option('pve-vmid'),
1030 },
1031 },
afdb31d5 1032 returns => {
1e3baf05
DM
1033 additionalProperties => 0,
1034 properties => {
1035 user => { type => 'string' },
1036 ticket => { type => 'string' },
1037 cert => { type => 'string' },
1038 port => { type => 'integer' },
1039 upid => { type => 'string' },
1040 },
1041 },
1042 code => sub {
1043 my ($param) = @_;
1044
1045 my $rpcenv = PVE::RPCEnvironment::get();
1046
a0d1b1a2 1047 my $authuser = $rpcenv->get_user();
1e3baf05
DM
1048
1049 my $vmid = $param->{vmid};
1050 my $node = $param->{node};
1051
b6f39da2
DM
1052 my $authpath = "/vms/$vmid";
1053
a0d1b1a2 1054 my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
b6f39da2 1055
1e3baf05
DM
1056 $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
1057 if !$sslcert;
1058
1059 my $port = PVE::Tools::next_vnc_port();
1060
1061 my $remip;
afdb31d5 1062
4f1be36c 1063 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
1e3baf05
DM
1064 $remip = PVE::Cluster::remote_node_ip($node);
1065 }
1066
1067 # NOTE: kvm VNC traffic is already TLS encrypted,
1068 # so we select the fastest chipher here (or 'none'?)
1069 my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
1070 '-c', 'blowfish-cbc', $remip] : [];
1071
afdb31d5 1072 my $timeout = 10;
1e3baf05
DM
1073
1074 my $realcmd = sub {
1075 my $upid = shift;
1076
1077 syslog('info', "starting vnc proxy $upid\n");
1078
1079 my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
1080
1081 my $qmstr = join(' ', @$qmcmd);
1082
1083 # also redirect stderr (else we get RFB protocol errors)
be62c45c 1084 my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"];
1e3baf05 1085
be62c45c 1086 PVE::Tools::run_command($cmd);
1e3baf05
DM
1087
1088 return;
1089 };
1090
a0d1b1a2 1091 my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd);
1e3baf05
DM
1092
1093 return {
a0d1b1a2 1094 user => $authuser,
1e3baf05 1095 ticket => $ticket,
afdb31d5
DM
1096 port => $port,
1097 upid => $upid,
1098 cert => $sslcert,
1e3baf05
DM
1099 };
1100 }});
1101
5fdbe4f0
DM
1102__PACKAGE__->register_method({
1103 name => 'vmcmdidx',
afdb31d5 1104 path => '{vmid}/status',
5fdbe4f0
DM
1105 method => 'GET',
1106 proxyto => 'node',
1107 description => "Directory index",
a0d1b1a2
DM
1108 permissions => {
1109 user => 'all',
1110 },
5fdbe4f0
DM
1111 parameters => {
1112 additionalProperties => 0,
1113 properties => {
1114 node => get_standard_option('pve-node'),
1115 vmid => get_standard_option('pve-vmid'),
1116 },
1117 },
1118 returns => {
1119 type => 'array',
1120 items => {
1121 type => "object",
1122 properties => {
1123 subdir => { type => 'string' },
1124 },
1125 },
1126 links => [ { rel => 'child', href => "{subdir}" } ],
1127 },
1128 code => sub {
1129 my ($param) = @_;
1130
1131 # test if VM exists
1132 my $conf = PVE::QemuServer::load_config($param->{vmid});
1133
1134 my $res = [
1135 { subdir => 'current' },
1136 { subdir => 'start' },
1137 { subdir => 'stop' },
1138 ];
afdb31d5 1139
5fdbe4f0
DM
1140 return $res;
1141 }});
1142
88fc87b4
DM
1143my $vm_is_ha_managed = sub {
1144 my ($vmid) = @_;
1145
1146 my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
1147 if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $vmid, 1)) {
1148 return 1;
1149 }
1150 return 0;
1151};
1152
1e3baf05 1153__PACKAGE__->register_method({
afdb31d5 1154 name => 'vm_status',
5fdbe4f0 1155 path => '{vmid}/status/current',
1e3baf05
DM
1156 method => 'GET',
1157 proxyto => 'node',
1158 protected => 1, # qemu pid files are only readable by root
1159 description => "Get virtual machine status.",
a0d1b1a2
DM
1160 permissions => {
1161 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
1162 },
1e3baf05
DM
1163 parameters => {
1164 additionalProperties => 0,
1165 properties => {
1166 node => get_standard_option('pve-node'),
1167 vmid => get_standard_option('pve-vmid'),
1168 },
1169 },
1170 returns => { type => 'object' },
1171 code => sub {
1172 my ($param) = @_;
1173
1174 # test if VM exists
1175 my $conf = PVE::QemuServer::load_config($param->{vmid});
1176
03a33f30 1177 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
8610701a 1178 my $status = $vmstatus->{$param->{vmid}};
1e3baf05 1179
88fc87b4 1180 $status->{ha} = &$vm_is_ha_managed($param->{vmid});
8610701a
DM
1181
1182 return $status;
1e3baf05
DM
1183 }});
1184
1185__PACKAGE__->register_method({
afdb31d5 1186 name => 'vm_start',
5fdbe4f0
DM
1187 path => '{vmid}/status/start',
1188 method => 'POST',
1e3baf05
DM
1189 protected => 1,
1190 proxyto => 'node',
5fdbe4f0 1191 description => "Start virtual machine.",
a0d1b1a2
DM
1192 permissions => {
1193 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1194 },
1e3baf05
DM
1195 parameters => {
1196 additionalProperties => 0,
1197 properties => {
1198 node => get_standard_option('pve-node'),
1199 vmid => get_standard_option('pve-vmid'),
3ea94c60
DM
1200 skiplock => get_standard_option('skiplock'),
1201 stateuri => get_standard_option('pve-qm-stateuri'),
7e8dcf2c
AD
1202 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
1203
1e3baf05
DM
1204 },
1205 },
afdb31d5 1206 returns => {
5fdbe4f0
DM
1207 type => 'string',
1208 },
1e3baf05
DM
1209 code => sub {
1210 my ($param) = @_;
1211
1212 my $rpcenv = PVE::RPCEnvironment::get();
1213
a0d1b1a2 1214 my $authuser = $rpcenv->get_user();
1e3baf05
DM
1215
1216 my $node = extract_param($param, 'node');
1217
1e3baf05
DM
1218 my $vmid = extract_param($param, 'vmid');
1219
3ea94c60 1220 my $stateuri = extract_param($param, 'stateuri');
afdb31d5 1221 raise_param_exc({ stateuri => "Only root may use this option." })
a0d1b1a2 1222 if $stateuri && $authuser ne 'root@pam';
3ea94c60 1223
1e3baf05 1224 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1225 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1226 if $skiplock && $authuser ne 'root@pam';
1e3baf05 1227
7e8dcf2c
AD
1228 my $migratedfrom = extract_param($param, 'migratedfrom');
1229 raise_param_exc({ migratedfrom => "Only root may use this option." })
1230 if $migratedfrom && $authuser ne 'root@pam';
1231
afdb31d5 1232 my $storecfg = PVE::Storage::config();
5fdbe4f0 1233
cce37749
DM
1234 if (&$vm_is_ha_managed($vmid) && !$stateuri &&
1235 $rpcenv->{type} ne 'ha') {
5fdbe4f0 1236
88fc87b4
DM
1237 my $hacmd = sub {
1238 my $upid = shift;
5fdbe4f0 1239
88fc87b4 1240 my $service = "pvevm:$vmid";
5fdbe4f0 1241
88fc87b4
DM
1242 my $cmd = ['clusvcadm', '-e', $service, '-m', $node];
1243
1244 print "Executing HA start for VM $vmid\n";
1245
1246 PVE::Tools::run_command($cmd);
1247
1248 return;
1249 };
1250
1251 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
1252
1253 } else {
1254
1255 my $realcmd = sub {
1256 my $upid = shift;
1257
1258 syslog('info', "start VM $vmid: $upid\n");
1259
7e8dcf2c 1260 PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom);
88fc87b4
DM
1261
1262 return;
1263 };
5fdbe4f0 1264
88fc87b4
DM
1265 return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd);
1266 }
5fdbe4f0
DM
1267 }});
1268
1269__PACKAGE__->register_method({
afdb31d5 1270 name => 'vm_stop',
5fdbe4f0
DM
1271 path => '{vmid}/status/stop',
1272 method => 'POST',
1273 protected => 1,
1274 proxyto => 'node',
1275 description => "Stop virtual machine.",
a0d1b1a2
DM
1276 permissions => {
1277 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1278 },
5fdbe4f0
DM
1279 parameters => {
1280 additionalProperties => 0,
1281 properties => {
1282 node => get_standard_option('pve-node'),
1283 vmid => get_standard_option('pve-vmid'),
1284 skiplock => get_standard_option('skiplock'),
af30308f 1285 migratedfrom => get_standard_option('pve-node',{ optional => 1 }),
c6bb9502
DM
1286 timeout => {
1287 description => "Wait maximal timeout seconds.",
1288 type => 'integer',
1289 minimum => 0,
1290 optional => 1,
254575e9
DM
1291 },
1292 keepActive => {
1293 description => "Do not decativate storage volumes.",
1294 type => 'boolean',
1295 optional => 1,
1296 default => 0,
c6bb9502 1297 }
5fdbe4f0
DM
1298 },
1299 },
afdb31d5 1300 returns => {
5fdbe4f0
DM
1301 type => 'string',
1302 },
1303 code => sub {
1304 my ($param) = @_;
1305
1306 my $rpcenv = PVE::RPCEnvironment::get();
1307
a0d1b1a2 1308 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1309
1310 my $node = extract_param($param, 'node');
1311
1312 my $vmid = extract_param($param, 'vmid');
1313
1314 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1315 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1316 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1317
254575e9 1318 my $keepActive = extract_param($param, 'keepActive');
afdb31d5 1319 raise_param_exc({ keepActive => "Only root may use this option." })
a0d1b1a2 1320 if $keepActive && $authuser ne 'root@pam';
254575e9 1321
af30308f
DM
1322 my $migratedfrom = extract_param($param, 'migratedfrom');
1323 raise_param_exc({ migratedfrom => "Only root may use this option." })
1324 if $migratedfrom && $authuser ne 'root@pam';
1325
1326
ff1a2432
DM
1327 my $storecfg = PVE::Storage::config();
1328
3be30d63 1329 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
5fdbe4f0 1330
88fc87b4
DM
1331 my $hacmd = sub {
1332 my $upid = shift;
5fdbe4f0 1333
88fc87b4 1334 my $service = "pvevm:$vmid";
c6bb9502 1335
88fc87b4
DM
1336 my $cmd = ['clusvcadm', '-d', $service];
1337
1338 print "Executing HA stop for VM $vmid\n";
1339
1340 PVE::Tools::run_command($cmd);
5fdbe4f0 1341
88fc87b4
DM
1342 return;
1343 };
1344
1345 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
1346
1347 } else {
1348 my $realcmd = sub {
1349 my $upid = shift;
1350
1351 syslog('info', "stop VM $vmid: $upid\n");
1352
1353 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
af30308f 1354 $param->{timeout}, 0, 1, $keepActive, $migratedfrom);
88fc87b4
DM
1355
1356 return;
1357 };
1358
1359 return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd);
1360 }
5fdbe4f0
DM
1361 }});
1362
1363__PACKAGE__->register_method({
afdb31d5 1364 name => 'vm_reset',
5fdbe4f0
DM
1365 path => '{vmid}/status/reset',
1366 method => 'POST',
1367 protected => 1,
1368 proxyto => 'node',
1369 description => "Reset virtual machine.",
a0d1b1a2
DM
1370 permissions => {
1371 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1372 },
5fdbe4f0
DM
1373 parameters => {
1374 additionalProperties => 0,
1375 properties => {
1376 node => get_standard_option('pve-node'),
1377 vmid => get_standard_option('pve-vmid'),
1378 skiplock => get_standard_option('skiplock'),
1379 },
1380 },
afdb31d5 1381 returns => {
5fdbe4f0
DM
1382 type => 'string',
1383 },
1384 code => sub {
1385 my ($param) = @_;
1386
1387 my $rpcenv = PVE::RPCEnvironment::get();
1388
a0d1b1a2 1389 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1390
1391 my $node = extract_param($param, 'node');
1392
1393 my $vmid = extract_param($param, 'vmid');
1394
1395 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1396 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1397 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1398
ff1a2432
DM
1399 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1400
5fdbe4f0
DM
1401 my $realcmd = sub {
1402 my $upid = shift;
1403
1e3baf05 1404 PVE::QemuServer::vm_reset($vmid, $skiplock);
5fdbe4f0
DM
1405
1406 return;
1407 };
1408
a0d1b1a2 1409 return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
1410 }});
1411
1412__PACKAGE__->register_method({
afdb31d5 1413 name => 'vm_shutdown',
5fdbe4f0
DM
1414 path => '{vmid}/status/shutdown',
1415 method => 'POST',
1416 protected => 1,
1417 proxyto => 'node',
1418 description => "Shutdown virtual machine.",
a0d1b1a2
DM
1419 permissions => {
1420 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1421 },
5fdbe4f0
DM
1422 parameters => {
1423 additionalProperties => 0,
1424 properties => {
1425 node => get_standard_option('pve-node'),
1426 vmid => get_standard_option('pve-vmid'),
1427 skiplock => get_standard_option('skiplock'),
c6bb9502
DM
1428 timeout => {
1429 description => "Wait maximal timeout seconds.",
1430 type => 'integer',
1431 minimum => 0,
1432 optional => 1,
9269013a
DM
1433 },
1434 forceStop => {
1435 description => "Make sure the VM stops.",
1436 type => 'boolean',
1437 optional => 1,
1438 default => 0,
254575e9
DM
1439 },
1440 keepActive => {
1441 description => "Do not decativate storage volumes.",
1442 type => 'boolean',
1443 optional => 1,
1444 default => 0,
c6bb9502 1445 }
5fdbe4f0
DM
1446 },
1447 },
afdb31d5 1448 returns => {
5fdbe4f0
DM
1449 type => 'string',
1450 },
1451 code => sub {
1452 my ($param) = @_;
1453
1454 my $rpcenv = PVE::RPCEnvironment::get();
1455
a0d1b1a2 1456 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1457
1458 my $node = extract_param($param, 'node');
1459
1460 my $vmid = extract_param($param, 'vmid');
1461
1462 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1463 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1464 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1465
254575e9 1466 my $keepActive = extract_param($param, 'keepActive');
afdb31d5 1467 raise_param_exc({ keepActive => "Only root may use this option." })
a0d1b1a2 1468 if $keepActive && $authuser ne 'root@pam';
254575e9 1469
02d07cf5
DM
1470 my $storecfg = PVE::Storage::config();
1471
5fdbe4f0
DM
1472 my $realcmd = sub {
1473 my $upid = shift;
1474
1475 syslog('info', "shutdown VM $vmid: $upid\n");
1476
afdb31d5 1477 PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
254575e9 1478 1, $param->{forceStop}, $keepActive);
c6bb9502 1479
5fdbe4f0
DM
1480 return;
1481 };
1482
a0d1b1a2 1483 return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
1484 }});
1485
1486__PACKAGE__->register_method({
afdb31d5 1487 name => 'vm_suspend',
5fdbe4f0
DM
1488 path => '{vmid}/status/suspend',
1489 method => 'POST',
1490 protected => 1,
1491 proxyto => 'node',
1492 description => "Suspend virtual machine.",
a0d1b1a2
DM
1493 permissions => {
1494 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1495 },
5fdbe4f0
DM
1496 parameters => {
1497 additionalProperties => 0,
1498 properties => {
1499 node => get_standard_option('pve-node'),
1500 vmid => get_standard_option('pve-vmid'),
1501 skiplock => get_standard_option('skiplock'),
1502 },
1503 },
afdb31d5 1504 returns => {
5fdbe4f0
DM
1505 type => 'string',
1506 },
1507 code => sub {
1508 my ($param) = @_;
1509
1510 my $rpcenv = PVE::RPCEnvironment::get();
1511
a0d1b1a2 1512 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1513
1514 my $node = extract_param($param, 'node');
1515
1516 my $vmid = extract_param($param, 'vmid');
1517
1518 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1519 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1520 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1521
ff1a2432
DM
1522 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
1523
5fdbe4f0
DM
1524 my $realcmd = sub {
1525 my $upid = shift;
1526
1527 syslog('info', "suspend VM $vmid: $upid\n");
1528
1e3baf05 1529 PVE::QemuServer::vm_suspend($vmid, $skiplock);
5fdbe4f0
DM
1530
1531 return;
1532 };
1533
a0d1b1a2 1534 return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
1535 }});
1536
1537__PACKAGE__->register_method({
afdb31d5 1538 name => 'vm_resume',
5fdbe4f0
DM
1539 path => '{vmid}/status/resume',
1540 method => 'POST',
1541 protected => 1,
1542 proxyto => 'node',
1543 description => "Resume virtual machine.",
a0d1b1a2
DM
1544 permissions => {
1545 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
1546 },
5fdbe4f0
DM
1547 parameters => {
1548 additionalProperties => 0,
1549 properties => {
1550 node => get_standard_option('pve-node'),
1551 vmid => get_standard_option('pve-vmid'),
1552 skiplock => get_standard_option('skiplock'),
1553 },
1554 },
afdb31d5 1555 returns => {
5fdbe4f0
DM
1556 type => 'string',
1557 },
1558 code => sub {
1559 my ($param) = @_;
1560
1561 my $rpcenv = PVE::RPCEnvironment::get();
1562
a0d1b1a2 1563 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1564
1565 my $node = extract_param($param, 'node');
1566
1567 my $vmid = extract_param($param, 'vmid');
1568
1569 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1570 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1571 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0 1572
b7eeab21 1573 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
ff1a2432 1574
5fdbe4f0
DM
1575 my $realcmd = sub {
1576 my $upid = shift;
1577
1578 syslog('info', "resume VM $vmid: $upid\n");
1579
1e3baf05 1580 PVE::QemuServer::vm_resume($vmid, $skiplock);
1e3baf05 1581
5fdbe4f0
DM
1582 return;
1583 };
1584
a0d1b1a2 1585 return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd);
5fdbe4f0
DM
1586 }});
1587
1588__PACKAGE__->register_method({
afdb31d5 1589 name => 'vm_sendkey',
5fdbe4f0
DM
1590 path => '{vmid}/sendkey',
1591 method => 'PUT',
1592 protected => 1,
1593 proxyto => 'node',
1594 description => "Send key event to virtual machine.",
a0d1b1a2
DM
1595 permissions => {
1596 check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
1597 },
5fdbe4f0
DM
1598 parameters => {
1599 additionalProperties => 0,
1600 properties => {
1601 node => get_standard_option('pve-node'),
1602 vmid => get_standard_option('pve-vmid'),
1603 skiplock => get_standard_option('skiplock'),
1604 key => {
1605 description => "The key (qemu monitor encoding).",
1606 type => 'string'
1607 }
1608 },
1609 },
1610 returns => { type => 'null'},
1611 code => sub {
1612 my ($param) = @_;
1613
1614 my $rpcenv = PVE::RPCEnvironment::get();
1615
a0d1b1a2 1616 my $authuser = $rpcenv->get_user();
5fdbe4f0
DM
1617
1618 my $node = extract_param($param, 'node');
1619
1620 my $vmid = extract_param($param, 'vmid');
1621
1622 my $skiplock = extract_param($param, 'skiplock');
afdb31d5 1623 raise_param_exc({ skiplock => "Only root may use this option." })
a0d1b1a2 1624 if $skiplock && $authuser ne 'root@pam';
5fdbe4f0
DM
1625
1626 PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
1627
1628 return;
1e3baf05
DM
1629 }});
1630
3ea94c60 1631__PACKAGE__->register_method({
afdb31d5 1632 name => 'migrate_vm',
3ea94c60
DM
1633 path => '{vmid}/migrate',
1634 method => 'POST',
1635 protected => 1,
1636 proxyto => 'node',
1637 description => "Migrate virtual machine. Creates a new migration task.",
a0d1b1a2
DM
1638 permissions => {
1639 check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
1640 },
3ea94c60
DM
1641 parameters => {
1642 additionalProperties => 0,
1643 properties => {
1644 node => get_standard_option('pve-node'),
1645 vmid => get_standard_option('pve-vmid'),
1646 target => get_standard_option('pve-node', { description => "Target node." }),
1647 online => {
1648 type => 'boolean',
1649 description => "Use online/live migration.",
1650 optional => 1,
1651 },
1652 force => {
1653 type => 'boolean',
1654 description => "Allow to migrate VMs which use local devices. Only root may use this option.",
1655 optional => 1,
1656 },
1657 },
1658 },
afdb31d5 1659 returns => {
3ea94c60
DM
1660 type => 'string',
1661 description => "the task ID.",
1662 },
1663 code => sub {
1664 my ($param) = @_;
1665
1666 my $rpcenv = PVE::RPCEnvironment::get();
1667
a0d1b1a2 1668 my $authuser = $rpcenv->get_user();
3ea94c60
DM
1669
1670 my $target = extract_param($param, 'target');
1671
1672 my $localnode = PVE::INotify::nodename();
1673 raise_param_exc({ target => "target is local node."}) if $target eq $localnode;
1674
1675 PVE::Cluster::check_cfs_quorum();
1676
1677 PVE::Cluster::check_node_exists($target);
1678
1679 my $targetip = PVE::Cluster::remote_node_ip($target);
1680
1681 my $vmid = extract_param($param, 'vmid');
1682
afdb31d5 1683 raise_param_exc({ force => "Only root may use this option." })
a0d1b1a2 1684 if $param->{force} && $authuser ne 'root@pam';
3ea94c60
DM
1685
1686 # test if VM exists
a5ed42d3 1687 my $conf = PVE::QemuServer::load_config($vmid);
3ea94c60
DM
1688
1689 # try to detect errors early
a5ed42d3
DM
1690
1691 PVE::QemuServer::check_lock($conf);
1692
3ea94c60 1693 if (PVE::QemuServer::check_running($vmid)) {
afdb31d5 1694 die "cant migrate running VM without --online\n"
3ea94c60
DM
1695 if !$param->{online};
1696 }
1697
47152e2e 1698 my $storecfg = PVE::Storage::config();
22d646a7 1699 PVE::QemuServer::check_storage_availability($storecfg, $conf, $target);
47152e2e 1700
3be30d63 1701 if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
3ea94c60 1702
88fc87b4
DM
1703 my $hacmd = sub {
1704 my $upid = shift;
3ea94c60 1705
88fc87b4
DM
1706 my $service = "pvevm:$vmid";
1707
1708 my $cmd = ['clusvcadm', '-M', $service, '-m', $target];
1709
1710 print "Executing HA migrate for VM $vmid to node $target\n";
1711
1712 PVE::Tools::run_command($cmd);
1713
1714 return;
1715 };
1716
1717 return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd);
1718
1719 } else {
1720
1721 my $realcmd = sub {
1722 my $upid = shift;
1723
1724 PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
1725 };
1726
1727 return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd);
1728 }
3ea94c60 1729
3ea94c60 1730 }});
1e3baf05 1731
91c94f0a 1732__PACKAGE__->register_method({
afdb31d5
DM
1733 name => 'monitor',
1734 path => '{vmid}/monitor',
91c94f0a
DM
1735 method => 'POST',
1736 protected => 1,
1737 proxyto => 'node',
1738 description => "Execute Qemu monitor commands.",
a0d1b1a2
DM
1739 permissions => {
1740 check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
1741 },
91c94f0a
DM
1742 parameters => {
1743 additionalProperties => 0,
1744 properties => {
1745 node => get_standard_option('pve-node'),
1746 vmid => get_standard_option('pve-vmid'),
1747 command => {
1748 type => 'string',
1749 description => "The monitor command.",
1750 }
1751 },
1752 },
1753 returns => { type => 'string'},
1754 code => sub {
1755 my ($param) = @_;
1756
1757 my $vmid = $param->{vmid};
1758
1759 my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
1760
1761 my $res = '';
1762 eval {
7b7c6d1b 1763 $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command});
91c94f0a
DM
1764 };
1765 $res = "ERROR: $@" if $@;
1766
1767 return $res;
1768 }});
1769
0d02881c
AD
1770__PACKAGE__->register_method({
1771 name => 'resize_vm',
614e3941 1772 path => '{vmid}/resize',
0d02881c
AD
1773 method => 'PUT',
1774 protected => 1,
1775 proxyto => 'node',
2f48a4f5 1776 description => "Extend volume size.",
0d02881c 1777 permissions => {
3b2773f6 1778 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
0d02881c
AD
1779 },
1780 parameters => {
1781 additionalProperties => 0,
2f48a4f5
DM
1782 properties => {
1783 node => get_standard_option('pve-node'),
1784 vmid => get_standard_option('pve-vmid'),
1785 skiplock => get_standard_option('skiplock'),
1786 disk => {
1787 type => 'string',
1788 description => "The disk you want to resize.",
1789 enum => [PVE::QemuServer::disknames()],
1790 },
1791 size => {
1792 type => 'string',
f91b2e45 1793 pattern => '\+?\d+(\.\d+)?[KMGT]?',
2f48a4f5
DM
1794 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.",
1795 },
1796 digest => {
1797 type => 'string',
1798 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1799 maxLength => 40,
1800 optional => 1,
1801 },
1802 },
0d02881c
AD
1803 },
1804 returns => { type => 'null'},
1805 code => sub {
1806 my ($param) = @_;
1807
1808 my $rpcenv = PVE::RPCEnvironment::get();
1809
1810 my $authuser = $rpcenv->get_user();
1811
1812 my $node = extract_param($param, 'node');
1813
1814 my $vmid = extract_param($param, 'vmid');
1815
1816 my $digest = extract_param($param, 'digest');
1817
2f48a4f5
DM
1818 my $disk = extract_param($param, 'disk');
1819
1820 my $sizestr = extract_param($param, 'size');
0d02881c 1821
f91b2e45 1822 my $skiplock = extract_param($param, 'skiplock');
0d02881c
AD
1823 raise_param_exc({ skiplock => "Only root may use this option." })
1824 if $skiplock && $authuser ne 'root@pam';
1825
0d02881c
AD
1826 my $storecfg = PVE::Storage::config();
1827
0d02881c
AD
1828 my $updatefn = sub {
1829
1830 my $conf = PVE::QemuServer::load_config($vmid);
1831
1832 die "checksum missmatch (file change by other user?)\n"
1833 if $digest && $digest ne $conf->{digest};
1834 PVE::QemuServer::check_lock($conf) if !$skiplock;
1835
f91b2e45
DM
1836 die "disk '$disk' does not exist\n" if !$conf->{$disk};
1837
1838 my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
1839
1840 my $volid = $drive->{file};
1841
1842 die "disk '$disk' has no associated volume\n" if !$volid;
1843
1844 die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
1845
1846 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
1847
1848 $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
1849
1850 my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
1851
1852 die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
1853 my ($ext, $newsize, $unit) = ($1, $2, $4);
1854 if ($unit) {
1855 if ($unit eq 'K') {
1856 $newsize = $newsize * 1024;
1857 } elsif ($unit eq 'M') {
1858 $newsize = $newsize * 1024 * 1024;
1859 } elsif ($unit eq 'G') {
1860 $newsize = $newsize * 1024 * 1024 * 1024;
1861 } elsif ($unit eq 'T') {
1862 $newsize = $newsize * 1024 * 1024 * 1024 * 1024;
1863 }
1864 }
1865 $newsize += $size if $ext;
1866 $newsize = int($newsize);
1867
1868 die "unable to skrink disk size\n" if $newsize < $size;
1869
1870 return if $size == $newsize;
1871
2f48a4f5 1872 PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr");
0d02881c 1873
f91b2e45
DM
1874 PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize);
1875
1876 $drive->{size} = $newsize;
1877 $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive);
1878
1879 PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
1880 };
0d02881c
AD
1881
1882 PVE::QemuServer::lock_config($vmid, $updatefn);
1883 return undef;
1884 }});
1885
9dbd1ee4
AD
1886__PACKAGE__->register_method({
1887 name => 'snapshot_vm',
1888 path => '{vmid}/snapshot',
1889 method => 'PUT',
1890 protected => 1,
1891 proxyto => 'node',
1892 description => "Snapshot a VM.",
1893 permissions => {
1894 check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
1895 },
1896 parameters => {
1897 additionalProperties => 0,
1898 properties => {
1899 node => get_standard_option('pve-node'),
1900 vmid => get_standard_option('pve-vmid'),
1901 skiplock => get_standard_option('skiplock'),
1902 action => {
1903 type => 'string',
1904 description => "Action",
1905 enum => [ 'create', 'delete', 'rollback' ],
1906 },
1907 snapname => {
1908 type => 'string',
1909 description => "The name of the snapshot",
1910 maxLength => 40,
1911 },
1912 vmstate => {
1913 optional => 1,
1914 type => 'boolean',
1915 description => "Save the vmstate",
1916 },
1917 freezefs => {
1918 optional => 1,
1919 type => 'boolean',
1920 description => "Freeze the filesystem",
1921 },
1922 digest => {
1923 type => 'string',
1924 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
1925 maxLength => 40,
1926 optional => 1,
1927 },
1928 },
1929 },
1930 returns => { type => 'null'},
1931 code => sub {
1932 my ($param) = @_;
1933
1934 my $rpcenv = PVE::RPCEnvironment::get();
1935
1936 my $authuser = $rpcenv->get_user();
1937
1938 my $node = extract_param($param, 'node');
1939
1940 my $vmid = extract_param($param, 'vmid');
1941
1942 my $digest = extract_param($param, 'digest');
1943
1944 my $action = extract_param($param, 'action');
1945
1946 my $snapname = extract_param($param, 'snapname');
1947
1948 my $vmstate = extract_param($param, 'vmstate');
1949
1950 my $freezefs = extract_param($param, 'freezefs');
1951
1952 my $skiplock = extract_param($param, 'skiplock');
1953 raise_param_exc({ skiplock => "Only root may use this option." })
1954 if $skiplock && $authuser ne 'root@pam';
1955
9dbd1ee4 1956
22c377f0
DM
1957 # fixme: digest?
1958 # fixme: access rights?
1959 # &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $conf);
1960 # fixme: need to implement a check to see if all storages support snapshots
1961
1962 if($action eq 'create') {
1963 PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
1964 PVE::QemuServer::snapshot_create($vmid, $snapname, $vmstate, $freezefs);
1965 } elsif($action eq 'rollback'){
1966 PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname");
1967 PVE::QemuServer::snapshot_rollback($vmid, $snapname);
1968 } elsif($action eq 'delete') {
1969 PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname");
1970 PVE::QemuServer::snapshot_delete($vmid, $snapname, $vmstate);
1971 }
9dbd1ee4 1972
9dbd1ee4
AD
1973 return undef;
1974 }});
1975
1e3baf05 19761;