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