]>
Commit | Line | Data |
---|---|---|
1e3baf05 DM |
1 | package PVE::API2::Qemu; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5b9d692a | 5 | use Cwd 'abs_path'; |
451b2b81 | 6 | use Net::SSLeay; |
655d7462 WB |
7 | use POSIX; |
8 | use IO::Socket::IP; | |
0c9a7596 | 9 | use URI::Escape; |
3c5bdde8 | 10 | use Crypt::OpenSSL::Random; |
1e3baf05 | 11 | |
502d18a2 | 12 | use PVE::Cluster qw (cfs_read_file cfs_write_file);; |
95896f80 | 13 | use PVE::RRD; |
1e3baf05 DM |
14 | use PVE::SafeSyslog; |
15 | use PVE::Tools qw(extract_param); | |
f9bfceef | 16 | use PVE::Exception qw(raise raise_param_exc raise_perm_exc); |
1e3baf05 DM |
17 | use PVE::Storage; |
18 | use PVE::JSONSchema qw(get_standard_option); | |
19 | use PVE::RESTHandler; | |
628bb7f2 | 20 | use PVE::ReplicationConfig; |
95f42d61 | 21 | use PVE::GuestHelpers; |
ffda963f | 22 | use PVE::QemuConfig; |
1e3baf05 | 23 | use PVE::QemuServer; |
6e72f90b | 24 | use PVE::QemuServer::CPUConfig; |
e6ac9fed DJ |
25 | use PVE::QemuServer::Drive; |
26 | use PVE::QemuServer::ImportDisk; | |
0a13e08e | 27 | use PVE::QemuServer::Monitor qw(mon_cmd); |
90b20b15 | 28 | use PVE::QemuServer::Machine; |
3ea94c60 | 29 | use PVE::QemuMigrate; |
1e3baf05 DM |
30 | use PVE::RPCEnvironment; |
31 | use PVE::AccessControl; | |
32 | use PVE::INotify; | |
de8f60b2 | 33 | use PVE::Network; |
e9abcde6 | 34 | use PVE::Firewall; |
228a998b | 35 | use PVE::API2::Firewall::VM; |
b8158701 | 36 | use PVE::API2::Qemu::Agent; |
d9123ef5 | 37 | use PVE::VZDump::Plugin; |
48cf040f | 38 | use PVE::DataCenterConfig; |
f42ea29b | 39 | use PVE::SSHInfo; |
a9453218 | 40 | use PVE::Replication; |
9f11fc5f WB |
41 | |
42 | BEGIN { | |
43 | if (!$ENV{PVE_GENERATING_DOCS}) { | |
44 | require PVE::HA::Env::PVE2; | |
45 | import PVE::HA::Env::PVE2; | |
46 | require PVE::HA::Config; | |
47 | import PVE::HA::Config; | |
48 | } | |
49 | } | |
1e3baf05 DM |
50 | |
51 | use Data::Dumper; # fixme: remove | |
52 | ||
53 | use base qw(PVE::RESTHandler); | |
54 | ||
55 | 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."; | |
56 | ||
57 | my $resolve_cdrom_alias = sub { | |
58 | my $param = shift; | |
59 | ||
60 | if (my $value = $param->{cdrom}) { | |
61 | $value .= ",media=cdrom" if $value !~ m/media=/; | |
62 | $param->{ide2} = $value; | |
63 | delete $param->{cdrom}; | |
64 | } | |
65 | }; | |
66 | ||
c1accf9d FE |
67 | # Used in import-enabled API endpoints. Parses drives using the extended '_with_alloc' schema. |
68 | my $foreach_volume_with_alloc = sub { | |
69 | my ($param, $func) = @_; | |
70 | ||
71 | for my $opt (sort keys $param->%*) { | |
72 | next if !PVE::QemuServer::is_valid_drivename($opt); | |
73 | ||
74 | my $drive = PVE::QemuServer::Drive::parse_drive($opt, $param->{$opt}, 1); | |
75 | next if !$drive; | |
76 | ||
77 | $func->($opt, $drive); | |
78 | } | |
79 | }; | |
80 | ||
81 | my $NEW_DISK_RE = qr!^(([^/:\s]+):)?(\d+(\.\d+)?)$!; | |
82 | ||
7979bbcd FE |
83 | my $check_drive_param = sub { |
84 | my ($param, $storecfg, $extra_checks) = @_; | |
85 | ||
86 | for my $opt (sort keys $param->%*) { | |
87 | next if !PVE::QemuServer::is_valid_drivename($opt); | |
88 | ||
c1accf9d | 89 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}, 1); |
7979bbcd FE |
90 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; |
91 | ||
e6ac9fed DJ |
92 | if ($drive->{'import-from'}) { |
93 | if ($drive->{file} !~ $NEW_DISK_RE || $3 != 0) { | |
94 | raise_param_exc({ | |
95 | $opt => "'import-from' requires special syntax - ". | |
96 | "use <storage ID>:0,import-from=<source>", | |
97 | }); | |
98 | } | |
99 | ||
100 | if ($opt eq 'efidisk0') { | |
101 | for my $required (qw(efitype pre-enrolled-keys)) { | |
102 | if (!defined($drive->{$required})) { | |
103 | raise_param_exc({ | |
104 | $opt => "need to specify '$required' when using 'import-from'", | |
105 | }); | |
106 | } | |
107 | } | |
108 | } elsif ($opt eq 'tpmstate0') { | |
109 | raise_param_exc({ $opt => "need to specify 'version' when using 'import-from'" }) | |
110 | if !defined($drive->{version}); | |
111 | } | |
112 | } | |
113 | ||
7979bbcd FE |
114 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); |
115 | ||
116 | $extra_checks->($drive) if $extra_checks; | |
117 | ||
c1accf9d | 118 | $param->{$opt} = PVE::QemuServer::print_drive($drive, 1); |
7979bbcd FE |
119 | } |
120 | }; | |
121 | ||
ae57f6b3 | 122 | my $check_storage_access = sub { |
fcbb753e | 123 | my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_; |
a0d1b1a2 | 124 | |
c1accf9d | 125 | $foreach_volume_with_alloc->($settings, sub { |
ae57f6b3 | 126 | my ($ds, $drive) = @_; |
a0d1b1a2 | 127 | |
ae57f6b3 | 128 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); |
a0d1b1a2 | 129 | |
ae57f6b3 | 130 | my $volid = $drive->{file}; |
21e1ee7b | 131 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); |
a0d1b1a2 | 132 | |
8f2c9019 | 133 | if (!$volid || ($volid eq 'none' || $volid eq 'cloudinit' || (defined($volname) && $volname eq 'cloudinit'))) { |
09d0ee64 | 134 | # nothing to check |
f5782fd0 DM |
135 | } elsif ($isCDROM && ($volid eq 'cdrom')) { |
136 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
bf1312d8 | 137 | } elsif (!$isCDROM && ($volid =~ $NEW_DISK_RE)) { |
a0d1b1a2 DM |
138 | my ($storeid, $size) = ($2 || $default_storage, $3); |
139 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
fcbb753e | 140 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); |
c46366fd DC |
141 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); |
142 | raise_param_exc({ storage => "storage '$storeid' does not support vm images"}) | |
143 | if !$scfg->{content}->{images}; | |
a0d1b1a2 | 144 | } else { |
db81c007 FE |
145 | PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid); |
146 | if ($storeid) { | |
147 | my ($vtype) = PVE::Storage::parse_volname($storecfg, $volid); | |
148 | raise_param_exc({ $ds => "content type needs to be 'images' or 'iso'" }) | |
149 | if $vtype ne 'images' && $vtype ne 'iso'; | |
150 | } | |
a0d1b1a2 | 151 | } |
e6ac9fed DJ |
152 | |
153 | if (my $src_image = $drive->{'import-from'}) { | |
154 | my $src_vmid; | |
155 | if (PVE::Storage::parse_volume_id($src_image, 1)) { # PVE-managed volume | |
156 | (my $vtype, undef, $src_vmid) = PVE::Storage::parse_volname($storecfg, $src_image); | |
157 | raise_param_exc({ $ds => "$src_image has wrong type '$vtype' - not an image" }) | |
158 | if $vtype ne 'images'; | |
159 | } | |
160 | ||
161 | if ($src_vmid) { # might be actively used by VM and will be copied via clone_disk() | |
162 | $rpcenv->check($authuser, "/vms/${src_vmid}", ['VM.Clone']); | |
163 | } else { | |
164 | PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $src_image); | |
165 | } | |
166 | } | |
a0d1b1a2 | 167 | }); |
253624c7 FG |
168 | |
169 | $rpcenv->check($authuser, "/storage/$settings->{vmstatestorage}", ['Datastore.AllocateSpace']) | |
170 | if defined($settings->{vmstatestorage}); | |
ae57f6b3 | 171 | }; |
a0d1b1a2 | 172 | |
9418baad | 173 | my $check_storage_access_clone = sub { |
81f043eb | 174 | my ($rpcenv, $authuser, $storecfg, $conf, $storage) = @_; |
6116f729 | 175 | |
55173c6b DM |
176 | my $sharedvm = 1; |
177 | ||
912792e2 | 178 | PVE::QemuConfig->foreach_volume($conf, sub { |
6116f729 DM |
179 | my ($ds, $drive) = @_; |
180 | ||
181 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); | |
182 | ||
183 | my $volid = $drive->{file}; | |
184 | ||
185 | return if !$volid || $volid eq 'none'; | |
186 | ||
187 | if ($isCDROM) { | |
188 | if ($volid eq 'cdrom') { | |
189 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
190 | } else { | |
75466c4f | 191 | # we simply allow access |
55173c6b DM |
192 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); |
193 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
194 | $sharedvm = 0 if !$scfg->{shared}; | |
195 | ||
6116f729 DM |
196 | } |
197 | } else { | |
55173c6b DM |
198 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); |
199 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
200 | $sharedvm = 0 if !$scfg->{shared}; | |
201 | ||
81f043eb | 202 | $sid = $storage if $storage; |
6116f729 DM |
203 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.AllocateSpace']); |
204 | } | |
205 | }); | |
55173c6b | 206 | |
253624c7 FG |
207 | $rpcenv->check($authuser, "/storage/$conf->{vmstatestorage}", ['Datastore.AllocateSpace']) |
208 | if defined($conf->{vmstatestorage}); | |
209 | ||
55173c6b | 210 | return $sharedvm; |
6116f729 DM |
211 | }; |
212 | ||
9fb295d0 FG |
213 | my $check_storage_access_migrate = sub { |
214 | my ($rpcenv, $authuser, $storecfg, $storage, $node) = @_; | |
215 | ||
216 | PVE::Storage::storage_check_enabled($storecfg, $storage, $node); | |
217 | ||
218 | $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']); | |
219 | ||
220 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
221 | die "storage '$storage' does not support vm images\n" | |
222 | if !$scfg->{content}->{images}; | |
223 | }; | |
224 | ||
e6ac9fed DJ |
225 | my $import_from_volid = sub { |
226 | my ($storecfg, $src_volid, $dest_info, $vollist) = @_; | |
227 | ||
228 | die "could not get size of $src_volid\n" | |
229 | if !PVE::Storage::volume_size_info($storecfg, $src_volid, 10); | |
230 | ||
231 | die "cannot import from cloudinit disk\n" | |
232 | if PVE::QemuServer::Drive::drive_is_cloudinit({ file => $src_volid }); | |
233 | ||
234 | my $src_vmid = (PVE::Storage::parse_volname($storecfg, $src_volid))[2]; | |
235 | ||
236 | my $src_vm_state = sub { | |
237 | my $exists = $src_vmid && PVE::Cluster::get_vmlist()->{ids}->{$src_vmid} ? 1 : 0; | |
238 | ||
239 | my $runs = 0; | |
240 | if ($exists) { | |
241 | eval { PVE::QemuConfig::assert_config_exists_on_node($src_vmid); }; | |
242 | die "owner VM $src_vmid not on local node\n" if $@; | |
243 | $runs = PVE::QemuServer::Helpers::vm_running_locally($src_vmid) || 0; | |
244 | } | |
245 | ||
246 | return ($exists, $runs); | |
247 | }; | |
248 | ||
249 | my ($src_vm_exists, $running) = $src_vm_state->(); | |
250 | ||
251 | die "cannot import from '$src_volid' - full clone feature is not supported\n" | |
252 | if !PVE::Storage::volume_has_feature($storecfg, 'copy', $src_volid, undef, $running); | |
253 | ||
254 | my $clonefn = sub { | |
255 | my ($src_vm_exists_now, $running_now) = $src_vm_state->(); | |
256 | ||
257 | die "owner VM $src_vmid changed state unexpectedly\n" | |
258 | if $src_vm_exists_now != $src_vm_exists || $running_now != $running; | |
259 | ||
260 | my $src_conf = $src_vm_exists_now ? PVE::QemuConfig->load_config($src_vmid) : {}; | |
261 | ||
262 | my $src_drive = { file => $src_volid }; | |
263 | my $src_drivename; | |
264 | PVE::QemuConfig->foreach_volume($src_conf, sub { | |
265 | my ($ds, $drive) = @_; | |
266 | ||
267 | return if $src_drivename; | |
268 | ||
269 | if ($drive->{file} eq $src_volid) { | |
270 | $src_drive = $drive; | |
271 | $src_drivename = $ds; | |
272 | } | |
273 | }); | |
274 | ||
275 | my $source_info = { | |
276 | vmid => $src_vmid, | |
277 | running => $running_now, | |
278 | drivename => $src_drivename, | |
279 | drive => $src_drive, | |
280 | snapname => undef, | |
281 | }; | |
282 | ||
283 | my ($src_storeid) = PVE::Storage::parse_volume_id($src_volid); | |
284 | ||
285 | return PVE::QemuServer::clone_disk( | |
286 | $storecfg, | |
287 | $source_info, | |
288 | $dest_info, | |
289 | 1, | |
290 | $vollist, | |
291 | undef, | |
292 | undef, | |
293 | $src_conf->{agent}, | |
294 | PVE::Storage::get_bandwidth_limit('clone', [$src_storeid, $dest_info->{storage}]), | |
295 | ); | |
296 | }; | |
297 | ||
298 | my $cloned; | |
299 | if ($running) { | |
300 | $cloned = PVE::QemuConfig->lock_config_full($src_vmid, 30, $clonefn); | |
301 | } elsif ($src_vmid) { | |
302 | $cloned = PVE::QemuConfig->lock_config_shared($src_vmid, 30, $clonefn); | |
303 | } else { | |
304 | $cloned = $clonefn->(); | |
305 | } | |
306 | ||
307 | return $cloned->@{qw(file size)}; | |
308 | }; | |
309 | ||
ae57f6b3 DM |
310 | # Note: $pool is only needed when creating a VM, because pool permissions |
311 | # are automatically inherited if VM already exists inside a pool. | |
312 | my $create_disks = sub { | |
96ed3574 | 313 | my ($rpcenv, $authuser, $conf, $arch, $storecfg, $vmid, $pool, $settings, $default_storage) = @_; |
a0d1b1a2 DM |
314 | |
315 | my $vollist = []; | |
a0d1b1a2 | 316 | |
ae57f6b3 | 317 | my $res = {}; |
64932aeb DM |
318 | |
319 | my $code = sub { | |
ae57f6b3 DM |
320 | my ($ds, $disk) = @_; |
321 | ||
322 | my $volid = $disk->{file}; | |
21e1ee7b | 323 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); |
ae57f6b3 | 324 | |
f5782fd0 | 325 | if (!$volid || $volid eq 'none' || $volid eq 'cdrom') { |
628e9a2b | 326 | delete $disk->{size}; |
71c58bb7 | 327 | $res->{$ds} = PVE::QemuServer::print_drive($disk); |
8f2c9019 | 328 | } elsif (defined($volname) && $volname eq 'cloudinit') { |
21e1ee7b | 329 | $storeid = $storeid // $default_storage; |
0c9a7596 AD |
330 | die "no storage ID specified (and no default storage)\n" if !$storeid; |
331 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); | |
332 | my $name = "vm-$vmid-cloudinit"; | |
64d1a6ae | 333 | |
0c9a7596 AD |
334 | my $fmt = undef; |
335 | if ($scfg->{path}) { | |
c152600b | 336 | $fmt = $disk->{format} // "qcow2"; |
64d1a6ae WL |
337 | $name .= ".$fmt"; |
338 | } else { | |
c152600b | 339 | $fmt = $disk->{format} // "raw"; |
0c9a7596 | 340 | } |
64d1a6ae | 341 | |
4fdc1d3d | 342 | # Initial disk created with 4 MB and aligned to 4MB on regeneration |
7d761a01 ML |
343 | my $ci_size = PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE; |
344 | my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $name, $ci_size/1024); | |
0c9a7596 AD |
345 | $disk->{file} = $volid; |
346 | $disk->{media} = 'cdrom'; | |
347 | push @$vollist, $volid; | |
348 | delete $disk->{format}; # no longer needed | |
71c58bb7 | 349 | $res->{$ds} = PVE::QemuServer::print_drive($disk); |
3e7d9fac | 350 | print "$ds: successfully created disk '$res->{$ds}'\n"; |
17677004 | 351 | } elsif ($volid =~ $NEW_DISK_RE) { |
ae57f6b3 DM |
352 | my ($storeid, $size) = ($2 || $default_storage, $3); |
353 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
e6ac9fed DJ |
354 | |
355 | if (my $source = delete $disk->{'import-from'}) { | |
356 | my $dst_volid; | |
357 | ||
358 | if (PVE::Storage::parse_volume_id($source, 1)) { # PVE-managed volume | |
359 | my $dest_info = { | |
360 | vmid => $vmid, | |
361 | drivename => $ds, | |
362 | storage => $storeid, | |
363 | format => $disk->{format}, | |
364 | }; | |
365 | ||
366 | $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($conf, $disk) | |
367 | if $ds eq 'efidisk0'; | |
368 | ||
369 | ($dst_volid, $size) = eval { | |
370 | $import_from_volid->($storecfg, $source, $dest_info, $vollist); | |
371 | }; | |
372 | die "cannot import from '$source' - $@" if $@; | |
373 | } else { | |
374 | $source = PVE::Storage::abs_filesystem_path($storecfg, $source, 1); | |
375 | $size = PVE::Storage::file_size_info($source); | |
376 | die "could not get file size of $source\n" if !$size; | |
377 | ||
378 | (undef, $dst_volid) = PVE::QemuServer::ImportDisk::do_import( | |
379 | $source, | |
380 | $vmid, | |
381 | $storeid, | |
382 | { | |
383 | drive_name => $ds, | |
384 | format => $disk->{format}, | |
385 | 'skip-config-update' => 1, | |
386 | }, | |
387 | ); | |
388 | push @$vollist, $dst_volid; | |
389 | } | |
390 | ||
391 | $disk->{file} = $dst_volid; | |
392 | $disk->{size} = $size; | |
393 | delete $disk->{format}; # no longer needed | |
394 | $res->{$ds} = PVE::QemuServer::print_drive($disk); | |
1a35631a | 395 | } else { |
e6ac9fed DJ |
396 | my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid); |
397 | my $fmt = $disk->{format} || $defformat; | |
398 | ||
399 | $size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # vdisk_alloc uses kb | |
400 | ||
401 | my $volid; | |
402 | if ($ds eq 'efidisk0') { | |
403 | my $smm = PVE::QemuServer::Machine::machine_type_is_q35($conf); | |
404 | ($volid, $size) = PVE::QemuServer::create_efidisk( | |
405 | $storecfg, $storeid, $vmid, $fmt, $arch, $disk, $smm); | |
406 | } elsif ($ds eq 'tpmstate0') { | |
407 | # swtpm can only use raw volumes, and uses a fixed size | |
408 | $size = PVE::Tools::convert_size(PVE::QemuServer::Drive::TPMSTATE_DISK_SIZE, 'b' => 'kb'); | |
409 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, "raw", undef, $size); | |
410 | } else { | |
411 | $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $size); | |
412 | } | |
413 | push @$vollist, $volid; | |
414 | $disk->{file} = $volid; | |
415 | $disk->{size} = PVE::Tools::convert_size($size, 'kb' => 'b'); | |
416 | delete $disk->{format}; # no longer needed | |
417 | $res->{$ds} = PVE::QemuServer::print_drive($disk); | |
1a35631a | 418 | } |
3e7d9fac FE |
419 | |
420 | print "$ds: successfully created disk '$res->{$ds}'\n"; | |
ae57f6b3 | 421 | } else { |
db81c007 FE |
422 | PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid); |
423 | if ($storeid) { | |
424 | my ($vtype) = PVE::Storage::parse_volname($storecfg, $volid); | |
425 | die "cannot use volume $volid - content type needs to be 'images' or 'iso'" | |
426 | if $vtype ne 'images' && $vtype ne 'iso'; | |
427 | } | |
75466c4f | 428 | |
fe19840a | 429 | PVE::Storage::activate_volumes($storecfg, [ $volid ]) if $storeid; |
09a89895 | 430 | |
fe19840a FE |
431 | my $size = PVE::Storage::volume_size_info($storecfg, $volid); |
432 | die "volume $volid does not exist\n" if !$size; | |
433 | $disk->{size} = $size; | |
24afaca0 | 434 | |
71c58bb7 | 435 | $res->{$ds} = PVE::QemuServer::print_drive($disk); |
a0d1b1a2 | 436 | } |
64932aeb DM |
437 | }; |
438 | ||
c1accf9d | 439 | eval { $foreach_volume_with_alloc->($settings, $code); }; |
a0d1b1a2 DM |
440 | |
441 | # free allocated images on error | |
442 | if (my $err = $@) { | |
443 | syslog('err', "VM $vmid creating disks failed"); | |
444 | foreach my $volid (@$vollist) { | |
445 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
446 | warn $@ if $@; | |
447 | } | |
448 | die $err; | |
449 | } | |
450 | ||
367e6bf4 | 451 | return ($vollist, $res); |
a0d1b1a2 DM |
452 | }; |
453 | ||
6e72f90b SR |
454 | my $check_cpu_model_access = sub { |
455 | my ($rpcenv, $authuser, $new, $existing) = @_; | |
456 | ||
457 | return if !defined($new->{cpu}); | |
458 | ||
459 | my $cpu = PVE::JSONSchema::check_format('pve-vm-cpu-conf', $new->{cpu}); | |
460 | return if !$cpu || !$cpu->{cputype}; # always allow default | |
461 | my $cputype = $cpu->{cputype}; | |
462 | ||
463 | if ($existing && $existing->{cpu}) { | |
464 | # changing only other settings doesn't require permissions for CPU model | |
465 | my $existingCpu = PVE::JSONSchema::check_format('pve-vm-cpu-conf', $existing->{cpu}); | |
466 | return if $existingCpu->{cputype} eq $cputype; | |
467 | } | |
468 | ||
469 | if (PVE::QemuServer::CPUConfig::is_custom_model($cputype)) { | |
470 | $rpcenv->check($authuser, "/nodes", ['Sys.Audit']); | |
471 | } | |
472 | }; | |
473 | ||
58cb690b DC |
474 | my $cpuoptions = { |
475 | 'cores' => 1, | |
476 | 'cpu' => 1, | |
477 | 'cpulimit' => 1, | |
478 | 'cpuunits' => 1, | |
479 | 'numa' => 1, | |
480 | 'smp' => 1, | |
481 | 'sockets' => 1, | |
84b31f48 | 482 | 'vcpus' => 1, |
58cb690b DC |
483 | }; |
484 | ||
485 | my $memoryoptions = { | |
486 | 'memory' => 1, | |
487 | 'balloon' => 1, | |
488 | 'shares' => 1, | |
489 | }; | |
490 | ||
491 | my $hwtypeoptions = { | |
492 | 'acpi' => 1, | |
493 | 'hotplug' => 1, | |
494 | 'kvm' => 1, | |
495 | 'machine' => 1, | |
496 | 'scsihw' => 1, | |
497 | 'smbios1' => 1, | |
498 | 'tablet' => 1, | |
499 | 'vga' => 1, | |
500 | 'watchdog' => 1, | |
b2dd61a0 | 501 | 'audio0' => 1, |
58cb690b DC |
502 | }; |
503 | ||
6bcacc21 | 504 | my $generaloptions = { |
58cb690b DC |
505 | 'agent' => 1, |
506 | 'autostart' => 1, | |
507 | 'bios' => 1, | |
508 | 'description' => 1, | |
509 | 'keyboard' => 1, | |
510 | 'localtime' => 1, | |
511 | 'migrate_downtime' => 1, | |
512 | 'migrate_speed' => 1, | |
513 | 'name' => 1, | |
514 | 'onboot' => 1, | |
515 | 'ostype' => 1, | |
516 | 'protection' => 1, | |
517 | 'reboot' => 1, | |
518 | 'startdate' => 1, | |
519 | 'startup' => 1, | |
520 | 'tdf' => 1, | |
521 | 'template' => 1, | |
b8e7068a | 522 | 'tags' => 1, |
58cb690b DC |
523 | }; |
524 | ||
525 | my $vmpoweroptions = { | |
526 | 'freeze' => 1, | |
527 | }; | |
528 | ||
529 | my $diskoptions = { | |
530 | 'boot' => 1, | |
531 | 'bootdisk' => 1, | |
253624c7 | 532 | 'vmstatestorage' => 1, |
58cb690b DC |
533 | }; |
534 | ||
7ee990cd | 535 | my $cloudinitoptions = { |
cb702ebe | 536 | cicustom => 1, |
7ee990cd DM |
537 | cipassword => 1, |
538 | citype => 1, | |
539 | ciuser => 1, | |
540 | nameserver => 1, | |
541 | searchdomain => 1, | |
542 | sshkeys => 1, | |
543 | }; | |
544 | ||
0761ee01 FE |
545 | my $check_vm_create_serial_perm = sub { |
546 | my ($rpcenv, $authuser, $vmid, $pool, $param) = @_; | |
547 | ||
548 | return 1 if $authuser eq 'root@pam'; | |
549 | ||
550 | foreach my $opt (keys %{$param}) { | |
551 | next if $opt !~ m/^serial\d+$/; | |
552 | ||
553 | if ($param->{$opt} eq 'socket') { | |
554 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); | |
555 | } else { | |
556 | die "only root can set '$opt' config for real devices\n"; | |
557 | } | |
558 | } | |
559 | ||
560 | return 1; | |
561 | }; | |
562 | ||
563 | my $check_vm_create_usb_perm = sub { | |
564 | my ($rpcenv, $authuser, $vmid, $pool, $param) = @_; | |
565 | ||
566 | return 1 if $authuser eq 'root@pam'; | |
567 | ||
568 | foreach my $opt (keys %{$param}) { | |
569 | next if $opt !~ m/^usb\d+$/; | |
570 | ||
571 | if ($param->{$opt} =~ m/spice/) { | |
572 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); | |
573 | } else { | |
574 | die "only root can set '$opt' config for real devices\n"; | |
575 | } | |
576 | } | |
577 | ||
578 | return 1; | |
579 | }; | |
580 | ||
a0d1b1a2 | 581 | my $check_vm_modify_config_perm = sub { |
e30f75c5 | 582 | my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_; |
a0d1b1a2 | 583 | |
6e5c4da7 | 584 | return 1 if $authuser eq 'root@pam'; |
a0d1b1a2 | 585 | |
ae57f6b3 | 586 | foreach my $opt (@$key_list) { |
97415261 TL |
587 | # some checks (e.g., disk, serial port, usb) need to be done somewhere |
588 | # else, as there the permission can be value dependend | |
74479ee9 | 589 | next if PVE::QemuServer::is_valid_drivename($opt); |
58cb690b | 590 | next if $opt eq 'cdrom'; |
165be267 DC |
591 | next if $opt =~ m/^(?:unused|serial|usb)\d+$/; |
592 | ||
a0d1b1a2 | 593 | |
6bcacc21 | 594 | if ($cpuoptions->{$opt} || $opt =~ m/^numa\d+$/) { |
a0d1b1a2 | 595 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']); |
58cb690b | 596 | } elsif ($memoryoptions->{$opt}) { |
a0d1b1a2 | 597 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']); |
58cb690b | 598 | } elsif ($hwtypeoptions->{$opt}) { |
a0d1b1a2 | 599 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); |
6bcacc21 | 600 | } elsif ($generaloptions->{$opt}) { |
58cb690b DC |
601 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']); |
602 | # special case for startup since it changes host behaviour | |
603 | if ($opt eq 'startup') { | |
604 | $rpcenv->check_full($authuser, "/", ['Sys.Modify']); | |
605 | } | |
606 | } elsif ($vmpoweroptions->{$opt}) { | |
607 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.PowerMgmt']); | |
608 | } elsif ($diskoptions->{$opt}) { | |
609 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']); | |
fc701af7 | 610 | } elsif ($opt =~ m/^(?:net|ipconfig)\d+$/) { |
a0d1b1a2 | 611 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']); |
fc701af7 ML |
612 | } elsif ($cloudinitoptions->{$opt}) { |
613 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Cloudinit', 'VM.Config.Network'], 1); | |
5661a681 DC |
614 | } elsif ($opt eq 'vmstate') { |
615 | # the user needs Disk and PowerMgmt privileges to change the vmstate | |
616 | # also needs privileges on the storage, that will be checked later | |
617 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk', 'VM.PowerMgmt' ]); | |
a0d1b1a2 | 618 | } else { |
165be267 | 619 | # catches hostpci\d+, args, lock, etc. |
58cb690b DC |
620 | # new options will be checked here |
621 | die "only root can set '$opt' config\n"; | |
a0d1b1a2 DM |
622 | } |
623 | } | |
624 | ||
625 | return 1; | |
626 | }; | |
627 | ||
1e3baf05 | 628 | __PACKAGE__->register_method({ |
afdb31d5 DM |
629 | name => 'vmlist', |
630 | path => '', | |
1e3baf05 DM |
631 | method => 'GET', |
632 | description => "Virtual machine index (per node).", | |
a0d1b1a2 DM |
633 | permissions => { |
634 | description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.", | |
635 | user => 'all', | |
636 | }, | |
1e3baf05 DM |
637 | proxyto => 'node', |
638 | protected => 1, # qemu pid files are only readable by root | |
639 | parameters => { | |
3326ae19 | 640 | additionalProperties => 0, |
1e3baf05 DM |
641 | properties => { |
642 | node => get_standard_option('pve-node'), | |
12612b09 WB |
643 | full => { |
644 | type => 'boolean', | |
645 | optional => 1, | |
646 | description => "Determine the full status of active VMs.", | |
647 | }, | |
1e3baf05 DM |
648 | }, |
649 | }, | |
650 | returns => { | |
651 | type => 'array', | |
652 | items => { | |
653 | type => "object", | |
b1a70cab | 654 | properties => $PVE::QemuServer::vmstatus_return_properties, |
1e3baf05 DM |
655 | }, |
656 | links => [ { rel => 'child', href => "{vmid}" } ], | |
657 | }, | |
658 | code => sub { | |
659 | my ($param) = @_; | |
660 | ||
a0d1b1a2 DM |
661 | my $rpcenv = PVE::RPCEnvironment::get(); |
662 | my $authuser = $rpcenv->get_user(); | |
663 | ||
12612b09 | 664 | my $vmstatus = PVE::QemuServer::vmstatus(undef, $param->{full}); |
1e3baf05 | 665 | |
a0d1b1a2 DM |
666 | my $res = []; |
667 | foreach my $vmid (keys %$vmstatus) { | |
668 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1); | |
669 | ||
670 | my $data = $vmstatus->{$vmid}; | |
a0d1b1a2 DM |
671 | push @$res, $data; |
672 | } | |
1e3baf05 | 673 | |
a0d1b1a2 | 674 | return $res; |
1e3baf05 DM |
675 | }}); |
676 | ||
d1e92cf6 DM |
677 | my $parse_restore_archive = sub { |
678 | my ($storecfg, $archive) = @_; | |
679 | ||
e5fd1c65 | 680 | my ($archive_storeid, $archive_volname) = PVE::Storage::parse_volume_id($archive, 1); |
d1e92cf6 DM |
681 | |
682 | if (defined($archive_storeid)) { | |
683 | my $scfg = PVE::Storage::storage_config($storecfg, $archive_storeid); | |
684 | if ($scfg->{type} eq 'pbs') { | |
685 | return { | |
686 | type => 'pbs', | |
687 | volid => $archive, | |
688 | }; | |
689 | } | |
690 | } | |
691 | my $path = PVE::Storage::abs_filesystem_path($storecfg, $archive); | |
692 | return { | |
693 | type => 'file', | |
694 | path => $path, | |
695 | }; | |
696 | }; | |
d703d4c0 | 697 | |
d703d4c0 | 698 | |
1e3baf05 | 699 | __PACKAGE__->register_method({ |
afdb31d5 DM |
700 | name => 'create_vm', |
701 | path => '', | |
1e3baf05 | 702 | method => 'POST', |
3e16d5fc | 703 | description => "Create or restore a virtual machine.", |
a0d1b1a2 | 704 | permissions => { |
f9bfceef DM |
705 | description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " . |
706 | "For restore (option 'archive'), it is enough if the user has 'VM.Backup' permission and the VM already exists. " . | |
707 | "If you create disks you need 'Datastore.AllocateSpace' on any used storage.", | |
708 | user => 'all', # check inside | |
a0d1b1a2 | 709 | }, |
1e3baf05 DM |
710 | protected => 1, |
711 | proxyto => 'node', | |
712 | parameters => { | |
3326ae19 | 713 | additionalProperties => 0, |
1e3baf05 DM |
714 | properties => PVE::QemuServer::json_config_properties( |
715 | { | |
716 | node => get_standard_option('pve-node'), | |
65e866e5 | 717 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), |
3e16d5fc | 718 | archive => { |
d1e92cf6 | 719 | description => "The backup archive. Either the file system path to a .tar or .vma file (use '-' to pipe data from stdin) or a proxmox storage backup volume identifier.", |
3e16d5fc DM |
720 | type => 'string', |
721 | optional => 1, | |
722 | maxLength => 255, | |
65e866e5 | 723 | completion => \&PVE::QemuServer::complete_backup_archives, |
3e16d5fc DM |
724 | }, |
725 | storage => get_standard_option('pve-storage-id', { | |
726 | description => "Default storage.", | |
727 | optional => 1, | |
335af808 | 728 | completion => \&PVE::QemuServer::complete_storage, |
3e16d5fc DM |
729 | }), |
730 | force => { | |
afdb31d5 | 731 | optional => 1, |
3e16d5fc DM |
732 | type => 'boolean', |
733 | description => "Allow to overwrite existing VM.", | |
51586c3a DM |
734 | requires => 'archive', |
735 | }, | |
736 | unique => { | |
afdb31d5 | 737 | optional => 1, |
51586c3a DM |
738 | type => 'boolean', |
739 | description => "Assign a unique random ethernet address.", | |
740 | requires => 'archive', | |
3e16d5fc | 741 | }, |
26731a3c SR |
742 | 'live-restore' => { |
743 | optional => 1, | |
744 | type => 'boolean', | |
745 | description => "Start the VM immediately from the backup and restore in background. PBS only.", | |
746 | requires => 'archive', | |
747 | }, | |
75466c4f | 748 | pool => { |
a0d1b1a2 DM |
749 | optional => 1, |
750 | type => 'string', format => 'pve-poolid', | |
751 | description => "Add the VM to the specified pool.", | |
752 | }, | |
7c536e11 | 753 | bwlimit => { |
0aab5a16 | 754 | description => "Override I/O bandwidth limit (in KiB/s).", |
7c536e11 WB |
755 | optional => 1, |
756 | type => 'integer', | |
757 | minimum => '0', | |
41756a3b | 758 | default => 'restore limit from datacenter or storage config', |
e33f774d TL |
759 | }, |
760 | start => { | |
761 | optional => 1, | |
762 | type => 'boolean', | |
763 | default => 0, | |
764 | description => "Start VM after it was created successfully.", | |
765 | }, | |
c1accf9d FE |
766 | }, |
767 | 1, # with_disk_alloc | |
768 | ), | |
1e3baf05 | 769 | }, |
afdb31d5 | 770 | returns => { |
5fdbe4f0 DM |
771 | type => 'string', |
772 | }, | |
1e3baf05 DM |
773 | code => sub { |
774 | my ($param) = @_; | |
775 | ||
5fdbe4f0 | 776 | my $rpcenv = PVE::RPCEnvironment::get(); |
a0d1b1a2 | 777 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 | 778 | |
1e3baf05 | 779 | my $node = extract_param($param, 'node'); |
1e3baf05 DM |
780 | my $vmid = extract_param($param, 'vmid'); |
781 | ||
3e16d5fc | 782 | my $archive = extract_param($param, 'archive'); |
8ba8418c | 783 | my $is_restore = !!$archive; |
3e16d5fc | 784 | |
b924c435 | 785 | my $bwlimit = extract_param($param, 'bwlimit'); |
51586c3a | 786 | my $force = extract_param($param, 'force'); |
a0d1b1a2 | 787 | my $pool = extract_param($param, 'pool'); |
e33f774d | 788 | my $start_after_create = extract_param($param, 'start'); |
b924c435 TL |
789 | my $storage = extract_param($param, 'storage'); |
790 | my $unique = extract_param($param, 'unique'); | |
26731a3c SR |
791 | my $live_restore = extract_param($param, 'live-restore'); |
792 | ||
0c9a7596 AD |
793 | if (defined(my $ssh_keys = $param->{sshkeys})) { |
794 | $ssh_keys = URI::Escape::uri_unescape($ssh_keys); | |
795 | PVE::Tools::validate_ssh_public_keys($ssh_keys); | |
796 | } | |
797 | ||
3e16d5fc | 798 | PVE::Cluster::check_cfs_quorum(); |
1e3baf05 | 799 | |
b924c435 TL |
800 | my $filename = PVE::QemuConfig->config_file($vmid); |
801 | my $storecfg = PVE::Storage::config(); | |
802 | ||
a0d1b1a2 DM |
803 | if (defined($pool)) { |
804 | $rpcenv->check_pool_exist($pool); | |
75466c4f | 805 | } |
a0d1b1a2 | 806 | |
fcbb753e | 807 | $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']) |
a0d1b1a2 DM |
808 | if defined($storage); |
809 | ||
f9bfceef DM |
810 | if ($rpcenv->check($authuser, "/vms/$vmid", ['VM.Allocate'], 1)) { |
811 | # OK | |
812 | } elsif ($pool && $rpcenv->check($authuser, "/pool/$pool", ['VM.Allocate'], 1)) { | |
813 | # OK | |
814 | } elsif ($archive && $force && (-f $filename) && | |
815 | $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) { | |
816 | # OK: user has VM.Backup permissions, and want to restore an existing VM | |
817 | } else { | |
818 | raise_perm_exc(); | |
819 | } | |
820 | ||
325b32cc | 821 | if ($archive) { |
202a2a0b FE |
822 | for my $opt (sort keys $param->%*) { |
823 | if (PVE::QemuServer::Drive::is_valid_drivename($opt)) { | |
824 | raise_param_exc({ $opt => "option conflicts with option 'archive'" }); | |
825 | } | |
826 | } | |
bc4dcb99 | 827 | |
5b9d692a | 828 | if ($archive eq '-') { |
3326ae19 | 829 | die "pipe requires cli environment\n" if $rpcenv->{type} ne 'cli'; |
d1e92cf6 | 830 | $archive = { type => 'pipe' }; |
5b9d692a | 831 | } else { |
f9be9137 FE |
832 | PVE::Storage::check_volume_access( |
833 | $rpcenv, | |
834 | $authuser, | |
835 | $storecfg, | |
836 | $vmid, | |
837 | $archive, | |
838 | 'backup', | |
839 | ); | |
d1e92cf6 DM |
840 | |
841 | $archive = $parse_restore_archive->($storecfg, $archive); | |
971f27c4 | 842 | } |
1e3baf05 DM |
843 | } |
844 | ||
325b32cc FE |
845 | if (scalar(keys $param->%*) > 0) { |
846 | &$resolve_cdrom_alias($param); | |
847 | ||
848 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage); | |
849 | ||
850 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]); | |
851 | ||
852 | &$check_vm_create_serial_perm($rpcenv, $authuser, $vmid, $pool, $param); | |
853 | &$check_vm_create_usb_perm($rpcenv, $authuser, $vmid, $pool, $param); | |
854 | ||
855 | &$check_cpu_model_access($rpcenv, $authuser, $param); | |
856 | ||
857 | $check_drive_param->($param, $storecfg); | |
858 | ||
859 | PVE::QemuServer::add_random_macs($param); | |
860 | } | |
861 | ||
4fedc13b | 862 | my $emsg = $is_restore ? "unable to restore VM $vmid -" : "unable to create VM $vmid -"; |
3e16d5fc | 863 | |
4fedc13b TL |
864 | eval { PVE::QemuConfig->create_and_lock_config($vmid, $force) }; |
865 | die "$emsg $@" if $@; | |
3e16d5fc | 866 | |
b973806e | 867 | my $restored_data = 0; |
4fedc13b TL |
868 | my $restorefn = sub { |
869 | my $conf = PVE::QemuConfig->load_config($vmid); | |
4d8d55f1 | 870 | |
4fedc13b | 871 | PVE::QemuConfig->check_protection($conf, $emsg); |
3a07a8a9 | 872 | |
4fedc13b | 873 | die "$emsg vm is running\n" if PVE::QemuServer::check_running($vmid); |
3e16d5fc DM |
874 | |
875 | my $realcmd = sub { | |
d1e92cf6 | 876 | my $restore_options = { |
51586c3a | 877 | storage => $storage, |
a0d1b1a2 | 878 | pool => $pool, |
7c536e11 | 879 | unique => $unique, |
5294c110 | 880 | bwlimit => $bwlimit, |
26731a3c | 881 | live => $live_restore, |
202a2a0b | 882 | override_conf => $param, |
d1e92cf6 DM |
883 | }; |
884 | if ($archive->{type} eq 'file' || $archive->{type} eq 'pipe') { | |
b973806e TL |
885 | die "live-restore is only compatible with backup images from a Proxmox Backup Server\n" |
886 | if $live_restore; | |
d1e92cf6 DM |
887 | PVE::QemuServer::restore_file_archive($archive->{path} // '-', $vmid, $authuser, $restore_options); |
888 | } elsif ($archive->{type} eq 'pbs') { | |
889 | PVE::QemuServer::restore_proxmox_backup_archive($archive->{volid}, $vmid, $authuser, $restore_options); | |
890 | } else { | |
891 | die "unknown backup archive type\n"; | |
892 | } | |
b973806e TL |
893 | $restored_data = 1; |
894 | ||
5294c110 CE |
895 | my $restored_conf = PVE::QemuConfig->load_config($vmid); |
896 | # Convert restored VM to template if backup was VM template | |
897 | if (PVE::QemuConfig->is_template($restored_conf)) { | |
898 | warn "Convert to template.\n"; | |
899 | eval { PVE::QemuServer::template_create($vmid, $restored_conf) }; | |
900 | warn $@ if $@; | |
901 | } | |
3e16d5fc DM |
902 | }; |
903 | ||
223e032b WL |
904 | # ensure no old replication state are exists |
905 | PVE::ReplicationState::delete_guest_states($vmid); | |
906 | ||
0c97024d TL |
907 | PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd); |
908 | ||
a0e27afb | 909 | if ($start_after_create && !$live_restore) { |
0c97024d TL |
910 | print "Execute autostart\n"; |
911 | eval { PVE::API2::Qemu->vm_start({ vmid => $vmid, node => $node }) }; | |
912 | warn $@ if $@; | |
913 | } | |
3e16d5fc | 914 | }; |
1e3baf05 | 915 | |
1e3baf05 | 916 | my $createfn = sub { |
223e032b WL |
917 | # ensure no old replication state are exists |
918 | PVE::ReplicationState::delete_guest_states($vmid); | |
919 | ||
5fdbe4f0 | 920 | my $realcmd = sub { |
1858638f | 921 | my $conf = $param; |
de64f101 | 922 | my $arch = PVE::QemuServer::get_vm_arch($conf); |
40c3bcf8 | 923 | |
26b443c8 TL |
924 | $conf->{meta} = PVE::QemuServer::new_meta_info_string(); |
925 | ||
a85ff91b | 926 | my $vollist = []; |
5fdbe4f0 | 927 | eval { |
367e6bf4 FE |
928 | ($vollist, my $created_opts) = $create_disks->( |
929 | $rpcenv, | |
930 | $authuser, | |
931 | $conf, | |
932 | $arch, | |
933 | $storecfg, | |
934 | $vmid, | |
935 | $pool, | |
936 | $param, | |
937 | $storage, | |
938 | ); | |
939 | $conf->{$_} = $created_opts->{$_} for keys $created_opts->%*; | |
1e3baf05 | 940 | |
2141a802 SR |
941 | if (!$conf->{boot}) { |
942 | my $devs = PVE::QemuServer::get_default_bootdevices($conf); | |
943 | $conf->{boot} = PVE::QemuServer::print_bootorder($devs); | |
5fdbe4f0 | 944 | } |
1e3baf05 | 945 | |
47314bf5 DM |
946 | # auto generate uuid if user did not specify smbios1 option |
947 | if (!$conf->{smbios1}) { | |
ae2fcb3b | 948 | $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid(); |
47314bf5 DM |
949 | } |
950 | ||
40c3bcf8 | 951 | if ((!defined($conf->{vmgenid}) || $conf->{vmgenid} eq '1') && $arch ne 'aarch64') { |
6ee499ff DC |
952 | $conf->{vmgenid} = PVE::QemuServer::generate_uuid(); |
953 | } | |
954 | ||
4dd1e83c TL |
955 | my $machine = $conf->{machine}; |
956 | if (!$machine || $machine =~ m/^(?:pc|q35|virt)$/) { | |
957 | # always pin Windows' machine version on create, they get to easily confused | |
958 | if (PVE::QemuServer::windows_version($conf->{ostype})) { | |
0761e619 | 959 | $conf->{machine} = PVE::QemuServer::windows_get_pinned_machine_version($machine); |
4dd1e83c TL |
960 | } |
961 | } | |
962 | ||
ffda963f | 963 | PVE::QemuConfig->write_config($vmid, $conf); |
ae9ca91d | 964 | |
5fdbe4f0 DM |
965 | }; |
966 | my $err = $@; | |
1e3baf05 | 967 | |
5fdbe4f0 DM |
968 | if ($err) { |
969 | foreach my $volid (@$vollist) { | |
970 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
971 | warn $@ if $@; | |
972 | } | |
4fedc13b | 973 | die "$emsg $err"; |
5fdbe4f0 | 974 | } |
502d18a2 | 975 | |
be517049 | 976 | PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; |
5fdbe4f0 DM |
977 | }; |
978 | ||
e33f774d TL |
979 | PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd); |
980 | ||
981 | if ($start_after_create) { | |
982 | print "Execute autostart\n"; | |
5bf96183 WB |
983 | eval { PVE::API2::Qemu->vm_start({vmid => $vmid, node => $node}) }; |
984 | warn $@ if $@; | |
e33f774d | 985 | } |
5fdbe4f0 DM |
986 | }; |
987 | ||
5bf96183 WB |
988 | my ($code, $worker_name); |
989 | if ($is_restore) { | |
990 | $worker_name = 'qmrestore'; | |
991 | $code = sub { | |
992 | eval { $restorefn->() }; | |
993 | if (my $err = $@) { | |
994 | eval { PVE::QemuConfig->remove_lock($vmid, 'create') }; | |
995 | warn $@ if $@; | |
b973806e TL |
996 | if ($restored_data) { |
997 | warn "error after data was restored, VM disks should be OK but config may " | |
998 | ."require adaptions. VM $vmid state is NOT cleaned up.\n"; | |
999 | } else { | |
1000 | warn "error before or during data restore, some or all disks were not " | |
1001 | ."completely restored. VM $vmid state is NOT cleaned up.\n"; | |
1002 | } | |
5bf96183 WB |
1003 | die $err; |
1004 | } | |
1005 | }; | |
1006 | } else { | |
1007 | $worker_name = 'qmcreate'; | |
1008 | $code = sub { | |
1009 | eval { $createfn->() }; | |
1010 | if (my $err = $@) { | |
1011 | eval { | |
1012 | my $conffile = PVE::QemuConfig->config_file($vmid); | |
f1e277cd | 1013 | unlink($conffile) or die "failed to remove config file: $!\n"; |
5bf96183 WB |
1014 | }; |
1015 | warn $@ if $@; | |
1016 | die $err; | |
1017 | } | |
1018 | }; | |
1019 | } | |
8ba8418c TL |
1020 | |
1021 | return $rpcenv->fork_worker($worker_name, $vmid, $authuser, $code); | |
1e3baf05 DM |
1022 | }}); |
1023 | ||
1024 | __PACKAGE__->register_method({ | |
1025 | name => 'vmdiridx', | |
afdb31d5 | 1026 | path => '{vmid}', |
1e3baf05 DM |
1027 | method => 'GET', |
1028 | proxyto => 'node', | |
1029 | description => "Directory index", | |
a0d1b1a2 DM |
1030 | permissions => { |
1031 | user => 'all', | |
1032 | }, | |
1e3baf05 | 1033 | parameters => { |
3326ae19 | 1034 | additionalProperties => 0, |
1e3baf05 DM |
1035 | properties => { |
1036 | node => get_standard_option('pve-node'), | |
1037 | vmid => get_standard_option('pve-vmid'), | |
1038 | }, | |
1039 | }, | |
1040 | returns => { | |
1041 | type => 'array', | |
1042 | items => { | |
1043 | type => "object", | |
1044 | properties => { | |
1045 | subdir => { type => 'string' }, | |
1046 | }, | |
1047 | }, | |
1048 | links => [ { rel => 'child', href => "{subdir}" } ], | |
1049 | }, | |
1050 | code => sub { | |
1051 | my ($param) = @_; | |
1052 | ||
1053 | my $res = [ | |
1054 | { subdir => 'config' }, | |
df2a2dbb | 1055 | { subdir => 'pending' }, |
1e3baf05 DM |
1056 | { subdir => 'status' }, |
1057 | { subdir => 'unlink' }, | |
1058 | { subdir => 'vncproxy' }, | |
87302002 | 1059 | { subdir => 'termproxy' }, |
3ea94c60 | 1060 | { subdir => 'migrate' }, |
2f48a4f5 | 1061 | { subdir => 'resize' }, |
586bfa78 | 1062 | { subdir => 'move' }, |
1e3baf05 DM |
1063 | { subdir => 'rrd' }, |
1064 | { subdir => 'rrddata' }, | |
91c94f0a | 1065 | { subdir => 'monitor' }, |
d1a47427 | 1066 | { subdir => 'agent' }, |
7e7d7b61 | 1067 | { subdir => 'snapshot' }, |
288eeea8 | 1068 | { subdir => 'spiceproxy' }, |
7aa608d6 | 1069 | { subdir => 'sendkey' }, |
228a998b | 1070 | { subdir => 'firewall' }, |
1e3baf05 | 1071 | ]; |
afdb31d5 | 1072 | |
1e3baf05 DM |
1073 | return $res; |
1074 | }}); | |
1075 | ||
228a998b | 1076 | __PACKAGE__->register_method ({ |
f34ebd52 | 1077 | subclass => "PVE::API2::Firewall::VM", |
228a998b DM |
1078 | path => '{vmid}/firewall', |
1079 | }); | |
1080 | ||
b8158701 DC |
1081 | __PACKAGE__->register_method ({ |
1082 | subclass => "PVE::API2::Qemu::Agent", | |
1083 | path => '{vmid}/agent', | |
1084 | }); | |
1085 | ||
1e3baf05 | 1086 | __PACKAGE__->register_method({ |
afdb31d5 DM |
1087 | name => 'rrd', |
1088 | path => '{vmid}/rrd', | |
1e3baf05 DM |
1089 | method => 'GET', |
1090 | protected => 1, # fixme: can we avoid that? | |
1091 | permissions => { | |
378b359e | 1092 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], |
1e3baf05 DM |
1093 | }, |
1094 | description => "Read VM RRD statistics (returns PNG)", | |
1095 | parameters => { | |
3326ae19 | 1096 | additionalProperties => 0, |
1e3baf05 DM |
1097 | properties => { |
1098 | node => get_standard_option('pve-node'), | |
1099 | vmid => get_standard_option('pve-vmid'), | |
1100 | timeframe => { | |
1101 | description => "Specify the time frame you are interested in.", | |
1102 | type => 'string', | |
1103 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
1104 | }, | |
1105 | ds => { | |
1106 | description => "The list of datasources you want to display.", | |
1107 | type => 'string', format => 'pve-configid-list', | |
1108 | }, | |
1109 | cf => { | |
1110 | description => "The RRD consolidation function", | |
1111 | type => 'string', | |
1112 | enum => [ 'AVERAGE', 'MAX' ], | |
1113 | optional => 1, | |
1114 | }, | |
1115 | }, | |
1116 | }, | |
1117 | returns => { | |
1118 | type => "object", | |
1119 | properties => { | |
1120 | filename => { type => 'string' }, | |
1121 | }, | |
1122 | }, | |
1123 | code => sub { | |
1124 | my ($param) = @_; | |
1125 | ||
95896f80 | 1126 | return PVE::RRD::create_rrd_graph( |
afdb31d5 | 1127 | "pve2-vm/$param->{vmid}", $param->{timeframe}, |
1e3baf05 | 1128 | $param->{ds}, $param->{cf}); |
afdb31d5 | 1129 | |
1e3baf05 DM |
1130 | }}); |
1131 | ||
1132 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1133 | name => 'rrddata', |
1134 | path => '{vmid}/rrddata', | |
1e3baf05 DM |
1135 | method => 'GET', |
1136 | protected => 1, # fixme: can we avoid that? | |
1137 | permissions => { | |
378b359e | 1138 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], |
1e3baf05 DM |
1139 | }, |
1140 | description => "Read VM RRD statistics", | |
1141 | parameters => { | |
3326ae19 | 1142 | additionalProperties => 0, |
1e3baf05 DM |
1143 | properties => { |
1144 | node => get_standard_option('pve-node'), | |
1145 | vmid => get_standard_option('pve-vmid'), | |
1146 | timeframe => { | |
1147 | description => "Specify the time frame you are interested in.", | |
1148 | type => 'string', | |
1149 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
1150 | }, | |
1151 | cf => { | |
1152 | description => "The RRD consolidation function", | |
1153 | type => 'string', | |
1154 | enum => [ 'AVERAGE', 'MAX' ], | |
1155 | optional => 1, | |
1156 | }, | |
1157 | }, | |
1158 | }, | |
1159 | returns => { | |
1160 | type => "array", | |
1161 | items => { | |
1162 | type => "object", | |
1163 | properties => {}, | |
1164 | }, | |
1165 | }, | |
1166 | code => sub { | |
1167 | my ($param) = @_; | |
1168 | ||
95896f80 | 1169 | return PVE::RRD::create_rrd_data( |
1e3baf05 DM |
1170 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); |
1171 | }}); | |
1172 | ||
1173 | ||
1174 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1175 | name => 'vm_config', |
1176 | path => '{vmid}/config', | |
1e3baf05 DM |
1177 | method => 'GET', |
1178 | proxyto => 'node', | |
86ea0ed0 FE |
1179 | description => "Get the virtual machine configuration with pending configuration " . |
1180 | "changes applied. Set the 'current' parameter to get the current configuration instead.", | |
a0d1b1a2 DM |
1181 | permissions => { |
1182 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1183 | }, | |
1e3baf05 | 1184 | parameters => { |
3326ae19 | 1185 | additionalProperties => 0, |
1e3baf05 DM |
1186 | properties => { |
1187 | node => get_standard_option('pve-node'), | |
335af808 | 1188 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
2a68ec78 TL |
1189 | current => { |
1190 | description => "Get current values (instead of pending values).", | |
1191 | optional => 1, | |
6d89b548 DM |
1192 | default => 0, |
1193 | type => 'boolean', | |
2a68ec78 | 1194 | }, |
b14477e7 RV |
1195 | snapshot => get_standard_option('pve-snapshot-name', { |
1196 | description => "Fetch config values from given snapshot.", | |
1197 | optional => 1, | |
1198 | completion => sub { | |
1199 | my ($cmd, $pname, $cur, $args) = @_; | |
1200 | PVE::QemuConfig->snapshot_list($args->[0]); | |
1201 | }, | |
1202 | }), | |
1e3baf05 DM |
1203 | }, |
1204 | }, | |
afdb31d5 | 1205 | returns => { |
86ea0ed0 | 1206 | description => "The VM configuration.", |
1e3baf05 | 1207 | type => "object", |
ce9b0a38 | 1208 | properties => PVE::QemuServer::json_config_properties({ |
554ac7e7 DM |
1209 | digest => { |
1210 | type => 'string', | |
1211 | description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', | |
1212 | } | |
ce9b0a38 | 1213 | }), |
1e3baf05 DM |
1214 | }, |
1215 | code => sub { | |
1216 | my ($param) = @_; | |
1217 | ||
d3179e1c OB |
1218 | raise_param_exc({ snapshot => "cannot use 'snapshot' parameter with 'current'", |
1219 | current => "cannot use 'snapshot' parameter with 'current'"}) | |
1220 | if ($param->{snapshot} && $param->{current}); | |
1e3baf05 | 1221 | |
d3179e1c OB |
1222 | my $conf; |
1223 | if ($param->{snapshot}) { | |
1224 | $conf = PVE::QemuConfig->load_snapshot_config($param->{vmid}, $param->{snapshot}); | |
1225 | } else { | |
1226 | $conf = PVE::QemuConfig->load_current_config($param->{vmid}, $param->{current}); | |
2254ffcf | 1227 | } |
d3179e1c | 1228 | $conf->{cipassword} = '**********' if $conf->{cipassword}; |
1e3baf05 | 1229 | return $conf; |
d3179e1c | 1230 | |
1e3baf05 DM |
1231 | }}); |
1232 | ||
1e7f2726 DM |
1233 | __PACKAGE__->register_method({ |
1234 | name => 'vm_pending', | |
1235 | path => '{vmid}/pending', | |
1236 | method => 'GET', | |
1237 | proxyto => 'node', | |
86ea0ed0 | 1238 | description => "Get the virtual machine configuration with both current and pending values.", |
1e7f2726 DM |
1239 | permissions => { |
1240 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1241 | }, | |
1242 | parameters => { | |
1243 | additionalProperties => 0, | |
1244 | properties => { | |
1245 | node => get_standard_option('pve-node'), | |
335af808 | 1246 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
1e7f2726 DM |
1247 | }, |
1248 | }, | |
1249 | returns => { | |
1250 | type => "array", | |
1251 | items => { | |
1252 | type => "object", | |
1253 | properties => { | |
1254 | key => { | |
1255 | description => "Configuration option name.", | |
1256 | type => 'string', | |
1257 | }, | |
1258 | value => { | |
1259 | description => "Current value.", | |
1260 | type => 'string', | |
1261 | optional => 1, | |
1262 | }, | |
1263 | pending => { | |
1264 | description => "Pending value.", | |
1265 | type => 'string', | |
1266 | optional => 1, | |
1267 | }, | |
1268 | delete => { | |
1bc483f6 WB |
1269 | description => "Indicates a pending delete request if present and not 0. " . |
1270 | "The value 2 indicates a force-delete request.", | |
1271 | type => 'integer', | |
1272 | minimum => 0, | |
1273 | maximum => 2, | |
1e7f2726 DM |
1274 | optional => 1, |
1275 | }, | |
1276 | }, | |
1277 | }, | |
1278 | }, | |
1279 | code => sub { | |
1280 | my ($param) = @_; | |
1281 | ||
ffda963f | 1282 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); |
1e7f2726 | 1283 | |
98bc3aeb | 1284 | my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete}); |
1e7f2726 | 1285 | |
59ef7003 OB |
1286 | $conf->{cipassword} = '**********' if defined($conf->{cipassword}); |
1287 | $conf->{pending}->{cipassword} = '********** ' if defined($conf->{pending}->{cipassword}); | |
1e7f2726 | 1288 | |
69f2907c | 1289 | return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash); |
59ef7003 | 1290 | }}); |
1e7f2726 | 1291 | |
5555edea DM |
1292 | # POST/PUT {vmid}/config implementation |
1293 | # | |
1294 | # The original API used PUT (idempotent) an we assumed that all operations | |
1295 | # are fast. But it turned out that almost any configuration change can | |
1296 | # involve hot-plug actions, or disk alloc/free. Such actions can take long | |
1297 | # time to complete and have side effects (not idempotent). | |
1298 | # | |
7043d946 | 1299 | # The new implementation uses POST and forks a worker process. We added |
5555edea | 1300 | # a new option 'background_delay'. If specified we wait up to |
7043d946 | 1301 | # 'background_delay' second for the worker task to complete. It returns null |
5555edea | 1302 | # if the task is finished within that time, else we return the UPID. |
7043d946 | 1303 | |
5555edea DM |
1304 | my $update_vm_api = sub { |
1305 | my ($param, $sync) = @_; | |
a0d1b1a2 | 1306 | |
5555edea | 1307 | my $rpcenv = PVE::RPCEnvironment::get(); |
1e3baf05 | 1308 | |
5555edea | 1309 | my $authuser = $rpcenv->get_user(); |
1e3baf05 | 1310 | |
5555edea | 1311 | my $node = extract_param($param, 'node'); |
1e3baf05 | 1312 | |
5555edea | 1313 | my $vmid = extract_param($param, 'vmid'); |
1e3baf05 | 1314 | |
5555edea | 1315 | my $digest = extract_param($param, 'digest'); |
1e3baf05 | 1316 | |
5555edea | 1317 | my $background_delay = extract_param($param, 'background_delay'); |
1e3baf05 | 1318 | |
cefb41fa WB |
1319 | if (defined(my $cipassword = $param->{cipassword})) { |
1320 | # Same logic as in cloud-init (but with the regex fixed...) | |
1321 | $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) | |
1322 | if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/; | |
1323 | } | |
0c9a7596 | 1324 | |
5555edea | 1325 | my @paramarr = (); # used for log message |
edd48c32 | 1326 | foreach my $key (sort keys %$param) { |
cefb41fa WB |
1327 | my $value = $key eq 'cipassword' ? '<hidden>' : $param->{$key}; |
1328 | push @paramarr, "-$key", $value; | |
5555edea | 1329 | } |
0532bc63 | 1330 | |
5555edea DM |
1331 | my $skiplock = extract_param($param, 'skiplock'); |
1332 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1333 | if $skiplock && $authuser ne 'root@pam'; | |
1e3baf05 | 1334 | |
5555edea | 1335 | my $delete_str = extract_param($param, 'delete'); |
0532bc63 | 1336 | |
d3df8cf3 DM |
1337 | my $revert_str = extract_param($param, 'revert'); |
1338 | ||
5555edea | 1339 | my $force = extract_param($param, 'force'); |
1e68cb19 | 1340 | |
0c9a7596 | 1341 | if (defined(my $ssh_keys = $param->{sshkeys})) { |
231f824b WB |
1342 | $ssh_keys = URI::Escape::uri_unescape($ssh_keys); |
1343 | PVE::Tools::validate_ssh_public_keys($ssh_keys); | |
0c9a7596 AD |
1344 | } |
1345 | ||
d3df8cf3 | 1346 | die "no options specified\n" if !$delete_str && !$revert_str && !scalar(keys %$param); |
7bfdeb5f | 1347 | |
5555edea | 1348 | my $storecfg = PVE::Storage::config(); |
1e68cb19 | 1349 | |
5555edea | 1350 | my $defaults = PVE::QemuServer::load_defaults(); |
1e68cb19 | 1351 | |
5555edea | 1352 | &$resolve_cdrom_alias($param); |
0532bc63 | 1353 | |
5555edea | 1354 | # now try to verify all parameters |
ae57f6b3 | 1355 | |
d3df8cf3 DM |
1356 | my $revert = {}; |
1357 | foreach my $opt (PVE::Tools::split_list($revert_str)) { | |
1358 | if (!PVE::QemuServer::option_exists($opt)) { | |
1359 | raise_param_exc({ revert => "unknown option '$opt'" }); | |
1360 | } | |
1361 | ||
1362 | raise_param_exc({ delete => "you can't use '-$opt' and " . | |
1363 | "-revert $opt' at the same time" }) | |
1364 | if defined($param->{$opt}); | |
1365 | ||
1366 | $revert->{$opt} = 1; | |
1367 | } | |
1368 | ||
5555edea DM |
1369 | my @delete = (); |
1370 | foreach my $opt (PVE::Tools::split_list($delete_str)) { | |
1371 | $opt = 'ide2' if $opt eq 'cdrom'; | |
d3df8cf3 | 1372 | |
5555edea DM |
1373 | raise_param_exc({ delete => "you can't use '-$opt' and " . |
1374 | "-delete $opt' at the same time" }) | |
1375 | if defined($param->{$opt}); | |
7043d946 | 1376 | |
d3df8cf3 DM |
1377 | raise_param_exc({ revert => "you can't use '-delete $opt' and " . |
1378 | "-revert $opt' at the same time" }) | |
1379 | if $revert->{$opt}; | |
1380 | ||
5555edea DM |
1381 | if (!PVE::QemuServer::option_exists($opt)) { |
1382 | raise_param_exc({ delete => "unknown option '$opt'" }); | |
0532bc63 | 1383 | } |
1e3baf05 | 1384 | |
5555edea DM |
1385 | push @delete, $opt; |
1386 | } | |
1387 | ||
17677004 WB |
1388 | my $repl_conf = PVE::ReplicationConfig->new(); |
1389 | my $is_replicated = $repl_conf->check_for_existing_jobs($vmid, 1); | |
1390 | my $check_replication = sub { | |
1391 | my ($drive) = @_; | |
1392 | return if !$is_replicated; | |
1393 | my $volid = $drive->{file}; | |
1394 | return if !$volid || !($drive->{replicate}//1); | |
1395 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
21e1ee7b ML |
1396 | |
1397 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
35171ddb FG |
1398 | die "cannot add non-managed/pass-through volume to a replicated VM\n" |
1399 | if !defined($storeid); | |
1400 | ||
e3d31944 | 1401 | return if defined($volname) && $volname eq 'cloudinit'; |
21e1ee7b ML |
1402 | |
1403 | my $format; | |
17677004 WB |
1404 | if ($volid =~ $NEW_DISK_RE) { |
1405 | $storeid = $2; | |
1406 | $format = $drive->{format} || PVE::Storage::storage_default_format($storecfg, $storeid); | |
1407 | } else { | |
17677004 WB |
1408 | $format = (PVE::Storage::parse_volname($storecfg, $volid))[6]; |
1409 | } | |
1410 | return if PVE::Storage::storage_can_replicate($storecfg, $storeid, $format); | |
9b1396ed WB |
1411 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); |
1412 | return if $scfg->{shared}; | |
17677004 WB |
1413 | die "cannot add non-replicatable volume to a replicated VM\n"; |
1414 | }; | |
1415 | ||
7979bbcd FE |
1416 | $check_drive_param->($param, $storecfg, $check_replication); |
1417 | ||
5555edea | 1418 | foreach my $opt (keys %$param) { |
7979bbcd | 1419 | if ($opt =~ m/^net(\d+)$/) { |
5555edea DM |
1420 | # add macaddr |
1421 | my $net = PVE::QemuServer::parse_net($param->{$opt}); | |
1422 | $param->{$opt} = PVE::QemuServer::print_net($net); | |
6ee499ff DC |
1423 | } elsif ($opt eq 'vmgenid') { |
1424 | if ($param->{$opt} eq '1') { | |
1425 | $param->{$opt} = PVE::QemuServer::generate_uuid(); | |
1426 | } | |
9e784b11 DC |
1427 | } elsif ($opt eq 'hookscript') { |
1428 | eval { PVE::GuestHelpers::check_hookscript($param->{$opt}, $storecfg); }; | |
1429 | raise_param_exc({ $opt => $@ }) if $@; | |
1e68cb19 | 1430 | } |
5555edea | 1431 | } |
1e3baf05 | 1432 | |
5555edea | 1433 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]); |
ae57f6b3 | 1434 | |
e30f75c5 | 1435 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]); |
ae57f6b3 | 1436 | |
5555edea | 1437 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param); |
1e3baf05 | 1438 | |
5555edea | 1439 | my $updatefn = sub { |
1e3baf05 | 1440 | |
ffda963f | 1441 | my $conf = PVE::QemuConfig->load_config($vmid); |
1e3baf05 | 1442 | |
5555edea DM |
1443 | die "checksum missmatch (file change by other user?)\n" |
1444 | if $digest && $digest ne $conf->{digest}; | |
1445 | ||
6e72f90b SR |
1446 | &$check_cpu_model_access($rpcenv, $authuser, $param, $conf); |
1447 | ||
546644e2 TL |
1448 | # FIXME: 'suspended' lock should probabyl be a state or "weak" lock?! |
1449 | if (scalar(@delete) && grep { $_ eq 'vmstate'} @delete) { | |
1450 | if (defined($conf->{lock}) && $conf->{lock} eq 'suspended') { | |
1451 | delete $conf->{lock}; # for check lock check, not written out | |
1452 | push @delete, 'lock'; # this is the real deal to write it out | |
1453 | } | |
1454 | push @delete, 'runningmachine' if $conf->{runningmachine}; | |
ea1c2110 | 1455 | push @delete, 'runningcpu' if $conf->{runningcpu}; |
546644e2 TL |
1456 | } |
1457 | ||
ffda963f | 1458 | PVE::QemuConfig->check_lock($conf) if !$skiplock; |
7043d946 | 1459 | |
d3df8cf3 DM |
1460 | foreach my $opt (keys %$revert) { |
1461 | if (defined($conf->{$opt})) { | |
1462 | $param->{$opt} = $conf->{$opt}; | |
1463 | } elsif (defined($conf->{pending}->{$opt})) { | |
1464 | push @delete, $opt; | |
1465 | } | |
1466 | } | |
1467 | ||
5555edea | 1468 | if ($param->{memory} || defined($param->{balloon})) { |
6ca8b698 DM |
1469 | my $maxmem = $param->{memory} || $conf->{pending}->{memory} || $conf->{memory} || $defaults->{memory}; |
1470 | my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{pending}->{balloon} || $conf->{balloon}; | |
7043d946 | 1471 | |
5555edea DM |
1472 | die "balloon value too large (must be smaller than assigned memory)\n" |
1473 | if $balloon && $balloon > $maxmem; | |
1474 | } | |
1e3baf05 | 1475 | |
5555edea | 1476 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr)); |
1e3baf05 | 1477 | |
5555edea | 1478 | my $worker = sub { |
7bfdeb5f | 1479 | |
5555edea | 1480 | print "update VM $vmid: " . join (' ', @paramarr) . "\n"; |
c2a64aa7 | 1481 | |
202d1f45 DM |
1482 | # write updates to pending section |
1483 | ||
3a11fadb DM |
1484 | my $modified = {}; # record what $option we modify |
1485 | ||
11c601e9 TL |
1486 | my @bootorder; |
1487 | if (my $boot = $conf->{boot}) { | |
1488 | my $bootcfg = PVE::JSONSchema::parse_property_string('pve-qm-boot', $boot); | |
1489 | @bootorder = PVE::Tools::split_list($bootcfg->{order}) if $bootcfg && $bootcfg->{order}; | |
1490 | } | |
078c109f SR |
1491 | my $bootorder_deleted = grep {$_ eq 'bootorder'} @delete; |
1492 | ||
bb660bc3 DC |
1493 | my $check_drive_perms = sub { |
1494 | my ($opt, $val) = @_; | |
c1accf9d | 1495 | my $drive = PVE::QemuServer::parse_drive($opt, $val, 1); |
bb660bc3 DC |
1496 | # FIXME: cloudinit: CDROM or Disk? |
1497 | if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM | |
1498 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']); | |
1499 | } else { | |
1500 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
1501 | } | |
1502 | }; | |
1503 | ||
202d1f45 | 1504 | foreach my $opt (@delete) { |
3a11fadb | 1505 | $modified->{$opt} = 1; |
ffda963f | 1506 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload |
f70a6ea9 TL |
1507 | |
1508 | # value of what we want to delete, independent if pending or not | |
1509 | my $val = $conf->{$opt} // $conf->{pending}->{$opt}; | |
1510 | if (!defined($val)) { | |
d2c6bf93 FG |
1511 | warn "cannot delete '$opt' - not set in current configuration!\n"; |
1512 | $modified->{$opt} = 0; | |
1513 | next; | |
1514 | } | |
f70a6ea9 | 1515 | my $is_pending_val = defined($conf->{pending}->{$opt}); |
6aa43f92 | 1516 | delete $conf->{pending}->{$opt}; |
d2c6bf93 | 1517 | |
078c109f SR |
1518 | # remove from bootorder if necessary |
1519 | if (!$bootorder_deleted && @bootorder && grep {$_ eq $opt} @bootorder) { | |
1520 | @bootorder = grep {$_ ne $opt} @bootorder; | |
1521 | $conf->{pending}->{boot} = PVE::QemuServer::print_bootorder(\@bootorder); | |
1522 | $modified->{boot} = 1; | |
1523 | } | |
1524 | ||
202d1f45 | 1525 | if ($opt =~ m/^unused/) { |
f70a6ea9 | 1526 | my $drive = PVE::QemuServer::parse_drive($opt, $val); |
ffda963f | 1527 | PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'"); |
4d8d55f1 | 1528 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); |
3dc38fbb WB |
1529 | if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) { |
1530 | delete $conf->{$opt}; | |
ffda963f | 1531 | PVE::QemuConfig->write_config($vmid, $conf); |
202d1f45 | 1532 | } |
6afb6794 DC |
1533 | } elsif ($opt eq 'vmstate') { |
1534 | PVE::QemuConfig->check_protection($conf, "can't remove vmstate '$val'"); | |
6afb6794 DC |
1535 | if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, { file => $val }, $rpcenv, $authuser, 1)) { |
1536 | delete $conf->{$opt}; | |
1537 | PVE::QemuConfig->write_config($vmid, $conf); | |
1538 | } | |
74479ee9 | 1539 | } elsif (PVE::QemuServer::is_valid_drivename($opt)) { |
ffda963f | 1540 | PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'"); |
bb660bc3 | 1541 | $check_drive_perms->($opt, $val); |
f70a6ea9 TL |
1542 | PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $val)) |
1543 | if $is_pending_val; | |
98bc3aeb | 1544 | PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); |
ffda963f | 1545 | PVE::QemuConfig->write_config($vmid, $conf); |
e30f75c5 | 1546 | } elsif ($opt =~ m/^serial\d+$/) { |
f70a6ea9 | 1547 | if ($val eq 'socket') { |
e5453043 DC |
1548 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); |
1549 | } elsif ($authuser ne 'root@pam') { | |
1550 | die "only root can delete '$opt' config for real devices\n"; | |
1551 | } | |
98bc3aeb | 1552 | PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); |
e5453043 | 1553 | PVE::QemuConfig->write_config($vmid, $conf); |
165be267 | 1554 | } elsif ($opt =~ m/^usb\d+$/) { |
f70a6ea9 | 1555 | if ($val =~ m/spice/) { |
165be267 DC |
1556 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); |
1557 | } elsif ($authuser ne 'root@pam') { | |
1558 | die "only root can delete '$opt' config for real devices\n"; | |
1559 | } | |
98bc3aeb | 1560 | PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); |
165be267 | 1561 | PVE::QemuConfig->write_config($vmid, $conf); |
202d1f45 | 1562 | } else { |
98bc3aeb | 1563 | PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force); |
ffda963f | 1564 | PVE::QemuConfig->write_config($vmid, $conf); |
202d1f45 | 1565 | } |
5d39a182 | 1566 | } |
1e3baf05 | 1567 | |
202d1f45 | 1568 | foreach my $opt (keys %$param) { # add/change |
3a11fadb | 1569 | $modified->{$opt} = 1; |
ffda963f | 1570 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload |
202d1f45 DM |
1571 | next if defined($conf->{pending}->{$opt}) && ($param->{$opt} eq $conf->{pending}->{$opt}); # skip if nothing changed |
1572 | ||
de64f101 | 1573 | my $arch = PVE::QemuServer::get_vm_arch($conf); |
96ed3574 | 1574 | |
74479ee9 | 1575 | if (PVE::QemuServer::is_valid_drivename($opt)) { |
2c44ec49 DC |
1576 | # old drive |
1577 | if ($conf->{$opt}) { | |
1578 | $check_drive_perms->($opt, $conf->{$opt}); | |
1579 | } | |
1580 | ||
1581 | # new drive | |
bb660bc3 | 1582 | $check_drive_perms->($opt, $param->{$opt}); |
055d554d | 1583 | PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt})) |
202d1f45 DM |
1584 | if defined($conf->{pending}->{$opt}); |
1585 | ||
367e6bf4 FE |
1586 | my (undef, $created_opts) = $create_disks->( |
1587 | $rpcenv, | |
1588 | $authuser, | |
1589 | $conf, | |
1590 | $arch, | |
1591 | $storecfg, | |
1592 | $vmid, | |
1593 | undef, | |
1594 | {$opt => $param->{$opt}}, | |
1595 | ); | |
1596 | $conf->{pending}->{$_} = $created_opts->{$_} for keys $created_opts->%*; | |
deb734e3 | 1597 | |
a2e22f9f DC |
1598 | # default legacy boot order implies all cdroms anyway |
1599 | if (@bootorder) { | |
1600 | # append new CD drives to bootorder to mark them bootable | |
c1accf9d | 1601 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}, 1); |
a2e22f9f DC |
1602 | if (PVE::QemuServer::drive_is_cdrom($drive, 1) && !grep(/^$opt$/, @bootorder)) { |
1603 | push @bootorder, $opt; | |
1604 | $conf->{pending}->{boot} = PVE::QemuServer::print_bootorder(\@bootorder); | |
1605 | $modified->{boot} = 1; | |
1606 | } | |
deb734e3 | 1607 | } |
e30f75c5 DC |
1608 | } elsif ($opt =~ m/^serial\d+/) { |
1609 | if ((!defined($conf->{$opt}) || $conf->{$opt} eq 'socket') && $param->{$opt} eq 'socket') { | |
1610 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); | |
1611 | } elsif ($authuser ne 'root@pam') { | |
1612 | die "only root can modify '$opt' config for real devices\n"; | |
1613 | } | |
1614 | $conf->{pending}->{$opt} = $param->{$opt}; | |
165be267 DC |
1615 | } elsif ($opt =~ m/^usb\d+/) { |
1616 | if ((!defined($conf->{$opt}) || $conf->{$opt} =~ m/spice/) && $param->{$opt} =~ m/spice/) { | |
1617 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']); | |
1618 | } elsif ($authuser ne 'root@pam') { | |
1619 | die "only root can modify '$opt' config for real devices\n"; | |
1620 | } | |
1621 | $conf->{pending}->{$opt} = $param->{$opt}; | |
202d1f45 DM |
1622 | } else { |
1623 | $conf->{pending}->{$opt} = $param->{$opt}; | |
078c109f SR |
1624 | |
1625 | if ($opt eq 'boot') { | |
1626 | my $new_bootcfg = PVE::JSONSchema::parse_property_string('pve-qm-boot', $param->{$opt}); | |
1627 | if ($new_bootcfg->{order}) { | |
1628 | my @devs = PVE::Tools::split_list($new_bootcfg->{order}); | |
1629 | for my $dev (@devs) { | |
c9c32c1b | 1630 | my $exists = $conf->{$dev} || $conf->{pending}->{$dev} || $param->{$dev}; |
078c109f SR |
1631 | my $deleted = grep {$_ eq $dev} @delete; |
1632 | die "invalid bootorder: device '$dev' does not exist'\n" | |
1633 | if !$exists || $deleted; | |
1634 | } | |
1635 | ||
1636 | # remove legacy boot order settings if new one set | |
1637 | $conf->{pending}->{$opt} = PVE::QemuServer::print_bootorder(\@devs); | |
1638 | PVE::QemuConfig->add_to_pending_delete($conf, "bootdisk") | |
1639 | if $conf->{bootdisk}; | |
1640 | } | |
1641 | } | |
202d1f45 | 1642 | } |
98bc3aeb | 1643 | PVE::QemuConfig->remove_from_pending_delete($conf, $opt); |
ffda963f | 1644 | PVE::QemuConfig->write_config($vmid, $conf); |
202d1f45 DM |
1645 | } |
1646 | ||
1647 | # remove pending changes when nothing changed | |
ffda963f | 1648 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload |
98bc3aeb | 1649 | my $changes = PVE::QemuConfig->cleanup_pending($conf); |
ffda963f | 1650 | PVE::QemuConfig->write_config($vmid, $conf) if $changes; |
202d1f45 DM |
1651 | |
1652 | return if !scalar(keys %{$conf->{pending}}); | |
1653 | ||
7bfdeb5f | 1654 | my $running = PVE::QemuServer::check_running($vmid); |
39001640 DM |
1655 | |
1656 | # apply pending changes | |
1657 | ||
ffda963f | 1658 | $conf = PVE::QemuConfig->load_config($vmid); # update/reload |
39001640 | 1659 | |
eb5e482d | 1660 | my $errors = {}; |
3a11fadb | 1661 | if ($running) { |
3a11fadb | 1662 | PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors); |
3a11fadb | 1663 | } else { |
4a5cb613 | 1664 | PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $errors); |
3a11fadb | 1665 | } |
eb5e482d | 1666 | raise_param_exc($errors) if scalar(keys %$errors); |
1e68cb19 | 1667 | |
915d3481 | 1668 | return; |
5d39a182 DM |
1669 | }; |
1670 | ||
5555edea DM |
1671 | if ($sync) { |
1672 | &$worker(); | |
d1c1af4b | 1673 | return; |
5555edea DM |
1674 | } else { |
1675 | my $upid = $rpcenv->fork_worker('qmconfig', $vmid, $authuser, $worker); | |
fcdb0117 | 1676 | |
5555edea DM |
1677 | if ($background_delay) { |
1678 | ||
1679 | # Note: It would be better to do that in the Event based HTTPServer | |
7043d946 | 1680 | # to avoid blocking call to sleep. |
5555edea DM |
1681 | |
1682 | my $end_time = time() + $background_delay; | |
1683 | ||
1684 | my $task = PVE::Tools::upid_decode($upid); | |
1685 | ||
1686 | my $running = 1; | |
1687 | while (time() < $end_time) { | |
1688 | $running = PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}); | |
1689 | last if !$running; | |
1690 | sleep(1); # this gets interrupted when child process ends | |
1691 | } | |
1692 | ||
1693 | if (!$running) { | |
1694 | my $status = PVE::Tools::upid_read_status($upid); | |
872cfcf5 | 1695 | return if !PVE::Tools::upid_status_is_error($status); |
ce3fbcd4 | 1696 | die "failed to update VM $vmid: $status\n"; |
5555edea | 1697 | } |
7043d946 | 1698 | } |
5555edea DM |
1699 | |
1700 | return $upid; | |
1701 | } | |
1702 | }; | |
1703 | ||
ffda963f | 1704 | return PVE::QemuConfig->lock_config($vmid, $updatefn); |
5555edea DM |
1705 | }; |
1706 | ||
1707 | my $vm_config_perm_list = [ | |
1708 | 'VM.Config.Disk', | |
1709 | 'VM.Config.CDROM', | |
1710 | 'VM.Config.CPU', | |
1711 | 'VM.Config.Memory', | |
1712 | 'VM.Config.Network', | |
1713 | 'VM.Config.HWType', | |
1714 | 'VM.Config.Options', | |
fc701af7 | 1715 | 'VM.Config.Cloudinit', |
5555edea DM |
1716 | ]; |
1717 | ||
1718 | __PACKAGE__->register_method({ | |
1719 | name => 'update_vm_async', | |
1720 | path => '{vmid}/config', | |
1721 | method => 'POST', | |
1722 | protected => 1, | |
1723 | proxyto => 'node', | |
1724 | description => "Set virtual machine options (asynchrounous API).", | |
1725 | permissions => { | |
1726 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
1727 | }, | |
1728 | parameters => { | |
3326ae19 | 1729 | additionalProperties => 0, |
5555edea DM |
1730 | properties => PVE::QemuServer::json_config_properties( |
1731 | { | |
1732 | node => get_standard_option('pve-node'), | |
1733 | vmid => get_standard_option('pve-vmid'), | |
1734 | skiplock => get_standard_option('skiplock'), | |
1735 | delete => { | |
1736 | type => 'string', format => 'pve-configid-list', | |
1737 | description => "A list of settings you want to delete.", | |
1738 | optional => 1, | |
1739 | }, | |
4c8365fa DM |
1740 | revert => { |
1741 | type => 'string', format => 'pve-configid-list', | |
1742 | description => "Revert a pending change.", | |
1743 | optional => 1, | |
1744 | }, | |
5555edea DM |
1745 | force => { |
1746 | type => 'boolean', | |
1747 | description => $opt_force_description, | |
1748 | optional => 1, | |
1749 | requires => 'delete', | |
1750 | }, | |
1751 | digest => { | |
1752 | type => 'string', | |
1753 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1754 | maxLength => 40, | |
1755 | optional => 1, | |
1756 | }, | |
1757 | background_delay => { | |
1758 | type => 'integer', | |
1759 | description => "Time to wait for the task to finish. We return 'null' if the task finish within that time.", | |
1760 | minimum => 1, | |
1761 | maximum => 30, | |
1762 | optional => 1, | |
1763 | }, | |
c1accf9d FE |
1764 | }, |
1765 | 1, # with_disk_alloc | |
1766 | ), | |
5555edea DM |
1767 | }, |
1768 | returns => { | |
1769 | type => 'string', | |
1770 | optional => 1, | |
1771 | }, | |
1772 | code => $update_vm_api, | |
1773 | }); | |
1774 | ||
1775 | __PACKAGE__->register_method({ | |
1776 | name => 'update_vm', | |
1777 | path => '{vmid}/config', | |
1778 | method => 'PUT', | |
1779 | protected => 1, | |
1780 | proxyto => 'node', | |
1781 | description => "Set virtual machine options (synchrounous API) - You should consider using the POST method instead for any actions involving hotplug or storage allocation.", | |
1782 | permissions => { | |
1783 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
1784 | }, | |
1785 | parameters => { | |
3326ae19 | 1786 | additionalProperties => 0, |
5555edea DM |
1787 | properties => PVE::QemuServer::json_config_properties( |
1788 | { | |
1789 | node => get_standard_option('pve-node'), | |
335af808 | 1790 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
5555edea DM |
1791 | skiplock => get_standard_option('skiplock'), |
1792 | delete => { | |
1793 | type => 'string', format => 'pve-configid-list', | |
1794 | description => "A list of settings you want to delete.", | |
1795 | optional => 1, | |
1796 | }, | |
4c8365fa DM |
1797 | revert => { |
1798 | type => 'string', format => 'pve-configid-list', | |
1799 | description => "Revert a pending change.", | |
1800 | optional => 1, | |
1801 | }, | |
5555edea DM |
1802 | force => { |
1803 | type => 'boolean', | |
1804 | description => $opt_force_description, | |
1805 | optional => 1, | |
1806 | requires => 'delete', | |
1807 | }, | |
1808 | digest => { | |
1809 | type => 'string', | |
1810 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1811 | maxLength => 40, | |
1812 | optional => 1, | |
1813 | }, | |
c1accf9d FE |
1814 | }, |
1815 | 1, # with_disk_alloc | |
1816 | ), | |
5555edea DM |
1817 | }, |
1818 | returns => { type => 'null' }, | |
1819 | code => sub { | |
1820 | my ($param) = @_; | |
1821 | &$update_vm_api($param, 1); | |
d1c1af4b | 1822 | return; |
5555edea DM |
1823 | } |
1824 | }); | |
1e3baf05 | 1825 | |
1e3baf05 | 1826 | __PACKAGE__->register_method({ |
afdb31d5 DM |
1827 | name => 'destroy_vm', |
1828 | path => '{vmid}', | |
1e3baf05 DM |
1829 | method => 'DELETE', |
1830 | protected => 1, | |
1831 | proxyto => 'node', | |
e00319af TL |
1832 | description => "Destroy the VM and all used/owned volumes. Removes any VM specific permissions" |
1833 | ." and firewall rules", | |
a0d1b1a2 DM |
1834 | permissions => { |
1835 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1836 | }, | |
1e3baf05 | 1837 | parameters => { |
3326ae19 | 1838 | additionalProperties => 0, |
1e3baf05 DM |
1839 | properties => { |
1840 | node => get_standard_option('pve-node'), | |
335af808 | 1841 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }), |
3ea94c60 | 1842 | skiplock => get_standard_option('skiplock'), |
d9123ef5 CE |
1843 | purge => { |
1844 | type => 'boolean', | |
e00319af | 1845 | description => "Remove VMID from configurations, like backup & replication jobs and HA.", |
d9123ef5 CE |
1846 | optional => 1, |
1847 | }, | |
75854662 TL |
1848 | 'destroy-unreferenced-disks' => { |
1849 | type => 'boolean', | |
99676a6c TL |
1850 | description => "If set, destroy additionally all disks not referenced in the config" |
1851 | ." but with a matching VMID from all enabled storages.", | |
75854662 | 1852 | optional => 1, |
16e66777 | 1853 | default => 0, |
47f9f50b | 1854 | }, |
1e3baf05 DM |
1855 | }, |
1856 | }, | |
afdb31d5 | 1857 | returns => { |
5fdbe4f0 DM |
1858 | type => 'string', |
1859 | }, | |
1e3baf05 DM |
1860 | code => sub { |
1861 | my ($param) = @_; | |
1862 | ||
1863 | my $rpcenv = PVE::RPCEnvironment::get(); | |
a0d1b1a2 | 1864 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1865 | my $vmid = $param->{vmid}; |
1866 | ||
1867 | my $skiplock = $param->{skiplock}; | |
afdb31d5 | 1868 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1869 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 1870 | |
a4c7029d FG |
1871 | my $early_checks = sub { |
1872 | # test if VM exists | |
1873 | my $conf = PVE::QemuConfig->load_config($vmid); | |
1874 | PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid"); | |
7c4351f7 | 1875 | |
a4c7029d | 1876 | my $ha_managed = PVE::HA::Config::service_is_configured("vm:$vmid"); |
e9f2f8e5 | 1877 | |
a4c7029d FG |
1878 | if (!$param->{purge}) { |
1879 | die "unable to remove VM $vmid - used in HA resources and purge parameter not set.\n" | |
1880 | if $ha_managed; | |
1881 | # don't allow destroy if with replication jobs but no purge param | |
1882 | my $repl_conf = PVE::ReplicationConfig->new(); | |
1883 | $repl_conf->check_for_existing_jobs($vmid); | |
1884 | } | |
628bb7f2 | 1885 | |
a4c7029d FG |
1886 | die "VM $vmid is running - destroy failed\n" |
1887 | if PVE::QemuServer::check_running($vmid); | |
1888 | ||
1889 | return $ha_managed; | |
1890 | }; | |
1891 | ||
1892 | $early_checks->(); | |
db593da2 | 1893 | |
5fdbe4f0 | 1894 | my $realcmd = sub { |
ff1a2432 DM |
1895 | my $upid = shift; |
1896 | ||
a4c7029d FG |
1897 | my $storecfg = PVE::Storage::config(); |
1898 | ||
ff1a2432 | 1899 | syslog('info', "destroy VM $vmid: $upid\n"); |
3e8e214d | 1900 | PVE::QemuConfig->lock_config($vmid, sub { |
a4c7029d FG |
1901 | # repeat, config might have changed |
1902 | my $ha_managed = $early_checks->(); | |
d9123ef5 | 1903 | |
16e66777 | 1904 | my $purge_unreferenced = $param->{'destroy-unreferenced-disks'}; |
75854662 TL |
1905 | |
1906 | PVE::QemuServer::destroy_vm( | |
1907 | $storecfg, | |
1908 | $vmid, | |
1909 | $skiplock, { lock => 'destroyed' }, | |
1910 | $purge_unreferenced, | |
1911 | ); | |
d9123ef5 | 1912 | |
3e8e214d DJ |
1913 | PVE::AccessControl::remove_vm_access($vmid); |
1914 | PVE::Firewall::remove_vmfw_conf($vmid); | |
d9123ef5 | 1915 | if ($param->{purge}) { |
7c4351f7 | 1916 | print "purging VM $vmid from related configurations..\n"; |
d9123ef5 CE |
1917 | PVE::ReplicationConfig::remove_vmid_jobs($vmid); |
1918 | PVE::VZDump::Plugin::remove_vmid_from_backup_jobs($vmid); | |
7c4351f7 TL |
1919 | |
1920 | if ($ha_managed) { | |
1921 | PVE::HA::Config::delete_service_from_config("vm:$vmid"); | |
1922 | print "NOTE: removed VM $vmid from HA resource configuration.\n"; | |
1923 | } | |
d9123ef5 | 1924 | } |
5172770d TL |
1925 | |
1926 | # only now remove the zombie config, else we can have reuse race | |
1927 | PVE::QemuConfig->destroy_config($vmid); | |
3e8e214d | 1928 | }); |
5fdbe4f0 | 1929 | }; |
1e3baf05 | 1930 | |
a0d1b1a2 | 1931 | return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd); |
1e3baf05 DM |
1932 | }}); |
1933 | ||
1934 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1935 | name => 'unlink', |
1936 | path => '{vmid}/unlink', | |
1e3baf05 DM |
1937 | method => 'PUT', |
1938 | protected => 1, | |
1939 | proxyto => 'node', | |
1940 | description => "Unlink/delete disk images.", | |
a0d1b1a2 DM |
1941 | permissions => { |
1942 | check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']], | |
1943 | }, | |
1e3baf05 | 1944 | parameters => { |
3326ae19 | 1945 | additionalProperties => 0, |
1e3baf05 DM |
1946 | properties => { |
1947 | node => get_standard_option('pve-node'), | |
335af808 | 1948 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
1e3baf05 DM |
1949 | idlist => { |
1950 | type => 'string', format => 'pve-configid-list', | |
1951 | description => "A list of disk IDs you want to delete.", | |
1952 | }, | |
1953 | force => { | |
1954 | type => 'boolean', | |
1955 | description => $opt_force_description, | |
1956 | optional => 1, | |
1957 | }, | |
1958 | }, | |
1959 | }, | |
1960 | returns => { type => 'null'}, | |
1961 | code => sub { | |
1962 | my ($param) = @_; | |
1963 | ||
1964 | $param->{delete} = extract_param($param, 'idlist'); | |
1965 | ||
1966 | __PACKAGE__->update_vm($param); | |
1967 | ||
d1c1af4b | 1968 | return; |
1e3baf05 DM |
1969 | }}); |
1970 | ||
3c5bdde8 TL |
1971 | # uses good entropy, each char is limited to 6 bit to get printable chars simply |
1972 | my $gen_rand_chars = sub { | |
1973 | my ($length) = @_; | |
1974 | ||
1975 | die "invalid length $length" if $length < 1; | |
1976 | ||
1977 | my $min = ord('!'); # first printable ascii | |
a4e128a9 FG |
1978 | |
1979 | my $rand_bytes = Crypt::OpenSSL::Random::random_bytes($length); | |
1980 | die "failed to generate random bytes!\n" | |
1981 | if !$rand_bytes; | |
1982 | ||
1983 | my $str = join('', map { chr((ord($_) & 0x3F) + $min) } split('', $rand_bytes)); | |
3c5bdde8 TL |
1984 | |
1985 | return $str; | |
1986 | }; | |
1987 | ||
1e3baf05 DM |
1988 | my $sslcert; |
1989 | ||
1990 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1991 | name => 'vncproxy', |
1992 | path => '{vmid}/vncproxy', | |
1e3baf05 DM |
1993 | method => 'POST', |
1994 | protected => 1, | |
1995 | permissions => { | |
378b359e | 1996 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], |
1e3baf05 DM |
1997 | }, |
1998 | description => "Creates a TCP VNC proxy connections.", | |
1999 | parameters => { | |
3326ae19 | 2000 | additionalProperties => 0, |
1e3baf05 DM |
2001 | properties => { |
2002 | node => get_standard_option('pve-node'), | |
2003 | vmid => get_standard_option('pve-vmid'), | |
b4d5c000 SP |
2004 | websocket => { |
2005 | optional => 1, | |
2006 | type => 'boolean', | |
2007 | description => "starts websockify instead of vncproxy", | |
2008 | }, | |
3c5bdde8 TL |
2009 | 'generate-password' => { |
2010 | optional => 1, | |
2011 | type => 'boolean', | |
2012 | default => 0, | |
2013 | description => "Generates a random password to be used as ticket instead of the API ticket.", | |
2014 | }, | |
1e3baf05 DM |
2015 | }, |
2016 | }, | |
afdb31d5 | 2017 | returns => { |
3326ae19 | 2018 | additionalProperties => 0, |
1e3baf05 DM |
2019 | properties => { |
2020 | user => { type => 'string' }, | |
2021 | ticket => { type => 'string' }, | |
3c5bdde8 TL |
2022 | password => { |
2023 | optional => 1, | |
2024 | description => "Returned if requested with 'generate-password' param." | |
2025 | ." Consists of printable ASCII characters ('!' .. '~').", | |
2026 | type => 'string', | |
2027 | }, | |
1e3baf05 DM |
2028 | cert => { type => 'string' }, |
2029 | port => { type => 'integer' }, | |
2030 | upid => { type => 'string' }, | |
2031 | }, | |
2032 | }, | |
2033 | code => sub { | |
2034 | my ($param) = @_; | |
2035 | ||
2036 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2037 | ||
a0d1b1a2 | 2038 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
2039 | |
2040 | my $vmid = $param->{vmid}; | |
2041 | my $node = $param->{node}; | |
983d4582 | 2042 | my $websocket = $param->{websocket}; |
1e3baf05 | 2043 | |
ffda963f | 2044 | my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists |
326007b2 | 2045 | |
d3efae29 FG |
2046 | my $serial; |
2047 | if ($conf->{vga}) { | |
2048 | my $vga = PVE::QemuServer::parse_vga($conf->{vga}); | |
2049 | $serial = $vga->{type} if $vga->{type} =~ m/^serial\d+$/; | |
2050 | } | |
ef5e2be2 | 2051 | |
b6f39da2 DM |
2052 | my $authpath = "/vms/$vmid"; |
2053 | ||
a0d1b1a2 | 2054 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); |
3c5bdde8 TL |
2055 | my $password = $ticket; |
2056 | if ($param->{'generate-password'}) { | |
2057 | $password = $gen_rand_chars->(8); | |
2058 | } | |
b6f39da2 | 2059 | |
1e3baf05 DM |
2060 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) |
2061 | if !$sslcert; | |
2062 | ||
414b42d8 | 2063 | my $family; |
ef5e2be2 | 2064 | my $remcmd = []; |
afdb31d5 | 2065 | |
4f1be36c | 2066 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { |
414b42d8 | 2067 | (undef, $family) = PVE::Cluster::remote_node_ip($node); |
f42ea29b | 2068 | my $sshinfo = PVE::SSHInfo::get_ssh_info($node); |
b4d5c000 | 2069 | # NOTE: kvm VNC traffic is already TLS encrypted or is known unsecure |
d3efae29 | 2070 | $remcmd = PVE::SSHInfo::ssh_info_to_command($sshinfo, defined($serial) ? '-t' : '-T'); |
af0eba7e WB |
2071 | } else { |
2072 | $family = PVE::Tools::get_host_address_family($node); | |
1e3baf05 DM |
2073 | } |
2074 | ||
af0eba7e WB |
2075 | my $port = PVE::Tools::next_vnc_port($family); |
2076 | ||
afdb31d5 | 2077 | my $timeout = 10; |
1e3baf05 DM |
2078 | |
2079 | my $realcmd = sub { | |
2080 | my $upid = shift; | |
2081 | ||
2082 | syslog('info', "starting vnc proxy $upid\n"); | |
2083 | ||
ef5e2be2 | 2084 | my $cmd; |
1e3baf05 | 2085 | |
d3efae29 | 2086 | if (defined($serial)) { |
b4d5c000 | 2087 | |
d3efae29 | 2088 | my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-iface', $serial, '-escape', '0' ]; |
9e6d6e97 | 2089 | |
ef5e2be2 | 2090 | $cmd = ['/usr/bin/vncterm', '-rfbport', $port, |
fa8ea931 | 2091 | '-timeout', $timeout, '-authpath', $authpath, |
9e6d6e97 DC |
2092 | '-perm', 'Sys.Console']; |
2093 | ||
2094 | if ($param->{websocket}) { | |
3c5bdde8 | 2095 | $ENV{PVE_VNC_TICKET} = $password; # pass ticket to vncterm |
9e6d6e97 DC |
2096 | push @$cmd, '-notls', '-listen', 'localhost'; |
2097 | } | |
2098 | ||
2099 | push @$cmd, '-c', @$remcmd, @$termcmd; | |
2100 | ||
655d7462 | 2101 | PVE::Tools::run_command($cmd); |
9e6d6e97 | 2102 | |
ef5e2be2 | 2103 | } else { |
1e3baf05 | 2104 | |
3c5bdde8 | 2105 | $ENV{LC_PVE_TICKET} = $password if $websocket; # set ticket with "qm vncproxy" |
3e7567e0 | 2106 | |
655d7462 WB |
2107 | $cmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid]; |
2108 | ||
2109 | my $sock = IO::Socket::IP->new( | |
dd32a466 | 2110 | ReuseAddr => 1, |
655d7462 WB |
2111 | Listen => 1, |
2112 | LocalPort => $port, | |
2113 | Proto => 'tcp', | |
2114 | GetAddrInfoFlags => 0, | |
b63f34b8 | 2115 | ) or die "failed to create socket: $!\n"; |
655d7462 WB |
2116 | # Inside the worker we shouldn't have any previous alarms |
2117 | # running anyway...: | |
2118 | alarm(0); | |
2119 | local $SIG{ALRM} = sub { die "connection timed out\n" }; | |
2120 | alarm $timeout; | |
2121 | accept(my $cli, $sock) or die "connection failed: $!\n"; | |
058ff55b | 2122 | alarm(0); |
655d7462 WB |
2123 | close($sock); |
2124 | if (PVE::Tools::run_command($cmd, | |
2125 | output => '>&'.fileno($cli), | |
2126 | input => '<&'.fileno($cli), | |
2127 | noerr => 1) != 0) | |
2128 | { | |
2129 | die "Failed to run vncproxy.\n"; | |
2130 | } | |
ef5e2be2 | 2131 | } |
1e3baf05 | 2132 | |
1e3baf05 DM |
2133 | return; |
2134 | }; | |
2135 | ||
2c7fc947 | 2136 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1); |
1e3baf05 | 2137 | |
3da85107 DM |
2138 | PVE::Tools::wait_for_vnc_port($port); |
2139 | ||
3c5bdde8 | 2140 | my $res = { |
a0d1b1a2 | 2141 | user => $authuser, |
1e3baf05 | 2142 | ticket => $ticket, |
afdb31d5 DM |
2143 | port => $port, |
2144 | upid => $upid, | |
2145 | cert => $sslcert, | |
1e3baf05 | 2146 | }; |
3c5bdde8 TL |
2147 | $res->{password} = $password if $param->{'generate-password'}; |
2148 | ||
2149 | return $res; | |
1e3baf05 DM |
2150 | }}); |
2151 | ||
87302002 DC |
2152 | __PACKAGE__->register_method({ |
2153 | name => 'termproxy', | |
2154 | path => '{vmid}/termproxy', | |
2155 | method => 'POST', | |
2156 | protected => 1, | |
2157 | permissions => { | |
2158 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
2159 | }, | |
2160 | description => "Creates a TCP proxy connections.", | |
2161 | parameters => { | |
2162 | additionalProperties => 0, | |
2163 | properties => { | |
2164 | node => get_standard_option('pve-node'), | |
2165 | vmid => get_standard_option('pve-vmid'), | |
2166 | serial=> { | |
2167 | optional => 1, | |
2168 | type => 'string', | |
2169 | enum => [qw(serial0 serial1 serial2 serial3)], | |
2170 | description => "opens a serial terminal (defaults to display)", | |
2171 | }, | |
2172 | }, | |
2173 | }, | |
2174 | returns => { | |
2175 | additionalProperties => 0, | |
2176 | properties => { | |
2177 | user => { type => 'string' }, | |
2178 | ticket => { type => 'string' }, | |
2179 | port => { type => 'integer' }, | |
2180 | upid => { type => 'string' }, | |
2181 | }, | |
2182 | }, | |
2183 | code => sub { | |
2184 | my ($param) = @_; | |
2185 | ||
2186 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2187 | ||
2188 | my $authuser = $rpcenv->get_user(); | |
2189 | ||
2190 | my $vmid = $param->{vmid}; | |
2191 | my $node = $param->{node}; | |
2192 | my $serial = $param->{serial}; | |
2193 | ||
2194 | my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists | |
2195 | ||
2196 | if (!defined($serial)) { | |
d7856be5 FG |
2197 | if ($conf->{vga}) { |
2198 | my $vga = PVE::QemuServer::parse_vga($conf->{vga}); | |
2199 | $serial = $vga->{type} if $vga->{type} =~ m/^serial\d+$/; | |
87302002 DC |
2200 | } |
2201 | } | |
2202 | ||
2203 | my $authpath = "/vms/$vmid"; | |
2204 | ||
2205 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); | |
2206 | ||
414b42d8 DC |
2207 | my $family; |
2208 | my $remcmd = []; | |
87302002 DC |
2209 | |
2210 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { | |
414b42d8 | 2211 | (undef, $family) = PVE::Cluster::remote_node_ip($node); |
f42ea29b FG |
2212 | my $sshinfo = PVE::SSHInfo::get_ssh_info($node); |
2213 | $remcmd = PVE::SSHInfo::ssh_info_to_command($sshinfo, '-t'); | |
414b42d8 | 2214 | push @$remcmd, '--'; |
87302002 DC |
2215 | } else { |
2216 | $family = PVE::Tools::get_host_address_family($node); | |
2217 | } | |
2218 | ||
2219 | my $port = PVE::Tools::next_vnc_port($family); | |
2220 | ||
ccb88f45 | 2221 | my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-escape', '0']; |
87302002 DC |
2222 | push @$termcmd, '-iface', $serial if $serial; |
2223 | ||
2224 | my $realcmd = sub { | |
2225 | my $upid = shift; | |
2226 | ||
2227 | syslog('info', "starting qemu termproxy $upid\n"); | |
2228 | ||
2229 | my $cmd = ['/usr/bin/termproxy', $port, '--path', $authpath, | |
2230 | '--perm', 'VM.Console', '--']; | |
2231 | push @$cmd, @$remcmd, @$termcmd; | |
2232 | ||
2233 | PVE::Tools::run_command($cmd); | |
2234 | }; | |
2235 | ||
2236 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1); | |
2237 | ||
2238 | PVE::Tools::wait_for_vnc_port($port); | |
2239 | ||
2240 | return { | |
2241 | user => $authuser, | |
2242 | ticket => $ticket, | |
2243 | port => $port, | |
2244 | upid => $upid, | |
2245 | }; | |
2246 | }}); | |
2247 | ||
3e7567e0 DM |
2248 | __PACKAGE__->register_method({ |
2249 | name => 'vncwebsocket', | |
2250 | path => '{vmid}/vncwebsocket', | |
2251 | method => 'GET', | |
3e7567e0 | 2252 | permissions => { |
c422ce93 | 2253 | description => "You also need to pass a valid ticket (vncticket).", |
3e7567e0 DM |
2254 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], |
2255 | }, | |
4d00f52f | 2256 | description => "Opens a weksocket for VNC traffic.", |
3e7567e0 | 2257 | parameters => { |
3326ae19 | 2258 | additionalProperties => 0, |
3e7567e0 DM |
2259 | properties => { |
2260 | node => get_standard_option('pve-node'), | |
2261 | vmid => get_standard_option('pve-vmid'), | |
c422ce93 DM |
2262 | vncticket => { |
2263 | description => "Ticket from previous call to vncproxy.", | |
2264 | type => 'string', | |
2265 | maxLength => 512, | |
2266 | }, | |
3e7567e0 DM |
2267 | port => { |
2268 | description => "Port number returned by previous vncproxy call.", | |
2269 | type => 'integer', | |
2270 | minimum => 5900, | |
2271 | maximum => 5999, | |
2272 | }, | |
2273 | }, | |
2274 | }, | |
2275 | returns => { | |
2276 | type => "object", | |
2277 | properties => { | |
2278 | port => { type => 'string' }, | |
2279 | }, | |
2280 | }, | |
2281 | code => sub { | |
2282 | my ($param) = @_; | |
2283 | ||
2284 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2285 | ||
2286 | my $authuser = $rpcenv->get_user(); | |
2287 | ||
2288 | my $vmid = $param->{vmid}; | |
2289 | my $node = $param->{node}; | |
2290 | ||
c422ce93 DM |
2291 | my $authpath = "/vms/$vmid"; |
2292 | ||
2293 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath); | |
2294 | ||
ffda963f | 2295 | my $conf = PVE::QemuConfig->load_config($vmid, $node); # VM exists ? |
3e7567e0 DM |
2296 | |
2297 | # Note: VNC ports are acessible from outside, so we do not gain any | |
2298 | # security if we verify that $param->{port} belongs to VM $vmid. This | |
2299 | # check is done by verifying the VNC ticket (inside VNC protocol). | |
2300 | ||
2301 | my $port = $param->{port}; | |
f34ebd52 | 2302 | |
3e7567e0 DM |
2303 | return { port => $port }; |
2304 | }}); | |
2305 | ||
288eeea8 DM |
2306 | __PACKAGE__->register_method({ |
2307 | name => 'spiceproxy', | |
2308 | path => '{vmid}/spiceproxy', | |
78252ce7 | 2309 | method => 'POST', |
288eeea8 | 2310 | protected => 1, |
78252ce7 | 2311 | proxyto => 'node', |
288eeea8 DM |
2312 | permissions => { |
2313 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
2314 | }, | |
2315 | description => "Returns a SPICE configuration to connect to the VM.", | |
2316 | parameters => { | |
3326ae19 | 2317 | additionalProperties => 0, |
288eeea8 DM |
2318 | properties => { |
2319 | node => get_standard_option('pve-node'), | |
2320 | vmid => get_standard_option('pve-vmid'), | |
dd25eecf | 2321 | proxy => get_standard_option('spice-proxy', { optional => 1 }), |
288eeea8 DM |
2322 | }, |
2323 | }, | |
dd25eecf | 2324 | returns => get_standard_option('remote-viewer-config'), |
288eeea8 DM |
2325 | code => sub { |
2326 | my ($param) = @_; | |
2327 | ||
2328 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2329 | ||
2330 | my $authuser = $rpcenv->get_user(); | |
2331 | ||
2332 | my $vmid = $param->{vmid}; | |
2333 | my $node = $param->{node}; | |
fb6c7260 | 2334 | my $proxy = $param->{proxy}; |
288eeea8 | 2335 | |
ffda963f | 2336 | my $conf = PVE::QemuConfig->load_config($vmid, $node); |
7f9e28e4 TL |
2337 | my $title = "VM $vmid"; |
2338 | $title .= " - ". $conf->{name} if $conf->{name}; | |
288eeea8 | 2339 | |
943340a6 | 2340 | my $port = PVE::QemuServer::spice_port($vmid); |
dd25eecf | 2341 | |
f34ebd52 | 2342 | my ($ticket, undef, $remote_viewer_config) = |
dd25eecf | 2343 | PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port); |
f34ebd52 | 2344 | |
0a13e08e SR |
2345 | mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket); |
2346 | mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30"); | |
f34ebd52 | 2347 | |
dd25eecf | 2348 | return $remote_viewer_config; |
288eeea8 DM |
2349 | }}); |
2350 | ||
5fdbe4f0 DM |
2351 | __PACKAGE__->register_method({ |
2352 | name => 'vmcmdidx', | |
afdb31d5 | 2353 | path => '{vmid}/status', |
5fdbe4f0 DM |
2354 | method => 'GET', |
2355 | proxyto => 'node', | |
2356 | description => "Directory index", | |
a0d1b1a2 DM |
2357 | permissions => { |
2358 | user => 'all', | |
2359 | }, | |
5fdbe4f0 | 2360 | parameters => { |
3326ae19 | 2361 | additionalProperties => 0, |
5fdbe4f0 DM |
2362 | properties => { |
2363 | node => get_standard_option('pve-node'), | |
2364 | vmid => get_standard_option('pve-vmid'), | |
2365 | }, | |
2366 | }, | |
2367 | returns => { | |
2368 | type => 'array', | |
2369 | items => { | |
2370 | type => "object", | |
2371 | properties => { | |
2372 | subdir => { type => 'string' }, | |
2373 | }, | |
2374 | }, | |
2375 | links => [ { rel => 'child', href => "{subdir}" } ], | |
2376 | }, | |
2377 | code => sub { | |
2378 | my ($param) = @_; | |
2379 | ||
2380 | # test if VM exists | |
ffda963f | 2381 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); |
5fdbe4f0 DM |
2382 | |
2383 | my $res = [ | |
2384 | { subdir => 'current' }, | |
2385 | { subdir => 'start' }, | |
2386 | { subdir => 'stop' }, | |
58f9db6a DC |
2387 | { subdir => 'reset' }, |
2388 | { subdir => 'shutdown' }, | |
2389 | { subdir => 'suspend' }, | |
165411f0 | 2390 | { subdir => 'reboot' }, |
5fdbe4f0 | 2391 | ]; |
afdb31d5 | 2392 | |
5fdbe4f0 DM |
2393 | return $res; |
2394 | }}); | |
2395 | ||
1e3baf05 | 2396 | __PACKAGE__->register_method({ |
afdb31d5 | 2397 | name => 'vm_status', |
5fdbe4f0 | 2398 | path => '{vmid}/status/current', |
1e3baf05 DM |
2399 | method => 'GET', |
2400 | proxyto => 'node', | |
2401 | protected => 1, # qemu pid files are only readable by root | |
2402 | description => "Get virtual machine status.", | |
a0d1b1a2 DM |
2403 | permissions => { |
2404 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
2405 | }, | |
1e3baf05 | 2406 | parameters => { |
3326ae19 | 2407 | additionalProperties => 0, |
1e3baf05 DM |
2408 | properties => { |
2409 | node => get_standard_option('pve-node'), | |
2410 | vmid => get_standard_option('pve-vmid'), | |
2411 | }, | |
2412 | }, | |
b1a70cab DM |
2413 | returns => { |
2414 | type => 'object', | |
2415 | properties => { | |
2416 | %$PVE::QemuServer::vmstatus_return_properties, | |
2417 | ha => { | |
2418 | description => "HA manager service status.", | |
2419 | type => 'object', | |
2420 | }, | |
2421 | spice => { | |
2422 | description => "Qemu VGA configuration supports spice.", | |
2423 | type => 'boolean', | |
2424 | optional => 1, | |
2425 | }, | |
2426 | agent => { | |
2427 | description => "Qemu GuestAgent enabled in config.", | |
2428 | type => 'boolean', | |
2429 | optional => 1, | |
2430 | }, | |
2431 | }, | |
2432 | }, | |
1e3baf05 DM |
2433 | code => sub { |
2434 | my ($param) = @_; | |
2435 | ||
2436 | # test if VM exists | |
ffda963f | 2437 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); |
1e3baf05 | 2438 | |
03a33f30 | 2439 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); |
8610701a | 2440 | my $status = $vmstatus->{$param->{vmid}}; |
1e3baf05 | 2441 | |
4d2a734e | 2442 | $status->{ha} = PVE::HA::Config::get_service_status("vm:$param->{vmid}"); |
8610701a | 2443 | |
3591b62b TL |
2444 | if ($conf->{vga}) { |
2445 | my $vga = PVE::QemuServer::parse_vga($conf->{vga}); | |
2446 | $status->{spice} = 1 | |
f8ea1b30 | 2447 | if $vga->{type} =~ /^virtio/ || PVE::QemuServer::vga_conf_has_spice($conf->{vga}); |
3591b62b | 2448 | } |
a2af1bbe | 2449 | $status->{agent} = 1 if PVE::QemuServer::get_qga_key($conf, 'enabled'); |
c9a074b8 | 2450 | |
8610701a | 2451 | return $status; |
1e3baf05 DM |
2452 | }}); |
2453 | ||
2454 | __PACKAGE__->register_method({ | |
afdb31d5 | 2455 | name => 'vm_start', |
5fdbe4f0 DM |
2456 | path => '{vmid}/status/start', |
2457 | method => 'POST', | |
1e3baf05 DM |
2458 | protected => 1, |
2459 | proxyto => 'node', | |
5fdbe4f0 | 2460 | description => "Start virtual machine.", |
a0d1b1a2 DM |
2461 | permissions => { |
2462 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2463 | }, | |
1e3baf05 | 2464 | parameters => { |
3326ae19 | 2465 | additionalProperties => 0, |
1e3baf05 DM |
2466 | properties => { |
2467 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
2468 | vmid => get_standard_option('pve-vmid', |
2469 | { completion => \&PVE::QemuServer::complete_vmid_stopped }), | |
3ea94c60 DM |
2470 | skiplock => get_standard_option('skiplock'), |
2471 | stateuri => get_standard_option('pve-qm-stateuri'), | |
7e8dcf2c | 2472 | migratedfrom => get_standard_option('pve-node',{ optional => 1 }), |
2de2d6f7 TL |
2473 | migration_type => { |
2474 | type => 'string', | |
2475 | enum => ['secure', 'insecure'], | |
2476 | description => "Migration traffic is encrypted using an SSH " . | |
2477 | "tunnel by default. On secure, completely private networks " . | |
2478 | "this can be disabled to increase performance.", | |
2479 | optional => 1, | |
2480 | }, | |
2481 | migration_network => { | |
29ddbe70 | 2482 | type => 'string', format => 'CIDR', |
2de2d6f7 TL |
2483 | description => "CIDR of the (sub) network that is used for migration.", |
2484 | optional => 1, | |
2485 | }, | |
d58b93a8 | 2486 | machine => get_standard_option('pve-qemu-machine'), |
58c64ad5 SR |
2487 | 'force-cpu' => { |
2488 | description => "Override QEMU's -cpu argument with the given string.", | |
2489 | type => 'string', | |
2490 | optional => 1, | |
2491 | }, | |
bf8fc5a3 | 2492 | targetstorage => get_standard_option('pve-targetstorage'), |
ef3f4293 TM |
2493 | timeout => { |
2494 | description => "Wait maximal timeout seconds.", | |
2495 | type => 'integer', | |
2496 | minimum => 0, | |
5a7f7b99 | 2497 | default => 'max(30, vm memory in GiB)', |
ef3f4293 TM |
2498 | optional => 1, |
2499 | }, | |
1e3baf05 DM |
2500 | }, |
2501 | }, | |
afdb31d5 | 2502 | returns => { |
5fdbe4f0 DM |
2503 | type => 'string', |
2504 | }, | |
1e3baf05 DM |
2505 | code => sub { |
2506 | my ($param) = @_; | |
2507 | ||
2508 | my $rpcenv = PVE::RPCEnvironment::get(); | |
a0d1b1a2 | 2509 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
2510 | |
2511 | my $node = extract_param($param, 'node'); | |
1e3baf05 | 2512 | my $vmid = extract_param($param, 'vmid'); |
ef3f4293 | 2513 | my $timeout = extract_param($param, 'timeout'); |
952958bc DM |
2514 | my $machine = extract_param($param, 'machine'); |
2515 | ||
736c92f6 TL |
2516 | my $get_root_param = sub { |
2517 | my $value = extract_param($param, $_[0]); | |
2518 | raise_param_exc({ "$_[0]" => "Only root may use this option." }) | |
2519 | if $value && $authuser ne 'root@pam'; | |
2520 | return $value; | |
2521 | }; | |
2de2d6f7 | 2522 | |
736c92f6 TL |
2523 | my $stateuri = $get_root_param->('stateuri'); |
2524 | my $skiplock = $get_root_param->('skiplock'); | |
2525 | my $migratedfrom = $get_root_param->('migratedfrom'); | |
2526 | my $migration_type = $get_root_param->('migration_type'); | |
2527 | my $migration_network = $get_root_param->('migration_network'); | |
2528 | my $targetstorage = $get_root_param->('targetstorage'); | |
6ab41628 | 2529 | my $force_cpu = $get_root_param->('force-cpu'); |
2189246c | 2530 | |
bf8fc5a3 FG |
2531 | my $storagemap; |
2532 | ||
2533 | if ($targetstorage) { | |
2534 | raise_param_exc({ targetstorage => "targetstorage can only by used with migratedfrom." }) | |
2535 | if !$migratedfrom; | |
2536 | $storagemap = eval { PVE::JSONSchema::parse_idmap($targetstorage, 'pve-storage-id') }; | |
e214cda8 | 2537 | raise_param_exc({ targetstorage => "failed to parse storage map: $@" }) |
bf8fc5a3 FG |
2538 | if $@; |
2539 | } | |
2189246c | 2540 | |
7c14dcae DM |
2541 | # read spice ticket from STDIN |
2542 | my $spice_ticket; | |
c4ac8f71 | 2543 | my $nbd_protocol_version = 0; |
88126be3 | 2544 | my $replicated_volumes = {}; |
13d121d7 | 2545 | my $offline_volumes = {}; |
ccab68c2 | 2546 | if ($stateuri && ($stateuri eq 'tcp' || $stateuri eq 'unix') && $migratedfrom && ($rpcenv->{type} eq 'cli')) { |
c4ac8f71 | 2547 | while (defined(my $line = <STDIN>)) { |
760fb3c8 | 2548 | chomp $line; |
c4ac8f71 ML |
2549 | if ($line =~ m/^spice_ticket: (.+)$/) { |
2550 | $spice_ticket = $1; | |
2551 | } elsif ($line =~ m/^nbd_protocol_version: (\d+)$/) { | |
2552 | $nbd_protocol_version = $1; | |
88126be3 FG |
2553 | } elsif ($line =~ m/^replicated_volume: (.*)$/) { |
2554 | $replicated_volumes->{$1} = 1; | |
13d121d7 FE |
2555 | } elsif ($line =~ m/^tpmstate0: (.*)$/) { # Deprecated, use offline_volume instead |
2556 | $offline_volumes->{tpmstate0} = $1; | |
2557 | } elsif ($line =~ m/^offline_volume: ([^:]+): (.*)$/) { | |
2558 | $offline_volumes->{$1} = $2; | |
399ca0d6 | 2559 | } elsif (!$spice_ticket) { |
c4ac8f71 ML |
2560 | # fallback for old source node |
2561 | $spice_ticket = $line; | |
399ca0d6 FG |
2562 | } else { |
2563 | warn "unknown 'start' parameter on STDIN: '$line'\n"; | |
c4ac8f71 | 2564 | } |
760fb3c8 | 2565 | } |
7c14dcae DM |
2566 | } |
2567 | ||
98cbd0f4 WB |
2568 | PVE::Cluster::check_cfs_quorum(); |
2569 | ||
afdb31d5 | 2570 | my $storecfg = PVE::Storage::config(); |
5fdbe4f0 | 2571 | |
a4262553 | 2572 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && !$stateuri && $rpcenv->{type} ne 'ha') { |
88fc87b4 DM |
2573 | my $hacmd = sub { |
2574 | my $upid = shift; | |
5fdbe4f0 | 2575 | |
02765844 | 2576 | print "Requesting HA start for VM $vmid\n"; |
88fc87b4 | 2577 | |
a4262553 | 2578 | my $cmd = ['ha-manager', 'set', "vm:$vmid", '--state', 'started']; |
88fc87b4 | 2579 | PVE::Tools::run_command($cmd); |
88fc87b4 DM |
2580 | return; |
2581 | }; | |
2582 | ||
2583 | return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd); | |
2584 | ||
2585 | } else { | |
2586 | ||
2587 | my $realcmd = sub { | |
2588 | my $upid = shift; | |
2589 | ||
2590 | syslog('info', "start VM $vmid: $upid\n"); | |
2591 | ||
0c498cca FG |
2592 | my $migrate_opts = { |
2593 | migratedfrom => $migratedfrom, | |
2594 | spice_ticket => $spice_ticket, | |
2595 | network => $migration_network, | |
2596 | type => $migration_type, | |
bf8fc5a3 | 2597 | storagemap => $storagemap, |
0c498cca FG |
2598 | nbd_proto_version => $nbd_protocol_version, |
2599 | replicated_volumes => $replicated_volumes, | |
13d121d7 | 2600 | offline_volumes => $offline_volumes, |
0c498cca FG |
2601 | }; |
2602 | ||
2603 | my $params = { | |
2604 | statefile => $stateuri, | |
2605 | skiplock => $skiplock, | |
2606 | forcemachine => $machine, | |
2607 | timeout => $timeout, | |
58c64ad5 | 2608 | forcecpu => $force_cpu, |
0c498cca FG |
2609 | }; |
2610 | ||
2611 | PVE::QemuServer::vm_start($storecfg, $vmid, $params, $migrate_opts); | |
88fc87b4 DM |
2612 | return; |
2613 | }; | |
5fdbe4f0 | 2614 | |
88fc87b4 DM |
2615 | return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd); |
2616 | } | |
5fdbe4f0 DM |
2617 | }}); |
2618 | ||
2619 | __PACKAGE__->register_method({ | |
afdb31d5 | 2620 | name => 'vm_stop', |
5fdbe4f0 DM |
2621 | path => '{vmid}/status/stop', |
2622 | method => 'POST', | |
2623 | protected => 1, | |
2624 | proxyto => 'node', | |
346130b2 | 2625 | description => "Stop virtual machine. The qemu process will exit immediately. This" . |
d6c747ff | 2626 | "is akin to pulling the power plug of a running computer and may damage the VM data", |
a0d1b1a2 DM |
2627 | permissions => { |
2628 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2629 | }, | |
5fdbe4f0 | 2630 | parameters => { |
3326ae19 | 2631 | additionalProperties => 0, |
5fdbe4f0 DM |
2632 | properties => { |
2633 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
2634 | vmid => get_standard_option('pve-vmid', |
2635 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 | 2636 | skiplock => get_standard_option('skiplock'), |
debe8882 | 2637 | migratedfrom => get_standard_option('pve-node', { optional => 1 }), |
c6bb9502 DM |
2638 | timeout => { |
2639 | description => "Wait maximal timeout seconds.", | |
2640 | type => 'integer', | |
2641 | minimum => 0, | |
2642 | optional => 1, | |
254575e9 DM |
2643 | }, |
2644 | keepActive => { | |
94a17e1d | 2645 | description => "Do not deactivate storage volumes.", |
254575e9 DM |
2646 | type => 'boolean', |
2647 | optional => 1, | |
2648 | default => 0, | |
c6bb9502 | 2649 | } |
5fdbe4f0 DM |
2650 | }, |
2651 | }, | |
afdb31d5 | 2652 | returns => { |
5fdbe4f0 DM |
2653 | type => 'string', |
2654 | }, | |
2655 | code => sub { | |
2656 | my ($param) = @_; | |
2657 | ||
2658 | my $rpcenv = PVE::RPCEnvironment::get(); | |
a0d1b1a2 | 2659 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
2660 | |
2661 | my $node = extract_param($param, 'node'); | |
5fdbe4f0 DM |
2662 | my $vmid = extract_param($param, 'vmid'); |
2663 | ||
2664 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 2665 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 2666 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 2667 | |
254575e9 | 2668 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 2669 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 2670 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 2671 | |
af30308f DM |
2672 | my $migratedfrom = extract_param($param, 'migratedfrom'); |
2673 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
2674 | if $migratedfrom && $authuser ne 'root@pam'; | |
2675 | ||
2676 | ||
ff1a2432 DM |
2677 | my $storecfg = PVE::Storage::config(); |
2678 | ||
2003f0f8 | 2679 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && ($rpcenv->{type} ne 'ha') && !defined($migratedfrom)) { |
5fdbe4f0 | 2680 | |
88fc87b4 DM |
2681 | my $hacmd = sub { |
2682 | my $upid = shift; | |
5fdbe4f0 | 2683 | |
02765844 | 2684 | print "Requesting HA stop for VM $vmid\n"; |
88fc87b4 | 2685 | |
1805fac3 | 2686 | my $cmd = ['ha-manager', 'crm-command', 'stop', "vm:$vmid", '0']; |
88fc87b4 | 2687 | PVE::Tools::run_command($cmd); |
88fc87b4 DM |
2688 | return; |
2689 | }; | |
2690 | ||
2691 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
2692 | ||
2693 | } else { | |
2694 | my $realcmd = sub { | |
2695 | my $upid = shift; | |
2696 | ||
2697 | syslog('info', "stop VM $vmid: $upid\n"); | |
2698 | ||
2699 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, | |
af30308f | 2700 | $param->{timeout}, 0, 1, $keepActive, $migratedfrom); |
88fc87b4 DM |
2701 | return; |
2702 | }; | |
2703 | ||
2704 | return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd); | |
2705 | } | |
5fdbe4f0 DM |
2706 | }}); |
2707 | ||
2708 | __PACKAGE__->register_method({ | |
afdb31d5 | 2709 | name => 'vm_reset', |
5fdbe4f0 DM |
2710 | path => '{vmid}/status/reset', |
2711 | method => 'POST', | |
2712 | protected => 1, | |
2713 | proxyto => 'node', | |
2714 | description => "Reset virtual machine.", | |
a0d1b1a2 DM |
2715 | permissions => { |
2716 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2717 | }, | |
5fdbe4f0 | 2718 | parameters => { |
3326ae19 | 2719 | additionalProperties => 0, |
5fdbe4f0 DM |
2720 | properties => { |
2721 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
2722 | vmid => get_standard_option('pve-vmid', |
2723 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 DM |
2724 | skiplock => get_standard_option('skiplock'), |
2725 | }, | |
2726 | }, | |
afdb31d5 | 2727 | returns => { |
5fdbe4f0 DM |
2728 | type => 'string', |
2729 | }, | |
2730 | code => sub { | |
2731 | my ($param) = @_; | |
2732 | ||
2733 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2734 | ||
a0d1b1a2 | 2735 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
2736 | |
2737 | my $node = extract_param($param, 'node'); | |
2738 | ||
2739 | my $vmid = extract_param($param, 'vmid'); | |
2740 | ||
2741 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 2742 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 2743 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 2744 | |
ff1a2432 DM |
2745 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
2746 | ||
5fdbe4f0 DM |
2747 | my $realcmd = sub { |
2748 | my $upid = shift; | |
2749 | ||
1e3baf05 | 2750 | PVE::QemuServer::vm_reset($vmid, $skiplock); |
5fdbe4f0 DM |
2751 | |
2752 | return; | |
2753 | }; | |
2754 | ||
a0d1b1a2 | 2755 | return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
2756 | }}); |
2757 | ||
2758 | __PACKAGE__->register_method({ | |
afdb31d5 | 2759 | name => 'vm_shutdown', |
5fdbe4f0 DM |
2760 | path => '{vmid}/status/shutdown', |
2761 | method => 'POST', | |
2762 | protected => 1, | |
2763 | proxyto => 'node', | |
d6c747ff EK |
2764 | description => "Shutdown virtual machine. This is similar to pressing the power button on a physical machine." . |
2765 | "This will send an ACPI event for the guest OS, which should then proceed to a clean shutdown.", | |
a0d1b1a2 DM |
2766 | permissions => { |
2767 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2768 | }, | |
5fdbe4f0 | 2769 | parameters => { |
3326ae19 | 2770 | additionalProperties => 0, |
5fdbe4f0 DM |
2771 | properties => { |
2772 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
2773 | vmid => get_standard_option('pve-vmid', |
2774 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 | 2775 | skiplock => get_standard_option('skiplock'), |
c6bb9502 DM |
2776 | timeout => { |
2777 | description => "Wait maximal timeout seconds.", | |
2778 | type => 'integer', | |
2779 | minimum => 0, | |
2780 | optional => 1, | |
9269013a DM |
2781 | }, |
2782 | forceStop => { | |
2783 | description => "Make sure the VM stops.", | |
2784 | type => 'boolean', | |
2785 | optional => 1, | |
2786 | default => 0, | |
254575e9 DM |
2787 | }, |
2788 | keepActive => { | |
94a17e1d | 2789 | description => "Do not deactivate storage volumes.", |
254575e9 DM |
2790 | type => 'boolean', |
2791 | optional => 1, | |
2792 | default => 0, | |
c6bb9502 | 2793 | } |
5fdbe4f0 DM |
2794 | }, |
2795 | }, | |
afdb31d5 | 2796 | returns => { |
5fdbe4f0 DM |
2797 | type => 'string', |
2798 | }, | |
2799 | code => sub { | |
2800 | my ($param) = @_; | |
2801 | ||
2802 | my $rpcenv = PVE::RPCEnvironment::get(); | |
a0d1b1a2 | 2803 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
2804 | |
2805 | my $node = extract_param($param, 'node'); | |
5fdbe4f0 DM |
2806 | my $vmid = extract_param($param, 'vmid'); |
2807 | ||
2808 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 2809 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 2810 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 2811 | |
254575e9 | 2812 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 2813 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 2814 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 2815 | |
02d07cf5 DM |
2816 | my $storecfg = PVE::Storage::config(); |
2817 | ||
89897367 DC |
2818 | my $shutdown = 1; |
2819 | ||
2820 | # if vm is paused, do not shutdown (but stop if forceStop = 1) | |
2821 | # otherwise, we will infer a shutdown command, but run into the timeout, | |
2822 | # then when the vm is resumed, it will instantly shutdown | |
2823 | # | |
2824 | # checking the qmp status here to get feedback to the gui/cli/api | |
2825 | # and the status query should not take too long | |
b08c37c3 | 2826 | if (PVE::QemuServer::vm_is_paused($vmid)) { |
89897367 DC |
2827 | if ($param->{forceStop}) { |
2828 | warn "VM is paused - stop instead of shutdown\n"; | |
2829 | $shutdown = 0; | |
2830 | } else { | |
2831 | die "VM is paused - cannot shutdown\n"; | |
2832 | } | |
2833 | } | |
2834 | ||
a4262553 | 2835 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { |
5fdbe4f0 | 2836 | |
1805fac3 | 2837 | my $timeout = $param->{timeout} // 60; |
ae849692 DM |
2838 | my $hacmd = sub { |
2839 | my $upid = shift; | |
5fdbe4f0 | 2840 | |
02765844 | 2841 | print "Requesting HA stop for VM $vmid\n"; |
ae849692 | 2842 | |
1805fac3 | 2843 | my $cmd = ['ha-manager', 'crm-command', 'stop', "vm:$vmid", "$timeout"]; |
ae849692 | 2844 | PVE::Tools::run_command($cmd); |
ae849692 DM |
2845 | return; |
2846 | }; | |
2847 | ||
2848 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
2849 | ||
2850 | } else { | |
2851 | ||
2852 | my $realcmd = sub { | |
2853 | my $upid = shift; | |
2854 | ||
2855 | syslog('info', "shutdown VM $vmid: $upid\n"); | |
5fdbe4f0 | 2856 | |
ae849692 DM |
2857 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout}, |
2858 | $shutdown, $param->{forceStop}, $keepActive); | |
ae849692 DM |
2859 | return; |
2860 | }; | |
2861 | ||
2862 | return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd); | |
2863 | } | |
5fdbe4f0 DM |
2864 | }}); |
2865 | ||
165411f0 DC |
2866 | __PACKAGE__->register_method({ |
2867 | name => 'vm_reboot', | |
2868 | path => '{vmid}/status/reboot', | |
2869 | method => 'POST', | |
2870 | protected => 1, | |
2871 | proxyto => 'node', | |
2872 | description => "Reboot the VM by shutting it down, and starting it again. Applies pending changes.", | |
2873 | permissions => { | |
2874 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
2875 | }, | |
2876 | parameters => { | |
2877 | additionalProperties => 0, | |
2878 | properties => { | |
2879 | node => get_standard_option('pve-node'), | |
2880 | vmid => get_standard_option('pve-vmid', | |
2881 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
2882 | timeout => { | |
2883 | description => "Wait maximal timeout seconds for the shutdown.", | |
2884 | type => 'integer', | |
2885 | minimum => 0, | |
2886 | optional => 1, | |
2887 | }, | |
2888 | }, | |
2889 | }, | |
2890 | returns => { | |
2891 | type => 'string', | |
2892 | }, | |
2893 | code => sub { | |
2894 | my ($param) = @_; | |
2895 | ||
2896 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2897 | my $authuser = $rpcenv->get_user(); | |
2898 | ||
2899 | my $node = extract_param($param, 'node'); | |
2900 | my $vmid = extract_param($param, 'vmid'); | |
2901 | ||
b08c37c3 | 2902 | die "VM is paused - cannot shutdown\n" if PVE::QemuServer::vm_is_paused($vmid); |
165411f0 DC |
2903 | |
2904 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
2905 | ||
2906 | my $realcmd = sub { | |
2907 | my $upid = shift; | |
2908 | ||
2909 | syslog('info', "requesting reboot of VM $vmid: $upid\n"); | |
2910 | PVE::QemuServer::vm_reboot($vmid, $param->{timeout}); | |
2911 | return; | |
2912 | }; | |
2913 | ||
2914 | return $rpcenv->fork_worker('qmreboot', $vmid, $authuser, $realcmd); | |
2915 | }}); | |
2916 | ||
5fdbe4f0 | 2917 | __PACKAGE__->register_method({ |
afdb31d5 | 2918 | name => 'vm_suspend', |
5fdbe4f0 DM |
2919 | path => '{vmid}/status/suspend', |
2920 | method => 'POST', | |
2921 | protected => 1, | |
2922 | proxyto => 'node', | |
2923 | description => "Suspend virtual machine.", | |
a0d1b1a2 | 2924 | permissions => { |
75c24bba DC |
2925 | description => "You need 'VM.PowerMgmt' on /vms/{vmid}, and if you have set 'todisk',". |
2926 | " you need also 'VM.Config.Disk' on /vms/{vmid} and 'Datastore.AllocateSpace'". | |
2927 | " on the storage for the vmstate.", | |
a0d1b1a2 DM |
2928 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], |
2929 | }, | |
5fdbe4f0 | 2930 | parameters => { |
3326ae19 | 2931 | additionalProperties => 0, |
5fdbe4f0 DM |
2932 | properties => { |
2933 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
2934 | vmid => get_standard_option('pve-vmid', |
2935 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 | 2936 | skiplock => get_standard_option('skiplock'), |
22371fe0 DC |
2937 | todisk => { |
2938 | type => 'boolean', | |
2939 | default => 0, | |
2940 | optional => 1, | |
2941 | description => 'If set, suspends the VM to disk. Will be resumed on next VM start.', | |
2942 | }, | |
48b4cdc2 DC |
2943 | statestorage => get_standard_option('pve-storage-id', { |
2944 | description => "The storage for the VM state", | |
2945 | requires => 'todisk', | |
2946 | optional => 1, | |
2947 | completion => \&PVE::Storage::complete_storage_enabled, | |
2948 | }), | |
5fdbe4f0 DM |
2949 | }, |
2950 | }, | |
afdb31d5 | 2951 | returns => { |
5fdbe4f0 DM |
2952 | type => 'string', |
2953 | }, | |
2954 | code => sub { | |
2955 | my ($param) = @_; | |
2956 | ||
2957 | my $rpcenv = PVE::RPCEnvironment::get(); | |
a0d1b1a2 | 2958 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
2959 | |
2960 | my $node = extract_param($param, 'node'); | |
5fdbe4f0 DM |
2961 | my $vmid = extract_param($param, 'vmid'); |
2962 | ||
22371fe0 DC |
2963 | my $todisk = extract_param($param, 'todisk') // 0; |
2964 | ||
48b4cdc2 DC |
2965 | my $statestorage = extract_param($param, 'statestorage'); |
2966 | ||
5fdbe4f0 | 2967 | my $skiplock = extract_param($param, 'skiplock'); |
afdb31d5 | 2968 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 2969 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 2970 | |
ff1a2432 DM |
2971 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
2972 | ||
22371fe0 DC |
2973 | die "Cannot suspend HA managed VM to disk\n" |
2974 | if $todisk && PVE::HA::Config::vm_is_ha_managed($vmid); | |
2975 | ||
75c24bba DC |
2976 | # early check for storage permission, for better user feedback |
2977 | if ($todisk) { | |
2978 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
2979 | ||
2980 | if (!$statestorage) { | |
2981 | # get statestorage from config if none is given | |
2982 | my $conf = PVE::QemuConfig->load_config($vmid); | |
2983 | my $storecfg = PVE::Storage::config(); | |
2984 | $statestorage = PVE::QemuServer::find_vmstate_storage($conf, $storecfg); | |
2985 | } | |
2986 | ||
2987 | $rpcenv->check($authuser, "/storage/$statestorage", ['Datastore.AllocateSpace']); | |
2988 | } | |
2989 | ||
5fdbe4f0 DM |
2990 | my $realcmd = sub { |
2991 | my $upid = shift; | |
2992 | ||
2993 | syslog('info', "suspend VM $vmid: $upid\n"); | |
2994 | ||
48b4cdc2 | 2995 | PVE::QemuServer::vm_suspend($vmid, $skiplock, $todisk, $statestorage); |
5fdbe4f0 DM |
2996 | |
2997 | return; | |
2998 | }; | |
2999 | ||
a4262553 | 3000 | my $taskname = $todisk ? 'qmsuspend' : 'qmpause'; |
f17fb184 | 3001 | return $rpcenv->fork_worker($taskname, $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
3002 | }}); |
3003 | ||
3004 | __PACKAGE__->register_method({ | |
afdb31d5 | 3005 | name => 'vm_resume', |
5fdbe4f0 DM |
3006 | path => '{vmid}/status/resume', |
3007 | method => 'POST', | |
3008 | protected => 1, | |
3009 | proxyto => 'node', | |
3010 | description => "Resume virtual machine.", | |
a0d1b1a2 DM |
3011 | permissions => { |
3012 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
3013 | }, | |
5fdbe4f0 | 3014 | parameters => { |
3326ae19 | 3015 | additionalProperties => 0, |
5fdbe4f0 DM |
3016 | properties => { |
3017 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
3018 | vmid => get_standard_option('pve-vmid', |
3019 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 | 3020 | skiplock => get_standard_option('skiplock'), |
289e0b85 AD |
3021 | nocheck => { type => 'boolean', optional => 1 }, |
3022 | ||
5fdbe4f0 DM |
3023 | }, |
3024 | }, | |
afdb31d5 | 3025 | returns => { |
5fdbe4f0 DM |
3026 | type => 'string', |
3027 | }, | |
3028 | code => sub { | |
3029 | my ($param) = @_; | |
3030 | ||
3031 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3032 | ||
a0d1b1a2 | 3033 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
3034 | |
3035 | my $node = extract_param($param, 'node'); | |
3036 | ||
3037 | my $vmid = extract_param($param, 'vmid'); | |
3038 | ||
3039 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 3040 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 3041 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 3042 | |
289e0b85 | 3043 | my $nocheck = extract_param($param, 'nocheck'); |
4fb85adc FG |
3044 | raise_param_exc({ nocheck => "Only root may use this option." }) |
3045 | if $nocheck && $authuser ne 'root@pam'; | |
289e0b85 | 3046 | |
cd9a035b TL |
3047 | my $to_disk_suspended; |
3048 | eval { | |
3049 | PVE::QemuConfig->lock_config($vmid, sub { | |
3050 | my $conf = PVE::QemuConfig->load_config($vmid); | |
3051 | $to_disk_suspended = PVE::QemuConfig->has_lock($conf, 'suspended'); | |
3052 | }); | |
3053 | }; | |
3054 | ||
3055 | die "VM $vmid not running\n" | |
3056 | if !$to_disk_suspended && !PVE::QemuServer::check_running($vmid, $nocheck); | |
ff1a2432 | 3057 | |
5fdbe4f0 DM |
3058 | my $realcmd = sub { |
3059 | my $upid = shift; | |
3060 | ||
3061 | syslog('info', "resume VM $vmid: $upid\n"); | |
3062 | ||
cd9a035b TL |
3063 | if (!$to_disk_suspended) { |
3064 | PVE::QemuServer::vm_resume($vmid, $skiplock, $nocheck); | |
3065 | } else { | |
3066 | my $storecfg = PVE::Storage::config(); | |
0c498cca | 3067 | PVE::QemuServer::vm_start($storecfg, $vmid, { skiplock => $skiplock }); |
cd9a035b | 3068 | } |
1e3baf05 | 3069 | |
5fdbe4f0 DM |
3070 | return; |
3071 | }; | |
3072 | ||
a0d1b1a2 | 3073 | return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
3074 | }}); |
3075 | ||
3076 | __PACKAGE__->register_method({ | |
afdb31d5 | 3077 | name => 'vm_sendkey', |
5fdbe4f0 DM |
3078 | path => '{vmid}/sendkey', |
3079 | method => 'PUT', | |
3080 | protected => 1, | |
3081 | proxyto => 'node', | |
3082 | description => "Send key event to virtual machine.", | |
a0d1b1a2 DM |
3083 | permissions => { |
3084 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
3085 | }, | |
5fdbe4f0 | 3086 | parameters => { |
3326ae19 | 3087 | additionalProperties => 0, |
5fdbe4f0 DM |
3088 | properties => { |
3089 | node => get_standard_option('pve-node'), | |
ab5904f7 TL |
3090 | vmid => get_standard_option('pve-vmid', |
3091 | { completion => \&PVE::QemuServer::complete_vmid_running }), | |
5fdbe4f0 DM |
3092 | skiplock => get_standard_option('skiplock'), |
3093 | key => { | |
3094 | description => "The key (qemu monitor encoding).", | |
3095 | type => 'string' | |
3096 | } | |
3097 | }, | |
3098 | }, | |
3099 | returns => { type => 'null'}, | |
3100 | code => sub { | |
3101 | my ($param) = @_; | |
3102 | ||
3103 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3104 | ||
a0d1b1a2 | 3105 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
3106 | |
3107 | my $node = extract_param($param, 'node'); | |
3108 | ||
3109 | my $vmid = extract_param($param, 'vmid'); | |
3110 | ||
3111 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 3112 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 3113 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 DM |
3114 | |
3115 | PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); | |
3116 | ||
3117 | return; | |
1e3baf05 DM |
3118 | }}); |
3119 | ||
1ac0d2ee AD |
3120 | __PACKAGE__->register_method({ |
3121 | name => 'vm_feature', | |
3122 | path => '{vmid}/feature', | |
3123 | method => 'GET', | |
3124 | proxyto => 'node', | |
75466c4f | 3125 | protected => 1, |
1ac0d2ee AD |
3126 | description => "Check if feature for virtual machine is available.", |
3127 | permissions => { | |
3128 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
3129 | }, | |
3130 | parameters => { | |
3326ae19 | 3131 | additionalProperties => 0, |
1ac0d2ee AD |
3132 | properties => { |
3133 | node => get_standard_option('pve-node'), | |
3134 | vmid => get_standard_option('pve-vmid'), | |
3135 | feature => { | |
3136 | description => "Feature to check.", | |
3137 | type => 'string', | |
7758ce86 | 3138 | enum => [ 'snapshot', 'clone', 'copy' ], |
1ac0d2ee AD |
3139 | }, |
3140 | snapname => get_standard_option('pve-snapshot-name', { | |
3141 | optional => 1, | |
3142 | }), | |
3143 | }, | |
1ac0d2ee AD |
3144 | }, |
3145 | returns => { | |
719893a9 DM |
3146 | type => "object", |
3147 | properties => { | |
3148 | hasFeature => { type => 'boolean' }, | |
7043d946 | 3149 | nodes => { |
719893a9 DM |
3150 | type => 'array', |
3151 | items => { type => 'string' }, | |
3152 | } | |
3153 | }, | |
1ac0d2ee AD |
3154 | }, |
3155 | code => sub { | |
3156 | my ($param) = @_; | |
3157 | ||
3158 | my $node = extract_param($param, 'node'); | |
3159 | ||
3160 | my $vmid = extract_param($param, 'vmid'); | |
3161 | ||
3162 | my $snapname = extract_param($param, 'snapname'); | |
3163 | ||
3164 | my $feature = extract_param($param, 'feature'); | |
3165 | ||
3166 | my $running = PVE::QemuServer::check_running($vmid); | |
3167 | ||
ffda963f | 3168 | my $conf = PVE::QemuConfig->load_config($vmid); |
1ac0d2ee AD |
3169 | |
3170 | if($snapname){ | |
3171 | my $snap = $conf->{snapshots}->{$snapname}; | |
3172 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
3173 | $conf = $snap; | |
3174 | } | |
3175 | my $storecfg = PVE::Storage::config(); | |
3176 | ||
719893a9 | 3177 | my $nodelist = PVE::QemuServer::shared_nodes($conf, $storecfg); |
b2c9558d | 3178 | my $hasFeature = PVE::QemuConfig->has_feature($feature, $conf, $storecfg, $snapname, $running); |
7043d946 | 3179 | |
719893a9 DM |
3180 | return { |
3181 | hasFeature => $hasFeature, | |
3182 | nodes => [ keys %$nodelist ], | |
7043d946 | 3183 | }; |
1ac0d2ee AD |
3184 | }}); |
3185 | ||
6116f729 | 3186 | __PACKAGE__->register_method({ |
9418baad DM |
3187 | name => 'clone_vm', |
3188 | path => '{vmid}/clone', | |
6116f729 DM |
3189 | method => 'POST', |
3190 | protected => 1, | |
3191 | proxyto => 'node', | |
37329185 | 3192 | description => "Create a copy of virtual machine/template.", |
6116f729 | 3193 | permissions => { |
9418baad | 3194 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " . |
6116f729 DM |
3195 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . |
3196 | "'Datastore.AllocateSpace' on any used storage.", | |
75466c4f DM |
3197 | check => |
3198 | [ 'and', | |
9418baad | 3199 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], |
75466c4f | 3200 | [ 'or', |
6116f729 DM |
3201 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], |
3202 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
3203 | ], | |
3204 | ] | |
3205 | }, | |
3206 | parameters => { | |
3326ae19 | 3207 | additionalProperties => 0, |
6116f729 | 3208 | properties => { |
6116f729 | 3209 | node => get_standard_option('pve-node'), |
335af808 | 3210 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
1ae43f8c DM |
3211 | newid => get_standard_option('pve-vmid', { |
3212 | completion => \&PVE::Cluster::complete_next_vmid, | |
3213 | description => 'VMID for the clone.' }), | |
a60ab1a6 DM |
3214 | name => { |
3215 | optional => 1, | |
3216 | type => 'string', format => 'dns-name', | |
3217 | description => "Set a name for the new VM.", | |
3218 | }, | |
3219 | description => { | |
3220 | optional => 1, | |
3221 | type => 'string', | |
3222 | description => "Description for the new VM.", | |
3223 | }, | |
75466c4f | 3224 | pool => { |
6116f729 DM |
3225 | optional => 1, |
3226 | type => 'string', format => 'pve-poolid', | |
3227 | description => "Add the new VM to the specified pool.", | |
3228 | }, | |
9076d880 | 3229 | snapname => get_standard_option('pve-snapshot-name', { |
9076d880 DM |
3230 | optional => 1, |
3231 | }), | |
81f043eb | 3232 | storage => get_standard_option('pve-storage-id', { |
9418baad | 3233 | description => "Target storage for full clone.", |
81f043eb AD |
3234 | optional => 1, |
3235 | }), | |
55173c6b | 3236 | 'format' => { |
fd13b1d0 | 3237 | description => "Target format for file storage. Only valid for full clone.", |
42a19c87 AD |
3238 | type => 'string', |
3239 | optional => 1, | |
55173c6b | 3240 | enum => [ 'raw', 'qcow2', 'vmdk'], |
42a19c87 | 3241 | }, |
6116f729 DM |
3242 | full => { |
3243 | optional => 1, | |
55173c6b | 3244 | type => 'boolean', |
fd13b1d0 | 3245 | description => "Create a full copy of all disks. This is always done when " . |
9418baad | 3246 | "you clone a normal VM. For VM templates, we try to create a linked clone by default.", |
6116f729 | 3247 | }, |
75466c4f | 3248 | target => get_standard_option('pve-node', { |
55173c6b DM |
3249 | description => "Target node. Only allowed if the original VM is on shared storage.", |
3250 | optional => 1, | |
3251 | }), | |
0aab5a16 SI |
3252 | bwlimit => { |
3253 | description => "Override I/O bandwidth limit (in KiB/s).", | |
3254 | optional => 1, | |
3255 | type => 'integer', | |
3256 | minimum => '0', | |
41756a3b | 3257 | default => 'clone limit from datacenter or storage config', |
0aab5a16 | 3258 | }, |
55173c6b | 3259 | }, |
6116f729 DM |
3260 | }, |
3261 | returns => { | |
3262 | type => 'string', | |
3263 | }, | |
3264 | code => sub { | |
3265 | my ($param) = @_; | |
3266 | ||
3267 | my $rpcenv = PVE::RPCEnvironment::get(); | |
a85ff91b | 3268 | my $authuser = $rpcenv->get_user(); |
6116f729 DM |
3269 | |
3270 | my $node = extract_param($param, 'node'); | |
6116f729 | 3271 | my $vmid = extract_param($param, 'vmid'); |
6116f729 | 3272 | my $newid = extract_param($param, 'newid'); |
6116f729 | 3273 | my $pool = extract_param($param, 'pool'); |
6116f729 | 3274 | |
55173c6b | 3275 | my $snapname = extract_param($param, 'snapname'); |
81f043eb | 3276 | my $storage = extract_param($param, 'storage'); |
42a19c87 | 3277 | my $format = extract_param($param, 'format'); |
55173c6b DM |
3278 | my $target = extract_param($param, 'target'); |
3279 | ||
3280 | my $localnode = PVE::INotify::nodename(); | |
3281 | ||
e099bad4 | 3282 | if ($target && ($target eq $localnode || $target eq 'localhost')) { |
a85ff91b | 3283 | undef $target; |
a85ff91b | 3284 | } |
55173c6b | 3285 | |
4df8fe45 | 3286 | my $running = PVE::QemuServer::check_running($vmid) || 0; |
d069275f | 3287 | |
4df8fe45 FG |
3288 | my $load_and_check = sub { |
3289 | $rpcenv->check_pool_exist($pool) if defined($pool); | |
3290 | PVE::Cluster::check_node_exists($target) if $target; | |
6116f729 | 3291 | |
4df8fe45 | 3292 | my $storecfg = PVE::Storage::config(); |
4a5a2590 | 3293 | |
4df8fe45 FG |
3294 | if ($storage) { |
3295 | # check if storage is enabled on local node | |
3296 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
3297 | if ($target) { | |
3298 | # check if storage is available on target node | |
3299 | PVE::Storage::storage_check_enabled($storecfg, $storage, $target); | |
3300 | # clone only works if target storage is shared | |
3301 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
3302 | die "can't clone to non-shared storage '$storage'\n" | |
3303 | if !$scfg->{shared}; | |
3304 | } | |
3305 | } | |
6116f729 | 3306 | |
4df8fe45 | 3307 | PVE::Cluster::check_cfs_quorum(); |
4e4f83fe | 3308 | |
ffda963f | 3309 | my $conf = PVE::QemuConfig->load_config($vmid); |
ffda963f | 3310 | PVE::QemuConfig->check_lock($conf); |
6116f729 | 3311 | |
4e4f83fe | 3312 | my $verify_running = PVE::QemuServer::check_running($vmid) || 0; |
4e4f83fe | 3313 | die "unexpected state change\n" if $verify_running != $running; |
6116f729 | 3314 | |
75466c4f DM |
3315 | die "snapshot '$snapname' does not exist\n" |
3316 | if $snapname && !defined( $conf->{snapshots}->{$snapname}); | |
6116f729 | 3317 | |
dbecb46f | 3318 | my $full = $param->{full} // !PVE::QemuConfig->is_template($conf); |
fd13b1d0 DM |
3319 | |
3320 | die "parameter 'storage' not allowed for linked clones\n" | |
3321 | if defined($storage) && !$full; | |
3322 | ||
3323 | die "parameter 'format' not allowed for linked clones\n" | |
3324 | if defined($format) && !$full; | |
3325 | ||
75466c4f | 3326 | my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf; |
9076d880 | 3327 | |
9418baad | 3328 | my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage); |
6116f729 | 3329 | |
a85ff91b TL |
3330 | die "can't clone VM to node '$target' (VM uses local storage)\n" |
3331 | if $target && !$sharedvm; | |
75466c4f | 3332 | |
ffda963f | 3333 | my $conffile = PVE::QemuConfig->config_file($newid); |
6116f729 DM |
3334 | die "unable to create VM $newid: config file already exists\n" |
3335 | if -f $conffile; | |
3336 | ||
9418baad | 3337 | my $newconf = { lock => 'clone' }; |
829967a9 | 3338 | my $drives = {}; |
34456bf0 | 3339 | my $fullclone = {}; |
829967a9 DM |
3340 | my $vollist = []; |
3341 | ||
3342 | foreach my $opt (keys %$oldconf) { | |
3343 | my $value = $oldconf->{$opt}; | |
3344 | ||
3345 | # do not copy snapshot related info | |
3346 | next if $opt eq 'snapshots' || $opt eq 'parent' || $opt eq 'snaptime' || | |
3347 | $opt eq 'vmstate' || $opt eq 'snapstate'; | |
3348 | ||
a78ea5df WL |
3349 | # no need to copy unused images, because VMID(owner) changes anyways |
3350 | next if $opt =~ m/^unused\d+$/; | |
3351 | ||
e4a70a41 FE |
3352 | die "cannot clone TPM state while VM is running\n" |
3353 | if $full && $running && !$snapname && $opt eq 'tpmstate0'; | |
3354 | ||
829967a9 DM |
3355 | # always change MAC! address |
3356 | if ($opt =~ m/^net(\d+)$/) { | |
3357 | my $net = PVE::QemuServer::parse_net($value); | |
b5b99790 WB |
3358 | my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); |
3359 | $net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); | |
829967a9 | 3360 | $newconf->{$opt} = PVE::QemuServer::print_net($net); |
74479ee9 | 3361 | } elsif (PVE::QemuServer::is_valid_drivename($opt)) { |
1f1412d1 DM |
3362 | my $drive = PVE::QemuServer::parse_drive($opt, $value); |
3363 | die "unable to parse drive options for '$opt'\n" if !$drive; | |
7fe8b44c | 3364 | if (PVE::QemuServer::drive_is_cdrom($drive, 1)) { |
829967a9 DM |
3365 | $newconf->{$opt} = $value; # simply copy configuration |
3366 | } else { | |
7fe8b44c | 3367 | if ($full || PVE::QemuServer::drive_is_cloudinit($drive)) { |
6318daca | 3368 | die "Full clone feature is not supported for drive '$opt'\n" |
dba198b0 | 3369 | if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running); |
34456bf0 | 3370 | $fullclone->{$opt} = 1; |
64ff6fe4 SP |
3371 | } else { |
3372 | # not full means clone instead of copy | |
6318daca | 3373 | die "Linked clone feature is not supported for drive '$opt'\n" |
64ff6fe4 | 3374 | if !PVE::Storage::volume_has_feature($storecfg, 'clone', $drive->{file}, $snapname, $running); |
dba198b0 | 3375 | } |
829967a9 | 3376 | $drives->{$opt} = $drive; |
f8c4b2c5 | 3377 | next if PVE::QemuServer::drive_is_cloudinit($drive); |
829967a9 DM |
3378 | push @$vollist, $drive->{file}; |
3379 | } | |
3380 | } else { | |
3381 | # copy everything else | |
3382 | $newconf->{$opt} = $value; | |
3383 | } | |
3384 | } | |
3385 | ||
dbecb46f FE |
3386 | return ($conffile, $newconf, $oldconf, $vollist, $drives, $fullclone); |
3387 | }; | |
3388 | ||
3389 | my $clonefn = sub { | |
3390 | my ($conffile, $newconf, $oldconf, $vollist, $drives, $fullclone) = $load_and_check->(); | |
4df8fe45 | 3391 | my $storecfg = PVE::Storage::config(); |
dbecb46f FE |
3392 | |
3393 | # auto generate a new uuid | |
cd11416f | 3394 | my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || ''); |
6ee499ff | 3395 | $smbios1->{uuid} = PVE::QemuServer::generate_uuid(); |
cd11416f | 3396 | $newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1); |
a85ff91b | 3397 | # auto generate a new vmgenid only if the option was set for template |
6ee499ff DC |
3398 | if ($newconf->{vmgenid}) { |
3399 | $newconf->{vmgenid} = PVE::QemuServer::generate_uuid(); | |
3400 | } | |
3401 | ||
829967a9 DM |
3402 | delete $newconf->{template}; |
3403 | ||
3404 | if ($param->{name}) { | |
3405 | $newconf->{name} = $param->{name}; | |
3406 | } else { | |
a85ff91b | 3407 | $newconf->{name} = "Copy-of-VM-" . ($oldconf->{name} // $vmid); |
829967a9 | 3408 | } |
2dd53043 | 3409 | |
829967a9 DM |
3410 | if ($param->{description}) { |
3411 | $newconf->{description} = $param->{description}; | |
3412 | } | |
3413 | ||
6116f729 | 3414 | # create empty/temp config - this fails if VM already exists on other node |
a85ff91b | 3415 | # FIXME use PVE::QemuConfig->create_and_lock_config and adapt code |
9418baad | 3416 | PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n"); |
6116f729 | 3417 | |
dbecb46f | 3418 | PVE::Firewall::clone_vmfw_conf($vmid, $newid); |
68e46b84 | 3419 | |
dbecb46f FE |
3420 | my $newvollist = []; |
3421 | my $jobs = {}; | |
3422 | ||
3423 | eval { | |
3424 | local $SIG{INT} = | |
3425 | local $SIG{TERM} = | |
3426 | local $SIG{QUIT} = | |
3427 | local $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
3428 | ||
3429 | PVE::Storage::activate_volumes($storecfg, $vollist, $snapname); | |
3430 | ||
3431 | my $bwlimit = extract_param($param, 'bwlimit'); | |
3432 | ||
3433 | my $total_jobs = scalar(keys %{$drives}); | |
3434 | my $i = 1; | |
3435 | ||
3436 | foreach my $opt (sort keys %$drives) { | |
3437 | my $drive = $drives->{$opt}; | |
3438 | my $skipcomplete = ($total_jobs != $i); # finish after last drive | |
3439 | my $completion = $skipcomplete ? 'skip' : 'complete'; | |
3440 | ||
3441 | my $src_sid = PVE::Storage::parse_volume_id($drive->{file}); | |
3442 | my $storage_list = [ $src_sid ]; | |
3443 | push @$storage_list, $storage if defined($storage); | |
3444 | my $clonelimit = PVE::Storage::get_bandwidth_limit('clone', $storage_list, $bwlimit); | |
3445 | ||
1196086f FE |
3446 | my $source_info = { |
3447 | vmid => $vmid, | |
3448 | running => $running, | |
3449 | drivename => $opt, | |
3450 | drive => $drive, | |
3451 | snapname => $snapname, | |
3452 | }; | |
3453 | ||
3454 | my $dest_info = { | |
3455 | vmid => $newid, | |
25166060 | 3456 | drivename => $opt, |
1196086f FE |
3457 | storage => $storage, |
3458 | format => $format, | |
3459 | }; | |
3460 | ||
7344af7b FE |
3461 | $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($oldconf) |
3462 | if $opt eq 'efidisk0'; | |
3463 | ||
dbecb46f FE |
3464 | my $newdrive = PVE::QemuServer::clone_disk( |
3465 | $storecfg, | |
1196086f FE |
3466 | $source_info, |
3467 | $dest_info, | |
dbecb46f FE |
3468 | $fullclone->{$opt}, |
3469 | $newvollist, | |
3470 | $jobs, | |
3471 | $completion, | |
3472 | $oldconf->{agent}, | |
3473 | $clonelimit, | |
dbecb46f FE |
3474 | ); |
3475 | ||
3476 | $newconf->{$opt} = PVE::QemuServer::print_drive($newdrive); | |
68e46b84 | 3477 | |
ffda963f | 3478 | PVE::QemuConfig->write_config($newid, $newconf); |
dbecb46f FE |
3479 | $i++; |
3480 | } | |
55173c6b | 3481 | |
dbecb46f | 3482 | delete $newconf->{lock}; |
baca276d | 3483 | |
dbecb46f FE |
3484 | # do not write pending changes |
3485 | if (my @changes = keys %{$newconf->{pending}}) { | |
3486 | my $pending = join(',', @changes); | |
3487 | warn "found pending changes for '$pending', discarding for clone\n"; | |
3488 | delete $newconf->{pending}; | |
3489 | } | |
d703d4c0 | 3490 | |
dbecb46f | 3491 | PVE::QemuConfig->write_config($newid, $newconf); |
b83e0181 | 3492 | |
dbecb46f FE |
3493 | if ($target) { |
3494 | # always deactivate volumes - avoid lvm LVs to be active on several nodes | |
3495 | PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running; | |
3496 | PVE::Storage::deactivate_volumes($storecfg, $newvollist); | |
c05c90a1 | 3497 | |
dbecb46f FE |
3498 | my $newconffile = PVE::QemuConfig->config_file($newid, $target); |
3499 | die "Failed to move config to node '$target' - rename failed: $!\n" | |
3500 | if !rename($conffile, $newconffile); | |
3501 | } | |
c05c90a1 | 3502 | |
dbecb46f FE |
3503 | PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool; |
3504 | }; | |
3505 | if (my $err = $@) { | |
3506 | eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs) }; | |
3507 | sleep 1; # some storage like rbd need to wait before release volume - really? | |
990b65ab | 3508 | |
dbecb46f FE |
3509 | foreach my $volid (@$newvollist) { |
3510 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
3511 | warn $@ if $@; | |
6116f729 DM |
3512 | } |
3513 | ||
dbecb46f | 3514 | PVE::Firewall::remove_vmfw_conf($newid); |
6116f729 | 3515 | |
dbecb46f FE |
3516 | unlink $conffile; # avoid races -> last thing before die |
3517 | ||
3518 | die "clone failed: $err"; | |
3519 | } | |
457010cc | 3520 | |
dbecb46f | 3521 | return; |
6116f729 DM |
3522 | }; |
3523 | ||
45fd77bb FG |
3524 | # Aquire exclusive lock lock for $newid |
3525 | my $lock_target_vm = sub { | |
ffda963f | 3526 | return PVE::QemuConfig->lock_config_full($newid, 1, $clonefn); |
45fd77bb | 3527 | }; |
6116f729 | 3528 | |
dbecb46f FE |
3529 | my $lock_source_vm = sub { |
3530 | # exclusive lock if VM is running - else shared lock is enough; | |
3531 | if ($running) { | |
3532 | return PVE::QemuConfig->lock_config_full($vmid, 1, $lock_target_vm); | |
3533 | } else { | |
3534 | return PVE::QemuConfig->lock_config_shared($vmid, 1, $lock_target_vm); | |
3535 | } | |
3536 | }; | |
3537 | ||
3538 | $load_and_check->(); # early checks before forking/locking | |
3539 | ||
3540 | return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $lock_source_vm); | |
6116f729 DM |
3541 | }}); |
3542 | ||
586bfa78 | 3543 | __PACKAGE__->register_method({ |
43bc02a9 DM |
3544 | name => 'move_vm_disk', |
3545 | path => '{vmid}/move_disk', | |
e2cd75fa | 3546 | method => 'POST', |
586bfa78 AD |
3547 | protected => 1, |
3548 | proxyto => 'node', | |
a9453218 | 3549 | description => "Move volume to different storage or to a different VM.", |
586bfa78 | 3550 | permissions => { |
a9453218 AL |
3551 | description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " . |
3552 | "and 'Datastore.AllocateSpace' permissions on the storage. To move ". | |
3553 | "a disk to another VM, you need the permissions on the target VM as well.", | |
44102492 | 3554 | check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], |
586bfa78 AD |
3555 | }, |
3556 | parameters => { | |
3326ae19 | 3557 | additionalProperties => 0, |
c07a9e3d | 3558 | properties => { |
586bfa78 | 3559 | node => get_standard_option('pve-node'), |
335af808 | 3560 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
a9453218 AL |
3561 | 'target-vmid' => get_standard_option('pve-vmid', { |
3562 | completion => \&PVE::QemuServer::complete_vmid, | |
3563 | optional => 1, | |
3564 | }), | |
586bfa78 AD |
3565 | disk => { |
3566 | type => 'string', | |
3567 | description => "The disk you want to move.", | |
a9453218 | 3568 | enum => [PVE::QemuServer::Drive::valid_drive_names_with_unused()], |
586bfa78 | 3569 | }, |
335af808 DM |
3570 | storage => get_standard_option('pve-storage-id', { |
3571 | description => "Target storage.", | |
3572 | completion => \&PVE::QemuServer::complete_storage, | |
a9453218 | 3573 | optional => 1, |
335af808 | 3574 | }), |
f519ab0b TL |
3575 | 'format' => { |
3576 | type => 'string', | |
3577 | description => "Target Format.", | |
3578 | enum => [ 'raw', 'qcow2', 'vmdk' ], | |
3579 | optional => 1, | |
3580 | }, | |
70d45e33 DM |
3581 | delete => { |
3582 | type => 'boolean', | |
f519ab0b TL |
3583 | description => "Delete the original disk after successful copy. By default the" |
3584 | ." original disk is kept as unused disk.", | |
70d45e33 DM |
3585 | optional => 1, |
3586 | default => 0, | |
3587 | }, | |
586bfa78 AD |
3588 | digest => { |
3589 | type => 'string', | |
f519ab0b TL |
3590 | description => 'Prevent changes if current configuration file has different SHA1" |
3591 | ." digest. This can be used to prevent concurrent modifications.', | |
586bfa78 AD |
3592 | maxLength => 40, |
3593 | optional => 1, | |
3594 | }, | |
0aab5a16 SI |
3595 | bwlimit => { |
3596 | description => "Override I/O bandwidth limit (in KiB/s).", | |
3597 | optional => 1, | |
3598 | type => 'integer', | |
3599 | minimum => '0', | |
41756a3b | 3600 | default => 'move limit from datacenter or storage config', |
0aab5a16 | 3601 | }, |
a9453218 AL |
3602 | 'target-disk' => { |
3603 | type => 'string', | |
f519ab0b TL |
3604 | description => "The config key the disk will be moved to on the target VM" |
3605 | ." (for example, ide0 or scsi1). Default is the source disk key.", | |
a9453218 AL |
3606 | enum => [PVE::QemuServer::Drive::valid_drive_names_with_unused()], |
3607 | optional => 1, | |
3608 | }, | |
3609 | 'target-digest' => { | |
3610 | type => 'string', | |
f519ab0b TL |
3611 | description => 'Prevent changes if the current config file of the target VM has a" |
3612 | ." different SHA1 digest. This can be used to detect concurrent modifications.', | |
a9453218 AL |
3613 | maxLength => 40, |
3614 | optional => 1, | |
3615 | }, | |
586bfa78 AD |
3616 | }, |
3617 | }, | |
e2cd75fa DM |
3618 | returns => { |
3619 | type => 'string', | |
3620 | description => "the task ID.", | |
3621 | }, | |
586bfa78 AD |
3622 | code => sub { |
3623 | my ($param) = @_; | |
3624 | ||
3625 | my $rpcenv = PVE::RPCEnvironment::get(); | |
586bfa78 AD |
3626 | my $authuser = $rpcenv->get_user(); |
3627 | ||
3628 | my $node = extract_param($param, 'node'); | |
586bfa78 | 3629 | my $vmid = extract_param($param, 'vmid'); |
a9453218 | 3630 | my $target_vmid = extract_param($param, 'target-vmid'); |
586bfa78 | 3631 | my $digest = extract_param($param, 'digest'); |
a9453218 | 3632 | my $target_digest = extract_param($param, 'target-digest'); |
586bfa78 | 3633 | my $disk = extract_param($param, 'disk'); |
a9453218 | 3634 | my $target_disk = extract_param($param, 'target-disk') // $disk; |
586bfa78 | 3635 | my $storeid = extract_param($param, 'storage'); |
586bfa78 AD |
3636 | my $format = extract_param($param, 'format'); |
3637 | ||
586bfa78 AD |
3638 | my $storecfg = PVE::Storage::config(); |
3639 | ||
bdf6ba1e | 3640 | my $load_and_check_move = sub { |
ffda963f | 3641 | my $conf = PVE::QemuConfig->load_config($vmid); |
dcce9b46 FG |
3642 | PVE::QemuConfig->check_lock($conf); |
3643 | ||
44102492 | 3644 | PVE::Tools::assert_if_modified($digest, $conf->{digest}); |
586bfa78 AD |
3645 | |
3646 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; | |
3647 | ||
3648 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
3649 | ||
a85ff91b | 3650 | die "disk '$disk' has no associated volume\n" if !$drive->{file}; |
931432bd | 3651 | die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive, 1); |
586bfa78 | 3652 | |
a85ff91b | 3653 | my $old_volid = $drive->{file}; |
e2cd75fa | 3654 | my $oldfmt; |
70d45e33 | 3655 | my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($old_volid); |
586bfa78 AD |
3656 | if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){ |
3657 | $oldfmt = $1; | |
3658 | } | |
3659 | ||
bdf6ba1e FE |
3660 | die "you can't move to the same storage with same format\n" |
3661 | if $oldstoreid eq $storeid && (!$format || !$oldfmt || $oldfmt eq $format); | |
586bfa78 | 3662 | |
9dbf9b54 | 3663 | # this only checks snapshots because $disk is passed! |
70c0ad66 AL |
3664 | my $snapshotted = PVE::QemuServer::Drive::is_volume_in_use( |
3665 | $storecfg, | |
3666 | $conf, | |
3667 | $disk, | |
3668 | $old_volid | |
3669 | ); | |
9dbf9b54 FG |
3670 | die "you can't move a disk with snapshots and delete the source\n" |
3671 | if $snapshotted && $param->{delete}; | |
3672 | ||
bdf6ba1e FE |
3673 | return ($conf, $drive, $oldstoreid, $snapshotted); |
3674 | }; | |
3675 | ||
3676 | my $move_updatefn = sub { | |
3677 | my ($conf, $drive, $oldstoreid, $snapshotted) = $load_and_check_move->(); | |
3678 | my $old_volid = $drive->{file}; | |
3679 | ||
70c0ad66 AL |
3680 | PVE::Cluster::log_msg( |
3681 | 'info', | |
3682 | $authuser, | |
3683 | "move disk VM $vmid: move --disk $disk --storage $storeid" | |
3684 | ); | |
586bfa78 AD |
3685 | |
3686 | my $running = PVE::QemuServer::check_running($vmid); | |
e2cd75fa DM |
3687 | |
3688 | PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]); | |
3689 | ||
bdf6ba1e | 3690 | my $newvollist = []; |
586bfa78 | 3691 | |
bdf6ba1e FE |
3692 | eval { |
3693 | local $SIG{INT} = | |
3694 | local $SIG{TERM} = | |
3695 | local $SIG{QUIT} = | |
3696 | local $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
0aab5a16 | 3697 | |
bdf6ba1e FE |
3698 | warn "moving disk with snapshots, snapshots will not be moved!\n" |
3699 | if $snapshotted; | |
e2cd75fa | 3700 | |
bdf6ba1e FE |
3701 | my $bwlimit = extract_param($param, 'bwlimit'); |
3702 | my $movelimit = PVE::Storage::get_bandwidth_limit( | |
3703 | 'move', | |
3704 | [$oldstoreid, $storeid], | |
3705 | $bwlimit | |
3706 | ); | |
7043d946 | 3707 | |
1196086f FE |
3708 | my $source_info = { |
3709 | vmid => $vmid, | |
3710 | running => $running, | |
3711 | drivename => $disk, | |
3712 | drive => $drive, | |
3713 | snapname => undef, | |
3714 | }; | |
3715 | ||
3716 | my $dest_info = { | |
3717 | vmid => $vmid, | |
25166060 | 3718 | drivename => $disk, |
1196086f FE |
3719 | storage => $storeid, |
3720 | format => $format, | |
3721 | }; | |
3722 | ||
7344af7b FE |
3723 | $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($conf) |
3724 | if $disk eq 'efidisk0'; | |
3725 | ||
bdf6ba1e FE |
3726 | my $newdrive = PVE::QemuServer::clone_disk( |
3727 | $storecfg, | |
1196086f FE |
3728 | $source_info, |
3729 | $dest_info, | |
bdf6ba1e FE |
3730 | 1, |
3731 | $newvollist, | |
3732 | undef, | |
3733 | undef, | |
3734 | undef, | |
3735 | $movelimit, | |
bdf6ba1e FE |
3736 | ); |
3737 | $conf->{$disk} = PVE::QemuServer::print_drive($newdrive); | |
fbd7dcce | 3738 | |
bdf6ba1e | 3739 | PVE::QemuConfig->add_unused_volume($conf, $old_volid) if !$param->{delete}; |
73272365 | 3740 | |
bdf6ba1e FE |
3741 | # convert moved disk to base if part of template |
3742 | PVE::QemuServer::template_create($vmid, $conf, $disk) | |
3743 | if PVE::QemuConfig->is_template($conf); | |
ca662131 | 3744 | |
bdf6ba1e | 3745 | PVE::QemuConfig->write_config($vmid, $conf); |
70d45e33 | 3746 | |
bdf6ba1e FE |
3747 | my $do_trim = PVE::QemuServer::get_qga_key($conf, 'fstrim_cloned_disks'); |
3748 | if ($running && $do_trim && PVE::QemuServer::qga_check_running($vmid)) { | |
3749 | eval { mon_cmd($vmid, "guest-fstrim") }; | |
70d45e33 | 3750 | } |
bdf6ba1e FE |
3751 | |
3752 | eval { | |
3753 | # try to deactivate volumes - avoid lvm LVs to be active on several nodes | |
3754 | PVE::Storage::deactivate_volumes($storecfg, [ $newdrive->{file} ]) | |
3755 | if !$running; | |
3756 | }; | |
3757 | warn $@ if $@; | |
586bfa78 | 3758 | }; |
bdf6ba1e FE |
3759 | if (my $err = $@) { |
3760 | foreach my $volid (@$newvollist) { | |
3761 | eval { PVE::Storage::vdisk_free($storecfg, $volid) }; | |
3762 | warn $@ if $@; | |
3763 | } | |
3764 | die "storage migration failed: $err"; | |
3765 | } | |
586bfa78 | 3766 | |
bdf6ba1e FE |
3767 | if ($param->{delete}) { |
3768 | eval { | |
3769 | PVE::Storage::deactivate_volumes($storecfg, [$old_volid]); | |
3770 | PVE::Storage::vdisk_free($storecfg, $old_volid); | |
3771 | }; | |
3772 | warn $@ if $@; | |
3773 | } | |
586bfa78 | 3774 | }; |
e2cd75fa | 3775 | |
a9453218 AL |
3776 | my $load_and_check_reassign_configs = sub { |
3777 | my $vmlist = PVE::Cluster::get_vmlist()->{ids}; | |
3778 | ||
3779 | die "could not find VM ${vmid}\n" if !exists($vmlist->{$vmid}); | |
dbc817ba | 3780 | die "could not find target VM ${target_vmid}\n" if !exists($vmlist->{$target_vmid}); |
a9453218 | 3781 | |
dbc817ba FG |
3782 | my $source_node = $vmlist->{$vmid}->{node}; |
3783 | my $target_node = $vmlist->{$target_vmid}->{node}; | |
3784 | ||
3785 | die "Both VMs need to be on the same node ($source_node != $target_node)\n" | |
3786 | if $source_node ne $target_node; | |
a9453218 AL |
3787 | |
3788 | my $source_conf = PVE::QemuConfig->load_config($vmid); | |
3789 | PVE::QemuConfig->check_lock($source_conf); | |
3790 | my $target_conf = PVE::QemuConfig->load_config($target_vmid); | |
3791 | PVE::QemuConfig->check_lock($target_conf); | |
3792 | ||
3793 | die "Can't move disks from or to template VMs\n" | |
3794 | if ($source_conf->{template} || $target_conf->{template}); | |
3795 | ||
3796 | if ($digest) { | |
3797 | eval { PVE::Tools::assert_if_modified($digest, $source_conf->{digest}) }; | |
3798 | die "VM ${vmid}: $@" if $@; | |
3799 | } | |
3800 | ||
3801 | if ($target_digest) { | |
3802 | eval { PVE::Tools::assert_if_modified($target_digest, $target_conf->{digest}) }; | |
3803 | die "VM ${target_vmid}: $@" if $@; | |
3804 | } | |
3805 | ||
3806 | die "Disk '${disk}' for VM '$vmid' does not exist\n" if !defined($source_conf->{$disk}); | |
3807 | ||
3808 | die "Target disk key '${target_disk}' is already in use for VM '$target_vmid'\n" | |
dbc817ba | 3809 | if $target_conf->{$target_disk}; |
a9453218 AL |
3810 | |
3811 | my $drive = PVE::QemuServer::parse_drive( | |
3812 | $disk, | |
3813 | $source_conf->{$disk}, | |
3814 | ); | |
dbc817ba FG |
3815 | die "failed to parse source disk - $@\n" if !$drive; |
3816 | ||
3817 | my $source_volid = $drive->{file}; | |
a9453218 AL |
3818 | |
3819 | die "disk '${disk}' has no associated volume\n" if !$source_volid; | |
3820 | die "CD drive contents can't be moved to another VM\n" | |
3821 | if PVE::QemuServer::drive_is_cdrom($drive, 1); | |
dbc817ba FG |
3822 | |
3823 | my $storeid = PVE::Storage::parse_volume_id($source_volid, 1); | |
3824 | die "Volume '$source_volid' not managed by PVE\n" if !defined($storeid); | |
3825 | ||
a9453218 AL |
3826 | die "Can't move disk used by a snapshot to another VM\n" |
3827 | if PVE::QemuServer::Drive::is_volume_in_use($storecfg, $source_conf, $disk, $source_volid); | |
3828 | die "Storage does not support moving of this disk to another VM\n" | |
3829 | if (!PVE::Storage::volume_has_feature($storecfg, 'rename', $source_volid)); | |
dbc817ba | 3830 | die "Cannot move disk to another VM while the source VM is running - detach first\n" |
a9453218 AL |
3831 | if PVE::QemuServer::check_running($vmid) && $disk !~ m/^unused\d+$/; |
3832 | ||
dbc817ba | 3833 | # now re-parse using target disk slot format |
f4e4c779 FG |
3834 | if ($target_disk =~ /^unused\d+$/) { |
3835 | $drive = PVE::QemuServer::parse_drive( | |
3836 | $target_disk, | |
3837 | $source_volid, | |
3838 | ); | |
3839 | } else { | |
3840 | $drive = PVE::QemuServer::parse_drive( | |
3841 | $target_disk, | |
3842 | $source_conf->{$disk}, | |
3843 | ); | |
3844 | } | |
dbc817ba | 3845 | die "failed to parse source disk for target disk format - $@\n" if !$drive; |
a9453218 AL |
3846 | |
3847 | my $repl_conf = PVE::ReplicationConfig->new(); | |
dbc817ba FG |
3848 | if ($repl_conf->check_for_existing_jobs($target_vmid, 1)) { |
3849 | my $format = (PVE::Storage::parse_volname($storecfg, $source_volid))[6]; | |
3850 | die "Cannot move disk to a replicated VM. Storage does not support replication!\n" | |
3851 | if !PVE::Storage::storage_can_replicate($storecfg, $storeid, $format); | |
a9453218 AL |
3852 | } |
3853 | ||
dbc817ba | 3854 | return ($source_conf, $target_conf, $drive); |
a9453218 AL |
3855 | }; |
3856 | ||
3857 | my $logfunc = sub { | |
3858 | my ($msg) = @_; | |
3859 | print STDERR "$msg\n"; | |
3860 | }; | |
3861 | ||
3862 | my $disk_reassignfn = sub { | |
3863 | return PVE::QemuConfig->lock_config($vmid, sub { | |
3864 | return PVE::QemuConfig->lock_config($target_vmid, sub { | |
dbc817ba | 3865 | my ($source_conf, $target_conf, $drive) = &$load_and_check_reassign_configs(); |
a9453218 | 3866 | |
dbc817ba | 3867 | my $source_volid = $drive->{file}; |
a9453218 AL |
3868 | |
3869 | print "moving disk '$disk' from VM '$vmid' to '$target_vmid'\n"; | |
3870 | my ($storeid, $source_volname) = PVE::Storage::parse_volume_id($source_volid); | |
3871 | ||
3872 | my $fmt = (PVE::Storage::parse_volname($storecfg, $source_volid))[6]; | |
3873 | ||
3874 | my $new_volid = PVE::Storage::rename_volume( | |
3875 | $storecfg, | |
3876 | $source_volid, | |
3877 | $target_vmid, | |
3878 | ); | |
3879 | ||
dbc817ba | 3880 | $drive->{file} = $new_volid; |
a9453218 | 3881 | |
96670745 TL |
3882 | my $boot_order = PVE::QemuServer::device_bootorder($source_conf); |
3883 | if (defined(delete $boot_order->{$disk})) { | |
3884 | print "removing disk '$disk' from boot order config\n"; | |
3885 | my $boot_devs = [ sort { $boot_order->{$a} <=> $boot_order->{$b} } keys %$boot_order ]; | |
3886 | $source_conf->{boot} = PVE::QemuServer::print_bootorder($boot_devs); | |
3887 | } | |
3888 | ||
a9453218 AL |
3889 | delete $source_conf->{$disk}; |
3890 | print "removing disk '${disk}' from VM '${vmid}' config\n"; | |
3891 | PVE::QemuConfig->write_config($vmid, $source_conf); | |
3892 | ||
dbc817ba | 3893 | my $drive_string = PVE::QemuServer::print_drive($drive); |
bf67da2b AL |
3894 | |
3895 | if ($target_disk =~ /^unused\d+$/) { | |
3896 | $target_conf->{$target_disk} = $drive_string; | |
3897 | PVE::QemuConfig->write_config($target_vmid, $target_conf); | |
3898 | } else { | |
3899 | &$update_vm_api( | |
3900 | { | |
3901 | node => $node, | |
3902 | vmid => $target_vmid, | |
3903 | digest => $target_digest, | |
3904 | $target_disk => $drive_string, | |
3905 | }, | |
3906 | 1, | |
3907 | ); | |
3908 | } | |
a9453218 AL |
3909 | |
3910 | # remove possible replication snapshots | |
3911 | if (PVE::Storage::volume_has_feature( | |
3912 | $storecfg, | |
3913 | 'replicate', | |
3914 | $source_volid), | |
3915 | ) { | |
3916 | eval { | |
3917 | PVE::Replication::prepare( | |
3918 | $storecfg, | |
3919 | [$new_volid], | |
3920 | undef, | |
3921 | 1, | |
3922 | undef, | |
3923 | $logfunc, | |
3924 | ) | |
3925 | }; | |
3926 | if (my $err = $@) { | |
3927 | print "Failed to remove replication snapshots on moved disk " . | |
3928 | "'$target_disk'. Manual cleanup could be necessary.\n"; | |
3929 | } | |
3930 | } | |
3931 | }); | |
3932 | }); | |
3933 | }; | |
3934 | ||
dbc817ba FG |
3935 | if ($target_vmid && $storeid) { |
3936 | my $msg = "either set 'storage' or 'target-vmid', but not both"; | |
3937 | raise_param_exc({ 'target-vmid' => $msg, 'storage' => $msg }); | |
3938 | } elsif ($target_vmid) { | |
a9453218 AL |
3939 | $rpcenv->check_vm_perm($authuser, $target_vmid, undef, ['VM.Config.Disk']) |
3940 | if $authuser ne 'root@pam'; | |
3941 | ||
dbc817ba | 3942 | raise_param_exc({ 'target-vmid' => "must be different than source VMID to reassign disk" }) |
a9453218 AL |
3943 | if $vmid eq $target_vmid; |
3944 | ||
44102492 | 3945 | my (undef, undef, $drive) = &$load_and_check_reassign_configs(); |
a6273aa8 FG |
3946 | my $storage = PVE::Storage::parse_volume_id($drive->{file}); |
3947 | $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']); | |
44102492 | 3948 | |
a9453218 AL |
3949 | return $rpcenv->fork_worker( |
3950 | 'qmmove', | |
3951 | "${vmid}-${disk}>${target_vmid}-${target_disk}", | |
3952 | $authuser, | |
3953 | $disk_reassignfn | |
3954 | ); | |
3955 | } elsif ($storeid) { | |
44102492 FG |
3956 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); |
3957 | ||
a9453218 AL |
3958 | die "cannot move disk '$disk', only configured disks can be moved to another storage\n" |
3959 | if $disk =~ m/^unused\d+$/; | |
bdf6ba1e FE |
3960 | |
3961 | $load_and_check_move->(); # early checks before forking/locking | |
3962 | ||
3963 | my $realcmd = sub { | |
3964 | PVE::QemuConfig->lock_config($vmid, $move_updatefn); | |
3965 | }; | |
3966 | ||
3967 | return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd); | |
a9453218 | 3968 | } else { |
dbc817ba FG |
3969 | my $msg = "both 'storage' and 'target-vmid' missing, either needs to be set"; |
3970 | raise_param_exc({ 'target-vmid' => $msg, 'storage' => $msg }); | |
a9453218 | 3971 | } |
586bfa78 AD |
3972 | }}); |
3973 | ||
71fc647f TM |
3974 | my $check_vm_disks_local = sub { |
3975 | my ($storecfg, $vmconf, $vmid) = @_; | |
3976 | ||
3977 | my $local_disks = {}; | |
3978 | ||
3979 | # add some more information to the disks e.g. cdrom | |
3980 | PVE::QemuServer::foreach_volid($vmconf, sub { | |
3981 | my ($volid, $attr) = @_; | |
3982 | ||
3983 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
3984 | if ($storeid) { | |
3985 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); | |
3986 | return if $scfg->{shared}; | |
3987 | } | |
3988 | # The shared attr here is just a special case where the vdisk | |
3989 | # is marked as shared manually | |
3990 | return if $attr->{shared}; | |
3991 | return if $attr->{cdrom} and $volid eq "none"; | |
3992 | ||
3993 | if (exists $local_disks->{$volid}) { | |
3994 | @{$local_disks->{$volid}}{keys %$attr} = values %$attr | |
3995 | } else { | |
3996 | $local_disks->{$volid} = $attr; | |
3997 | # ensure volid is present in case it's needed | |
3998 | $local_disks->{$volid}->{volid} = $volid; | |
3999 | } | |
4000 | }); | |
4001 | ||
4002 | return $local_disks; | |
4003 | }; | |
4004 | ||
4005 | __PACKAGE__->register_method({ | |
4006 | name => 'migrate_vm_precondition', | |
4007 | path => '{vmid}/migrate', | |
4008 | method => 'GET', | |
4009 | protected => 1, | |
4010 | proxyto => 'node', | |
4011 | description => "Get preconditions for migration.", | |
4012 | permissions => { | |
4013 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], | |
4014 | }, | |
4015 | parameters => { | |
4016 | additionalProperties => 0, | |
4017 | properties => { | |
4018 | node => get_standard_option('pve-node'), | |
4019 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
4020 | target => get_standard_option('pve-node', { | |
4021 | description => "Target node.", | |
4022 | completion => \&PVE::Cluster::complete_migration_target, | |
4023 | optional => 1, | |
4024 | }), | |
4025 | }, | |
4026 | }, | |
4027 | returns => { | |
4028 | type => "object", | |
4029 | properties => { | |
4030 | running => { type => 'boolean' }, | |
4031 | allowed_nodes => { | |
4032 | type => 'array', | |
4033 | optional => 1, | |
f25852c2 TM |
4034 | description => "List nodes allowed for offline migration, only passed if VM is offline" |
4035 | }, | |
4036 | not_allowed_nodes => { | |
4037 | type => 'object', | |
4038 | optional => 1, | |
4039 | description => "List not allowed nodes with additional informations, only passed if VM is offline" | |
71fc647f TM |
4040 | }, |
4041 | local_disks => { | |
4042 | type => 'array', | |
4043 | description => "List local disks including CD-Rom, unsused and not referenced disks" | |
4044 | }, | |
4045 | local_resources => { | |
4046 | type => 'array', | |
4047 | description => "List local resources e.g. pci, usb" | |
4048 | } | |
4049 | }, | |
4050 | }, | |
4051 | code => sub { | |
4052 | my ($param) = @_; | |
4053 | ||
4054 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4055 | ||
4056 | my $authuser = $rpcenv->get_user(); | |
4057 | ||
4058 | PVE::Cluster::check_cfs_quorum(); | |
4059 | ||
4060 | my $res = {}; | |
4061 | ||
4062 | my $vmid = extract_param($param, 'vmid'); | |
4063 | my $target = extract_param($param, 'target'); | |
4064 | my $localnode = PVE::INotify::nodename(); | |
4065 | ||
4066 | ||
4067 | # test if VM exists | |
4068 | my $vmconf = PVE::QemuConfig->load_config($vmid); | |
4069 | my $storecfg = PVE::Storage::config(); | |
4070 | ||
4071 | ||
4072 | # try to detect errors early | |
4073 | PVE::QemuConfig->check_lock($vmconf); | |
4074 | ||
4075 | $res->{running} = PVE::QemuServer::check_running($vmid) ? 1:0; | |
4076 | ||
4077 | # if vm is not running, return target nodes where local storage is available | |
4078 | # for offline migration | |
4079 | if (!$res->{running}) { | |
f25852c2 TM |
4080 | $res->{allowed_nodes} = []; |
4081 | my $checked_nodes = PVE::QemuServer::check_local_storage_availability($vmconf, $storecfg); | |
32075a2c | 4082 | delete $checked_nodes->{$localnode}; |
f25852c2 | 4083 | |
f25852c2 | 4084 | foreach my $node (keys %$checked_nodes) { |
32075a2c | 4085 | if (!defined $checked_nodes->{$node}->{unavailable_storages}) { |
f25852c2 | 4086 | push @{$res->{allowed_nodes}}, $node; |
f25852c2 | 4087 | } |
71fc647f | 4088 | |
f25852c2 TM |
4089 | } |
4090 | $res->{not_allowed_nodes} = $checked_nodes; | |
71fc647f TM |
4091 | } |
4092 | ||
4093 | ||
4094 | my $local_disks = &$check_vm_disks_local($storecfg, $vmconf, $vmid); | |
4095 | $res->{local_disks} = [ values %$local_disks ];; | |
4096 | ||
4097 | my $local_resources = PVE::QemuServer::check_local_resources($vmconf, 1); | |
4098 | ||
4099 | $res->{local_resources} = $local_resources; | |
4100 | ||
4101 | return $res; | |
4102 | ||
4103 | ||
4104 | }}); | |
4105 | ||
3ea94c60 | 4106 | __PACKAGE__->register_method({ |
afdb31d5 | 4107 | name => 'migrate_vm', |
3ea94c60 DM |
4108 | path => '{vmid}/migrate', |
4109 | method => 'POST', | |
4110 | protected => 1, | |
4111 | proxyto => 'node', | |
4112 | description => "Migrate virtual machine. Creates a new migration task.", | |
a0d1b1a2 DM |
4113 | permissions => { |
4114 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], | |
4115 | }, | |
3ea94c60 | 4116 | parameters => { |
3326ae19 | 4117 | additionalProperties => 0, |
3ea94c60 DM |
4118 | properties => { |
4119 | node => get_standard_option('pve-node'), | |
335af808 | 4120 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
c2ed338e | 4121 | target => get_standard_option('pve-node', { |
335af808 DM |
4122 | description => "Target node.", |
4123 | completion => \&PVE::Cluster::complete_migration_target, | |
4124 | }), | |
3ea94c60 DM |
4125 | online => { |
4126 | type => 'boolean', | |
13739386 | 4127 | description => "Use online/live migration if VM is running. Ignored if VM is stopped.", |
3ea94c60 DM |
4128 | optional => 1, |
4129 | }, | |
4130 | force => { | |
4131 | type => 'boolean', | |
4132 | description => "Allow to migrate VMs which use local devices. Only root may use this option.", | |
4133 | optional => 1, | |
4134 | }, | |
2de2d6f7 TL |
4135 | migration_type => { |
4136 | type => 'string', | |
4137 | enum => ['secure', 'insecure'], | |
c07a9e3d | 4138 | description => "Migration traffic is encrypted using an SSH tunnel by default. On secure, completely private networks this can be disabled to increase performance.", |
2de2d6f7 TL |
4139 | optional => 1, |
4140 | }, | |
4141 | migration_network => { | |
c07a9e3d | 4142 | type => 'string', format => 'CIDR', |
2de2d6f7 TL |
4143 | description => "CIDR of the (sub) network that is used for migration.", |
4144 | optional => 1, | |
4145 | }, | |
56af7146 AD |
4146 | "with-local-disks" => { |
4147 | type => 'boolean', | |
4148 | description => "Enable live storage migration for local disk", | |
b74cad8a | 4149 | optional => 1, |
56af7146 | 4150 | }, |
bf8fc5a3 | 4151 | targetstorage => get_standard_option('pve-targetstorage', { |
255e9c54 | 4152 | completion => \&PVE::QemuServer::complete_migration_storage, |
56af7146 | 4153 | }), |
0aab5a16 SI |
4154 | bwlimit => { |
4155 | description => "Override I/O bandwidth limit (in KiB/s).", | |
4156 | optional => 1, | |
4157 | type => 'integer', | |
4158 | minimum => '0', | |
41756a3b | 4159 | default => 'migrate limit from datacenter or storage config', |
0aab5a16 | 4160 | }, |
3ea94c60 DM |
4161 | }, |
4162 | }, | |
afdb31d5 | 4163 | returns => { |
3ea94c60 DM |
4164 | type => 'string', |
4165 | description => "the task ID.", | |
4166 | }, | |
4167 | code => sub { | |
4168 | my ($param) = @_; | |
4169 | ||
4170 | my $rpcenv = PVE::RPCEnvironment::get(); | |
a0d1b1a2 | 4171 | my $authuser = $rpcenv->get_user(); |
3ea94c60 DM |
4172 | |
4173 | my $target = extract_param($param, 'target'); | |
4174 | ||
4175 | my $localnode = PVE::INotify::nodename(); | |
4176 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
4177 | ||
4178 | PVE::Cluster::check_cfs_quorum(); | |
4179 | ||
4180 | PVE::Cluster::check_node_exists($target); | |
4181 | ||
4182 | my $targetip = PVE::Cluster::remote_node_ip($target); | |
4183 | ||
4184 | my $vmid = extract_param($param, 'vmid'); | |
4185 | ||
afdb31d5 | 4186 | raise_param_exc({ force => "Only root may use this option." }) |
a0d1b1a2 | 4187 | if $param->{force} && $authuser ne 'root@pam'; |
3ea94c60 | 4188 | |
2de2d6f7 TL |
4189 | raise_param_exc({ migration_type => "Only root may use this option." }) |
4190 | if $param->{migration_type} && $authuser ne 'root@pam'; | |
4191 | ||
4192 | # allow root only until better network permissions are available | |
4193 | raise_param_exc({ migration_network => "Only root may use this option." }) | |
4194 | if $param->{migration_network} && $authuser ne 'root@pam'; | |
4195 | ||
3ea94c60 | 4196 | # test if VM exists |
ffda963f | 4197 | my $conf = PVE::QemuConfig->load_config($vmid); |
3ea94c60 DM |
4198 | |
4199 | # try to detect errors early | |
a5ed42d3 | 4200 | |
ffda963f | 4201 | PVE::QemuConfig->check_lock($conf); |
a5ed42d3 | 4202 | |
3ea94c60 | 4203 | if (PVE::QemuServer::check_running($vmid)) { |
fda72913 | 4204 | die "can't migrate running VM without --online\n" if !$param->{online}; |
aa491a6e FE |
4205 | |
4206 | my $repl_conf = PVE::ReplicationConfig->new(); | |
4207 | my $is_replicated = $repl_conf->check_for_existing_jobs($vmid, 1); | |
4208 | my $is_replicated_to_target = defined($repl_conf->find_local_replication_job($vmid, $target)); | |
68980d66 FE |
4209 | if (!$param->{force} && $is_replicated && !$is_replicated_to_target) { |
4210 | die "Cannot live-migrate replicated VM to node '$target' - not a replication " . | |
4211 | "target. Use 'force' to override.\n"; | |
aa491a6e | 4212 | } |
13739386 | 4213 | } else { |
c3ddb94d | 4214 | warn "VM isn't running. Doing offline migration instead.\n" if $param->{online}; |
13739386 | 4215 | $param->{online} = 0; |
3ea94c60 DM |
4216 | } |
4217 | ||
47152e2e | 4218 | my $storecfg = PVE::Storage::config(); |
bf8fc5a3 FG |
4219 | if (my $targetstorage = $param->{targetstorage}) { |
4220 | my $storagemap = eval { PVE::JSONSchema::parse_idmap($targetstorage, 'pve-storage-id') }; | |
e214cda8 | 4221 | raise_param_exc({ targetstorage => "failed to parse storage map: $@" }) |
bf8fc5a3 FG |
4222 | if $@; |
4223 | ||
aea447bb FG |
4224 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']) |
4225 | if !defined($storagemap->{identity}); | |
4226 | ||
bd61033e | 4227 | foreach my $target_sid (values %{$storagemap->{entries}}) { |
9fb295d0 | 4228 | $check_storage_access_migrate->($rpcenv, $authuser, $storecfg, $target_sid, $target); |
bf8fc5a3 FG |
4229 | } |
4230 | ||
9fb295d0 | 4231 | $check_storage_access_migrate->($rpcenv, $authuser, $storecfg, $storagemap->{default}, $target) |
bf8fc5a3 FG |
4232 | if $storagemap->{default}; |
4233 | ||
4234 | PVE::QemuServer::check_storage_availability($storecfg, $conf, $target) | |
4235 | if $storagemap->{identity}; | |
4236 | ||
4237 | $param->{storagemap} = $storagemap; | |
d80ad67f AD |
4238 | } else { |
4239 | PVE::QemuServer::check_storage_availability($storecfg, $conf, $target); | |
4240 | } | |
47152e2e | 4241 | |
2003f0f8 | 4242 | if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { |
3ea94c60 | 4243 | |
88fc87b4 DM |
4244 | my $hacmd = sub { |
4245 | my $upid = shift; | |
3ea94c60 | 4246 | |
02765844 | 4247 | print "Requesting HA migration for VM $vmid to node $target\n"; |
88fc87b4 | 4248 | |
a4262553 | 4249 | my $cmd = ['ha-manager', 'migrate', "vm:$vmid", $target]; |
88fc87b4 | 4250 | PVE::Tools::run_command($cmd); |
88fc87b4 DM |
4251 | return; |
4252 | }; | |
4253 | ||
4254 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); | |
4255 | ||
4256 | } else { | |
4257 | ||
f53c6ad8 | 4258 | my $realcmd = sub { |
f53c6ad8 DM |
4259 | PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param); |
4260 | }; | |
88fc87b4 | 4261 | |
f53c6ad8 DM |
4262 | my $worker = sub { |
4263 | return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd); | |
88fc87b4 DM |
4264 | }; |
4265 | ||
f53c6ad8 | 4266 | return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $worker); |
88fc87b4 | 4267 | } |
3ea94c60 | 4268 | |
3ea94c60 | 4269 | }}); |
1e3baf05 | 4270 | |
91c94f0a | 4271 | __PACKAGE__->register_method({ |
afdb31d5 DM |
4272 | name => 'monitor', |
4273 | path => '{vmid}/monitor', | |
91c94f0a DM |
4274 | method => 'POST', |
4275 | protected => 1, | |
4276 | proxyto => 'node', | |
4277 | description => "Execute Qemu monitor commands.", | |
a0d1b1a2 | 4278 | permissions => { |
a8f2f427 | 4279 | description => "Sys.Modify is required for (sub)commands which are not read-only ('info *' and 'help')", |
c07a9e3d | 4280 | check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]], |
a0d1b1a2 | 4281 | }, |
91c94f0a | 4282 | parameters => { |
3326ae19 | 4283 | additionalProperties => 0, |
91c94f0a DM |
4284 | properties => { |
4285 | node => get_standard_option('pve-node'), | |
4286 | vmid => get_standard_option('pve-vmid'), | |
4287 | command => { | |
4288 | type => 'string', | |
4289 | description => "The monitor command.", | |
4290 | } | |
4291 | }, | |
4292 | }, | |
4293 | returns => { type => 'string'}, | |
4294 | code => sub { | |
4295 | my ($param) = @_; | |
4296 | ||
a8f2f427 FG |
4297 | my $rpcenv = PVE::RPCEnvironment::get(); |
4298 | my $authuser = $rpcenv->get_user(); | |
4299 | ||
4300 | my $is_ro = sub { | |
4301 | my $command = shift; | |
4302 | return $command =~ m/^\s*info(\s+|$)/ | |
4303 | || $command =~ m/^\s*help\s*$/; | |
4304 | }; | |
4305 | ||
4306 | $rpcenv->check_full($authuser, "/", ['Sys.Modify']) | |
4307 | if !&$is_ro($param->{command}); | |
4308 | ||
91c94f0a DM |
4309 | my $vmid = $param->{vmid}; |
4310 | ||
ffda963f | 4311 | my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists |
91c94f0a DM |
4312 | |
4313 | my $res = ''; | |
4314 | eval { | |
0a13e08e | 4315 | $res = PVE::QemuServer::Monitor::hmp_cmd($vmid, $param->{command}); |
91c94f0a DM |
4316 | }; |
4317 | $res = "ERROR: $@" if $@; | |
4318 | ||
4319 | return $res; | |
4320 | }}); | |
4321 | ||
0d02881c AD |
4322 | __PACKAGE__->register_method({ |
4323 | name => 'resize_vm', | |
614e3941 | 4324 | path => '{vmid}/resize', |
0d02881c AD |
4325 | method => 'PUT', |
4326 | protected => 1, | |
4327 | proxyto => 'node', | |
2f48a4f5 | 4328 | description => "Extend volume size.", |
0d02881c | 4329 | permissions => { |
3b2773f6 | 4330 | check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], |
0d02881c AD |
4331 | }, |
4332 | parameters => { | |
3326ae19 TL |
4333 | additionalProperties => 0, |
4334 | properties => { | |
2f48a4f5 | 4335 | node => get_standard_option('pve-node'), |
335af808 | 4336 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
2f48a4f5 DM |
4337 | skiplock => get_standard_option('skiplock'), |
4338 | disk => { | |
4339 | type => 'string', | |
4340 | description => "The disk you want to resize.", | |
e0fd2b2f | 4341 | enum => [PVE::QemuServer::Drive::valid_drive_names()], |
2f48a4f5 DM |
4342 | }, |
4343 | size => { | |
4344 | type => 'string', | |
f91b2e45 | 4345 | pattern => '\+?\d+(\.\d+)?[KMGT]?', |
e248477e | 4346 | 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.", |
2f48a4f5 DM |
4347 | }, |
4348 | digest => { | |
4349 | type => 'string', | |
4350 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
4351 | maxLength => 40, | |
4352 | optional => 1, | |
4353 | }, | |
4354 | }, | |
0d02881c AD |
4355 | }, |
4356 | returns => { type => 'null'}, | |
4357 | code => sub { | |
4358 | my ($param) = @_; | |
4359 | ||
4360 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4361 | ||
4362 | my $authuser = $rpcenv->get_user(); | |
4363 | ||
4364 | my $node = extract_param($param, 'node'); | |
4365 | ||
4366 | my $vmid = extract_param($param, 'vmid'); | |
4367 | ||
4368 | my $digest = extract_param($param, 'digest'); | |
4369 | ||
2f48a4f5 | 4370 | my $disk = extract_param($param, 'disk'); |
75466c4f | 4371 | |
2f48a4f5 | 4372 | my $sizestr = extract_param($param, 'size'); |
0d02881c | 4373 | |
f91b2e45 | 4374 | my $skiplock = extract_param($param, 'skiplock'); |
0d02881c AD |
4375 | raise_param_exc({ skiplock => "Only root may use this option." }) |
4376 | if $skiplock && $authuser ne 'root@pam'; | |
4377 | ||
0d02881c AD |
4378 | my $storecfg = PVE::Storage::config(); |
4379 | ||
0d02881c AD |
4380 | my $updatefn = sub { |
4381 | ||
ffda963f | 4382 | my $conf = PVE::QemuConfig->load_config($vmid); |
0d02881c AD |
4383 | |
4384 | die "checksum missmatch (file change by other user?)\n" | |
4385 | if $digest && $digest ne $conf->{digest}; | |
ffda963f | 4386 | PVE::QemuConfig->check_lock($conf) if !$skiplock; |
0d02881c | 4387 | |
f91b2e45 DM |
4388 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; |
4389 | ||
4390 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
4391 | ||
d662790a WL |
4392 | my (undef, undef, undef, undef, undef, undef, $format) = |
4393 | PVE::Storage::parse_volname($storecfg, $drive->{file}); | |
4394 | ||
c2ed338e | 4395 | die "can't resize volume: $disk if snapshot exists\n" |
d662790a WL |
4396 | if %{$conf->{snapshots}} && $format eq 'qcow2'; |
4397 | ||
f91b2e45 DM |
4398 | my $volid = $drive->{file}; |
4399 | ||
4400 | die "disk '$disk' has no associated volume\n" if !$volid; | |
4401 | ||
4402 | die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
4403 | ||
4404 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
4405 | ||
4406 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
4407 | ||
b572a606 | 4408 | PVE::Storage::activate_volumes($storecfg, [$volid]); |
f91b2e45 DM |
4409 | my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5); |
4410 | ||
ed94b2ad | 4411 | die "Could not determine current size of volume '$volid'\n" if !defined($size); |
f8b829aa | 4412 | |
f91b2e45 DM |
4413 | die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/; |
4414 | my ($ext, $newsize, $unit) = ($1, $2, $4); | |
4415 | if ($unit) { | |
4416 | if ($unit eq 'K') { | |
4417 | $newsize = $newsize * 1024; | |
4418 | } elsif ($unit eq 'M') { | |
4419 | $newsize = $newsize * 1024 * 1024; | |
4420 | } elsif ($unit eq 'G') { | |
4421 | $newsize = $newsize * 1024 * 1024 * 1024; | |
4422 | } elsif ($unit eq 'T') { | |
4423 | $newsize = $newsize * 1024 * 1024 * 1024 * 1024; | |
4424 | } | |
4425 | } | |
4426 | $newsize += $size if $ext; | |
4427 | $newsize = int($newsize); | |
4428 | ||
9a478b17 | 4429 | die "shrinking disks is not supported\n" if $newsize < $size; |
f91b2e45 DM |
4430 | |
4431 | return if $size == $newsize; | |
4432 | ||
2f48a4f5 | 4433 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr"); |
0d02881c | 4434 | |
f91b2e45 | 4435 | PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize); |
75466c4f | 4436 | |
e29e5be6 | 4437 | $drive->{size} = $newsize; |
71c58bb7 | 4438 | $conf->{$disk} = PVE::QemuServer::print_drive($drive); |
f91b2e45 | 4439 | |
ffda963f | 4440 | PVE::QemuConfig->write_config($vmid, $conf); |
f91b2e45 | 4441 | }; |
0d02881c | 4442 | |
ffda963f | 4443 | PVE::QemuConfig->lock_config($vmid, $updatefn); |
d1c1af4b | 4444 | return; |
0d02881c AD |
4445 | }}); |
4446 | ||
9dbd1ee4 | 4447 | __PACKAGE__->register_method({ |
7e7d7b61 | 4448 | name => 'snapshot_list', |
9dbd1ee4 | 4449 | path => '{vmid}/snapshot', |
7e7d7b61 DM |
4450 | method => 'GET', |
4451 | description => "List all snapshots.", | |
4452 | permissions => { | |
4453 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
4454 | }, | |
4455 | proxyto => 'node', | |
4456 | protected => 1, # qemu pid files are only readable by root | |
4457 | parameters => { | |
3326ae19 | 4458 | additionalProperties => 0, |
7e7d7b61 | 4459 | properties => { |
e261de40 | 4460 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
7e7d7b61 DM |
4461 | node => get_standard_option('pve-node'), |
4462 | }, | |
4463 | }, | |
4464 | returns => { | |
4465 | type => 'array', | |
4466 | items => { | |
4467 | type => "object", | |
ce9b0a38 DM |
4468 | properties => { |
4469 | name => { | |
4470 | description => "Snapshot identifier. Value 'current' identifies the current VM.", | |
4471 | type => 'string', | |
4472 | }, | |
4473 | vmstate => { | |
4474 | description => "Snapshot includes RAM.", | |
4475 | type => 'boolean', | |
4476 | optional => 1, | |
4477 | }, | |
4478 | description => { | |
4479 | description => "Snapshot description.", | |
4480 | type => 'string', | |
4481 | }, | |
4482 | snaptime => { | |
4483 | description => "Snapshot creation time", | |
4484 | type => 'integer', | |
4485 | renderer => 'timestamp', | |
4486 | optional => 1, | |
4487 | }, | |
4488 | parent => { | |
4489 | description => "Parent snapshot identifier.", | |
4490 | type => 'string', | |
4491 | optional => 1, | |
4492 | }, | |
4493 | }, | |
7e7d7b61 DM |
4494 | }, |
4495 | links => [ { rel => 'child', href => "{name}" } ], | |
4496 | }, | |
4497 | code => sub { | |
4498 | my ($param) = @_; | |
4499 | ||
6aa4651b DM |
4500 | my $vmid = $param->{vmid}; |
4501 | ||
ffda963f | 4502 | my $conf = PVE::QemuConfig->load_config($vmid); |
7e7d7b61 DM |
4503 | my $snaphash = $conf->{snapshots} || {}; |
4504 | ||
4505 | my $res = []; | |
4506 | ||
4507 | foreach my $name (keys %$snaphash) { | |
0ea6bc69 | 4508 | my $d = $snaphash->{$name}; |
75466c4f DM |
4509 | my $item = { |
4510 | name => $name, | |
4511 | snaptime => $d->{snaptime} || 0, | |
6aa4651b | 4512 | vmstate => $d->{vmstate} ? 1 : 0, |
982c7f12 DM |
4513 | description => $d->{description} || '', |
4514 | }; | |
0ea6bc69 | 4515 | $item->{parent} = $d->{parent} if $d->{parent}; |
3ee28e38 | 4516 | $item->{snapstate} = $d->{snapstate} if $d->{snapstate}; |
0ea6bc69 DM |
4517 | push @$res, $item; |
4518 | } | |
4519 | ||
6aa4651b | 4520 | my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0; |
ce9b0a38 DM |
4521 | my $current = { |
4522 | name => 'current', | |
4523 | digest => $conf->{digest}, | |
4524 | running => $running, | |
4525 | description => "You are here!", | |
4526 | }; | |
d1914468 DM |
4527 | $current->{parent} = $conf->{parent} if $conf->{parent}; |
4528 | ||
4529 | push @$res, $current; | |
7e7d7b61 DM |
4530 | |
4531 | return $res; | |
4532 | }}); | |
4533 | ||
4534 | __PACKAGE__->register_method({ | |
4535 | name => 'snapshot', | |
4536 | path => '{vmid}/snapshot', | |
4537 | method => 'POST', | |
9dbd1ee4 AD |
4538 | protected => 1, |
4539 | proxyto => 'node', | |
4540 | description => "Snapshot a VM.", | |
4541 | permissions => { | |
f1baf1df | 4542 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], |
9dbd1ee4 AD |
4543 | }, |
4544 | parameters => { | |
4545 | additionalProperties => 0, | |
4546 | properties => { | |
4547 | node => get_standard_option('pve-node'), | |
335af808 | 4548 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
8abd398b | 4549 | snapname => get_standard_option('pve-snapshot-name'), |
9dbd1ee4 AD |
4550 | vmstate => { |
4551 | optional => 1, | |
4552 | type => 'boolean', | |
4553 | description => "Save the vmstate", | |
4554 | }, | |
782f4f75 DM |
4555 | description => { |
4556 | optional => 1, | |
4557 | type => 'string', | |
4558 | description => "A textual description or comment.", | |
4559 | }, | |
9dbd1ee4 AD |
4560 | }, |
4561 | }, | |
7e7d7b61 DM |
4562 | returns => { |
4563 | type => 'string', | |
4564 | description => "the task ID.", | |
4565 | }, | |
9dbd1ee4 AD |
4566 | code => sub { |
4567 | my ($param) = @_; | |
4568 | ||
4569 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4570 | ||
4571 | my $authuser = $rpcenv->get_user(); | |
4572 | ||
4573 | my $node = extract_param($param, 'node'); | |
4574 | ||
4575 | my $vmid = extract_param($param, 'vmid'); | |
4576 | ||
9dbd1ee4 AD |
4577 | my $snapname = extract_param($param, 'snapname'); |
4578 | ||
d1914468 DM |
4579 | die "unable to use snapshot name 'current' (reserved name)\n" |
4580 | if $snapname eq 'current'; | |
4581 | ||
a85c6be1 FG |
4582 | die "unable to use snapshot name 'pending' (reserved name)\n" |
4583 | if lc($snapname) eq 'pending'; | |
4584 | ||
7e7d7b61 | 4585 | my $realcmd = sub { |
22c377f0 | 4586 | PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname"); |
c2ed338e | 4587 | PVE::QemuConfig->snapshot_create($vmid, $snapname, $param->{vmstate}, |
af9110dd | 4588 | $param->{description}); |
7e7d7b61 DM |
4589 | }; |
4590 | ||
4591 | return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd); | |
4592 | }}); | |
4593 | ||
154ccdcd DM |
4594 | __PACKAGE__->register_method({ |
4595 | name => 'snapshot_cmd_idx', | |
4596 | path => '{vmid}/snapshot/{snapname}', | |
4597 | description => '', | |
4598 | method => 'GET', | |
4599 | permissions => { | |
4600 | user => 'all', | |
4601 | }, | |
4602 | parameters => { | |
3326ae19 | 4603 | additionalProperties => 0, |
154ccdcd DM |
4604 | properties => { |
4605 | vmid => get_standard_option('pve-vmid'), | |
4606 | node => get_standard_option('pve-node'), | |
8abd398b | 4607 | snapname => get_standard_option('pve-snapshot-name'), |
154ccdcd DM |
4608 | }, |
4609 | }, | |
4610 | returns => { | |
4611 | type => 'array', | |
4612 | items => { | |
4613 | type => "object", | |
4614 | properties => {}, | |
4615 | }, | |
4616 | links => [ { rel => 'child', href => "{cmd}" } ], | |
4617 | }, | |
4618 | code => sub { | |
4619 | my ($param) = @_; | |
4620 | ||
4621 | my $res = []; | |
4622 | ||
4623 | push @$res, { cmd => 'rollback' }; | |
d788cea6 | 4624 | push @$res, { cmd => 'config' }; |
154ccdcd DM |
4625 | |
4626 | return $res; | |
4627 | }}); | |
4628 | ||
d788cea6 DM |
4629 | __PACKAGE__->register_method({ |
4630 | name => 'update_snapshot_config', | |
4631 | path => '{vmid}/snapshot/{snapname}/config', | |
4632 | method => 'PUT', | |
4633 | protected => 1, | |
4634 | proxyto => 'node', | |
4635 | description => "Update snapshot metadata.", | |
4636 | permissions => { | |
4637 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
4638 | }, | |
4639 | parameters => { | |
4640 | additionalProperties => 0, | |
4641 | properties => { | |
4642 | node => get_standard_option('pve-node'), | |
4643 | vmid => get_standard_option('pve-vmid'), | |
4644 | snapname => get_standard_option('pve-snapshot-name'), | |
4645 | description => { | |
4646 | optional => 1, | |
4647 | type => 'string', | |
4648 | description => "A textual description or comment.", | |
4649 | }, | |
4650 | }, | |
4651 | }, | |
4652 | returns => { type => 'null' }, | |
4653 | code => sub { | |
4654 | my ($param) = @_; | |
4655 | ||
4656 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4657 | ||
4658 | my $authuser = $rpcenv->get_user(); | |
4659 | ||
4660 | my $vmid = extract_param($param, 'vmid'); | |
4661 | ||
4662 | my $snapname = extract_param($param, 'snapname'); | |
4663 | ||
d1c1af4b | 4664 | return if !defined($param->{description}); |
d788cea6 DM |
4665 | |
4666 | my $updatefn = sub { | |
4667 | ||
ffda963f | 4668 | my $conf = PVE::QemuConfig->load_config($vmid); |
d788cea6 | 4669 | |
ffda963f | 4670 | PVE::QemuConfig->check_lock($conf); |
d788cea6 DM |
4671 | |
4672 | my $snap = $conf->{snapshots}->{$snapname}; | |
4673 | ||
75466c4f DM |
4674 | die "snapshot '$snapname' does not exist\n" if !defined($snap); |
4675 | ||
d788cea6 DM |
4676 | $snap->{description} = $param->{description} if defined($param->{description}); |
4677 | ||
ffda963f | 4678 | PVE::QemuConfig->write_config($vmid, $conf); |
d788cea6 DM |
4679 | }; |
4680 | ||
ffda963f | 4681 | PVE::QemuConfig->lock_config($vmid, $updatefn); |
d788cea6 | 4682 | |
d1c1af4b | 4683 | return; |
d788cea6 DM |
4684 | }}); |
4685 | ||
4686 | __PACKAGE__->register_method({ | |
4687 | name => 'get_snapshot_config', | |
4688 | path => '{vmid}/snapshot/{snapname}/config', | |
4689 | method => 'GET', | |
4690 | proxyto => 'node', | |
4691 | description => "Get snapshot configuration", | |
4692 | permissions => { | |
65204e92 | 4693 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot', 'VM.Snapshot.Rollback', 'VM.Audit' ], any => 1], |
d788cea6 DM |
4694 | }, |
4695 | parameters => { | |
4696 | additionalProperties => 0, | |
4697 | properties => { | |
4698 | node => get_standard_option('pve-node'), | |
4699 | vmid => get_standard_option('pve-vmid'), | |
4700 | snapname => get_standard_option('pve-snapshot-name'), | |
4701 | }, | |
4702 | }, | |
4703 | returns => { type => "object" }, | |
4704 | code => sub { | |
4705 | my ($param) = @_; | |
4706 | ||
4707 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4708 | ||
4709 | my $authuser = $rpcenv->get_user(); | |
4710 | ||
4711 | my $vmid = extract_param($param, 'vmid'); | |
4712 | ||
4713 | my $snapname = extract_param($param, 'snapname'); | |
4714 | ||
ffda963f | 4715 | my $conf = PVE::QemuConfig->load_config($vmid); |
d788cea6 DM |
4716 | |
4717 | my $snap = $conf->{snapshots}->{$snapname}; | |
4718 | ||
75466c4f DM |
4719 | die "snapshot '$snapname' does not exist\n" if !defined($snap); |
4720 | ||
d788cea6 DM |
4721 | return $snap; |
4722 | }}); | |
4723 | ||
7e7d7b61 DM |
4724 | __PACKAGE__->register_method({ |
4725 | name => 'rollback', | |
154ccdcd | 4726 | path => '{vmid}/snapshot/{snapname}/rollback', |
7e7d7b61 DM |
4727 | method => 'POST', |
4728 | protected => 1, | |
4729 | proxyto => 'node', | |
4730 | description => "Rollback VM state to specified snapshot.", | |
4731 | permissions => { | |
c268337d | 4732 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot', 'VM.Snapshot.Rollback' ], any => 1], |
7e7d7b61 DM |
4733 | }, |
4734 | parameters => { | |
4735 | additionalProperties => 0, | |
4736 | properties => { | |
4737 | node => get_standard_option('pve-node'), | |
335af808 | 4738 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
8abd398b | 4739 | snapname => get_standard_option('pve-snapshot-name'), |
7e7d7b61 DM |
4740 | }, |
4741 | }, | |
4742 | returns => { | |
4743 | type => 'string', | |
4744 | description => "the task ID.", | |
4745 | }, | |
4746 | code => sub { | |
4747 | my ($param) = @_; | |
4748 | ||
4749 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4750 | ||
4751 | my $authuser = $rpcenv->get_user(); | |
4752 | ||
4753 | my $node = extract_param($param, 'node'); | |
4754 | ||
4755 | my $vmid = extract_param($param, 'vmid'); | |
4756 | ||
4757 | my $snapname = extract_param($param, 'snapname'); | |
4758 | ||
7e7d7b61 | 4759 | my $realcmd = sub { |
22c377f0 | 4760 | PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname"); |
b2c9558d | 4761 | PVE::QemuConfig->snapshot_rollback($vmid, $snapname); |
7e7d7b61 DM |
4762 | }; |
4763 | ||
c068c1c3 WL |
4764 | my $worker = sub { |
4765 | # hold migration lock, this makes sure that nobody create replication snapshots | |
4766 | return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd); | |
4767 | }; | |
4768 | ||
4769 | return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $worker); | |
7e7d7b61 DM |
4770 | }}); |
4771 | ||
4772 | __PACKAGE__->register_method({ | |
4773 | name => 'delsnapshot', | |
4774 | path => '{vmid}/snapshot/{snapname}', | |
4775 | method => 'DELETE', | |
4776 | protected => 1, | |
4777 | proxyto => 'node', | |
4778 | description => "Delete a VM snapshot.", | |
4779 | permissions => { | |
f1baf1df | 4780 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], |
7e7d7b61 DM |
4781 | }, |
4782 | parameters => { | |
4783 | additionalProperties => 0, | |
4784 | properties => { | |
4785 | node => get_standard_option('pve-node'), | |
335af808 | 4786 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
8abd398b | 4787 | snapname => get_standard_option('pve-snapshot-name'), |
3ee28e38 DM |
4788 | force => { |
4789 | optional => 1, | |
4790 | type => 'boolean', | |
4791 | description => "For removal from config file, even if removing disk snapshots fails.", | |
4792 | }, | |
7e7d7b61 DM |
4793 | }, |
4794 | }, | |
4795 | returns => { | |
4796 | type => 'string', | |
4797 | description => "the task ID.", | |
4798 | }, | |
4799 | code => sub { | |
4800 | my ($param) = @_; | |
4801 | ||
4802 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4803 | ||
4804 | my $authuser = $rpcenv->get_user(); | |
4805 | ||
4806 | my $node = extract_param($param, 'node'); | |
4807 | ||
4808 | my $vmid = extract_param($param, 'vmid'); | |
4809 | ||
4810 | my $snapname = extract_param($param, 'snapname'); | |
4811 | ||
1770b70f | 4812 | my $lock_obtained; |
fdbbed2f | 4813 | my $do_delete = sub { |
1770b70f | 4814 | $lock_obtained = 1; |
22c377f0 | 4815 | PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname"); |
b2c9558d | 4816 | PVE::QemuConfig->snapshot_delete($vmid, $snapname, $param->{force}); |
7e7d7b61 | 4817 | }; |
9dbd1ee4 | 4818 | |
fdbbed2f FE |
4819 | my $realcmd = sub { |
4820 | if ($param->{force}) { | |
4821 | $do_delete->(); | |
4822 | } else { | |
1770b70f FG |
4823 | eval { PVE::GuestHelpers::guest_migration_lock($vmid, 10, $do_delete); }; |
4824 | if (my $err = $@) { | |
4825 | die $err if $lock_obtained; | |
4826 | die "Failed to obtain guest migration lock - replication running?\n"; | |
4827 | } | |
fdbbed2f FE |
4828 | } |
4829 | }; | |
4830 | ||
7b2257a8 | 4831 | return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd); |
9dbd1ee4 AD |
4832 | }}); |
4833 | ||
04a69bb4 AD |
4834 | __PACKAGE__->register_method({ |
4835 | name => 'template', | |
4836 | path => '{vmid}/template', | |
4837 | method => 'POST', | |
4838 | protected => 1, | |
4839 | proxyto => 'node', | |
4840 | description => "Create a Template.", | |
b02691d8 | 4841 | permissions => { |
7af0a6c8 DM |
4842 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", |
4843 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
b02691d8 | 4844 | }, |
04a69bb4 AD |
4845 | parameters => { |
4846 | additionalProperties => 0, | |
4847 | properties => { | |
4848 | node => get_standard_option('pve-node'), | |
335af808 | 4849 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }), |
04a69bb4 AD |
4850 | disk => { |
4851 | optional => 1, | |
4852 | type => 'string', | |
4853 | description => "If you want to convert only 1 disk to base image.", | |
e0fd2b2f | 4854 | enum => [PVE::QemuServer::Drive::valid_drive_names()], |
04a69bb4 AD |
4855 | }, |
4856 | ||
4857 | }, | |
4858 | }, | |
b297918c FG |
4859 | returns => { |
4860 | type => 'string', | |
4861 | description => "the task ID.", | |
4862 | }, | |
04a69bb4 AD |
4863 | code => sub { |
4864 | my ($param) = @_; | |
4865 | ||
4866 | my $rpcenv = PVE::RPCEnvironment::get(); | |
4867 | ||
4868 | my $authuser = $rpcenv->get_user(); | |
4869 | ||
4870 | my $node = extract_param($param, 'node'); | |
4871 | ||
4872 | my $vmid = extract_param($param, 'vmid'); | |
4873 | ||
4874 | my $disk = extract_param($param, 'disk'); | |
4875 | ||
d2ceac56 | 4876 | my $load_and_check = sub { |
ffda963f | 4877 | my $conf = PVE::QemuConfig->load_config($vmid); |
04a69bb4 | 4878 | |
ffda963f | 4879 | PVE::QemuConfig->check_lock($conf); |
04a69bb4 | 4880 | |
75466c4f | 4881 | die "unable to create template, because VM contains snapshots\n" |
b91c2aae | 4882 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); |
0402a80b | 4883 | |
75466c4f | 4884 | die "you can't convert a template to a template\n" |
ffda963f | 4885 | if PVE::QemuConfig->is_template($conf) && !$disk; |
0402a80b | 4886 | |
75466c4f | 4887 | die "you can't convert a VM to template if VM is running\n" |
218cab9a | 4888 | if PVE::QemuServer::check_running($vmid); |
35c5fdef | 4889 | |
d2ceac56 FG |
4890 | return $conf; |
4891 | }; | |
04a69bb4 | 4892 | |
d2ceac56 | 4893 | $load_and_check->(); |
75e7e997 | 4894 | |
d2ceac56 FG |
4895 | my $realcmd = sub { |
4896 | PVE::QemuConfig->lock_config($vmid, sub { | |
4897 | my $conf = $load_and_check->(); | |
4898 | ||
4899 | $conf->{template} = 1; | |
4900 | PVE::QemuConfig->write_config($vmid, $conf); | |
4901 | ||
4902 | PVE::QemuServer::template_create($vmid, $conf, $disk); | |
4903 | }); | |
04a69bb4 AD |
4904 | }; |
4905 | ||
d2ceac56 | 4906 | return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd); |
04a69bb4 AD |
4907 | }}); |
4908 | ||
73709749 ML |
4909 | __PACKAGE__->register_method({ |
4910 | name => 'cloudinit_generated_config_dump', | |
4911 | path => '{vmid}/cloudinit/dump', | |
4912 | method => 'GET', | |
4913 | proxyto => 'node', | |
4914 | description => "Get automatically generated cloudinit config.", | |
4915 | permissions => { | |
4916 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
4917 | }, | |
4918 | parameters => { | |
4919 | additionalProperties => 0, | |
4920 | properties => { | |
4921 | node => get_standard_option('pve-node'), | |
4922 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
4923 | type => { | |
4924 | description => 'Config type.', | |
4925 | type => 'string', | |
4926 | enum => ['user', 'network', 'meta'], | |
4927 | }, | |
4928 | }, | |
4929 | }, | |
4930 | returns => { | |
4931 | type => 'string', | |
4932 | }, | |
4933 | code => sub { | |
4934 | my ($param) = @_; | |
4935 | ||
4936 | my $conf = PVE::QemuConfig->load_config($param->{vmid}); | |
4937 | ||
4938 | return PVE::QemuServer::Cloudinit::dump_cloudinit_config($conf, $param->{vmid}, $param->{type}); | |
4939 | }}); | |
4940 | ||
1e3baf05 | 4941 | 1; |