]>
Commit | Line | Data |
---|---|---|
f3e76e36 DM |
1 | package PVE::CLI::qm; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | # Note: disable '+' prefix for Getopt::Long (for resize command) | |
7 | use Getopt::Long qw(:config no_getopt_compat); | |
8 | ||
9 | use Fcntl ':flock'; | |
10 | use File::Path; | |
11 | use IO::Socket::UNIX; | |
12 | use IO::Select; | |
3351aacc | 13 | use URI::Escape; |
f3e76e36 DM |
14 | |
15 | use PVE::Tools qw(extract_param); | |
16 | use PVE::Cluster; | |
17 | use PVE::SafeSyslog; | |
18 | use PVE::INotify; | |
19 | use PVE::RPCEnvironment; | |
520884de | 20 | use PVE::Exception qw(raise_param_exc); |
3ea84aeb | 21 | use PVE::Network; |
f3e76e36 | 22 | use PVE::QemuServer; |
8653feeb | 23 | use PVE::QemuServer::ImportDisk; |
7cd9f6d7 | 24 | use PVE::QemuServer::OVF; |
520884de | 25 | use PVE::QemuServer::Agent qw(agent_available); |
f3e76e36 | 26 | use PVE::API2::Qemu; |
b8158701 | 27 | use PVE::API2::Qemu::Agent; |
788a6a35 | 28 | use JSON; |
f3e76e36 DM |
29 | use PVE::JSONSchema qw(get_standard_option); |
30 | use Term::ReadLine; | |
31 | ||
32 | use PVE::CLIHandler; | |
33 | ||
34 | use base qw(PVE::CLIHandler); | |
35 | ||
36 | my $upid_exit = sub { | |
37 | my $upid = shift; | |
38 | my $status = PVE::Tools::upid_read_status($upid); | |
39 | exit($status eq 'OK' ? 0 : -1); | |
40 | }; | |
41 | ||
42 | my $nodename = PVE::INotify::nodename(); | |
43 | ||
5e4035c7 DM |
44 | sub setup_environment { |
45 | PVE::RPCEnvironment->setup_default_cli_env(); | |
46 | } | |
47 | ||
f3e76e36 DM |
48 | sub run_vnc_proxy { |
49 | my ($path) = @_; | |
50 | ||
51 | my $c; | |
52 | while ( ++$c < 10 && !-e $path ) { sleep(1); } | |
53 | ||
54 | my $s = IO::Socket::UNIX->new(Peer => $path, Timeout => 120); | |
55 | ||
56 | die "unable to connect to socket '$path' - $!" if !$s; | |
57 | ||
58 | my $select = new IO::Select; | |
59 | ||
60 | $select->add(\*STDIN); | |
61 | $select->add($s); | |
62 | ||
63 | my $timeout = 60*15; # 15 minutes | |
64 | ||
65 | my @handles; | |
66 | while ($select->count && | |
67 | scalar(@handles = $select->can_read ($timeout))) { | |
68 | foreach my $h (@handles) { | |
69 | my $buf; | |
70 | my $n = $h->sysread($buf, 4096); | |
71 | ||
72 | if ($h == \*STDIN) { | |
73 | if ($n) { | |
74 | syswrite($s, $buf); | |
75 | } else { | |
76 | exit(0); | |
77 | } | |
78 | } elsif ($h == $s) { | |
79 | if ($n) { | |
80 | syswrite(\*STDOUT, $buf); | |
81 | } else { | |
82 | exit(0); | |
83 | } | |
84 | } | |
85 | } | |
86 | } | |
87 | exit(0); | |
88 | } | |
89 | ||
81fff836 DC |
90 | sub print_recursive_hash { |
91 | my ($prefix, $hash, $key) = @_; | |
92 | ||
93 | if (ref($hash) eq 'HASH') { | |
94 | if (defined($key)) { | |
95 | print "$prefix$key:\n"; | |
96 | } | |
97 | foreach my $itemkey (keys %$hash) { | |
98 | print_recursive_hash("\t$prefix", $hash->{$itemkey}, $itemkey); | |
99 | } | |
100 | } elsif (ref($hash) eq 'ARRAY') { | |
101 | if (defined($key)) { | |
102 | print "$prefix$key:\n"; | |
103 | } | |
104 | foreach my $item (@$hash) { | |
105 | print_recursive_hash("\t$prefix", $item); | |
106 | } | |
107 | } elsif (!ref($hash) && defined($hash)) { | |
108 | if (defined($key)) { | |
109 | print "$prefix$key: $hash\n"; | |
110 | } else { | |
111 | print "$prefix$hash\n"; | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
f3e76e36 DM |
116 | __PACKAGE__->register_method ({ |
117 | name => 'showcmd', | |
118 | path => 'showcmd', | |
119 | method => 'GET', | |
120 | description => "Show command line which is used to start the VM (debug info).", | |
121 | parameters => { | |
122 | additionalProperties => 0, | |
123 | properties => { | |
335af808 | 124 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
16a01738 TL |
125 | pretty => { |
126 | description => "Puts each option on a new line to enhance human readability", | |
127 | type => 'boolean', | |
128 | optional => 1, | |
129 | default => 0, | |
b14477e7 RV |
130 | }, |
131 | snapshot => get_standard_option('pve-snapshot-name', { | |
132 | description => "Fetch config values from given snapshot.", | |
133 | optional => 1, | |
134 | completion => sub { | |
135 | my ($cmd, $pname, $cur, $args) = @_; | |
136 | PVE::QemuConfig->snapshot_list($args->[0]); | |
137 | } | |
138 | }), | |
f3e76e36 DM |
139 | }, |
140 | }, | |
141 | returns => { type => 'null'}, | |
142 | code => sub { | |
143 | my ($param) = @_; | |
144 | ||
145 | my $storecfg = PVE::Storage::config(); | |
b14477e7 | 146 | my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $param->{vmid}, $param->{snapshot}); |
16a01738 | 147 | |
108899b4 | 148 | $cmdline =~ s/ -/ \\\n -/g if $param->{pretty}; |
16a01738 TL |
149 | |
150 | print "$cmdline\n"; | |
f3e76e36 DM |
151 | |
152 | return undef; | |
153 | }}); | |
154 | ||
155 | __PACKAGE__->register_method ({ | |
156 | name => 'status', | |
157 | path => 'status', | |
158 | method => 'GET', | |
159 | description => "Show VM status.", | |
160 | parameters => { | |
161 | additionalProperties => 0, | |
162 | properties => { | |
335af808 | 163 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
f3e76e36 DM |
164 | verbose => { |
165 | description => "Verbose output format", | |
166 | type => 'boolean', | |
167 | optional => 1, | |
168 | } | |
169 | }, | |
170 | }, | |
171 | returns => { type => 'null'}, | |
172 | code => sub { | |
173 | my ($param) = @_; | |
174 | ||
175 | # test if VM exists | |
ffda963f | 176 | my $conf = PVE::QemuConfig->load_config ($param->{vmid}); |
f3e76e36 | 177 | |
12612b09 | 178 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); |
f3e76e36 DM |
179 | my $stat = $vmstatus->{$param->{vmid}}; |
180 | if ($param->{verbose}) { | |
181 | foreach my $k (sort (keys %$stat)) { | |
182 | next if $k eq 'cpu' || $k eq 'relcpu'; # always 0 | |
183 | my $v = $stat->{$k}; | |
81fff836 | 184 | print_recursive_hash("", $v, $k); |
f3e76e36 DM |
185 | } |
186 | } else { | |
12612b09 | 187 | my $status = $stat->{qmpstatus} || 'unknown'; |
f3e76e36 DM |
188 | print "status: $status\n"; |
189 | } | |
190 | ||
191 | return undef; | |
192 | }}); | |
193 | ||
194 | __PACKAGE__->register_method ({ | |
195 | name => 'vncproxy', | |
196 | path => 'vncproxy', | |
197 | method => 'PUT', | |
198 | description => "Proxy VM VNC traffic to stdin/stdout", | |
199 | parameters => { | |
200 | additionalProperties => 0, | |
201 | properties => { | |
335af808 | 202 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }), |
f3e76e36 DM |
203 | }, |
204 | }, | |
205 | returns => { type => 'null'}, | |
206 | code => sub { | |
207 | my ($param) = @_; | |
208 | ||
209 | my $vmid = $param->{vmid}; | |
210 | my $vnc_socket = PVE::QemuServer::vnc_socket($vmid); | |
211 | ||
212 | if (my $ticket = $ENV{LC_PVE_TICKET}) { # NOTE: ssh on debian only pass LC_* variables | |
213 | PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,password"); | |
214 | PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'vnc', password => $ticket); | |
215 | PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'vnc', time => "+30"); | |
216 | } else { | |
217 | PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,x509,password"); | |
218 | } | |
219 | ||
220 | run_vnc_proxy($vnc_socket); | |
221 | ||
222 | return undef; | |
223 | }}); | |
224 | ||
225 | __PACKAGE__->register_method ({ | |
226 | name => 'unlock', | |
227 | path => 'unlock', | |
228 | method => 'PUT', | |
229 | description => "Unlock the VM.", | |
230 | parameters => { | |
231 | additionalProperties => 0, | |
232 | properties => { | |
335af808 | 233 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), |
f3e76e36 DM |
234 | }, |
235 | }, | |
236 | returns => { type => 'null'}, | |
237 | code => sub { | |
238 | my ($param) = @_; | |
239 | ||
240 | my $vmid = $param->{vmid}; | |
241 | ||
04096e7b | 242 | PVE::QemuConfig->lock_config ($vmid, sub { |
ffda963f | 243 | my $conf = PVE::QemuConfig->load_config($vmid); |
f3e76e36 DM |
244 | delete $conf->{lock}; |
245 | delete $conf->{pending}->{lock} if $conf->{pending}; # just to be sure | |
ffda963f | 246 | PVE::QemuConfig->write_config($vmid, $conf); |
f3e76e36 DM |
247 | }); |
248 | ||
249 | return undef; | |
250 | }}); | |
251 | ||
63a09370 AD |
252 | __PACKAGE__->register_method ({ |
253 | name => 'nbdstop', | |
254 | path => 'nbdstop', | |
255 | method => 'PUT', | |
256 | description => "Stop embedded nbd server.", | |
257 | parameters => { | |
258 | additionalProperties => 0, | |
259 | properties => { | |
260 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
261 | }, | |
262 | }, | |
263 | returns => { type => 'null'}, | |
264 | code => sub { | |
265 | my ($param) = @_; | |
266 | ||
267 | my $vmid = $param->{vmid}; | |
268 | ||
269 | PVE::QemuServer::nbd_stop($vmid); | |
270 | ||
271 | return undef; | |
272 | }}); | |
273 | ||
f3e76e36 DM |
274 | __PACKAGE__->register_method ({ |
275 | name => 'mtunnel', | |
276 | path => 'mtunnel', | |
277 | method => 'POST', | |
278 | description => "Used by qmigrate - do not use manually.", | |
279 | parameters => { | |
280 | additionalProperties => 0, | |
281 | properties => {}, | |
282 | }, | |
283 | returns => { type => 'null'}, | |
284 | code => sub { | |
285 | my ($param) = @_; | |
286 | ||
287 | if (!PVE::Cluster::check_cfs_quorum(1)) { | |
288 | print "no quorum\n"; | |
289 | return undef; | |
290 | } | |
291 | ||
79c9e079 FG |
292 | my $tunnel_write = sub { |
293 | my $text = shift; | |
294 | chomp $text; | |
295 | print "$text\n"; | |
296 | *STDOUT->flush(); | |
297 | }; | |
298 | ||
299 | $tunnel_write->("tunnel online"); | |
300 | $tunnel_write->("ver 1"); | |
d8518469 | 301 | |
e5caa02e | 302 | while (my $line = <STDIN>) { |
f3e76e36 | 303 | chomp $line; |
bcb51ae8 FG |
304 | if ($line =~ /^quit$/) { |
305 | $tunnel_write->("OK"); | |
306 | last; | |
1d5aaa1d FG |
307 | } elsif ($line =~ /^resume (\d+)$/) { |
308 | my $vmid = $1; | |
309 | if (PVE::QemuServer::check_running($vmid, 1)) { | |
310 | eval { PVE::QemuServer::vm_resume($vmid, 1, 1); }; | |
311 | if ($@) { | |
312 | $tunnel_write->("ERR: resume failed - $@"); | |
313 | } else { | |
314 | $tunnel_write->("OK"); | |
315 | } | |
316 | } else { | |
317 | $tunnel_write->("ERR: resume failed - VM $vmid not running"); | |
318 | } | |
bcb51ae8 | 319 | } |
f3e76e36 DM |
320 | } |
321 | ||
322 | return undef; | |
323 | }}); | |
324 | ||
325 | __PACKAGE__->register_method ({ | |
326 | name => 'wait', | |
327 | path => 'wait', | |
328 | method => 'GET', | |
329 | description => "Wait until the VM is stopped.", | |
330 | parameters => { | |
331 | additionalProperties => 0, | |
332 | properties => { | |
335af808 | 333 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }), |
f3e76e36 DM |
334 | timeout => { |
335 | description => "Timeout in seconds. Default is to wait forever.", | |
336 | type => 'integer', | |
337 | minimum => 1, | |
338 | optional => 1, | |
339 | } | |
340 | }, | |
341 | }, | |
342 | returns => { type => 'null'}, | |
343 | code => sub { | |
344 | my ($param) = @_; | |
345 | ||
346 | my $vmid = $param->{vmid}; | |
347 | my $timeout = $param->{timeout}; | |
348 | ||
349 | my $pid = PVE::QemuServer::check_running ($vmid); | |
350 | return if !$pid; | |
351 | ||
352 | print "waiting until VM $vmid stopps (PID $pid)\n"; | |
353 | ||
354 | my $count = 0; | |
355 | while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) { | |
356 | $count++; | |
357 | sleep 1; | |
358 | } | |
359 | ||
360 | die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid); | |
361 | ||
362 | return undef; | |
363 | }}); | |
364 | ||
365 | __PACKAGE__->register_method ({ | |
366 | name => 'monitor', | |
367 | path => 'monitor', | |
368 | method => 'POST', | |
369 | description => "Enter Qemu Monitor interface.", | |
370 | parameters => { | |
371 | additionalProperties => 0, | |
372 | properties => { | |
335af808 | 373 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }), |
f3e76e36 DM |
374 | }, |
375 | }, | |
376 | returns => { type => 'null'}, | |
377 | code => sub { | |
378 | my ($param) = @_; | |
379 | ||
380 | my $vmid = $param->{vmid}; | |
381 | ||
ffda963f | 382 | my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists |
f3e76e36 DM |
383 | |
384 | print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n"; | |
385 | ||
386 | my $term = new Term::ReadLine ('qm'); | |
387 | ||
388 | my $input; | |
389 | while (defined ($input = $term->readline('qm> '))) { | |
390 | chomp $input; | |
391 | ||
392 | next if $input =~ m/^\s*$/; | |
393 | ||
394 | last if $input =~ m/^\s*q(uit)?\s*$/; | |
395 | ||
396 | eval { | |
397 | print PVE::QemuServer::vm_human_monitor_command ($vmid, $input); | |
398 | }; | |
399 | print "ERROR: $@" if $@; | |
400 | } | |
401 | ||
402 | return undef; | |
403 | ||
404 | }}); | |
405 | ||
406 | __PACKAGE__->register_method ({ | |
407 | name => 'rescan', | |
408 | path => 'rescan', | |
409 | method => 'POST', | |
410 | description => "Rescan all storages and update disk sizes and unused disk images.", | |
411 | parameters => { | |
412 | additionalProperties => 0, | |
413 | properties => { | |
335af808 DM |
414 | vmid => get_standard_option('pve-vmid', { |
415 | optional => 1, | |
416 | completion => \&PVE::QemuServer::complete_vmid, | |
417 | }), | |
9224dcee TL |
418 | dryrun => { |
419 | type => 'boolean', | |
420 | optional => 1, | |
421 | default => 0, | |
dc02254e | 422 | description => 'Do not actually write changes out to VM config(s).', |
9224dcee | 423 | }, |
f3e76e36 DM |
424 | }, |
425 | }, | |
426 | returns => { type => 'null'}, | |
427 | code => sub { | |
428 | my ($param) = @_; | |
429 | ||
9224dcee TL |
430 | my $dryrun = $param->{dryrun}; |
431 | ||
432 | print "NOTE: running in dry-run mode, won't write changes out!\n" if $dryrun; | |
433 | ||
434 | PVE::QemuServer::rescan($param->{vmid}, 0, $dryrun); | |
f3e76e36 DM |
435 | |
436 | return undef; | |
437 | }}); | |
438 | ||
8653feeb EK |
439 | __PACKAGE__->register_method ({ |
440 | name => 'importdisk', | |
441 | path => 'importdisk', | |
442 | method => 'POST', | |
443 | description => "Import an external disk image as an unused disk in a VM. The | |
444 | image format has to be supported by qemu-img(1).", | |
445 | parameters => { | |
446 | additionalProperties => 0, | |
447 | properties => { | |
448 | vmid => get_standard_option('pve-vmid', {completion => \&PVE::QemuServer::complete_vmid}), | |
449 | source => { | |
450 | description => 'Path to the disk image to import', | |
451 | type => 'string', | |
452 | optional => 0, | |
453 | }, | |
454 | storage => get_standard_option('pve-storage-id', { | |
455 | description => 'Target storage ID', | |
456 | completion => \&PVE::QemuServer::complete_storage, | |
457 | optional => 0, | |
458 | }), | |
459 | format => { | |
460 | type => 'string', | |
461 | description => 'Target format', | |
462 | enum => [ 'raw', 'qcow2', 'vmdk' ], | |
463 | optional => 1, | |
464 | }, | |
465 | }, | |
466 | }, | |
467 | returns => { type => 'null'}, | |
468 | code => sub { | |
469 | my ($param) = @_; | |
470 | ||
471 | my $vmid = extract_param($param, 'vmid'); | |
472 | my $source = extract_param($param, 'source'); | |
473 | my $storeid = extract_param($param, 'storage'); | |
474 | my $format = extract_param($param, 'format'); | |
475 | ||
476 | my $vm_conf = PVE::QemuConfig->load_config($vmid); | |
477 | PVE::QemuConfig->check_lock($vm_conf); | |
478 | die "$source: non-existent or non-regular file\n" if (! -f $source); | |
c7db1e40 | 479 | |
8653feeb EK |
480 | my $storecfg = PVE::Storage::config(); |
481 | PVE::Storage::storage_check_enabled($storecfg, $storeid); | |
482 | ||
c7db1e40 EK |
483 | my $target_storage_config = |
484 | PVE::Storage::storage_config($storecfg, $storeid); | |
485 | die "storage $storeid does not support vm images\n" | |
486 | if !$target_storage_config->{content}->{images}; | |
487 | ||
8653feeb EK |
488 | PVE::QemuServer::ImportDisk::do_import($source, $vmid, $storeid, { format => $format }); |
489 | ||
490 | return undef; | |
491 | }}); | |
492 | ||
f3e76e36 DM |
493 | __PACKAGE__->register_method ({ |
494 | name => 'terminal', | |
495 | path => 'terminal', | |
496 | method => 'POST', | |
497 | description => "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')", | |
498 | parameters => { | |
499 | additionalProperties => 0, | |
500 | properties => { | |
335af808 | 501 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }), |
f3e76e36 DM |
502 | iface => { |
503 | description => "Select the serial device. By default we simply use the first suitable device.", | |
504 | type => 'string', | |
505 | optional => 1, | |
506 | enum => [qw(serial0 serial1 serial2 serial3)], | |
aa320bcd WB |
507 | }, |
508 | escape => { | |
509 | description => "Escape character.", | |
510 | type => 'string', | |
511 | optional => 1, | |
512 | default => '^O', | |
513 | }, | |
f3e76e36 DM |
514 | }, |
515 | }, | |
516 | returns => { type => 'null'}, | |
517 | code => sub { | |
518 | my ($param) = @_; | |
519 | ||
520 | my $vmid = $param->{vmid}; | |
521 | ||
aa320bcd WB |
522 | my $escape = $param->{escape} // '^O'; |
523 | if ($escape =~ /^\^([\x40-\x7a])$/) { | |
524 | $escape = ord($1) & 0x1F; | |
525 | } elsif ($escape =~ /^0x[0-9a-f]+$/i) { | |
526 | $escape = hex($escape); | |
527 | } elsif ($escape =~ /^[0-9]+$/) { | |
528 | $escape = int($escape); | |
529 | } else { | |
530 | die "invalid escape character definition: $escape\n"; | |
531 | } | |
532 | my $escapemsg = ''; | |
533 | if ($escape) { | |
534 | $escapemsg = sprintf(' (press Ctrl+%c to exit)', $escape+0x40); | |
535 | $escape = sprintf(',escape=0x%x', $escape); | |
536 | } else { | |
537 | $escape = ''; | |
538 | } | |
539 | ||
ffda963f | 540 | my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists |
f3e76e36 DM |
541 | |
542 | my $iface = $param->{iface}; | |
543 | ||
544 | if ($iface) { | |
545 | die "serial interface '$iface' is not configured\n" if !$conf->{$iface}; | |
546 | die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket'; | |
547 | } else { | |
548 | foreach my $opt (qw(serial0 serial1 serial2 serial3)) { | |
549 | if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) { | |
550 | $iface = $opt; | |
551 | last; | |
552 | } | |
553 | } | |
554 | die "unable to find a serial interface\n" if !$iface; | |
555 | } | |
556 | ||
557 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
558 | ||
559 | my $socket = "/var/run/qemu-server/${vmid}.$iface"; | |
560 | ||
aa320bcd | 561 | my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0$escape"; |
f3e76e36 | 562 | |
aa320bcd | 563 | print "starting serial terminal on interface ${iface}${escapemsg}\n"; |
f3e76e36 DM |
564 | |
565 | system($cmd); | |
566 | ||
567 | return undef; | |
568 | }}); | |
569 | ||
7cd9f6d7 EK |
570 | __PACKAGE__->register_method ({ |
571 | name => 'importovf', | |
572 | path => 'importovf', | |
573 | description => "Create a new VM using parameters read from an OVF manifest", | |
574 | parameters => { | |
575 | additionalProperties => 0, | |
576 | properties => { | |
577 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), | |
578 | manifest => { | |
579 | type => 'string', | |
580 | description => 'path to the ovf file', | |
581 | }, | |
582 | storage => get_standard_option('pve-storage-id', { | |
583 | description => 'Target storage ID', | |
584 | completion => \&PVE::QemuServer::complete_storage, | |
585 | optional => 0, | |
586 | }), | |
587 | format => { | |
588 | type => 'string', | |
589 | description => 'Target format', | |
590 | enum => [ 'raw', 'qcow2', 'vmdk' ], | |
591 | optional => 1, | |
592 | }, | |
593 | dryrun => { | |
594 | type => 'boolean', | |
595 | description => 'Print a parsed representation of the extracted OVF parameters, but do not create a VM', | |
596 | optional => 1, | |
f6306646 | 597 | } |
7cd9f6d7 EK |
598 | }, |
599 | }, | |
b533b995 | 600 | returns => { type => 'null' }, |
7cd9f6d7 EK |
601 | code => sub { |
602 | my ($param) = @_; | |
603 | ||
604 | my $vmid = PVE::Tools::extract_param($param, 'vmid'); | |
605 | my $ovf_file = PVE::Tools::extract_param($param, 'manifest'); | |
606 | my $storeid = PVE::Tools::extract_param($param, 'storage'); | |
607 | my $format = PVE::Tools::extract_param($param, 'format'); | |
608 | my $dryrun = PVE::Tools::extract_param($param, 'dryrun'); | |
609 | ||
610 | die "$ovf_file: non-existent or non-regular file\n" if (! -f $ovf_file); | |
611 | my $storecfg = PVE::Storage::config(); | |
612 | PVE::Storage::storage_check_enabled($storecfg, $storeid); | |
613 | ||
614 | my $parsed = PVE::QemuServer::OVF::parse_ovf($ovf_file); | |
615 | ||
616 | if ($dryrun) { | |
0f80f1ab WB |
617 | print to_json($parsed, { pretty => 1, canonical => 1}); |
618 | return; | |
7cd9f6d7 EK |
619 | } |
620 | ||
621 | $param->{name} = $parsed->{qm}->{name} if defined($parsed->{qm}->{name}); | |
622 | $param->{memory} = $parsed->{qm}->{memory} if defined($parsed->{qm}->{memory}); | |
623 | $param->{cores} = $parsed->{qm}->{cores} if defined($parsed->{qm}->{cores}); | |
624 | ||
625 | my $importfn = sub { | |
626 | ||
627 | PVE::Cluster::check_vmid_unused($vmid); | |
628 | ||
629 | my $conf = $param; | |
630 | ||
631 | eval { | |
632 | # order matters, as do_import() will load_config() internally | |
6ee499ff | 633 | $conf->{vmgenid} = PVE::QemuServer::generate_uuid(); |
7cd9f6d7 EK |
634 | $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid(); |
635 | PVE::QemuConfig->write_config($vmid, $conf); | |
636 | ||
637 | foreach my $disk (@{ $parsed->{disks} }) { | |
638 | my ($file, $drive) = ($disk->{backing_file}, $disk->{disk_address}); | |
639 | PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid, | |
640 | { drive_name => $drive, format => $format }); | |
641 | } | |
642 | ||
643 | # reload after disks entries have been created | |
644 | $conf = PVE::QemuConfig->load_config($vmid); | |
645 | PVE::QemuConfig->check_lock($conf); | |
646 | my $firstdisk = PVE::QemuServer::resolve_first_disk($conf); | |
647 | $conf->{bootdisk} = $firstdisk if $firstdisk; | |
648 | PVE::QemuConfig->write_config($vmid, $conf); | |
f6306646 | 649 | }; |
7cd9f6d7 EK |
650 | |
651 | my $err = $@; | |
652 | if ($err) { | |
653 | my $skiplock = 1; | |
654 | eval { PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); }; | |
655 | die "import failed - $err"; | |
656 | } | |
7cd9f6d7 EK |
657 | }; |
658 | ||
659 | my $wait_for_lock = 1; | |
5d942f5a EK |
660 | PVE::QemuConfig->lock_config_full($vmid, $wait_for_lock, $importfn); |
661 | ||
662 | return undef; | |
7cd9f6d7 EK |
663 | |
664 | } | |
665 | }); | |
788a6a35 | 666 | |
520884de DC |
667 | __PACKAGE__->register_method({ |
668 | name => 'exec', | |
669 | path => 'exec', | |
670 | method => 'POST', | |
671 | protected => 1, | |
672 | description => "Executes the given command via the guest agent", | |
673 | parameters => { | |
674 | additionalProperties => 0, | |
675 | properties => { | |
676 | node => get_standard_option('pve-node'), | |
677 | vmid => get_standard_option('pve-vmid', { | |
678 | completion => \&PVE::QemuServer::complete_vmid_running }), | |
679 | synchronous => { | |
680 | type => 'boolean', | |
681 | optional => 1, | |
682 | default => 1, | |
683 | description => "If set to off, returns the pid immediately instead of waiting for the commmand to finish or the timeout.", | |
684 | }, | |
685 | 'timeout' => { | |
686 | type => 'integer', | |
687 | description => "The maximum time to wait synchronously for the command to finish. If reached, the pid gets returned. Set to 0 to deactivate", | |
688 | minimum => 0, | |
689 | optional => 1, | |
690 | default => 30, | |
691 | }, | |
692 | 'extra-args' => get_standard_option('extra-args'), | |
693 | }, | |
694 | }, | |
695 | returns => { | |
696 | type => 'object', | |
697 | }, | |
698 | code => sub { | |
699 | my ($param) = @_; | |
700 | ||
701 | my $vmid = $param->{vmid}; | |
702 | my $sync = $param->{synchronous} // 1; | |
703 | if (!$param->{'extra-args'} || !@{$param->{'extra-args'}}) { | |
704 | raise_param_exc( { 'extra-args' => "No command given" }); | |
705 | } | |
706 | if (defined($param->{timeout}) && !$sync) { | |
707 | raise_param_exc({ synchronous => "needs to be set for 'timeout'"}); | |
708 | } | |
709 | ||
710 | my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $param->{'extra-args'}); | |
711 | ||
712 | if ($sync) { | |
713 | my $pid = $res->{pid}; | |
714 | my $timeout = $param->{timeout} // 30; | |
715 | my $starttime = time(); | |
716 | ||
717 | while ($timeout == 0 || (time() - $starttime) < $timeout) { | |
718 | my $out = PVE::QemuServer::Agent::qemu_exec_status($vmid, $pid); | |
719 | if ($out->{exited}) { | |
720 | $res = $out; | |
721 | last; | |
722 | } | |
723 | sleep 1; | |
724 | } | |
725 | ||
726 | if (!$res->{exited}) { | |
727 | warn "timeout reached, returning pid\n"; | |
728 | } | |
729 | } | |
730 | ||
731 | return { result => $res }; | |
732 | }}); | |
733 | ||
3ea84aeb DC |
734 | __PACKAGE__->register_method({ |
735 | name => 'cleanup', | |
736 | path => 'cleanup', | |
737 | method => 'POST', | |
738 | protected => 1, | |
739 | description => "Cleans up resources like tap devices, vgpus, etc. Called after a vm shuts down, crashes, etc.", | |
740 | parameters => { | |
741 | additionalProperties => 0, | |
742 | properties => { | |
743 | node => get_standard_option('pve-node'), | |
744 | vmid => get_standard_option('pve-vmid', { | |
745 | completion => \&PVE::QemuServer::complete_vmid_running }), | |
746 | 'clean-shutdown' => { | |
747 | type => 'boolean', | |
748 | description => "Indicates if qemu shutdown cleanly.", | |
749 | }, | |
750 | 'guest-requested' => { | |
751 | type => 'boolean', | |
752 | description => "Indicates if the shutdown was requested by the guest or via qmp.", | |
753 | }, | |
754 | }, | |
755 | }, | |
756 | returns => { type => 'null', }, | |
757 | code => sub { | |
758 | my ($param) = @_; | |
759 | ||
760 | my $vmid = $param->{vmid}; | |
761 | my $clean = $param->{'clean-shutdown'}; | |
762 | my $guest = $param->{'guest-requested'}; | |
763 | ||
0f56fff2 DC |
764 | # return if we do not have the config anymore |
765 | return if !-f PVE::QemuConfig->config_file($vmid); | |
766 | ||
3ea84aeb DC |
767 | my $storecfg = PVE::Storage::config(); |
768 | warn "Starting cleanup for $vmid\n"; | |
769 | ||
770 | PVE::QemuConfig->lock_config($vmid, sub { | |
771 | my $conf = PVE::QemuConfig->load_config ($vmid); | |
772 | my $pid = PVE::QemuServer::check_running ($vmid); | |
773 | die "vm still running\n" if $pid; | |
774 | ||
775 | if (!$clean) { | |
776 | # we have to cleanup the tap devices after a crash | |
777 | ||
778 | foreach my $opt (keys %$conf) { | |
779 | next if $opt !~ m/^net(\d)+$/; | |
780 | my $interface = $1; | |
781 | PVE::Network::tap_unplug("tap${vmid}i${interface}"); | |
782 | } | |
783 | } | |
784 | ||
785 | if (!$clean || $guest) { | |
786 | # vm was shutdown from inside the guest or crashed, doing api cleanup | |
787 | PVE::QemuServer::vm_stop_cleanup($storecfg, $vmid, $conf, 0, 0); | |
788 | } | |
789 | }); | |
790 | ||
791 | warn "Finished cleanup for $vmid\n"; | |
792 | ||
793 | return undef; | |
794 | }}); | |
795 | ||
788a6a35 DM |
796 | my $print_agent_result = sub { |
797 | my ($data) = @_; | |
798 | ||
520884de | 799 | my $result = $data->{result} // $data; |
788a6a35 DM |
800 | return if !defined($result); |
801 | ||
802 | my $class = ref($result); | |
803 | ||
804 | if (!$class) { | |
805 | chomp $result; | |
806 | return if $result =~ m/^\s*$/; | |
807 | print "$result\n"; | |
808 | return; | |
809 | } | |
810 | ||
811 | if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash | |
812 | return; | |
813 | } | |
814 | ||
815 | print to_json($result, { pretty => 1, canonical => 1}); | |
816 | }; | |
817 | ||
3351aacc WB |
818 | sub param_mapping { |
819 | my ($name) = @_; | |
820 | ||
821 | my $ssh_key_map = ['sshkeys', sub { | |
822 | return URI::Escape::uri_escape(PVE::Tools::file_get_contents($_[0])); | |
823 | }]; | |
3dba118c | 824 | my $cipassword_map = PVE::CLIHandler::get_standard_mapping('pve-password', { name => 'cipassword' }); |
8593cbe4 | 825 | my $password_map = PVE::CLIHandler::get_standard_mapping('pve-password'); |
3351aacc | 826 | my $mapping = { |
29d1f147 WB |
827 | 'update_vm' => [$ssh_key_map, $cipassword_map], |
828 | 'create_vm' => [$ssh_key_map, $cipassword_map], | |
8593cbe4 | 829 | 'set-user-password' => [$password_map], |
3351aacc WB |
830 | }; |
831 | ||
832 | return $mapping->{$name}; | |
833 | } | |
834 | ||
f3e76e36 DM |
835 | our $cmddef = { |
836 | list => [ "PVE::API2::Qemu", 'vmlist', [], | |
837 | { node => $nodename }, sub { | |
838 | my $vmlist = shift; | |
839 | ||
840 | exit 0 if (!scalar(@$vmlist)); | |
841 | ||
842 | printf "%10s %-20s %-10s %-10s %12s %-10s\n", | |
843 | qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID); | |
844 | ||
845 | foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) { | |
846 | printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name}, | |
12612b09 | 847 | $rec->{qmpstatus} || $rec->{status}, |
f3e76e36 DM |
848 | ($rec->{maxmem} || 0)/(1024*1024), |
849 | ($rec->{maxdisk} || 0)/(1024*1024*1024), | |
850 | $rec->{pid}||0; | |
851 | } | |
852 | ||
853 | ||
854 | } ], | |
855 | ||
856 | create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename }, $upid_exit ], | |
857 | ||
858 | destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename }, $upid_exit ], | |
859 | ||
860 | clone => [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ], | |
861 | ||
862 | migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit ], | |
863 | ||
864 | set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ], | |
865 | ||
866 | resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ], | |
867 | ||
868 | move_disk => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node => $nodename }, $upid_exit ], | |
869 | ||
870 | unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node => $nodename } ], | |
871 | ||
872 | config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], | |
873 | { node => $nodename }, sub { | |
874 | my $config = shift; | |
875 | foreach my $k (sort (keys %$config)) { | |
876 | next if $k eq 'digest'; | |
877 | my $v = $config->{$k}; | |
878 | if ($k eq 'description') { | |
879 | $v = PVE::Tools::encode_text($v); | |
880 | } | |
881 | print "$k: $v\n"; | |
882 | } | |
883 | }], | |
884 | ||
885 | pending => [ "PVE::API2::Qemu", 'vm_pending', ['vmid'], | |
886 | { node => $nodename }, sub { | |
887 | my $data = shift; | |
888 | foreach my $item (sort { $a->{key} cmp $b->{key}} @$data) { | |
889 | my $k = $item->{key}; | |
890 | next if $k eq 'digest'; | |
891 | my $v = $item->{value}; | |
892 | my $p = $item->{pending}; | |
893 | if ($k eq 'description') { | |
894 | $v = PVE::Tools::encode_text($v) if defined($v); | |
895 | $p = PVE::Tools::encode_text($p) if defined($p); | |
896 | } | |
897 | if (defined($v)) { | |
898 | if ($item->{delete}) { | |
899 | print "del $k: $v\n"; | |
900 | } elsif (defined($p)) { | |
901 | print "cur $k: $v\n"; | |
902 | print "new $k: $p\n"; | |
903 | } else { | |
904 | print "cur $k: $v\n"; | |
905 | } | |
906 | } elsif (defined($p)) { | |
907 | print "new $k: $p\n"; | |
908 | } | |
909 | } | |
910 | }], | |
911 | ||
912 | showcmd => [ __PACKAGE__, 'showcmd', ['vmid']], | |
913 | ||
914 | status => [ __PACKAGE__, 'status', ['vmid']], | |
915 | ||
916 | snapshot => [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
917 | ||
918 | delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
919 | ||
265db461 DC |
920 | listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node => $nodename }, |
921 | sub { | |
922 | my $res = shift; | |
923 | foreach my $e (@$res) { | |
924 | my $headline = $e->{description} || 'no-description'; | |
925 | $headline =~ s/\n.*//sg; | |
926 | my $parent = $e->{parent} // 'no-parent'; | |
927 | printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline); | |
928 | } | |
929 | }], | |
930 | ||
f3e76e36 DM |
931 | rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], |
932 | ||
933 | template => [ "PVE::API2::Qemu", 'template', ['vmid'], { node => $nodename }], | |
934 | ||
935 | start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } , $upid_exit ], | |
936 | ||
937 | stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit ], | |
938 | ||
939 | reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node => $nodename }, $upid_exit ], | |
940 | ||
941 | shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit ], | |
942 | ||
943 | suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit ], | |
944 | ||
945 | resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit ], | |
946 | ||
947 | sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node => $nodename } ], | |
948 | ||
949 | vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']], | |
950 | ||
951 | wait => [ __PACKAGE__, 'wait', ['vmid']], | |
952 | ||
953 | unlock => [ __PACKAGE__, 'unlock', ['vmid']], | |
954 | ||
955 | rescan => [ __PACKAGE__, 'rescan', []], | |
956 | ||
957 | monitor => [ __PACKAGE__, 'monitor', ['vmid']], | |
958 | ||
bf744e94 | 959 | agent => { alias => 'guest cmd' }, |
d1a47427 | 960 | |
34e4c0aa | 961 | guest => { |
bf744e94 | 962 | cmd => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'], { node => $nodename }, $print_agent_result ], |
8593cbe4 | 963 | passwd => [ "PVE::API2::Qemu::Agent", 'set-user-password', [ 'vmid', 'username' ], { node => $nodename }], |
520884de DC |
964 | exec => [ __PACKAGE__, 'exec', [ 'vmid', 'extra-args' ], { node => $nodename }, $print_agent_result], |
965 | 'exec-status' => [ "PVE::API2::Qemu::Agent", 'exec-status', [ 'vmid', 'pid' ], { node => $nodename }, $print_agent_result], | |
8593cbe4 DC |
966 | }, |
967 | ||
f3e76e36 DM |
968 | mtunnel => [ __PACKAGE__, 'mtunnel', []], |
969 | ||
63a09370 AD |
970 | nbdstop => [ __PACKAGE__, 'nbdstop', ['vmid']], |
971 | ||
f3e76e36 | 972 | terminal => [ __PACKAGE__, 'terminal', ['vmid']], |
8653feeb EK |
973 | |
974 | importdisk => [ __PACKAGE__, 'importdisk', ['vmid', 'source', 'storage']], | |
7cd9f6d7 EK |
975 | |
976 | importovf => [ __PACKAGE__, 'importovf', ['vmid', 'manifest', 'storage']], | |
977 | ||
3ea84aeb DC |
978 | cleanup => [ __PACKAGE__, 'cleanup', ['vmid', 'clean-shutdown', 'guest-requested'], { node => $nodename }], |
979 | ||
f3e76e36 DM |
980 | }; |
981 | ||
982 | 1; |