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