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