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