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