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