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