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