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