]>
Commit | Line | Data |
---|---|---|
1e3baf05 DM |
1 | package PVE::API2::Qemu; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5b9d692a | 5 | use Cwd 'abs_path'; |
1e3baf05 | 6 | |
502d18a2 | 7 | use PVE::Cluster qw (cfs_read_file cfs_write_file);; |
1e3baf05 DM |
8 | use PVE::SafeSyslog; |
9 | use PVE::Tools qw(extract_param); | |
10 | use PVE::Exception qw(raise raise_param_exc); | |
11 | use PVE::Storage; | |
12 | use PVE::JSONSchema qw(get_standard_option); | |
13 | use PVE::RESTHandler; | |
14 | use PVE::QemuServer; | |
3ea94c60 | 15 | use PVE::QemuMigrate; |
1e3baf05 DM |
16 | use PVE::RPCEnvironment; |
17 | use PVE::AccessControl; | |
18 | use PVE::INotify; | |
19 | ||
20 | use Data::Dumper; # fixme: remove | |
21 | ||
22 | use base qw(PVE::RESTHandler); | |
23 | ||
24 | 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."; | |
25 | ||
26 | my $resolve_cdrom_alias = sub { | |
27 | my $param = shift; | |
28 | ||
29 | if (my $value = $param->{cdrom}) { | |
30 | $value .= ",media=cdrom" if $value !~ m/media=/; | |
31 | $param->{ide2} = $value; | |
32 | delete $param->{cdrom}; | |
33 | } | |
34 | }; | |
35 | ||
a0d1b1a2 | 36 | |
ae57f6b3 | 37 | my $check_storage_access = sub { |
fcbb753e | 38 | my ($rpcenv, $authuser, $storecfg, $vmid, $settings, $default_storage) = @_; |
a0d1b1a2 | 39 | |
ae57f6b3 DM |
40 | PVE::QemuServer::foreach_drive($settings, sub { |
41 | my ($ds, $drive) = @_; | |
a0d1b1a2 | 42 | |
ae57f6b3 | 43 | my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive); |
a0d1b1a2 | 44 | |
ae57f6b3 | 45 | my $volid = $drive->{file}; |
a0d1b1a2 | 46 | |
09d0ee64 DM |
47 | if (!$volid || $volid eq 'none') { |
48 | # nothing to check | |
f5782fd0 DM |
49 | } elsif ($isCDROM && ($volid eq 'cdrom')) { |
50 | $rpcenv->check($authuser, "/", ['Sys.Console']); | |
09d0ee64 | 51 | } elsif (!$isCDROM && ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/)) { |
a0d1b1a2 DM |
52 | my ($storeid, $size) = ($2 || $default_storage, $3); |
53 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
fcbb753e | 54 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); |
a0d1b1a2 | 55 | } else { |
8b192abf | 56 | $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid); |
a0d1b1a2 DM |
57 | } |
58 | }); | |
ae57f6b3 | 59 | }; |
a0d1b1a2 | 60 | |
ae57f6b3 DM |
61 | # Note: $pool is only needed when creating a VM, because pool permissions |
62 | # are automatically inherited if VM already exists inside a pool. | |
63 | my $create_disks = sub { | |
64 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $default_storage) = @_; | |
a0d1b1a2 DM |
65 | |
66 | my $vollist = []; | |
a0d1b1a2 | 67 | |
ae57f6b3 DM |
68 | my $res = {}; |
69 | PVE::QemuServer::foreach_drive($settings, sub { | |
70 | my ($ds, $disk) = @_; | |
71 | ||
72 | my $volid = $disk->{file}; | |
73 | ||
f5782fd0 | 74 | if (!$volid || $volid eq 'none' || $volid eq 'cdrom') { |
09d0ee64 DM |
75 | $res->{$ds} = $settings->{$ds}; |
76 | } elsif ($volid =~ m/^(([^:\s]+):)?(\d+(\.\d+)?)$/) { | |
ae57f6b3 DM |
77 | my ($storeid, $size) = ($2 || $default_storage, $3); |
78 | die "no storage ID specified (and no default storage)\n" if !$storeid; | |
79 | my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid); | |
80 | my $fmt = $disk->{format} || $defformat; | |
a0d1b1a2 DM |
81 | my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, |
82 | $fmt, undef, $size*1024*1024); | |
a0d1b1a2 | 83 | $disk->{file} = $volid; |
24afaca0 | 84 | $disk->{size} = $size*1024*1024*1024; |
a0d1b1a2 | 85 | push @$vollist, $volid; |
ae57f6b3 DM |
86 | delete $disk->{format}; # no longer needed |
87 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
88 | } else { | |
eabe0da0 | 89 | |
ba68cf09 | 90 | my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $volid); |
eabe0da0 DM |
91 | |
92 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
35cb731c | 93 | |
35cb731c AD |
94 | my $foundvolid = undef; |
95 | ||
eabe0da0 DM |
96 | if ($storeid) { |
97 | PVE::Storage::activate_volumes($storecfg, [ $volid ]); | |
98 | my $dl = PVE::Storage::vdisk_list($storecfg, $storeid, undef); | |
99 | ||
100 | PVE::Storage::foreach_volid($dl, sub { | |
101 | my ($volumeid) = @_; | |
102 | if($volumeid eq $volid) { | |
103 | $foundvolid = 1; | |
104 | return; | |
105 | } | |
106 | }); | |
107 | } | |
35cb731c AD |
108 | |
109 | die "image '$path' does not exists\n" if (!(-f $path || -b $path || $foundvolid)); | |
24afaca0 DM |
110 | |
111 | my ($size) = PVE::Storage::volume_size_info($storecfg, $volid, 1); | |
112 | $disk->{size} = $size; | |
113 | $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk); | |
a0d1b1a2 | 114 | } |
ae57f6b3 | 115 | }); |
a0d1b1a2 DM |
116 | |
117 | # free allocated images on error | |
118 | if (my $err = $@) { | |
119 | syslog('err', "VM $vmid creating disks failed"); | |
120 | foreach my $volid (@$vollist) { | |
121 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
122 | warn $@ if $@; | |
123 | } | |
124 | die $err; | |
125 | } | |
126 | ||
127 | # modify vm config if everything went well | |
ae57f6b3 DM |
128 | foreach my $ds (keys %$res) { |
129 | $conf->{$ds} = $res->{$ds}; | |
a0d1b1a2 DM |
130 | } |
131 | ||
132 | return $vollist; | |
133 | }; | |
134 | ||
135 | my $check_vm_modify_config_perm = sub { | |
ae57f6b3 | 136 | my ($rpcenv, $authuser, $vmid, $pool, $key_list) = @_; |
a0d1b1a2 | 137 | |
6e5c4da7 | 138 | return 1 if $authuser eq 'root@pam'; |
a0d1b1a2 | 139 | |
ae57f6b3 | 140 | foreach my $opt (@$key_list) { |
a0d1b1a2 DM |
141 | # disk checks need to be done somewhere else |
142 | next if PVE::QemuServer::valid_drivename($opt); | |
143 | ||
144 | if ($opt eq 'sockets' || $opt eq 'cores' || | |
145 | $opt eq 'cpu' || $opt eq 'smp' || | |
ab6b35df | 146 | $opt eq 'cpulimit' || $opt eq 'cpuunits') { |
a0d1b1a2 DM |
147 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']); |
148 | } elsif ($opt eq 'boot' || $opt eq 'bootdisk') { | |
149 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']); | |
150 | } elsif ($opt eq 'memory' || $opt eq 'balloon') { | |
151 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Memory']); | |
152 | } elsif ($opt eq 'args' || $opt eq 'lock') { | |
153 | die "only root can set '$opt' config\n"; | |
154 | } elsif ($opt eq 'cpu' || $opt eq 'kvm' || $opt eq 'acpi' || | |
155 | $opt eq 'vga' || $opt eq 'watchdog' || $opt eq 'tablet') { | |
156 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']); | |
157 | } elsif ($opt =~ m/^net\d+$/) { | |
158 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']); | |
159 | } else { | |
160 | $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Options']); | |
161 | } | |
162 | } | |
163 | ||
164 | return 1; | |
165 | }; | |
166 | ||
1e3baf05 | 167 | __PACKAGE__->register_method({ |
afdb31d5 DM |
168 | name => 'vmlist', |
169 | path => '', | |
1e3baf05 DM |
170 | method => 'GET', |
171 | description => "Virtual machine index (per node).", | |
a0d1b1a2 DM |
172 | permissions => { |
173 | description => "Only list VMs where you have VM.Audit permissons on /vms/<vmid>.", | |
174 | user => 'all', | |
175 | }, | |
1e3baf05 DM |
176 | proxyto => 'node', |
177 | protected => 1, # qemu pid files are only readable by root | |
178 | parameters => { | |
179 | additionalProperties => 0, | |
180 | properties => { | |
181 | node => get_standard_option('pve-node'), | |
182 | }, | |
183 | }, | |
184 | returns => { | |
185 | type => 'array', | |
186 | items => { | |
187 | type => "object", | |
188 | properties => {}, | |
189 | }, | |
190 | links => [ { rel => 'child', href => "{vmid}" } ], | |
191 | }, | |
192 | code => sub { | |
193 | my ($param) = @_; | |
194 | ||
a0d1b1a2 DM |
195 | my $rpcenv = PVE::RPCEnvironment::get(); |
196 | my $authuser = $rpcenv->get_user(); | |
197 | ||
1e3baf05 DM |
198 | my $vmstatus = PVE::QemuServer::vmstatus(); |
199 | ||
a0d1b1a2 DM |
200 | my $res = []; |
201 | foreach my $vmid (keys %$vmstatus) { | |
202 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ], 1); | |
203 | ||
204 | my $data = $vmstatus->{$vmid}; | |
205 | $data->{vmid} = $vmid; | |
206 | push @$res, $data; | |
207 | } | |
1e3baf05 | 208 | |
a0d1b1a2 | 209 | return $res; |
1e3baf05 DM |
210 | }}); |
211 | ||
212 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
213 | name => 'create_vm', |
214 | path => '', | |
1e3baf05 | 215 | method => 'POST', |
3e16d5fc | 216 | description => "Create or restore a virtual machine.", |
a0d1b1a2 DM |
217 | permissions => { |
218 | description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. If you create disks you need 'Datastore.AllocateSpace' on any used storage.", | |
219 | check => [ 'or', | |
220 | [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
221 | [ 'perm', '/pool/{pool}', ['VM.Allocate'], require_param => 'pool'], | |
222 | ], | |
223 | }, | |
1e3baf05 DM |
224 | protected => 1, |
225 | proxyto => 'node', | |
226 | parameters => { | |
227 | additionalProperties => 0, | |
228 | properties => PVE::QemuServer::json_config_properties( | |
229 | { | |
230 | node => get_standard_option('pve-node'), | |
231 | vmid => get_standard_option('pve-vmid'), | |
3e16d5fc DM |
232 | archive => { |
233 | description => "The backup file.", | |
234 | type => 'string', | |
235 | optional => 1, | |
236 | maxLength => 255, | |
237 | }, | |
238 | storage => get_standard_option('pve-storage-id', { | |
239 | description => "Default storage.", | |
240 | optional => 1, | |
241 | }), | |
242 | force => { | |
afdb31d5 | 243 | optional => 1, |
3e16d5fc DM |
244 | type => 'boolean', |
245 | description => "Allow to overwrite existing VM.", | |
51586c3a DM |
246 | requires => 'archive', |
247 | }, | |
248 | unique => { | |
afdb31d5 | 249 | optional => 1, |
51586c3a DM |
250 | type => 'boolean', |
251 | description => "Assign a unique random ethernet address.", | |
252 | requires => 'archive', | |
3e16d5fc | 253 | }, |
a0d1b1a2 DM |
254 | pool => { |
255 | optional => 1, | |
256 | type => 'string', format => 'pve-poolid', | |
257 | description => "Add the VM to the specified pool.", | |
258 | }, | |
1e3baf05 DM |
259 | }), |
260 | }, | |
afdb31d5 | 261 | returns => { |
5fdbe4f0 DM |
262 | type => 'string', |
263 | }, | |
1e3baf05 DM |
264 | code => sub { |
265 | my ($param) = @_; | |
266 | ||
5fdbe4f0 DM |
267 | my $rpcenv = PVE::RPCEnvironment::get(); |
268 | ||
a0d1b1a2 | 269 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 | 270 | |
1e3baf05 DM |
271 | my $node = extract_param($param, 'node'); |
272 | ||
1e3baf05 DM |
273 | my $vmid = extract_param($param, 'vmid'); |
274 | ||
3e16d5fc DM |
275 | my $archive = extract_param($param, 'archive'); |
276 | ||
277 | my $storage = extract_param($param, 'storage'); | |
278 | ||
51586c3a DM |
279 | my $force = extract_param($param, 'force'); |
280 | ||
281 | my $unique = extract_param($param, 'unique'); | |
a0d1b1a2 DM |
282 | |
283 | my $pool = extract_param($param, 'pool'); | |
51586c3a | 284 | |
1e3baf05 | 285 | my $filename = PVE::QemuServer::config_file($vmid); |
afdb31d5 DM |
286 | |
287 | my $storecfg = PVE::Storage::config(); | |
1e3baf05 | 288 | |
3e16d5fc | 289 | PVE::Cluster::check_cfs_quorum(); |
1e3baf05 | 290 | |
a0d1b1a2 DM |
291 | if (defined($pool)) { |
292 | $rpcenv->check_pool_exist($pool); | |
a0d1b1a2 DM |
293 | } |
294 | ||
fcbb753e | 295 | $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']) |
a0d1b1a2 DM |
296 | if defined($storage); |
297 | ||
afdb31d5 | 298 | if (!$archive) { |
3e16d5fc | 299 | &$resolve_cdrom_alias($param); |
1e3baf05 | 300 | |
fcbb753e | 301 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage); |
ae57f6b3 DM |
302 | |
303 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]); | |
304 | ||
3e16d5fc DM |
305 | foreach my $opt (keys %$param) { |
306 | if (PVE::QemuServer::valid_drivename($opt)) { | |
307 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
308 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; | |
afdb31d5 | 309 | |
3e16d5fc DM |
310 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); |
311 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
312 | } | |
1e3baf05 | 313 | } |
3e16d5fc DM |
314 | |
315 | PVE::QemuServer::add_random_macs($param); | |
51586c3a DM |
316 | } else { |
317 | my $keystr = join(' ', keys %$param); | |
bc4dcb99 DM |
318 | raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr; |
319 | ||
5b9d692a | 320 | if ($archive eq '-') { |
afdb31d5 | 321 | die "pipe requires cli environment\n" |
d7810bc1 | 322 | if $rpcenv->{type} ne 'cli'; |
5b9d692a | 323 | } else { |
ba68cf09 | 324 | my $path = $rpcenv->check_volume_access($authuser, $storecfg, $vmid, $archive); |
d7810bc1 DM |
325 | |
326 | PVE::Storage::activate_volumes($storecfg, [ $archive ]) | |
327 | if PVE::Storage::parse_volume_id ($archive, 1); | |
328 | ||
971f27c4 DM |
329 | die "can't find archive file '$archive'\n" if !($path && -f $path); |
330 | $archive = $path; | |
331 | } | |
1e3baf05 DM |
332 | } |
333 | ||
502d18a2 DM |
334 | my $addVMtoPoolFn = sub { |
335 | my $usercfg = cfs_read_file("user.cfg"); | |
336 | if (my $data = $usercfg->{pools}->{$pool}) { | |
337 | $data->{vms}->{$vmid} = 1; | |
338 | $usercfg->{vms}->{$vmid} = $pool; | |
339 | cfs_write_file("user.cfg", $usercfg); | |
340 | } | |
341 | }; | |
342 | ||
3e16d5fc DM |
343 | my $restorefn = sub { |
344 | ||
345 | if (-f $filename) { | |
afdb31d5 | 346 | die "unable to restore vm $vmid: config file already exists\n" |
51586c3a | 347 | if !$force; |
3e16d5fc | 348 | |
afdb31d5 | 349 | die "unable to restore vm $vmid: vm is running\n" |
3e16d5fc | 350 | if PVE::QemuServer::check_running($vmid); |
a6af7b3e DM |
351 | |
352 | # destroy existing data - keep empty config | |
353 | PVE::QemuServer::destroy_vm($storecfg, $vmid, 1); | |
3e16d5fc DM |
354 | } |
355 | ||
356 | my $realcmd = sub { | |
a0d1b1a2 | 357 | PVE::QemuServer::restore_archive($archive, $vmid, $authuser, { |
51586c3a | 358 | storage => $storage, |
a0d1b1a2 | 359 | pool => $pool, |
51586c3a | 360 | unique => $unique }); |
502d18a2 DM |
361 | |
362 | PVE::AccessControl::lock_user_config($addVMtoPoolFn, "can't add VM to pool") if $pool; | |
3e16d5fc DM |
363 | }; |
364 | ||
a0d1b1a2 | 365 | return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd); |
3e16d5fc | 366 | }; |
1e3baf05 | 367 | |
1e3baf05 DM |
368 | my $createfn = sub { |
369 | ||
191435c6 | 370 | # test after locking |
afdb31d5 | 371 | die "unable to create vm $vmid: config file already exists\n" |
1e3baf05 DM |
372 | if -f $filename; |
373 | ||
5fdbe4f0 | 374 | my $realcmd = sub { |
1e3baf05 | 375 | |
5fdbe4f0 | 376 | my $vollist = []; |
1e3baf05 | 377 | |
1858638f DM |
378 | my $conf = $param; |
379 | ||
5fdbe4f0 | 380 | eval { |
ae57f6b3 | 381 | |
1858638f | 382 | $vollist = &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $param, $storage); |
1e3baf05 | 383 | |
5fdbe4f0 DM |
384 | # try to be smart about bootdisk |
385 | my @disks = PVE::QemuServer::disknames(); | |
386 | my $firstdisk; | |
387 | foreach my $ds (reverse @disks) { | |
1858638f DM |
388 | next if !$conf->{$ds}; |
389 | my $disk = PVE::QemuServer::parse_drive($ds, $conf->{$ds}); | |
5fdbe4f0 DM |
390 | next if PVE::QemuServer::drive_is_cdrom($disk); |
391 | $firstdisk = $ds; | |
392 | } | |
1e3baf05 | 393 | |
1858638f DM |
394 | if (!$conf->{bootdisk} && $firstdisk) { |
395 | $conf->{bootdisk} = $firstdisk; | |
5fdbe4f0 | 396 | } |
1e3baf05 | 397 | |
ae9ca91d DM |
398 | PVE::QemuServer::update_config_nolock($vmid, $conf); |
399 | ||
5fdbe4f0 DM |
400 | }; |
401 | my $err = $@; | |
1e3baf05 | 402 | |
5fdbe4f0 DM |
403 | if ($err) { |
404 | foreach my $volid (@$vollist) { | |
405 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
406 | warn $@ if $@; | |
407 | } | |
408 | die "create failed - $err"; | |
409 | } | |
502d18a2 DM |
410 | |
411 | PVE::AccessControl::lock_user_config($addVMtoPoolFn, "can't add VM to pool") if $pool; | |
5fdbe4f0 DM |
412 | }; |
413 | ||
a0d1b1a2 | 414 | return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
415 | }; |
416 | ||
191435c6 | 417 | return PVE::QemuServer::lock_config_full($vmid, 1, $archive ? $restorefn : $createfn); |
1e3baf05 DM |
418 | }}); |
419 | ||
420 | __PACKAGE__->register_method({ | |
421 | name => 'vmdiridx', | |
afdb31d5 | 422 | path => '{vmid}', |
1e3baf05 DM |
423 | method => 'GET', |
424 | proxyto => 'node', | |
425 | description => "Directory index", | |
a0d1b1a2 DM |
426 | permissions => { |
427 | user => 'all', | |
428 | }, | |
1e3baf05 DM |
429 | parameters => { |
430 | additionalProperties => 0, | |
431 | properties => { | |
432 | node => get_standard_option('pve-node'), | |
433 | vmid => get_standard_option('pve-vmid'), | |
434 | }, | |
435 | }, | |
436 | returns => { | |
437 | type => 'array', | |
438 | items => { | |
439 | type => "object", | |
440 | properties => { | |
441 | subdir => { type => 'string' }, | |
442 | }, | |
443 | }, | |
444 | links => [ { rel => 'child', href => "{subdir}" } ], | |
445 | }, | |
446 | code => sub { | |
447 | my ($param) = @_; | |
448 | ||
449 | my $res = [ | |
450 | { subdir => 'config' }, | |
451 | { subdir => 'status' }, | |
452 | { subdir => 'unlink' }, | |
453 | { subdir => 'vncproxy' }, | |
3ea94c60 | 454 | { subdir => 'migrate' }, |
1e3baf05 DM |
455 | { subdir => 'rrd' }, |
456 | { subdir => 'rrddata' }, | |
91c94f0a | 457 | { subdir => 'monitor' }, |
1e3baf05 | 458 | ]; |
afdb31d5 | 459 | |
1e3baf05 DM |
460 | return $res; |
461 | }}); | |
462 | ||
463 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
464 | name => 'rrd', |
465 | path => '{vmid}/rrd', | |
1e3baf05 DM |
466 | method => 'GET', |
467 | protected => 1, # fixme: can we avoid that? | |
468 | permissions => { | |
378b359e | 469 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], |
1e3baf05 DM |
470 | }, |
471 | description => "Read VM RRD statistics (returns PNG)", | |
472 | parameters => { | |
473 | additionalProperties => 0, | |
474 | properties => { | |
475 | node => get_standard_option('pve-node'), | |
476 | vmid => get_standard_option('pve-vmid'), | |
477 | timeframe => { | |
478 | description => "Specify the time frame you are interested in.", | |
479 | type => 'string', | |
480 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
481 | }, | |
482 | ds => { | |
483 | description => "The list of datasources you want to display.", | |
484 | type => 'string', format => 'pve-configid-list', | |
485 | }, | |
486 | cf => { | |
487 | description => "The RRD consolidation function", | |
488 | type => 'string', | |
489 | enum => [ 'AVERAGE', 'MAX' ], | |
490 | optional => 1, | |
491 | }, | |
492 | }, | |
493 | }, | |
494 | returns => { | |
495 | type => "object", | |
496 | properties => { | |
497 | filename => { type => 'string' }, | |
498 | }, | |
499 | }, | |
500 | code => sub { | |
501 | my ($param) = @_; | |
502 | ||
503 | return PVE::Cluster::create_rrd_graph( | |
afdb31d5 | 504 | "pve2-vm/$param->{vmid}", $param->{timeframe}, |
1e3baf05 | 505 | $param->{ds}, $param->{cf}); |
afdb31d5 | 506 | |
1e3baf05 DM |
507 | }}); |
508 | ||
509 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
510 | name => 'rrddata', |
511 | path => '{vmid}/rrddata', | |
1e3baf05 DM |
512 | method => 'GET', |
513 | protected => 1, # fixme: can we avoid that? | |
514 | permissions => { | |
378b359e | 515 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], |
1e3baf05 DM |
516 | }, |
517 | description => "Read VM RRD statistics", | |
518 | parameters => { | |
519 | additionalProperties => 0, | |
520 | properties => { | |
521 | node => get_standard_option('pve-node'), | |
522 | vmid => get_standard_option('pve-vmid'), | |
523 | timeframe => { | |
524 | description => "Specify the time frame you are interested in.", | |
525 | type => 'string', | |
526 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
527 | }, | |
528 | cf => { | |
529 | description => "The RRD consolidation function", | |
530 | type => 'string', | |
531 | enum => [ 'AVERAGE', 'MAX' ], | |
532 | optional => 1, | |
533 | }, | |
534 | }, | |
535 | }, | |
536 | returns => { | |
537 | type => "array", | |
538 | items => { | |
539 | type => "object", | |
540 | properties => {}, | |
541 | }, | |
542 | }, | |
543 | code => sub { | |
544 | my ($param) = @_; | |
545 | ||
546 | return PVE::Cluster::create_rrd_data( | |
547 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); | |
548 | }}); | |
549 | ||
550 | ||
551 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
552 | name => 'vm_config', |
553 | path => '{vmid}/config', | |
1e3baf05 DM |
554 | method => 'GET', |
555 | proxyto => 'node', | |
556 | description => "Get virtual machine configuration.", | |
a0d1b1a2 DM |
557 | permissions => { |
558 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
559 | }, | |
1e3baf05 DM |
560 | parameters => { |
561 | additionalProperties => 0, | |
562 | properties => { | |
563 | node => get_standard_option('pve-node'), | |
564 | vmid => get_standard_option('pve-vmid'), | |
565 | }, | |
566 | }, | |
afdb31d5 | 567 | returns => { |
1e3baf05 | 568 | type => "object", |
554ac7e7 DM |
569 | properties => { |
570 | digest => { | |
571 | type => 'string', | |
572 | description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', | |
573 | } | |
574 | }, | |
1e3baf05 DM |
575 | }, |
576 | code => sub { | |
577 | my ($param) = @_; | |
578 | ||
579 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
580 | ||
581 | return $conf; | |
582 | }}); | |
583 | ||
ae57f6b3 DM |
584 | my $vm_is_volid_owner = sub { |
585 | my ($storecfg, $vmid, $volid) =@_; | |
586 | ||
587 | if ($volid !~ m|^/|) { | |
588 | my ($path, $owner); | |
589 | eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); }; | |
590 | if ($owner && ($owner == $vmid)) { | |
591 | return 1; | |
592 | } | |
593 | } | |
594 | ||
595 | return undef; | |
596 | }; | |
597 | ||
598 | my $test_deallocate_drive = sub { | |
599 | my ($storecfg, $vmid, $key, $drive, $force) = @_; | |
600 | ||
601 | if (!PVE::QemuServer::drive_is_cdrom($drive)) { | |
602 | my $volid = $drive->{file}; | |
603 | if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) { | |
604 | if ($force || $key =~ m/^unused/) { | |
605 | my $sid = PVE::Storage::parse_volume_id($volid); | |
606 | return $sid; | |
607 | } | |
608 | } | |
609 | } | |
610 | ||
611 | return undef; | |
612 | }; | |
613 | ||
5d7a6767 | 614 | my $delete_drive = sub { |
1858638f | 615 | my ($conf, $storecfg, $vmid, $key, $drive, $force) = @_; |
5d7a6767 DM |
616 | |
617 | if (!PVE::QemuServer::drive_is_cdrom($drive)) { | |
618 | my $volid = $drive->{file}; | |
ae57f6b3 DM |
619 | if (&$vm_is_volid_owner($storecfg, $vmid, $volid)) { |
620 | if ($force || $key =~ m/^unused/) { | |
621 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
7e4e69a6 | 622 | die $@ if $@; |
ae57f6b3 DM |
623 | } else { |
624 | PVE::QemuServer::add_unused_volume($conf, $volid, $vmid); | |
5d7a6767 | 625 | } |
ae57f6b3 DM |
626 | } |
627 | } | |
49f9db93 DM |
628 | |
629 | delete $conf->{$key}; | |
ae57f6b3 DM |
630 | }; |
631 | ||
632 | my $vmconfig_delete_option = sub { | |
633 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force) = @_; | |
634 | ||
635 | return if !defined($conf->{$opt}); | |
636 | ||
637 | my $isDisk = PVE::QemuServer::valid_drivename($opt)|| ($opt =~ m/^unused/); | |
638 | ||
639 | if ($isDisk) { | |
640 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
641 | ||
642 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
643 | if (my $sid = &$test_deallocate_drive($storecfg, $vmid, $opt, $drive, $force)) { | |
fcbb753e | 644 | $rpcenv->check($authuser, "/storage/$sid", ['Datastore.Allocate']); |
5d7a6767 | 645 | } |
ae57f6b3 DM |
646 | } |
647 | ||
648 | die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt); | |
649 | ||
650 | if ($isDisk) { | |
651 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
652 | &$delete_drive($conf, $storecfg, $vmid, $opt, $drive, $force); | |
5d7a6767 | 653 | } else { |
ae57f6b3 DM |
654 | delete $conf->{$opt}; |
655 | } | |
656 | ||
657 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
658 | }; | |
659 | ||
93ae06e1 DM |
660 | my $safe_int_ne = sub { |
661 | my ($a, $b) = @_; | |
662 | ||
663 | return 0 if !defined($a) && !defined($b); | |
664 | return 1 if !defined($a); | |
665 | return 1 if !defined($b); | |
666 | ||
667 | return $a != $b; | |
668 | }; | |
669 | ||
ae57f6b3 DM |
670 | my $vmconfig_update_disk = sub { |
671 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_; | |
5d7a6767 | 672 | |
ae57f6b3 DM |
673 | my $drive = PVE::QemuServer::parse_drive($opt, $value); |
674 | ||
675 | if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom | |
676 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']); | |
677 | } else { | |
678 | $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']); | |
679 | } | |
680 | ||
681 | if ($conf->{$opt}) { | |
682 | ||
683 | if (my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt})) { | |
684 | ||
685 | my $media = $drive->{media} || 'disk'; | |
686 | my $oldmedia = $old_drive->{media} || 'disk'; | |
687 | die "unable to change media type\n" if $media ne $oldmedia; | |
688 | ||
689 | if (!PVE::QemuServer::drive_is_cdrom($old_drive) && | |
690 | ($drive->{file} ne $old_drive->{file})) { # delete old disks | |
691 | ||
692 | &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force); | |
693 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
694 | } | |
0f56d571 | 695 | |
93ae06e1 DM |
696 | if(&$safe_int_ne($drive->{bps}, $old_drive->{bps}) || |
697 | &$safe_int_ne($drive->{bps_rd}, $old_drive->{bps_rd}) || | |
698 | &$safe_int_ne($drive->{bps_wr}, $old_drive->{bps_wr}) || | |
699 | &$safe_int_ne($drive->{iops}, $old_drive->{iops}) || | |
700 | &$safe_int_ne($drive->{iops_rd}, $old_drive->{iops_rd}) || | |
701 | &$safe_int_ne($drive->{iops_wr}, $old_drive->{iops_wr})) { | |
0f56d571 AD |
702 | PVE::QemuServer::qemu_block_set_io_throttle($vmid,"drive-$opt",$drive->{bps}, $drive->{bps_rd}, $drive->{bps_wr}, $drive->{iops}, $drive->{iops_rd}, $drive->{iops_wr}) if !PVE::QemuServer::drive_is_cdrom($drive); |
703 | } | |
ae57f6b3 DM |
704 | } |
705 | } | |
706 | ||
707 | &$create_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, {$opt => $value}); | |
708 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
709 | ||
710 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
711 | $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
712 | ||
713 | if (PVE::QemuServer::drive_is_cdrom($drive)) { # cdrom | |
714 | ||
715 | if (PVE::QemuServer::check_running($vmid)) { | |
716 | if ($drive->{file} eq 'none') { | |
ce156282 | 717 | PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt"); |
ae57f6b3 DM |
718 | } else { |
719 | my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file}); | |
ce156282 AD |
720 | PVE::QemuServer::vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt"); #force eject if locked |
721 | PVE::QemuServer::vm_mon_cmd($vmid, "change",device => "drive-$opt",target => "$path") if $path; | |
ae57f6b3 DM |
722 | } |
723 | } | |
724 | ||
725 | } else { # hotplug new disks | |
726 | ||
727 | die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive); | |
5d7a6767 | 728 | } |
5d7a6767 DM |
729 | }; |
730 | ||
0d02881c AD |
731 | my $vmconfig_resize_disk = sub { |
732 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value, $force) = @_; | |
733 | ||
734 | my $drive = PVE::QemuServer::parse_drive($opt, $value); | |
735 | ||
736 | if (PVE::QemuServer::drive_is_cdrom($drive)) { #cdrom | |
737 | die "you can't resize a cdrom"; | |
0d02881c AD |
738 | } |
739 | ||
740 | if ($conf->{$opt}) { | |
741 | ||
742 | if (my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt})) { | |
743 | my $volid = $drive->{file}; | |
744 | my $size = PVE::Storage::volume_size_info($storecfg, $volid, 1); | |
745 | if ($value =~ m/^\+([1-9]\d*(\.\d+)?)([KMG])?$/){ | |
746 | my ($sizeextent, $unit) = ($1, $3); | |
747 | if ($unit) { | |
748 | if ($unit eq 'K') { | |
749 | $sizeextent = $sizeextent * 1024; | |
750 | } elsif ($unit eq 'M') { | |
751 | $sizeextent = $sizeextent * 1024 * 1024; | |
752 | } elsif ($unit eq 'G') { | |
753 | $sizeextent = $sizeextent * 1024 * 1024 * 1024; | |
754 | } | |
755 | } | |
756 | ||
757 | my $targetsize = $size + int($sizeextent); | |
758 | PVE::QemuServer::qemu_block_resize($vmid, "drive-$opt", $storecfg, $drive->{file}, $targetsize); | |
759 | my $newsize = PVE::Storage::volume_size_info($storecfg, $volid, 1); | |
760 | $drive->{size} = $newsize; | |
761 | $conf->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
762 | ||
763 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
764 | } | |
765 | } | |
766 | } | |
767 | ||
768 | }; | |
769 | ||
ae57f6b3 DM |
770 | my $vmconfig_update_net = sub { |
771 | my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $value) = @_; | |
772 | ||
773 | if ($conf->{$opt}) { | |
774 | #if online update, then unplug first | |
775 | die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt); | |
776 | } | |
777 | ||
778 | $conf->{$opt} = $value; | |
779 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
780 | $conf = PVE::QemuServer::load_config($vmid); # update/reload | |
781 | ||
782 | my $net = PVE::QemuServer::parse_net($conf->{$opt}); | |
783 | ||
784 | die "error hotplug $opt" if !PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $net); | |
785 | }; | |
786 | ||
a0d1b1a2 DM |
787 | my $vm_config_perm_list = [ |
788 | 'VM.Config.Disk', | |
789 | 'VM.Config.CDROM', | |
790 | 'VM.Config.CPU', | |
791 | 'VM.Config.Memory', | |
792 | 'VM.Config.Network', | |
793 | 'VM.Config.HWType', | |
794 | 'VM.Config.Options', | |
795 | ]; | |
796 | ||
1e3baf05 | 797 | __PACKAGE__->register_method({ |
afdb31d5 DM |
798 | name => 'update_vm', |
799 | path => '{vmid}/config', | |
1e3baf05 DM |
800 | method => 'PUT', |
801 | protected => 1, | |
802 | proxyto => 'node', | |
803 | description => "Set virtual machine options.", | |
a0d1b1a2 DM |
804 | permissions => { |
805 | check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1], | |
806 | }, | |
1e3baf05 DM |
807 | parameters => { |
808 | additionalProperties => 0, | |
809 | properties => PVE::QemuServer::json_config_properties( | |
810 | { | |
811 | node => get_standard_option('pve-node'), | |
812 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 | 813 | skiplock => get_standard_option('skiplock'), |
1e3baf05 DM |
814 | delete => { |
815 | type => 'string', format => 'pve-configid-list', | |
816 | description => "A list of settings you want to delete.", | |
817 | optional => 1, | |
818 | }, | |
819 | force => { | |
820 | type => 'boolean', | |
821 | description => $opt_force_description, | |
822 | optional => 1, | |
823 | requires => 'delete', | |
824 | }, | |
554ac7e7 DM |
825 | digest => { |
826 | type => 'string', | |
827 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
828 | maxLength => 40, | |
afdb31d5 | 829 | optional => 1, |
554ac7e7 | 830 | } |
1e3baf05 DM |
831 | }), |
832 | }, | |
833 | returns => { type => 'null'}, | |
834 | code => sub { | |
835 | my ($param) = @_; | |
836 | ||
837 | my $rpcenv = PVE::RPCEnvironment::get(); | |
838 | ||
a0d1b1a2 | 839 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
840 | |
841 | my $node = extract_param($param, 'node'); | |
842 | ||
1e3baf05 DM |
843 | my $vmid = extract_param($param, 'vmid'); |
844 | ||
5fdbe4f0 DM |
845 | my $digest = extract_param($param, 'digest'); |
846 | ||
847 | my @paramarr = (); # used for log message | |
848 | foreach my $key (keys %$param) { | |
849 | push @paramarr, "-$key", $param->{$key}; | |
850 | } | |
851 | ||
1e3baf05 | 852 | my $skiplock = extract_param($param, 'skiplock'); |
afdb31d5 | 853 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 854 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 855 | |
0532bc63 DM |
856 | my $delete_str = extract_param($param, 'delete'); |
857 | ||
1e3baf05 DM |
858 | my $force = extract_param($param, 'force'); |
859 | ||
0532bc63 DM |
860 | die "no options specified\n" if !$delete_str && !scalar(keys %$param); |
861 | ||
1e68cb19 DM |
862 | my $storecfg = PVE::Storage::config(); |
863 | ||
864 | &$resolve_cdrom_alias($param); | |
865 | ||
866 | # now try to verify all parameters | |
867 | ||
0532bc63 DM |
868 | my @delete = (); |
869 | foreach my $opt (PVE::Tools::split_list($delete_str)) { | |
870 | $opt = 'ide2' if $opt eq 'cdrom'; | |
871 | raise_param_exc({ delete => "you can't use '-$opt' and " . | |
872 | "-delete $opt' at the same time" }) | |
873 | if defined($param->{$opt}); | |
874 | ||
875 | if (!PVE::QemuServer::option_exists($opt)) { | |
876 | raise_param_exc({ delete => "unknown option '$opt'" }); | |
877 | } | |
ae57f6b3 | 878 | |
0532bc63 DM |
879 | push @delete, $opt; |
880 | } | |
1e3baf05 | 881 | |
1e68cb19 DM |
882 | foreach my $opt (keys %$param) { |
883 | if (PVE::QemuServer::valid_drivename($opt)) { | |
884 | # cleanup drive path | |
885 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
886 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); | |
887 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
1e68cb19 DM |
888 | } elsif ($opt =~ m/^net(\d+)$/) { |
889 | # add macaddr | |
890 | my $net = PVE::QemuServer::parse_net($param->{$opt}); | |
891 | $param->{$opt} = PVE::QemuServer::print_net($net); | |
1e68cb19 DM |
892 | } |
893 | } | |
1e3baf05 | 894 | |
ae57f6b3 DM |
895 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]); |
896 | ||
897 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]); | |
898 | ||
fcbb753e | 899 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param); |
1e3baf05 | 900 | |
5d39a182 | 901 | my $updatefn = sub { |
1e3baf05 | 902 | |
5d39a182 | 903 | my $conf = PVE::QemuServer::load_config($vmid); |
1e3baf05 | 904 | |
5d39a182 DM |
905 | die "checksum missmatch (file change by other user?)\n" |
906 | if $digest && $digest ne $conf->{digest}; | |
1e3baf05 | 907 | |
5d39a182 | 908 | PVE::QemuServer::check_lock($conf) if !$skiplock; |
1e3baf05 | 909 | |
a0d1b1a2 | 910 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr)); |
c2a64aa7 | 911 | |
5d7a6767 | 912 | foreach my $opt (@delete) { # delete |
1e68cb19 | 913 | $conf = PVE::QemuServer::load_config($vmid); # update/reload |
ae57f6b3 | 914 | &$vmconfig_delete_option($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $force); |
5d39a182 | 915 | } |
1e3baf05 | 916 | |
5d7a6767 | 917 | foreach my $opt (keys %$param) { # add/change |
1e3baf05 | 918 | |
1e68cb19 | 919 | $conf = PVE::QemuServer::load_config($vmid); # update/reload |
1e3baf05 | 920 | |
5d7a6767 DM |
921 | next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed |
922 | ||
ae57f6b3 | 923 | if (PVE::QemuServer::valid_drivename($opt)) { |
5d7a6767 | 924 | |
ae57f6b3 DM |
925 | &$vmconfig_update_disk($rpcenv, $authuser, $conf, $storecfg, $vmid, |
926 | $opt, $param->{$opt}, $force); | |
1e68cb19 | 927 | |
ae57f6b3 | 928 | } elsif ($opt =~ m/^net(\d+)$/) { #nics |
1858638f | 929 | |
ae57f6b3 DM |
930 | &$vmconfig_update_net($rpcenv, $authuser, $conf, $storecfg, $vmid, |
931 | $opt, $param->{$opt}); | |
1e68cb19 DM |
932 | |
933 | } else { | |
934 | ||
1858638f DM |
935 | $conf->{$opt} = $param->{$opt}; |
936 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
3a1e36bb | 937 | } |
5d39a182 DM |
938 | } |
939 | }; | |
940 | ||
941 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
fcdb0117 | 942 | |
1e3baf05 DM |
943 | return undef; |
944 | }}); | |
945 | ||
946 | ||
947 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
948 | name => 'destroy_vm', |
949 | path => '{vmid}', | |
1e3baf05 DM |
950 | method => 'DELETE', |
951 | protected => 1, | |
952 | proxyto => 'node', | |
953 | description => "Destroy the vm (also delete all used/owned volumes).", | |
a0d1b1a2 DM |
954 | permissions => { |
955 | check => [ 'perm', '/vms/{vmid}', ['VM.Allocate']], | |
956 | }, | |
1e3baf05 DM |
957 | parameters => { |
958 | additionalProperties => 0, | |
959 | properties => { | |
960 | node => get_standard_option('pve-node'), | |
961 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 | 962 | skiplock => get_standard_option('skiplock'), |
1e3baf05 DM |
963 | }, |
964 | }, | |
afdb31d5 | 965 | returns => { |
5fdbe4f0 DM |
966 | type => 'string', |
967 | }, | |
1e3baf05 DM |
968 | code => sub { |
969 | my ($param) = @_; | |
970 | ||
971 | my $rpcenv = PVE::RPCEnvironment::get(); | |
972 | ||
a0d1b1a2 | 973 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
974 | |
975 | my $vmid = $param->{vmid}; | |
976 | ||
977 | my $skiplock = $param->{skiplock}; | |
afdb31d5 | 978 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 979 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 980 | |
5fdbe4f0 DM |
981 | # test if VM exists |
982 | my $conf = PVE::QemuServer::load_config($vmid); | |
983 | ||
afdb31d5 | 984 | my $storecfg = PVE::Storage::config(); |
1e3baf05 | 985 | |
502d18a2 DM |
986 | my $delVMfromPoolFn = sub { |
987 | my $usercfg = cfs_read_file("user.cfg"); | |
5d0094ea DM |
988 | if (my $pool = $usercfg->{vms}->{$vmid}) { |
989 | if (my $data = $usercfg->{pools}->{$pool}) { | |
990 | delete $data->{vms}->{$vmid}; | |
991 | delete $usercfg->{vms}->{$vmid}; | |
992 | cfs_write_file("user.cfg", $usercfg); | |
993 | } | |
502d18a2 DM |
994 | } |
995 | }; | |
996 | ||
5fdbe4f0 | 997 | my $realcmd = sub { |
ff1a2432 DM |
998 | my $upid = shift; |
999 | ||
1000 | syslog('info', "destroy VM $vmid: $upid\n"); | |
1001 | ||
5fdbe4f0 | 1002 | PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); |
502d18a2 DM |
1003 | |
1004 | PVE::AccessControl::lock_user_config($delVMfromPoolFn, "pool cleanup failed"); | |
5fdbe4f0 | 1005 | }; |
1e3baf05 | 1006 | |
a0d1b1a2 | 1007 | return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd); |
1e3baf05 DM |
1008 | }}); |
1009 | ||
1010 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1011 | name => 'unlink', |
1012 | path => '{vmid}/unlink', | |
1e3baf05 DM |
1013 | method => 'PUT', |
1014 | protected => 1, | |
1015 | proxyto => 'node', | |
1016 | description => "Unlink/delete disk images.", | |
a0d1b1a2 DM |
1017 | permissions => { |
1018 | check => [ 'perm', '/vms/{vmid}', ['VM.Config.Disk']], | |
1019 | }, | |
1e3baf05 DM |
1020 | parameters => { |
1021 | additionalProperties => 0, | |
1022 | properties => { | |
1023 | node => get_standard_option('pve-node'), | |
1024 | vmid => get_standard_option('pve-vmid'), | |
1025 | idlist => { | |
1026 | type => 'string', format => 'pve-configid-list', | |
1027 | description => "A list of disk IDs you want to delete.", | |
1028 | }, | |
1029 | force => { | |
1030 | type => 'boolean', | |
1031 | description => $opt_force_description, | |
1032 | optional => 1, | |
1033 | }, | |
1034 | }, | |
1035 | }, | |
1036 | returns => { type => 'null'}, | |
1037 | code => sub { | |
1038 | my ($param) = @_; | |
1039 | ||
1040 | $param->{delete} = extract_param($param, 'idlist'); | |
1041 | ||
1042 | __PACKAGE__->update_vm($param); | |
1043 | ||
1044 | return undef; | |
1045 | }}); | |
1046 | ||
1047 | my $sslcert; | |
1048 | ||
1049 | __PACKAGE__->register_method({ | |
afdb31d5 DM |
1050 | name => 'vncproxy', |
1051 | path => '{vmid}/vncproxy', | |
1e3baf05 DM |
1052 | method => 'POST', |
1053 | protected => 1, | |
1054 | permissions => { | |
378b359e | 1055 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], |
1e3baf05 DM |
1056 | }, |
1057 | description => "Creates a TCP VNC proxy connections.", | |
1058 | parameters => { | |
1059 | additionalProperties => 0, | |
1060 | properties => { | |
1061 | node => get_standard_option('pve-node'), | |
1062 | vmid => get_standard_option('pve-vmid'), | |
1063 | }, | |
1064 | }, | |
afdb31d5 | 1065 | returns => { |
1e3baf05 DM |
1066 | additionalProperties => 0, |
1067 | properties => { | |
1068 | user => { type => 'string' }, | |
1069 | ticket => { type => 'string' }, | |
1070 | cert => { type => 'string' }, | |
1071 | port => { type => 'integer' }, | |
1072 | upid => { type => 'string' }, | |
1073 | }, | |
1074 | }, | |
1075 | code => sub { | |
1076 | my ($param) = @_; | |
1077 | ||
1078 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1079 | ||
a0d1b1a2 | 1080 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1081 | |
1082 | my $vmid = $param->{vmid}; | |
1083 | my $node = $param->{node}; | |
1084 | ||
b6f39da2 DM |
1085 | my $authpath = "/vms/$vmid"; |
1086 | ||
a0d1b1a2 | 1087 | my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath); |
b6f39da2 | 1088 | |
1e3baf05 DM |
1089 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) |
1090 | if !$sslcert; | |
1091 | ||
1092 | my $port = PVE::Tools::next_vnc_port(); | |
1093 | ||
1094 | my $remip; | |
afdb31d5 | 1095 | |
4f1be36c | 1096 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { |
1e3baf05 DM |
1097 | $remip = PVE::Cluster::remote_node_ip($node); |
1098 | } | |
1099 | ||
1100 | # NOTE: kvm VNC traffic is already TLS encrypted, | |
1101 | # so we select the fastest chipher here (or 'none'?) | |
1102 | my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', | |
1103 | '-c', 'blowfish-cbc', $remip] : []; | |
1104 | ||
afdb31d5 | 1105 | my $timeout = 10; |
1e3baf05 DM |
1106 | |
1107 | my $realcmd = sub { | |
1108 | my $upid = shift; | |
1109 | ||
1110 | syslog('info', "starting vnc proxy $upid\n"); | |
1111 | ||
1112 | my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid]; | |
1113 | ||
1114 | my $qmstr = join(' ', @$qmcmd); | |
1115 | ||
1116 | # also redirect stderr (else we get RFB protocol errors) | |
be62c45c | 1117 | my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"]; |
1e3baf05 | 1118 | |
be62c45c | 1119 | PVE::Tools::run_command($cmd); |
1e3baf05 DM |
1120 | |
1121 | return; | |
1122 | }; | |
1123 | ||
a0d1b1a2 | 1124 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd); |
1e3baf05 DM |
1125 | |
1126 | return { | |
a0d1b1a2 | 1127 | user => $authuser, |
1e3baf05 | 1128 | ticket => $ticket, |
afdb31d5 DM |
1129 | port => $port, |
1130 | upid => $upid, | |
1131 | cert => $sslcert, | |
1e3baf05 DM |
1132 | }; |
1133 | }}); | |
1134 | ||
5fdbe4f0 DM |
1135 | __PACKAGE__->register_method({ |
1136 | name => 'vmcmdidx', | |
afdb31d5 | 1137 | path => '{vmid}/status', |
5fdbe4f0 DM |
1138 | method => 'GET', |
1139 | proxyto => 'node', | |
1140 | description => "Directory index", | |
a0d1b1a2 DM |
1141 | permissions => { |
1142 | user => 'all', | |
1143 | }, | |
5fdbe4f0 DM |
1144 | parameters => { |
1145 | additionalProperties => 0, | |
1146 | properties => { | |
1147 | node => get_standard_option('pve-node'), | |
1148 | vmid => get_standard_option('pve-vmid'), | |
1149 | }, | |
1150 | }, | |
1151 | returns => { | |
1152 | type => 'array', | |
1153 | items => { | |
1154 | type => "object", | |
1155 | properties => { | |
1156 | subdir => { type => 'string' }, | |
1157 | }, | |
1158 | }, | |
1159 | links => [ { rel => 'child', href => "{subdir}" } ], | |
1160 | }, | |
1161 | code => sub { | |
1162 | my ($param) = @_; | |
1163 | ||
1164 | # test if VM exists | |
1165 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
1166 | ||
1167 | my $res = [ | |
1168 | { subdir => 'current' }, | |
1169 | { subdir => 'start' }, | |
1170 | { subdir => 'stop' }, | |
1171 | ]; | |
afdb31d5 | 1172 | |
5fdbe4f0 DM |
1173 | return $res; |
1174 | }}); | |
1175 | ||
88fc87b4 DM |
1176 | my $vm_is_ha_managed = sub { |
1177 | my ($vmid) = @_; | |
1178 | ||
1179 | my $cc = PVE::Cluster::cfs_read_file('cluster.conf'); | |
1180 | if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $vmid, 1)) { | |
1181 | return 1; | |
1182 | } | |
1183 | return 0; | |
1184 | }; | |
1185 | ||
1e3baf05 | 1186 | __PACKAGE__->register_method({ |
afdb31d5 | 1187 | name => 'vm_status', |
5fdbe4f0 | 1188 | path => '{vmid}/status/current', |
1e3baf05 DM |
1189 | method => 'GET', |
1190 | proxyto => 'node', | |
1191 | protected => 1, # qemu pid files are only readable by root | |
1192 | description => "Get virtual machine status.", | |
a0d1b1a2 DM |
1193 | permissions => { |
1194 | check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], | |
1195 | }, | |
1e3baf05 DM |
1196 | parameters => { |
1197 | additionalProperties => 0, | |
1198 | properties => { | |
1199 | node => get_standard_option('pve-node'), | |
1200 | vmid => get_standard_option('pve-vmid'), | |
1201 | }, | |
1202 | }, | |
1203 | returns => { type => 'object' }, | |
1204 | code => sub { | |
1205 | my ($param) = @_; | |
1206 | ||
1207 | # test if VM exists | |
1208 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
1209 | ||
03a33f30 | 1210 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); |
8610701a | 1211 | my $status = $vmstatus->{$param->{vmid}}; |
1e3baf05 | 1212 | |
88fc87b4 | 1213 | $status->{ha} = &$vm_is_ha_managed($param->{vmid}); |
8610701a DM |
1214 | |
1215 | return $status; | |
1e3baf05 DM |
1216 | }}); |
1217 | ||
1218 | __PACKAGE__->register_method({ | |
afdb31d5 | 1219 | name => 'vm_start', |
5fdbe4f0 DM |
1220 | path => '{vmid}/status/start', |
1221 | method => 'POST', | |
1e3baf05 DM |
1222 | protected => 1, |
1223 | proxyto => 'node', | |
5fdbe4f0 | 1224 | description => "Start virtual machine.", |
a0d1b1a2 DM |
1225 | permissions => { |
1226 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1227 | }, | |
1e3baf05 DM |
1228 | parameters => { |
1229 | additionalProperties => 0, | |
1230 | properties => { | |
1231 | node => get_standard_option('pve-node'), | |
1232 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 DM |
1233 | skiplock => get_standard_option('skiplock'), |
1234 | stateuri => get_standard_option('pve-qm-stateuri'), | |
1e3baf05 DM |
1235 | }, |
1236 | }, | |
afdb31d5 | 1237 | returns => { |
5fdbe4f0 DM |
1238 | type => 'string', |
1239 | }, | |
1e3baf05 DM |
1240 | code => sub { |
1241 | my ($param) = @_; | |
1242 | ||
1243 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1244 | ||
a0d1b1a2 | 1245 | my $authuser = $rpcenv->get_user(); |
1e3baf05 DM |
1246 | |
1247 | my $node = extract_param($param, 'node'); | |
1248 | ||
1e3baf05 DM |
1249 | my $vmid = extract_param($param, 'vmid'); |
1250 | ||
3ea94c60 | 1251 | my $stateuri = extract_param($param, 'stateuri'); |
afdb31d5 | 1252 | raise_param_exc({ stateuri => "Only root may use this option." }) |
a0d1b1a2 | 1253 | if $stateuri && $authuser ne 'root@pam'; |
3ea94c60 | 1254 | |
1e3baf05 | 1255 | my $skiplock = extract_param($param, 'skiplock'); |
afdb31d5 | 1256 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1257 | if $skiplock && $authuser ne 'root@pam'; |
1e3baf05 | 1258 | |
afdb31d5 | 1259 | my $storecfg = PVE::Storage::config(); |
5fdbe4f0 | 1260 | |
cce37749 DM |
1261 | if (&$vm_is_ha_managed($vmid) && !$stateuri && |
1262 | $rpcenv->{type} ne 'ha') { | |
5fdbe4f0 | 1263 | |
88fc87b4 DM |
1264 | my $hacmd = sub { |
1265 | my $upid = shift; | |
5fdbe4f0 | 1266 | |
88fc87b4 | 1267 | my $service = "pvevm:$vmid"; |
5fdbe4f0 | 1268 | |
88fc87b4 DM |
1269 | my $cmd = ['clusvcadm', '-e', $service, '-m', $node]; |
1270 | ||
1271 | print "Executing HA start for VM $vmid\n"; | |
1272 | ||
1273 | PVE::Tools::run_command($cmd); | |
1274 | ||
1275 | return; | |
1276 | }; | |
1277 | ||
1278 | return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd); | |
1279 | ||
1280 | } else { | |
1281 | ||
1282 | my $realcmd = sub { | |
1283 | my $upid = shift; | |
1284 | ||
1285 | syslog('info', "start VM $vmid: $upid\n"); | |
1286 | ||
1287 | PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock); | |
1288 | ||
1289 | return; | |
1290 | }; | |
5fdbe4f0 | 1291 | |
88fc87b4 DM |
1292 | return $rpcenv->fork_worker('qmstart', $vmid, $authuser, $realcmd); |
1293 | } | |
5fdbe4f0 DM |
1294 | }}); |
1295 | ||
1296 | __PACKAGE__->register_method({ | |
afdb31d5 | 1297 | name => 'vm_stop', |
5fdbe4f0 DM |
1298 | path => '{vmid}/status/stop', |
1299 | method => 'POST', | |
1300 | protected => 1, | |
1301 | proxyto => 'node', | |
1302 | description => "Stop virtual machine.", | |
a0d1b1a2 DM |
1303 | permissions => { |
1304 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1305 | }, | |
5fdbe4f0 DM |
1306 | parameters => { |
1307 | additionalProperties => 0, | |
1308 | properties => { | |
1309 | node => get_standard_option('pve-node'), | |
1310 | vmid => get_standard_option('pve-vmid'), | |
1311 | skiplock => get_standard_option('skiplock'), | |
c6bb9502 DM |
1312 | timeout => { |
1313 | description => "Wait maximal timeout seconds.", | |
1314 | type => 'integer', | |
1315 | minimum => 0, | |
1316 | optional => 1, | |
254575e9 DM |
1317 | }, |
1318 | keepActive => { | |
1319 | description => "Do not decativate storage volumes.", | |
1320 | type => 'boolean', | |
1321 | optional => 1, | |
1322 | default => 0, | |
c6bb9502 | 1323 | } |
5fdbe4f0 DM |
1324 | }, |
1325 | }, | |
afdb31d5 | 1326 | returns => { |
5fdbe4f0 DM |
1327 | type => 'string', |
1328 | }, | |
1329 | code => sub { | |
1330 | my ($param) = @_; | |
1331 | ||
1332 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1333 | ||
a0d1b1a2 | 1334 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1335 | |
1336 | my $node = extract_param($param, 'node'); | |
1337 | ||
1338 | my $vmid = extract_param($param, 'vmid'); | |
1339 | ||
1340 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1341 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1342 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1343 | |
254575e9 | 1344 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 1345 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 1346 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 1347 | |
ff1a2432 DM |
1348 | my $storecfg = PVE::Storage::config(); |
1349 | ||
3be30d63 | 1350 | if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { |
5fdbe4f0 | 1351 | |
88fc87b4 DM |
1352 | my $hacmd = sub { |
1353 | my $upid = shift; | |
5fdbe4f0 | 1354 | |
88fc87b4 | 1355 | my $service = "pvevm:$vmid"; |
c6bb9502 | 1356 | |
88fc87b4 DM |
1357 | my $cmd = ['clusvcadm', '-d', $service]; |
1358 | ||
1359 | print "Executing HA stop for VM $vmid\n"; | |
1360 | ||
1361 | PVE::Tools::run_command($cmd); | |
5fdbe4f0 | 1362 | |
88fc87b4 DM |
1363 | return; |
1364 | }; | |
1365 | ||
1366 | return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd); | |
1367 | ||
1368 | } else { | |
1369 | my $realcmd = sub { | |
1370 | my $upid = shift; | |
1371 | ||
1372 | syslog('info', "stop VM $vmid: $upid\n"); | |
1373 | ||
1374 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, | |
1375 | $param->{timeout}, 0, 1, $keepActive); | |
1376 | ||
1377 | return; | |
1378 | }; | |
1379 | ||
1380 | return $rpcenv->fork_worker('qmstop', $vmid, $authuser, $realcmd); | |
1381 | } | |
5fdbe4f0 DM |
1382 | }}); |
1383 | ||
1384 | __PACKAGE__->register_method({ | |
afdb31d5 | 1385 | name => 'vm_reset', |
5fdbe4f0 DM |
1386 | path => '{vmid}/status/reset', |
1387 | method => 'POST', | |
1388 | protected => 1, | |
1389 | proxyto => 'node', | |
1390 | description => "Reset virtual machine.", | |
a0d1b1a2 DM |
1391 | permissions => { |
1392 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1393 | }, | |
5fdbe4f0 DM |
1394 | parameters => { |
1395 | additionalProperties => 0, | |
1396 | properties => { | |
1397 | node => get_standard_option('pve-node'), | |
1398 | vmid => get_standard_option('pve-vmid'), | |
1399 | skiplock => get_standard_option('skiplock'), | |
1400 | }, | |
1401 | }, | |
afdb31d5 | 1402 | returns => { |
5fdbe4f0 DM |
1403 | type => 'string', |
1404 | }, | |
1405 | code => sub { | |
1406 | my ($param) = @_; | |
1407 | ||
1408 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1409 | ||
a0d1b1a2 | 1410 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1411 | |
1412 | my $node = extract_param($param, 'node'); | |
1413 | ||
1414 | my $vmid = extract_param($param, 'vmid'); | |
1415 | ||
1416 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1417 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1418 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1419 | |
ff1a2432 DM |
1420 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
1421 | ||
5fdbe4f0 DM |
1422 | my $realcmd = sub { |
1423 | my $upid = shift; | |
1424 | ||
1e3baf05 | 1425 | PVE::QemuServer::vm_reset($vmid, $skiplock); |
5fdbe4f0 DM |
1426 | |
1427 | return; | |
1428 | }; | |
1429 | ||
a0d1b1a2 | 1430 | return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1431 | }}); |
1432 | ||
1433 | __PACKAGE__->register_method({ | |
afdb31d5 | 1434 | name => 'vm_shutdown', |
5fdbe4f0 DM |
1435 | path => '{vmid}/status/shutdown', |
1436 | method => 'POST', | |
1437 | protected => 1, | |
1438 | proxyto => 'node', | |
1439 | description => "Shutdown virtual machine.", | |
a0d1b1a2 DM |
1440 | permissions => { |
1441 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1442 | }, | |
5fdbe4f0 DM |
1443 | parameters => { |
1444 | additionalProperties => 0, | |
1445 | properties => { | |
1446 | node => get_standard_option('pve-node'), | |
1447 | vmid => get_standard_option('pve-vmid'), | |
1448 | skiplock => get_standard_option('skiplock'), | |
c6bb9502 DM |
1449 | timeout => { |
1450 | description => "Wait maximal timeout seconds.", | |
1451 | type => 'integer', | |
1452 | minimum => 0, | |
1453 | optional => 1, | |
9269013a DM |
1454 | }, |
1455 | forceStop => { | |
1456 | description => "Make sure the VM stops.", | |
1457 | type => 'boolean', | |
1458 | optional => 1, | |
1459 | default => 0, | |
254575e9 DM |
1460 | }, |
1461 | keepActive => { | |
1462 | description => "Do not decativate storage volumes.", | |
1463 | type => 'boolean', | |
1464 | optional => 1, | |
1465 | default => 0, | |
c6bb9502 | 1466 | } |
5fdbe4f0 DM |
1467 | }, |
1468 | }, | |
afdb31d5 | 1469 | returns => { |
5fdbe4f0 DM |
1470 | type => 'string', |
1471 | }, | |
1472 | code => sub { | |
1473 | my ($param) = @_; | |
1474 | ||
1475 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1476 | ||
a0d1b1a2 | 1477 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1478 | |
1479 | my $node = extract_param($param, 'node'); | |
1480 | ||
1481 | my $vmid = extract_param($param, 'vmid'); | |
1482 | ||
1483 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1484 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1485 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1486 | |
254575e9 | 1487 | my $keepActive = extract_param($param, 'keepActive'); |
afdb31d5 | 1488 | raise_param_exc({ keepActive => "Only root may use this option." }) |
a0d1b1a2 | 1489 | if $keepActive && $authuser ne 'root@pam'; |
254575e9 | 1490 | |
02d07cf5 DM |
1491 | my $storecfg = PVE::Storage::config(); |
1492 | ||
5fdbe4f0 DM |
1493 | my $realcmd = sub { |
1494 | my $upid = shift; | |
1495 | ||
1496 | syslog('info', "shutdown VM $vmid: $upid\n"); | |
1497 | ||
afdb31d5 | 1498 | PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout}, |
254575e9 | 1499 | 1, $param->{forceStop}, $keepActive); |
c6bb9502 | 1500 | |
5fdbe4f0 DM |
1501 | return; |
1502 | }; | |
1503 | ||
a0d1b1a2 | 1504 | return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1505 | }}); |
1506 | ||
1507 | __PACKAGE__->register_method({ | |
afdb31d5 | 1508 | name => 'vm_suspend', |
5fdbe4f0 DM |
1509 | path => '{vmid}/status/suspend', |
1510 | method => 'POST', | |
1511 | protected => 1, | |
1512 | proxyto => 'node', | |
1513 | description => "Suspend virtual machine.", | |
a0d1b1a2 DM |
1514 | permissions => { |
1515 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1516 | }, | |
5fdbe4f0 DM |
1517 | parameters => { |
1518 | additionalProperties => 0, | |
1519 | properties => { | |
1520 | node => get_standard_option('pve-node'), | |
1521 | vmid => get_standard_option('pve-vmid'), | |
1522 | skiplock => get_standard_option('skiplock'), | |
1523 | }, | |
1524 | }, | |
afdb31d5 | 1525 | returns => { |
5fdbe4f0 DM |
1526 | type => 'string', |
1527 | }, | |
1528 | code => sub { | |
1529 | my ($param) = @_; | |
1530 | ||
1531 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1532 | ||
a0d1b1a2 | 1533 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1534 | |
1535 | my $node = extract_param($param, 'node'); | |
1536 | ||
1537 | my $vmid = extract_param($param, 'vmid'); | |
1538 | ||
1539 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1540 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1541 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1542 | |
ff1a2432 DM |
1543 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
1544 | ||
5fdbe4f0 DM |
1545 | my $realcmd = sub { |
1546 | my $upid = shift; | |
1547 | ||
1548 | syslog('info', "suspend VM $vmid: $upid\n"); | |
1549 | ||
1e3baf05 | 1550 | PVE::QemuServer::vm_suspend($vmid, $skiplock); |
5fdbe4f0 DM |
1551 | |
1552 | return; | |
1553 | }; | |
1554 | ||
a0d1b1a2 | 1555 | return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1556 | }}); |
1557 | ||
1558 | __PACKAGE__->register_method({ | |
afdb31d5 | 1559 | name => 'vm_resume', |
5fdbe4f0 DM |
1560 | path => '{vmid}/status/resume', |
1561 | method => 'POST', | |
1562 | protected => 1, | |
1563 | proxyto => 'node', | |
1564 | description => "Resume virtual machine.", | |
a0d1b1a2 DM |
1565 | permissions => { |
1566 | check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]], | |
1567 | }, | |
5fdbe4f0 DM |
1568 | parameters => { |
1569 | additionalProperties => 0, | |
1570 | properties => { | |
1571 | node => get_standard_option('pve-node'), | |
1572 | vmid => get_standard_option('pve-vmid'), | |
1573 | skiplock => get_standard_option('skiplock'), | |
1574 | }, | |
1575 | }, | |
afdb31d5 | 1576 | returns => { |
5fdbe4f0 DM |
1577 | type => 'string', |
1578 | }, | |
1579 | code => sub { | |
1580 | my ($param) = @_; | |
1581 | ||
1582 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1583 | ||
a0d1b1a2 | 1584 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1585 | |
1586 | my $node = extract_param($param, 'node'); | |
1587 | ||
1588 | my $vmid = extract_param($param, 'vmid'); | |
1589 | ||
1590 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1591 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1592 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 | 1593 | |
b7eeab21 | 1594 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); |
ff1a2432 | 1595 | |
5fdbe4f0 DM |
1596 | my $realcmd = sub { |
1597 | my $upid = shift; | |
1598 | ||
1599 | syslog('info', "resume VM $vmid: $upid\n"); | |
1600 | ||
1e3baf05 | 1601 | PVE::QemuServer::vm_resume($vmid, $skiplock); |
1e3baf05 | 1602 | |
5fdbe4f0 DM |
1603 | return; |
1604 | }; | |
1605 | ||
a0d1b1a2 | 1606 | return $rpcenv->fork_worker('qmresume', $vmid, $authuser, $realcmd); |
5fdbe4f0 DM |
1607 | }}); |
1608 | ||
1609 | __PACKAGE__->register_method({ | |
afdb31d5 | 1610 | name => 'vm_sendkey', |
5fdbe4f0 DM |
1611 | path => '{vmid}/sendkey', |
1612 | method => 'PUT', | |
1613 | protected => 1, | |
1614 | proxyto => 'node', | |
1615 | description => "Send key event to virtual machine.", | |
a0d1b1a2 DM |
1616 | permissions => { |
1617 | check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]], | |
1618 | }, | |
5fdbe4f0 DM |
1619 | parameters => { |
1620 | additionalProperties => 0, | |
1621 | properties => { | |
1622 | node => get_standard_option('pve-node'), | |
1623 | vmid => get_standard_option('pve-vmid'), | |
1624 | skiplock => get_standard_option('skiplock'), | |
1625 | key => { | |
1626 | description => "The key (qemu monitor encoding).", | |
1627 | type => 'string' | |
1628 | } | |
1629 | }, | |
1630 | }, | |
1631 | returns => { type => 'null'}, | |
1632 | code => sub { | |
1633 | my ($param) = @_; | |
1634 | ||
1635 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1636 | ||
a0d1b1a2 | 1637 | my $authuser = $rpcenv->get_user(); |
5fdbe4f0 DM |
1638 | |
1639 | my $node = extract_param($param, 'node'); | |
1640 | ||
1641 | my $vmid = extract_param($param, 'vmid'); | |
1642 | ||
1643 | my $skiplock = extract_param($param, 'skiplock'); | |
afdb31d5 | 1644 | raise_param_exc({ skiplock => "Only root may use this option." }) |
a0d1b1a2 | 1645 | if $skiplock && $authuser ne 'root@pam'; |
5fdbe4f0 DM |
1646 | |
1647 | PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); | |
1648 | ||
1649 | return; | |
1e3baf05 DM |
1650 | }}); |
1651 | ||
3ea94c60 | 1652 | __PACKAGE__->register_method({ |
afdb31d5 | 1653 | name => 'migrate_vm', |
3ea94c60 DM |
1654 | path => '{vmid}/migrate', |
1655 | method => 'POST', | |
1656 | protected => 1, | |
1657 | proxyto => 'node', | |
1658 | description => "Migrate virtual machine. Creates a new migration task.", | |
a0d1b1a2 DM |
1659 | permissions => { |
1660 | check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], | |
1661 | }, | |
3ea94c60 DM |
1662 | parameters => { |
1663 | additionalProperties => 0, | |
1664 | properties => { | |
1665 | node => get_standard_option('pve-node'), | |
1666 | vmid => get_standard_option('pve-vmid'), | |
1667 | target => get_standard_option('pve-node', { description => "Target node." }), | |
1668 | online => { | |
1669 | type => 'boolean', | |
1670 | description => "Use online/live migration.", | |
1671 | optional => 1, | |
1672 | }, | |
1673 | force => { | |
1674 | type => 'boolean', | |
1675 | description => "Allow to migrate VMs which use local devices. Only root may use this option.", | |
1676 | optional => 1, | |
1677 | }, | |
1678 | }, | |
1679 | }, | |
afdb31d5 | 1680 | returns => { |
3ea94c60 DM |
1681 | type => 'string', |
1682 | description => "the task ID.", | |
1683 | }, | |
1684 | code => sub { | |
1685 | my ($param) = @_; | |
1686 | ||
1687 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1688 | ||
a0d1b1a2 | 1689 | my $authuser = $rpcenv->get_user(); |
3ea94c60 DM |
1690 | |
1691 | my $target = extract_param($param, 'target'); | |
1692 | ||
1693 | my $localnode = PVE::INotify::nodename(); | |
1694 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
1695 | ||
1696 | PVE::Cluster::check_cfs_quorum(); | |
1697 | ||
1698 | PVE::Cluster::check_node_exists($target); | |
1699 | ||
1700 | my $targetip = PVE::Cluster::remote_node_ip($target); | |
1701 | ||
1702 | my $vmid = extract_param($param, 'vmid'); | |
1703 | ||
afdb31d5 | 1704 | raise_param_exc({ force => "Only root may use this option." }) |
a0d1b1a2 | 1705 | if $param->{force} && $authuser ne 'root@pam'; |
3ea94c60 DM |
1706 | |
1707 | # test if VM exists | |
a5ed42d3 | 1708 | my $conf = PVE::QemuServer::load_config($vmid); |
3ea94c60 DM |
1709 | |
1710 | # try to detect errors early | |
a5ed42d3 DM |
1711 | |
1712 | PVE::QemuServer::check_lock($conf); | |
1713 | ||
3ea94c60 | 1714 | if (PVE::QemuServer::check_running($vmid)) { |
afdb31d5 | 1715 | die "cant migrate running VM without --online\n" |
3ea94c60 DM |
1716 | if !$param->{online}; |
1717 | } | |
1718 | ||
47152e2e | 1719 | my $storecfg = PVE::Storage::config(); |
22d646a7 | 1720 | PVE::QemuServer::check_storage_availability($storecfg, $conf, $target); |
47152e2e | 1721 | |
3be30d63 | 1722 | if (&$vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { |
3ea94c60 | 1723 | |
88fc87b4 DM |
1724 | my $hacmd = sub { |
1725 | my $upid = shift; | |
3ea94c60 | 1726 | |
88fc87b4 DM |
1727 | my $service = "pvevm:$vmid"; |
1728 | ||
1729 | my $cmd = ['clusvcadm', '-M', $service, '-m', $target]; | |
1730 | ||
1731 | print "Executing HA migrate for VM $vmid to node $target\n"; | |
1732 | ||
1733 | PVE::Tools::run_command($cmd); | |
1734 | ||
1735 | return; | |
1736 | }; | |
1737 | ||
1738 | return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); | |
1739 | ||
1740 | } else { | |
1741 | ||
1742 | my $realcmd = sub { | |
1743 | my $upid = shift; | |
1744 | ||
1745 | PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param); | |
1746 | }; | |
1747 | ||
1748 | return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $realcmd); | |
1749 | } | |
3ea94c60 | 1750 | |
3ea94c60 | 1751 | }}); |
1e3baf05 | 1752 | |
91c94f0a | 1753 | __PACKAGE__->register_method({ |
afdb31d5 DM |
1754 | name => 'monitor', |
1755 | path => '{vmid}/monitor', | |
91c94f0a DM |
1756 | method => 'POST', |
1757 | protected => 1, | |
1758 | proxyto => 'node', | |
1759 | description => "Execute Qemu monitor commands.", | |
a0d1b1a2 DM |
1760 | permissions => { |
1761 | check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]], | |
1762 | }, | |
91c94f0a DM |
1763 | parameters => { |
1764 | additionalProperties => 0, | |
1765 | properties => { | |
1766 | node => get_standard_option('pve-node'), | |
1767 | vmid => get_standard_option('pve-vmid'), | |
1768 | command => { | |
1769 | type => 'string', | |
1770 | description => "The monitor command.", | |
1771 | } | |
1772 | }, | |
1773 | }, | |
1774 | returns => { type => 'string'}, | |
1775 | code => sub { | |
1776 | my ($param) = @_; | |
1777 | ||
1778 | my $vmid = $param->{vmid}; | |
1779 | ||
1780 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
1781 | ||
1782 | my $res = ''; | |
1783 | eval { | |
7b7c6d1b | 1784 | $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command}); |
91c94f0a DM |
1785 | }; |
1786 | $res = "ERROR: $@" if $@; | |
1787 | ||
1788 | return $res; | |
1789 | }}); | |
1790 | ||
0d02881c AD |
1791 | __PACKAGE__->register_method({ |
1792 | name => 'resize_vm', | |
614e3941 | 1793 | path => '{vmid}/resize', |
0d02881c AD |
1794 | method => 'PUT', |
1795 | protected => 1, | |
1796 | proxyto => 'node', | |
1797 | description => "extend volume size.", | |
1798 | permissions => { | |
3b2773f6 | 1799 | check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]], |
0d02881c AD |
1800 | }, |
1801 | parameters => { | |
1802 | additionalProperties => 0, | |
1803 | properties => PVE::QemuServer::json_config_properties( | |
1804 | { | |
1805 | node => get_standard_option('pve-node'), | |
1806 | vmid => get_standard_option('pve-vmid'), | |
1807 | skiplock => get_standard_option('skiplock'), | |
1808 | digest => { | |
1809 | type => 'string', | |
1810 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
1811 | maxLength => 40, | |
1812 | optional => 1, | |
1813 | } | |
1814 | }), | |
1815 | }, | |
1816 | returns => { type => 'null'}, | |
1817 | code => sub { | |
1818 | my ($param) = @_; | |
1819 | ||
1820 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1821 | ||
1822 | my $authuser = $rpcenv->get_user(); | |
1823 | ||
1824 | my $node = extract_param($param, 'node'); | |
1825 | ||
1826 | my $vmid = extract_param($param, 'vmid'); | |
1827 | ||
1828 | my $digest = extract_param($param, 'digest'); | |
1829 | ||
1830 | my @paramarr = (); # used for log message | |
1831 | foreach my $key (keys %$param) { | |
1832 | push @paramarr, "-$key", $param->{$key}; | |
1833 | } | |
1834 | ||
1835 | my $skiplock = extract_param($param, 'skiplock'); | |
1836 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1837 | if $skiplock && $authuser ne 'root@pam'; | |
1838 | ||
1839 | my $force = extract_param($param, 'force'); | |
1840 | ||
1841 | die "no options specified\n" if !scalar(keys %$param); | |
1842 | ||
1843 | my $storecfg = PVE::Storage::config(); | |
1844 | ||
1845 | ||
1846 | &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]); | |
1847 | ||
1848 | &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param); | |
1849 | ||
1850 | my $updatefn = sub { | |
1851 | ||
1852 | my $conf = PVE::QemuServer::load_config($vmid); | |
1853 | ||
1854 | die "checksum missmatch (file change by other user?)\n" | |
1855 | if $digest && $digest ne $conf->{digest}; | |
1856 | PVE::QemuServer::check_lock($conf) if !$skiplock; | |
1857 | ||
1858 | PVE::Cluster::log_msg('info', $authuser, "update VM $vmid: " . join (' ', @paramarr)); | |
1859 | ||
1860 | foreach my $opt (keys %$param) { # add/change | |
1861 | if (PVE::QemuServer::valid_drivename($opt)) { | |
1862 | &$vmconfig_resize_disk($rpcenv, $authuser, $conf, $storecfg, $vmid, $opt, $param->{$opt}, $force); | |
1863 | } | |
1864 | } | |
1865 | ||
1866 | }; | |
1867 | ||
1868 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
1869 | return undef; | |
1870 | }}); | |
1871 | ||
1e3baf05 | 1872 | 1; |