]>
Commit | Line | Data |
---|---|---|
1 | package PVE::API2::Qemu; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Cwd 'abs_path'; | |
6 | use Net::SSLeay; | |
7 | use UUID; | |
8 | ||
9 | use PVE::Cluster qw (cfs_read_file cfs_write_file);; | |
10 | use PVE::SafeSyslog; | |
11 | use PVE::Tools qw(extract_param); | |
12 | use PVE::Exception qw(raise raise_param_exc raise_perm_exc); | |
13 | use PVE::Storage; | |
14 | use PVE::JSONSchema qw(get_standard_option); | |
15 | use PVE::RESTHandler; | |
16 | use PVE::QemuConfig; | |
17 | use PVE::QemuServer; | |
18 | use PVE::QemuMigrate; | |
19 | use PVE::RPCEnvironment; | |
20 | use PVE::AccessControl; | |
21 | use PVE::INotify; | |
22 | use PVE::Network; | |
23 | use PVE::Firewall; | |
24 | use PVE::API2::Firewall::VM; | |
25 | use PVE::HA::Env::PVE2; | |
26 | use PVE::HA::Config; | |
27 | ||
28 | use Data::Dumper; # fixme: remove | |
29 | ||
30 | use base qw(PVE::RESTHandler); | |
31 | ||
32 | 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."; | |
33 | ||
34 | my $resolve_cdrom_alias = sub { | |
35 | my $param = shift; | |
36 | ||
37 | if (my $value = $param->{cdrom}) { | |
38 | $value .= ",media=cdrom" if $value !~ m/media=/; | |
39 | $param->{ide2} = $value; | |
40 | delete $param->{cdrom}; | |
41 | } | |
42 | }; | |
43 | ||
44 | my $check_storage_access = sub { | |
45 | my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_; | |
46 | ||
47 | PVE::QemuServer::foreach_drive($settings, sub { | |
48 | my ($ds, $drive) = @_; | |
49 | ||
50 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); | |
51 | ||
52 | my $volid = $drive->{file}; | |
53 | ||
54 | if (!$volid || $volid eq 'none') { | |
55 | # nothing to check | |
56 | } elsif ($isCDROM && ($volid eq 'cdrom')) { | |
57 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
58 | } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) { | |
59 | my ($storeid, $size) = ($2 || $default_storage, $3); | |
60 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
61 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
62 | } else { | |
63 | $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid); | |
64 | } | |
65 | }); | |
66 | }; | |
67 | ||
68 | my $check_storage_access_clone = sub { | |
69 | my ($rpcenv, $authuser, $storecfg, $conf, $storage) = @_; | |
70 | ||
71 | my $sharedvm = 1; | |
72 | ||
73 | PVE::QemuServer::foreach_drive($conf, sub { | |
74 | my ($ds, $drive) = @_; | |
75 | ||
76 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); | |
77 | ||
78 | my $volid = $drive->{file}; | |
79 | ||
80 | return if !$volid || $volid eq 'none'; | |
81 | ||
82 | if ($isCDROM) { | |
83 | if ($volid eq 'cdrom') { | |
84 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
85 | } else { | |
86 | # we simply allow access | |
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 | } | |
92 | } else { | |
93 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
94 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
95 | $sharedvm = 0 if !$scfg->{shared}; | |
96 | ||
97 | $sid = $storage if $storage; | |
98 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); | |
99 | } | |
100 | }); | |
101 | ||
102 | return $sharedvm; | |
103 | }; | |
104 | ||
105 | # Note: $pool is only needed when creating a VM, because pool permissions | |
106 | # are automatically inherited if VM already exists inside a pool. | |
107 | my $create_disks = sub { | |
108 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_; | |
109 | ||
110 | my $vollist = []; | |
111 | ||
112 | my $res = {}; | |
113 | PVE::QemuServer::foreach_drive($settings, sub { | |
114 | my ($ds, $disk) = @_; | |
115 | ||
116 | my $volid = $disk->{file}; | |
117 | ||
118 | if (!$volid || $volid eq 'none' || $volid eq 'cdrom') { | |
119 | delete $disk->{size}; | |
120 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
121 | } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) { | |
122 | my ($storeid, $size) = ($2 || $default_storage, $3); | |
123 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
124 | my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid); | |
125 | my $fmt = $disk->{format} || $defformat; | |
126 | ||
127 | my $volid; | |
128 | if ($ds eq 'efidisk0') { | |
129 | # handle efidisk | |
130 | my $ovmfvars = '/usr/share/kvm/OVMF_VARS-pure-efi.fd'; | |
131 | die "uefi vars image not found\n" if ! -f $ovmfvars; | |
132 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, | |
133 | $fmt, undef, 128); | |
134 | $disk->{file} = $volid; | |
135 | $disk->{size} = 128*1024; | |
136 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
137 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); | |
138 | my $qemufmt = PVE::QemuServer::qemu_img_format($scfg, $volname); | |
139 | my $path = PVE::Storage::path($storecfg, $volid); | |
140 | my $efidiskcmd = ['/usr/bin/qemu-img', 'convert', '-n', '-f', 'raw', '-O', $qemufmt]; | |
141 | push @$efidiskcmd, $ovmfvars; | |
142 | push @$efidiskcmd, $path; | |
143 | ||
144 | PVE::Storage::activate_volumes($storecfg, [$volid]); | |
145 | ||
146 | eval { PVE::Tools::run_command($efidiskcmd); }; | |
147 | my $err = $@; | |
148 | die "Copying of EFI Vars image failed: $err" if $err; | |
149 | } else { | |
150 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, | |
151 | $fmt, undef, $size*1024*1024); | |
152 | $disk->{file} = $volid; | |
153 | $disk->{size} = $size*1024*1024*1024; | |
154 | } | |
155 | push @$vollist, $volid; | |
156 | delete $disk->{format}; # no longer needed | |
157 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
158 | } else { | |
159 | ||
160 | $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid); | |
161 | ||
162 | my $volid_is_new = 1; | |
163 | ||
164 | if ($conf->{$ds}) { | |
165 | my $olddrive = PVE::QemuServer::parse_drive($ds, $conf->{$ds}); | |
166 | $volid_is_new = undef if $olddrive->{file} && $olddrive->{file} eq $volid; | |
167 | } | |
168 | ||
169 | if ($volid_is_new) { | |
170 | ||
171 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
172 | ||
173 | PVE::Storage::activate_volumes($storecfg, [ $volid ]) if $storeid; | |
174 | ||
175 | my $size = PVE::Storage::volume_size_info($storecfg, $volid); | |
176 | ||
177 | die "volume $volid does not exists\n" if !$size; | |
178 | ||
179 | $disk->{size} = $size; | |
180 | } | |
181 | ||
182 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
183 | } | |
184 | }); | |
185 | ||
186 | # free allocated images on error | |
187 | if (my $err = $@) { | |
188 | syslog('err', "VM $vmid creating disks failed"); | |
189 | foreach my $volid (@$vollist) { | |
190 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
191 | warn $@ if $@; | |
192 | } | |
193 | die $err; | |
194 | } | |
195 | ||
196 | # modify vm config if everything went well | |
197 | foreach my $ds (keys %$res) { | |
198 | $conf->{$ds} = $res->{$ds}; | |
199 | } | |
200 | ||
201 | return $vollist; | |
202 | }; | |
203 | ||
204 | my $cpuoptions = { | |
205 | 'cores' => 1, | |
206 | 'cpu' => 1, | |
207 | 'cpulimit' => 1, | |
208 | 'cpuunits' => 1, | |
209 | 'numa' => 1, | |
210 | 'smp' => 1, | |
211 | 'sockets' => 1, | |
212 | 'vcpus' => 1, | |
213 | }; | |
214 | ||
215 | my $memoryoptions = { | |
216 | 'memory' => 1, | |
217 | 'balloon' => 1, | |
218 | 'shares' => 1, | |
219 | }; | |
220 | ||
221 | my $hwtypeoptions = { | |
222 | 'acpi' => 1, | |
223 | 'hotplug' => 1, | |
224 | 'kvm' => 1, | |
225 | 'machine' => 1, | |
226 | 'scsihw' => 1, | |
227 | 'smbios1' => 1, | |
228 | 'tablet' => 1, | |
229 | 'vga' => 1, | |
230 | 'watchdog' => 1, | |
231 | }; | |
232 | ||
233 | my $generaloptions = { | |
234 | 'agent' => 1, | |
235 | 'autostart' => 1, | |
236 | 'bios' => 1, | |
237 | 'description' => 1, | |
238 | 'keyboard' => 1, | |
239 | 'localtime' => 1, | |
240 | 'migrate_downtime' => 1, | |
241 | 'migrate_speed' => 1, | |
242 | 'name' => 1, | |
243 | 'onboot' => 1, | |
244 | 'ostype' => 1, | |
245 | 'protection' => 1, | |
246 | 'reboot' => 1, | |
247 | 'startdate' => 1, | |
248 | 'startup' => 1, | |
249 | 'tdf' => 1, | |
250 | 'template' => 1, | |
251 | }; | |
252 | ||
253 | my $vmpoweroptions = { | |
254 | 'freeze' => 1, | |
255 | }; | |
256 | ||
257 | my $diskoptions = { | |
258 | 'boot' => 1, | |
259 | 'bootdisk' => 1, | |
260 | }; | |
261 | ||
262 | my $check_vm_modify_config_perm = sub { | |
263 | my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_; | |
264 | ||
265 | return 1 if $authuser eq 'root@pam'; | |
266 | ||
267 | foreach my $opt (@$key_list) { | |
268 | # disk checks need to be done somewhere else | |
269 | next if PVE::QemuServer::is_valid_drivename($opt); | |
270 | next if $opt eq 'cdrom'; | |
271 | next if $opt =~ m/^unused\d+$/; | |
272 | ||
273 | if ($cpuoptions->{$opt} || $opt =~ m/^numa\d+$/) { | |
274 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']); | |
275 | } elsif ($memoryoptions->{$opt}) { | |
276 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']); | |
277 | } elsif ($hwtypeoptions->{$opt}) { | |
278 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); | |
279 | } elsif ($generaloptions->{$opt}) { | |
280 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']); | |
281 | # special case for startup since it changes host behaviour | |
282 | if ($opt eq 'startup') { | |
283 | $rpcenv->check_full($authuser, "/", ['Sys.Modify']); | |
284 | } | |
285 | } elsif ($vmpoweroptions->{$opt}) { | |
286 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.PowerMgmt']); | |
287 | } elsif ($diskoptions->{$opt}) { | |
288 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']); | |
289 | } elsif ($opt =~ m/^net\d+$/) { | |
290 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']); | |
291 | } else { | |
292 | # catches usb\d+, hostpci\d+, args, lock, etc. | |
293 | # new options will be checked here | |
294 | die "only root can set '$opt' config\n"; | |
295 | } | |
296 | } | |
297 | ||
298 | return 1; | |
299 | }; | |
300 | ||
301 | __PACKAGE__->register_method({ | |
302 | name => 'vmlist', | |
303 | path => '', | |
304 | method => 'GET', | |
305 | description => "Virtual machine index (per node).", | |
306 | permissions => { | |
307 | description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.", | |
308 | user => 'all', | |
309 | }, | |
310 | proxyto => 'node', | |
311 | protected => 1, # qemu pid files are only readable by root | |
312 | parameters => { | |
313 | additionalProperties => 0, | |
314 | properties => { | |
315 | node => get_standard_option('pve-node'), | |
316 | full => { | |
317 | type => 'boolean', | |
318 | optional => 1, | |
319 | description => "Determine the full status of active VMs.", | |
320 | }, | |
321 | }, | |
322 | }, | |
323 | returns => { | |
324 | type => 'array', | |
325 | items => { | |
326 | type => "object", | |
327 | properties => {}, | |
328 | }, | |
329 | links => [ { rel => 'child', href => "{vmid}" } ], | |
330 | }, | |
331 | code => sub { | |
332 | my ($param) = @_; | |
333 | ||
334 | my $rpcenv = PVE::RPCEnvironment::get(); | |
335 | my $authuser = $rpcenv->get_user(); | |
336 | ||
337 | my $vmstatus = PVE::QemuServer::vmstatus(undef, $param->{full}); | |
338 | ||
339 | my $res = []; | |
340 | foreach my $vmid (keys %$vmstatus) { | |
341 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1); | |
342 | ||
343 | my $data = $vmstatus->{$vmid}; | |
344 | $data->{vmid} = int($vmid); | |
345 | push @$res, $data; | |
346 | } | |
347 | ||
348 | return $res; | |
349 | }}); | |
350 | ||
351 | ||
352 | ||
353 | __PACKAGE__->register_method({ | |
354 | name => 'create_vm', | |
355 | path => '', | |
356 | method => 'POST', | |
357 | description => "Create or restore a virtual machine.", | |
358 | permissions => { | |
359 | description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " . | |
360 | "For restore (option 'archive'), it is enough if the user has 'VM.Backup' permission and the VM already exists. " . | |
361 | "If you create disks you need 'Datastore.AllocateSpace' on any used storage.", | |
362 | user => 'all', # check inside | |
363 | }, | |
364 | protected => 1, | |
365 | proxyto => 'node', | |
366 | parameters => { | |
367 | additionalProperties => 0, | |
368 | properties => PVE::QemuServer::json_config_properties( | |
369 | { | |
370 | node => get_standard_option('pve-node'), | |
371 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), | |
372 | archive => { | |
373 | description => "The backup file.", | |
374 | type => 'string', | |
375 | optional => 1, | |
376 | maxLength => 255, | |
377 | completion => \&PVE::QemuServer::complete_backup_archives, | |
378 | }, | |
379 | storage => get_standard_option('pve-storage-id', { | |
380 | description => "Default storage.", | |
381 | optional => 1, | |
382 | completion => \&PVE::QemuServer::complete_storage, | |
383 | }), | |
384 | force => { | |
385 | optional => 1, | |
386 | type => 'boolean', | |
387 | description => "Allow to overwrite existing VM.", | |
388 | requires => 'archive', | |
389 | }, | |
390 | unique => { | |
391 | optional => 1, | |
392 | type => 'boolean', | |
393 | description => "Assign a unique random ethernet address.", | |
394 | requires => 'archive', | |
395 | }, | |
396 | pool => { | |
397 | optional => 1, | |
398 | type => 'string', format => 'pve-poolid', | |
399 | description => "Add the VM to the specified pool.", | |
400 | }, | |
401 | }), | |
402 | }, | |
403 | returns => { | |
404 | type => 'string', | |
405 | }, | |
406 | code => sub { | |
407 | my ($param) = @_; | |
408 | ||
409 | my $rpcenv = PVE::RPCEnvironment::get(); | |
410 | ||
411 | my $authuser = $rpcenv->get_user(); | |
412 | ||
413 | my $node = extract_param($param, 'node'); | |
414 | ||
415 | my $vmid = extract_param($param, 'vmid'); | |
416 | ||
417 | my $archive = extract_param($param, 'archive'); | |
418 | ||
419 | my $storage = extract_param($param, 'storage'); | |
420 | ||
421 | my $force = extract_param($param, 'force'); | |
422 | ||
423 | my $unique = extract_param($param, 'unique'); | |
424 | ||
425 | my $pool = extract_param($param, 'pool'); | |
426 | ||
427 | my $filename = PVE::QemuConfig->config_file($vmid); | |
428 | ||
429 | my $storecfg = PVE::Storage::config(); | |
430 | ||
431 | PVE::Cluster::check_cfs_quorum(); | |
432 | ||
433 | if (defined($pool)) { | |
434 | $rpcenv->check_pool_exist($pool); | |
435 | } | |
436 | ||
437 | $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']) | |
438 | if defined($storage); | |
439 | ||
440 | if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) { | |
441 | # OK | |
442 | } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) { | |
443 | # OK | |
444 | } elsif ($archive && $force && (-f $filename) && | |
445 | $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) { | |
446 | # OK: user has VM.Backup permissions, and want to restore an existing VM | |
447 | } else { | |
448 | raise_perm_exc(); | |
449 | } | |
450 | ||
451 | if (!$archive) { | |
452 | &$resolve_cdrom_alias($param); | |
453 | ||
454 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage); | |
455 | ||
456 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]); | |
457 | ||
458 | foreach my $opt (keys %$param) { | |
459 | if (PVE::QemuServer::is_valid_drivename($opt)) { | |
460 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
461 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; | |
462 | ||
463 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); | |
464 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
465 | } | |
466 | } | |
467 | ||
468 | PVE::QemuServer::add_random_macs($param); | |
469 | } else { | |
470 | my $keystr = join(' ', keys %$param); | |
471 | raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr; | |
472 | ||
473 | if ($archive eq '-') { | |
474 | die "pipe requires cli environment\n" | |
475 | if $rpcenv->{type} ne 'cli'; | |
476 | } else { | |
477 | $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive); | |
478 | $archive = PVE::Storage::abs_filesystem_path($storecfg, $archive); | |
479 | } | |
480 | } | |
481 | ||
482 | my $restorefn = sub { | |
483 | my $vmlist = PVE::Cluster::get_vmlist(); | |
484 | if ($vmlist->{ids}->{$vmid}) { | |
485 | my $current_node = $vmlist->{ids}->{$vmid}->{node}; | |
486 | if ($current_node eq $node) { | |
487 | my $conf = PVE::QemuConfig->load_config($vmid); | |
488 | ||
489 | PVE::QemuConfig->check_protection($conf, "unable to restore VM $vmid"); | |
490 | ||
491 | die "unable to restore vm $vmid - config file already exists\n" | |
492 | if !$force; | |
493 | ||
494 | die "unable to restore vm $vmid - vm is running\n" | |
495 | if PVE::QemuServer::check_running($vmid); | |
496 | ||
497 | die "unable to restore vm $vmid - vm is a template\n" | |
498 | if PVE::QemuConfig->is_template($conf); | |
499 | ||
500 | } else { | |
501 | die "unable to restore vm $vmid - already existing on cluster node '$current_node'\n"; | |
502 | } | |
503 | } | |
504 | ||
505 | my $realcmd = sub { | |
506 | PVE::QemuServer::restore_archive($archive, $vmid, $authuser, { | |
507 | storage => $storage, | |
508 | pool => $pool, | |
509 | unique => $unique }); | |
510 | ||
511 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; | |
512 | }; | |
513 | ||
514 | return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd); | |
515 | }; | |
516 | ||
517 | my $createfn = sub { | |
518 | ||
519 | # test after locking | |
520 | PVE::Cluster::check_vmid_unused($vmid); | |
521 | ||
522 | my $realcmd = sub { | |
523 | ||
524 | my $vollist = []; | |
525 | ||
526 | my $conf = $param; | |
527 | ||
528 | eval { | |
529 | ||
530 | $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage); | |
531 | ||
532 | # try to be smart about bootdisk | |
533 | my @disks = PVE::QemuServer::valid_drive_names(); | |
534 | my $firstdisk; | |
535 | foreach my $ds (reverse @disks) { | |
536 | next if !$conf->{$ds}; | |
537 | my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds}); | |
538 | next if PVE::QemuServer::drive_is_cdrom($disk); | |
539 | $firstdisk = $ds; | |
540 | } | |
541 | ||
542 | if (!$conf->{bootdisk} && $firstdisk) { | |
543 | $conf->{bootdisk} = $firstdisk; | |
544 | } | |
545 | ||
546 | # auto generate uuid if user did not specify smbios1 option | |
547 | if (!$conf->{smbios1}) { | |
548 | my ($uuid, $uuid_str); | |
549 | UUID::generate($uuid); | |
550 | UUID::unparse($uuid, $uuid_str); | |
551 | $conf->{smbios1} = "uuid=$uuid_str"; | |
552 | } | |
553 | ||
554 | PVE::QemuConfig->write_config($vmid, $conf); | |
555 | ||
556 | }; | |
557 | my $err = $@; | |
558 | ||
559 | if ($err) { | |
560 | foreach my $volid (@$vollist) { | |
561 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
562 | warn $@ if $@; | |
563 | } | |
564 | die "create failed - $err"; | |
565 | } | |
566 | ||
567 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; | |
568 | }; | |
569 | ||
570 | return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd); | |
571 | }; | |
572 | ||
573 | return PVE::QemuConfig->lock_config_full($vmid, 1, $archive ? $restorefn : $createfn); | |
574 | }}); | |
575 | ||
576 | __PACKAGE__->register_method({ | |
577 | name => 'vmdiridx', | |
578 | path => '{vmid}', | |
579 | method => 'GET', | |
580 | proxyto => 'node', | |
581 | description => "Directory index", | |
582 | permissions => { | |
583 | user => 'all', | |
584 | }, | |
585 | parameters => { | |
586 | additionalProperties => 0, | |
587 | properties => { | |
588 | node => get_standard_option('pve-node'), | |
589 | vmid => get_standard_option('pve-vmid'), | |
590 | }, | |
591 | }, | |
592 | returns => { | |
593 | type => 'array', | |
594 | items => { | |
595 | type => "object", | |
596 | properties => { | |
597 | subdir => { type => 'string' }, | |
598 | }, | |
599 | }, | |
600 | links => [ { rel => 'child', href => "{subdir}" } ], | |
601 | }, | |
602 | code => sub { | |
603 | my ($param) = @_; | |
604 | ||
605 | my $res = [ | |
606 | { subdir => 'config' }, | |
607 | { subdir => 'pending' }, | |
608 | { subdir => 'status' }, | |
609 | { subdir => 'unlink' }, | |
610 | { subdir => 'vncproxy' }, | |
611 | { subdir => 'migrate' }, | |
612 | { subdir => 'resize' }, | |
613 | { subdir => 'move' }, | |
614 | { subdir => 'rrd' }, | |
615 | { subdir => 'rrddata' }, | |
616 | { subdir => 'monitor' }, | |
617 | { subdir => 'agent' }, | |
618 | { subdir => 'snapshot' }, | |
619 | { subdir => 'spiceproxy' }, | |
620 | { subdir => 'sendkey' }, | |
621 | { subdir => 'firewall' }, | |
622 | ]; | |
623 | ||
624 | return $res; | |
625 | }}); | |
626 | ||
627 | __PACKAGE__->register_method ({ | |
628 | subclass => "PVE::API2::Firewall::VM", | |
629 | path => '{vmid}/firewall', | |
630 | }); | |
631 | ||
632 | __PACKAGE__->register_method({ | |
633 | name => 'rrd', | |
634 | path => '{vmid}/rrd', | |
635 | method => 'GET', | |
636 | protected => 1, # fixme: can we avoid that? | |
637 | permissions => { | |
638 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
639 | }, | |
640 | description => "Read VM RRD statistics (returns PNG)", | |
641 | parameters => { | |
642 | additionalProperties => 0, | |
643 | properties => { | |
644 | node => get_standard_option('pve-node'), | |
645 | vmid => get_standard_option('pve-vmid'), | |
646 | timeframe => { | |
647 | description => "Specify the time frame you are interested in.", | |
648 | type => 'string', | |
649 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
650 | }, | |
651 | ds => { | |
652 | description => "The list of datasources you want to display.", | |
653 | type => 'string', format => 'pve-configid-list', | |
654 | }, | |
655 | cf => { | |
656 | description => "The RRD consolidation function", | |
657 | type => 'string', | |
658 | enum => [ 'AVERAGE', 'MAX' ], | |
659 | optional => 1, | |
660 | }, | |
661 | }, | |
662 | }, | |
663 | returns => { | |
664 | type => "object", | |
665 | properties => { | |
666 | filename => { type => 'string' }, | |
667 | }, | |
668 | }, | |
669 | code => sub { | |
670 | my ($param) = @_; | |
671 | ||
672 | return PVE::Cluster::create_rrd_graph( | |
673 | "pve2-vm/$param->{vmid}", $param->{timeframe}, | |
674 | $param->{ds}, $param->{cf}); | |
675 | ||
676 | }}); | |
677 | ||
678 | __PACKAGE__->register_method({ | |
679 | name => 'rrddata', | |
680 | path => '{vmid}/rrddata', | |
681 | method => 'GET', | |
682 | protected => 1, # fixme: can we avoid that? | |
683 | permissions => { | |
684 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
685 | }, | |
686 | description => "Read VM RRD statistics", | |
687 | parameters => { | |
688 | additionalProperties => 0, | |
689 | properties => { | |
690 | node => get_standard_option('pve-node'), | |
691 | vmid => get_standard_option('pve-vmid'), | |
692 | timeframe => { | |
693 | description => "Specify the time frame you are interested in.", | |
694 | type => 'string', | |
695 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
696 | }, | |
697 | cf => { | |
698 | description => "The RRD consolidation function", | |
699 | type => 'string', | |
700 | enum => [ 'AVERAGE', 'MAX' ], | |
701 | optional => 1, | |
702 | }, | |
703 | }, | |
704 | }, | |
705 | returns => { | |
706 | type => "array", | |
707 | items => { | |
708 | type => "object", | |
709 | properties => {}, | |
710 | }, | |
711 | }, | |
712 | code => sub { | |
713 | my ($param) = @_; | |
714 | ||
715 | return PVE::Cluster::create_rrd_data( | |
716 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); | |
717 | }}); | |
718 | ||
719 | ||
720 | __PACKAGE__->register_method({ | |
721 | name => 'vm_config', | |
722 | path => '{vmid}/config', | |
723 | method => 'GET', | |
724 | proxyto => 'node', | |
725 | description => "Get current virtual machine configuration. This does not include pending configuration changes (see 'pending' API).", | |
726 | permissions => { | |
727 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
728 | }, | |
729 | parameters => { | |
730 | additionalProperties => 0, | |
731 | properties => { | |
732 | node => get_standard_option('pve-node'), | |
733 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
734 | current => { | |
735 | description => "Get current values (instead of pending values).", | |
736 | optional => 1, | |
737 | default => 0, | |
738 | type => 'boolean', | |
739 | }, | |
740 | }, | |
741 | }, | |
742 | returns => { | |
743 | type => "object", | |
744 | properties => { | |
745 | digest => { | |
746 | type => 'string', | |
747 | description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', | |
748 | } | |
749 | }, | |
750 | }, | |
751 | code => sub { | |
752 | my ($param) = @_; | |
753 | ||
754 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); | |
755 | ||
756 | delete $conf->{snapshots}; | |
757 | ||
758 | if (!$param->{current}) { | |
759 | foreach my $opt (keys %{$conf->{pending}}) { | |
760 | next if $opt eq 'delete'; | |
761 | my $value = $conf->{pending}->{$opt}; | |
762 | next if ref($value); # just to be sure | |
763 | $conf->{$opt} = $value; | |
764 | } | |
765 | my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete}); | |
766 | foreach my $opt (keys %$pending_delete_hash) { | |
767 | delete $conf->{$opt} if $conf->{$opt}; | |
768 | } | |
769 | } | |
770 | ||
771 | delete $conf->{pending}; | |
772 | ||
773 | return $conf; | |
774 | }}); | |
775 | ||
776 | __PACKAGE__->register_method({ | |
777 | name => 'vm_pending', | |
778 | path => '{vmid}/pending', | |
779 | method => 'GET', | |
780 | proxyto => 'node', | |
781 | description => "Get virtual machine configuration, including pending changes.", | |
782 | permissions => { | |
783 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
784 | }, | |
785 | parameters => { | |
786 | additionalProperties => 0, | |
787 | properties => { | |
788 | node => get_standard_option('pve-node'), | |
789 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
790 | }, | |
791 | }, | |
792 | returns => { | |
793 | type => "array", | |
794 | items => { | |
795 | type => "object", | |
796 | properties => { | |
797 | key => { | |
798 | description => "Configuration option name.", | |
799 | type => 'string', | |
800 | }, | |
801 | value => { | |
802 | description => "Current value.", | |
803 | type => 'string', | |
804 | optional => 1, | |
805 | }, | |
806 | pending => { | |
807 | description => "Pending value.", | |
808 | type => 'string', | |
809 | optional => 1, | |
810 | }, | |
811 | delete => { | |
812 | description => "Indicates a pending delete request if present and not 0. " . | |
813 | "The value 2 indicates a force-delete request.", | |
814 | type => 'integer', | |
815 | minimum => 0, | |
816 | maximum => 2, | |
817 | optional => 1, | |
818 | }, | |
819 | }, | |
820 | }, | |
821 | }, | |
822 | code => sub { | |
823 | my ($param) = @_; | |
824 | ||
825 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); | |
826 | ||
827 | my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete}); | |
828 | ||
829 | my $res = []; | |
830 | ||
831 | foreach my $opt (keys %$conf) { | |
832 | next if ref($conf->{$opt}); | |
833 | my $item = { key => $opt }; | |
834 | $item->{value} = $conf->{$opt} if defined($conf->{$opt}); | |
835 | $item->{pending} = $conf->{pending}->{$opt} if defined($conf->{pending}->{$opt}); | |
836 | $item->{delete} = ($pending_delete_hash->{$opt} ? 2 : 1) if exists $pending_delete_hash->{$opt}; | |
837 | push @$res, $item; | |
838 | } | |
839 | ||
840 | foreach my $opt (keys %{$conf->{pending}}) { | |
841 | next if $opt eq 'delete'; | |
842 | next if ref($conf->{pending}->{$opt}); # just to be sure | |
843 | next if defined($conf->{$opt}); | |
844 | my $item = { key => $opt }; | |
845 | $item->{pending} = $conf->{pending}->{$opt}; | |
846 | push @$res, $item; | |
847 | } | |
848 | ||
849 | while (my ($opt, $force) = each %$pending_delete_hash) { | |
850 | next if $conf->{pending}->{$opt}; # just to be sure | |
851 | next if $conf->{$opt}; | |
852 | my $item = { key => $opt, delete => ($force ? 2 : 1)}; | |
853 | push @$res, $item; | |
854 | } | |
855 | ||
856 | return $res; | |
857 | }}); | |
858 | ||
859 | # POST/PUT {vmid}/config implementation | |
860 | # | |
861 | # The original API used PUT (idempotent) an we assumed that all operations | |
862 | # are fast. But it turned out that almost any configuration change can | |
863 | # involve hot-plug actions, or disk alloc/free. Such actions can take long | |
864 | # time to complete and have side effects (not idempotent). | |
865 | # | |
866 | # The new implementation uses POST and forks a worker process. We added | |
867 | # a new option 'background_delay'. If specified we wait up to | |
868 | # 'background_delay' second for the worker task to complete. It returns null | |
869 | # if the task is finished within that time, else we return the UPID. | |
870 | ||
871 | my $update_vm_api = sub { | |
872 | my ($param, $sync) = @_; | |
873 | ||
874 | my $rpcenv = PVE::RPCEnvironment::get(); | |
875 | ||
876 | my $authuser = $rpcenv->get_user(); | |
877 | ||
878 | my $node = extract_param($param, 'node'); | |
879 | ||
880 | my $vmid = extract_param($param, 'vmid'); | |
881 | ||
882 | my $digest = extract_param($param, 'digest'); | |
883 | ||
884 | my $background_delay = extract_param($param, 'background_delay'); | |
885 | ||
886 | my @paramarr = (); # used for log message | |
887 | foreach my $key (keys %$param) { | |
888 | push @paramarr, "-$key", $param->{$key}; | |
889 | } | |
890 | ||
891 | my $skiplock = extract_param($param, 'skiplock'); | |
892 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
893 | if $skiplock && $authuser ne 'root@pam'; | |
894 | ||
895 | my $delete_str = extract_param($param, 'delete'); | |
896 | ||
897 | my $revert_str = extract_param($param, 'revert'); | |
898 | ||
899 | my $force = extract_param($param, 'force'); | |
900 | ||
901 | die "no options specified\n" if !$delete_str && !$revert_str && !scalar(keys %$param); | |
902 | ||
903 | my $storecfg = PVE::Storage::config(); | |
904 | ||
905 | my $defaults = PVE::QemuServer::load_defaults(); | |
906 | ||
907 | &$resolve_cdrom_alias($param); | |
908 | ||
909 | # now try to verify all parameters | |
910 | ||
911 | my $revert = {}; | |
912 | foreach my $opt (PVE::Tools::split_list($revert_str)) { | |
913 | if (!PVE::QemuServer::option_exists($opt)) { | |
914 | raise_param_exc({ revert => "unknown option '$opt'" }); | |
915 | } | |
916 | ||
917 | raise_param_exc({ delete => "you can't use '-$opt' and " . | |
918 | "-revert $opt' at the same time" }) | |
919 | if defined($param->{$opt}); | |
920 | ||
921 | $revert->{$opt} = 1; | |
922 | } | |
923 | ||
924 | my @delete = (); | |
925 | foreach my $opt (PVE::Tools::split_list($delete_str)) { | |
926 | $opt = 'ide2' if $opt eq 'cdrom'; | |
927 | ||
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 | raise_param_exc({ revert => "you can't use '-delete $opt' and " . | |
933 | "-revert $opt' at the same time" }) | |
934 | if $revert->{$opt}; | |
935 | ||
936 | if (!PVE::QemuServer::option_exists($opt)) { | |
937 | raise_param_exc({ delete => "unknown option '$opt'" }); | |
938 | } | |
939 | ||
940 | push @delete, $opt; | |
941 | } | |
942 | ||
943 | foreach my $opt (keys %$param) { | |
944 | if (PVE::QemuServer::is_valid_drivename($opt)) { | |
945 | # cleanup drive path | |
946 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
947 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; | |
948 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); | |
949 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
950 | } elsif ($opt =~ m/^net(\d+)$/) { | |
951 | # add macaddr | |
952 | my $net = PVE::QemuServer::parse_net($param->{$opt}); | |
953 | $param->{$opt} = PVE::QemuServer::print_net($net); | |
954 | } | |
955 | } | |
956 | ||
957 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]); | |
958 | ||
959 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]); | |
960 | ||
961 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param); | |
962 | ||
963 | my $updatefn = sub { | |
964 | ||
965 | my $conf = PVE::QemuConfig->load_config($vmid); | |
966 | ||
967 | die "checksum missmatch (file change by other user?)\n" | |
968 | if $digest && $digest ne $conf->{digest}; | |
969 | ||
970 | PVE::QemuConfig->check_lock($conf) if !$skiplock; | |
971 | ||
972 | foreach my $opt (keys %$revert) { | |
973 | if (defined($conf->{$opt})) { | |
974 | $param->{$opt} = $conf->{$opt}; | |
975 | } elsif (defined($conf->{pending}->{$opt})) { | |
976 | push @delete, $opt; | |
977 | } | |
978 | } | |
979 | ||
980 | if ($param->{memory} || defined($param->{balloon})) { | |
981 | my $maxmem = $param->{memory} || $conf->{pending}->{memory} || $conf->{memory} || $defaults->{memory}; | |
982 | my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{pending}->{balloon} || $conf->{balloon}; | |
983 | ||
984 | die "balloon value too large (must be smaller than assigned memory)\n" | |
985 | if $balloon && $balloon > $maxmem; | |
986 | } | |
987 | ||
988 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr)); | |
989 | ||
990 | my $worker = sub { | |
991 | ||
992 | print "update VM $vmid: " . join (' ', @paramarr) . "\n"; | |
993 | ||
994 | # write updates to pending section | |
995 | ||
996 | my $modified = {}; # record what $option we modify | |
997 | ||
998 | foreach my $opt (@delete) { | |
999 | $modified->{$opt} = 1; | |
1000 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload | |
1001 | if ($opt =~ m/^unused/) { | |
1002 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
1003 | PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'"); | |
1004 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
1005 | if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) { | |
1006 | delete $conf->{$opt}; | |
1007 | PVE::QemuConfig->write_config($vmid, $conf); | |
1008 | } | |
1009 | } elsif (PVE::QemuServer::is_valid_drivename($opt)) { | |
1010 | PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'"); | |
1011 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
1012 | PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt})) | |
1013 | if defined($conf->{pending}->{$opt}); | |
1014 | PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force); | |
1015 | PVE::QemuConfig->write_config($vmid, $conf); | |
1016 | } else { | |
1017 | PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force); | |
1018 | PVE::QemuConfig->write_config($vmid, $conf); | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | foreach my $opt (keys %$param) { # add/change | |
1023 | $modified->{$opt} = 1; | |
1024 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload | |
1025 | next if defined($conf->{pending}->{$opt}) && ($param->{$opt} eq $conf->{pending}->{$opt}); # skip if nothing changed | |
1026 | ||
1027 | if (PVE::QemuServer::is_valid_drivename($opt)) { | |
1028 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
1029 | if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM | |
1030 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']); | |
1031 | } else { | |
1032 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
1033 | } | |
1034 | PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt})) | |
1035 | if defined($conf->{pending}->{$opt}); | |
1036 | ||
1037 | &$create_disks($rpcenv, $authuser, $conf->{pending}, $storecfg, $vmid, undef, {$opt => $param->{$opt}}); | |
1038 | } else { | |
1039 | $conf->{pending}->{$opt} = $param->{$opt}; | |
1040 | } | |
1041 | PVE::QemuServer::vmconfig_undelete_pending_option($conf, $opt); | |
1042 | PVE::QemuConfig->write_config($vmid, $conf); | |
1043 | } | |
1044 | ||
1045 | # remove pending changes when nothing changed | |
1046 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload | |
1047 | my $changes = PVE::QemuServer::vmconfig_cleanup_pending($conf); | |
1048 | PVE::QemuConfig->write_config($vmid, $conf) if $changes; | |
1049 | ||
1050 | return if !scalar(keys %{$conf->{pending}}); | |
1051 | ||
1052 | my $running = PVE::QemuServer::check_running($vmid); | |
1053 | ||
1054 | # apply pending changes | |
1055 | ||
1056 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload | |
1057 | ||
1058 | if ($running) { | |
1059 | my $errors = {}; | |
1060 | PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors); | |
1061 | raise_param_exc($errors) if scalar(keys %$errors); | |
1062 | } else { | |
1063 | PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $running); | |
1064 | } | |
1065 | ||
1066 | return; | |
1067 | }; | |
1068 | ||
1069 | if ($sync) { | |
1070 | &$worker(); | |
1071 | return undef; | |
1072 | } else { | |
1073 | my $upid = $rpcenv->fork_worker('qmconfig', $vmid, $authuser, $worker); | |
1074 | ||
1075 | if ($background_delay) { | |
1076 | ||
1077 | # Note: It would be better to do that in the Event based HTTPServer | |
1078 | # to avoid blocking call to sleep. | |
1079 | ||
1080 | my $end_time = time() + $background_delay; | |
1081 | ||
1082 | my $task = PVE::Tools::upid_decode($upid); | |
1083 | ||
1084 | my $running = 1; | |
1085 | while (time() < $end_time) { | |
1086 | $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}); | |
1087 | last if !$running; | |
1088 | sleep(1); # this gets interrupted when child process ends | |
1089 | } | |
1090 | ||
1091 | if (!$running) { | |
1092 | my $status = PVE::Tools::upid_read_status($upid); | |
1093 | return undef if $status eq 'OK'; | |
1094 | die $status; | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | return $upid; | |
1099 | } | |
1100 | }; | |
1101 | ||
1102 | return PVE::QemuConfig->lock_config($vmid, $updatefn); | |
1103 | }; | |
1104 | ||
1105 | my $vm_config_perm_list = [ | |
1106 | 'VM.Config.Disk', | |
1107 | 'VM.Config.CDROM', | |
1108 | 'VM.Config.CPU', | |
1109 | 'VM.Config.Memory', | |
1110 | 'VM.Config.Network', | |
1111 | 'VM.Config.HWType', | |
1112 | 'VM.Config.Options', | |
1113 | ]; | |
1114 | ||
1115 | __PACKAGE__->register_method({ | |
1116 | name => 'update_vm_async', | |
1117 | path => '{vmid}/config', | |
1118 | method => 'POST', | |
1119 | protected => 1, | |
1120 | proxyto => 'node', | |
1121 | description => "Set virtual machine options (asynchrounous API).", | |
1122 | permissions => { | |
1123 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
1124 | }, | |
1125 | parameters => { | |
1126 | additionalProperties => 0, | |
1127 | properties => PVE::QemuServer::json_config_properties( | |
1128 | { | |
1129 | node => get_standard_option('pve-node'), | |
1130 | vmid => get_standard_option('pve-vmid'), | |
1131 | skiplock => get_standard_option('skiplock'), | |
1132 | delete => { | |
1133 | type => 'string', format => 'pve-configid-list', | |
1134 | description => "A list of settings you want to delete.", | |
1135 | optional => 1, | |
1136 | }, | |
1137 | revert => { | |
1138 | type => 'string', format => 'pve-configid-list', | |
1139 | description => "Revert a pending change.", | |
1140 | optional => 1, | |
1141 | }, | |
1142 | force => { | |
1143 | type => 'boolean', | |
1144 | description => $opt_force_description, | |
1145 | optional => 1, | |
1146 | requires => 'delete', | |
1147 | }, | |
1148 | digest => { | |
1149 | type => 'string', | |
1150 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1151 | maxLength => 40, | |
1152 | optional => 1, | |
1153 | }, | |
1154 | background_delay => { | |
1155 | type => 'integer', | |
1156 | description => "Time to wait for the task to finish. We return 'null' if the task finish within that time.", | |
1157 | minimum => 1, | |
1158 | maximum => 30, | |
1159 | optional => 1, | |
1160 | }, | |
1161 | }), | |
1162 | }, | |
1163 | returns => { | |
1164 | type => 'string', | |
1165 | optional => 1, | |
1166 | }, | |
1167 | code => $update_vm_api, | |
1168 | }); | |
1169 | ||
1170 | __PACKAGE__->register_method({ | |
1171 | name => 'update_vm', | |
1172 | path => '{vmid}/config', | |
1173 | method => 'PUT', | |
1174 | protected => 1, | |
1175 | proxyto => 'node', | |
1176 | description => "Set virtual machine options (synchrounous API) - You should consider using the POST method instead for any actions involving hotplug or storage allocation.", | |
1177 | permissions => { | |
1178 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
1179 | }, | |
1180 | parameters => { | |
1181 | additionalProperties => 0, | |
1182 | properties => PVE::QemuServer::json_config_properties( | |
1183 | { | |
1184 | node => get_standard_option('pve-node'), | |
1185 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
1186 | skiplock => get_standard_option('skiplock'), | |
1187 | delete => { | |
1188 | type => 'string', format => 'pve-configid-list', | |
1189 | description => "A list of settings you want to delete.", | |
1190 | optional => 1, | |
1191 | }, | |
1192 | revert => { | |
1193 | type => 'string', format => 'pve-configid-list', | |
1194 | description => "Revert a pending change.", | |
1195 | optional => 1, | |
1196 | }, | |
1197 | force => { | |
1198 | type => 'boolean', | |
1199 | description => $opt_force_description, | |
1200 | optional => 1, | |
1201 | requires => 'delete', | |
1202 | }, | |
1203 | digest => { | |
1204 | type => 'string', | |
1205 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1206 | maxLength => 40, | |
1207 | optional => 1, | |
1208 | }, | |
1209 | }), | |
1210 | }, | |
1211 | returns => { type => 'null' }, | |
1212 | code => sub { | |
1213 | my ($param) = @_; | |
1214 | &$update_vm_api($param, 1); | |
1215 | return undef; | |
1216 | } | |
1217 | }); | |
1218 | ||
1219 | ||
1220 | __PACKAGE__->register_method({ | |
1221 | name => 'destroy_vm', | |
1222 | path => '{vmid}', | |
1223 | method => 'DELETE', | |
1224 | protected => 1, | |
1225 | proxyto => 'node', | |
1226 | description => "Destroy the vm (also delete all used/owned volumes).", | |
1227 | permissions => { | |
1228 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1229 | }, | |
1230 | parameters => { | |
1231 | additionalProperties => 0, | |
1232 | properties => { | |
1233 | node => get_standard_option('pve-node'), | |
1234 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }), | |
1235 | skiplock => get_standard_option('skiplock'), | |
1236 | }, | |
1237 | }, | |
1238 | returns => { | |
1239 | type => 'string', | |
1240 | }, | |
1241 | code => sub { | |
1242 | my ($param) = @_; | |
1243 | ||
1244 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1245 | ||
1246 | my $authuser = $rpcenv->get_user(); | |
1247 | ||
1248 | my $vmid = $param->{vmid}; | |
1249 | ||
1250 | my $skiplock = $param->{skiplock}; | |
1251 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1252 | if $skiplock && $authuser ne 'root@pam'; | |
1253 | ||
1254 | # test if VM exists | |
1255 | my $conf = PVE::QemuConfig->load_config($vmid); | |
1256 | ||
1257 | my $storecfg = PVE::Storage::config(); | |
1258 | ||
1259 | PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid"); | |
1260 | ||
1261 | die "unable to remove VM $vmid - used in HA resources\n" | |
1262 | if PVE::HA::Config::vm_is_ha_managed($vmid); | |
1263 | ||
1264 | # early tests (repeat after locking) | |
1265 | die "VM $vmid is running - destroy failed\n" | |
1266 | if PVE::QemuServer::check_running($vmid); | |
1267 | ||
1268 | my $realcmd = sub { | |
1269 | my $upid = shift; | |
1270 | ||
1271 | syslog('info', "destroy VM $vmid: $upid\n"); | |
1272 | ||
1273 | PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); | |
1274 | ||
1275 | PVE::AccessControl::remove_vm_access($vmid); | |
1276 | ||
1277 | PVE::Firewall::remove_vmfw_conf($vmid); | |
1278 | }; | |
1279 | ||
1280 | return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd); | |
1281 | }}); | |
1282 | ||
1283 | __PACKAGE__->register_method({ | |
1284 | name => 'unlink', | |
1285 | path => '{vmid}/unlink', | |
1286 | method => 'PUT', | |
1287 | protected => 1, | |
1288 | proxyto => 'node', | |
1289 | description => "Unlink/delete disk images.", | |
1290 | permissions => { | |
1291 | check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']], | |
1292 | }, | |
1293 | parameters => { | |
1294 | additionalProperties => 0, | |
1295 | properties => { | |
1296 | node => get_standard_option('pve-node'), | |
1297 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
1298 | idlist => { | |
1299 | type => 'string', format => 'pve-configid-list', | |
1300 | description => "A list of disk IDs you want to delete.", | |
1301 | }, | |
1302 | force => { | |
1303 | type => 'boolean', | |
1304 | description => $opt_force_description, | |
1305 | optional => 1, | |
1306 | }, | |
1307 | }, | |
1308 | }, | |
1309 | returns => { type => 'null'}, | |
1310 | code => sub { | |
1311 | my ($param) = @_; | |
1312 | ||
1313 | $param->{delete} = extract_param($param, 'idlist'); | |
1314 | ||
1315 | __PACKAGE__->update_vm($param); | |
1316 | ||
1317 | return undef; | |
1318 | }}); | |
1319 | ||
1320 | my $sslcert; | |
1321 | ||
1322 | __PACKAGE__->register_method({ | |
1323 | name => 'vncproxy', | |
1324 | path => '{vmid}/vncproxy', | |
1325 | method => 'POST', | |
1326 | protected => 1, | |
1327 | permissions => { | |
1328 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1329 | }, | |
1330 | description => "Creates a TCP VNC proxy connections.", | |
1331 | parameters => { | |
1332 | additionalProperties => 0, | |
1333 | properties => { | |
1334 | node => get_standard_option('pve-node'), | |
1335 | vmid => get_standard_option('pve-vmid'), | |
1336 | websocket => { | |
1337 | optional => 1, | |
1338 | type => 'boolean', | |
1339 | description => "starts websockify instead of vncproxy", | |
1340 | }, | |
1341 | }, | |
1342 | }, | |
1343 | returns => { | |
1344 | additionalProperties => 0, | |
1345 | properties => { | |
1346 | user => { type => 'string' }, | |
1347 | ticket => { type => 'string' }, | |
1348 | cert => { type => 'string' }, | |
1349 | port => { type => 'integer' }, | |
1350 | upid => { type => 'string' }, | |
1351 | }, | |
1352 | }, | |
1353 | code => sub { | |
1354 | my ($param) = @_; | |
1355 | ||
1356 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1357 | ||
1358 | my $authuser = $rpcenv->get_user(); | |
1359 | ||
1360 | my $vmid = $param->{vmid}; | |
1361 | my $node = $param->{node}; | |
1362 | my $websocket = $param->{websocket}; | |
1363 | ||
1364 | my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists | |
1365 | ||
1366 | my $authpath = "/vms/$vmid"; | |
1367 | ||
1368 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
1369 | ||
1370 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) | |
1371 | if !$sslcert; | |
1372 | ||
1373 | my ($remip, $family); | |
1374 | my $remcmd = []; | |
1375 | ||
1376 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
1377 | ($remip, $family) = PVE::Cluster::remote_node_ip($node); | |
1378 | # NOTE: kvm VNC traffic is already TLS encrypted or is known unsecure | |
1379 | $remcmd = ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', $remip]; | |
1380 | } else { | |
1381 | $family = PVE::Tools::get_host_address_family($node); | |
1382 | } | |
1383 | ||
1384 | my $port = PVE::Tools::next_vnc_port($family); | |
1385 | ||
1386 | my $timeout = 10; | |
1387 | ||
1388 | my $realcmd = sub { | |
1389 | my $upid = shift; | |
1390 | ||
1391 | syslog('info', "starting vnc proxy $upid\n"); | |
1392 | ||
1393 | my $cmd; | |
1394 | ||
1395 | if ($conf->{vga} && ($conf->{vga} =~ m/^serial\d+$/)) { | |
1396 | ||
1397 | die "Websocket mode is not supported in vga serial mode!" if $websocket; | |
1398 | ||
1399 | my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-iface', $conf->{vga} ]; | |
1400 | #my $termcmd = "/usr/bin/qm terminal -iface $conf->{vga}"; | |
1401 | $cmd = ['/usr/bin/vncterm', '-rfbport', $port, | |
1402 | '-timeout', $timeout, '-authpath', $authpath, | |
1403 | '-perm', 'Sys.Console', '-c', @$remcmd, @$termcmd]; | |
1404 | } else { | |
1405 | ||
1406 | $ENV{LC_PVE_TICKET} = $ticket if $websocket; # set ticket with "qm vncproxy" | |
1407 | ||
1408 | my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid]; | |
1409 | ||
1410 | my $qmstr = join(' ', @$qmcmd); | |
1411 | ||
1412 | # also redirect stderr (else we get RFB protocol errors) | |
1413 | $cmd = ['/bin/nc6', '-l', '-p', $port, '-w', $timeout, '-e', "$qmstr 2>/dev/null"]; | |
1414 | } | |
1415 | ||
1416 | PVE::Tools::run_command($cmd); | |
1417 | ||
1418 | return; | |
1419 | }; | |
1420 | ||
1421 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); | |
1422 | ||
1423 | PVE::Tools::wait_for_vnc_port($port); | |
1424 | ||
1425 | return { | |
1426 | user => $authuser, | |
1427 | ticket => $ticket, | |
1428 | port => $port, | |
1429 | upid => $upid, | |
1430 | cert => $sslcert, | |
1431 | }; | |
1432 | }}); | |
1433 | ||
1434 | __PACKAGE__->register_method({ | |
1435 | name => 'vncwebsocket', | |
1436 | path => '{vmid}/vncwebsocket', | |
1437 | method => 'GET', | |
1438 | permissions => { | |
1439 | description => "You also need to pass a valid ticket (vncticket).", | |
1440 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1441 | }, | |
1442 | description => "Opens a weksocket for VNC traffic.", | |
1443 | parameters => { | |
1444 | additionalProperties => 0, | |
1445 | properties => { | |
1446 | node => get_standard_option('pve-node'), | |
1447 | vmid => get_standard_option('pve-vmid'), | |
1448 | vncticket => { | |
1449 | description => "Ticket from previous call to vncproxy.", | |
1450 | type => 'string', | |
1451 | maxLength => 512, | |
1452 | }, | |
1453 | port => { | |
1454 | description => "Port number returned by previous vncproxy call.", | |
1455 | type => 'integer', | |
1456 | minimum => 5900, | |
1457 | maximum => 5999, | |
1458 | }, | |
1459 | }, | |
1460 | }, | |
1461 | returns => { | |
1462 | type => "object", | |
1463 | properties => { | |
1464 | port => { type => 'string' }, | |
1465 | }, | |
1466 | }, | |
1467 | code => sub { | |
1468 | my ($param) = @_; | |
1469 | ||
1470 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1471 | ||
1472 | my $authuser = $rpcenv->get_user(); | |
1473 | ||
1474 | my $vmid = $param->{vmid}; | |
1475 | my $node = $param->{node}; | |
1476 | ||
1477 | my $authpath = "/vms/$vmid"; | |
1478 | ||
1479 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath); | |
1480 | ||
1481 | my $conf = PVE::QemuConfig->load_config($vmid, $node); # VM exists ? | |
1482 | ||
1483 | # Note: VNC ports are acessible from outside, so we do not gain any | |
1484 | # security if we verify that $param->{port} belongs to VM $vmid. This | |
1485 | # check is done by verifying the VNC ticket (inside VNC protocol). | |
1486 | ||
1487 | my $port = $param->{port}; | |
1488 | ||
1489 | return { port => $port }; | |
1490 | }}); | |
1491 | ||
1492 | __PACKAGE__->register_method({ | |
1493 | name => 'spiceproxy', | |
1494 | path => '{vmid}/spiceproxy', | |
1495 | method => 'POST', | |
1496 | protected => 1, | |
1497 | proxyto => 'node', | |
1498 | permissions => { | |
1499 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1500 | }, | |
1501 | description => "Returns a SPICE configuration to connect to the VM.", | |
1502 | parameters => { | |
1503 | additionalProperties => 0, | |
1504 | properties => { | |
1505 | node => get_standard_option('pve-node'), | |
1506 | vmid => get_standard_option('pve-vmid'), | |
1507 | proxy => get_standard_option('spice-proxy', { optional => 1 }), | |
1508 | }, | |
1509 | }, | |
1510 | returns => get_standard_option('remote-viewer-config'), | |
1511 | code => sub { | |
1512 | my ($param) = @_; | |
1513 | ||
1514 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1515 | ||
1516 | my $authuser = $rpcenv->get_user(); | |
1517 | ||
1518 | my $vmid = $param->{vmid}; | |
1519 | my $node = $param->{node}; | |
1520 | my $proxy = $param->{proxy}; | |
1521 | ||
1522 | my $conf = PVE::QemuConfig->load_config($vmid, $node); | |
1523 | my $title = "VM $vmid"; | |
1524 | $title .= " - ". $conf->{name} if $conf->{name}; | |
1525 | ||
1526 | my $port = PVE::QemuServer::spice_port($vmid); | |
1527 | ||
1528 | my ($ticket, undef, $remote_viewer_config) = | |
1529 | PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port); | |
1530 | ||
1531 | PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket); | |
1532 | PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30"); | |
1533 | ||
1534 | return $remote_viewer_config; | |
1535 | }}); | |
1536 | ||
1537 | __PACKAGE__->register_method({ | |
1538 | name => 'vmcmdidx', | |
1539 | path => '{vmid}/status', | |
1540 | method => 'GET', | |
1541 | proxyto => 'node', | |
1542 | description => "Directory index", | |
1543 | permissions => { | |
1544 | user => 'all', | |
1545 | }, | |
1546 | parameters => { | |
1547 | additionalProperties => 0, | |
1548 | properties => { | |
1549 | node => get_standard_option('pve-node'), | |
1550 | vmid => get_standard_option('pve-vmid'), | |
1551 | }, | |
1552 | }, | |
1553 | returns => { | |
1554 | type => 'array', | |
1555 | items => { | |
1556 | type => "object", | |
1557 | properties => { | |
1558 | subdir => { type => 'string' }, | |
1559 | }, | |
1560 | }, | |
1561 | links => [ { rel => 'child', href => "{subdir}" } ], | |
1562 | }, | |
1563 | code => sub { | |
1564 | my ($param) = @_; | |
1565 | ||
1566 | # test if VM exists | |
1567 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); | |
1568 | ||
1569 | my $res = [ | |
1570 | { subdir => 'current' }, | |
1571 | { subdir => 'start' }, | |
1572 | { subdir => 'stop' }, | |
1573 | ]; | |
1574 | ||
1575 | return $res; | |
1576 | }}); | |
1577 | ||
1578 | __PACKAGE__->register_method({ | |
1579 | name => 'vm_status', | |
1580 | path => '{vmid}/status/current', | |
1581 | method => 'GET', | |
1582 | proxyto => 'node', | |
1583 | protected => 1, # qemu pid files are only readable by root | |
1584 | description => "Get virtual machine status.", | |
1585 | permissions => { | |
1586 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1587 | }, | |
1588 | parameters => { | |
1589 | additionalProperties => 0, | |
1590 | properties => { | |
1591 | node => get_standard_option('pve-node'), | |
1592 | vmid => get_standard_option('pve-vmid'), | |
1593 | }, | |
1594 | }, | |
1595 | returns => { type => 'object' }, | |
1596 | code => sub { | |
1597 | my ($param) = @_; | |
1598 | ||
1599 | # test if VM exists | |
1600 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); | |
1601 | ||
1602 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); | |
1603 | my $status = $vmstatus->{$param->{vmid}}; | |
1604 | ||
1605 | $status->{ha} = PVE::HA::Config::get_service_status("vm:$param->{vmid}"); | |
1606 | ||
1607 | $status->{spice} = 1 if PVE::QemuServer::vga_conf_has_spice($conf->{vga}); | |
1608 | ||
1609 | return $status; | |
1610 | }}); | |
1611 | ||
1612 | __PACKAGE__->register_method({ | |
1613 | name => 'vm_start', | |
1614 | path => '{vmid}/status/start', | |
1615 | method => 'POST', | |
1616 | protected => 1, | |
1617 | proxyto => 'node', | |
1618 | description => "Start virtual machine.", | |
1619 | permissions => { | |
1620 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1621 | }, | |
1622 | parameters => { | |
1623 | additionalProperties => 0, | |
1624 | properties => { | |
1625 | node => get_standard_option('pve-node'), | |
1626 | vmid => get_standard_option('pve-vmid', | |
1627 | { completion => \&PVE::QemuServer::complete_vmid_stopped }), | |
1628 | skiplock => get_standard_option('skiplock'), | |
1629 | stateuri => get_standard_option('pve-qm-stateuri'), | |
1630 | migratedfrom => get_standard_option('pve-node',{ optional => 1 }), | |
1631 | migration_type => { | |
1632 | type => 'string', | |
1633 | enum => ['secure', 'insecure'], | |
1634 | description => "Migration traffic is encrypted using an SSH " . | |
1635 | "tunnel by default. On secure, completely private networks " . | |
1636 | "this can be disabled to increase performance.", | |
1637 | optional => 1, | |
1638 | }, | |
1639 | migration_network => { | |
1640 | type => 'string', format => 'CIDR', | |
1641 | description => "CIDR of the (sub) network that is used for migration.", | |
1642 | optional => 1, | |
1643 | }, | |
1644 | machine => get_standard_option('pve-qm-machine'), | |
1645 | }, | |
1646 | }, | |
1647 | returns => { | |
1648 | type => 'string', | |
1649 | }, | |
1650 | code => sub { | |
1651 | my ($param) = @_; | |
1652 | ||
1653 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1654 | ||
1655 | my $authuser = $rpcenv->get_user(); | |
1656 | ||
1657 | my $node = extract_param($param, 'node'); | |
1658 | ||
1659 | my $vmid = extract_param($param, 'vmid'); | |
1660 | ||
1661 | my $machine = extract_param($param, 'machine'); | |
1662 | ||
1663 | my $stateuri = extract_param($param, 'stateuri'); | |
1664 | raise_param_exc({ stateuri => "Only root may use this option." }) | |
1665 | if $stateuri && $authuser ne 'root@pam'; | |
1666 | ||
1667 | my $skiplock = extract_param($param, 'skiplock'); | |
1668 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1669 | if $skiplock && $authuser ne 'root@pam'; | |
1670 | ||
1671 | my $migratedfrom = extract_param($param, 'migratedfrom'); | |
1672 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
1673 | if $migratedfrom && $authuser ne 'root@pam'; | |
1674 | ||
1675 | my $migration_type = extract_param($param, 'migration_type'); | |
1676 | raise_param_exc({ migration_type => "Only root may use this option." }) | |
1677 | if $migration_type && $authuser ne 'root@pam'; | |
1678 | ||
1679 | my $migration_network = extract_param($param, 'migration_network'); | |
1680 | raise_param_exc({ migration_network => "Only root may use this option." }) | |
1681 | if $migration_network && $authuser ne 'root@pam'; | |
1682 | ||
1683 | # read spice ticket from STDIN | |
1684 | my $spice_ticket; | |
1685 | if ($stateuri && ($stateuri eq 'tcp') && $migratedfrom && ($rpcenv->{type} eq 'cli')) { | |
1686 | if (defined(my $line = <>)) { | |
1687 | chomp $line; | |
1688 | $spice_ticket = $line; | |
1689 | } | |
1690 | } | |
1691 | ||
1692 | PVE::Cluster::check_cfs_quorum(); | |
1693 | ||
1694 | my $storecfg = PVE::Storage::config(); | |
1695 | ||
1696 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && !$stateuri && | |
1697 | $rpcenv->{type} ne 'ha') { | |
1698 | ||
1699 | my $hacmd = sub { | |
1700 | my $upid = shift; | |
1701 | ||
1702 | my $service = "vm:$vmid"; | |
1703 | ||
1704 | my $cmd = ['ha-manager', 'set', $service, '--state', 'started']; | |
1705 | ||
1706 | print "Executing HA start for VM $vmid\n"; | |
1707 | ||
1708 | PVE::Tools::run_command($cmd); | |
1709 | ||
1710 | return; | |
1711 | }; | |
1712 | ||
1713 | return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd); | |
1714 | ||
1715 | } else { | |
1716 | ||
1717 | my $realcmd = sub { | |
1718 | my $upid = shift; | |
1719 | ||
1720 | syslog('info', "start VM $vmid: $upid\n"); | |
1721 | ||
1722 | PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom, undef, | |
1723 | $machine, $spice_ticket, $migration_network, $migration_type); | |
1724 | ||
1725 | return; | |
1726 | }; | |
1727 | ||
1728 | return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd); | |
1729 | } | |
1730 | }}); | |
1731 | ||
1732 | __PACKAGE__->register_method({ | |
1733 | name => 'vm_stop', | |
1734 | path => '{vmid}/status/stop', | |
1735 | method => 'POST', | |
1736 | protected => 1, | |
1737 | proxyto => 'node', | |
1738 | description => "Stop virtual machine. The qemu process will exit immediately. This" . | |
1739 | "is akin to pulling the power plug of a running computer and may damage the VM data", | |
1740 | permissions => { | |
1741 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1742 | }, | |
1743 | parameters => { | |
1744 | additionalProperties => 0, | |
1745 | properties => { | |
1746 | node => get_standard_option('pve-node'), | |
1747 | vmid => get_standard_option('pve-vmid', | |
1748 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
1749 | skiplock => get_standard_option('skiplock'), | |
1750 | migratedfrom => get_standard_option('pve-node', { optional => 1 }), | |
1751 | timeout => { | |
1752 | description => "Wait maximal timeout seconds.", | |
1753 | type => 'integer', | |
1754 | minimum => 0, | |
1755 | optional => 1, | |
1756 | }, | |
1757 | keepActive => { | |
1758 | description => "Do not deactivate storage volumes.", | |
1759 | type => 'boolean', | |
1760 | optional => 1, | |
1761 | default => 0, | |
1762 | } | |
1763 | }, | |
1764 | }, | |
1765 | returns => { | |
1766 | type => 'string', | |
1767 | }, | |
1768 | code => sub { | |
1769 | my ($param) = @_; | |
1770 | ||
1771 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1772 | ||
1773 | my $authuser = $rpcenv->get_user(); | |
1774 | ||
1775 | my $node = extract_param($param, 'node'); | |
1776 | ||
1777 | my $vmid = extract_param($param, 'vmid'); | |
1778 | ||
1779 | my $skiplock = extract_param($param, 'skiplock'); | |
1780 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1781 | if $skiplock && $authuser ne 'root@pam'; | |
1782 | ||
1783 | my $keepActive = extract_param($param, 'keepActive'); | |
1784 | raise_param_exc({ keepActive => "Only root may use this option." }) | |
1785 | if $keepActive && $authuser ne 'root@pam'; | |
1786 | ||
1787 | my $migratedfrom = extract_param($param, 'migratedfrom'); | |
1788 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
1789 | if $migratedfrom && $authuser ne 'root@pam'; | |
1790 | ||
1791 | ||
1792 | my $storecfg = PVE::Storage::config(); | |
1793 | ||
1794 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && ($rpcenv->{type} ne 'ha') && !defined($migratedfrom)) { | |
1795 | ||
1796 | my $hacmd = sub { | |
1797 | my $upid = shift; | |
1798 | ||
1799 | my $service = "vm:$vmid"; | |
1800 | ||
1801 | my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped']; | |
1802 | ||
1803 | print "Executing HA stop for VM $vmid\n"; | |
1804 | ||
1805 | PVE::Tools::run_command($cmd); | |
1806 | ||
1807 | return; | |
1808 | }; | |
1809 | ||
1810 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
1811 | ||
1812 | } else { | |
1813 | my $realcmd = sub { | |
1814 | my $upid = shift; | |
1815 | ||
1816 | syslog('info', "stop VM $vmid: $upid\n"); | |
1817 | ||
1818 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, | |
1819 | $param->{timeout}, 0, 1, $keepActive, $migratedfrom); | |
1820 | ||
1821 | return; | |
1822 | }; | |
1823 | ||
1824 | return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd); | |
1825 | } | |
1826 | }}); | |
1827 | ||
1828 | __PACKAGE__->register_method({ | |
1829 | name => 'vm_reset', | |
1830 | path => '{vmid}/status/reset', | |
1831 | method => 'POST', | |
1832 | protected => 1, | |
1833 | proxyto => 'node', | |
1834 | description => "Reset virtual machine.", | |
1835 | permissions => { | |
1836 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1837 | }, | |
1838 | parameters => { | |
1839 | additionalProperties => 0, | |
1840 | properties => { | |
1841 | node => get_standard_option('pve-node'), | |
1842 | vmid => get_standard_option('pve-vmid', | |
1843 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
1844 | skiplock => get_standard_option('skiplock'), | |
1845 | }, | |
1846 | }, | |
1847 | returns => { | |
1848 | type => 'string', | |
1849 | }, | |
1850 | code => sub { | |
1851 | my ($param) = @_; | |
1852 | ||
1853 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1854 | ||
1855 | my $authuser = $rpcenv->get_user(); | |
1856 | ||
1857 | my $node = extract_param($param, 'node'); | |
1858 | ||
1859 | my $vmid = extract_param($param, 'vmid'); | |
1860 | ||
1861 | my $skiplock = extract_param($param, 'skiplock'); | |
1862 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1863 | if $skiplock && $authuser ne 'root@pam'; | |
1864 | ||
1865 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
1866 | ||
1867 | my $realcmd = sub { | |
1868 | my $upid = shift; | |
1869 | ||
1870 | PVE::QemuServer::vm_reset($vmid, $skiplock); | |
1871 | ||
1872 | return; | |
1873 | }; | |
1874 | ||
1875 | return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd); | |
1876 | }}); | |
1877 | ||
1878 | __PACKAGE__->register_method({ | |
1879 | name => 'vm_shutdown', | |
1880 | path => '{vmid}/status/shutdown', | |
1881 | method => 'POST', | |
1882 | protected => 1, | |
1883 | proxyto => 'node', | |
1884 | description => "Shutdown virtual machine. This is similar to pressing the power button on a physical machine." . | |
1885 | "This will send an ACPI event for the guest OS, which should then proceed to a clean shutdown.", | |
1886 | permissions => { | |
1887 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1888 | }, | |
1889 | parameters => { | |
1890 | additionalProperties => 0, | |
1891 | properties => { | |
1892 | node => get_standard_option('pve-node'), | |
1893 | vmid => get_standard_option('pve-vmid', | |
1894 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
1895 | skiplock => get_standard_option('skiplock'), | |
1896 | timeout => { | |
1897 | description => "Wait maximal timeout seconds.", | |
1898 | type => 'integer', | |
1899 | minimum => 0, | |
1900 | optional => 1, | |
1901 | }, | |
1902 | forceStop => { | |
1903 | description => "Make sure the VM stops.", | |
1904 | type => 'boolean', | |
1905 | optional => 1, | |
1906 | default => 0, | |
1907 | }, | |
1908 | keepActive => { | |
1909 | description => "Do not deactivate storage volumes.", | |
1910 | type => 'boolean', | |
1911 | optional => 1, | |
1912 | default => 0, | |
1913 | } | |
1914 | }, | |
1915 | }, | |
1916 | returns => { | |
1917 | type => 'string', | |
1918 | }, | |
1919 | code => sub { | |
1920 | my ($param) = @_; | |
1921 | ||
1922 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1923 | ||
1924 | my $authuser = $rpcenv->get_user(); | |
1925 | ||
1926 | my $node = extract_param($param, 'node'); | |
1927 | ||
1928 | my $vmid = extract_param($param, 'vmid'); | |
1929 | ||
1930 | my $skiplock = extract_param($param, 'skiplock'); | |
1931 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1932 | if $skiplock && $authuser ne 'root@pam'; | |
1933 | ||
1934 | my $keepActive = extract_param($param, 'keepActive'); | |
1935 | raise_param_exc({ keepActive => "Only root may use this option." }) | |
1936 | if $keepActive && $authuser ne 'root@pam'; | |
1937 | ||
1938 | my $storecfg = PVE::Storage::config(); | |
1939 | ||
1940 | my $shutdown = 1; | |
1941 | ||
1942 | # if vm is paused, do not shutdown (but stop if forceStop = 1) | |
1943 | # otherwise, we will infer a shutdown command, but run into the timeout, | |
1944 | # then when the vm is resumed, it will instantly shutdown | |
1945 | # | |
1946 | # checking the qmp status here to get feedback to the gui/cli/api | |
1947 | # and the status query should not take too long | |
1948 | my $qmpstatus; | |
1949 | eval { | |
1950 | $qmpstatus = PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0); | |
1951 | }; | |
1952 | my $err = $@ if $@; | |
1953 | ||
1954 | if (!$err && $qmpstatus->{status} eq "paused") { | |
1955 | if ($param->{forceStop}) { | |
1956 | warn "VM is paused - stop instead of shutdown\n"; | |
1957 | $shutdown = 0; | |
1958 | } else { | |
1959 | die "VM is paused - cannot shutdown\n"; | |
1960 | } | |
1961 | } | |
1962 | ||
1963 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && | |
1964 | ($rpcenv->{type} ne 'ha')) { | |
1965 | ||
1966 | my $hacmd = sub { | |
1967 | my $upid = shift; | |
1968 | ||
1969 | my $service = "vm:$vmid"; | |
1970 | ||
1971 | my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped']; | |
1972 | ||
1973 | print "Executing HA stop for VM $vmid\n"; | |
1974 | ||
1975 | PVE::Tools::run_command($cmd); | |
1976 | ||
1977 | return; | |
1978 | }; | |
1979 | ||
1980 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
1981 | ||
1982 | } else { | |
1983 | ||
1984 | my $realcmd = sub { | |
1985 | my $upid = shift; | |
1986 | ||
1987 | syslog('info', "shutdown VM $vmid: $upid\n"); | |
1988 | ||
1989 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout}, | |
1990 | $shutdown, $param->{forceStop}, $keepActive); | |
1991 | ||
1992 | return; | |
1993 | }; | |
1994 | ||
1995 | return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd); | |
1996 | } | |
1997 | }}); | |
1998 | ||
1999 | __PACKAGE__->register_method({ | |
2000 | name => 'vm_suspend', | |
2001 | path => '{vmid}/status/suspend', | |
2002 | method => 'POST', | |
2003 | protected => 1, | |
2004 | proxyto => 'node', | |
2005 | description => "Suspend virtual machine.", | |
2006 | permissions => { | |
2007 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2008 | }, | |
2009 | parameters => { | |
2010 | additionalProperties => 0, | |
2011 | properties => { | |
2012 | node => get_standard_option('pve-node'), | |
2013 | vmid => get_standard_option('pve-vmid', | |
2014 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
2015 | skiplock => get_standard_option('skiplock'), | |
2016 | }, | |
2017 | }, | |
2018 | returns => { | |
2019 | type => 'string', | |
2020 | }, | |
2021 | code => sub { | |
2022 | my ($param) = @_; | |
2023 | ||
2024 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2025 | ||
2026 | my $authuser = $rpcenv->get_user(); | |
2027 | ||
2028 | my $node = extract_param($param, 'node'); | |
2029 | ||
2030 | my $vmid = extract_param($param, 'vmid'); | |
2031 | ||
2032 | my $skiplock = extract_param($param, 'skiplock'); | |
2033 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
2034 | if $skiplock && $authuser ne 'root@pam'; | |
2035 | ||
2036 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
2037 | ||
2038 | my $realcmd = sub { | |
2039 | my $upid = shift; | |
2040 | ||
2041 | syslog('info', "suspend VM $vmid: $upid\n"); | |
2042 | ||
2043 | PVE::QemuServer::vm_suspend($vmid, $skiplock); | |
2044 | ||
2045 | return; | |
2046 | }; | |
2047 | ||
2048 | return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd); | |
2049 | }}); | |
2050 | ||
2051 | __PACKAGE__->register_method({ | |
2052 | name => 'vm_resume', | |
2053 | path => '{vmid}/status/resume', | |
2054 | method => 'POST', | |
2055 | protected => 1, | |
2056 | proxyto => 'node', | |
2057 | description => "Resume virtual machine.", | |
2058 | permissions => { | |
2059 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2060 | }, | |
2061 | parameters => { | |
2062 | additionalProperties => 0, | |
2063 | properties => { | |
2064 | node => get_standard_option('pve-node'), | |
2065 | vmid => get_standard_option('pve-vmid', | |
2066 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
2067 | skiplock => get_standard_option('skiplock'), | |
2068 | nocheck => { type => 'boolean', optional => 1 }, | |
2069 | ||
2070 | }, | |
2071 | }, | |
2072 | returns => { | |
2073 | type => 'string', | |
2074 | }, | |
2075 | code => sub { | |
2076 | my ($param) = @_; | |
2077 | ||
2078 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2079 | ||
2080 | my $authuser = $rpcenv->get_user(); | |
2081 | ||
2082 | my $node = extract_param($param, 'node'); | |
2083 | ||
2084 | my $vmid = extract_param($param, 'vmid'); | |
2085 | ||
2086 | my $skiplock = extract_param($param, 'skiplock'); | |
2087 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
2088 | if $skiplock && $authuser ne 'root@pam'; | |
2089 | ||
2090 | my $nocheck = extract_param($param, 'nocheck'); | |
2091 | ||
2092 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid, $nocheck); | |
2093 | ||
2094 | my $realcmd = sub { | |
2095 | my $upid = shift; | |
2096 | ||
2097 | syslog('info', "resume VM $vmid: $upid\n"); | |
2098 | ||
2099 | PVE::QemuServer::vm_resume($vmid, $skiplock, $nocheck); | |
2100 | ||
2101 | return; | |
2102 | }; | |
2103 | ||
2104 | return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd); | |
2105 | }}); | |
2106 | ||
2107 | __PACKAGE__->register_method({ | |
2108 | name => 'vm_sendkey', | |
2109 | path => '{vmid}/sendkey', | |
2110 | method => 'PUT', | |
2111 | protected => 1, | |
2112 | proxyto => 'node', | |
2113 | description => "Send key event to virtual machine.", | |
2114 | permissions => { | |
2115 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
2116 | }, | |
2117 | parameters => { | |
2118 | additionalProperties => 0, | |
2119 | properties => { | |
2120 | node => get_standard_option('pve-node'), | |
2121 | vmid => get_standard_option('pve-vmid', | |
2122 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
2123 | skiplock => get_standard_option('skiplock'), | |
2124 | key => { | |
2125 | description => "The key (qemu monitor encoding).", | |
2126 | type => 'string' | |
2127 | } | |
2128 | }, | |
2129 | }, | |
2130 | returns => { type => 'null'}, | |
2131 | code => sub { | |
2132 | my ($param) = @_; | |
2133 | ||
2134 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2135 | ||
2136 | my $authuser = $rpcenv->get_user(); | |
2137 | ||
2138 | my $node = extract_param($param, 'node'); | |
2139 | ||
2140 | my $vmid = extract_param($param, 'vmid'); | |
2141 | ||
2142 | my $skiplock = extract_param($param, 'skiplock'); | |
2143 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
2144 | if $skiplock && $authuser ne 'root@pam'; | |
2145 | ||
2146 | PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); | |
2147 | ||
2148 | return; | |
2149 | }}); | |
2150 | ||
2151 | __PACKAGE__->register_method({ | |
2152 | name => 'vm_feature', | |
2153 | path => '{vmid}/feature', | |
2154 | method => 'GET', | |
2155 | proxyto => 'node', | |
2156 | protected => 1, | |
2157 | description => "Check if feature for virtual machine is available.", | |
2158 | permissions => { | |
2159 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
2160 | }, | |
2161 | parameters => { | |
2162 | additionalProperties => 0, | |
2163 | properties => { | |
2164 | node => get_standard_option('pve-node'), | |
2165 | vmid => get_standard_option('pve-vmid'), | |
2166 | feature => { | |
2167 | description => "Feature to check.", | |
2168 | type => 'string', | |
2169 | enum => [ 'snapshot', 'clone', 'copy' ], | |
2170 | }, | |
2171 | snapname => get_standard_option('pve-snapshot-name', { | |
2172 | optional => 1, | |
2173 | }), | |
2174 | }, | |
2175 | }, | |
2176 | returns => { | |
2177 | type => "object", | |
2178 | properties => { | |
2179 | hasFeature => { type => 'boolean' }, | |
2180 | nodes => { | |
2181 | type => 'array', | |
2182 | items => { type => 'string' }, | |
2183 | } | |
2184 | }, | |
2185 | }, | |
2186 | code => sub { | |
2187 | my ($param) = @_; | |
2188 | ||
2189 | my $node = extract_param($param, 'node'); | |
2190 | ||
2191 | my $vmid = extract_param($param, 'vmid'); | |
2192 | ||
2193 | my $snapname = extract_param($param, 'snapname'); | |
2194 | ||
2195 | my $feature = extract_param($param, 'feature'); | |
2196 | ||
2197 | my $running = PVE::QemuServer::check_running($vmid); | |
2198 | ||
2199 | my $conf = PVE::QemuConfig->load_config($vmid); | |
2200 | ||
2201 | if($snapname){ | |
2202 | my $snap = $conf->{snapshots}->{$snapname}; | |
2203 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
2204 | $conf = $snap; | |
2205 | } | |
2206 | my $storecfg = PVE::Storage::config(); | |
2207 | ||
2208 | my $nodelist = PVE::QemuServer::shared_nodes($conf, $storecfg); | |
2209 | my $hasFeature = PVE::QemuConfig->has_feature($feature, $conf, $storecfg, $snapname, $running); | |
2210 | ||
2211 | return { | |
2212 | hasFeature => $hasFeature, | |
2213 | nodes => [ keys %$nodelist ], | |
2214 | }; | |
2215 | }}); | |
2216 | ||
2217 | __PACKAGE__->register_method({ | |
2218 | name => 'clone_vm', | |
2219 | path => '{vmid}/clone', | |
2220 | method => 'POST', | |
2221 | protected => 1, | |
2222 | proxyto => 'node', | |
2223 | description => "Create a copy of virtual machine/template.", | |
2224 | permissions => { | |
2225 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " . | |
2226 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . | |
2227 | "'Datastore.AllocateSpace' on any used storage.", | |
2228 | check => | |
2229 | [ 'and', | |
2230 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], | |
2231 | [ 'or', | |
2232 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], | |
2233 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
2234 | ], | |
2235 | ] | |
2236 | }, | |
2237 | parameters => { | |
2238 | additionalProperties => 0, | |
2239 | properties => { | |
2240 | node => get_standard_option('pve-node'), | |
2241 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
2242 | newid => get_standard_option('pve-vmid', { description => 'VMID for the clone.' }), | |
2243 | name => { | |
2244 | optional => 1, | |
2245 | type => 'string', format => 'dns-name', | |
2246 | description => "Set a name for the new VM.", | |
2247 | }, | |
2248 | description => { | |
2249 | optional => 1, | |
2250 | type => 'string', | |
2251 | description => "Description for the new VM.", | |
2252 | }, | |
2253 | pool => { | |
2254 | optional => 1, | |
2255 | type => 'string', format => 'pve-poolid', | |
2256 | description => "Add the new VM to the specified pool.", | |
2257 | }, | |
2258 | snapname => get_standard_option('pve-snapshot-name', { | |
2259 | optional => 1, | |
2260 | }), | |
2261 | storage => get_standard_option('pve-storage-id', { | |
2262 | description => "Target storage for full clone.", | |
2263 | requires => 'full', | |
2264 | optional => 1, | |
2265 | }), | |
2266 | 'format' => { | |
2267 | description => "Target format for file storage.", | |
2268 | requires => 'full', | |
2269 | type => 'string', | |
2270 | optional => 1, | |
2271 | enum => [ 'raw', 'qcow2', 'vmdk'], | |
2272 | }, | |
2273 | full => { | |
2274 | optional => 1, | |
2275 | type => 'boolean', | |
2276 | description => "Create a full copy of all disk. This is always done when " . | |
2277 | "you clone a normal VM. For VM templates, we try to create a linked clone by default.", | |
2278 | default => 0, | |
2279 | }, | |
2280 | target => get_standard_option('pve-node', { | |
2281 | description => "Target node. Only allowed if the original VM is on shared storage.", | |
2282 | optional => 1, | |
2283 | }), | |
2284 | }, | |
2285 | }, | |
2286 | returns => { | |
2287 | type => 'string', | |
2288 | }, | |
2289 | code => sub { | |
2290 | my ($param) = @_; | |
2291 | ||
2292 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2293 | ||
2294 | my $authuser = $rpcenv->get_user(); | |
2295 | ||
2296 | my $node = extract_param($param, 'node'); | |
2297 | ||
2298 | my $vmid = extract_param($param, 'vmid'); | |
2299 | ||
2300 | my $newid = extract_param($param, 'newid'); | |
2301 | ||
2302 | my $pool = extract_param($param, 'pool'); | |
2303 | ||
2304 | if (defined($pool)) { | |
2305 | $rpcenv->check_pool_exist($pool); | |
2306 | } | |
2307 | ||
2308 | my $snapname = extract_param($param, 'snapname'); | |
2309 | ||
2310 | my $storage = extract_param($param, 'storage'); | |
2311 | ||
2312 | my $format = extract_param($param, 'format'); | |
2313 | ||
2314 | my $target = extract_param($param, 'target'); | |
2315 | ||
2316 | my $localnode = PVE::INotify::nodename(); | |
2317 | ||
2318 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); | |
2319 | ||
2320 | PVE::Cluster::check_node_exists($target) if $target; | |
2321 | ||
2322 | my $storecfg = PVE::Storage::config(); | |
2323 | ||
2324 | if ($storage) { | |
2325 | # check if storage is enabled on local node | |
2326 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
2327 | if ($target) { | |
2328 | # check if storage is available on target node | |
2329 | PVE::Storage::storage_check_node($storecfg, $storage, $target); | |
2330 | # clone only works if target storage is shared | |
2331 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
2332 | die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared}; | |
2333 | } | |
2334 | } | |
2335 | ||
2336 | PVE::Cluster::check_cfs_quorum(); | |
2337 | ||
2338 | my $running = PVE::QemuServer::check_running($vmid) || 0; | |
2339 | ||
2340 | # exclusive lock if VM is running - else shared lock is enough; | |
2341 | my $shared_lock = $running ? 0 : 1; | |
2342 | ||
2343 | my $clonefn = sub { | |
2344 | ||
2345 | # do all tests after lock | |
2346 | # we also try to do all tests before we fork the worker | |
2347 | ||
2348 | my $conf = PVE::QemuConfig->load_config($vmid); | |
2349 | ||
2350 | PVE::QemuConfig->check_lock($conf); | |
2351 | ||
2352 | my $verify_running = PVE::QemuServer::check_running($vmid) || 0; | |
2353 | ||
2354 | die "unexpected state change\n" if $verify_running != $running; | |
2355 | ||
2356 | die "snapshot '$snapname' does not exist\n" | |
2357 | if $snapname && !defined( $conf->{snapshots}->{$snapname}); | |
2358 | ||
2359 | my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf; | |
2360 | ||
2361 | my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage); | |
2362 | ||
2363 | die "can't clone VM to node '$target' (VM uses local storage)\n" if $target && !$sharedvm; | |
2364 | ||
2365 | my $conffile = PVE::QemuConfig->config_file($newid); | |
2366 | ||
2367 | die "unable to create VM $newid: config file already exists\n" | |
2368 | if -f $conffile; | |
2369 | ||
2370 | my $newconf = { lock => 'clone' }; | |
2371 | my $drives = {}; | |
2372 | my $fullclone = {}; | |
2373 | my $vollist = []; | |
2374 | ||
2375 | foreach my $opt (keys %$oldconf) { | |
2376 | my $value = $oldconf->{$opt}; | |
2377 | ||
2378 | # do not copy snapshot related info | |
2379 | next if $opt eq 'snapshots' || $opt eq 'parent' || $opt eq 'snaptime' || | |
2380 | $opt eq 'vmstate' || $opt eq 'snapstate'; | |
2381 | ||
2382 | # no need to copy unused images, because VMID(owner) changes anyways | |
2383 | next if $opt =~ m/^unused\d+$/; | |
2384 | ||
2385 | # always change MAC! address | |
2386 | if ($opt =~ m/^net(\d+)$/) { | |
2387 | my $net = PVE::QemuServer::parse_net($value); | |
2388 | my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
2389 | $net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); | |
2390 | $newconf->{$opt} = PVE::QemuServer::print_net($net); | |
2391 | } elsif (PVE::QemuServer::is_valid_drivename($opt)) { | |
2392 | my $drive = PVE::QemuServer::parse_drive($opt, $value); | |
2393 | die "unable to parse drive options for '$opt'\n" if !$drive; | |
2394 | if (PVE::QemuServer::drive_is_cdrom($drive)) { | |
2395 | $newconf->{$opt} = $value; # simply copy configuration | |
2396 | } else { | |
2397 | if ($param->{full}) { | |
2398 | die "Full clone feature is not available" | |
2399 | if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running); | |
2400 | $fullclone->{$opt} = 1; | |
2401 | } else { | |
2402 | # not full means clone instead of copy | |
2403 | die "Linked clone feature is not available" | |
2404 | if !PVE::Storage::volume_has_feature($storecfg, 'clone', $drive->{file}, $snapname, $running); | |
2405 | } | |
2406 | $drives->{$opt} = $drive; | |
2407 | push @$vollist, $drive->{file}; | |
2408 | } | |
2409 | } else { | |
2410 | # copy everything else | |
2411 | $newconf->{$opt} = $value; | |
2412 | } | |
2413 | } | |
2414 | ||
2415 | # auto generate a new uuid | |
2416 | my ($uuid, $uuid_str); | |
2417 | UUID::generate($uuid); | |
2418 | UUID::unparse($uuid, $uuid_str); | |
2419 | my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || ''); | |
2420 | $smbios1->{uuid} = $uuid_str; | |
2421 | $newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1); | |
2422 | ||
2423 | delete $newconf->{template}; | |
2424 | ||
2425 | if ($param->{name}) { | |
2426 | $newconf->{name} = $param->{name}; | |
2427 | } else { | |
2428 | if ($oldconf->{name}) { | |
2429 | $newconf->{name} = "Copy-of-$oldconf->{name}"; | |
2430 | } else { | |
2431 | $newconf->{name} = "Copy-of-VM-$vmid"; | |
2432 | } | |
2433 | } | |
2434 | ||
2435 | if ($param->{description}) { | |
2436 | $newconf->{description} = $param->{description}; | |
2437 | } | |
2438 | ||
2439 | # create empty/temp config - this fails if VM already exists on other node | |
2440 | PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n"); | |
2441 | ||
2442 | my $realcmd = sub { | |
2443 | my $upid = shift; | |
2444 | ||
2445 | my $newvollist = []; | |
2446 | my $jobs = {}; | |
2447 | ||
2448 | eval { | |
2449 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
2450 | ||
2451 | PVE::Storage::activate_volumes($storecfg, $vollist, $snapname); | |
2452 | ||
2453 | my $total_jobs = scalar(keys %{$drives}); | |
2454 | my $i = 1; | |
2455 | my $skipcomplete = 1; | |
2456 | ||
2457 | foreach my $opt (keys %$drives) { | |
2458 | ||
2459 | my $drive = $drives->{$opt}; | |
2460 | $skipcomplete = undef if $total_jobs == $i; #finish after last drive | |
2461 | ||
2462 | my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $opt, $drive, $snapname, | |
2463 | $newid, $storage, $format, $fullclone->{$opt}, $newvollist, $jobs, $skipcomplete); | |
2464 | ||
2465 | $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive); | |
2466 | ||
2467 | PVE::QemuConfig->write_config($newid, $newconf); | |
2468 | $i++; | |
2469 | } | |
2470 | ||
2471 | delete $newconf->{lock}; | |
2472 | PVE::QemuConfig->write_config($newid, $newconf); | |
2473 | ||
2474 | if ($target) { | |
2475 | # always deactivate volumes - avoid lvm LVs to be active on several nodes | |
2476 | PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running; | |
2477 | PVE::Storage::deactivate_volumes($storecfg, $newvollist); | |
2478 | ||
2479 | my $newconffile = PVE::QemuConfig->config_file($newid, $target); | |
2480 | die "Failed to move config to node '$target' - rename failed: $!\n" | |
2481 | if !rename($conffile, $newconffile); | |
2482 | } | |
2483 | ||
2484 | PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool; | |
2485 | }; | |
2486 | if (my $err = $@) { | |
2487 | unlink $conffile; | |
2488 | ||
2489 | eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs) }; | |
2490 | ||
2491 | sleep 1; # some storage like rbd need to wait before release volume - really? | |
2492 | ||
2493 | foreach my $volid (@$newvollist) { | |
2494 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
2495 | warn $@ if $@; | |
2496 | } | |
2497 | die "clone failed: $err"; | |
2498 | } | |
2499 | ||
2500 | return; | |
2501 | }; | |
2502 | ||
2503 | PVE::Firewall::clone_vmfw_conf($vmid, $newid); | |
2504 | ||
2505 | return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $realcmd); | |
2506 | }; | |
2507 | ||
2508 | return PVE::QemuConfig->lock_config_mode($vmid, 1, $shared_lock, sub { | |
2509 | # Aquire exclusive lock lock for $newid | |
2510 | return PVE::QemuConfig->lock_config_full($newid, 1, $clonefn); | |
2511 | }); | |
2512 | ||
2513 | }}); | |
2514 | ||
2515 | __PACKAGE__->register_method({ | |
2516 | name => 'move_vm_disk', | |
2517 | path => '{vmid}/move_disk', | |
2518 | method => 'POST', | |
2519 | protected => 1, | |
2520 | proxyto => 'node', | |
2521 | description => "Move volume to different storage.", | |
2522 | permissions => { | |
2523 | description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, and 'Datastore.AllocateSpace' permissions on the storage.", | |
2524 | check => [ 'and', | |
2525 | ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
2526 | ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]], | |
2527 | ], | |
2528 | }, | |
2529 | parameters => { | |
2530 | additionalProperties => 0, | |
2531 | properties => { | |
2532 | node => get_standard_option('pve-node'), | |
2533 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
2534 | disk => { | |
2535 | type => 'string', | |
2536 | description => "The disk you want to move.", | |
2537 | enum => [ PVE::QemuServer::valid_drive_names() ], | |
2538 | }, | |
2539 | storage => get_standard_option('pve-storage-id', { | |
2540 | description => "Target storage.", | |
2541 | completion => \&PVE::QemuServer::complete_storage, | |
2542 | }), | |
2543 | 'format' => { | |
2544 | type => 'string', | |
2545 | description => "Target Format.", | |
2546 | enum => [ 'raw', 'qcow2', 'vmdk' ], | |
2547 | optional => 1, | |
2548 | }, | |
2549 | delete => { | |
2550 | type => 'boolean', | |
2551 | description => "Delete the original disk after successful copy. By default the original disk is kept as unused disk.", | |
2552 | optional => 1, | |
2553 | default => 0, | |
2554 | }, | |
2555 | digest => { | |
2556 | type => 'string', | |
2557 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
2558 | maxLength => 40, | |
2559 | optional => 1, | |
2560 | }, | |
2561 | }, | |
2562 | }, | |
2563 | returns => { | |
2564 | type => 'string', | |
2565 | description => "the task ID.", | |
2566 | }, | |
2567 | code => sub { | |
2568 | my ($param) = @_; | |
2569 | ||
2570 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2571 | ||
2572 | my $authuser = $rpcenv->get_user(); | |
2573 | ||
2574 | my $node = extract_param($param, 'node'); | |
2575 | ||
2576 | my $vmid = extract_param($param, 'vmid'); | |
2577 | ||
2578 | my $digest = extract_param($param, 'digest'); | |
2579 | ||
2580 | my $disk = extract_param($param, 'disk'); | |
2581 | ||
2582 | my $storeid = extract_param($param, 'storage'); | |
2583 | ||
2584 | my $format = extract_param($param, 'format'); | |
2585 | ||
2586 | my $storecfg = PVE::Storage::config(); | |
2587 | ||
2588 | my $updatefn = sub { | |
2589 | ||
2590 | my $conf = PVE::QemuConfig->load_config($vmid); | |
2591 | ||
2592 | PVE::QemuConfig->check_lock($conf); | |
2593 | ||
2594 | die "checksum missmatch (file change by other user?)\n" | |
2595 | if $digest && $digest ne $conf->{digest}; | |
2596 | ||
2597 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; | |
2598 | ||
2599 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
2600 | ||
2601 | my $old_volid = $drive->{file} || die "disk '$disk' has no associated volume\n"; | |
2602 | ||
2603 | die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
2604 | ||
2605 | my $oldfmt; | |
2606 | my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($old_volid); | |
2607 | if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){ | |
2608 | $oldfmt = $1; | |
2609 | } | |
2610 | ||
2611 | die "you can't move on the same storage with same format\n" if $oldstoreid eq $storeid && | |
2612 | (!$format || !$oldfmt || $oldfmt eq $format); | |
2613 | ||
2614 | # this only checks snapshots because $disk is passed! | |
2615 | my $snapshotted = PVE::QemuServer::is_volume_in_use($storecfg, $conf, $disk, $old_volid); | |
2616 | die "you can't move a disk with snapshots and delete the source\n" | |
2617 | if $snapshotted && $param->{delete}; | |
2618 | ||
2619 | PVE::Cluster::log_msg('info', $authuser, "move disk VM $vmid: move --disk $disk --storage $storeid"); | |
2620 | ||
2621 | my $running = PVE::QemuServer::check_running($vmid); | |
2622 | ||
2623 | PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]); | |
2624 | ||
2625 | my $realcmd = sub { | |
2626 | ||
2627 | my $newvollist = []; | |
2628 | ||
2629 | eval { | |
2630 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
2631 | ||
2632 | warn "moving disk with snapshots, snapshots will not be moved!\n" | |
2633 | if $snapshotted; | |
2634 | ||
2635 | my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $disk, $drive, undef, | |
2636 | $vmid, $storeid, $format, 1, $newvollist); | |
2637 | ||
2638 | $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $newdrive); | |
2639 | ||
2640 | PVE::QemuConfig->add_unused_volume($conf, $old_volid) if !$param->{delete}; | |
2641 | ||
2642 | PVE::QemuConfig->write_config($vmid, $conf); | |
2643 | ||
2644 | eval { | |
2645 | # try to deactivate volumes - avoid lvm LVs to be active on several nodes | |
2646 | PVE::Storage::deactivate_volumes($storecfg, [ $newdrive->{file} ]) | |
2647 | if !$running; | |
2648 | }; | |
2649 | warn $@ if $@; | |
2650 | }; | |
2651 | if (my $err = $@) { | |
2652 | ||
2653 | foreach my $volid (@$newvollist) { | |
2654 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
2655 | warn $@ if $@; | |
2656 | } | |
2657 | die "storage migration failed: $err"; | |
2658 | } | |
2659 | ||
2660 | if ($param->{delete}) { | |
2661 | eval { | |
2662 | PVE::Storage::deactivate_volumes($storecfg, [$old_volid]); | |
2663 | PVE::Storage::vdisk_free($storecfg, $old_volid); | |
2664 | }; | |
2665 | warn $@ if $@; | |
2666 | } | |
2667 | }; | |
2668 | ||
2669 | return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd); | |
2670 | }; | |
2671 | ||
2672 | return PVE::QemuConfig->lock_config($vmid, $updatefn); | |
2673 | }}); | |
2674 | ||
2675 | __PACKAGE__->register_method({ | |
2676 | name => 'migrate_vm', | |
2677 | path => '{vmid}/migrate', | |
2678 | method => 'POST', | |
2679 | protected => 1, | |
2680 | proxyto => 'node', | |
2681 | description => "Migrate virtual machine. Creates a new migration task.", | |
2682 | permissions => { | |
2683 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], | |
2684 | }, | |
2685 | parameters => { | |
2686 | additionalProperties => 0, | |
2687 | properties => { | |
2688 | node => get_standard_option('pve-node'), | |
2689 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
2690 | target => get_standard_option('pve-node', { | |
2691 | description => "Target node.", | |
2692 | completion => \&PVE::Cluster::complete_migration_target, | |
2693 | }), | |
2694 | online => { | |
2695 | type => 'boolean', | |
2696 | description => "Use online/live migration.", | |
2697 | optional => 1, | |
2698 | }, | |
2699 | force => { | |
2700 | type => 'boolean', | |
2701 | description => "Allow to migrate VMs which use local devices. Only root may use this option.", | |
2702 | optional => 1, | |
2703 | }, | |
2704 | migration_type => { | |
2705 | type => 'string', | |
2706 | enum => ['secure', 'insecure'], | |
2707 | description => "Migration traffic is encrypted using an SSH tunnel by default. On secure, completely private networks this can be disabled to increase performance.", | |
2708 | optional => 1, | |
2709 | }, | |
2710 | migration_network => { | |
2711 | type => 'string', format => 'CIDR', | |
2712 | description => "CIDR of the (sub) network that is used for migration.", | |
2713 | optional => 1, | |
2714 | }, | |
2715 | }, | |
2716 | }, | |
2717 | returns => { | |
2718 | type => 'string', | |
2719 | description => "the task ID.", | |
2720 | }, | |
2721 | code => sub { | |
2722 | my ($param) = @_; | |
2723 | ||
2724 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2725 | ||
2726 | my $authuser = $rpcenv->get_user(); | |
2727 | ||
2728 | my $target = extract_param($param, 'target'); | |
2729 | ||
2730 | my $localnode = PVE::INotify::nodename(); | |
2731 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
2732 | ||
2733 | PVE::Cluster::check_cfs_quorum(); | |
2734 | ||
2735 | PVE::Cluster::check_node_exists($target); | |
2736 | ||
2737 | my $targetip = PVE::Cluster::remote_node_ip($target); | |
2738 | ||
2739 | my $vmid = extract_param($param, 'vmid'); | |
2740 | ||
2741 | raise_param_exc({ force => "Only root may use this option." }) | |
2742 | if $param->{force} && $authuser ne 'root@pam'; | |
2743 | ||
2744 | raise_param_exc({ migration_type => "Only root may use this option." }) | |
2745 | if $param->{migration_type} && $authuser ne 'root@pam'; | |
2746 | ||
2747 | # allow root only until better network permissions are available | |
2748 | raise_param_exc({ migration_network => "Only root may use this option." }) | |
2749 | if $param->{migration_network} && $authuser ne 'root@pam'; | |
2750 | ||
2751 | # test if VM exists | |
2752 | my $conf = PVE::QemuConfig->load_config($vmid); | |
2753 | ||
2754 | # try to detect errors early | |
2755 | ||
2756 | PVE::QemuConfig->check_lock($conf); | |
2757 | ||
2758 | if (PVE::QemuServer::check_running($vmid)) { | |
2759 | die "cant migrate running VM without --online\n" | |
2760 | if !$param->{online}; | |
2761 | } | |
2762 | ||
2763 | my $storecfg = PVE::Storage::config(); | |
2764 | PVE::QemuServer::check_storage_availability($storecfg, $conf, $target); | |
2765 | ||
2766 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { | |
2767 | ||
2768 | my $hacmd = sub { | |
2769 | my $upid = shift; | |
2770 | ||
2771 | my $service = "vm:$vmid"; | |
2772 | ||
2773 | my $cmd = ['ha-manager', 'migrate', $service, $target]; | |
2774 | ||
2775 | print "Executing HA migrate for VM $vmid to node $target\n"; | |
2776 | ||
2777 | PVE::Tools::run_command($cmd); | |
2778 | ||
2779 | return; | |
2780 | }; | |
2781 | ||
2782 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); | |
2783 | ||
2784 | } else { | |
2785 | ||
2786 | my $realcmd = sub { | |
2787 | my $upid = shift; | |
2788 | ||
2789 | PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param); | |
2790 | }; | |
2791 | ||
2792 | return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd); | |
2793 | } | |
2794 | ||
2795 | }}); | |
2796 | ||
2797 | __PACKAGE__->register_method({ | |
2798 | name => 'monitor', | |
2799 | path => '{vmid}/monitor', | |
2800 | method => 'POST', | |
2801 | protected => 1, | |
2802 | proxyto => 'node', | |
2803 | description => "Execute Qemu monitor commands.", | |
2804 | permissions => { | |
2805 | description => "Sys.Modify is required for (sub)commands which are not read-only ('info *' and 'help')", | |
2806 | check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]], | |
2807 | }, | |
2808 | parameters => { | |
2809 | additionalProperties => 0, | |
2810 | properties => { | |
2811 | node => get_standard_option('pve-node'), | |
2812 | vmid => get_standard_option('pve-vmid'), | |
2813 | command => { | |
2814 | type => 'string', | |
2815 | description => "The monitor command.", | |
2816 | } | |
2817 | }, | |
2818 | }, | |
2819 | returns => { type => 'string'}, | |
2820 | code => sub { | |
2821 | my ($param) = @_; | |
2822 | ||
2823 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2824 | my $authuser = $rpcenv->get_user(); | |
2825 | ||
2826 | my $is_ro = sub { | |
2827 | my $command = shift; | |
2828 | return $command =~ m/^\s*info(\s+|$)/ | |
2829 | || $command =~ m/^\s*help\s*$/; | |
2830 | }; | |
2831 | ||
2832 | $rpcenv->check_full($authuser, "/", ['Sys.Modify']) | |
2833 | if !&$is_ro($param->{command}); | |
2834 | ||
2835 | my $vmid = $param->{vmid}; | |
2836 | ||
2837 | my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists | |
2838 | ||
2839 | my $res = ''; | |
2840 | eval { | |
2841 | $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command}); | |
2842 | }; | |
2843 | $res = "ERROR: $@" if $@; | |
2844 | ||
2845 | return $res; | |
2846 | }}); | |
2847 | ||
2848 | my $guest_agent_commands = [ | |
2849 | 'ping', | |
2850 | 'get-time', | |
2851 | 'info', | |
2852 | 'fsfreeze-status', | |
2853 | 'fsfreeze-freeze', | |
2854 | 'fsfreeze-thaw', | |
2855 | 'fstrim', | |
2856 | 'network-get-interfaces', | |
2857 | 'get-vcpus', | |
2858 | 'get-fsinfo', | |
2859 | 'get-memory-blocks', | |
2860 | 'get-memory-block-info', | |
2861 | 'suspend-hybrid', | |
2862 | 'suspend-ram', | |
2863 | 'suspend-disk', | |
2864 | 'shutdown', | |
2865 | ]; | |
2866 | ||
2867 | __PACKAGE__->register_method({ | |
2868 | name => 'agent', | |
2869 | path => '{vmid}/agent', | |
2870 | method => 'POST', | |
2871 | protected => 1, | |
2872 | proxyto => 'node', | |
2873 | description => "Execute Qemu Guest Agent commands.", | |
2874 | permissions => { | |
2875 | check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]], | |
2876 | }, | |
2877 | parameters => { | |
2878 | additionalProperties => 0, | |
2879 | properties => { | |
2880 | node => get_standard_option('pve-node'), | |
2881 | vmid => get_standard_option('pve-vmid', { | |
2882 | completion => \&PVE::QemuServer::complete_vmid_running }), | |
2883 | command => { | |
2884 | type => 'string', | |
2885 | description => "The QGA command.", | |
2886 | enum => $guest_agent_commands, | |
2887 | }, | |
2888 | }, | |
2889 | }, | |
2890 | returns => { | |
2891 | type => 'object', | |
2892 | description => "Returns an object with a single `result` property. The type of that | |
2893 | property depends on the executed command.", | |
2894 | }, | |
2895 | code => sub { | |
2896 | my ($param) = @_; | |
2897 | ||
2898 | my $vmid = $param->{vmid}; | |
2899 | ||
2900 | my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists | |
2901 | ||
2902 | die "No Qemu Guest Agent\n" if !defined($conf->{agent}); | |
2903 | die "VM $vmid is not running\n" if !PVE::QemuServer::check_running($vmid); | |
2904 | ||
2905 | my $cmd = $param->{command}; | |
2906 | ||
2907 | my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-$cmd"); | |
2908 | ||
2909 | return { result => $res }; | |
2910 | }}); | |
2911 | ||
2912 | __PACKAGE__->register_method({ | |
2913 | name => 'resize_vm', | |
2914 | path => '{vmid}/resize', | |
2915 | method => 'PUT', | |
2916 | protected => 1, | |
2917 | proxyto => 'node', | |
2918 | description => "Extend volume size.", | |
2919 | permissions => { | |
2920 | check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
2921 | }, | |
2922 | parameters => { | |
2923 | additionalProperties => 0, | |
2924 | properties => { | |
2925 | node => get_standard_option('pve-node'), | |
2926 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
2927 | skiplock => get_standard_option('skiplock'), | |
2928 | disk => { | |
2929 | type => 'string', | |
2930 | description => "The disk you want to resize.", | |
2931 | enum => [PVE::QemuServer::valid_drive_names()], | |
2932 | }, | |
2933 | size => { | |
2934 | type => 'string', | |
2935 | pattern => '\+?\d+(\.\d+)?[KMGT]?', | |
2936 | 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.", | |
2937 | }, | |
2938 | digest => { | |
2939 | type => 'string', | |
2940 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
2941 | maxLength => 40, | |
2942 | optional => 1, | |
2943 | }, | |
2944 | }, | |
2945 | }, | |
2946 | returns => { type => 'null'}, | |
2947 | code => sub { | |
2948 | my ($param) = @_; | |
2949 | ||
2950 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2951 | ||
2952 | my $authuser = $rpcenv->get_user(); | |
2953 | ||
2954 | my $node = extract_param($param, 'node'); | |
2955 | ||
2956 | my $vmid = extract_param($param, 'vmid'); | |
2957 | ||
2958 | my $digest = extract_param($param, 'digest'); | |
2959 | ||
2960 | my $disk = extract_param($param, 'disk'); | |
2961 | ||
2962 | my $sizestr = extract_param($param, 'size'); | |
2963 | ||
2964 | my $skiplock = extract_param($param, 'skiplock'); | |
2965 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
2966 | if $skiplock && $authuser ne 'root@pam'; | |
2967 | ||
2968 | my $storecfg = PVE::Storage::config(); | |
2969 | ||
2970 | my $updatefn = sub { | |
2971 | ||
2972 | my $conf = PVE::QemuConfig->load_config($vmid); | |
2973 | ||
2974 | die "checksum missmatch (file change by other user?)\n" | |
2975 | if $digest && $digest ne $conf->{digest}; | |
2976 | PVE::QemuConfig->check_lock($conf) if !$skiplock; | |
2977 | ||
2978 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; | |
2979 | ||
2980 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
2981 | ||
2982 | my (undef, undef, undef, undef, undef, undef, $format) = | |
2983 | PVE::Storage::parse_volname($storecfg, $drive->{file}); | |
2984 | ||
2985 | die "can't resize volume: $disk if snapshot exists\n" | |
2986 | if %{$conf->{snapshots}} && $format eq 'qcow2'; | |
2987 | ||
2988 | my $volid = $drive->{file}; | |
2989 | ||
2990 | die "disk '$disk' has no associated volume\n" if !$volid; | |
2991 | ||
2992 | die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
2993 | ||
2994 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
2995 | ||
2996 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
2997 | ||
2998 | PVE::Storage::activate_volumes($storecfg, [$volid]); | |
2999 | my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5); | |
3000 | ||
3001 | die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/; | |
3002 | my ($ext, $newsize, $unit) = ($1, $2, $4); | |
3003 | if ($unit) { | |
3004 | if ($unit eq 'K') { | |
3005 | $newsize = $newsize * 1024; | |
3006 | } elsif ($unit eq 'M') { | |
3007 | $newsize = $newsize * 1024 * 1024; | |
3008 | } elsif ($unit eq 'G') { | |
3009 | $newsize = $newsize * 1024 * 1024 * 1024; | |
3010 | } elsif ($unit eq 'T') { | |
3011 | $newsize = $newsize * 1024 * 1024 * 1024 * 1024; | |
3012 | } | |
3013 | } | |
3014 | $newsize += $size if $ext; | |
3015 | $newsize = int($newsize); | |
3016 | ||
3017 | die "unable to skrink disk size\n" if $newsize < $size; | |
3018 | ||
3019 | return if $size == $newsize; | |
3020 | ||
3021 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr"); | |
3022 | ||
3023 | PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize); | |
3024 | ||
3025 | $drive->{size} = $newsize; | |
3026 | $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive); | |
3027 | ||
3028 | PVE::QemuConfig->write_config($vmid, $conf); | |
3029 | }; | |
3030 | ||
3031 | PVE::QemuConfig->lock_config($vmid, $updatefn); | |
3032 | return undef; | |
3033 | }}); | |
3034 | ||
3035 | __PACKAGE__->register_method({ | |
3036 | name => 'snapshot_list', | |
3037 | path => '{vmid}/snapshot', | |
3038 | method => 'GET', | |
3039 | description => "List all snapshots.", | |
3040 | permissions => { | |
3041 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
3042 | }, | |
3043 | proxyto => 'node', | |
3044 | protected => 1, # qemu pid files are only readable by root | |
3045 | parameters => { | |
3046 | additionalProperties => 0, | |
3047 | properties => { | |
3048 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
3049 | node => get_standard_option('pve-node'), | |
3050 | }, | |
3051 | }, | |
3052 | returns => { | |
3053 | type => 'array', | |
3054 | items => { | |
3055 | type => "object", | |
3056 | properties => {}, | |
3057 | }, | |
3058 | links => [ { rel => 'child', href => "{name}" } ], | |
3059 | }, | |
3060 | code => sub { | |
3061 | my ($param) = @_; | |
3062 | ||
3063 | my $vmid = $param->{vmid}; | |
3064 | ||
3065 | my $conf = PVE::QemuConfig->load_config($vmid); | |
3066 | my $snaphash = $conf->{snapshots} || {}; | |
3067 | ||
3068 | my $res = []; | |
3069 | ||
3070 | foreach my $name (keys %$snaphash) { | |
3071 | my $d = $snaphash->{$name}; | |
3072 | my $item = { | |
3073 | name => $name, | |
3074 | snaptime => $d->{snaptime} || 0, | |
3075 | vmstate => $d->{vmstate} ? 1 : 0, | |
3076 | description => $d->{description} || '', | |
3077 | }; | |
3078 | $item->{parent} = $d->{parent} if $d->{parent}; | |
3079 | $item->{snapstate} = $d->{snapstate} if $d->{snapstate}; | |
3080 | push @$res, $item; | |
3081 | } | |
3082 | ||
3083 | my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0; | |
3084 | my $current = { name => 'current', digest => $conf->{digest}, running => $running }; | |
3085 | $current->{parent} = $conf->{parent} if $conf->{parent}; | |
3086 | ||
3087 | push @$res, $current; | |
3088 | ||
3089 | return $res; | |
3090 | }}); | |
3091 | ||
3092 | __PACKAGE__->register_method({ | |
3093 | name => 'snapshot', | |
3094 | path => '{vmid}/snapshot', | |
3095 | method => 'POST', | |
3096 | protected => 1, | |
3097 | proxyto => 'node', | |
3098 | description => "Snapshot a VM.", | |
3099 | permissions => { | |
3100 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
3101 | }, | |
3102 | parameters => { | |
3103 | additionalProperties => 0, | |
3104 | properties => { | |
3105 | node => get_standard_option('pve-node'), | |
3106 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
3107 | snapname => get_standard_option('pve-snapshot-name'), | |
3108 | vmstate => { | |
3109 | optional => 1, | |
3110 | type => 'boolean', | |
3111 | description => "Save the vmstate", | |
3112 | }, | |
3113 | description => { | |
3114 | optional => 1, | |
3115 | type => 'string', | |
3116 | description => "A textual description or comment.", | |
3117 | }, | |
3118 | }, | |
3119 | }, | |
3120 | returns => { | |
3121 | type => 'string', | |
3122 | description => "the task ID.", | |
3123 | }, | |
3124 | code => sub { | |
3125 | my ($param) = @_; | |
3126 | ||
3127 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3128 | ||
3129 | my $authuser = $rpcenv->get_user(); | |
3130 | ||
3131 | my $node = extract_param($param, 'node'); | |
3132 | ||
3133 | my $vmid = extract_param($param, 'vmid'); | |
3134 | ||
3135 | my $snapname = extract_param($param, 'snapname'); | |
3136 | ||
3137 | die "unable to use snapshot name 'current' (reserved name)\n" | |
3138 | if $snapname eq 'current'; | |
3139 | ||
3140 | my $realcmd = sub { | |
3141 | PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname"); | |
3142 | PVE::QemuConfig->snapshot_create($vmid, $snapname, $param->{vmstate}, | |
3143 | $param->{description}); | |
3144 | }; | |
3145 | ||
3146 | return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd); | |
3147 | }}); | |
3148 | ||
3149 | __PACKAGE__->register_method({ | |
3150 | name => 'snapshot_cmd_idx', | |
3151 | path => '{vmid}/snapshot/{snapname}', | |
3152 | description => '', | |
3153 | method => 'GET', | |
3154 | permissions => { | |
3155 | user => 'all', | |
3156 | }, | |
3157 | parameters => { | |
3158 | additionalProperties => 0, | |
3159 | properties => { | |
3160 | vmid => get_standard_option('pve-vmid'), | |
3161 | node => get_standard_option('pve-node'), | |
3162 | snapname => get_standard_option('pve-snapshot-name'), | |
3163 | }, | |
3164 | }, | |
3165 | returns => { | |
3166 | type => 'array', | |
3167 | items => { | |
3168 | type => "object", | |
3169 | properties => {}, | |
3170 | }, | |
3171 | links => [ { rel => 'child', href => "{cmd}" } ], | |
3172 | }, | |
3173 | code => sub { | |
3174 | my ($param) = @_; | |
3175 | ||
3176 | my $res = []; | |
3177 | ||
3178 | push @$res, { cmd => 'rollback' }; | |
3179 | push @$res, { cmd => 'config' }; | |
3180 | ||
3181 | return $res; | |
3182 | }}); | |
3183 | ||
3184 | __PACKAGE__->register_method({ | |
3185 | name => 'update_snapshot_config', | |
3186 | path => '{vmid}/snapshot/{snapname}/config', | |
3187 | method => 'PUT', | |
3188 | protected => 1, | |
3189 | proxyto => 'node', | |
3190 | description => "Update snapshot metadata.", | |
3191 | permissions => { | |
3192 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
3193 | }, | |
3194 | parameters => { | |
3195 | additionalProperties => 0, | |
3196 | properties => { | |
3197 | node => get_standard_option('pve-node'), | |
3198 | vmid => get_standard_option('pve-vmid'), | |
3199 | snapname => get_standard_option('pve-snapshot-name'), | |
3200 | description => { | |
3201 | optional => 1, | |
3202 | type => 'string', | |
3203 | description => "A textual description or comment.", | |
3204 | }, | |
3205 | }, | |
3206 | }, | |
3207 | returns => { type => 'null' }, | |
3208 | code => sub { | |
3209 | my ($param) = @_; | |
3210 | ||
3211 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3212 | ||
3213 | my $authuser = $rpcenv->get_user(); | |
3214 | ||
3215 | my $vmid = extract_param($param, 'vmid'); | |
3216 | ||
3217 | my $snapname = extract_param($param, 'snapname'); | |
3218 | ||
3219 | return undef if !defined($param->{description}); | |
3220 | ||
3221 | my $updatefn = sub { | |
3222 | ||
3223 | my $conf = PVE::QemuConfig->load_config($vmid); | |
3224 | ||
3225 | PVE::QemuConfig->check_lock($conf); | |
3226 | ||
3227 | my $snap = $conf->{snapshots}->{$snapname}; | |
3228 | ||
3229 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
3230 | ||
3231 | $snap->{description} = $param->{description} if defined($param->{description}); | |
3232 | ||
3233 | PVE::QemuConfig->write_config($vmid, $conf); | |
3234 | }; | |
3235 | ||
3236 | PVE::QemuConfig->lock_config($vmid, $updatefn); | |
3237 | ||
3238 | return undef; | |
3239 | }}); | |
3240 | ||
3241 | __PACKAGE__->register_method({ | |
3242 | name => 'get_snapshot_config', | |
3243 | path => '{vmid}/snapshot/{snapname}/config', | |
3244 | method => 'GET', | |
3245 | proxyto => 'node', | |
3246 | description => "Get snapshot configuration", | |
3247 | permissions => { | |
3248 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
3249 | }, | |
3250 | parameters => { | |
3251 | additionalProperties => 0, | |
3252 | properties => { | |
3253 | node => get_standard_option('pve-node'), | |
3254 | vmid => get_standard_option('pve-vmid'), | |
3255 | snapname => get_standard_option('pve-snapshot-name'), | |
3256 | }, | |
3257 | }, | |
3258 | returns => { type => "object" }, | |
3259 | code => sub { | |
3260 | my ($param) = @_; | |
3261 | ||
3262 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3263 | ||
3264 | my $authuser = $rpcenv->get_user(); | |
3265 | ||
3266 | my $vmid = extract_param($param, 'vmid'); | |
3267 | ||
3268 | my $snapname = extract_param($param, 'snapname'); | |
3269 | ||
3270 | my $conf = PVE::QemuConfig->load_config($vmid); | |
3271 | ||
3272 | my $snap = $conf->{snapshots}->{$snapname}; | |
3273 | ||
3274 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
3275 | ||
3276 | return $snap; | |
3277 | }}); | |
3278 | ||
3279 | __PACKAGE__->register_method({ | |
3280 | name => 'rollback', | |
3281 | path => '{vmid}/snapshot/{snapname}/rollback', | |
3282 | method => 'POST', | |
3283 | protected => 1, | |
3284 | proxyto => 'node', | |
3285 | description => "Rollback VM state to specified snapshot.", | |
3286 | permissions => { | |
3287 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
3288 | }, | |
3289 | parameters => { | |
3290 | additionalProperties => 0, | |
3291 | properties => { | |
3292 | node => get_standard_option('pve-node'), | |
3293 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
3294 | snapname => get_standard_option('pve-snapshot-name'), | |
3295 | }, | |
3296 | }, | |
3297 | returns => { | |
3298 | type => 'string', | |
3299 | description => "the task ID.", | |
3300 | }, | |
3301 | code => sub { | |
3302 | my ($param) = @_; | |
3303 | ||
3304 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3305 | ||
3306 | my $authuser = $rpcenv->get_user(); | |
3307 | ||
3308 | my $node = extract_param($param, 'node'); | |
3309 | ||
3310 | my $vmid = extract_param($param, 'vmid'); | |
3311 | ||
3312 | my $snapname = extract_param($param, 'snapname'); | |
3313 | ||
3314 | my $realcmd = sub { | |
3315 | PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname"); | |
3316 | PVE::QemuConfig->snapshot_rollback($vmid, $snapname); | |
3317 | }; | |
3318 | ||
3319 | return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd); | |
3320 | }}); | |
3321 | ||
3322 | __PACKAGE__->register_method({ | |
3323 | name => 'delsnapshot', | |
3324 | path => '{vmid}/snapshot/{snapname}', | |
3325 | method => 'DELETE', | |
3326 | protected => 1, | |
3327 | proxyto => 'node', | |
3328 | description => "Delete a VM snapshot.", | |
3329 | permissions => { | |
3330 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
3331 | }, | |
3332 | parameters => { | |
3333 | additionalProperties => 0, | |
3334 | properties => { | |
3335 | node => get_standard_option('pve-node'), | |
3336 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
3337 | snapname => get_standard_option('pve-snapshot-name'), | |
3338 | force => { | |
3339 | optional => 1, | |
3340 | type => 'boolean', | |
3341 | description => "For removal from config file, even if removing disk snapshots fails.", | |
3342 | }, | |
3343 | }, | |
3344 | }, | |
3345 | returns => { | |
3346 | type => 'string', | |
3347 | description => "the task ID.", | |
3348 | }, | |
3349 | code => sub { | |
3350 | my ($param) = @_; | |
3351 | ||
3352 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3353 | ||
3354 | my $authuser = $rpcenv->get_user(); | |
3355 | ||
3356 | my $node = extract_param($param, 'node'); | |
3357 | ||
3358 | my $vmid = extract_param($param, 'vmid'); | |
3359 | ||
3360 | my $snapname = extract_param($param, 'snapname'); | |
3361 | ||
3362 | my $realcmd = sub { | |
3363 | PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname"); | |
3364 | PVE::QemuConfig->snapshot_delete($vmid, $snapname, $param->{force}); | |
3365 | }; | |
3366 | ||
3367 | return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd); | |
3368 | }}); | |
3369 | ||
3370 | __PACKAGE__->register_method({ | |
3371 | name => 'template', | |
3372 | path => '{vmid}/template', | |
3373 | method => 'POST', | |
3374 | protected => 1, | |
3375 | proxyto => 'node', | |
3376 | description => "Create a Template.", | |
3377 | permissions => { | |
3378 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", | |
3379 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
3380 | }, | |
3381 | parameters => { | |
3382 | additionalProperties => 0, | |
3383 | properties => { | |
3384 | node => get_standard_option('pve-node'), | |
3385 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }), | |
3386 | disk => { | |
3387 | optional => 1, | |
3388 | type => 'string', | |
3389 | description => "If you want to convert only 1 disk to base image.", | |
3390 | enum => [PVE::QemuServer::valid_drive_names()], | |
3391 | }, | |
3392 | ||
3393 | }, | |
3394 | }, | |
3395 | returns => { type => 'null'}, | |
3396 | code => sub { | |
3397 | my ($param) = @_; | |
3398 | ||
3399 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3400 | ||
3401 | my $authuser = $rpcenv->get_user(); | |
3402 | ||
3403 | my $node = extract_param($param, 'node'); | |
3404 | ||
3405 | my $vmid = extract_param($param, 'vmid'); | |
3406 | ||
3407 | my $disk = extract_param($param, 'disk'); | |
3408 | ||
3409 | my $updatefn = sub { | |
3410 | ||
3411 | my $conf = PVE::QemuConfig->load_config($vmid); | |
3412 | ||
3413 | PVE::QemuConfig->check_lock($conf); | |
3414 | ||
3415 | die "unable to create template, because VM contains snapshots\n" | |
3416 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); | |
3417 | ||
3418 | die "you can't convert a template to a template\n" | |
3419 | if PVE::QemuConfig->is_template($conf) && !$disk; | |
3420 | ||
3421 | die "you can't convert a VM to template if VM is running\n" | |
3422 | if PVE::QemuServer::check_running($vmid); | |
3423 | ||
3424 | my $realcmd = sub { | |
3425 | PVE::QemuServer::template_create($vmid, $conf, $disk); | |
3426 | }; | |
3427 | ||
3428 | $conf->{template} = 1; | |
3429 | PVE::QemuConfig->write_config($vmid, $conf); | |
3430 | ||
3431 | return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd); | |
3432 | }; | |
3433 | ||
3434 | PVE::QemuConfig->lock_config($vmid, $updatefn); | |
3435 | return undef; | |
3436 | }}); | |
3437 | ||
3438 | 1; |