]>
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 raise_perm_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 | use PVE::Network; | |
20 | ||
21 | use Data::Dumper; # fixme: remove | |
22 | ||
23 | use base qw(PVE::RESTHandler); | |
24 | ||
25 | 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."; | |
26 | ||
27 | my $resolve_cdrom_alias = sub { | |
28 | my $param = shift; | |
29 | ||
30 | if (my $value = $param->{cdrom}) { | |
31 | $value .= ",media=cdrom" if $value !~ m/media=/; | |
32 | $param->{ide2} = $value; | |
33 | delete $param->{cdrom}; | |
34 | } | |
35 | }; | |
36 | ||
37 | ||
38 | my $check_storage_access = sub { | |
39 | my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_; | |
40 | ||
41 | PVE::QemuServer::foreach_drive($settings, sub { | |
42 | my ($ds, $drive) = @_; | |
43 | ||
44 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); | |
45 | ||
46 | my $volid = $drive->{file}; | |
47 | ||
48 | if (!$volid || $volid eq 'none') { | |
49 | # nothing to check | |
50 | } elsif ($isCDROM && ($volid eq 'cdrom')) { | |
51 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
52 | } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) { | |
53 | my ($storeid, $size) = ($2 || $default_storage, $3); | |
54 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
55 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
56 | } else { | |
57 | $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid); | |
58 | } | |
59 | }); | |
60 | }; | |
61 | ||
62 | my $check_storage_access_clone = sub { | |
63 | my ($rpcenv, $authuser, $storecfg, $conf, $storage) = @_; | |
64 | ||
65 | my $sharedvm = 1; | |
66 | ||
67 | PVE::QemuServer::foreach_drive($conf, sub { | |
68 | my ($ds, $drive) = @_; | |
69 | ||
70 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); | |
71 | ||
72 | my $volid = $drive->{file}; | |
73 | ||
74 | return if !$volid || $volid eq 'none'; | |
75 | ||
76 | if ($isCDROM) { | |
77 | if ($volid eq 'cdrom') { | |
78 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
79 | } else { | |
80 | # we simply allow access | |
81 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
82 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
83 | $sharedvm = 0 if !$scfg->{shared}; | |
84 | ||
85 | } | |
86 | } else { | |
87 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
88 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
89 | $sharedvm = 0 if !$scfg->{shared}; | |
90 | ||
91 | $sid = $storage if $storage; | |
92 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); | |
93 | } | |
94 | }); | |
95 | ||
96 | return $sharedvm; | |
97 | }; | |
98 | ||
99 | # Note: $pool is only needed when creating a VM, because pool permissions | |
100 | # are automatically inherited if VM already exists inside a pool. | |
101 | my $create_disks = sub { | |
102 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_; | |
103 | ||
104 | my $vollist = []; | |
105 | ||
106 | my $res = {}; | |
107 | PVE::QemuServer::foreach_drive($settings, sub { | |
108 | my ($ds, $disk) = @_; | |
109 | ||
110 | my $volid = $disk->{file}; | |
111 | ||
112 | if (!$volid || $volid eq 'none' || $volid eq 'cdrom') { | |
113 | delete $disk->{size}; | |
114 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
115 | } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) { | |
116 | my ($storeid, $size) = ($2 || $default_storage, $3); | |
117 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
118 | my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid); | |
119 | my $fmt = $disk->{format} || $defformat; | |
120 | my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, | |
121 | $fmt, undef, $size*1024*1024); | |
122 | $disk->{file} = $volid; | |
123 | $disk->{size} = $size*1024*1024*1024; | |
124 | push @$vollist, $volid; | |
125 | delete $disk->{format}; # no longer needed | |
126 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
127 | } else { | |
128 | ||
129 | my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid); | |
130 | ||
131 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
132 | ||
133 | my $foundvolid = undef; | |
134 | ||
135 | if ($storeid) { | |
136 | PVE::Storage::activate_volumes($storecfg, [ $volid ]); | |
137 | my $dl = PVE::Storage::vdisk_list($storecfg, $storeid, undef); | |
138 | ||
139 | PVE::Storage::foreach_volid($dl, sub { | |
140 | my ($volumeid) = @_; | |
141 | if($volumeid eq $volid) { | |
142 | $foundvolid = 1; | |
143 | return; | |
144 | } | |
145 | }); | |
146 | } | |
147 | ||
148 | die "image '$path' does not exists\n" if (!(-f $path || -b $path || $foundvolid)); | |
149 | ||
150 | my ($size) = PVE::Storage::volume_size_info($storecfg, $volid, 1); | |
151 | $disk->{size} = $size; | |
152 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
153 | } | |
154 | }); | |
155 | ||
156 | # free allocated images on error | |
157 | if (my $err = $@) { | |
158 | syslog('err', "VM $vmid creating disks failed"); | |
159 | foreach my $volid (@$vollist) { | |
160 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
161 | warn $@ if $@; | |
162 | } | |
163 | die $err; | |
164 | } | |
165 | ||
166 | # modify vm config if everything went well | |
167 | foreach my $ds (keys %$res) { | |
168 | $conf->{$ds} = $res->{$ds}; | |
169 | } | |
170 | ||
171 | return $vollist; | |
172 | }; | |
173 | ||
174 | my $check_vm_modify_config_perm = sub { | |
175 | my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_; | |
176 | ||
177 | return 1 if $authuser eq 'root@pam'; | |
178 | ||
179 | foreach my $opt (@$key_list) { | |
180 | # disk checks need to be done somewhere else | |
181 | next if PVE::QemuServer::valid_drivename($opt); | |
182 | ||
183 | if ($opt eq 'sockets' || $opt eq 'cores' || | |
184 | $opt eq 'cpu' || $opt eq 'smp' || | |
185 | $opt eq 'cpulimit' || $opt eq 'cpuunits') { | |
186 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']); | |
187 | } elsif ($opt eq 'boot' || $opt eq 'bootdisk') { | |
188 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']); | |
189 | } elsif ($opt eq 'memory' || $opt eq 'balloon' || $opt eq 'shares') { | |
190 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']); | |
191 | } elsif ($opt eq 'args' || $opt eq 'lock') { | |
192 | die "only root can set '$opt' config\n"; | |
193 | } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' || | |
194 | $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') { | |
195 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); | |
196 | } elsif ($opt =~ m/^net\d+$/) { | |
197 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']); | |
198 | } else { | |
199 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']); | |
200 | } | |
201 | } | |
202 | ||
203 | return 1; | |
204 | }; | |
205 | ||
206 | __PACKAGE__->register_method({ | |
207 | name => 'vmlist', | |
208 | path => '', | |
209 | method => 'GET', | |
210 | description => "Virtual machine index (per node).", | |
211 | permissions => { | |
212 | description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.", | |
213 | user => 'all', | |
214 | }, | |
215 | proxyto => 'node', | |
216 | protected => 1, # qemu pid files are only readable by root | |
217 | parameters => { | |
218 | additionalProperties => 0, | |
219 | properties => { | |
220 | node => get_standard_option('pve-node'), | |
221 | }, | |
222 | }, | |
223 | returns => { | |
224 | type => 'array', | |
225 | items => { | |
226 | type => "object", | |
227 | properties => {}, | |
228 | }, | |
229 | links => [ { rel => 'child', href => "{vmid}" } ], | |
230 | }, | |
231 | code => sub { | |
232 | my ($param) = @_; | |
233 | ||
234 | my $rpcenv = PVE::RPCEnvironment::get(); | |
235 | my $authuser = $rpcenv->get_user(); | |
236 | ||
237 | my $vmstatus = PVE::QemuServer::vmstatus(); | |
238 | ||
239 | my $res = []; | |
240 | foreach my $vmid (keys %$vmstatus) { | |
241 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1); | |
242 | ||
243 | my $data = $vmstatus->{$vmid}; | |
244 | $data->{vmid} = $vmid; | |
245 | push @$res, $data; | |
246 | } | |
247 | ||
248 | return $res; | |
249 | }}); | |
250 | ||
251 | ||
252 | ||
253 | __PACKAGE__->register_method({ | |
254 | name => 'create_vm', | |
255 | path => '', | |
256 | method => 'POST', | |
257 | description => "Create or restore a virtual machine.", | |
258 | permissions => { | |
259 | description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " . | |
260 | "For restore (option 'archive'), it is enough if the user has 'VM.Backup' permission and the VM already exists. " . | |
261 | "If you create disks you need 'Datastore.AllocateSpace' on any used storage.", | |
262 | user => 'all', # check inside | |
263 | }, | |
264 | protected => 1, | |
265 | proxyto => 'node', | |
266 | parameters => { | |
267 | additionalProperties => 0, | |
268 | properties => PVE::QemuServer::json_config_properties( | |
269 | { | |
270 | node => get_standard_option('pve-node'), | |
271 | vmid => get_standard_option('pve-vmid'), | |
272 | archive => { | |
273 | description => "The backup file.", | |
274 | type => 'string', | |
275 | optional => 1, | |
276 | maxLength => 255, | |
277 | }, | |
278 | storage => get_standard_option('pve-storage-id', { | |
279 | description => "Default storage.", | |
280 | optional => 1, | |
281 | }), | |
282 | force => { | |
283 | optional => 1, | |
284 | type => 'boolean', | |
285 | description => "Allow to overwrite existing VM.", | |
286 | requires => 'archive', | |
287 | }, | |
288 | unique => { | |
289 | optional => 1, | |
290 | type => 'boolean', | |
291 | description => "Assign a unique random ethernet address.", | |
292 | requires => 'archive', | |
293 | }, | |
294 | pool => { | |
295 | optional => 1, | |
296 | type => 'string', format => 'pve-poolid', | |
297 | description => "Add the VM to the specified pool.", | |
298 | }, | |
299 | }), | |
300 | }, | |
301 | returns => { | |
302 | type => 'string', | |
303 | }, | |
304 | code => sub { | |
305 | my ($param) = @_; | |
306 | ||
307 | my $rpcenv = PVE::RPCEnvironment::get(); | |
308 | ||
309 | my $authuser = $rpcenv->get_user(); | |
310 | ||
311 | my $node = extract_param($param, 'node'); | |
312 | ||
313 | my $vmid = extract_param($param, 'vmid'); | |
314 | ||
315 | my $archive = extract_param($param, 'archive'); | |
316 | ||
317 | my $storage = extract_param($param, 'storage'); | |
318 | ||
319 | my $force = extract_param($param, 'force'); | |
320 | ||
321 | my $unique = extract_param($param, 'unique'); | |
322 | ||
323 | my $pool = extract_param($param, 'pool'); | |
324 | ||
325 | my $filename = PVE::QemuServer::config_file($vmid); | |
326 | ||
327 | my $storecfg = PVE::Storage::config(); | |
328 | ||
329 | PVE::Cluster::check_cfs_quorum(); | |
330 | ||
331 | if (defined($pool)) { | |
332 | $rpcenv->check_pool_exist($pool); | |
333 | } | |
334 | ||
335 | $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']) | |
336 | if defined($storage); | |
337 | ||
338 | if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) { | |
339 | # OK | |
340 | } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) { | |
341 | # OK | |
342 | } elsif ($archive && $force && (-f $filename) && | |
343 | $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) { | |
344 | # OK: user has VM.Backup permissions, and want to restore an existing VM | |
345 | } else { | |
346 | raise_perm_exc(); | |
347 | } | |
348 | ||
349 | if (!$archive) { | |
350 | &$resolve_cdrom_alias($param); | |
351 | ||
352 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage); | |
353 | ||
354 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]); | |
355 | ||
356 | foreach my $opt (keys %$param) { | |
357 | if (PVE::QemuServer::valid_drivename($opt)) { | |
358 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
359 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; | |
360 | ||
361 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); | |
362 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
363 | } | |
364 | } | |
365 | ||
366 | PVE::QemuServer::add_random_macs($param); | |
367 | } else { | |
368 | my $keystr = join(' ', keys %$param); | |
369 | raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr; | |
370 | ||
371 | if ($archive eq '-') { | |
372 | die "pipe requires cli environment\n" | |
373 | if $rpcenv->{type} ne 'cli'; | |
374 | } else { | |
375 | my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive); | |
376 | ||
377 | PVE::Storage::activate_volumes($storecfg, [ $archive ]) | |
378 | if PVE::Storage::parse_volume_id ($archive, 1); | |
379 | ||
380 | die "can't find archive file '$archive'\n" if !($path && -f $path); | |
381 | $archive = $path; | |
382 | } | |
383 | } | |
384 | ||
385 | my $restorefn = sub { | |
386 | ||
387 | # fixme: this test does not work if VM exists on other node! | |
388 | if (-f $filename) { | |
389 | die "unable to restore vm $vmid: config file already exists\n" | |
390 | if !$force; | |
391 | ||
392 | die "unable to restore vm $vmid: vm is running\n" | |
393 | if PVE::QemuServer::check_running($vmid); | |
394 | } | |
395 | ||
396 | my $realcmd = sub { | |
397 | PVE::QemuServer::restore_archive($archive, $vmid, $authuser, { | |
398 | storage => $storage, | |
399 | pool => $pool, | |
400 | unique => $unique }); | |
401 | ||
402 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; | |
403 | }; | |
404 | ||
405 | return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd); | |
406 | }; | |
407 | ||
408 | my $createfn = sub { | |
409 | ||
410 | # test after locking | |
411 | die "unable to create vm $vmid: config file already exists\n" | |
412 | if -f $filename; | |
413 | ||
414 | my $realcmd = sub { | |
415 | ||
416 | my $vollist = []; | |
417 | ||
418 | my $conf = $param; | |
419 | ||
420 | eval { | |
421 | ||
422 | $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage); | |
423 | ||
424 | # try to be smart about bootdisk | |
425 | my @disks = PVE::QemuServer::disknames(); | |
426 | my $firstdisk; | |
427 | foreach my $ds (reverse @disks) { | |
428 | next if !$conf->{$ds}; | |
429 | my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds}); | |
430 | next if PVE::QemuServer::drive_is_cdrom($disk); | |
431 | $firstdisk = $ds; | |
432 | } | |
433 | ||
434 | if (!$conf->{bootdisk} && $firstdisk) { | |
435 | $conf->{bootdisk} = $firstdisk; | |
436 | } | |
437 | ||
438 | PVE::QemuServer::update_config_nolock($vmid, $conf); | |
439 | ||
440 | }; | |
441 | my $err = $@; | |
442 | ||
443 | if ($err) { | |
444 | foreach my $volid (@$vollist) { | |
445 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
446 | warn $@ if $@; | |
447 | } | |
448 | die "create failed - $err"; | |
449 | } | |
450 | ||
451 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; | |
452 | }; | |
453 | ||
454 | return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd); | |
455 | }; | |
456 | ||
457 | return PVE::QemuServer::lock_config_full($vmid, 1, $archive ? $restorefn : $createfn); | |
458 | }}); | |
459 | ||
460 | __PACKAGE__->register_method({ | |
461 | name => 'vmdiridx', | |
462 | path => '{vmid}', | |
463 | method => 'GET', | |
464 | proxyto => 'node', | |
465 | description => "Directory index", | |
466 | permissions => { | |
467 | user => 'all', | |
468 | }, | |
469 | parameters => { | |
470 | additionalProperties => 0, | |
471 | properties => { | |
472 | node => get_standard_option('pve-node'), | |
473 | vmid => get_standard_option('pve-vmid'), | |
474 | }, | |
475 | }, | |
476 | returns => { | |
477 | type => 'array', | |
478 | items => { | |
479 | type => "object", | |
480 | properties => { | |
481 | subdir => { type => 'string' }, | |
482 | }, | |
483 | }, | |
484 | links => [ { rel => 'child', href => "{subdir}" } ], | |
485 | }, | |
486 | code => sub { | |
487 | my ($param) = @_; | |
488 | ||
489 | my $res = [ | |
490 | { subdir => 'config' }, | |
491 | { subdir => 'status' }, | |
492 | { subdir => 'unlink' }, | |
493 | { subdir => 'vncproxy' }, | |
494 | { subdir => 'migrate' }, | |
495 | { subdir => 'resize' }, | |
496 | { subdir => 'move' }, | |
497 | { subdir => 'rrd' }, | |
498 | { subdir => 'rrddata' }, | |
499 | { subdir => 'monitor' }, | |
500 | { subdir => 'snapshot' }, | |
501 | ]; | |
502 | ||
503 | return $res; | |
504 | }}); | |
505 | ||
506 | __PACKAGE__->register_method({ | |
507 | name => 'rrd', | |
508 | path => '{vmid}/rrd', | |
509 | method => 'GET', | |
510 | protected => 1, # fixme: can we avoid that? | |
511 | permissions => { | |
512 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
513 | }, | |
514 | description => "Read VM RRD statistics (returns PNG)", | |
515 | parameters => { | |
516 | additionalProperties => 0, | |
517 | properties => { | |
518 | node => get_standard_option('pve-node'), | |
519 | vmid => get_standard_option('pve-vmid'), | |
520 | timeframe => { | |
521 | description => "Specify the time frame you are interested in.", | |
522 | type => 'string', | |
523 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
524 | }, | |
525 | ds => { | |
526 | description => "The list of datasources you want to display.", | |
527 | type => 'string', format => 'pve-configid-list', | |
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 => "object", | |
539 | properties => { | |
540 | filename => { type => 'string' }, | |
541 | }, | |
542 | }, | |
543 | code => sub { | |
544 | my ($param) = @_; | |
545 | ||
546 | return PVE::Cluster::create_rrd_graph( | |
547 | "pve2-vm/$param->{vmid}", $param->{timeframe}, | |
548 | $param->{ds}, $param->{cf}); | |
549 | ||
550 | }}); | |
551 | ||
552 | __PACKAGE__->register_method({ | |
553 | name => 'rrddata', | |
554 | path => '{vmid}/rrddata', | |
555 | method => 'GET', | |
556 | protected => 1, # fixme: can we avoid that? | |
557 | permissions => { | |
558 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
559 | }, | |
560 | description => "Read VM RRD statistics", | |
561 | parameters => { | |
562 | additionalProperties => 0, | |
563 | properties => { | |
564 | node => get_standard_option('pve-node'), | |
565 | vmid => get_standard_option('pve-vmid'), | |
566 | timeframe => { | |
567 | description => "Specify the time frame you are interested in.", | |
568 | type => 'string', | |
569 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
570 | }, | |
571 | cf => { | |
572 | description => "The RRD consolidation function", | |
573 | type => 'string', | |
574 | enum => [ 'AVERAGE', 'MAX' ], | |
575 | optional => 1, | |
576 | }, | |
577 | }, | |
578 | }, | |
579 | returns => { | |
580 | type => "array", | |
581 | items => { | |
582 | type => "object", | |
583 | properties => {}, | |
584 | }, | |
585 | }, | |
586 | code => sub { | |
587 | my ($param) = @_; | |
588 | ||
589 | return PVE::Cluster::create_rrd_data( | |
590 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); | |
591 | }}); | |
592 | ||
593 | ||
594 | __PACKAGE__->register_method({ | |
595 | name => 'vm_config', | |
596 | path => '{vmid}/config', | |
597 | method => 'GET', | |
598 | proxyto => 'node', | |
599 | description => "Get virtual machine configuration.", | |
600 | permissions => { | |
601 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
602 | }, | |
603 | parameters => { | |
604 | additionalProperties => 0, | |
605 | properties => { | |
606 | node => get_standard_option('pve-node'), | |
607 | vmid => get_standard_option('pve-vmid'), | |
608 | }, | |
609 | }, | |
610 | returns => { | |
611 | type => "object", | |
612 | properties => { | |
613 | digest => { | |
614 | type => 'string', | |
615 | description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', | |
616 | } | |
617 | }, | |
618 | }, | |
619 | code => sub { | |
620 | my ($param) = @_; | |
621 | ||
622 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
623 | ||
624 | delete $conf->{snapshots}; | |
625 | ||
626 | return $conf; | |
627 | }}); | |
628 | ||
629 | my $vm_is_volid_owner = sub { | |
630 | my ($storecfg, $vmid, $volid) =@_; | |
631 | ||
632 | if ($volid !~ m|^/|) { | |
633 | my ($path, $owner); | |
634 | eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); }; | |
635 | if ($owner && ($owner == $vmid)) { | |
636 | return 1; | |
637 | } | |
638 | } | |
639 | ||
640 | return undef; | |
641 | }; | |
642 | ||
643 | my $test_deallocate_drive = sub { | |
644 | my ($storecfg, $vmid, $key, $drive, $force) = @_; | |
645 | ||
646 | if (!PVE::QemuServer::drive_is_cdrom($drive)) { | |
647 | my $volid = $drive->{file}; | |
648 | if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) { | |
649 | if ($force || $key =~ m/^unused/) { | |
650 | my $sid = PVE::Storage::parse_volume_id($volid); | |
651 | return $sid; | |
652 | } | |
653 | } | |
654 | } | |
655 | ||
656 | return undef; | |
657 | }; | |
658 | ||
659 | my $delete_drive = sub { | |
660 | my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_; | |
661 | ||
662 | if (!PVE::QemuServer::drive_is_cdrom($drive)) { | |
663 | my $volid = $drive->{file}; | |
664 | ||
665 | if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) { | |
666 | if ($force || $key =~ m/^unused/) { | |
667 | eval { | |
668 | # check if the disk is really unused | |
669 | my $used_paths = PVE::QemuServer::get_used_paths($vmid, $storecfg, $conf, 1, $key); | |
670 | my $path = PVE::Storage::path($storecfg, $volid); | |
671 | ||
672 | die "unable to delete '$volid' - volume is still in use (snapshot?)\n" | |
673 | if $used_paths->{$path}; | |
674 | ||
675 | PVE::Storage::vdisk_free($storecfg, $volid); | |
676 | }; | |
677 | die $@ if $@; | |
678 | } else { | |
679 | PVE::QemuServer::add_unused_volume($conf, $volid, $vmid); | |
680 | } | |
681 | } | |
682 | } | |
683 | ||
684 | delete $conf->{$key}; | |
685 | }; | |
686 | ||
687 | my $vmconfig_delete_option = sub { | |
688 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_; | |
689 | ||
690 | return if !defined($conf->{$opt}); | |
691 | ||
692 | my $isDisk = PVE::QemuServer::valid_drivename($opt)|| ($opt =~ m/^unused/); | |
693 | ||
694 | if ($isDisk) { | |
695 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
696 | ||
697 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
698 | if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) { | |
699 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']); | |
700 | } | |
701 | } | |
702 | ||
703 | my $unplugwarning = ""; | |
704 | if($conf->{ostype} && $conf->{ostype} eq 'l26'){ | |
705 | $unplugwarning = "<br>verify that you have acpiphp && pci_hotplug modules loaded in your guest VM"; | |
706 | }elsif($conf->{ostype} && $conf->{ostype} eq 'l24'){ | |
707 | $unplugwarning = "<br>kernel 2.4 don't support hotplug, please disable hotplug in options"; | |
708 | }elsif(!$conf->{ostype} || ($conf->{ostype} && $conf->{ostype} eq 'other')){ | |
709 | $unplugwarning = "<br>verify that your guest support acpi hotplug"; | |
710 | } | |
711 | ||
712 | if($opt eq 'tablet'){ | |
713 | PVE::QemuServer::vm_deviceplug(undef, $conf, $vmid, $opt); | |
714 | }else{ | |
715 | die "error hot-unplug $opt $unplugwarning" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt); | |
716 | } | |
717 | ||
718 | if ($isDisk) { | |
719 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
720 | &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force); | |
721 | } else { | |
722 | delete $conf->{$opt}; | |
723 | } | |
724 | ||
725 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
726 | }; | |
727 | ||
728 | my $safe_num_ne = sub { | |
729 | my ($a, $b) = @_; | |
730 | ||
731 | return 0 if !defined($a) && !defined($b); | |
732 | return 1 if !defined($a); | |
733 | return 1 if !defined($b); | |
734 | ||
735 | return $a != $b; | |
736 | }; | |
737 | ||
738 | my $vmconfig_update_disk = sub { | |
739 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_; | |
740 | ||
741 | my $drive = PVE::QemuServer::parse_drive($opt, $value); | |
742 | ||
743 | if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom | |
744 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']); | |
745 | } else { | |
746 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
747 | } | |
748 | ||
749 | if ($conf->{$opt}) { | |
750 | ||
751 | if (my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt})) { | |
752 | ||
753 | my $media = $drive->{media} || 'disk'; | |
754 | my $oldmedia = $old_drive->{media} || 'disk'; | |
755 | die "unable to change media type\n" if $media ne $oldmedia; | |
756 | ||
757 | if (!PVE::QemuServer::drive_is_cdrom($old_drive) && | |
758 | ($drive->{file} ne $old_drive->{file})) { # delete old disks | |
759 | ||
760 | &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force); | |
761 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
762 | } | |
763 | ||
764 | if(&$safe_num_ne($drive->{mbps}, $old_drive->{mbps}) || | |
765 | &$safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) || | |
766 | &$safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) || | |
767 | &$safe_num_ne($drive->{iops}, $old_drive->{iops}) || | |
768 | &$safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) || | |
769 | &$safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr})) { | |
770 | PVE::QemuServer::qemu_block_set_io_throttle($vmid,"drive-$opt", $drive->{mbps}*1024*1024, | |
771 | $drive->{mbps_rd}*1024*1024, $drive->{mbps_wr}*1024*1024, | |
772 | $drive->{iops}, $drive->{iops_rd}, $drive->{iops_wr}) | |
773 | if !PVE::QemuServer::drive_is_cdrom($drive); | |
774 | } | |
775 | } | |
776 | } | |
777 | ||
778 | &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value}); | |
779 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
780 | ||
781 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
782 | $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
783 | ||
784 | if (PVE::QemuServer::drive_is_cdrom($drive)) { # cdrom | |
785 | ||
786 | if (PVE::QemuServer::check_running($vmid)) { | |
787 | if ($drive->{file} eq 'none') { | |
788 | PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt"); | |
789 | } else { | |
790 | my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file}); | |
791 | PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt"); #force eject if locked | |
792 | PVE::QemuServer::vm_mon_cmd($vmid, "change",device => "drive-$opt",target => "$path") if $path; | |
793 | } | |
794 | } | |
795 | ||
796 | } else { # hotplug new disks | |
797 | ||
798 | die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive); | |
799 | } | |
800 | }; | |
801 | ||
802 | my $vmconfig_update_net = sub { | |
803 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_; | |
804 | ||
805 | if ($conf->{$opt} && PVE::QemuServer::check_running($vmid)) { | |
806 | my $oldnet = PVE::QemuServer::parse_net($conf->{$opt}); | |
807 | my $newnet = PVE::QemuServer::parse_net($value); | |
808 | ||
809 | if($oldnet->{model} ne $newnet->{model}){ | |
810 | #if model change, we try to hot-unplug | |
811 | die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt); | |
812 | }else{ | |
813 | ||
814 | if($newnet->{bridge} && $oldnet->{bridge}){ | |
815 | my $iface = "tap".$vmid."i".$1 if $opt =~ m/net(\d+)/; | |
816 | ||
817 | if($newnet->{rate} ne $oldnet->{rate}){ | |
818 | PVE::Network::tap_rate_limit($iface, $newnet->{rate}); | |
819 | } | |
820 | ||
821 | if(($newnet->{bridge} ne $oldnet->{bridge}) || ($newnet->{tag} ne $oldnet->{tag})){ | |
822 | eval{PVE::Network::tap_unplug($iface, $oldnet->{bridge}, $oldnet->{tag});}; | |
823 | PVE::Network::tap_plug($iface, $newnet->{bridge}, $newnet->{tag}); | |
824 | } | |
825 | ||
826 | }else{ | |
827 | #if bridge/nat mode change, we try to hot-unplug | |
828 | die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt); | |
829 | } | |
830 | } | |
831 | ||
832 | } | |
833 | $conf->{$opt} = $value; | |
834 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
835 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
836 | ||
837 | my $net = PVE::QemuServer::parse_net($conf->{$opt}); | |
838 | ||
839 | die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net); | |
840 | }; | |
841 | ||
842 | my $vm_config_perm_list = [ | |
843 | 'VM.Config.Disk', | |
844 | 'VM.Config.CDROM', | |
845 | 'VM.Config.CPU', | |
846 | 'VM.Config.Memory', | |
847 | 'VM.Config.Network', | |
848 | 'VM.Config.HWType', | |
849 | 'VM.Config.Options', | |
850 | ]; | |
851 | ||
852 | __PACKAGE__->register_method({ | |
853 | name => 'update_vm', | |
854 | path => '{vmid}/config', | |
855 | method => 'PUT', | |
856 | protected => 1, | |
857 | proxyto => 'node', | |
858 | description => "Set virtual machine options.", | |
859 | permissions => { | |
860 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
861 | }, | |
862 | parameters => { | |
863 | additionalProperties => 0, | |
864 | properties => PVE::QemuServer::json_config_properties( | |
865 | { | |
866 | node => get_standard_option('pve-node'), | |
867 | vmid => get_standard_option('pve-vmid'), | |
868 | skiplock => get_standard_option('skiplock'), | |
869 | delete => { | |
870 | type => 'string', format => 'pve-configid-list', | |
871 | description => "A list of settings you want to delete.", | |
872 | optional => 1, | |
873 | }, | |
874 | force => { | |
875 | type => 'boolean', | |
876 | description => $opt_force_description, | |
877 | optional => 1, | |
878 | requires => 'delete', | |
879 | }, | |
880 | digest => { | |
881 | type => 'string', | |
882 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
883 | maxLength => 40, | |
884 | optional => 1, | |
885 | } | |
886 | }), | |
887 | }, | |
888 | returns => { type => 'null'}, | |
889 | code => sub { | |
890 | my ($param) = @_; | |
891 | ||
892 | my $rpcenv = PVE::RPCEnvironment::get(); | |
893 | ||
894 | my $authuser = $rpcenv->get_user(); | |
895 | ||
896 | my $node = extract_param($param, 'node'); | |
897 | ||
898 | my $vmid = extract_param($param, 'vmid'); | |
899 | ||
900 | my $digest = extract_param($param, 'digest'); | |
901 | ||
902 | my @paramarr = (); # used for log message | |
903 | foreach my $key (keys %$param) { | |
904 | push @paramarr, "-$key", $param->{$key}; | |
905 | } | |
906 | ||
907 | my $skiplock = extract_param($param, 'skiplock'); | |
908 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
909 | if $skiplock && $authuser ne 'root@pam'; | |
910 | ||
911 | my $delete_str = extract_param($param, 'delete'); | |
912 | ||
913 | my $force = extract_param($param, 'force'); | |
914 | ||
915 | die "no options specified\n" if !$delete_str && !scalar(keys %$param); | |
916 | ||
917 | my $storecfg = PVE::Storage::config(); | |
918 | ||
919 | my $defaults = PVE::QemuServer::load_defaults(); | |
920 | ||
921 | &$resolve_cdrom_alias($param); | |
922 | ||
923 | # now try to verify all parameters | |
924 | ||
925 | my @delete = (); | |
926 | foreach my $opt (PVE::Tools::split_list($delete_str)) { | |
927 | $opt = 'ide2' if $opt eq 'cdrom'; | |
928 | raise_param_exc({ delete => "you can't use '-$opt' and " . | |
929 | "-delete $opt' at the same time" }) | |
930 | if defined($param->{$opt}); | |
931 | ||
932 | if (!PVE::QemuServer::option_exists($opt)) { | |
933 | raise_param_exc({ delete => "unknown option '$opt'" }); | |
934 | } | |
935 | ||
936 | push @delete, $opt; | |
937 | } | |
938 | ||
939 | foreach my $opt (keys %$param) { | |
940 | if (PVE::QemuServer::valid_drivename($opt)) { | |
941 | # cleanup drive path | |
942 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
943 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); | |
944 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
945 | } elsif ($opt =~ m/^net(\d+)$/) { | |
946 | # add macaddr | |
947 | my $net = PVE::QemuServer::parse_net($param->{$opt}); | |
948 | $param->{$opt} = PVE::QemuServer::print_net($net); | |
949 | } | |
950 | } | |
951 | ||
952 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]); | |
953 | ||
954 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]); | |
955 | ||
956 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param); | |
957 | ||
958 | my $updatefn = sub { | |
959 | ||
960 | my $conf = PVE::QemuServer::load_config($vmid); | |
961 | ||
962 | die "checksum missmatch (file change by other user?)\n" | |
963 | if $digest && $digest ne $conf->{digest}; | |
964 | ||
965 | PVE::QemuServer::check_lock($conf) if !$skiplock; | |
966 | ||
967 | if ($param->{memory} || defined($param->{balloon})) { | |
968 | my $maxmem = $param->{memory} || $conf->{memory} || $defaults->{memory}; | |
969 | my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{balloon}; | |
970 | ||
971 | die "balloon value too large (must be smaller than assigned memory)\n" | |
972 | if $balloon && $balloon > $maxmem; | |
973 | } | |
974 | ||
975 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr)); | |
976 | ||
977 | foreach my $opt (@delete) { # delete | |
978 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
979 | &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force); | |
980 | } | |
981 | ||
982 | my $running = PVE::QemuServer::check_running($vmid); | |
983 | ||
984 | foreach my $opt (keys %$param) { # add/change | |
985 | ||
986 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
987 | ||
988 | next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed | |
989 | ||
990 | if (PVE::QemuServer::valid_drivename($opt)) { | |
991 | ||
992 | &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid, | |
993 | $opt, $param->{$opt}, $force); | |
994 | ||
995 | } elsif ($opt =~ m/^net(\d+)$/) { #nics | |
996 | ||
997 | &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid, | |
998 | $opt, $param->{$opt}); | |
999 | ||
1000 | } else { | |
1001 | ||
1002 | if($opt eq 'tablet' && $param->{$opt} == 1){ | |
1003 | PVE::QemuServer::vm_deviceplug(undef, $conf, $vmid, $opt); | |
1004 | }elsif($opt eq 'tablet' && $param->{$opt} == 0){ | |
1005 | PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt); | |
1006 | } | |
1007 | ||
1008 | $conf->{$opt} = $param->{$opt}; | |
1009 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | # allow manual ballooning if shares is set to zero | |
1014 | if ($running && defined($param->{balloon}) && | |
1015 | defined($conf->{shares}) && ($conf->{shares} == 0)) { | |
1016 | my $balloon = $param->{'balloon'} || $conf->{memory} || $defaults->{memory}; | |
1017 | PVE::QemuServer::vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024); | |
1018 | } | |
1019 | ||
1020 | }; | |
1021 | ||
1022 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
1023 | ||
1024 | return undef; | |
1025 | }}); | |
1026 | ||
1027 | ||
1028 | __PACKAGE__->register_method({ | |
1029 | name => 'destroy_vm', | |
1030 | path => '{vmid}', | |
1031 | method => 'DELETE', | |
1032 | protected => 1, | |
1033 | proxyto => 'node', | |
1034 | description => "Destroy the vm (also delete all used/owned volumes).", | |
1035 | permissions => { | |
1036 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1037 | }, | |
1038 | parameters => { | |
1039 | additionalProperties => 0, | |
1040 | properties => { | |
1041 | node => get_standard_option('pve-node'), | |
1042 | vmid => get_standard_option('pve-vmid'), | |
1043 | skiplock => get_standard_option('skiplock'), | |
1044 | }, | |
1045 | }, | |
1046 | returns => { | |
1047 | type => 'string', | |
1048 | }, | |
1049 | code => sub { | |
1050 | my ($param) = @_; | |
1051 | ||
1052 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1053 | ||
1054 | my $authuser = $rpcenv->get_user(); | |
1055 | ||
1056 | my $vmid = $param->{vmid}; | |
1057 | ||
1058 | my $skiplock = $param->{skiplock}; | |
1059 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1060 | if $skiplock && $authuser ne 'root@pam'; | |
1061 | ||
1062 | # test if VM exists | |
1063 | my $conf = PVE::QemuServer::load_config($vmid); | |
1064 | ||
1065 | my $storecfg = PVE::Storage::config(); | |
1066 | ||
1067 | my $delVMfromPoolFn = sub { | |
1068 | my $usercfg = cfs_read_file("user.cfg"); | |
1069 | if (my $pool = $usercfg->{vms}->{$vmid}) { | |
1070 | if (my $data = $usercfg->{pools}->{$pool}) { | |
1071 | delete $data->{vms}->{$vmid}; | |
1072 | delete $usercfg->{vms}->{$vmid}; | |
1073 | cfs_write_file("user.cfg", $usercfg); | |
1074 | } | |
1075 | } | |
1076 | }; | |
1077 | ||
1078 | my $realcmd = sub { | |
1079 | my $upid = shift; | |
1080 | ||
1081 | syslog('info', "destroy VM $vmid: $upid\n"); | |
1082 | ||
1083 | PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); | |
1084 | ||
1085 | PVE::AccessControl::remove_vm_from_pool($vmid); | |
1086 | }; | |
1087 | ||
1088 | return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd); | |
1089 | }}); | |
1090 | ||
1091 | __PACKAGE__->register_method({ | |
1092 | name => 'unlink', | |
1093 | path => '{vmid}/unlink', | |
1094 | method => 'PUT', | |
1095 | protected => 1, | |
1096 | proxyto => 'node', | |
1097 | description => "Unlink/delete disk images.", | |
1098 | permissions => { | |
1099 | check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']], | |
1100 | }, | |
1101 | parameters => { | |
1102 | additionalProperties => 0, | |
1103 | properties => { | |
1104 | node => get_standard_option('pve-node'), | |
1105 | vmid => get_standard_option('pve-vmid'), | |
1106 | idlist => { | |
1107 | type => 'string', format => 'pve-configid-list', | |
1108 | description => "A list of disk IDs you want to delete.", | |
1109 | }, | |
1110 | force => { | |
1111 | type => 'boolean', | |
1112 | description => $opt_force_description, | |
1113 | optional => 1, | |
1114 | }, | |
1115 | }, | |
1116 | }, | |
1117 | returns => { type => 'null'}, | |
1118 | code => sub { | |
1119 | my ($param) = @_; | |
1120 | ||
1121 | $param->{delete} = extract_param($param, 'idlist'); | |
1122 | ||
1123 | __PACKAGE__->update_vm($param); | |
1124 | ||
1125 | return undef; | |
1126 | }}); | |
1127 | ||
1128 | my $sslcert; | |
1129 | ||
1130 | __PACKAGE__->register_method({ | |
1131 | name => 'vncproxy', | |
1132 | path => '{vmid}/vncproxy', | |
1133 | method => 'POST', | |
1134 | protected => 1, | |
1135 | permissions => { | |
1136 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1137 | }, | |
1138 | description => "Creates a TCP VNC proxy connections.", | |
1139 | parameters => { | |
1140 | additionalProperties => 0, | |
1141 | properties => { | |
1142 | node => get_standard_option('pve-node'), | |
1143 | vmid => get_standard_option('pve-vmid'), | |
1144 | }, | |
1145 | }, | |
1146 | returns => { | |
1147 | additionalProperties => 0, | |
1148 | properties => { | |
1149 | user => { type => 'string' }, | |
1150 | ticket => { type => 'string' }, | |
1151 | cert => { type => 'string' }, | |
1152 | port => { type => 'integer' }, | |
1153 | upid => { type => 'string' }, | |
1154 | }, | |
1155 | }, | |
1156 | code => sub { | |
1157 | my ($param) = @_; | |
1158 | ||
1159 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1160 | ||
1161 | my $authuser = $rpcenv->get_user(); | |
1162 | ||
1163 | my $vmid = $param->{vmid}; | |
1164 | my $node = $param->{node}; | |
1165 | ||
1166 | my $authpath = "/vms/$vmid"; | |
1167 | ||
1168 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
1169 | ||
1170 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) | |
1171 | if !$sslcert; | |
1172 | ||
1173 | my $port = PVE::Tools::next_vnc_port(); | |
1174 | ||
1175 | my $remip; | |
1176 | ||
1177 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
1178 | $remip = PVE::Cluster::remote_node_ip($node); | |
1179 | } | |
1180 | ||
1181 | # NOTE: kvm VNC traffic is already TLS encrypted | |
1182 | my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', $remip] : []; | |
1183 | ||
1184 | my $timeout = 10; | |
1185 | ||
1186 | my $realcmd = sub { | |
1187 | my $upid = shift; | |
1188 | ||
1189 | syslog('info', "starting vnc proxy $upid\n"); | |
1190 | ||
1191 | my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid]; | |
1192 | ||
1193 | my $qmstr = join(' ', @$qmcmd); | |
1194 | ||
1195 | # also redirect stderr (else we get RFB protocol errors) | |
1196 | my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"]; | |
1197 | ||
1198 | PVE::Tools::run_command($cmd); | |
1199 | ||
1200 | return; | |
1201 | }; | |
1202 | ||
1203 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); | |
1204 | ||
1205 | PVE::Tools::wait_for_vnc_port($port); | |
1206 | ||
1207 | return { | |
1208 | user => $authuser, | |
1209 | ticket => $ticket, | |
1210 | port => $port, | |
1211 | upid => $upid, | |
1212 | cert => $sslcert, | |
1213 | }; | |
1214 | }}); | |
1215 | ||
1216 | __PACKAGE__->register_method({ | |
1217 | name => 'vmcmdidx', | |
1218 | path => '{vmid}/status', | |
1219 | method => 'GET', | |
1220 | proxyto => 'node', | |
1221 | description => "Directory index", | |
1222 | permissions => { | |
1223 | user => 'all', | |
1224 | }, | |
1225 | parameters => { | |
1226 | additionalProperties => 0, | |
1227 | properties => { | |
1228 | node => get_standard_option('pve-node'), | |
1229 | vmid => get_standard_option('pve-vmid'), | |
1230 | }, | |
1231 | }, | |
1232 | returns => { | |
1233 | type => 'array', | |
1234 | items => { | |
1235 | type => "object", | |
1236 | properties => { | |
1237 | subdir => { type => 'string' }, | |
1238 | }, | |
1239 | }, | |
1240 | links => [ { rel => 'child', href => "{subdir}" } ], | |
1241 | }, | |
1242 | code => sub { | |
1243 | my ($param) = @_; | |
1244 | ||
1245 | # test if VM exists | |
1246 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
1247 | ||
1248 | my $res = [ | |
1249 | { subdir => 'current' }, | |
1250 | { subdir => 'start' }, | |
1251 | { subdir => 'stop' }, | |
1252 | ]; | |
1253 | ||
1254 | return $res; | |
1255 | }}); | |
1256 | ||
1257 | my $vm_is_ha_managed = sub { | |
1258 | my ($vmid) = @_; | |
1259 | ||
1260 | my $cc = PVE::Cluster::cfs_read_file('cluster.conf'); | |
1261 | if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $vmid, 1)) { | |
1262 | return 1; | |
1263 | } | |
1264 | return 0; | |
1265 | }; | |
1266 | ||
1267 | __PACKAGE__->register_method({ | |
1268 | name => 'vm_status', | |
1269 | path => '{vmid}/status/current', | |
1270 | method => 'GET', | |
1271 | proxyto => 'node', | |
1272 | protected => 1, # qemu pid files are only readable by root | |
1273 | description => "Get virtual machine status.", | |
1274 | permissions => { | |
1275 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1276 | }, | |
1277 | parameters => { | |
1278 | additionalProperties => 0, | |
1279 | properties => { | |
1280 | node => get_standard_option('pve-node'), | |
1281 | vmid => get_standard_option('pve-vmid'), | |
1282 | }, | |
1283 | }, | |
1284 | returns => { type => 'object' }, | |
1285 | code => sub { | |
1286 | my ($param) = @_; | |
1287 | ||
1288 | # test if VM exists | |
1289 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
1290 | ||
1291 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); | |
1292 | my $status = $vmstatus->{$param->{vmid}}; | |
1293 | ||
1294 | $status->{ha} = &$vm_is_ha_managed($param->{vmid}); | |
1295 | ||
1296 | return $status; | |
1297 | }}); | |
1298 | ||
1299 | __PACKAGE__->register_method({ | |
1300 | name => 'vm_start', | |
1301 | path => '{vmid}/status/start', | |
1302 | method => 'POST', | |
1303 | protected => 1, | |
1304 | proxyto => 'node', | |
1305 | description => "Start virtual machine.", | |
1306 | permissions => { | |
1307 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1308 | }, | |
1309 | parameters => { | |
1310 | additionalProperties => 0, | |
1311 | properties => { | |
1312 | node => get_standard_option('pve-node'), | |
1313 | vmid => get_standard_option('pve-vmid'), | |
1314 | skiplock => get_standard_option('skiplock'), | |
1315 | stateuri => get_standard_option('pve-qm-stateuri'), | |
1316 | migratedfrom => get_standard_option('pve-node',{ optional => 1 }), | |
1317 | ||
1318 | }, | |
1319 | }, | |
1320 | returns => { | |
1321 | type => 'string', | |
1322 | }, | |
1323 | code => sub { | |
1324 | my ($param) = @_; | |
1325 | ||
1326 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1327 | ||
1328 | my $authuser = $rpcenv->get_user(); | |
1329 | ||
1330 | my $node = extract_param($param, 'node'); | |
1331 | ||
1332 | my $vmid = extract_param($param, 'vmid'); | |
1333 | ||
1334 | my $stateuri = extract_param($param, 'stateuri'); | |
1335 | raise_param_exc({ stateuri => "Only root may use this option." }) | |
1336 | if $stateuri && $authuser ne 'root@pam'; | |
1337 | ||
1338 | my $skiplock = extract_param($param, 'skiplock'); | |
1339 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1340 | if $skiplock && $authuser ne 'root@pam'; | |
1341 | ||
1342 | my $migratedfrom = extract_param($param, 'migratedfrom'); | |
1343 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
1344 | if $migratedfrom && $authuser ne 'root@pam'; | |
1345 | ||
1346 | my $storecfg = PVE::Storage::config(); | |
1347 | ||
1348 | if (&$vm_is_ha_managed($vmid) && !$stateuri && | |
1349 | $rpcenv->{type} ne 'ha') { | |
1350 | ||
1351 | my $hacmd = sub { | |
1352 | my $upid = shift; | |
1353 | ||
1354 | my $service = "pvevm:$vmid"; | |
1355 | ||
1356 | my $cmd = ['clusvcadm', '-e', $service, '-m', $node]; | |
1357 | ||
1358 | print "Executing HA start for VM $vmid\n"; | |
1359 | ||
1360 | PVE::Tools::run_command($cmd); | |
1361 | ||
1362 | return; | |
1363 | }; | |
1364 | ||
1365 | return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd); | |
1366 | ||
1367 | } else { | |
1368 | ||
1369 | my $realcmd = sub { | |
1370 | my $upid = shift; | |
1371 | ||
1372 | syslog('info', "start VM $vmid: $upid\n"); | |
1373 | ||
1374 | PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom); | |
1375 | ||
1376 | return; | |
1377 | }; | |
1378 | ||
1379 | return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd); | |
1380 | } | |
1381 | }}); | |
1382 | ||
1383 | __PACKAGE__->register_method({ | |
1384 | name => 'vm_stop', | |
1385 | path => '{vmid}/status/stop', | |
1386 | method => 'POST', | |
1387 | protected => 1, | |
1388 | proxyto => 'node', | |
1389 | description => "Stop virtual machine.", | |
1390 | permissions => { | |
1391 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1392 | }, | |
1393 | parameters => { | |
1394 | additionalProperties => 0, | |
1395 | properties => { | |
1396 | node => get_standard_option('pve-node'), | |
1397 | vmid => get_standard_option('pve-vmid'), | |
1398 | skiplock => get_standard_option('skiplock'), | |
1399 | migratedfrom => get_standard_option('pve-node',{ optional => 1 }), | |
1400 | timeout => { | |
1401 | description => "Wait maximal timeout seconds.", | |
1402 | type => 'integer', | |
1403 | minimum => 0, | |
1404 | optional => 1, | |
1405 | }, | |
1406 | keepActive => { | |
1407 | description => "Do not decativate storage volumes.", | |
1408 | type => 'boolean', | |
1409 | optional => 1, | |
1410 | default => 0, | |
1411 | } | |
1412 | }, | |
1413 | }, | |
1414 | returns => { | |
1415 | type => 'string', | |
1416 | }, | |
1417 | code => sub { | |
1418 | my ($param) = @_; | |
1419 | ||
1420 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1421 | ||
1422 | my $authuser = $rpcenv->get_user(); | |
1423 | ||
1424 | my $node = extract_param($param, 'node'); | |
1425 | ||
1426 | my $vmid = extract_param($param, 'vmid'); | |
1427 | ||
1428 | my $skiplock = extract_param($param, 'skiplock'); | |
1429 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1430 | if $skiplock && $authuser ne 'root@pam'; | |
1431 | ||
1432 | my $keepActive = extract_param($param, 'keepActive'); | |
1433 | raise_param_exc({ keepActive => "Only root may use this option." }) | |
1434 | if $keepActive && $authuser ne 'root@pam'; | |
1435 | ||
1436 | my $migratedfrom = extract_param($param, 'migratedfrom'); | |
1437 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
1438 | if $migratedfrom && $authuser ne 'root@pam'; | |
1439 | ||
1440 | ||
1441 | my $storecfg = PVE::Storage::config(); | |
1442 | ||
1443 | if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { | |
1444 | ||
1445 | my $hacmd = sub { | |
1446 | my $upid = shift; | |
1447 | ||
1448 | my $service = "pvevm:$vmid"; | |
1449 | ||
1450 | my $cmd = ['clusvcadm', '-d', $service]; | |
1451 | ||
1452 | print "Executing HA stop for VM $vmid\n"; | |
1453 | ||
1454 | PVE::Tools::run_command($cmd); | |
1455 | ||
1456 | return; | |
1457 | }; | |
1458 | ||
1459 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
1460 | ||
1461 | } else { | |
1462 | my $realcmd = sub { | |
1463 | my $upid = shift; | |
1464 | ||
1465 | syslog('info', "stop VM $vmid: $upid\n"); | |
1466 | ||
1467 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, | |
1468 | $param->{timeout}, 0, 1, $keepActive, $migratedfrom); | |
1469 | ||
1470 | return; | |
1471 | }; | |
1472 | ||
1473 | return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd); | |
1474 | } | |
1475 | }}); | |
1476 | ||
1477 | __PACKAGE__->register_method({ | |
1478 | name => 'vm_reset', | |
1479 | path => '{vmid}/status/reset', | |
1480 | method => 'POST', | |
1481 | protected => 1, | |
1482 | proxyto => 'node', | |
1483 | description => "Reset virtual machine.", | |
1484 | permissions => { | |
1485 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1486 | }, | |
1487 | parameters => { | |
1488 | additionalProperties => 0, | |
1489 | properties => { | |
1490 | node => get_standard_option('pve-node'), | |
1491 | vmid => get_standard_option('pve-vmid'), | |
1492 | skiplock => get_standard_option('skiplock'), | |
1493 | }, | |
1494 | }, | |
1495 | returns => { | |
1496 | type => 'string', | |
1497 | }, | |
1498 | code => sub { | |
1499 | my ($param) = @_; | |
1500 | ||
1501 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1502 | ||
1503 | my $authuser = $rpcenv->get_user(); | |
1504 | ||
1505 | my $node = extract_param($param, 'node'); | |
1506 | ||
1507 | my $vmid = extract_param($param, 'vmid'); | |
1508 | ||
1509 | my $skiplock = extract_param($param, 'skiplock'); | |
1510 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1511 | if $skiplock && $authuser ne 'root@pam'; | |
1512 | ||
1513 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
1514 | ||
1515 | my $realcmd = sub { | |
1516 | my $upid = shift; | |
1517 | ||
1518 | PVE::QemuServer::vm_reset($vmid, $skiplock); | |
1519 | ||
1520 | return; | |
1521 | }; | |
1522 | ||
1523 | return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd); | |
1524 | }}); | |
1525 | ||
1526 | __PACKAGE__->register_method({ | |
1527 | name => 'vm_shutdown', | |
1528 | path => '{vmid}/status/shutdown', | |
1529 | method => 'POST', | |
1530 | protected => 1, | |
1531 | proxyto => 'node', | |
1532 | description => "Shutdown virtual machine.", | |
1533 | permissions => { | |
1534 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1535 | }, | |
1536 | parameters => { | |
1537 | additionalProperties => 0, | |
1538 | properties => { | |
1539 | node => get_standard_option('pve-node'), | |
1540 | vmid => get_standard_option('pve-vmid'), | |
1541 | skiplock => get_standard_option('skiplock'), | |
1542 | timeout => { | |
1543 | description => "Wait maximal timeout seconds.", | |
1544 | type => 'integer', | |
1545 | minimum => 0, | |
1546 | optional => 1, | |
1547 | }, | |
1548 | forceStop => { | |
1549 | description => "Make sure the VM stops.", | |
1550 | type => 'boolean', | |
1551 | optional => 1, | |
1552 | default => 0, | |
1553 | }, | |
1554 | keepActive => { | |
1555 | description => "Do not decativate storage volumes.", | |
1556 | type => 'boolean', | |
1557 | optional => 1, | |
1558 | default => 0, | |
1559 | } | |
1560 | }, | |
1561 | }, | |
1562 | returns => { | |
1563 | type => 'string', | |
1564 | }, | |
1565 | code => sub { | |
1566 | my ($param) = @_; | |
1567 | ||
1568 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1569 | ||
1570 | my $authuser = $rpcenv->get_user(); | |
1571 | ||
1572 | my $node = extract_param($param, 'node'); | |
1573 | ||
1574 | my $vmid = extract_param($param, 'vmid'); | |
1575 | ||
1576 | my $skiplock = extract_param($param, 'skiplock'); | |
1577 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1578 | if $skiplock && $authuser ne 'root@pam'; | |
1579 | ||
1580 | my $keepActive = extract_param($param, 'keepActive'); | |
1581 | raise_param_exc({ keepActive => "Only root may use this option." }) | |
1582 | if $keepActive && $authuser ne 'root@pam'; | |
1583 | ||
1584 | my $storecfg = PVE::Storage::config(); | |
1585 | ||
1586 | my $realcmd = sub { | |
1587 | my $upid = shift; | |
1588 | ||
1589 | syslog('info', "shutdown VM $vmid: $upid\n"); | |
1590 | ||
1591 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout}, | |
1592 | 1, $param->{forceStop}, $keepActive); | |
1593 | ||
1594 | return; | |
1595 | }; | |
1596 | ||
1597 | return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd); | |
1598 | }}); | |
1599 | ||
1600 | __PACKAGE__->register_method({ | |
1601 | name => 'vm_suspend', | |
1602 | path => '{vmid}/status/suspend', | |
1603 | method => 'POST', | |
1604 | protected => 1, | |
1605 | proxyto => 'node', | |
1606 | description => "Suspend virtual machine.", | |
1607 | permissions => { | |
1608 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1609 | }, | |
1610 | parameters => { | |
1611 | additionalProperties => 0, | |
1612 | properties => { | |
1613 | node => get_standard_option('pve-node'), | |
1614 | vmid => get_standard_option('pve-vmid'), | |
1615 | skiplock => get_standard_option('skiplock'), | |
1616 | }, | |
1617 | }, | |
1618 | returns => { | |
1619 | type => 'string', | |
1620 | }, | |
1621 | code => sub { | |
1622 | my ($param) = @_; | |
1623 | ||
1624 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1625 | ||
1626 | my $authuser = $rpcenv->get_user(); | |
1627 | ||
1628 | my $node = extract_param($param, 'node'); | |
1629 | ||
1630 | my $vmid = extract_param($param, 'vmid'); | |
1631 | ||
1632 | my $skiplock = extract_param($param, 'skiplock'); | |
1633 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1634 | if $skiplock && $authuser ne 'root@pam'; | |
1635 | ||
1636 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
1637 | ||
1638 | my $realcmd = sub { | |
1639 | my $upid = shift; | |
1640 | ||
1641 | syslog('info', "suspend VM $vmid: $upid\n"); | |
1642 | ||
1643 | PVE::QemuServer::vm_suspend($vmid, $skiplock); | |
1644 | ||
1645 | return; | |
1646 | }; | |
1647 | ||
1648 | return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd); | |
1649 | }}); | |
1650 | ||
1651 | __PACKAGE__->register_method({ | |
1652 | name => 'vm_resume', | |
1653 | path => '{vmid}/status/resume', | |
1654 | method => 'POST', | |
1655 | protected => 1, | |
1656 | proxyto => 'node', | |
1657 | description => "Resume virtual machine.", | |
1658 | permissions => { | |
1659 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1660 | }, | |
1661 | parameters => { | |
1662 | additionalProperties => 0, | |
1663 | properties => { | |
1664 | node => get_standard_option('pve-node'), | |
1665 | vmid => get_standard_option('pve-vmid'), | |
1666 | skiplock => get_standard_option('skiplock'), | |
1667 | }, | |
1668 | }, | |
1669 | returns => { | |
1670 | type => 'string', | |
1671 | }, | |
1672 | code => sub { | |
1673 | my ($param) = @_; | |
1674 | ||
1675 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1676 | ||
1677 | my $authuser = $rpcenv->get_user(); | |
1678 | ||
1679 | my $node = extract_param($param, 'node'); | |
1680 | ||
1681 | my $vmid = extract_param($param, 'vmid'); | |
1682 | ||
1683 | my $skiplock = extract_param($param, 'skiplock'); | |
1684 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1685 | if $skiplock && $authuser ne 'root@pam'; | |
1686 | ||
1687 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
1688 | ||
1689 | my $realcmd = sub { | |
1690 | my $upid = shift; | |
1691 | ||
1692 | syslog('info', "resume VM $vmid: $upid\n"); | |
1693 | ||
1694 | PVE::QemuServer::vm_resume($vmid, $skiplock); | |
1695 | ||
1696 | return; | |
1697 | }; | |
1698 | ||
1699 | return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd); | |
1700 | }}); | |
1701 | ||
1702 | __PACKAGE__->register_method({ | |
1703 | name => 'vm_sendkey', | |
1704 | path => '{vmid}/sendkey', | |
1705 | method => 'PUT', | |
1706 | protected => 1, | |
1707 | proxyto => 'node', | |
1708 | description => "Send key event to virtual machine.", | |
1709 | permissions => { | |
1710 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1711 | }, | |
1712 | parameters => { | |
1713 | additionalProperties => 0, | |
1714 | properties => { | |
1715 | node => get_standard_option('pve-node'), | |
1716 | vmid => get_standard_option('pve-vmid'), | |
1717 | skiplock => get_standard_option('skiplock'), | |
1718 | key => { | |
1719 | description => "The key (qemu monitor encoding).", | |
1720 | type => 'string' | |
1721 | } | |
1722 | }, | |
1723 | }, | |
1724 | returns => { type => 'null'}, | |
1725 | code => sub { | |
1726 | my ($param) = @_; | |
1727 | ||
1728 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1729 | ||
1730 | my $authuser = $rpcenv->get_user(); | |
1731 | ||
1732 | my $node = extract_param($param, 'node'); | |
1733 | ||
1734 | my $vmid = extract_param($param, 'vmid'); | |
1735 | ||
1736 | my $skiplock = extract_param($param, 'skiplock'); | |
1737 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1738 | if $skiplock && $authuser ne 'root@pam'; | |
1739 | ||
1740 | PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); | |
1741 | ||
1742 | return; | |
1743 | }}); | |
1744 | ||
1745 | __PACKAGE__->register_method({ | |
1746 | name => 'vm_feature', | |
1747 | path => '{vmid}/feature', | |
1748 | method => 'GET', | |
1749 | proxyto => 'node', | |
1750 | protected => 1, | |
1751 | description => "Check if feature for virtual machine is available.", | |
1752 | permissions => { | |
1753 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1754 | }, | |
1755 | parameters => { | |
1756 | additionalProperties => 0, | |
1757 | properties => { | |
1758 | node => get_standard_option('pve-node'), | |
1759 | vmid => get_standard_option('pve-vmid'), | |
1760 | feature => { | |
1761 | description => "Feature to check.", | |
1762 | type => 'string', | |
1763 | enum => [ 'snapshot', 'clone', 'copy' ], | |
1764 | }, | |
1765 | snapname => get_standard_option('pve-snapshot-name', { | |
1766 | optional => 1, | |
1767 | }), | |
1768 | }, | |
1769 | }, | |
1770 | returns => { | |
1771 | type => "object", | |
1772 | properties => { | |
1773 | hasFeature => { type => 'boolean' }, | |
1774 | nodes => { | |
1775 | type => 'array', | |
1776 | items => { type => 'string' }, | |
1777 | } | |
1778 | }, | |
1779 | }, | |
1780 | code => sub { | |
1781 | my ($param) = @_; | |
1782 | ||
1783 | my $node = extract_param($param, 'node'); | |
1784 | ||
1785 | my $vmid = extract_param($param, 'vmid'); | |
1786 | ||
1787 | my $snapname = extract_param($param, 'snapname'); | |
1788 | ||
1789 | my $feature = extract_param($param, 'feature'); | |
1790 | ||
1791 | my $running = PVE::QemuServer::check_running($vmid); | |
1792 | ||
1793 | my $conf = PVE::QemuServer::load_config($vmid); | |
1794 | ||
1795 | if($snapname){ | |
1796 | my $snap = $conf->{snapshots}->{$snapname}; | |
1797 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
1798 | $conf = $snap; | |
1799 | } | |
1800 | my $storecfg = PVE::Storage::config(); | |
1801 | ||
1802 | my $nodelist = PVE::QemuServer::shared_nodes($conf, $storecfg); | |
1803 | my $hasFeature = PVE::QemuServer::has_feature($feature, $conf, $storecfg, $snapname, $running); | |
1804 | ||
1805 | return { | |
1806 | hasFeature => $hasFeature, | |
1807 | nodes => [ keys %$nodelist ], | |
1808 | }; | |
1809 | }}); | |
1810 | ||
1811 | __PACKAGE__->register_method({ | |
1812 | name => 'clone_vm', | |
1813 | path => '{vmid}/clone', | |
1814 | method => 'POST', | |
1815 | protected => 1, | |
1816 | proxyto => 'node', | |
1817 | description => "Create a copy of virtual machine/template.", | |
1818 | permissions => { | |
1819 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " . | |
1820 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . | |
1821 | "'Datastore.AllocateSpace' on any used storage.", | |
1822 | check => | |
1823 | [ 'and', | |
1824 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], | |
1825 | [ 'or', | |
1826 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], | |
1827 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
1828 | ], | |
1829 | ] | |
1830 | }, | |
1831 | parameters => { | |
1832 | additionalProperties => 0, | |
1833 | properties => { | |
1834 | node => get_standard_option('pve-node'), | |
1835 | vmid => get_standard_option('pve-vmid'), | |
1836 | newid => get_standard_option('pve-vmid', { description => 'VMID for the clone.' }), | |
1837 | name => { | |
1838 | optional => 1, | |
1839 | type => 'string', format => 'dns-name', | |
1840 | description => "Set a name for the new VM.", | |
1841 | }, | |
1842 | description => { | |
1843 | optional => 1, | |
1844 | type => 'string', | |
1845 | description => "Description for the new VM.", | |
1846 | }, | |
1847 | pool => { | |
1848 | optional => 1, | |
1849 | type => 'string', format => 'pve-poolid', | |
1850 | description => "Add the new VM to the specified pool.", | |
1851 | }, | |
1852 | snapname => get_standard_option('pve-snapshot-name', { | |
1853 | requires => 'full', | |
1854 | optional => 1, | |
1855 | }), | |
1856 | storage => get_standard_option('pve-storage-id', { | |
1857 | description => "Target storage for full clone.", | |
1858 | requires => 'full', | |
1859 | optional => 1, | |
1860 | }), | |
1861 | 'format' => { | |
1862 | description => "Target format for file storage.", | |
1863 | requires => 'full', | |
1864 | type => 'string', | |
1865 | optional => 1, | |
1866 | enum => [ 'raw', 'qcow2', 'vmdk'], | |
1867 | }, | |
1868 | full => { | |
1869 | optional => 1, | |
1870 | type => 'boolean', | |
1871 | description => "Create a full copy of all disk. This is always done when " . | |
1872 | "you clone a normal VM. For VM templates, we try to create a linked clone by default.", | |
1873 | default => 0, | |
1874 | }, | |
1875 | target => get_standard_option('pve-node', { | |
1876 | description => "Target node. Only allowed if the original VM is on shared storage.", | |
1877 | optional => 1, | |
1878 | }), | |
1879 | }, | |
1880 | }, | |
1881 | returns => { | |
1882 | type => 'string', | |
1883 | }, | |
1884 | code => sub { | |
1885 | my ($param) = @_; | |
1886 | ||
1887 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1888 | ||
1889 | my $authuser = $rpcenv->get_user(); | |
1890 | ||
1891 | my $node = extract_param($param, 'node'); | |
1892 | ||
1893 | my $vmid = extract_param($param, 'vmid'); | |
1894 | ||
1895 | my $newid = extract_param($param, 'newid'); | |
1896 | ||
1897 | my $pool = extract_param($param, 'pool'); | |
1898 | ||
1899 | if (defined($pool)) { | |
1900 | $rpcenv->check_pool_exist($pool); | |
1901 | } | |
1902 | ||
1903 | my $snapname = extract_param($param, 'snapname'); | |
1904 | ||
1905 | my $storage = extract_param($param, 'storage'); | |
1906 | ||
1907 | my $format = extract_param($param, 'format'); | |
1908 | ||
1909 | my $target = extract_param($param, 'target'); | |
1910 | ||
1911 | my $localnode = PVE::INotify::nodename(); | |
1912 | ||
1913 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); | |
1914 | ||
1915 | PVE::Cluster::check_node_exists($target) if $target; | |
1916 | ||
1917 | my $storecfg = PVE::Storage::config(); | |
1918 | ||
1919 | if ($storage) { | |
1920 | # check if storage is enabled on local node | |
1921 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
1922 | if ($target) { | |
1923 | # check if storage is available on target node | |
1924 | PVE::Storage::storage_check_node($storecfg, $storage, $target); | |
1925 | # clone only works if target storage is shared | |
1926 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
1927 | die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared}; | |
1928 | } | |
1929 | } | |
1930 | ||
1931 | PVE::Cluster::check_cfs_quorum(); | |
1932 | ||
1933 | my $running = PVE::QemuServer::check_running($vmid) || 0; | |
1934 | ||
1935 | # exclusive lock if VM is running - else shared lock is enough; | |
1936 | my $shared_lock = $running ? 0 : 1; | |
1937 | ||
1938 | my $clonefn = sub { | |
1939 | ||
1940 | # do all tests after lock | |
1941 | # we also try to do all tests before we fork the worker | |
1942 | ||
1943 | my $conf = PVE::QemuServer::load_config($vmid); | |
1944 | ||
1945 | PVE::QemuServer::check_lock($conf); | |
1946 | ||
1947 | my $verify_running = PVE::QemuServer::check_running($vmid) || 0; | |
1948 | ||
1949 | die "unexpected state change\n" if $verify_running != $running; | |
1950 | ||
1951 | die "snapshot '$snapname' does not exist\n" | |
1952 | if $snapname && !defined( $conf->{snapshots}->{$snapname}); | |
1953 | ||
1954 | my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf; | |
1955 | ||
1956 | my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage); | |
1957 | ||
1958 | die "can't clone VM to node '$target' (VM uses local storage)\n" if $target && !$sharedvm; | |
1959 | ||
1960 | my $conffile = PVE::QemuServer::config_file($newid); | |
1961 | ||
1962 | die "unable to create VM $newid: config file already exists\n" | |
1963 | if -f $conffile; | |
1964 | ||
1965 | my $newconf = { lock => 'clone' }; | |
1966 | my $drives = {}; | |
1967 | my $vollist = []; | |
1968 | ||
1969 | foreach my $opt (keys %$oldconf) { | |
1970 | my $value = $oldconf->{$opt}; | |
1971 | ||
1972 | # do not copy snapshot related info | |
1973 | next if $opt eq 'snapshots' || $opt eq 'parent' || $opt eq 'snaptime' || | |
1974 | $opt eq 'vmstate' || $opt eq 'snapstate'; | |
1975 | ||
1976 | # always change MAC! address | |
1977 | if ($opt =~ m/^net(\d+)$/) { | |
1978 | my $net = PVE::QemuServer::parse_net($value); | |
1979 | $net->{macaddr} = PVE::Tools::random_ether_addr(); | |
1980 | $newconf->{$opt} = PVE::QemuServer::print_net($net); | |
1981 | } elsif (my $drive = PVE::QemuServer::parse_drive($opt, $value)) { | |
1982 | if (PVE::QemuServer::drive_is_cdrom($drive)) { | |
1983 | $newconf->{$opt} = $value; # simply copy configuration | |
1984 | } else { | |
1985 | if ($param->{full} || !PVE::Storage::volume_is_base($storecfg, $drive->{file})) { | |
1986 | die "Full clone feature is not available" | |
1987 | if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running); | |
1988 | $drive->{full} = 1; | |
1989 | } | |
1990 | $drives->{$opt} = $drive; | |
1991 | push @$vollist, $drive->{file}; | |
1992 | } | |
1993 | } else { | |
1994 | # copy everything else | |
1995 | $newconf->{$opt} = $value; | |
1996 | } | |
1997 | } | |
1998 | ||
1999 | delete $newconf->{template}; | |
2000 | ||
2001 | if ($param->{name}) { | |
2002 | $newconf->{name} = $param->{name}; | |
2003 | } else { | |
2004 | if ($oldconf->{name}) { | |
2005 | $newconf->{name} = "Copy-of-$oldconf->{name}"; | |
2006 | } else { | |
2007 | $newconf->{name} = "Copy-of-VM-$vmid"; | |
2008 | } | |
2009 | } | |
2010 | ||
2011 | if ($param->{description}) { | |
2012 | $newconf->{description} = $param->{description}; | |
2013 | } | |
2014 | ||
2015 | # create empty/temp config - this fails if VM already exists on other node | |
2016 | PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n"); | |
2017 | ||
2018 | my $realcmd = sub { | |
2019 | my $upid = shift; | |
2020 | ||
2021 | my $newvollist = []; | |
2022 | ||
2023 | eval { | |
2024 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
2025 | ||
2026 | PVE::Storage::activate_volumes($storecfg, $vollist); | |
2027 | ||
2028 | foreach my $opt (keys %$drives) { | |
2029 | my $drive = $drives->{$opt}; | |
2030 | ||
2031 | my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $opt, $drive, $snapname, | |
2032 | $newid, $storage, $format, $drive->{full}, $newvollist); | |
2033 | ||
2034 | $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive); | |
2035 | ||
2036 | PVE::QemuServer::update_config_nolock($newid, $newconf, 1); | |
2037 | } | |
2038 | ||
2039 | delete $newconf->{lock}; | |
2040 | PVE::QemuServer::update_config_nolock($newid, $newconf, 1); | |
2041 | ||
2042 | if ($target) { | |
2043 | my $newconffile = PVE::QemuServer::config_file($newid, $target); | |
2044 | die "Failed to move config to node '$target' - rename failed: $!\n" | |
2045 | if !rename($conffile, $newconffile); | |
2046 | } | |
2047 | ||
2048 | PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool; | |
2049 | }; | |
2050 | if (my $err = $@) { | |
2051 | unlink $conffile; | |
2052 | ||
2053 | sleep 1; # some storage like rbd need to wait before release volume - really? | |
2054 | ||
2055 | foreach my $volid (@$newvollist) { | |
2056 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
2057 | warn $@ if $@; | |
2058 | } | |
2059 | die "clone failed: $err"; | |
2060 | } | |
2061 | ||
2062 | return; | |
2063 | }; | |
2064 | ||
2065 | return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $realcmd); | |
2066 | }; | |
2067 | ||
2068 | return PVE::QemuServer::lock_config_mode($vmid, 1, $shared_lock, sub { | |
2069 | # Aquire exclusive lock lock for $newid | |
2070 | return PVE::QemuServer::lock_config_full($newid, 1, $clonefn); | |
2071 | }); | |
2072 | ||
2073 | }}); | |
2074 | ||
2075 | __PACKAGE__->register_method({ | |
2076 | name => 'move_vm', | |
2077 | path => '{vmid}/move', | |
2078 | method => 'POST', | |
2079 | protected => 1, | |
2080 | proxyto => 'node', | |
2081 | description => "Move volume to different storage.", | |
2082 | permissions => { | |
2083 | description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " . | |
2084 | "and 'Datastore.AllocateSpace' permissions on the storage.", | |
2085 | check => | |
2086 | [ 'and', | |
2087 | ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
2088 | ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]], | |
2089 | ], | |
2090 | }, | |
2091 | parameters => { | |
2092 | additionalProperties => 0, | |
2093 | properties => { | |
2094 | node => get_standard_option('pve-node'), | |
2095 | vmid => get_standard_option('pve-vmid'), | |
2096 | disk => { | |
2097 | type => 'string', | |
2098 | description => "The disk you want to move.", | |
2099 | enum => [ PVE::QemuServer::disknames() ], | |
2100 | }, | |
2101 | storage => get_standard_option('pve-storage-id', { description => "Target Storage." }), | |
2102 | format => { | |
2103 | type => 'string', | |
2104 | description => "Target Format.", | |
2105 | enum => [ 'raw', 'qcow2', 'vmdk' ], | |
2106 | optional => 1, | |
2107 | }, | |
2108 | digest => { | |
2109 | type => 'string', | |
2110 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
2111 | maxLength => 40, | |
2112 | optional => 1, | |
2113 | }, | |
2114 | }, | |
2115 | }, | |
2116 | returns => { | |
2117 | type => 'string', | |
2118 | description => "the task ID.", | |
2119 | }, | |
2120 | code => sub { | |
2121 | my ($param) = @_; | |
2122 | ||
2123 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2124 | ||
2125 | my $authuser = $rpcenv->get_user(); | |
2126 | ||
2127 | my $node = extract_param($param, 'node'); | |
2128 | ||
2129 | my $vmid = extract_param($param, 'vmid'); | |
2130 | ||
2131 | my $digest = extract_param($param, 'digest'); | |
2132 | ||
2133 | my $disk = extract_param($param, 'disk'); | |
2134 | ||
2135 | my $storeid = extract_param($param, 'storage'); | |
2136 | ||
2137 | my $format = extract_param($param, 'format'); | |
2138 | ||
2139 | my $storecfg = PVE::Storage::config(); | |
2140 | ||
2141 | my $updatefn = sub { | |
2142 | ||
2143 | my $conf = PVE::QemuServer::load_config($vmid); | |
2144 | ||
2145 | die "checksum missmatch (file change by other user?)\n" | |
2146 | if $digest && $digest ne $conf->{digest}; | |
2147 | ||
2148 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; | |
2149 | ||
2150 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
2151 | ||
2152 | my $volid = $drive->{file} || die "disk '$disk' has no associated volume\n"; | |
2153 | ||
2154 | die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
2155 | ||
2156 | my $oldfmt; | |
2157 | my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($volid); | |
2158 | if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){ | |
2159 | $oldfmt = $1; | |
2160 | } | |
2161 | ||
2162 | die "you can't move on the same storage with same format\n" if $oldstoreid eq $storeid && | |
2163 | (!$format || !$oldfmt || $oldfmt eq $format); | |
2164 | ||
2165 | PVE::Cluster::log_msg('info', $authuser, "move disk VM $vmid: move --disk $disk --storage $storeid"); | |
2166 | ||
2167 | my $running = PVE::QemuServer::check_running($vmid); | |
2168 | ||
2169 | PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]); | |
2170 | ||
2171 | my $realcmd = sub { | |
2172 | ||
2173 | my $newvollist = []; | |
2174 | ||
2175 | eval { | |
2176 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
2177 | ||
2178 | my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $disk, $drive, undef, | |
2179 | $vmid, $storeid, $format, 1, $newvollist); | |
2180 | ||
2181 | $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $newdrive); | |
2182 | ||
2183 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
2184 | }; | |
2185 | if (my $err = $@) { | |
2186 | ||
2187 | foreach my $volid (@$newvollist) { | |
2188 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
2189 | warn $@ if $@; | |
2190 | } | |
2191 | die "storage migration failed: $err"; | |
2192 | } | |
2193 | }; | |
2194 | ||
2195 | return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd); | |
2196 | }; | |
2197 | ||
2198 | return PVE::QemuServer::lock_config($vmid, $updatefn); | |
2199 | }}); | |
2200 | ||
2201 | __PACKAGE__->register_method({ | |
2202 | name => 'migrate_vm', | |
2203 | path => '{vmid}/migrate', | |
2204 | method => 'POST', | |
2205 | protected => 1, | |
2206 | proxyto => 'node', | |
2207 | description => "Migrate virtual machine. Creates a new migration task.", | |
2208 | permissions => { | |
2209 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], | |
2210 | }, | |
2211 | parameters => { | |
2212 | additionalProperties => 0, | |
2213 | properties => { | |
2214 | node => get_standard_option('pve-node'), | |
2215 | vmid => get_standard_option('pve-vmid'), | |
2216 | target => get_standard_option('pve-node', { description => "Target node." }), | |
2217 | online => { | |
2218 | type => 'boolean', | |
2219 | description => "Use online/live migration.", | |
2220 | optional => 1, | |
2221 | }, | |
2222 | force => { | |
2223 | type => 'boolean', | |
2224 | description => "Allow to migrate VMs which use local devices. Only root may use this option.", | |
2225 | optional => 1, | |
2226 | }, | |
2227 | }, | |
2228 | }, | |
2229 | returns => { | |
2230 | type => 'string', | |
2231 | description => "the task ID.", | |
2232 | }, | |
2233 | code => sub { | |
2234 | my ($param) = @_; | |
2235 | ||
2236 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2237 | ||
2238 | my $authuser = $rpcenv->get_user(); | |
2239 | ||
2240 | my $target = extract_param($param, 'target'); | |
2241 | ||
2242 | my $localnode = PVE::INotify::nodename(); | |
2243 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
2244 | ||
2245 | PVE::Cluster::check_cfs_quorum(); | |
2246 | ||
2247 | PVE::Cluster::check_node_exists($target); | |
2248 | ||
2249 | my $targetip = PVE::Cluster::remote_node_ip($target); | |
2250 | ||
2251 | my $vmid = extract_param($param, 'vmid'); | |
2252 | ||
2253 | raise_param_exc({ force => "Only root may use this option." }) | |
2254 | if $param->{force} && $authuser ne 'root@pam'; | |
2255 | ||
2256 | # test if VM exists | |
2257 | my $conf = PVE::QemuServer::load_config($vmid); | |
2258 | ||
2259 | # try to detect errors early | |
2260 | ||
2261 | PVE::QemuServer::check_lock($conf); | |
2262 | ||
2263 | if (PVE::QemuServer::check_running($vmid)) { | |
2264 | die "cant migrate running VM without --online\n" | |
2265 | if !$param->{online}; | |
2266 | } | |
2267 | ||
2268 | my $storecfg = PVE::Storage::config(); | |
2269 | PVE::QemuServer::check_storage_availability($storecfg, $conf, $target); | |
2270 | ||
2271 | if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { | |
2272 | ||
2273 | my $hacmd = sub { | |
2274 | my $upid = shift; | |
2275 | ||
2276 | my $service = "pvevm:$vmid"; | |
2277 | ||
2278 | my $cmd = ['clusvcadm', '-M', $service, '-m', $target]; | |
2279 | ||
2280 | print "Executing HA migrate for VM $vmid to node $target\n"; | |
2281 | ||
2282 | PVE::Tools::run_command($cmd); | |
2283 | ||
2284 | return; | |
2285 | }; | |
2286 | ||
2287 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); | |
2288 | ||
2289 | } else { | |
2290 | ||
2291 | my $realcmd = sub { | |
2292 | my $upid = shift; | |
2293 | ||
2294 | PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param); | |
2295 | }; | |
2296 | ||
2297 | return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd); | |
2298 | } | |
2299 | ||
2300 | }}); | |
2301 | ||
2302 | __PACKAGE__->register_method({ | |
2303 | name => 'monitor', | |
2304 | path => '{vmid}/monitor', | |
2305 | method => 'POST', | |
2306 | protected => 1, | |
2307 | proxyto => 'node', | |
2308 | description => "Execute Qemu monitor commands.", | |
2309 | permissions => { | |
2310 | check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]], | |
2311 | }, | |
2312 | parameters => { | |
2313 | additionalProperties => 0, | |
2314 | properties => { | |
2315 | node => get_standard_option('pve-node'), | |
2316 | vmid => get_standard_option('pve-vmid'), | |
2317 | command => { | |
2318 | type => 'string', | |
2319 | description => "The monitor command.", | |
2320 | } | |
2321 | }, | |
2322 | }, | |
2323 | returns => { type => 'string'}, | |
2324 | code => sub { | |
2325 | my ($param) = @_; | |
2326 | ||
2327 | my $vmid = $param->{vmid}; | |
2328 | ||
2329 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
2330 | ||
2331 | my $res = ''; | |
2332 | eval { | |
2333 | $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command}); | |
2334 | }; | |
2335 | $res = "ERROR: $@" if $@; | |
2336 | ||
2337 | return $res; | |
2338 | }}); | |
2339 | ||
2340 | __PACKAGE__->register_method({ | |
2341 | name => 'resize_vm', | |
2342 | path => '{vmid}/resize', | |
2343 | method => 'PUT', | |
2344 | protected => 1, | |
2345 | proxyto => 'node', | |
2346 | description => "Extend volume size.", | |
2347 | permissions => { | |
2348 | check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
2349 | }, | |
2350 | parameters => { | |
2351 | additionalProperties => 0, | |
2352 | properties => { | |
2353 | node => get_standard_option('pve-node'), | |
2354 | vmid => get_standard_option('pve-vmid'), | |
2355 | skiplock => get_standard_option('skiplock'), | |
2356 | disk => { | |
2357 | type => 'string', | |
2358 | description => "The disk you want to resize.", | |
2359 | enum => [PVE::QemuServer::disknames()], | |
2360 | }, | |
2361 | size => { | |
2362 | type => 'string', | |
2363 | pattern => '\+?\d+(\.\d+)?[KMGT]?', | |
2364 | 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.", | |
2365 | }, | |
2366 | digest => { | |
2367 | type => 'string', | |
2368 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
2369 | maxLength => 40, | |
2370 | optional => 1, | |
2371 | }, | |
2372 | }, | |
2373 | }, | |
2374 | returns => { type => 'null'}, | |
2375 | code => sub { | |
2376 | my ($param) = @_; | |
2377 | ||
2378 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2379 | ||
2380 | my $authuser = $rpcenv->get_user(); | |
2381 | ||
2382 | my $node = extract_param($param, 'node'); | |
2383 | ||
2384 | my $vmid = extract_param($param, 'vmid'); | |
2385 | ||
2386 | my $digest = extract_param($param, 'digest'); | |
2387 | ||
2388 | my $disk = extract_param($param, 'disk'); | |
2389 | ||
2390 | my $sizestr = extract_param($param, 'size'); | |
2391 | ||
2392 | my $skiplock = extract_param($param, 'skiplock'); | |
2393 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
2394 | if $skiplock && $authuser ne 'root@pam'; | |
2395 | ||
2396 | my $storecfg = PVE::Storage::config(); | |
2397 | ||
2398 | my $updatefn = sub { | |
2399 | ||
2400 | my $conf = PVE::QemuServer::load_config($vmid); | |
2401 | ||
2402 | die "checksum missmatch (file change by other user?)\n" | |
2403 | if $digest && $digest ne $conf->{digest}; | |
2404 | PVE::QemuServer::check_lock($conf) if !$skiplock; | |
2405 | ||
2406 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; | |
2407 | ||
2408 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
2409 | ||
2410 | my $volid = $drive->{file}; | |
2411 | ||
2412 | die "disk '$disk' has no associated volume\n" if !$volid; | |
2413 | ||
2414 | die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
2415 | ||
2416 | die "you can't online resize a virtio windows bootdisk\n" | |
2417 | if PVE::QemuServer::check_running($vmid) && $conf->{bootdisk} eq $disk && $conf->{ostype} =~ m/^w/ && $disk =~ m/^virtio/; | |
2418 | ||
2419 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
2420 | ||
2421 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
2422 | ||
2423 | my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5); | |
2424 | ||
2425 | die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/; | |
2426 | my ($ext, $newsize, $unit) = ($1, $2, $4); | |
2427 | if ($unit) { | |
2428 | if ($unit eq 'K') { | |
2429 | $newsize = $newsize * 1024; | |
2430 | } elsif ($unit eq 'M') { | |
2431 | $newsize = $newsize * 1024 * 1024; | |
2432 | } elsif ($unit eq 'G') { | |
2433 | $newsize = $newsize * 1024 * 1024 * 1024; | |
2434 | } elsif ($unit eq 'T') { | |
2435 | $newsize = $newsize * 1024 * 1024 * 1024 * 1024; | |
2436 | } | |
2437 | } | |
2438 | $newsize += $size if $ext; | |
2439 | $newsize = int($newsize); | |
2440 | ||
2441 | die "unable to skrink disk size\n" if $newsize < $size; | |
2442 | ||
2443 | return if $size == $newsize; | |
2444 | ||
2445 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr"); | |
2446 | ||
2447 | PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize); | |
2448 | ||
2449 | $drive->{size} = $newsize; | |
2450 | $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive); | |
2451 | ||
2452 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
2453 | }; | |
2454 | ||
2455 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
2456 | return undef; | |
2457 | }}); | |
2458 | ||
2459 | __PACKAGE__->register_method({ | |
2460 | name => 'snapshot_list', | |
2461 | path => '{vmid}/snapshot', | |
2462 | method => 'GET', | |
2463 | description => "List all snapshots.", | |
2464 | permissions => { | |
2465 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
2466 | }, | |
2467 | proxyto => 'node', | |
2468 | protected => 1, # qemu pid files are only readable by root | |
2469 | parameters => { | |
2470 | additionalProperties => 0, | |
2471 | properties => { | |
2472 | vmid => get_standard_option('pve-vmid'), | |
2473 | node => get_standard_option('pve-node'), | |
2474 | }, | |
2475 | }, | |
2476 | returns => { | |
2477 | type => 'array', | |
2478 | items => { | |
2479 | type => "object", | |
2480 | properties => {}, | |
2481 | }, | |
2482 | links => [ { rel => 'child', href => "{name}" } ], | |
2483 | }, | |
2484 | code => sub { | |
2485 | my ($param) = @_; | |
2486 | ||
2487 | my $vmid = $param->{vmid}; | |
2488 | ||
2489 | my $conf = PVE::QemuServer::load_config($vmid); | |
2490 | my $snaphash = $conf->{snapshots} || {}; | |
2491 | ||
2492 | my $res = []; | |
2493 | ||
2494 | foreach my $name (keys %$snaphash) { | |
2495 | my $d = $snaphash->{$name}; | |
2496 | my $item = { | |
2497 | name => $name, | |
2498 | snaptime => $d->{snaptime} || 0, | |
2499 | vmstate => $d->{vmstate} ? 1 : 0, | |
2500 | description => $d->{description} || '', | |
2501 | }; | |
2502 | $item->{parent} = $d->{parent} if $d->{parent}; | |
2503 | $item->{snapstate} = $d->{snapstate} if $d->{snapstate}; | |
2504 | push @$res, $item; | |
2505 | } | |
2506 | ||
2507 | my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0; | |
2508 | my $current = { name => 'current', digest => $conf->{digest}, running => $running }; | |
2509 | $current->{parent} = $conf->{parent} if $conf->{parent}; | |
2510 | ||
2511 | push @$res, $current; | |
2512 | ||
2513 | return $res; | |
2514 | }}); | |
2515 | ||
2516 | __PACKAGE__->register_method({ | |
2517 | name => 'snapshot', | |
2518 | path => '{vmid}/snapshot', | |
2519 | method => 'POST', | |
2520 | protected => 1, | |
2521 | proxyto => 'node', | |
2522 | description => "Snapshot a VM.", | |
2523 | permissions => { | |
2524 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
2525 | }, | |
2526 | parameters => { | |
2527 | additionalProperties => 0, | |
2528 | properties => { | |
2529 | node => get_standard_option('pve-node'), | |
2530 | vmid => get_standard_option('pve-vmid'), | |
2531 | snapname => get_standard_option('pve-snapshot-name'), | |
2532 | vmstate => { | |
2533 | optional => 1, | |
2534 | type => 'boolean', | |
2535 | description => "Save the vmstate", | |
2536 | }, | |
2537 | freezefs => { | |
2538 | optional => 1, | |
2539 | type => 'boolean', | |
2540 | description => "Freeze the filesystem", | |
2541 | }, | |
2542 | description => { | |
2543 | optional => 1, | |
2544 | type => 'string', | |
2545 | description => "A textual description or comment.", | |
2546 | }, | |
2547 | }, | |
2548 | }, | |
2549 | returns => { | |
2550 | type => 'string', | |
2551 | description => "the task ID.", | |
2552 | }, | |
2553 | code => sub { | |
2554 | my ($param) = @_; | |
2555 | ||
2556 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2557 | ||
2558 | my $authuser = $rpcenv->get_user(); | |
2559 | ||
2560 | my $node = extract_param($param, 'node'); | |
2561 | ||
2562 | my $vmid = extract_param($param, 'vmid'); | |
2563 | ||
2564 | my $snapname = extract_param($param, 'snapname'); | |
2565 | ||
2566 | die "unable to use snapshot name 'current' (reserved name)\n" | |
2567 | if $snapname eq 'current'; | |
2568 | ||
2569 | my $realcmd = sub { | |
2570 | PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname"); | |
2571 | PVE::QemuServer::snapshot_create($vmid, $snapname, $param->{vmstate}, | |
2572 | $param->{freezefs}, $param->{description}); | |
2573 | }; | |
2574 | ||
2575 | return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd); | |
2576 | }}); | |
2577 | ||
2578 | __PACKAGE__->register_method({ | |
2579 | name => 'snapshot_cmd_idx', | |
2580 | path => '{vmid}/snapshot/{snapname}', | |
2581 | description => '', | |
2582 | method => 'GET', | |
2583 | permissions => { | |
2584 | user => 'all', | |
2585 | }, | |
2586 | parameters => { | |
2587 | additionalProperties => 0, | |
2588 | properties => { | |
2589 | vmid => get_standard_option('pve-vmid'), | |
2590 | node => get_standard_option('pve-node'), | |
2591 | snapname => get_standard_option('pve-snapshot-name'), | |
2592 | }, | |
2593 | }, | |
2594 | returns => { | |
2595 | type => 'array', | |
2596 | items => { | |
2597 | type => "object", | |
2598 | properties => {}, | |
2599 | }, | |
2600 | links => [ { rel => 'child', href => "{cmd}" } ], | |
2601 | }, | |
2602 | code => sub { | |
2603 | my ($param) = @_; | |
2604 | ||
2605 | my $res = []; | |
2606 | ||
2607 | push @$res, { cmd => 'rollback' }; | |
2608 | push @$res, { cmd => 'config' }; | |
2609 | ||
2610 | return $res; | |
2611 | }}); | |
2612 | ||
2613 | __PACKAGE__->register_method({ | |
2614 | name => 'update_snapshot_config', | |
2615 | path => '{vmid}/snapshot/{snapname}/config', | |
2616 | method => 'PUT', | |
2617 | protected => 1, | |
2618 | proxyto => 'node', | |
2619 | description => "Update snapshot metadata.", | |
2620 | permissions => { | |
2621 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
2622 | }, | |
2623 | parameters => { | |
2624 | additionalProperties => 0, | |
2625 | properties => { | |
2626 | node => get_standard_option('pve-node'), | |
2627 | vmid => get_standard_option('pve-vmid'), | |
2628 | snapname => get_standard_option('pve-snapshot-name'), | |
2629 | description => { | |
2630 | optional => 1, | |
2631 | type => 'string', | |
2632 | description => "A textual description or comment.", | |
2633 | }, | |
2634 | }, | |
2635 | }, | |
2636 | returns => { type => 'null' }, | |
2637 | code => sub { | |
2638 | my ($param) = @_; | |
2639 | ||
2640 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2641 | ||
2642 | my $authuser = $rpcenv->get_user(); | |
2643 | ||
2644 | my $vmid = extract_param($param, 'vmid'); | |
2645 | ||
2646 | my $snapname = extract_param($param, 'snapname'); | |
2647 | ||
2648 | return undef if !defined($param->{description}); | |
2649 | ||
2650 | my $updatefn = sub { | |
2651 | ||
2652 | my $conf = PVE::QemuServer::load_config($vmid); | |
2653 | ||
2654 | PVE::QemuServer::check_lock($conf); | |
2655 | ||
2656 | my $snap = $conf->{snapshots}->{$snapname}; | |
2657 | ||
2658 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
2659 | ||
2660 | $snap->{description} = $param->{description} if defined($param->{description}); | |
2661 | ||
2662 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
2663 | }; | |
2664 | ||
2665 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
2666 | ||
2667 | return undef; | |
2668 | }}); | |
2669 | ||
2670 | __PACKAGE__->register_method({ | |
2671 | name => 'get_snapshot_config', | |
2672 | path => '{vmid}/snapshot/{snapname}/config', | |
2673 | method => 'GET', | |
2674 | proxyto => 'node', | |
2675 | description => "Get snapshot configuration", | |
2676 | permissions => { | |
2677 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
2678 | }, | |
2679 | parameters => { | |
2680 | additionalProperties => 0, | |
2681 | properties => { | |
2682 | node => get_standard_option('pve-node'), | |
2683 | vmid => get_standard_option('pve-vmid'), | |
2684 | snapname => get_standard_option('pve-snapshot-name'), | |
2685 | }, | |
2686 | }, | |
2687 | returns => { type => "object" }, | |
2688 | code => sub { | |
2689 | my ($param) = @_; | |
2690 | ||
2691 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2692 | ||
2693 | my $authuser = $rpcenv->get_user(); | |
2694 | ||
2695 | my $vmid = extract_param($param, 'vmid'); | |
2696 | ||
2697 | my $snapname = extract_param($param, 'snapname'); | |
2698 | ||
2699 | my $conf = PVE::QemuServer::load_config($vmid); | |
2700 | ||
2701 | my $snap = $conf->{snapshots}->{$snapname}; | |
2702 | ||
2703 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
2704 | ||
2705 | return $snap; | |
2706 | }}); | |
2707 | ||
2708 | __PACKAGE__->register_method({ | |
2709 | name => 'rollback', | |
2710 | path => '{vmid}/snapshot/{snapname}/rollback', | |
2711 | method => 'POST', | |
2712 | protected => 1, | |
2713 | proxyto => 'node', | |
2714 | description => "Rollback VM state to specified snapshot.", | |
2715 | permissions => { | |
2716 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
2717 | }, | |
2718 | parameters => { | |
2719 | additionalProperties => 0, | |
2720 | properties => { | |
2721 | node => get_standard_option('pve-node'), | |
2722 | vmid => get_standard_option('pve-vmid'), | |
2723 | snapname => get_standard_option('pve-snapshot-name'), | |
2724 | }, | |
2725 | }, | |
2726 | returns => { | |
2727 | type => 'string', | |
2728 | description => "the task ID.", | |
2729 | }, | |
2730 | code => sub { | |
2731 | my ($param) = @_; | |
2732 | ||
2733 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2734 | ||
2735 | my $authuser = $rpcenv->get_user(); | |
2736 | ||
2737 | my $node = extract_param($param, 'node'); | |
2738 | ||
2739 | my $vmid = extract_param($param, 'vmid'); | |
2740 | ||
2741 | my $snapname = extract_param($param, 'snapname'); | |
2742 | ||
2743 | my $realcmd = sub { | |
2744 | PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname"); | |
2745 | PVE::QemuServer::snapshot_rollback($vmid, $snapname); | |
2746 | }; | |
2747 | ||
2748 | return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd); | |
2749 | }}); | |
2750 | ||
2751 | __PACKAGE__->register_method({ | |
2752 | name => 'delsnapshot', | |
2753 | path => '{vmid}/snapshot/{snapname}', | |
2754 | method => 'DELETE', | |
2755 | protected => 1, | |
2756 | proxyto => 'node', | |
2757 | description => "Delete a VM snapshot.", | |
2758 | permissions => { | |
2759 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
2760 | }, | |
2761 | parameters => { | |
2762 | additionalProperties => 0, | |
2763 | properties => { | |
2764 | node => get_standard_option('pve-node'), | |
2765 | vmid => get_standard_option('pve-vmid'), | |
2766 | snapname => get_standard_option('pve-snapshot-name'), | |
2767 | force => { | |
2768 | optional => 1, | |
2769 | type => 'boolean', | |
2770 | description => "For removal from config file, even if removing disk snapshots fails.", | |
2771 | }, | |
2772 | }, | |
2773 | }, | |
2774 | returns => { | |
2775 | type => 'string', | |
2776 | description => "the task ID.", | |
2777 | }, | |
2778 | code => sub { | |
2779 | my ($param) = @_; | |
2780 | ||
2781 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2782 | ||
2783 | my $authuser = $rpcenv->get_user(); | |
2784 | ||
2785 | my $node = extract_param($param, 'node'); | |
2786 | ||
2787 | my $vmid = extract_param($param, 'vmid'); | |
2788 | ||
2789 | my $snapname = extract_param($param, 'snapname'); | |
2790 | ||
2791 | my $realcmd = sub { | |
2792 | PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname"); | |
2793 | PVE::QemuServer::snapshot_delete($vmid, $snapname, $param->{force}); | |
2794 | }; | |
2795 | ||
2796 | return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd); | |
2797 | }}); | |
2798 | ||
2799 | __PACKAGE__->register_method({ | |
2800 | name => 'template', | |
2801 | path => '{vmid}/template', | |
2802 | method => 'POST', | |
2803 | protected => 1, | |
2804 | proxyto => 'node', | |
2805 | description => "Create a Template.", | |
2806 | permissions => { | |
2807 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", | |
2808 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
2809 | }, | |
2810 | parameters => { | |
2811 | additionalProperties => 0, | |
2812 | properties => { | |
2813 | node => get_standard_option('pve-node'), | |
2814 | vmid => get_standard_option('pve-vmid'), | |
2815 | disk => { | |
2816 | optional => 1, | |
2817 | type => 'string', | |
2818 | description => "If you want to convert only 1 disk to base image.", | |
2819 | enum => [PVE::QemuServer::disknames()], | |
2820 | }, | |
2821 | ||
2822 | }, | |
2823 | }, | |
2824 | returns => { type => 'null'}, | |
2825 | code => sub { | |
2826 | my ($param) = @_; | |
2827 | ||
2828 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2829 | ||
2830 | my $authuser = $rpcenv->get_user(); | |
2831 | ||
2832 | my $node = extract_param($param, 'node'); | |
2833 | ||
2834 | my $vmid = extract_param($param, 'vmid'); | |
2835 | ||
2836 | my $disk = extract_param($param, 'disk'); | |
2837 | ||
2838 | my $updatefn = sub { | |
2839 | ||
2840 | my $conf = PVE::QemuServer::load_config($vmid); | |
2841 | ||
2842 | PVE::QemuServer::check_lock($conf); | |
2843 | ||
2844 | die "unable to create template, because VM contains snapshots\n" | |
2845 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); | |
2846 | ||
2847 | die "you can't convert a template to a template\n" | |
2848 | if PVE::QemuServer::is_template($conf) && !$disk; | |
2849 | ||
2850 | die "you can't convert a VM to template if VM is running\n" | |
2851 | if PVE::QemuServer::check_running($vmid); | |
2852 | ||
2853 | my $realcmd = sub { | |
2854 | PVE::QemuServer::template_create($vmid, $conf, $disk); | |
2855 | }; | |
2856 | ||
2857 | $conf->{template} = 1; | |
2858 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
2859 | ||
2860 | return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd); | |
2861 | }; | |
2862 | ||
2863 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
2864 | return undef; | |
2865 | }}); | |
2866 | ||
2867 | 1; |