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