]>
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' || | |
75466c4f | 229 | $opt eq 'cpu' || $opt eq 'smp' || |
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}) { | |
690 | foreach my $opt (keys $conf->{pending}) { | |
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 | ||
761 | foreach my $opt (keys $conf) { | |
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 | ||
770 | foreach my $opt (keys $conf->{pending}) { | |
771 | next if $opt eq 'delete'; | |
772 | next if ref($conf->{pending}->{$opt}); # just to be sure | |
773 | next if $conf->{$opt}; | |
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 | }, | |
1065 | force => { | |
1066 | type => 'boolean', | |
1067 | description => $opt_force_description, | |
1068 | optional => 1, | |
1069 | requires => 'delete', | |
1070 | }, | |
1071 | digest => { | |
1072 | type => 'string', | |
1073 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1074 | maxLength => 40, | |
1075 | optional => 1, | |
1076 | }, | |
1077 | background_delay => { | |
1078 | type => 'integer', | |
1079 | description => "Time to wait for the task to finish. We return 'null' if the task finish within that time.", | |
1080 | minimum => 1, | |
1081 | maximum => 30, | |
1082 | optional => 1, | |
1083 | }, | |
1084 | }), | |
1085 | }, | |
1086 | returns => { | |
1087 | type => 'string', | |
1088 | optional => 1, | |
1089 | }, | |
1090 | code => $update_vm_api, | |
1091 | }); | |
1092 | ||
1093 | __PACKAGE__->register_method({ | |
1094 | name => 'update_vm', | |
1095 | path => '{vmid}/config', | |
1096 | method => 'PUT', | |
1097 | protected => 1, | |
1098 | proxyto => 'node', | |
1099 | description => "Set virtual machine options (synchrounous API) - You should consider using the POST method instead for any actions involving hotplug or storage allocation.", | |
1100 | permissions => { | |
1101 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
1102 | }, | |
1103 | parameters => { | |
1104 | additionalProperties => 0, | |
1105 | properties => PVE::QemuServer::json_config_properties( | |
1106 | { | |
1107 | node => get_standard_option('pve-node'), | |
1108 | vmid => get_standard_option('pve-vmid'), | |
1109 | skiplock => get_standard_option('skiplock'), | |
1110 | delete => { | |
1111 | type => 'string', format => 'pve-configid-list', | |
1112 | description => "A list of settings you want to delete.", | |
1113 | optional => 1, | |
1114 | }, | |
1115 | force => { | |
1116 | type => 'boolean', | |
1117 | description => $opt_force_description, | |
1118 | optional => 1, | |
1119 | requires => 'delete', | |
1120 | }, | |
1121 | digest => { | |
1122 | type => 'string', | |
1123 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1124 | maxLength => 40, | |
1125 | optional => 1, | |
1126 | }, | |
1127 | }), | |
1128 | }, | |
1129 | returns => { type => 'null' }, | |
1130 | code => sub { | |
1131 | my ($param) = @_; | |
1132 | &$update_vm_api($param, 1); | |
1e3baf05 | 1133 | return undef; |
5555edea DM |
1134 | } |
1135 | }); | |
1e3baf05 DM |
1136 | |
1137 | ||
1138 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1139 | name => 'destroy_vm', |
1140 | path => '{vmid}', | |
1e3baf05 DM |
1141 | method => 'DELETE', |
1142 | protected => 1, | |
1143 | proxyto => 'node', | |
1144 | description => "Destroy the vm (also delete all used/owned volumes).", | |
a0d1b1a2 DM |
1145 | permissions => { |
1146 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
1147 | }, | |
1e3baf05 DM |
1148 | parameters => { |
1149 | additionalProperties => 0, | |
1150 | properties => { | |
1151 | node => get_standard_option('pve-node'), | |
1152 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 | 1153 | skiplock => get_standard_option('skiplock'), |
1e3baf05 DM |
1154 | }, |
1155 | }, | |
afdb31d5 | 1156 | returns => { |
5fdbe4f0 DM |
1157 | type => 'string', |
1158 | }, | |
1e3baf05 DM |
1159 | code => sub { |
1160 | my ($param) = @_; | |
1161 | ||
1162 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1163 | ||
a0d1b1a2 | 1164 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1165 | |
1166 | my $vmid = $param->{vmid}; | |
1167 | ||
1168 | my $skiplock = $param->{skiplock}; | |
afdb31d5 | 1169 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1170 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 1171 | |
5fdbe4f0 DM |
1172 | # test if VM exists |
1173 | my $conf = PVE::QemuServer::load_config($vmid); | |
1174 | ||
afdb31d5 | 1175 | my $storecfg = PVE::Storage::config(); |
1e3baf05 | 1176 | |
75466c4f | 1177 | my $delVMfromPoolFn = sub { |
502d18a2 | 1178 | my $usercfg = cfs_read_file("user.cfg"); |
5d0094ea DM |
1179 | if (my $pool = $usercfg->{vms}->{$vmid}) { |
1180 | if (my $data = $usercfg->{pools}->{$pool}) { | |
1181 | delete $data->{vms}->{$vmid}; | |
1182 | delete $usercfg->{vms}->{$vmid}; | |
1183 | cfs_write_file("user.cfg", $usercfg); | |
1184 | } | |
502d18a2 DM |
1185 | } |
1186 | }; | |
1187 | ||
5fdbe4f0 | 1188 | my $realcmd = sub { |
ff1a2432 DM |
1189 | my $upid = shift; |
1190 | ||
1191 | syslog('info', "destroy VM $vmid: $upid\n"); | |
1192 | ||
5fdbe4f0 | 1193 | PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); |
502d18a2 | 1194 | |
be517049 | 1195 | PVE::AccessControl::remove_vm_from_pool($vmid); |
5fdbe4f0 | 1196 | }; |
1e3baf05 | 1197 | |
a0d1b1a2 | 1198 | return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd); |
1e3baf05 DM |
1199 | }}); |
1200 | ||
1201 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1202 | name => 'unlink', |
1203 | path => '{vmid}/unlink', | |
1e3baf05 DM |
1204 | method => 'PUT', |
1205 | protected => 1, | |
1206 | proxyto => 'node', | |
1207 | description => "Unlink/delete disk images.", | |
a0d1b1a2 DM |
1208 | permissions => { |
1209 | check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']], | |
1210 | }, | |
1e3baf05 DM |
1211 | parameters => { |
1212 | additionalProperties => 0, | |
1213 | properties => { | |
1214 | node => get_standard_option('pve-node'), | |
1215 | vmid => get_standard_option('pve-vmid'), | |
1216 | idlist => { | |
1217 | type => 'string', format => 'pve-configid-list', | |
1218 | description => "A list of disk IDs you want to delete.", | |
1219 | }, | |
1220 | force => { | |
1221 | type => 'boolean', | |
1222 | description => $opt_force_description, | |
1223 | optional => 1, | |
1224 | }, | |
1225 | }, | |
1226 | }, | |
1227 | returns => { type => 'null'}, | |
1228 | code => sub { | |
1229 | my ($param) = @_; | |
1230 | ||
1231 | $param->{delete} = extract_param($param, 'idlist'); | |
1232 | ||
1233 | __PACKAGE__->update_vm($param); | |
1234 | ||
1235 | return undef; | |
1236 | }}); | |
1237 | ||
1238 | my $sslcert; | |
1239 | ||
1240 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1241 | name => 'vncproxy', |
1242 | path => '{vmid}/vncproxy', | |
1e3baf05 DM |
1243 | method => 'POST', |
1244 | protected => 1, | |
1245 | permissions => { | |
378b359e | 1246 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], |
1e3baf05 DM |
1247 | }, |
1248 | description => "Creates a TCP VNC proxy connections.", | |
1249 | parameters => { | |
1250 | additionalProperties => 0, | |
1251 | properties => { | |
1252 | node => get_standard_option('pve-node'), | |
1253 | vmid => get_standard_option('pve-vmid'), | |
b4d5c000 SP |
1254 | websocket => { |
1255 | optional => 1, | |
1256 | type => 'boolean', | |
1257 | description => "starts websockify instead of vncproxy", | |
1258 | }, | |
1e3baf05 DM |
1259 | }, |
1260 | }, | |
afdb31d5 | 1261 | returns => { |
1e3baf05 DM |
1262 | additionalProperties => 0, |
1263 | properties => { | |
1264 | user => { type => 'string' }, | |
1265 | ticket => { type => 'string' }, | |
1266 | cert => { type => 'string' }, | |
1267 | port => { type => 'integer' }, | |
1268 | upid => { type => 'string' }, | |
1269 | }, | |
1270 | }, | |
1271 | code => sub { | |
1272 | my ($param) = @_; | |
1273 | ||
1274 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1275 | ||
a0d1b1a2 | 1276 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1277 | |
1278 | my $vmid = $param->{vmid}; | |
1279 | my $node = $param->{node}; | |
983d4582 | 1280 | my $websocket = $param->{websocket}; |
1e3baf05 | 1281 | |
2dc9c148 | 1282 | my $conf = PVE::QemuServer::load_config($vmid, $node); # check if VM exists |
ef5e2be2 | 1283 | |
b6f39da2 DM |
1284 | my $authpath = "/vms/$vmid"; |
1285 | ||
a0d1b1a2 | 1286 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); |
b6f39da2 | 1287 | |
1e3baf05 DM |
1288 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) |
1289 | if !$sslcert; | |
1290 | ||
1291 | my $port = PVE::Tools::next_vnc_port(); | |
1292 | ||
1293 | my $remip; | |
ef5e2be2 | 1294 | my $remcmd = []; |
afdb31d5 | 1295 | |
4f1be36c | 1296 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { |
1e3baf05 | 1297 | $remip = PVE::Cluster::remote_node_ip($node); |
b4d5c000 | 1298 | # NOTE: kvm VNC traffic is already TLS encrypted or is known unsecure |
ef5e2be2 | 1299 | $remcmd = ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', $remip]; |
1e3baf05 DM |
1300 | } |
1301 | ||
afdb31d5 | 1302 | my $timeout = 10; |
1e3baf05 DM |
1303 | |
1304 | my $realcmd = sub { | |
1305 | my $upid = shift; | |
1306 | ||
1307 | syslog('info', "starting vnc proxy $upid\n"); | |
1308 | ||
ef5e2be2 | 1309 | my $cmd; |
1e3baf05 | 1310 | |
2dc23d72 | 1311 | if ($conf->{vga} && ($conf->{vga} =~ m/^serial\d+$/)) { |
ef5e2be2 | 1312 | |
983d4582 | 1313 | die "Websocket mode is not supported in vga serial mode!" if $websocket; |
b4d5c000 | 1314 | |
ef5e2be2 DM |
1315 | my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid, '-iface', $conf->{vga} ]; |
1316 | #my $termcmd = "/usr/bin/qm terminal -iface $conf->{vga}"; | |
1317 | $cmd = ['/usr/bin/vncterm', '-rfbport', $port, | |
fa8ea931 | 1318 | '-timeout', $timeout, '-authpath', $authpath, |
ef5e2be2 DM |
1319 | '-perm', 'Sys.Console', '-c', @$remcmd, @$termcmd]; |
1320 | } else { | |
1e3baf05 | 1321 | |
3e7567e0 DM |
1322 | $ENV{LC_PVE_TICKET} = $ticket if $websocket; # set ticket with "qm vncproxy" |
1323 | ||
ef5e2be2 DM |
1324 | my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid]; |
1325 | ||
1326 | my $qmstr = join(' ', @$qmcmd); | |
1327 | ||
1328 | # also redirect stderr (else we get RFB protocol errors) | |
1329 | $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"]; | |
1330 | } | |
1e3baf05 | 1331 | |
be62c45c | 1332 | PVE::Tools::run_command($cmd); |
1e3baf05 DM |
1333 | |
1334 | return; | |
1335 | }; | |
1336 | ||
a0d1b1a2 | 1337 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); |
1e3baf05 | 1338 | |
3da85107 DM |
1339 | PVE::Tools::wait_for_vnc_port($port); |
1340 | ||
1e3baf05 | 1341 | return { |
a0d1b1a2 | 1342 | user => $authuser, |
1e3baf05 | 1343 | ticket => $ticket, |
afdb31d5 DM |
1344 | port => $port, |
1345 | upid => $upid, | |
1346 | cert => $sslcert, | |
1e3baf05 DM |
1347 | }; |
1348 | }}); | |
1349 | ||
3e7567e0 DM |
1350 | __PACKAGE__->register_method({ |
1351 | name => 'vncwebsocket', | |
1352 | path => '{vmid}/vncwebsocket', | |
1353 | method => 'GET', | |
3e7567e0 | 1354 | permissions => { |
c422ce93 | 1355 | description => "You also need to pass a valid ticket (vncticket).", |
3e7567e0 DM |
1356 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], |
1357 | }, | |
4d00f52f | 1358 | description => "Opens a weksocket for VNC traffic.", |
3e7567e0 DM |
1359 | parameters => { |
1360 | additionalProperties => 0, | |
1361 | properties => { | |
1362 | node => get_standard_option('pve-node'), | |
1363 | vmid => get_standard_option('pve-vmid'), | |
c422ce93 DM |
1364 | vncticket => { |
1365 | description => "Ticket from previous call to vncproxy.", | |
1366 | type => 'string', | |
1367 | maxLength => 512, | |
1368 | }, | |
3e7567e0 DM |
1369 | port => { |
1370 | description => "Port number returned by previous vncproxy call.", | |
1371 | type => 'integer', | |
1372 | minimum => 5900, | |
1373 | maximum => 5999, | |
1374 | }, | |
1375 | }, | |
1376 | }, | |
1377 | returns => { | |
1378 | type => "object", | |
1379 | properties => { | |
1380 | port => { type => 'string' }, | |
1381 | }, | |
1382 | }, | |
1383 | code => sub { | |
1384 | my ($param) = @_; | |
1385 | ||
1386 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1387 | ||
1388 | my $authuser = $rpcenv->get_user(); | |
1389 | ||
1390 | my $vmid = $param->{vmid}; | |
1391 | my $node = $param->{node}; | |
1392 | ||
c422ce93 DM |
1393 | my $authpath = "/vms/$vmid"; |
1394 | ||
1395 | PVE::AccessControl::verify_vnc_ticket($param->{vncticket}, $authuser, $authpath); | |
1396 | ||
3e7567e0 DM |
1397 | my $conf = PVE::QemuServer::load_config($vmid, $node); # VM exists ? |
1398 | ||
1399 | # Note: VNC ports are acessible from outside, so we do not gain any | |
1400 | # security if we verify that $param->{port} belongs to VM $vmid. This | |
1401 | # check is done by verifying the VNC ticket (inside VNC protocol). | |
1402 | ||
1403 | my $port = $param->{port}; | |
f34ebd52 | 1404 | |
3e7567e0 DM |
1405 | return { port => $port }; |
1406 | }}); | |
1407 | ||
288eeea8 DM |
1408 | __PACKAGE__->register_method({ |
1409 | name => 'spiceproxy', | |
1410 | path => '{vmid}/spiceproxy', | |
78252ce7 | 1411 | method => 'POST', |
288eeea8 | 1412 | protected => 1, |
78252ce7 | 1413 | proxyto => 'node', |
288eeea8 DM |
1414 | permissions => { |
1415 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1416 | }, | |
1417 | description => "Returns a SPICE configuration to connect to the VM.", | |
1418 | parameters => { | |
1419 | additionalProperties => 0, | |
1420 | properties => { | |
1421 | node => get_standard_option('pve-node'), | |
1422 | vmid => get_standard_option('pve-vmid'), | |
dd25eecf | 1423 | proxy => get_standard_option('spice-proxy', { optional => 1 }), |
288eeea8 DM |
1424 | }, |
1425 | }, | |
dd25eecf | 1426 | returns => get_standard_option('remote-viewer-config'), |
288eeea8 DM |
1427 | code => sub { |
1428 | my ($param) = @_; | |
1429 | ||
1430 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1431 | ||
1432 | my $authuser = $rpcenv->get_user(); | |
1433 | ||
1434 | my $vmid = $param->{vmid}; | |
1435 | my $node = $param->{node}; | |
fb6c7260 | 1436 | my $proxy = $param->{proxy}; |
288eeea8 | 1437 | |
3e7567e0 DM |
1438 | my $conf = PVE::QemuServer::load_config($vmid, $node); |
1439 | my $title = "VM $vmid - $conf->{'name'}", | |
288eeea8 | 1440 | |
943340a6 | 1441 | my $port = PVE::QemuServer::spice_port($vmid); |
dd25eecf | 1442 | |
f34ebd52 | 1443 | my ($ticket, undef, $remote_viewer_config) = |
dd25eecf | 1444 | PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port); |
f34ebd52 | 1445 | |
288eeea8 DM |
1446 | PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket); |
1447 | PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30"); | |
f34ebd52 | 1448 | |
dd25eecf | 1449 | return $remote_viewer_config; |
288eeea8 DM |
1450 | }}); |
1451 | ||
5fdbe4f0 DM |
1452 | __PACKAGE__->register_method({ |
1453 | name => 'vmcmdidx', | |
afdb31d5 | 1454 | path => '{vmid}/status', |
5fdbe4f0 DM |
1455 | method => 'GET', |
1456 | proxyto => 'node', | |
1457 | description => "Directory index", | |
a0d1b1a2 DM |
1458 | permissions => { |
1459 | user => 'all', | |
1460 | }, | |
5fdbe4f0 DM |
1461 | parameters => { |
1462 | additionalProperties => 0, | |
1463 | properties => { | |
1464 | node => get_standard_option('pve-node'), | |
1465 | vmid => get_standard_option('pve-vmid'), | |
1466 | }, | |
1467 | }, | |
1468 | returns => { | |
1469 | type => 'array', | |
1470 | items => { | |
1471 | type => "object", | |
1472 | properties => { | |
1473 | subdir => { type => 'string' }, | |
1474 | }, | |
1475 | }, | |
1476 | links => [ { rel => 'child', href => "{subdir}" } ], | |
1477 | }, | |
1478 | code => sub { | |
1479 | my ($param) = @_; | |
1480 | ||
1481 | # test if VM exists | |
1482 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
1483 | ||
1484 | my $res = [ | |
1485 | { subdir => 'current' }, | |
1486 | { subdir => 'start' }, | |
1487 | { subdir => 'stop' }, | |
1488 | ]; | |
afdb31d5 | 1489 | |
5fdbe4f0 DM |
1490 | return $res; |
1491 | }}); | |
1492 | ||
88fc87b4 DM |
1493 | my $vm_is_ha_managed = sub { |
1494 | my ($vmid) = @_; | |
1495 | ||
1496 | my $cc = PVE::Cluster::cfs_read_file('cluster.conf'); | |
1497 | if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $vmid, 1)) { | |
1498 | return 1; | |
75466c4f | 1499 | } |
88fc87b4 DM |
1500 | return 0; |
1501 | }; | |
1502 | ||
1e3baf05 | 1503 | __PACKAGE__->register_method({ |
afdb31d5 | 1504 | name => 'vm_status', |
5fdbe4f0 | 1505 | path => '{vmid}/status/current', |
1e3baf05 DM |
1506 | method => 'GET', |
1507 | proxyto => 'node', | |
1508 | protected => 1, # qemu pid files are only readable by root | |
1509 | description => "Get virtual machine status.", | |
a0d1b1a2 DM |
1510 | permissions => { |
1511 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1512 | }, | |
1e3baf05 DM |
1513 | parameters => { |
1514 | additionalProperties => 0, | |
1515 | properties => { | |
1516 | node => get_standard_option('pve-node'), | |
1517 | vmid => get_standard_option('pve-vmid'), | |
1518 | }, | |
1519 | }, | |
1520 | returns => { type => 'object' }, | |
1521 | code => sub { | |
1522 | my ($param) = @_; | |
1523 | ||
1524 | # test if VM exists | |
1525 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
1526 | ||
03a33f30 | 1527 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); |
8610701a | 1528 | my $status = $vmstatus->{$param->{vmid}}; |
1e3baf05 | 1529 | |
88fc87b4 | 1530 | $status->{ha} = &$vm_is_ha_managed($param->{vmid}); |
8610701a | 1531 | |
86b8228b | 1532 | $status->{spice} = 1 if PVE::QemuServer::vga_conf_has_spice($conf->{vga}); |
46246f04 | 1533 | |
8610701a | 1534 | return $status; |
1e3baf05 DM |
1535 | }}); |
1536 | ||
1537 | __PACKAGE__->register_method({ | |
afdb31d5 | 1538 | name => 'vm_start', |
5fdbe4f0 DM |
1539 | path => '{vmid}/status/start', |
1540 | method => 'POST', | |
1e3baf05 DM |
1541 | protected => 1, |
1542 | proxyto => 'node', | |
5fdbe4f0 | 1543 | description => "Start virtual machine.", |
a0d1b1a2 DM |
1544 | permissions => { |
1545 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1546 | }, | |
1e3baf05 DM |
1547 | parameters => { |
1548 | additionalProperties => 0, | |
1549 | properties => { | |
1550 | node => get_standard_option('pve-node'), | |
1551 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 DM |
1552 | skiplock => get_standard_option('skiplock'), |
1553 | stateuri => get_standard_option('pve-qm-stateuri'), | |
7e8dcf2c | 1554 | migratedfrom => get_standard_option('pve-node',{ optional => 1 }), |
952958bc | 1555 | machine => get_standard_option('pve-qm-machine'), |
1e3baf05 DM |
1556 | }, |
1557 | }, | |
afdb31d5 | 1558 | returns => { |
5fdbe4f0 DM |
1559 | type => 'string', |
1560 | }, | |
1e3baf05 DM |
1561 | code => sub { |
1562 | my ($param) = @_; | |
1563 | ||
1564 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1565 | ||
a0d1b1a2 | 1566 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1567 | |
1568 | my $node = extract_param($param, 'node'); | |
1569 | ||
1e3baf05 DM |
1570 | my $vmid = extract_param($param, 'vmid'); |
1571 | ||
952958bc DM |
1572 | my $machine = extract_param($param, 'machine'); |
1573 | ||
3ea94c60 | 1574 | my $stateuri = extract_param($param, 'stateuri'); |
afdb31d5 | 1575 | raise_param_exc({ stateuri => "Only root may use this option." }) |
a0d1b1a2 | 1576 | if $stateuri && $authuser ne 'root@pam'; |
3ea94c60 | 1577 | |
1e3baf05 | 1578 | my $skiplock = extract_param($param, 'skiplock'); |
afdb31d5 | 1579 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1580 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 1581 | |
7e8dcf2c AD |
1582 | my $migratedfrom = extract_param($param, 'migratedfrom'); |
1583 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
1584 | if $migratedfrom && $authuser ne 'root@pam'; | |
1585 | ||
7c14dcae DM |
1586 | # read spice ticket from STDIN |
1587 | my $spice_ticket; | |
1588 | if ($stateuri && ($stateuri eq 'tcp') && $migratedfrom && ($rpcenv->{type} eq 'cli')) { | |
a64d6146 | 1589 | if (defined(my $line = <>)) { |
760fb3c8 DM |
1590 | chomp $line; |
1591 | $spice_ticket = $line; | |
1592 | } | |
7c14dcae DM |
1593 | } |
1594 | ||
afdb31d5 | 1595 | my $storecfg = PVE::Storage::config(); |
5fdbe4f0 | 1596 | |
cce37749 DM |
1597 | if (&$vm_is_ha_managed($vmid) && !$stateuri && |
1598 | $rpcenv->{type} ne 'ha') { | |
5fdbe4f0 | 1599 | |
88fc87b4 DM |
1600 | my $hacmd = sub { |
1601 | my $upid = shift; | |
5fdbe4f0 | 1602 | |
88fc87b4 | 1603 | my $service = "pvevm:$vmid"; |
5fdbe4f0 | 1604 | |
88fc87b4 DM |
1605 | my $cmd = ['clusvcadm', '-e', $service, '-m', $node]; |
1606 | ||
1607 | print "Executing HA start for VM $vmid\n"; | |
1608 | ||
1609 | PVE::Tools::run_command($cmd); | |
1610 | ||
1611 | return; | |
1612 | }; | |
1613 | ||
1614 | return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd); | |
1615 | ||
1616 | } else { | |
1617 | ||
1618 | my $realcmd = sub { | |
1619 | my $upid = shift; | |
1620 | ||
1621 | syslog('info', "start VM $vmid: $upid\n"); | |
1622 | ||
fa8ea931 | 1623 | PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom, undef, |
7c14dcae | 1624 | $machine, $spice_ticket); |
88fc87b4 DM |
1625 | |
1626 | return; | |
1627 | }; | |
5fdbe4f0 | 1628 | |
88fc87b4 DM |
1629 | return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd); |
1630 | } | |
5fdbe4f0 DM |
1631 | }}); |
1632 | ||
1633 | __PACKAGE__->register_method({ | |
afdb31d5 | 1634 | name => 'vm_stop', |
5fdbe4f0 DM |
1635 | path => '{vmid}/status/stop', |
1636 | method => 'POST', | |
1637 | protected => 1, | |
1638 | proxyto => 'node', | |
1639 | description => "Stop virtual machine.", | |
a0d1b1a2 DM |
1640 | permissions => { |
1641 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1642 | }, | |
5fdbe4f0 DM |
1643 | parameters => { |
1644 | additionalProperties => 0, | |
1645 | properties => { | |
1646 | node => get_standard_option('pve-node'), | |
1647 | vmid => get_standard_option('pve-vmid'), | |
1648 | skiplock => get_standard_option('skiplock'), | |
debe8882 | 1649 | migratedfrom => get_standard_option('pve-node', { optional => 1 }), |
c6bb9502 DM |
1650 | timeout => { |
1651 | description => "Wait maximal timeout seconds.", | |
1652 | type => 'integer', | |
1653 | minimum => 0, | |
1654 | optional => 1, | |
254575e9 DM |
1655 | }, |
1656 | keepActive => { | |
1657 | description => "Do not decativate storage volumes.", | |
1658 | type => 'boolean', | |
1659 | optional => 1, | |
1660 | default => 0, | |
c6bb9502 | 1661 | } |
5fdbe4f0 DM |
1662 | }, |
1663 | }, | |
afdb31d5 | 1664 | returns => { |
5fdbe4f0 DM |
1665 | type => 'string', |
1666 | }, | |
1667 | code => sub { | |
1668 | my ($param) = @_; | |
1669 | ||
1670 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1671 | ||
a0d1b1a2 | 1672 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1673 | |
1674 | my $node = extract_param($param, 'node'); | |
1675 | ||
1676 | my $vmid = extract_param($param, 'vmid'); | |
1677 | ||
1678 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1679 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1680 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1681 | |
254575e9 | 1682 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 1683 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 1684 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 1685 | |
af30308f DM |
1686 | my $migratedfrom = extract_param($param, 'migratedfrom'); |
1687 | raise_param_exc({ migratedfrom => "Only root may use this option." }) | |
1688 | if $migratedfrom && $authuser ne 'root@pam'; | |
1689 | ||
1690 | ||
ff1a2432 DM |
1691 | my $storecfg = PVE::Storage::config(); |
1692 | ||
debe8882 | 1693 | if (&$vm_is_ha_managed($vmid) && ($rpcenv->{type} ne 'ha') && !defined($migratedfrom)) { |
5fdbe4f0 | 1694 | |
88fc87b4 DM |
1695 | my $hacmd = sub { |
1696 | my $upid = shift; | |
5fdbe4f0 | 1697 | |
88fc87b4 | 1698 | my $service = "pvevm:$vmid"; |
c6bb9502 | 1699 | |
88fc87b4 DM |
1700 | my $cmd = ['clusvcadm', '-d', $service]; |
1701 | ||
1702 | print "Executing HA stop for VM $vmid\n"; | |
1703 | ||
1704 | PVE::Tools::run_command($cmd); | |
5fdbe4f0 | 1705 | |
88fc87b4 DM |
1706 | return; |
1707 | }; | |
1708 | ||
1709 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
1710 | ||
1711 | } else { | |
1712 | my $realcmd = sub { | |
1713 | my $upid = shift; | |
1714 | ||
1715 | syslog('info', "stop VM $vmid: $upid\n"); | |
1716 | ||
1717 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, | |
af30308f | 1718 | $param->{timeout}, 0, 1, $keepActive, $migratedfrom); |
88fc87b4 DM |
1719 | |
1720 | return; | |
1721 | }; | |
1722 | ||
1723 | return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd); | |
1724 | } | |
5fdbe4f0 DM |
1725 | }}); |
1726 | ||
1727 | __PACKAGE__->register_method({ | |
afdb31d5 | 1728 | name => 'vm_reset', |
5fdbe4f0 DM |
1729 | path => '{vmid}/status/reset', |
1730 | method => 'POST', | |
1731 | protected => 1, | |
1732 | proxyto => 'node', | |
1733 | description => "Reset virtual machine.", | |
a0d1b1a2 DM |
1734 | permissions => { |
1735 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1736 | }, | |
5fdbe4f0 DM |
1737 | parameters => { |
1738 | additionalProperties => 0, | |
1739 | properties => { | |
1740 | node => get_standard_option('pve-node'), | |
1741 | vmid => get_standard_option('pve-vmid'), | |
1742 | skiplock => get_standard_option('skiplock'), | |
1743 | }, | |
1744 | }, | |
afdb31d5 | 1745 | returns => { |
5fdbe4f0 DM |
1746 | type => 'string', |
1747 | }, | |
1748 | code => sub { | |
1749 | my ($param) = @_; | |
1750 | ||
1751 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1752 | ||
a0d1b1a2 | 1753 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1754 | |
1755 | my $node = extract_param($param, 'node'); | |
1756 | ||
1757 | my $vmid = extract_param($param, 'vmid'); | |
1758 | ||
1759 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1760 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1761 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1762 | |
ff1a2432 DM |
1763 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
1764 | ||
5fdbe4f0 DM |
1765 | my $realcmd = sub { |
1766 | my $upid = shift; | |
1767 | ||
1e3baf05 | 1768 | PVE::QemuServer::vm_reset($vmid, $skiplock); |
5fdbe4f0 DM |
1769 | |
1770 | return; | |
1771 | }; | |
1772 | ||
a0d1b1a2 | 1773 | return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1774 | }}); |
1775 | ||
1776 | __PACKAGE__->register_method({ | |
afdb31d5 | 1777 | name => 'vm_shutdown', |
5fdbe4f0 DM |
1778 | path => '{vmid}/status/shutdown', |
1779 | method => 'POST', | |
1780 | protected => 1, | |
1781 | proxyto => 'node', | |
1782 | description => "Shutdown virtual machine.", | |
a0d1b1a2 DM |
1783 | permissions => { |
1784 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1785 | }, | |
5fdbe4f0 DM |
1786 | parameters => { |
1787 | additionalProperties => 0, | |
1788 | properties => { | |
1789 | node => get_standard_option('pve-node'), | |
1790 | vmid => get_standard_option('pve-vmid'), | |
1791 | skiplock => get_standard_option('skiplock'), | |
c6bb9502 DM |
1792 | timeout => { |
1793 | description => "Wait maximal timeout seconds.", | |
1794 | type => 'integer', | |
1795 | minimum => 0, | |
1796 | optional => 1, | |
9269013a DM |
1797 | }, |
1798 | forceStop => { | |
1799 | description => "Make sure the VM stops.", | |
1800 | type => 'boolean', | |
1801 | optional => 1, | |
1802 | default => 0, | |
254575e9 DM |
1803 | }, |
1804 | keepActive => { | |
1805 | description => "Do not decativate storage volumes.", | |
1806 | type => 'boolean', | |
1807 | optional => 1, | |
1808 | default => 0, | |
c6bb9502 | 1809 | } |
5fdbe4f0 DM |
1810 | }, |
1811 | }, | |
afdb31d5 | 1812 | returns => { |
5fdbe4f0 DM |
1813 | type => 'string', |
1814 | }, | |
1815 | code => sub { | |
1816 | my ($param) = @_; | |
1817 | ||
1818 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1819 | ||
a0d1b1a2 | 1820 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1821 | |
1822 | my $node = extract_param($param, 'node'); | |
1823 | ||
1824 | my $vmid = extract_param($param, 'vmid'); | |
1825 | ||
1826 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1827 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1828 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1829 | |
254575e9 | 1830 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 1831 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 1832 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 1833 | |
02d07cf5 DM |
1834 | my $storecfg = PVE::Storage::config(); |
1835 | ||
5fdbe4f0 DM |
1836 | my $realcmd = sub { |
1837 | my $upid = shift; | |
1838 | ||
1839 | syslog('info', "shutdown VM $vmid: $upid\n"); | |
1840 | ||
afdb31d5 | 1841 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout}, |
254575e9 | 1842 | 1, $param->{forceStop}, $keepActive); |
c6bb9502 | 1843 | |
5fdbe4f0 DM |
1844 | return; |
1845 | }; | |
1846 | ||
a0d1b1a2 | 1847 | return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1848 | }}); |
1849 | ||
1850 | __PACKAGE__->register_method({ | |
afdb31d5 | 1851 | name => 'vm_suspend', |
5fdbe4f0 DM |
1852 | path => '{vmid}/status/suspend', |
1853 | method => 'POST', | |
1854 | protected => 1, | |
1855 | proxyto => 'node', | |
1856 | description => "Suspend virtual machine.", | |
a0d1b1a2 DM |
1857 | permissions => { |
1858 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1859 | }, | |
5fdbe4f0 DM |
1860 | parameters => { |
1861 | additionalProperties => 0, | |
1862 | properties => { | |
1863 | node => get_standard_option('pve-node'), | |
1864 | vmid => get_standard_option('pve-vmid'), | |
1865 | skiplock => get_standard_option('skiplock'), | |
1866 | }, | |
1867 | }, | |
afdb31d5 | 1868 | returns => { |
5fdbe4f0 DM |
1869 | type => 'string', |
1870 | }, | |
1871 | code => sub { | |
1872 | my ($param) = @_; | |
1873 | ||
1874 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1875 | ||
a0d1b1a2 | 1876 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1877 | |
1878 | my $node = extract_param($param, 'node'); | |
1879 | ||
1880 | my $vmid = extract_param($param, 'vmid'); | |
1881 | ||
1882 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1883 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1884 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1885 | |
ff1a2432 DM |
1886 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
1887 | ||
5fdbe4f0 DM |
1888 | my $realcmd = sub { |
1889 | my $upid = shift; | |
1890 | ||
1891 | syslog('info', "suspend VM $vmid: $upid\n"); | |
1892 | ||
1e3baf05 | 1893 | PVE::QemuServer::vm_suspend($vmid, $skiplock); |
5fdbe4f0 DM |
1894 | |
1895 | return; | |
1896 | }; | |
1897 | ||
a0d1b1a2 | 1898 | return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1899 | }}); |
1900 | ||
1901 | __PACKAGE__->register_method({ | |
afdb31d5 | 1902 | name => 'vm_resume', |
5fdbe4f0 DM |
1903 | path => '{vmid}/status/resume', |
1904 | method => 'POST', | |
1905 | protected => 1, | |
1906 | proxyto => 'node', | |
1907 | description => "Resume virtual machine.", | |
a0d1b1a2 DM |
1908 | permissions => { |
1909 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1910 | }, | |
5fdbe4f0 DM |
1911 | parameters => { |
1912 | additionalProperties => 0, | |
1913 | properties => { | |
1914 | node => get_standard_option('pve-node'), | |
1915 | vmid => get_standard_option('pve-vmid'), | |
1916 | skiplock => get_standard_option('skiplock'), | |
1917 | }, | |
1918 | }, | |
afdb31d5 | 1919 | returns => { |
5fdbe4f0 DM |
1920 | type => 'string', |
1921 | }, | |
1922 | code => sub { | |
1923 | my ($param) = @_; | |
1924 | ||
1925 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1926 | ||
a0d1b1a2 | 1927 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1928 | |
1929 | my $node = extract_param($param, 'node'); | |
1930 | ||
1931 | my $vmid = extract_param($param, 'vmid'); | |
1932 | ||
1933 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1934 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1935 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1936 | |
b7eeab21 | 1937 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
ff1a2432 | 1938 | |
5fdbe4f0 DM |
1939 | my $realcmd = sub { |
1940 | my $upid = shift; | |
1941 | ||
1942 | syslog('info', "resume VM $vmid: $upid\n"); | |
1943 | ||
1e3baf05 | 1944 | PVE::QemuServer::vm_resume($vmid, $skiplock); |
1e3baf05 | 1945 | |
5fdbe4f0 DM |
1946 | return; |
1947 | }; | |
1948 | ||
a0d1b1a2 | 1949 | return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1950 | }}); |
1951 | ||
1952 | __PACKAGE__->register_method({ | |
afdb31d5 | 1953 | name => 'vm_sendkey', |
5fdbe4f0 DM |
1954 | path => '{vmid}/sendkey', |
1955 | method => 'PUT', | |
1956 | protected => 1, | |
1957 | proxyto => 'node', | |
1958 | description => "Send key event to virtual machine.", | |
a0d1b1a2 DM |
1959 | permissions => { |
1960 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1961 | }, | |
5fdbe4f0 DM |
1962 | parameters => { |
1963 | additionalProperties => 0, | |
1964 | properties => { | |
1965 | node => get_standard_option('pve-node'), | |
1966 | vmid => get_standard_option('pve-vmid'), | |
1967 | skiplock => get_standard_option('skiplock'), | |
1968 | key => { | |
1969 | description => "The key (qemu monitor encoding).", | |
1970 | type => 'string' | |
1971 | } | |
1972 | }, | |
1973 | }, | |
1974 | returns => { type => 'null'}, | |
1975 | code => sub { | |
1976 | my ($param) = @_; | |
1977 | ||
1978 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1979 | ||
a0d1b1a2 | 1980 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1981 | |
1982 | my $node = extract_param($param, 'node'); | |
1983 | ||
1984 | my $vmid = extract_param($param, 'vmid'); | |
1985 | ||
1986 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1987 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1988 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 DM |
1989 | |
1990 | PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); | |
1991 | ||
1992 | return; | |
1e3baf05 DM |
1993 | }}); |
1994 | ||
1ac0d2ee AD |
1995 | __PACKAGE__->register_method({ |
1996 | name => 'vm_feature', | |
1997 | path => '{vmid}/feature', | |
1998 | method => 'GET', | |
1999 | proxyto => 'node', | |
75466c4f | 2000 | protected => 1, |
1ac0d2ee AD |
2001 | description => "Check if feature for virtual machine is available.", |
2002 | permissions => { | |
2003 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
2004 | }, | |
2005 | parameters => { | |
2006 | additionalProperties => 0, | |
2007 | properties => { | |
2008 | node => get_standard_option('pve-node'), | |
2009 | vmid => get_standard_option('pve-vmid'), | |
2010 | feature => { | |
2011 | description => "Feature to check.", | |
2012 | type => 'string', | |
7758ce86 | 2013 | enum => [ 'snapshot', 'clone', 'copy' ], |
1ac0d2ee AD |
2014 | }, |
2015 | snapname => get_standard_option('pve-snapshot-name', { | |
2016 | optional => 1, | |
2017 | }), | |
2018 | }, | |
1ac0d2ee AD |
2019 | }, |
2020 | returns => { | |
719893a9 DM |
2021 | type => "object", |
2022 | properties => { | |
2023 | hasFeature => { type => 'boolean' }, | |
7043d946 | 2024 | nodes => { |
719893a9 DM |
2025 | type => 'array', |
2026 | items => { type => 'string' }, | |
2027 | } | |
2028 | }, | |
1ac0d2ee AD |
2029 | }, |
2030 | code => sub { | |
2031 | my ($param) = @_; | |
2032 | ||
2033 | my $node = extract_param($param, 'node'); | |
2034 | ||
2035 | my $vmid = extract_param($param, 'vmid'); | |
2036 | ||
2037 | my $snapname = extract_param($param, 'snapname'); | |
2038 | ||
2039 | my $feature = extract_param($param, 'feature'); | |
2040 | ||
2041 | my $running = PVE::QemuServer::check_running($vmid); | |
2042 | ||
2043 | my $conf = PVE::QemuServer::load_config($vmid); | |
2044 | ||
2045 | if($snapname){ | |
2046 | my $snap = $conf->{snapshots}->{$snapname}; | |
2047 | die "snapshot '$snapname' does not exist\n" if !defined($snap); | |
2048 | $conf = $snap; | |
2049 | } | |
2050 | my $storecfg = PVE::Storage::config(); | |
2051 | ||
719893a9 DM |
2052 | my $nodelist = PVE::QemuServer::shared_nodes($conf, $storecfg); |
2053 | my $hasFeature = PVE::QemuServer::has_feature($feature, $conf, $storecfg, $snapname, $running); | |
7043d946 | 2054 | |
719893a9 DM |
2055 | return { |
2056 | hasFeature => $hasFeature, | |
2057 | nodes => [ keys %$nodelist ], | |
7043d946 | 2058 | }; |
1ac0d2ee AD |
2059 | }}); |
2060 | ||
6116f729 | 2061 | __PACKAGE__->register_method({ |
9418baad DM |
2062 | name => 'clone_vm', |
2063 | path => '{vmid}/clone', | |
6116f729 DM |
2064 | method => 'POST', |
2065 | protected => 1, | |
2066 | proxyto => 'node', | |
37329185 | 2067 | description => "Create a copy of virtual machine/template.", |
6116f729 | 2068 | permissions => { |
9418baad | 2069 | description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " . |
6116f729 DM |
2070 | "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " . |
2071 | "'Datastore.AllocateSpace' on any used storage.", | |
75466c4f DM |
2072 | check => |
2073 | [ 'and', | |
9418baad | 2074 | ['perm', '/vms/{vmid}', [ 'VM.Clone' ]], |
75466c4f | 2075 | [ 'or', |
6116f729 DM |
2076 | [ 'perm', '/vms/{newid}', ['VM.Allocate']], |
2077 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
2078 | ], | |
2079 | ] | |
2080 | }, | |
2081 | parameters => { | |
2082 | additionalProperties => 0, | |
2083 | properties => { | |
6116f729 DM |
2084 | node => get_standard_option('pve-node'), |
2085 | vmid => get_standard_option('pve-vmid'), | |
9418baad | 2086 | newid => get_standard_option('pve-vmid', { description => 'VMID for the clone.' }), |
a60ab1a6 DM |
2087 | name => { |
2088 | optional => 1, | |
2089 | type => 'string', format => 'dns-name', | |
2090 | description => "Set a name for the new VM.", | |
2091 | }, | |
2092 | description => { | |
2093 | optional => 1, | |
2094 | type => 'string', | |
2095 | description => "Description for the new VM.", | |
2096 | }, | |
75466c4f | 2097 | pool => { |
6116f729 DM |
2098 | optional => 1, |
2099 | type => 'string', format => 'pve-poolid', | |
2100 | description => "Add the new VM to the specified pool.", | |
2101 | }, | |
9076d880 | 2102 | snapname => get_standard_option('pve-snapshot-name', { |
9076d880 DM |
2103 | optional => 1, |
2104 | }), | |
81f043eb | 2105 | storage => get_standard_option('pve-storage-id', { |
9418baad | 2106 | description => "Target storage for full clone.", |
4e4f83fe | 2107 | requires => 'full', |
81f043eb AD |
2108 | optional => 1, |
2109 | }), | |
55173c6b | 2110 | 'format' => { |
42a19c87 AD |
2111 | description => "Target format for file storage.", |
2112 | requires => 'full', | |
2113 | type => 'string', | |
2114 | optional => 1, | |
55173c6b | 2115 | enum => [ 'raw', 'qcow2', 'vmdk'], |
42a19c87 | 2116 | }, |
6116f729 DM |
2117 | full => { |
2118 | optional => 1, | |
55173c6b DM |
2119 | type => 'boolean', |
2120 | description => "Create a full copy of all disk. This is always done when " . | |
9418baad | 2121 | "you clone a normal VM. For VM templates, we try to create a linked clone by default.", |
6116f729 DM |
2122 | default => 0, |
2123 | }, | |
75466c4f | 2124 | target => get_standard_option('pve-node', { |
55173c6b DM |
2125 | description => "Target node. Only allowed if the original VM is on shared storage.", |
2126 | optional => 1, | |
2127 | }), | |
2128 | }, | |
6116f729 DM |
2129 | }, |
2130 | returns => { | |
2131 | type => 'string', | |
2132 | }, | |
2133 | code => sub { | |
2134 | my ($param) = @_; | |
2135 | ||
2136 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2137 | ||
55173c6b | 2138 | my $authuser = $rpcenv->get_user(); |
6116f729 DM |
2139 | |
2140 | my $node = extract_param($param, 'node'); | |
2141 | ||
2142 | my $vmid = extract_param($param, 'vmid'); | |
2143 | ||
2144 | my $newid = extract_param($param, 'newid'); | |
2145 | ||
6116f729 DM |
2146 | my $pool = extract_param($param, 'pool'); |
2147 | ||
2148 | if (defined($pool)) { | |
2149 | $rpcenv->check_pool_exist($pool); | |
2150 | } | |
2151 | ||
55173c6b | 2152 | my $snapname = extract_param($param, 'snapname'); |
9076d880 | 2153 | |
81f043eb AD |
2154 | my $storage = extract_param($param, 'storage'); |
2155 | ||
42a19c87 AD |
2156 | my $format = extract_param($param, 'format'); |
2157 | ||
55173c6b DM |
2158 | my $target = extract_param($param, 'target'); |
2159 | ||
2160 | my $localnode = PVE::INotify::nodename(); | |
2161 | ||
751cc556 | 2162 | undef $target if $target && ($target eq $localnode || $target eq 'localhost'); |
55173c6b DM |
2163 | |
2164 | PVE::Cluster::check_node_exists($target) if $target; | |
2165 | ||
6116f729 DM |
2166 | my $storecfg = PVE::Storage::config(); |
2167 | ||
4a5a2590 DM |
2168 | if ($storage) { |
2169 | # check if storage is enabled on local node | |
2170 | PVE::Storage::storage_check_enabled($storecfg, $storage); | |
2171 | if ($target) { | |
2172 | # check if storage is available on target node | |
2173 | PVE::Storage::storage_check_node($storecfg, $storage, $target); | |
2174 | # clone only works if target storage is shared | |
2175 | my $scfg = PVE::Storage::storage_config($storecfg, $storage); | |
2176 | die "can't clone to non-shared storage '$storage'\n" if !$scfg->{shared}; | |
2177 | } | |
2178 | } | |
2179 | ||
55173c6b | 2180 | PVE::Cluster::check_cfs_quorum(); |
6116f729 | 2181 | |
4e4f83fe DM |
2182 | my $running = PVE::QemuServer::check_running($vmid) || 0; |
2183 | ||
4e4f83fe DM |
2184 | # exclusive lock if VM is running - else shared lock is enough; |
2185 | my $shared_lock = $running ? 0 : 1; | |
2186 | ||
9418baad | 2187 | my $clonefn = sub { |
6116f729 | 2188 | |
829967a9 DM |
2189 | # do all tests after lock |
2190 | # we also try to do all tests before we fork the worker | |
2191 | ||
6116f729 DM |
2192 | my $conf = PVE::QemuServer::load_config($vmid); |
2193 | ||
2194 | PVE::QemuServer::check_lock($conf); | |
2195 | ||
4e4f83fe | 2196 | my $verify_running = PVE::QemuServer::check_running($vmid) || 0; |
6116f729 | 2197 | |
4e4f83fe | 2198 | die "unexpected state change\n" if $verify_running != $running; |
6116f729 | 2199 | |
75466c4f DM |
2200 | die "snapshot '$snapname' does not exist\n" |
2201 | if $snapname && !defined( $conf->{snapshots}->{$snapname}); | |
6116f729 | 2202 | |
75466c4f | 2203 | my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf; |
9076d880 | 2204 | |
9418baad | 2205 | my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage); |
6116f729 | 2206 | |
9418baad | 2207 | die "can't clone VM to node '$target' (VM uses local storage)\n" if $target && !$sharedvm; |
75466c4f | 2208 | |
6116f729 DM |
2209 | my $conffile = PVE::QemuServer::config_file($newid); |
2210 | ||
2211 | die "unable to create VM $newid: config file already exists\n" | |
2212 | if -f $conffile; | |
2213 | ||
9418baad | 2214 | my $newconf = { lock => 'clone' }; |
829967a9 DM |
2215 | my $drives = {}; |
2216 | my $vollist = []; | |
2217 | ||
2218 | foreach my $opt (keys %$oldconf) { | |
2219 | my $value = $oldconf->{$opt}; | |
2220 | ||
2221 | # do not copy snapshot related info | |
2222 | next if $opt eq 'snapshots' || $opt eq 'parent' || $opt eq 'snaptime' || | |
2223 | $opt eq 'vmstate' || $opt eq 'snapstate'; | |
2224 | ||
2225 | # always change MAC! address | |
2226 | if ($opt =~ m/^net(\d+)$/) { | |
2227 | my $net = PVE::QemuServer::parse_net($value); | |
2228 | $net->{macaddr} = PVE::Tools::random_ether_addr(); | |
2229 | $newconf->{$opt} = PVE::QemuServer::print_net($net); | |
1f1412d1 DM |
2230 | } elsif (PVE::QemuServer::valid_drivename($opt)) { |
2231 | my $drive = PVE::QemuServer::parse_drive($opt, $value); | |
2232 | die "unable to parse drive options for '$opt'\n" if !$drive; | |
829967a9 DM |
2233 | if (PVE::QemuServer::drive_is_cdrom($drive)) { |
2234 | $newconf->{$opt} = $value; # simply copy configuration | |
2235 | } else { | |
64ff6fe4 | 2236 | if ($param->{full}) { |
2dd53043 | 2237 | die "Full clone feature is not available" |
dba198b0 | 2238 | if !PVE::Storage::volume_has_feature($storecfg, 'copy', $drive->{file}, $snapname, $running); |
2dd53043 | 2239 | $drive->{full} = 1; |
64ff6fe4 SP |
2240 | } else { |
2241 | # not full means clone instead of copy | |
2242 | die "Linked clone feature is not available" | |
2243 | if !PVE::Storage::volume_has_feature($storecfg, 'clone', $drive->{file}, $snapname, $running); | |
dba198b0 | 2244 | } |
829967a9 DM |
2245 | $drives->{$opt} = $drive; |
2246 | push @$vollist, $drive->{file}; | |
2247 | } | |
2248 | } else { | |
2249 | # copy everything else | |
2250 | $newconf->{$opt} = $value; | |
2251 | } | |
2252 | } | |
2253 | ||
cd11416f DM |
2254 | # auto generate a new uuid |
2255 | my ($uuid, $uuid_str); | |
2256 | UUID::generate($uuid); | |
2257 | UUID::unparse($uuid, $uuid_str); | |
2258 | my $smbios1 = PVE::QemuServer::parse_smbios1($newconf->{smbios1} || ''); | |
f34ebd52 | 2259 | $smbios1->{uuid} = $uuid_str; |
cd11416f DM |
2260 | $newconf->{smbios1} = PVE::QemuServer::print_smbios1($smbios1); |
2261 | ||
829967a9 DM |
2262 | delete $newconf->{template}; |
2263 | ||
2264 | if ($param->{name}) { | |
2265 | $newconf->{name} = $param->{name}; | |
2266 | } else { | |
c55fee03 DM |
2267 | if ($oldconf->{name}) { |
2268 | $newconf->{name} = "Copy-of-$oldconf->{name}"; | |
2269 | } else { | |
2270 | $newconf->{name} = "Copy-of-VM-$vmid"; | |
2271 | } | |
829967a9 | 2272 | } |
2dd53043 | 2273 | |
829967a9 DM |
2274 | if ($param->{description}) { |
2275 | $newconf->{description} = $param->{description}; | |
2276 | } | |
2277 | ||
6116f729 | 2278 | # create empty/temp config - this fails if VM already exists on other node |
9418baad | 2279 | PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n"); |
6116f729 DM |
2280 | |
2281 | my $realcmd = sub { | |
2282 | my $upid = shift; | |
2283 | ||
b83e0181 | 2284 | my $newvollist = []; |
6116f729 | 2285 | |
b83e0181 | 2286 | eval { |
829967a9 | 2287 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; |
75466c4f | 2288 | |
6116f729 DM |
2289 | PVE::Storage::activate_volumes($storecfg, $vollist); |
2290 | ||
829967a9 DM |
2291 | foreach my $opt (keys %$drives) { |
2292 | my $drive = $drives->{$opt}; | |
2dd53043 | 2293 | |
152fe752 DM |
2294 | my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $opt, $drive, $snapname, |
2295 | $newid, $storage, $format, $drive->{full}, $newvollist); | |
00b095ca | 2296 | |
152fe752 | 2297 | $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive); |
2dd53043 | 2298 | |
829967a9 DM |
2299 | PVE::QemuServer::update_config_nolock($newid, $newconf, 1); |
2300 | } | |
b83e0181 DM |
2301 | |
2302 | delete $newconf->{lock}; | |
2303 | PVE::QemuServer::update_config_nolock($newid, $newconf, 1); | |
55173c6b DM |
2304 | |
2305 | if ($target) { | |
baca276d DM |
2306 | # always deactivate volumes - avoid lvm LVs to be active on several nodes |
2307 | PVE::Storage::deactivate_volumes($storecfg, $vollist); | |
2308 | ||
55173c6b DM |
2309 | my $newconffile = PVE::QemuServer::config_file($newid, $target); |
2310 | die "Failed to move config to node '$target' - rename failed: $!\n" | |
2311 | if !rename($conffile, $newconffile); | |
2312 | } | |
d703d4c0 | 2313 | |
be517049 | 2314 | PVE::AccessControl::add_vm_to_pool($newid, $pool) if $pool; |
6116f729 | 2315 | }; |
75466c4f | 2316 | if (my $err = $@) { |
6116f729 DM |
2317 | unlink $conffile; |
2318 | ||
b83e0181 DM |
2319 | sleep 1; # some storage like rbd need to wait before release volume - really? |
2320 | ||
2321 | foreach my $volid (@$newvollist) { | |
2322 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
2323 | warn $@ if $@; | |
2324 | } | |
9418baad | 2325 | die "clone failed: $err"; |
6116f729 DM |
2326 | } |
2327 | ||
2328 | return; | |
2329 | }; | |
2330 | ||
9418baad | 2331 | return $rpcenv->fork_worker('qmclone', $vmid, $authuser, $realcmd); |
6116f729 DM |
2332 | }; |
2333 | ||
4e4f83fe | 2334 | return PVE::QemuServer::lock_config_mode($vmid, 1, $shared_lock, sub { |
6116f729 | 2335 | # Aquire exclusive lock lock for $newid |
9418baad | 2336 | return PVE::QemuServer::lock_config_full($newid, 1, $clonefn); |
6116f729 DM |
2337 | }); |
2338 | ||
2339 | }}); | |
2340 | ||
586bfa78 | 2341 | __PACKAGE__->register_method({ |
43bc02a9 DM |
2342 | name => 'move_vm_disk', |
2343 | path => '{vmid}/move_disk', | |
e2cd75fa | 2344 | method => 'POST', |
586bfa78 AD |
2345 | protected => 1, |
2346 | proxyto => 'node', | |
2347 | description => "Move volume to different storage.", | |
2348 | permissions => { | |
e2cd75fa DM |
2349 | description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, " . |
2350 | "and 'Datastore.AllocateSpace' permissions on the storage.", | |
7043d946 | 2351 | check => |
e2cd75fa DM |
2352 | [ 'and', |
2353 | ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], | |
2354 | ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]], | |
2355 | ], | |
586bfa78 AD |
2356 | }, |
2357 | parameters => { | |
2358 | additionalProperties => 0, | |
2359 | properties => { | |
2360 | node => get_standard_option('pve-node'), | |
2361 | vmid => get_standard_option('pve-vmid'), | |
586bfa78 AD |
2362 | disk => { |
2363 | type => 'string', | |
2364 | description => "The disk you want to move.", | |
e2cd75fa | 2365 | enum => [ PVE::QemuServer::disknames() ], |
586bfa78 | 2366 | }, |
e2cd75fa | 2367 | storage => get_standard_option('pve-storage-id', { description => "Target Storage." }), |
635c3c44 | 2368 | 'format' => { |
586bfa78 AD |
2369 | type => 'string', |
2370 | description => "Target Format.", | |
2371 | enum => [ 'raw', 'qcow2', 'vmdk' ], | |
2372 | optional => 1, | |
2373 | }, | |
70d45e33 DM |
2374 | delete => { |
2375 | type => 'boolean', | |
2376 | description => "Delete the original disk after successful copy. By default the original disk is kept as unused disk.", | |
2377 | optional => 1, | |
2378 | default => 0, | |
2379 | }, | |
586bfa78 AD |
2380 | digest => { |
2381 | type => 'string', | |
2382 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
2383 | maxLength => 40, | |
2384 | optional => 1, | |
2385 | }, | |
2386 | }, | |
2387 | }, | |
e2cd75fa DM |
2388 | returns => { |
2389 | type => 'string', | |
2390 | description => "the task ID.", | |
2391 | }, | |
586bfa78 AD |
2392 | code => sub { |
2393 | my ($param) = @_; | |
2394 | ||
2395 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2396 | ||
2397 | my $authuser = $rpcenv->get_user(); | |
2398 | ||
2399 | my $node = extract_param($param, 'node'); | |
2400 | ||
2401 | my $vmid = extract_param($param, 'vmid'); | |
2402 | ||
2403 | my $digest = extract_param($param, 'digest'); | |
2404 | ||
2405 | my $disk = extract_param($param, 'disk'); | |
2406 | ||
2407 | my $storeid = extract_param($param, 'storage'); | |
2408 | ||
2409 | my $format = extract_param($param, 'format'); | |
2410 | ||
586bfa78 AD |
2411 | my $storecfg = PVE::Storage::config(); |
2412 | ||
2413 | my $updatefn = sub { | |
2414 | ||
2415 | my $conf = PVE::QemuServer::load_config($vmid); | |
2416 | ||
2417 | die "checksum missmatch (file change by other user?)\n" | |
2418 | if $digest && $digest ne $conf->{digest}; | |
586bfa78 AD |
2419 | |
2420 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; | |
2421 | ||
2422 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
2423 | ||
70d45e33 | 2424 | my $old_volid = $drive->{file} || die "disk '$disk' has no associated volume\n"; |
586bfa78 AD |
2425 | |
2426 | die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
2427 | ||
e2cd75fa | 2428 | my $oldfmt; |
70d45e33 | 2429 | my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($old_volid); |
586bfa78 AD |
2430 | if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){ |
2431 | $oldfmt = $1; | |
2432 | } | |
2433 | ||
7043d946 | 2434 | die "you can't move on the same storage with same format\n" if $oldstoreid eq $storeid && |
e2cd75fa | 2435 | (!$format || !$oldfmt || $oldfmt eq $format); |
586bfa78 AD |
2436 | |
2437 | PVE::Cluster::log_msg('info', $authuser, "move disk VM $vmid: move --disk $disk --storage $storeid"); | |
2438 | ||
2439 | my $running = PVE::QemuServer::check_running($vmid); | |
e2cd75fa DM |
2440 | |
2441 | PVE::Storage::activate_volumes($storecfg, [ $drive->{file} ]); | |
2442 | ||
586bfa78 AD |
2443 | my $realcmd = sub { |
2444 | ||
2445 | my $newvollist = []; | |
2446 | ||
2447 | eval { | |
2448 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; }; | |
2449 | ||
e2cd75fa DM |
2450 | my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $disk, $drive, undef, |
2451 | $vmid, $storeid, $format, 1, $newvollist); | |
2452 | ||
2453 | $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $newdrive); | |
2454 | ||
70d45e33 | 2455 | PVE::QemuServer::add_unused_volume($conf, $old_volid) if !$param->{delete}; |
7043d946 | 2456 | |
e2cd75fa | 2457 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); |
73272365 | 2458 | |
f34ebd52 | 2459 | eval { |
73272365 | 2460 | # try to deactivate volumes - avoid lvm LVs to be active on several nodes |
f34ebd52 | 2461 | PVE::Storage::deactivate_volumes($storecfg, [ $newdrive->{file} ]) |
73272365 DM |
2462 | if !$running; |
2463 | }; | |
2464 | warn $@ if $@; | |
586bfa78 AD |
2465 | }; |
2466 | if (my $err = $@) { | |
2467 | ||
2468 | foreach my $volid (@$newvollist) { | |
2469 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
2470 | warn $@ if $@; | |
2471 | } | |
2472 | die "storage migration failed: $err"; | |
2473 | } | |
70d45e33 DM |
2474 | |
2475 | if ($param->{delete}) { | |
5b0bd20d AD |
2476 | my $used_paths = PVE::QemuServer::get_used_paths($vmid, $storecfg, $conf, 1, 1); |
2477 | my $path = PVE::Storage::path($storecfg, $old_volid); | |
2478 | if ($used_paths->{$path}){ | |
19d13324 | 2479 | warn "volume $old_volid have snapshots. Can't delete it\n"; |
5b0bd20d AD |
2480 | PVE::QemuServer::add_unused_volume($conf, $old_volid); |
2481 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
2482 | } else { | |
2483 | eval { PVE::Storage::vdisk_free($storecfg, $old_volid); }; | |
2484 | warn $@ if $@; | |
2485 | } | |
70d45e33 | 2486 | } |
586bfa78 AD |
2487 | }; |
2488 | ||
2489 | return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd); | |
2490 | }; | |
e2cd75fa DM |
2491 | |
2492 | return PVE::QemuServer::lock_config($vmid, $updatefn); | |
586bfa78 AD |
2493 | }}); |
2494 | ||
3ea94c60 | 2495 | __PACKAGE__->register_method({ |
afdb31d5 | 2496 | name => 'migrate_vm', |
3ea94c60 DM |
2497 | path => '{vmid}/migrate', |
2498 | method => 'POST', | |
2499 | protected => 1, | |
2500 | proxyto => 'node', | |
2501 | description => "Migrate virtual machine. Creates a new migration task.", | |
a0d1b1a2 DM |
2502 | permissions => { |
2503 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], | |
2504 | }, | |
3ea94c60 DM |
2505 | parameters => { |
2506 | additionalProperties => 0, | |
2507 | properties => { | |
2508 | node => get_standard_option('pve-node'), | |
2509 | vmid => get_standard_option('pve-vmid'), | |
2510 | target => get_standard_option('pve-node', { description => "Target node." }), | |
2511 | online => { | |
2512 | type => 'boolean', | |
2513 | description => "Use online/live migration.", | |
2514 | optional => 1, | |
2515 | }, | |
2516 | force => { | |
2517 | type => 'boolean', | |
2518 | description => "Allow to migrate VMs which use local devices. Only root may use this option.", | |
2519 | optional => 1, | |
2520 | }, | |
2521 | }, | |
2522 | }, | |
afdb31d5 | 2523 | returns => { |
3ea94c60 DM |
2524 | type => 'string', |
2525 | description => "the task ID.", | |
2526 | }, | |
2527 | code => sub { | |
2528 | my ($param) = @_; | |
2529 | ||
2530 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2531 | ||
a0d1b1a2 | 2532 | my $authuser = $rpcenv->get_user(); |
3ea94c60 DM |
2533 | |
2534 | my $target = extract_param($param, 'target'); | |
2535 | ||
2536 | my $localnode = PVE::INotify::nodename(); | |
2537 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
2538 | ||
2539 | PVE::Cluster::check_cfs_quorum(); | |
2540 | ||
2541 | PVE::Cluster::check_node_exists($target); | |
2542 | ||
2543 | my $targetip = PVE::Cluster::remote_node_ip($target); | |
2544 | ||
2545 | my $vmid = extract_param($param, 'vmid'); | |
2546 | ||
afdb31d5 | 2547 | raise_param_exc({ force => "Only root may use this option." }) |
a0d1b1a2 | 2548 | if $param->{force} && $authuser ne 'root@pam'; |
3ea94c60 DM |
2549 | |
2550 | # test if VM exists | |
a5ed42d3 | 2551 | my $conf = PVE::QemuServer::load_config($vmid); |
3ea94c60 DM |
2552 | |
2553 | # try to detect errors early | |
a5ed42d3 DM |
2554 | |
2555 | PVE::QemuServer::check_lock($conf); | |
2556 | ||
3ea94c60 | 2557 | if (PVE::QemuServer::check_running($vmid)) { |
afdb31d5 | 2558 | die "cant migrate running VM without --online\n" |
3ea94c60 DM |
2559 | if !$param->{online}; |
2560 | } | |
2561 | ||
47152e2e | 2562 | my $storecfg = PVE::Storage::config(); |
22d646a7 | 2563 | PVE::QemuServer::check_storage_availability($storecfg, $conf, $target); |
47152e2e | 2564 | |
3be30d63 | 2565 | if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { |
3ea94c60 | 2566 | |
88fc87b4 DM |
2567 | my $hacmd = sub { |
2568 | my $upid = shift; | |
3ea94c60 | 2569 | |
88fc87b4 DM |
2570 | my $service = "pvevm:$vmid"; |
2571 | ||
2572 | my $cmd = ['clusvcadm', '-M', $service, '-m', $target]; | |
2573 | ||
2574 | print "Executing HA migrate for VM $vmid to node $target\n"; | |
2575 | ||
2576 | PVE::Tools::run_command($cmd); | |
2577 | ||
2578 | return; | |
2579 | }; | |
2580 | ||
2581 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); | |
2582 | ||
2583 | } else { | |
2584 | ||
2585 | my $realcmd = sub { | |
2586 | my $upid = shift; | |
2587 | ||
2588 | PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param); | |
2589 | }; | |
2590 | ||
2591 | return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd); | |
2592 | } | |
3ea94c60 | 2593 | |
3ea94c60 | 2594 | }}); |
1e3baf05 | 2595 | |
91c94f0a | 2596 | __PACKAGE__->register_method({ |
afdb31d5 DM |
2597 | name => 'monitor', |
2598 | path => '{vmid}/monitor', | |
91c94f0a DM |
2599 | method => 'POST', |
2600 | protected => 1, | |
2601 | proxyto => 'node', | |
2602 | description => "Execute Qemu monitor commands.", | |
a0d1b1a2 DM |
2603 | permissions => { |
2604 | check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]], | |
2605 | }, | |
91c94f0a DM |
2606 | parameters => { |
2607 | additionalProperties => 0, | |
2608 | properties => { | |
2609 | node => get_standard_option('pve-node'), | |
2610 | vmid => get_standard_option('pve-vmid'), | |
2611 | command => { | |
2612 | type => 'string', | |
2613 | description => "The monitor command.", | |
2614 | } | |
2615 | }, | |
2616 | }, | |
2617 | returns => { type => 'string'}, | |
2618 | code => sub { | |
2619 | my ($param) = @_; | |
2620 | ||
2621 | my $vmid = $param->{vmid}; | |
2622 | ||
2623 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
2624 | ||
2625 | my $res = ''; | |
2626 | eval { | |
7b7c6d1b | 2627 | $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command}); |
91c94f0a DM |
2628 | }; |
2629 | $res = "ERROR: $@" if $@; | |
2630 | ||
2631 | return $res; | |
2632 | }}); | |
2633 | ||
0d02881c AD |
2634 | __PACKAGE__->register_method({ |
2635 | name => 'resize_vm', | |
614e3941 | 2636 | path => '{vmid}/resize', |
0d02881c AD |
2637 | method => 'PUT', |
2638 | protected => 1, | |
2639 | proxyto => 'node', | |
2f48a4f5 | 2640 | description => "Extend volume size.", |
0d02881c | 2641 | permissions => { |
3b2773f6 | 2642 | check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], |
0d02881c AD |
2643 | }, |
2644 | parameters => { | |
2645 | additionalProperties => 0, | |
2f48a4f5 DM |
2646 | properties => { |
2647 | node => get_standard_option('pve-node'), | |
2648 | vmid => get_standard_option('pve-vmid'), | |
2649 | skiplock => get_standard_option('skiplock'), | |
2650 | disk => { | |
2651 | type => 'string', | |
2652 | description => "The disk you want to resize.", | |
2653 | enum => [PVE::QemuServer::disknames()], | |
2654 | }, | |
2655 | size => { | |
2656 | type => 'string', | |
f91b2e45 | 2657 | pattern => '\+?\d+(\.\d+)?[KMGT]?', |
2f48a4f5 DM |
2658 | 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.", |
2659 | }, | |
2660 | digest => { | |
2661 | type => 'string', | |
2662 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
2663 | maxLength => 40, | |
2664 | optional => 1, | |
2665 | }, | |
2666 | }, | |
0d02881c AD |
2667 | }, |
2668 | returns => { type => 'null'}, | |
2669 | code => sub { | |
2670 | my ($param) = @_; | |
2671 | ||
2672 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2673 | ||
2674 | my $authuser = $rpcenv->get_user(); | |
2675 | ||
2676 | my $node = extract_param($param, 'node'); | |
2677 | ||
2678 | my $vmid = extract_param($param, 'vmid'); | |
2679 | ||
2680 | my $digest = extract_param($param, 'digest'); | |
2681 | ||
2f48a4f5 | 2682 | my $disk = extract_param($param, 'disk'); |
75466c4f | 2683 | |
2f48a4f5 | 2684 | my $sizestr = extract_param($param, 'size'); |
0d02881c | 2685 | |
f91b2e45 | 2686 | my $skiplock = extract_param($param, 'skiplock'); |
0d02881c AD |
2687 | raise_param_exc({ skiplock => "Only root may use this option." }) |
2688 | if $skiplock && $authuser ne 'root@pam'; | |
2689 | ||
0d02881c AD |
2690 | my $storecfg = PVE::Storage::config(); |
2691 | ||
0d02881c AD |
2692 | my $updatefn = sub { |
2693 | ||
2694 | my $conf = PVE::QemuServer::load_config($vmid); | |
2695 | ||
2696 | die "checksum missmatch (file change by other user?)\n" | |
2697 | if $digest && $digest ne $conf->{digest}; | |
2698 | PVE::QemuServer::check_lock($conf) if !$skiplock; | |
2699 | ||
f91b2e45 DM |
2700 | die "disk '$disk' does not exist\n" if !$conf->{$disk}; |
2701 | ||
2702 | my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk}); | |
2703 | ||
2704 | my $volid = $drive->{file}; | |
2705 | ||
2706 | die "disk '$disk' has no associated volume\n" if !$volid; | |
2707 | ||
2708 | die "you can't resize a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive); | |
2709 | ||
2710 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid); | |
2711 | ||
2712 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
2713 | ||
2714 | my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5); | |
2715 | ||
2716 | die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/; | |
2717 | my ($ext, $newsize, $unit) = ($1, $2, $4); | |
2718 | if ($unit) { | |
2719 | if ($unit eq 'K') { | |
2720 | $newsize = $newsize * 1024; | |
2721 | } elsif ($unit eq 'M') { | |
2722 | $newsize = $newsize * 1024 * 1024; | |
2723 | } elsif ($unit eq 'G') { | |
2724 | $newsize = $newsize * 1024 * 1024 * 1024; | |
2725 | } elsif ($unit eq 'T') { | |
2726 | $newsize = $newsize * 1024 * 1024 * 1024 * 1024; | |
2727 | } | |
2728 | } | |
2729 | $newsize += $size if $ext; | |
2730 | $newsize = int($newsize); | |
2731 | ||
2732 | die "unable to skrink disk size\n" if $newsize < $size; | |
2733 | ||
2734 | return if $size == $newsize; | |
2735 | ||
2f48a4f5 | 2736 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: resize --disk $disk --size $sizestr"); |
0d02881c | 2737 | |
f91b2e45 | 2738 | PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize); |
75466c4f | 2739 | |
f91b2e45 DM |
2740 | $drive->{size} = $newsize; |
2741 | $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive); | |
2742 | ||
2743 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
2744 | }; | |
0d02881c AD |
2745 | |
2746 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
2747 | return undef; | |
2748 | }}); | |
2749 | ||
9dbd1ee4 | 2750 | __PACKAGE__->register_method({ |
7e7d7b61 | 2751 | name => 'snapshot_list', |
9dbd1ee4 | 2752 | path => '{vmid}/snapshot', |
7e7d7b61 DM |
2753 | method => 'GET', |
2754 | description => "List all snapshots.", | |
2755 | permissions => { | |
2756 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
2757 | }, | |
2758 | proxyto => 'node', | |
2759 | protected => 1, # qemu pid files are only readable by root | |
2760 | parameters => { | |
2761 | additionalProperties => 0, | |
2762 | properties => { | |
2763 | vmid => get_standard_option('pve-vmid'), | |
2764 | node => get_standard_option('pve-node'), | |
2765 | }, | |
2766 | }, | |
2767 | returns => { | |
2768 | type => 'array', | |
2769 | items => { | |
2770 | type => "object", | |
2771 | properties => {}, | |
2772 | }, | |
2773 | links => [ { rel => 'child', href => "{name}" } ], | |
2774 | }, | |
2775 | code => sub { | |
2776 | my ($param) = @_; | |
2777 | ||
6aa4651b DM |
2778 | my $vmid = $param->{vmid}; |
2779 | ||
2780 | my $conf = PVE::QemuServer::load_config($vmid); | |
7e7d7b61 DM |
2781 | my $snaphash = $conf->{snapshots} || {}; |
2782 | ||
2783 | my $res = []; | |
2784 | ||
2785 | foreach my $name (keys %$snaphash) { | |
0ea6bc69 | 2786 | my $d = $snaphash->{$name}; |
75466c4f DM |
2787 | my $item = { |
2788 | name => $name, | |
2789 | snaptime => $d->{snaptime} || 0, | |
6aa4651b | 2790 | vmstate => $d->{vmstate} ? 1 : 0, |
982c7f12 DM |
2791 | description => $d->{description} || '', |
2792 | }; | |
0ea6bc69 | 2793 | $item->{parent} = $d->{parent} if $d->{parent}; |
3ee28e38 | 2794 | $item->{snapstate} = $d->{snapstate} if $d->{snapstate}; |
0ea6bc69 DM |
2795 | push @$res, $item; |
2796 | } | |
2797 | ||
6aa4651b DM |
2798 | my $running = PVE::QemuServer::check_running($vmid, 1) ? 1 : 0; |
2799 | my $current = { name => 'current', digest => $conf->{digest}, running => $running }; | |
d1914468 DM |
2800 | $current->{parent} = $conf->{parent} if $conf->{parent}; |
2801 | ||
2802 | push @$res, $current; | |
7e7d7b61 DM |
2803 | |
2804 | return $res; | |
2805 | }}); | |
2806 | ||
2807 | __PACKAGE__->register_method({ | |
2808 | name => 'snapshot', | |
2809 | path => '{vmid}/snapshot', | |
2810 | method => 'POST', | |
9dbd1ee4 AD |
2811 | protected => 1, |
2812 | proxyto => 'node', | |
2813 | description => "Snapshot a VM.", | |
2814 | permissions => { | |
f1baf1df | 2815 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], |
9dbd1ee4 AD |
2816 | }, |
2817 | parameters => { | |
2818 | additionalProperties => 0, | |
2819 | properties => { | |
2820 | node => get_standard_option('pve-node'), | |
2821 | vmid => get_standard_option('pve-vmid'), | |
8abd398b | 2822 | snapname => get_standard_option('pve-snapshot-name'), |
9dbd1ee4 AD |
2823 | vmstate => { |
2824 | optional => 1, | |
2825 | type => 'boolean', | |
2826 | description => "Save the vmstate", | |
2827 | }, | |
782f4f75 DM |
2828 | description => { |
2829 | optional => 1, | |
2830 | type => 'string', | |
2831 | description => "A textual description or comment.", | |
2832 | }, | |
9dbd1ee4 AD |
2833 | }, |
2834 | }, | |
7e7d7b61 DM |
2835 | returns => { |
2836 | type => 'string', | |
2837 | description => "the task ID.", | |
2838 | }, | |
9dbd1ee4 AD |
2839 | code => sub { |
2840 | my ($param) = @_; | |
2841 | ||
2842 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2843 | ||
2844 | my $authuser = $rpcenv->get_user(); | |
2845 | ||
2846 | my $node = extract_param($param, 'node'); | |
2847 | ||
2848 | my $vmid = extract_param($param, 'vmid'); | |
2849 | ||
9dbd1ee4 AD |
2850 | my $snapname = extract_param($param, 'snapname'); |
2851 | ||
d1914468 DM |
2852 | die "unable to use snapshot name 'current' (reserved name)\n" |
2853 | if $snapname eq 'current'; | |
2854 | ||
7e7d7b61 | 2855 | my $realcmd = sub { |
22c377f0 | 2856 | PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname"); |
af9110dd WL |
2857 | PVE::QemuServer::snapshot_create($vmid, $snapname, $param->{vmstate}, |
2858 | $param->{description}); | |
7e7d7b61 DM |
2859 | }; |
2860 | ||
2861 | return $rpcenv->fork_worker('qmsnapshot', $vmid, $authuser, $realcmd); | |
2862 | }}); | |
2863 | ||
154ccdcd DM |
2864 | __PACKAGE__->register_method({ |
2865 | name => 'snapshot_cmd_idx', | |
2866 | path => '{vmid}/snapshot/{snapname}', | |
2867 | description => '', | |
2868 | method => 'GET', | |
2869 | permissions => { | |
2870 | user => 'all', | |
2871 | }, | |
2872 | parameters => { | |
2873 | additionalProperties => 0, | |
2874 | properties => { | |
2875 | vmid => get_standard_option('pve-vmid'), | |
2876 | node => get_standard_option('pve-node'), | |
8abd398b | 2877 | snapname => get_standard_option('pve-snapshot-name'), |
154ccdcd DM |
2878 | }, |
2879 | }, | |
2880 | returns => { | |
2881 | type => 'array', | |
2882 | items => { | |
2883 | type => "object", | |
2884 | properties => {}, | |
2885 | }, | |
2886 | links => [ { rel => 'child', href => "{cmd}" } ], | |
2887 | }, | |
2888 | code => sub { | |
2889 | my ($param) = @_; | |
2890 | ||
2891 | my $res = []; | |
2892 | ||
2893 | push @$res, { cmd => 'rollback' }; | |
d788cea6 | 2894 | push @$res, { cmd => 'config' }; |
154ccdcd DM |
2895 | |
2896 | return $res; | |
2897 | }}); | |
2898 | ||
d788cea6 DM |
2899 | __PACKAGE__->register_method({ |
2900 | name => 'update_snapshot_config', | |
2901 | path => '{vmid}/snapshot/{snapname}/config', | |
2902 | method => 'PUT', | |
2903 | protected => 1, | |
2904 | proxyto => 'node', | |
2905 | description => "Update snapshot metadata.", | |
2906 | permissions => { | |
2907 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
2908 | }, | |
2909 | parameters => { | |
2910 | additionalProperties => 0, | |
2911 | properties => { | |
2912 | node => get_standard_option('pve-node'), | |
2913 | vmid => get_standard_option('pve-vmid'), | |
2914 | snapname => get_standard_option('pve-snapshot-name'), | |
2915 | description => { | |
2916 | optional => 1, | |
2917 | type => 'string', | |
2918 | description => "A textual description or comment.", | |
2919 | }, | |
2920 | }, | |
2921 | }, | |
2922 | returns => { type => 'null' }, | |
2923 | code => sub { | |
2924 | my ($param) = @_; | |
2925 | ||
2926 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2927 | ||
2928 | my $authuser = $rpcenv->get_user(); | |
2929 | ||
2930 | my $vmid = extract_param($param, 'vmid'); | |
2931 | ||
2932 | my $snapname = extract_param($param, 'snapname'); | |
2933 | ||
2934 | return undef if !defined($param->{description}); | |
2935 | ||
2936 | my $updatefn = sub { | |
2937 | ||
2938 | my $conf = PVE::QemuServer::load_config($vmid); | |
2939 | ||
2940 | PVE::QemuServer::check_lock($conf); | |
2941 | ||
2942 | my $snap = $conf->{snapshots}->{$snapname}; | |
2943 | ||
75466c4f DM |
2944 | die "snapshot '$snapname' does not exist\n" if !defined($snap); |
2945 | ||
d788cea6 DM |
2946 | $snap->{description} = $param->{description} if defined($param->{description}); |
2947 | ||
2948 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
2949 | }; | |
2950 | ||
2951 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
2952 | ||
2953 | return undef; | |
2954 | }}); | |
2955 | ||
2956 | __PACKAGE__->register_method({ | |
2957 | name => 'get_snapshot_config', | |
2958 | path => '{vmid}/snapshot/{snapname}/config', | |
2959 | method => 'GET', | |
2960 | proxyto => 'node', | |
2961 | description => "Get snapshot configuration", | |
2962 | permissions => { | |
2963 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], | |
2964 | }, | |
2965 | parameters => { | |
2966 | additionalProperties => 0, | |
2967 | properties => { | |
2968 | node => get_standard_option('pve-node'), | |
2969 | vmid => get_standard_option('pve-vmid'), | |
2970 | snapname => get_standard_option('pve-snapshot-name'), | |
2971 | }, | |
2972 | }, | |
2973 | returns => { type => "object" }, | |
2974 | code => sub { | |
2975 | my ($param) = @_; | |
2976 | ||
2977 | my $rpcenv = PVE::RPCEnvironment::get(); | |
2978 | ||
2979 | my $authuser = $rpcenv->get_user(); | |
2980 | ||
2981 | my $vmid = extract_param($param, 'vmid'); | |
2982 | ||
2983 | my $snapname = extract_param($param, 'snapname'); | |
2984 | ||
2985 | my $conf = PVE::QemuServer::load_config($vmid); | |
2986 | ||
2987 | my $snap = $conf->{snapshots}->{$snapname}; | |
2988 | ||
75466c4f DM |
2989 | die "snapshot '$snapname' does not exist\n" if !defined($snap); |
2990 | ||
d788cea6 DM |
2991 | return $snap; |
2992 | }}); | |
2993 | ||
7e7d7b61 DM |
2994 | __PACKAGE__->register_method({ |
2995 | name => 'rollback', | |
154ccdcd | 2996 | path => '{vmid}/snapshot/{snapname}/rollback', |
7e7d7b61 DM |
2997 | method => 'POST', |
2998 | protected => 1, | |
2999 | proxyto => 'node', | |
3000 | description => "Rollback VM state to specified snapshot.", | |
3001 | permissions => { | |
f1baf1df | 3002 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], |
7e7d7b61 DM |
3003 | }, |
3004 | parameters => { | |
3005 | additionalProperties => 0, | |
3006 | properties => { | |
3007 | node => get_standard_option('pve-node'), | |
3008 | vmid => get_standard_option('pve-vmid'), | |
8abd398b | 3009 | snapname => get_standard_option('pve-snapshot-name'), |
7e7d7b61 DM |
3010 | }, |
3011 | }, | |
3012 | returns => { | |
3013 | type => 'string', | |
3014 | description => "the task ID.", | |
3015 | }, | |
3016 | code => sub { | |
3017 | my ($param) = @_; | |
3018 | ||
3019 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3020 | ||
3021 | my $authuser = $rpcenv->get_user(); | |
3022 | ||
3023 | my $node = extract_param($param, 'node'); | |
3024 | ||
3025 | my $vmid = extract_param($param, 'vmid'); | |
3026 | ||
3027 | my $snapname = extract_param($param, 'snapname'); | |
3028 | ||
7e7d7b61 | 3029 | my $realcmd = sub { |
22c377f0 DM |
3030 | PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname"); |
3031 | PVE::QemuServer::snapshot_rollback($vmid, $snapname); | |
7e7d7b61 DM |
3032 | }; |
3033 | ||
3034 | return $rpcenv->fork_worker('qmrollback', $vmid, $authuser, $realcmd); | |
3035 | }}); | |
3036 | ||
3037 | __PACKAGE__->register_method({ | |
3038 | name => 'delsnapshot', | |
3039 | path => '{vmid}/snapshot/{snapname}', | |
3040 | method => 'DELETE', | |
3041 | protected => 1, | |
3042 | proxyto => 'node', | |
3043 | description => "Delete a VM snapshot.", | |
3044 | permissions => { | |
f1baf1df | 3045 | check => ['perm', '/vms/{vmid}', [ 'VM.Snapshot' ]], |
7e7d7b61 DM |
3046 | }, |
3047 | parameters => { | |
3048 | additionalProperties => 0, | |
3049 | properties => { | |
3050 | node => get_standard_option('pve-node'), | |
3051 | vmid => get_standard_option('pve-vmid'), | |
8abd398b | 3052 | snapname => get_standard_option('pve-snapshot-name'), |
3ee28e38 DM |
3053 | force => { |
3054 | optional => 1, | |
3055 | type => 'boolean', | |
3056 | description => "For removal from config file, even if removing disk snapshots fails.", | |
3057 | }, | |
7e7d7b61 DM |
3058 | }, |
3059 | }, | |
3060 | returns => { | |
3061 | type => 'string', | |
3062 | description => "the task ID.", | |
3063 | }, | |
3064 | code => sub { | |
3065 | my ($param) = @_; | |
3066 | ||
3067 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3068 | ||
3069 | my $authuser = $rpcenv->get_user(); | |
3070 | ||
3071 | my $node = extract_param($param, 'node'); | |
3072 | ||
3073 | my $vmid = extract_param($param, 'vmid'); | |
3074 | ||
3075 | my $snapname = extract_param($param, 'snapname'); | |
3076 | ||
7e7d7b61 | 3077 | my $realcmd = sub { |
22c377f0 | 3078 | PVE::Cluster::log_msg('info', $authuser, "delete snapshot VM $vmid: $snapname"); |
3ee28e38 | 3079 | PVE::QemuServer::snapshot_delete($vmid, $snapname, $param->{force}); |
7e7d7b61 | 3080 | }; |
9dbd1ee4 | 3081 | |
7b2257a8 | 3082 | return $rpcenv->fork_worker('qmdelsnapshot', $vmid, $authuser, $realcmd); |
9dbd1ee4 AD |
3083 | }}); |
3084 | ||
04a69bb4 AD |
3085 | __PACKAGE__->register_method({ |
3086 | name => 'template', | |
3087 | path => '{vmid}/template', | |
3088 | method => 'POST', | |
3089 | protected => 1, | |
3090 | proxyto => 'node', | |
3091 | description => "Create a Template.", | |
b02691d8 | 3092 | permissions => { |
7af0a6c8 DM |
3093 | description => "You need 'VM.Allocate' permissions on /vms/{vmid}", |
3094 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
b02691d8 | 3095 | }, |
04a69bb4 AD |
3096 | parameters => { |
3097 | additionalProperties => 0, | |
3098 | properties => { | |
3099 | node => get_standard_option('pve-node'), | |
3100 | vmid => get_standard_option('pve-vmid'), | |
3101 | disk => { | |
3102 | optional => 1, | |
3103 | type => 'string', | |
3104 | description => "If you want to convert only 1 disk to base image.", | |
3105 | enum => [PVE::QemuServer::disknames()], | |
3106 | }, | |
3107 | ||
3108 | }, | |
3109 | }, | |
3110 | returns => { type => 'null'}, | |
3111 | code => sub { | |
3112 | my ($param) = @_; | |
3113 | ||
3114 | my $rpcenv = PVE::RPCEnvironment::get(); | |
3115 | ||
3116 | my $authuser = $rpcenv->get_user(); | |
3117 | ||
3118 | my $node = extract_param($param, 'node'); | |
3119 | ||
3120 | my $vmid = extract_param($param, 'vmid'); | |
3121 | ||
3122 | my $disk = extract_param($param, 'disk'); | |
3123 | ||
3124 | my $updatefn = sub { | |
3125 | ||
3126 | my $conf = PVE::QemuServer::load_config($vmid); | |
3127 | ||
3128 | PVE::QemuServer::check_lock($conf); | |
3129 | ||
75466c4f | 3130 | die "unable to create template, because VM contains snapshots\n" |
b91c2aae | 3131 | if $conf->{snapshots} && scalar(keys %{$conf->{snapshots}}); |
0402a80b | 3132 | |
75466c4f | 3133 | die "you can't convert a template to a template\n" |
03c2d0ad | 3134 | if PVE::QemuServer::is_template($conf) && !$disk; |
0402a80b | 3135 | |
75466c4f | 3136 | die "you can't convert a VM to template if VM is running\n" |
218cab9a | 3137 | if PVE::QemuServer::check_running($vmid); |
35c5fdef | 3138 | |
04a69bb4 AD |
3139 | my $realcmd = sub { |
3140 | PVE::QemuServer::template_create($vmid, $conf, $disk); | |
3141 | }; | |
04a69bb4 | 3142 | |
75e7e997 | 3143 | $conf->{template} = 1; |
04a69bb4 | 3144 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); |
75e7e997 DM |
3145 | |
3146 | return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd); | |
04a69bb4 AD |
3147 | }; |
3148 | ||
3149 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
3150 | return undef; | |
3151 | }}); | |
3152 | ||
1e3baf05 | 3153 | 1; |