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