]>
Commit | Line | Data |
---|---|---|
1e3baf05 DM |
1 | package PVE::API2::Qemu; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5b9d692a | 5 | use Cwd 'abs_path'; |
1e3baf05 DM |
6 | |
7 | use PVE::Cluster; | |
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 | ||
36 | __PACKAGE__->register_method({ | |
37 | name => 'vmlist', | |
38 | path => '', | |
39 | method => 'GET', | |
40 | description => "Virtual machine index (per node).", | |
41 | proxyto => 'node', | |
42 | protected => 1, # qemu pid files are only readable by root | |
43 | parameters => { | |
44 | additionalProperties => 0, | |
45 | properties => { | |
46 | node => get_standard_option('pve-node'), | |
47 | }, | |
48 | }, | |
49 | returns => { | |
50 | type => 'array', | |
51 | items => { | |
52 | type => "object", | |
53 | properties => {}, | |
54 | }, | |
55 | links => [ { rel => 'child', href => "{vmid}" } ], | |
56 | }, | |
57 | code => sub { | |
58 | my ($param) = @_; | |
59 | ||
60 | my $vmstatus = PVE::QemuServer::vmstatus(); | |
61 | ||
62 | return PVE::RESTHandler::hash_to_array($vmstatus, 'vmid'); | |
63 | ||
64 | }}); | |
65 | ||
66 | __PACKAGE__->register_method({ | |
67 | name => 'create_vm', | |
68 | path => '', | |
69 | method => 'POST', | |
3e16d5fc | 70 | description => "Create or restore a virtual machine.", |
1e3baf05 DM |
71 | protected => 1, |
72 | proxyto => 'node', | |
73 | parameters => { | |
74 | additionalProperties => 0, | |
75 | properties => PVE::QemuServer::json_config_properties( | |
76 | { | |
77 | node => get_standard_option('pve-node'), | |
78 | vmid => get_standard_option('pve-vmid'), | |
3e16d5fc DM |
79 | archive => { |
80 | description => "The backup file.", | |
81 | type => 'string', | |
82 | optional => 1, | |
83 | maxLength => 255, | |
84 | }, | |
85 | storage => get_standard_option('pve-storage-id', { | |
86 | description => "Default storage.", | |
87 | optional => 1, | |
88 | }), | |
89 | force => { | |
90 | optional => 1, | |
91 | type => 'boolean', | |
92 | description => "Allow to overwrite existing VM.", | |
51586c3a DM |
93 | requires => 'archive', |
94 | }, | |
95 | unique => { | |
96 | optional => 1, | |
97 | type => 'boolean', | |
98 | description => "Assign a unique random ethernet address.", | |
99 | requires => 'archive', | |
3e16d5fc | 100 | }, |
1e3baf05 DM |
101 | }), |
102 | }, | |
5fdbe4f0 DM |
103 | returns => { |
104 | type => 'string', | |
105 | }, | |
1e3baf05 DM |
106 | code => sub { |
107 | my ($param) = @_; | |
108 | ||
5fdbe4f0 DM |
109 | my $rpcenv = PVE::RPCEnvironment::get(); |
110 | ||
111 | my $user = $rpcenv->get_user(); | |
112 | ||
1e3baf05 DM |
113 | my $node = extract_param($param, 'node'); |
114 | ||
1e3baf05 DM |
115 | my $vmid = extract_param($param, 'vmid'); |
116 | ||
3e16d5fc DM |
117 | my $archive = extract_param($param, 'archive'); |
118 | ||
119 | my $storage = extract_param($param, 'storage'); | |
120 | ||
51586c3a DM |
121 | my $force = extract_param($param, 'force'); |
122 | ||
123 | my $unique = extract_param($param, 'unique'); | |
124 | ||
1e3baf05 | 125 | my $filename = PVE::QemuServer::config_file($vmid); |
1e3baf05 DM |
126 | |
127 | my $storecfg = PVE::Storage::config(); | |
128 | ||
3e16d5fc | 129 | PVE::Cluster::check_cfs_quorum(); |
1e3baf05 | 130 | |
3e16d5fc DM |
131 | if (!$archive) { |
132 | &$resolve_cdrom_alias($param); | |
1e3baf05 | 133 | |
3e16d5fc DM |
134 | foreach my $opt (keys %$param) { |
135 | if (PVE::QemuServer::valid_drivename($opt)) { | |
136 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
137 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; | |
138 | ||
139 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); | |
140 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
141 | } | |
1e3baf05 | 142 | } |
3e16d5fc DM |
143 | |
144 | PVE::QemuServer::add_random_macs($param); | |
51586c3a DM |
145 | } else { |
146 | my $keystr = join(' ', keys %$param); | |
bc4dcb99 DM |
147 | raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr; |
148 | ||
5b9d692a DM |
149 | if ($archive eq '-') { |
150 | die "pipe requires cli environment\n" | |
151 | && $rpcenv->{type} ne 'cli'; | |
152 | } else { | |
971f27c4 | 153 | my $path; |
5b9d692a | 154 | if (PVE::Storage::parse_volume_id($archive, 1)) { |
971f27c4 | 155 | $path = PVE::Storage::path($storecfg, $archive); |
5b9d692a DM |
156 | } else { |
157 | raise_param_exc({ archive => "Only root can pass arbitrary paths." }) | |
158 | if $user ne 'root@pam'; | |
159 | ||
971f27c4 | 160 | $path = abs_path($archive); |
5b9d692a | 161 | } |
971f27c4 DM |
162 | die "can't find archive file '$archive'\n" if !($path && -f $path); |
163 | $archive = $path; | |
164 | } | |
1e3baf05 DM |
165 | } |
166 | ||
3e16d5fc DM |
167 | my $restorefn = sub { |
168 | ||
169 | if (-f $filename) { | |
170 | die "unable to restore vm $vmid: config file already exists\n" | |
51586c3a | 171 | if !$force; |
3e16d5fc DM |
172 | |
173 | die "unable to restore vm $vmid: vm is running\n" | |
174 | if PVE::QemuServer::check_running($vmid); | |
a6af7b3e DM |
175 | |
176 | # destroy existing data - keep empty config | |
177 | PVE::QemuServer::destroy_vm($storecfg, $vmid, 1); | |
3e16d5fc DM |
178 | } |
179 | ||
180 | my $realcmd = sub { | |
51586c3a DM |
181 | PVE::QemuServer::restore_archive($archive, $vmid, { |
182 | storage => $storage, | |
183 | unique => $unique }); | |
3e16d5fc DM |
184 | }; |
185 | ||
186 | return $rpcenv->fork_worker('qmrestore', $vmid, $user, $realcmd); | |
187 | }; | |
1e3baf05 | 188 | |
1e3baf05 DM |
189 | my $createfn = sub { |
190 | ||
191 | # second test (after locking test is accurate) | |
192 | die "unable to create vm $vmid: config file already exists\n" | |
193 | if -f $filename; | |
194 | ||
5fdbe4f0 | 195 | my $realcmd = sub { |
1e3baf05 | 196 | |
5fdbe4f0 | 197 | my $vollist = []; |
1e3baf05 | 198 | |
5fdbe4f0 | 199 | eval { |
3e16d5fc | 200 | $vollist = PVE::QemuServer::create_disks($storecfg, $vmid, $param, $storage); |
1e3baf05 | 201 | |
5fdbe4f0 DM |
202 | # try to be smart about bootdisk |
203 | my @disks = PVE::QemuServer::disknames(); | |
204 | my $firstdisk; | |
205 | foreach my $ds (reverse @disks) { | |
206 | next if !$param->{$ds}; | |
207 | my $disk = PVE::QemuServer::parse_drive($ds, $param->{$ds}); | |
208 | next if PVE::QemuServer::drive_is_cdrom($disk); | |
209 | $firstdisk = $ds; | |
210 | } | |
1e3baf05 | 211 | |
5fdbe4f0 DM |
212 | if (!$param->{bootdisk} && $firstdisk) { |
213 | $param->{bootdisk} = $firstdisk; | |
214 | } | |
1e3baf05 | 215 | |
5fdbe4f0 DM |
216 | PVE::QemuServer::create_conf_nolock($vmid, $param); |
217 | }; | |
218 | my $err = $@; | |
1e3baf05 | 219 | |
5fdbe4f0 DM |
220 | if ($err) { |
221 | foreach my $volid (@$vollist) { | |
222 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
223 | warn $@ if $@; | |
224 | } | |
225 | die "create failed - $err"; | |
226 | } | |
227 | }; | |
228 | ||
229 | return $rpcenv->fork_worker('qmcreate', $vmid, $user, $realcmd); | |
230 | }; | |
231 | ||
3e16d5fc | 232 | return PVE::QemuServer::lock_config($vmid, $archive ? $restorefn : $createfn); |
1e3baf05 DM |
233 | }}); |
234 | ||
235 | __PACKAGE__->register_method({ | |
236 | name => 'vmdiridx', | |
237 | path => '{vmid}', | |
238 | method => 'GET', | |
239 | proxyto => 'node', | |
240 | description => "Directory index", | |
241 | parameters => { | |
242 | additionalProperties => 0, | |
243 | properties => { | |
244 | node => get_standard_option('pve-node'), | |
245 | vmid => get_standard_option('pve-vmid'), | |
246 | }, | |
247 | }, | |
248 | returns => { | |
249 | type => 'array', | |
250 | items => { | |
251 | type => "object", | |
252 | properties => { | |
253 | subdir => { type => 'string' }, | |
254 | }, | |
255 | }, | |
256 | links => [ { rel => 'child', href => "{subdir}" } ], | |
257 | }, | |
258 | code => sub { | |
259 | my ($param) = @_; | |
260 | ||
261 | my $res = [ | |
262 | { subdir => 'config' }, | |
263 | { subdir => 'status' }, | |
264 | { subdir => 'unlink' }, | |
265 | { subdir => 'vncproxy' }, | |
3ea94c60 | 266 | { subdir => 'migrate' }, |
1e3baf05 DM |
267 | { subdir => 'rrd' }, |
268 | { subdir => 'rrddata' }, | |
91c94f0a | 269 | { subdir => 'monitor' }, |
1e3baf05 DM |
270 | ]; |
271 | ||
272 | return $res; | |
273 | }}); | |
274 | ||
275 | __PACKAGE__->register_method({ | |
276 | name => 'rrd', | |
277 | path => '{vmid}/rrd', | |
278 | method => 'GET', | |
279 | protected => 1, # fixme: can we avoid that? | |
280 | permissions => { | |
281 | path => '/vms/{vmid}', | |
282 | privs => [ 'VM.Audit' ], | |
283 | }, | |
284 | description => "Read VM RRD statistics (returns PNG)", | |
285 | parameters => { | |
286 | additionalProperties => 0, | |
287 | properties => { | |
288 | node => get_standard_option('pve-node'), | |
289 | vmid => get_standard_option('pve-vmid'), | |
290 | timeframe => { | |
291 | description => "Specify the time frame you are interested in.", | |
292 | type => 'string', | |
293 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
294 | }, | |
295 | ds => { | |
296 | description => "The list of datasources you want to display.", | |
297 | type => 'string', format => 'pve-configid-list', | |
298 | }, | |
299 | cf => { | |
300 | description => "The RRD consolidation function", | |
301 | type => 'string', | |
302 | enum => [ 'AVERAGE', 'MAX' ], | |
303 | optional => 1, | |
304 | }, | |
305 | }, | |
306 | }, | |
307 | returns => { | |
308 | type => "object", | |
309 | properties => { | |
310 | filename => { type => 'string' }, | |
311 | }, | |
312 | }, | |
313 | code => sub { | |
314 | my ($param) = @_; | |
315 | ||
316 | return PVE::Cluster::create_rrd_graph( | |
317 | "pve2-vm/$param->{vmid}", $param->{timeframe}, | |
318 | $param->{ds}, $param->{cf}); | |
319 | ||
320 | }}); | |
321 | ||
322 | __PACKAGE__->register_method({ | |
323 | name => 'rrddata', | |
324 | path => '{vmid}/rrddata', | |
325 | method => 'GET', | |
326 | protected => 1, # fixme: can we avoid that? | |
327 | permissions => { | |
328 | path => '/vms/{vmid}', | |
329 | privs => [ 'VM.Audit' ], | |
330 | }, | |
331 | description => "Read VM RRD statistics", | |
332 | parameters => { | |
333 | additionalProperties => 0, | |
334 | properties => { | |
335 | node => get_standard_option('pve-node'), | |
336 | vmid => get_standard_option('pve-vmid'), | |
337 | timeframe => { | |
338 | description => "Specify the time frame you are interested in.", | |
339 | type => 'string', | |
340 | enum => [ 'hour', 'day', 'week', 'month', 'year' ], | |
341 | }, | |
342 | cf => { | |
343 | description => "The RRD consolidation function", | |
344 | type => 'string', | |
345 | enum => [ 'AVERAGE', 'MAX' ], | |
346 | optional => 1, | |
347 | }, | |
348 | }, | |
349 | }, | |
350 | returns => { | |
351 | type => "array", | |
352 | items => { | |
353 | type => "object", | |
354 | properties => {}, | |
355 | }, | |
356 | }, | |
357 | code => sub { | |
358 | my ($param) = @_; | |
359 | ||
360 | return PVE::Cluster::create_rrd_data( | |
361 | "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf}); | |
362 | }}); | |
363 | ||
364 | ||
365 | __PACKAGE__->register_method({ | |
366 | name => 'vm_config', | |
367 | path => '{vmid}/config', | |
368 | method => 'GET', | |
369 | proxyto => 'node', | |
370 | description => "Get virtual machine configuration.", | |
371 | parameters => { | |
372 | additionalProperties => 0, | |
373 | properties => { | |
374 | node => get_standard_option('pve-node'), | |
375 | vmid => get_standard_option('pve-vmid'), | |
376 | }, | |
377 | }, | |
378 | returns => { | |
379 | type => "object", | |
554ac7e7 DM |
380 | properties => { |
381 | digest => { | |
382 | type => 'string', | |
383 | description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', | |
384 | } | |
385 | }, | |
1e3baf05 DM |
386 | }, |
387 | code => sub { | |
388 | my ($param) = @_; | |
389 | ||
390 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
391 | ||
392 | return $conf; | |
393 | }}); | |
394 | ||
395 | __PACKAGE__->register_method({ | |
396 | name => 'update_vm', | |
397 | path => '{vmid}/config', | |
398 | method => 'PUT', | |
399 | protected => 1, | |
400 | proxyto => 'node', | |
401 | description => "Set virtual machine options.", | |
402 | parameters => { | |
403 | additionalProperties => 0, | |
404 | properties => PVE::QemuServer::json_config_properties( | |
405 | { | |
406 | node => get_standard_option('pve-node'), | |
407 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 | 408 | skiplock => get_standard_option('skiplock'), |
1e3baf05 DM |
409 | delete => { |
410 | type => 'string', format => 'pve-configid-list', | |
411 | description => "A list of settings you want to delete.", | |
412 | optional => 1, | |
413 | }, | |
414 | force => { | |
415 | type => 'boolean', | |
416 | description => $opt_force_description, | |
417 | optional => 1, | |
418 | requires => 'delete', | |
419 | }, | |
554ac7e7 DM |
420 | digest => { |
421 | type => 'string', | |
422 | description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.', | |
423 | maxLength => 40, | |
424 | optional => 1, | |
425 | } | |
1e3baf05 DM |
426 | }), |
427 | }, | |
428 | returns => { type => 'null'}, | |
429 | code => sub { | |
430 | my ($param) = @_; | |
431 | ||
432 | my $rpcenv = PVE::RPCEnvironment::get(); | |
433 | ||
434 | my $user = $rpcenv->get_user(); | |
435 | ||
436 | my $node = extract_param($param, 'node'); | |
437 | ||
1e3baf05 DM |
438 | my $vmid = extract_param($param, 'vmid'); |
439 | ||
5fdbe4f0 DM |
440 | my $digest = extract_param($param, 'digest'); |
441 | ||
442 | my @paramarr = (); # used for log message | |
443 | foreach my $key (keys %$param) { | |
444 | push @paramarr, "-$key", $param->{$key}; | |
445 | } | |
446 | ||
1e3baf05 | 447 | my $skiplock = extract_param($param, 'skiplock'); |
3ea94c60 DM |
448 | raise_param_exc({ skiplock => "Only root may use this option." }) |
449 | if $skiplock && $user ne 'root@pam'; | |
1e3baf05 DM |
450 | |
451 | my $delete = extract_param($param, 'delete'); | |
452 | my $force = extract_param($param, 'force'); | |
453 | ||
454 | die "no options specified\n" if !$delete && !scalar(keys %$param); | |
455 | ||
456 | my $storecfg = PVE::Storage::config(); | |
457 | ||
458 | &$resolve_cdrom_alias($param); | |
459 | ||
460 | my $eject = {}; | |
461 | my $cdchange = {}; | |
462 | ||
463 | foreach my $opt (keys %$param) { | |
464 | if (PVE::QemuServer::valid_drivename($opt)) { | |
465 | my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
466 | raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive; | |
467 | if ($drive->{file} eq 'eject') { | |
468 | $eject->{$opt} = 1; | |
469 | delete $param->{$opt}; | |
470 | next; | |
471 | } | |
472 | ||
473 | PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive); | |
474 | $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive); | |
475 | ||
476 | if (PVE::QemuServer::drive_is_cdrom($drive)) { | |
477 | $cdchange->{$opt} = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file}); | |
478 | } | |
479 | } | |
480 | } | |
481 | ||
482 | foreach my $opt (PVE::Tools::split_list($delete)) { | |
483 | $opt = 'ide2' if $opt eq 'cdrom'; | |
484 | die "you can't use '-$opt' and '-delete $opt' at the same time\n" | |
485 | if defined($param->{$opt}); | |
486 | } | |
487 | ||
488 | PVE::QemuServer::add_random_macs($param); | |
489 | ||
490 | my $vollist = []; | |
491 | ||
492 | my $updatefn = sub { | |
493 | ||
494 | my $conf = PVE::QemuServer::load_config($vmid); | |
495 | ||
554ac7e7 DM |
496 | die "checksum missmatch (file change by other user?)\n" |
497 | if $digest && $digest ne $conf->{digest}; | |
498 | ||
1e3baf05 DM |
499 | PVE::QemuServer::check_lock($conf) if !$skiplock; |
500 | ||
5fdbe4f0 DM |
501 | PVE::Cluster::log_msg('info', $user, "update VM $vmid: " . join (' ', @paramarr)); |
502 | ||
1e3baf05 DM |
503 | foreach my $opt (keys %$eject) { |
504 | if ($conf->{$opt}) { | |
505 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
506 | $cdchange->{$opt} = undef if PVE::QemuServer::drive_is_cdrom($drive); | |
507 | } else { | |
508 | raise_param_exc({ $opt => "eject failed - drive does not exist." }); | |
509 | } | |
510 | } | |
511 | ||
512 | foreach my $opt (keys %$param) { | |
513 | next if !PVE::QemuServer::valid_drivename($opt); | |
514 | next if !$conf->{$opt}; | |
515 | my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); | |
516 | next if PVE::QemuServer::drive_is_cdrom($old_drive); | |
517 | my $new_drive = PVE::QemuServer::parse_drive($opt, $param->{$opt}); | |
518 | if ($new_drive->{file} ne $old_drive->{file}) { | |
519 | my ($path, $owner); | |
520 | eval { ($path, $owner) = PVE::Storage::path($storecfg, $old_drive->{file}); }; | |
521 | if ($owner && ($owner == $vmid)) { | |
522 | PVE::QemuServer::add_unused_volume($conf, $param, $old_drive->{file}); | |
523 | } | |
524 | } | |
525 | } | |
526 | ||
527 | my $unset = {}; | |
528 | ||
529 | foreach my $opt (PVE::Tools::split_list($delete)) { | |
530 | $opt = 'ide2' if $opt eq 'cdrom'; | |
531 | if (!PVE::QemuServer::option_exists($opt)) { | |
532 | raise_param_exc({ delete => "unknown option '$opt'" }); | |
533 | } | |
534 | next if !defined($conf->{$opt}); | |
535 | if (PVE::QemuServer::valid_drivename($opt)) { | |
f19d1c47 | 536 | PVE::QemuServer::vm_devicedel($vmid, $conf, $opt); |
1e3baf05 DM |
537 | my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt}); |
538 | if (PVE::QemuServer::drive_is_cdrom($drive)) { | |
539 | $cdchange->{$opt} = undef; | |
540 | } else { | |
541 | my $volid = $drive->{file}; | |
542 | ||
543 | if ($volid !~ m|^/|) { | |
544 | my ($path, $owner); | |
545 | eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); }; | |
546 | if ($owner && ($owner == $vmid)) { | |
547 | if ($force) { | |
548 | push @$vollist, $volid; | |
549 | } else { | |
550 | PVE::QemuServer::add_unused_volume($conf, $param, $volid); | |
551 | } | |
552 | } | |
553 | } | |
554 | } | |
555 | } elsif ($opt =~ m/^unused/) { | |
556 | push @$vollist, $conf->{$opt}; | |
557 | } | |
558 | ||
559 | $unset->{$opt} = 1; | |
560 | } | |
561 | ||
f19d1c47 | 562 | PVE::QemuServer::create_disks($storecfg, $vmid, $param, $conf); |
1e3baf05 DM |
563 | |
564 | PVE::QemuServer::change_config_nolock($vmid, $param, $unset, 1); | |
565 | ||
566 | return if !PVE::QemuServer::check_running($vmid); | |
567 | ||
568 | foreach my $opt (keys %$cdchange) { | |
569 | my $qdn = PVE::QemuServer::qemu_drive_name($opt, 'cdrom'); | |
570 | my $path = $cdchange->{$opt}; | |
571 | PVE::QemuServer::vm_monitor_command($vmid, "eject $qdn", 0); | |
572 | PVE::QemuServer::vm_monitor_command($vmid, "change $qdn \"$path\"", 0) if $path; | |
573 | } | |
574 | }; | |
575 | ||
576 | PVE::QemuServer::lock_config($vmid, $updatefn); | |
577 | ||
578 | foreach my $volid (@$vollist) { | |
579 | eval { PVE::Storage::vdisk_free($storecfg, $volid); }; | |
580 | # fixme: log ? | |
581 | warn $@ if $@; | |
582 | } | |
583 | ||
584 | return undef; | |
585 | }}); | |
586 | ||
587 | ||
588 | __PACKAGE__->register_method({ | |
589 | name => 'destroy_vm', | |
590 | path => '{vmid}', | |
591 | method => 'DELETE', | |
592 | protected => 1, | |
593 | proxyto => 'node', | |
594 | description => "Destroy the vm (also delete all used/owned volumes).", | |
595 | parameters => { | |
596 | additionalProperties => 0, | |
597 | properties => { | |
598 | node => get_standard_option('pve-node'), | |
599 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 | 600 | skiplock => get_standard_option('skiplock'), |
1e3baf05 DM |
601 | }, |
602 | }, | |
5fdbe4f0 DM |
603 | returns => { |
604 | type => 'string', | |
605 | }, | |
1e3baf05 DM |
606 | code => sub { |
607 | my ($param) = @_; | |
608 | ||
609 | my $rpcenv = PVE::RPCEnvironment::get(); | |
610 | ||
611 | my $user = $rpcenv->get_user(); | |
612 | ||
613 | my $vmid = $param->{vmid}; | |
614 | ||
615 | my $skiplock = $param->{skiplock}; | |
616 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
3ea94c60 | 617 | if $skiplock && $user ne 'root@pam'; |
1e3baf05 | 618 | |
5fdbe4f0 DM |
619 | # test if VM exists |
620 | my $conf = PVE::QemuServer::load_config($vmid); | |
621 | ||
1e3baf05 DM |
622 | my $storecfg = PVE::Storage::config(); |
623 | ||
5fdbe4f0 DM |
624 | my $realcmd = sub { |
625 | PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); | |
626 | }; | |
1e3baf05 | 627 | |
5fdbe4f0 | 628 | return $rpcenv->fork_worker('qmdestroy', $vmid, $user, $realcmd); |
1e3baf05 DM |
629 | }}); |
630 | ||
631 | __PACKAGE__->register_method({ | |
632 | name => 'unlink', | |
633 | path => '{vmid}/unlink', | |
634 | method => 'PUT', | |
635 | protected => 1, | |
636 | proxyto => 'node', | |
637 | description => "Unlink/delete disk images.", | |
638 | parameters => { | |
639 | additionalProperties => 0, | |
640 | properties => { | |
641 | node => get_standard_option('pve-node'), | |
642 | vmid => get_standard_option('pve-vmid'), | |
643 | idlist => { | |
644 | type => 'string', format => 'pve-configid-list', | |
645 | description => "A list of disk IDs you want to delete.", | |
646 | }, | |
647 | force => { | |
648 | type => 'boolean', | |
649 | description => $opt_force_description, | |
650 | optional => 1, | |
651 | }, | |
652 | }, | |
653 | }, | |
654 | returns => { type => 'null'}, | |
655 | code => sub { | |
656 | my ($param) = @_; | |
657 | ||
658 | $param->{delete} = extract_param($param, 'idlist'); | |
659 | ||
660 | __PACKAGE__->update_vm($param); | |
661 | ||
662 | return undef; | |
663 | }}); | |
664 | ||
665 | my $sslcert; | |
666 | ||
667 | __PACKAGE__->register_method({ | |
668 | name => 'vncproxy', | |
669 | path => '{vmid}/vncproxy', | |
670 | method => 'POST', | |
671 | protected => 1, | |
672 | permissions => { | |
673 | path => '/vms/{vmid}', | |
674 | privs => [ 'VM.Console' ], | |
675 | }, | |
676 | description => "Creates a TCP VNC proxy connections.", | |
677 | parameters => { | |
678 | additionalProperties => 0, | |
679 | properties => { | |
680 | node => get_standard_option('pve-node'), | |
681 | vmid => get_standard_option('pve-vmid'), | |
682 | }, | |
683 | }, | |
684 | returns => { | |
685 | additionalProperties => 0, | |
686 | properties => { | |
687 | user => { type => 'string' }, | |
688 | ticket => { type => 'string' }, | |
689 | cert => { type => 'string' }, | |
690 | port => { type => 'integer' }, | |
691 | upid => { type => 'string' }, | |
692 | }, | |
693 | }, | |
694 | code => sub { | |
695 | my ($param) = @_; | |
696 | ||
697 | my $rpcenv = PVE::RPCEnvironment::get(); | |
698 | ||
699 | my $user = $rpcenv->get_user(); | |
700 | my $ticket = PVE::AccessControl::assemble_ticket($user); | |
701 | ||
702 | my $vmid = $param->{vmid}; | |
703 | my $node = $param->{node}; | |
704 | ||
705 | $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192) | |
706 | if !$sslcert; | |
707 | ||
708 | my $port = PVE::Tools::next_vnc_port(); | |
709 | ||
710 | my $remip; | |
711 | ||
4f1be36c | 712 | if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) { |
1e3baf05 DM |
713 | $remip = PVE::Cluster::remote_node_ip($node); |
714 | } | |
715 | ||
716 | # NOTE: kvm VNC traffic is already TLS encrypted, | |
717 | # so we select the fastest chipher here (or 'none'?) | |
718 | my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes', | |
719 | '-c', 'blowfish-cbc', $remip] : []; | |
720 | ||
721 | my $timeout = 10; | |
722 | ||
723 | my $realcmd = sub { | |
724 | my $upid = shift; | |
725 | ||
726 | syslog('info', "starting vnc proxy $upid\n"); | |
727 | ||
728 | my $qmcmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid]; | |
729 | ||
730 | my $qmstr = join(' ', @$qmcmd); | |
731 | ||
732 | # also redirect stderr (else we get RFB protocol errors) | |
be62c45c | 733 | my $cmd = ['/bin/nc', '-l', '-p', $port, '-w', $timeout, '-c', "$qmstr 2>/dev/null"]; |
1e3baf05 | 734 | |
be62c45c | 735 | PVE::Tools::run_command($cmd); |
1e3baf05 DM |
736 | |
737 | return; | |
738 | }; | |
739 | ||
740 | my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $user, $realcmd); | |
741 | ||
742 | return { | |
743 | user => $user, | |
744 | ticket => $ticket, | |
745 | port => $port, | |
746 | upid => $upid, | |
747 | cert => $sslcert, | |
748 | }; | |
749 | }}); | |
750 | ||
5fdbe4f0 DM |
751 | __PACKAGE__->register_method({ |
752 | name => 'vmcmdidx', | |
753 | path => '{vmid}/status', | |
754 | method => 'GET', | |
755 | proxyto => 'node', | |
756 | description => "Directory index", | |
757 | parameters => { | |
758 | additionalProperties => 0, | |
759 | properties => { | |
760 | node => get_standard_option('pve-node'), | |
761 | vmid => get_standard_option('pve-vmid'), | |
762 | }, | |
763 | }, | |
764 | returns => { | |
765 | type => 'array', | |
766 | items => { | |
767 | type => "object", | |
768 | properties => { | |
769 | subdir => { type => 'string' }, | |
770 | }, | |
771 | }, | |
772 | links => [ { rel => 'child', href => "{subdir}" } ], | |
773 | }, | |
774 | code => sub { | |
775 | my ($param) = @_; | |
776 | ||
777 | # test if VM exists | |
778 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
779 | ||
780 | my $res = [ | |
781 | { subdir => 'current' }, | |
782 | { subdir => 'start' }, | |
783 | { subdir => 'stop' }, | |
784 | ]; | |
785 | ||
786 | return $res; | |
787 | }}); | |
788 | ||
1e3baf05 DM |
789 | __PACKAGE__->register_method({ |
790 | name => 'vm_status', | |
5fdbe4f0 | 791 | path => '{vmid}/status/current', |
1e3baf05 DM |
792 | method => 'GET', |
793 | proxyto => 'node', | |
794 | protected => 1, # qemu pid files are only readable by root | |
795 | description => "Get virtual machine status.", | |
796 | parameters => { | |
797 | additionalProperties => 0, | |
798 | properties => { | |
799 | node => get_standard_option('pve-node'), | |
800 | vmid => get_standard_option('pve-vmid'), | |
801 | }, | |
802 | }, | |
803 | returns => { type => 'object' }, | |
804 | code => sub { | |
805 | my ($param) = @_; | |
806 | ||
807 | # test if VM exists | |
808 | my $conf = PVE::QemuServer::load_config($param->{vmid}); | |
809 | ||
810 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}); | |
811 | ||
812 | return $vmstatus->{$param->{vmid}}; | |
813 | }}); | |
814 | ||
815 | __PACKAGE__->register_method({ | |
5fdbe4f0 DM |
816 | name => 'vm_start', |
817 | path => '{vmid}/status/start', | |
818 | method => 'POST', | |
1e3baf05 DM |
819 | protected => 1, |
820 | proxyto => 'node', | |
5fdbe4f0 | 821 | description => "Start virtual machine.", |
1e3baf05 DM |
822 | parameters => { |
823 | additionalProperties => 0, | |
824 | properties => { | |
825 | node => get_standard_option('pve-node'), | |
826 | vmid => get_standard_option('pve-vmid'), | |
3ea94c60 DM |
827 | skiplock => get_standard_option('skiplock'), |
828 | stateuri => get_standard_option('pve-qm-stateuri'), | |
1e3baf05 DM |
829 | }, |
830 | }, | |
5fdbe4f0 DM |
831 | returns => { |
832 | type => 'string', | |
833 | }, | |
1e3baf05 DM |
834 | code => sub { |
835 | my ($param) = @_; | |
836 | ||
837 | my $rpcenv = PVE::RPCEnvironment::get(); | |
838 | ||
839 | my $user = $rpcenv->get_user(); | |
840 | ||
841 | my $node = extract_param($param, 'node'); | |
842 | ||
1e3baf05 DM |
843 | my $vmid = extract_param($param, 'vmid'); |
844 | ||
3ea94c60 DM |
845 | my $stateuri = extract_param($param, 'stateuri'); |
846 | raise_param_exc({ stateuri => "Only root may use this option." }) | |
847 | if $stateuri && $user ne 'root@pam'; | |
848 | ||
1e3baf05 DM |
849 | my $skiplock = extract_param($param, 'skiplock'); |
850 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
3ea94c60 | 851 | if $skiplock && $user ne 'root@pam'; |
1e3baf05 | 852 | |
1e3baf05 | 853 | my $storecfg = PVE::Storage::config(); |
5fdbe4f0 DM |
854 | |
855 | my $realcmd = sub { | |
856 | my $upid = shift; | |
857 | ||
858 | syslog('info', "start VM $vmid: $upid\n"); | |
859 | ||
3ea94c60 | 860 | PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock); |
5fdbe4f0 DM |
861 | |
862 | return; | |
863 | }; | |
864 | ||
865 | return $rpcenv->fork_worker('qmstart', $vmid, $user, $realcmd); | |
866 | }}); | |
867 | ||
868 | __PACKAGE__->register_method({ | |
869 | name => 'vm_stop', | |
870 | path => '{vmid}/status/stop', | |
871 | method => 'POST', | |
872 | protected => 1, | |
873 | proxyto => 'node', | |
874 | description => "Stop virtual machine.", | |
875 | parameters => { | |
876 | additionalProperties => 0, | |
877 | properties => { | |
878 | node => get_standard_option('pve-node'), | |
879 | vmid => get_standard_option('pve-vmid'), | |
880 | skiplock => get_standard_option('skiplock'), | |
c6bb9502 DM |
881 | timeout => { |
882 | description => "Wait maximal timeout seconds.", | |
883 | type => 'integer', | |
884 | minimum => 0, | |
885 | optional => 1, | |
886 | } | |
5fdbe4f0 DM |
887 | }, |
888 | }, | |
889 | returns => { | |
890 | type => 'string', | |
891 | }, | |
892 | code => sub { | |
893 | my ($param) = @_; | |
894 | ||
895 | my $rpcenv = PVE::RPCEnvironment::get(); | |
896 | ||
897 | my $user = $rpcenv->get_user(); | |
898 | ||
899 | my $node = extract_param($param, 'node'); | |
900 | ||
901 | my $vmid = extract_param($param, 'vmid'); | |
902 | ||
903 | my $skiplock = extract_param($param, 'skiplock'); | |
904 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
905 | if $skiplock && $user ne 'root@pam'; | |
906 | ||
907 | my $realcmd = sub { | |
908 | my $upid = shift; | |
909 | ||
910 | syslog('info', "stop VM $vmid: $upid\n"); | |
911 | ||
1e3baf05 | 912 | PVE::QemuServer::vm_stop($vmid, $skiplock); |
5fdbe4f0 | 913 | |
c6bb9502 DM |
914 | my $pid = PVE::QemuServer::check_running ($vmid); |
915 | ||
916 | if ($pid && $param->{timeout}) { | |
917 | print "waiting until VM $vmid stopps (PID $pid)\n"; | |
918 | ||
919 | my $count = 0; | |
920 | while (($count < $param->{timeout}) && | |
921 | PVE::QemuServer::check_running($vmid)) { | |
922 | $count++; | |
923 | sleep 1; | |
924 | } | |
925 | ||
926 | die "wait failed - got timeout\n" if PVE::QemuServer::check_running($vmid); | |
927 | } | |
928 | ||
5fdbe4f0 DM |
929 | return; |
930 | }; | |
931 | ||
932 | return $rpcenv->fork_worker('qmstop', $vmid, $user, $realcmd); | |
933 | }}); | |
934 | ||
935 | __PACKAGE__->register_method({ | |
936 | name => 'vm_reset', | |
937 | path => '{vmid}/status/reset', | |
938 | method => 'POST', | |
939 | protected => 1, | |
940 | proxyto => 'node', | |
941 | description => "Reset virtual machine.", | |
942 | parameters => { | |
943 | additionalProperties => 0, | |
944 | properties => { | |
945 | node => get_standard_option('pve-node'), | |
946 | vmid => get_standard_option('pve-vmid'), | |
947 | skiplock => get_standard_option('skiplock'), | |
948 | }, | |
949 | }, | |
950 | returns => { | |
951 | type => 'string', | |
952 | }, | |
953 | code => sub { | |
954 | my ($param) = @_; | |
955 | ||
956 | my $rpcenv = PVE::RPCEnvironment::get(); | |
957 | ||
958 | my $user = $rpcenv->get_user(); | |
959 | ||
960 | my $node = extract_param($param, 'node'); | |
961 | ||
962 | my $vmid = extract_param($param, 'vmid'); | |
963 | ||
964 | my $skiplock = extract_param($param, 'skiplock'); | |
965 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
966 | if $skiplock && $user ne 'root@pam'; | |
967 | ||
968 | my $realcmd = sub { | |
969 | my $upid = shift; | |
970 | ||
971 | syslog('info', "reset VM $vmid: $upid\n"); | |
972 | ||
1e3baf05 | 973 | PVE::QemuServer::vm_reset($vmid, $skiplock); |
5fdbe4f0 DM |
974 | |
975 | return; | |
976 | }; | |
977 | ||
978 | return $rpcenv->fork_worker('qmreset', $vmid, $user, $realcmd); | |
979 | }}); | |
980 | ||
981 | __PACKAGE__->register_method({ | |
982 | name => 'vm_shutdown', | |
983 | path => '{vmid}/status/shutdown', | |
984 | method => 'POST', | |
985 | protected => 1, | |
986 | proxyto => 'node', | |
987 | description => "Shutdown virtual machine.", | |
988 | parameters => { | |
989 | additionalProperties => 0, | |
990 | properties => { | |
991 | node => get_standard_option('pve-node'), | |
992 | vmid => get_standard_option('pve-vmid'), | |
993 | skiplock => get_standard_option('skiplock'), | |
c6bb9502 DM |
994 | timeout => { |
995 | description => "Wait maximal timeout seconds.", | |
996 | type => 'integer', | |
997 | minimum => 0, | |
998 | optional => 1, | |
999 | } | |
5fdbe4f0 DM |
1000 | }, |
1001 | }, | |
1002 | returns => { | |
1003 | type => 'string', | |
1004 | }, | |
1005 | code => sub { | |
1006 | my ($param) = @_; | |
1007 | ||
1008 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1009 | ||
1010 | my $user = $rpcenv->get_user(); | |
1011 | ||
1012 | my $node = extract_param($param, 'node'); | |
1013 | ||
1014 | my $vmid = extract_param($param, 'vmid'); | |
1015 | ||
1016 | my $skiplock = extract_param($param, 'skiplock'); | |
1017 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1018 | if $skiplock && $user ne 'root@pam'; | |
1019 | ||
1020 | my $realcmd = sub { | |
1021 | my $upid = shift; | |
1022 | ||
1023 | syslog('info', "shutdown VM $vmid: $upid\n"); | |
1024 | ||
1e3baf05 | 1025 | PVE::QemuServer::vm_shutdown($vmid, $skiplock); |
5fdbe4f0 | 1026 | |
c6bb9502 DM |
1027 | my $pid = PVE::QemuServer::check_running ($vmid); |
1028 | ||
1029 | if ($pid && $param->{timeout}) { | |
1030 | print "waiting until VM $vmid stopps (PID $pid)\n"; | |
1031 | ||
1032 | my $count = 0; | |
1033 | while (($count < $param->{timeout}) && | |
1034 | PVE::QemuServer::check_running($vmid)) { | |
1035 | $count++; | |
1036 | sleep 1; | |
1037 | } | |
1038 | ||
1039 | die "wait failed - got timeout\n" if PVE::QemuServer::check_running($vmid); | |
1040 | } | |
1041 | ||
5fdbe4f0 DM |
1042 | return; |
1043 | }; | |
1044 | ||
1045 | return $rpcenv->fork_worker('qmshutdown', $vmid, $user, $realcmd); | |
1046 | }}); | |
1047 | ||
1048 | __PACKAGE__->register_method({ | |
1049 | name => 'vm_suspend', | |
1050 | path => '{vmid}/status/suspend', | |
1051 | method => 'POST', | |
1052 | protected => 1, | |
1053 | proxyto => 'node', | |
1054 | description => "Suspend virtual machine.", | |
1055 | parameters => { | |
1056 | additionalProperties => 0, | |
1057 | properties => { | |
1058 | node => get_standard_option('pve-node'), | |
1059 | vmid => get_standard_option('pve-vmid'), | |
1060 | skiplock => get_standard_option('skiplock'), | |
1061 | }, | |
1062 | }, | |
1063 | returns => { | |
1064 | type => 'string', | |
1065 | }, | |
1066 | code => sub { | |
1067 | my ($param) = @_; | |
1068 | ||
1069 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1070 | ||
1071 | my $user = $rpcenv->get_user(); | |
1072 | ||
1073 | my $node = extract_param($param, 'node'); | |
1074 | ||
1075 | my $vmid = extract_param($param, 'vmid'); | |
1076 | ||
1077 | my $skiplock = extract_param($param, 'skiplock'); | |
1078 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1079 | if $skiplock && $user ne 'root@pam'; | |
1080 | ||
1081 | my $realcmd = sub { | |
1082 | my $upid = shift; | |
1083 | ||
1084 | syslog('info', "suspend VM $vmid: $upid\n"); | |
1085 | ||
1e3baf05 | 1086 | PVE::QemuServer::vm_suspend($vmid, $skiplock); |
5fdbe4f0 DM |
1087 | |
1088 | return; | |
1089 | }; | |
1090 | ||
1091 | return $rpcenv->fork_worker('qmsuspend', $vmid, $user, $realcmd); | |
1092 | }}); | |
1093 | ||
1094 | __PACKAGE__->register_method({ | |
1095 | name => 'vm_resume', | |
1096 | path => '{vmid}/status/resume', | |
1097 | method => 'POST', | |
1098 | protected => 1, | |
1099 | proxyto => 'node', | |
1100 | description => "Resume virtual machine.", | |
1101 | parameters => { | |
1102 | additionalProperties => 0, | |
1103 | properties => { | |
1104 | node => get_standard_option('pve-node'), | |
1105 | vmid => get_standard_option('pve-vmid'), | |
1106 | skiplock => get_standard_option('skiplock'), | |
1107 | }, | |
1108 | }, | |
1109 | returns => { | |
1110 | type => 'string', | |
1111 | }, | |
1112 | code => sub { | |
1113 | my ($param) = @_; | |
1114 | ||
1115 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1116 | ||
1117 | my $user = $rpcenv->get_user(); | |
1118 | ||
1119 | my $node = extract_param($param, 'node'); | |
1120 | ||
1121 | my $vmid = extract_param($param, 'vmid'); | |
1122 | ||
1123 | my $skiplock = extract_param($param, 'skiplock'); | |
1124 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1125 | if $skiplock && $user ne 'root@pam'; | |
1126 | ||
1127 | my $realcmd = sub { | |
1128 | my $upid = shift; | |
1129 | ||
1130 | syslog('info', "resume VM $vmid: $upid\n"); | |
1131 | ||
1e3baf05 | 1132 | PVE::QemuServer::vm_resume($vmid, $skiplock); |
1e3baf05 | 1133 | |
5fdbe4f0 DM |
1134 | return; |
1135 | }; | |
1136 | ||
1137 | return $rpcenv->fork_worker('qmresume', $vmid, $user, $realcmd); | |
1138 | }}); | |
1139 | ||
1140 | __PACKAGE__->register_method({ | |
1141 | name => 'vm_sendkey', | |
1142 | path => '{vmid}/sendkey', | |
1143 | method => 'PUT', | |
1144 | protected => 1, | |
1145 | proxyto => 'node', | |
1146 | description => "Send key event to virtual machine.", | |
1147 | parameters => { | |
1148 | additionalProperties => 0, | |
1149 | properties => { | |
1150 | node => get_standard_option('pve-node'), | |
1151 | vmid => get_standard_option('pve-vmid'), | |
1152 | skiplock => get_standard_option('skiplock'), | |
1153 | key => { | |
1154 | description => "The key (qemu monitor encoding).", | |
1155 | type => 'string' | |
1156 | } | |
1157 | }, | |
1158 | }, | |
1159 | returns => { type => 'null'}, | |
1160 | code => sub { | |
1161 | my ($param) = @_; | |
1162 | ||
1163 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1164 | ||
1165 | my $user = $rpcenv->get_user(); | |
1166 | ||
1167 | my $node = extract_param($param, 'node'); | |
1168 | ||
1169 | my $vmid = extract_param($param, 'vmid'); | |
1170 | ||
1171 | my $skiplock = extract_param($param, 'skiplock'); | |
1172 | raise_param_exc({ skiplock => "Only root may use this option." }) | |
1173 | if $skiplock && $user ne 'root@pam'; | |
1174 | ||
1175 | PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key}); | |
1176 | ||
1177 | return; | |
1e3baf05 DM |
1178 | }}); |
1179 | ||
3ea94c60 DM |
1180 | __PACKAGE__->register_method({ |
1181 | name => 'migrate_vm', | |
1182 | path => '{vmid}/migrate', | |
1183 | method => 'POST', | |
1184 | protected => 1, | |
1185 | proxyto => 'node', | |
1186 | description => "Migrate virtual machine. Creates a new migration task.", | |
1187 | parameters => { | |
1188 | additionalProperties => 0, | |
1189 | properties => { | |
1190 | node => get_standard_option('pve-node'), | |
1191 | vmid => get_standard_option('pve-vmid'), | |
1192 | target => get_standard_option('pve-node', { description => "Target node." }), | |
1193 | online => { | |
1194 | type => 'boolean', | |
1195 | description => "Use online/live migration.", | |
1196 | optional => 1, | |
1197 | }, | |
1198 | force => { | |
1199 | type => 'boolean', | |
1200 | description => "Allow to migrate VMs which use local devices. Only root may use this option.", | |
1201 | optional => 1, | |
1202 | }, | |
1203 | }, | |
1204 | }, | |
1205 | returns => { | |
1206 | type => 'string', | |
1207 | description => "the task ID.", | |
1208 | }, | |
1209 | code => sub { | |
1210 | my ($param) = @_; | |
1211 | ||
1212 | my $rpcenv = PVE::RPCEnvironment::get(); | |
1213 | ||
1214 | my $user = $rpcenv->get_user(); | |
1215 | ||
1216 | my $target = extract_param($param, 'target'); | |
1217 | ||
1218 | my $localnode = PVE::INotify::nodename(); | |
1219 | raise_param_exc({ target => "target is local node."}) if $target eq $localnode; | |
1220 | ||
1221 | PVE::Cluster::check_cfs_quorum(); | |
1222 | ||
1223 | PVE::Cluster::check_node_exists($target); | |
1224 | ||
1225 | my $targetip = PVE::Cluster::remote_node_ip($target); | |
1226 | ||
1227 | my $vmid = extract_param($param, 'vmid'); | |
1228 | ||
a591eeba DM |
1229 | raise_param_exc({ force => "Only root may use this option." }) |
1230 | if $param->{force} && $user ne 'root@pam'; | |
3ea94c60 DM |
1231 | |
1232 | # test if VM exists | |
1233 | PVE::QemuServer::load_config($vmid); | |
1234 | ||
1235 | # try to detect errors early | |
1236 | if (PVE::QemuServer::check_running($vmid)) { | |
1237 | die "cant migrate running VM without --online\n" | |
1238 | if !$param->{online}; | |
1239 | } | |
1240 | ||
1241 | my $realcmd = sub { | |
1242 | my $upid = shift; | |
1243 | ||
1244 | PVE::QemuMigrate::migrate($target, $targetip, $vmid, $param->{online}, $param->{force}); | |
1245 | }; | |
1246 | ||
1247 | my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $user, $realcmd); | |
1248 | ||
1249 | return $upid; | |
1250 | }}); | |
1e3baf05 | 1251 | |
91c94f0a DM |
1252 | __PACKAGE__->register_method({ |
1253 | name => 'monitor', | |
1254 | path => '{vmid}/monitor', | |
1255 | method => 'POST', | |
1256 | protected => 1, | |
1257 | proxyto => 'node', | |
1258 | description => "Execute Qemu monitor commands.", | |
1259 | parameters => { | |
1260 | additionalProperties => 0, | |
1261 | properties => { | |
1262 | node => get_standard_option('pve-node'), | |
1263 | vmid => get_standard_option('pve-vmid'), | |
1264 | command => { | |
1265 | type => 'string', | |
1266 | description => "The monitor command.", | |
1267 | } | |
1268 | }, | |
1269 | }, | |
1270 | returns => { type => 'string'}, | |
1271 | code => sub { | |
1272 | my ($param) = @_; | |
1273 | ||
1274 | my $vmid = $param->{vmid}; | |
1275 | ||
1276 | my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists | |
1277 | ||
1278 | my $res = ''; | |
1279 | eval { | |
1280 | $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command}); | |
1281 | }; | |
1282 | $res = "ERROR: $@" if $@; | |
1283 | ||
1284 | return $res; | |
1285 | }}); | |
1286 | ||
1e3baf05 | 1287 | 1; |