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