6 # Note: disable '+' prefix for Getopt::Long (for resize command)
7 use Getopt
::Long
qw(:config no_getopt_compat);
14 use PVE
::Tools
qw(extract_param);
18 use PVE
::RPCEnvironment
;
22 use PVE
::JSONSchema
qw(get_standard_option);
27 use base
qw(PVE::CLIHandler);
31 my $status = PVE
::Tools
::upid_read_status
($upid);
32 exit($status eq 'OK' ?
0 : -1);
35 my $nodename = PVE
::INotify
::nodename
();
41 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
43 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
45 die "unable to connect to socket '$path' - $!" if !$s;
47 my $select = new IO
::Select
;
49 $select->add(\
*STDIN
);
52 my $timeout = 60*15; # 15 minutes
55 while ($select->count &&
56 scalar(@handles = $select->can_read ($timeout))) {
57 foreach my $h (@handles) {
59 my $n = $h->sysread($buf, 4096);
69 syswrite(\
*STDOUT
, $buf);
79 sub print_recursive_hash
{
80 my ($prefix, $hash, $key) = @_;
82 if (ref($hash) eq 'HASH') {
84 print "$prefix$key:\n";
86 foreach my $itemkey (keys %$hash) {
87 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
89 } elsif (ref($hash) eq 'ARRAY') {
91 print "$prefix$key:\n";
93 foreach my $item (@$hash) {
94 print_recursive_hash
("\t$prefix", $item);
96 } elsif (!ref($hash) && defined($hash)) {
98 print "$prefix$key: $hash\n";
100 print "$prefix$hash\n";
105 __PACKAGE__-
>register_method ({
109 description
=> "Show command line which is used to start the VM (debug info).",
111 additionalProperties
=> 0,
113 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
116 returns
=> { type
=> 'null'},
120 my $storecfg = PVE
::Storage
::config
();
121 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
126 __PACKAGE__-
>register_method ({
130 description
=> "Show VM status.",
132 additionalProperties
=> 0,
134 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
136 description
=> "Verbose output format",
142 returns
=> { type
=> 'null'},
147 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
149 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
150 my $stat = $vmstatus->{$param->{vmid
}};
151 if ($param->{verbose
}) {
152 foreach my $k (sort (keys %$stat)) {
153 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
155 print_recursive_hash
("", $v, $k);
158 my $status = $stat->{qmpstatus
} || 'unknown';
159 print "status: $status\n";
165 __PACKAGE__-
>register_method ({
169 description
=> "Proxy VM VNC traffic to stdin/stdout",
171 additionalProperties
=> 0,
173 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
176 returns
=> { type
=> 'null'},
180 my $vmid = $param->{vmid
};
181 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
183 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
184 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
185 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
186 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
188 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
191 run_vnc_proxy
($vnc_socket);
196 __PACKAGE__-
>register_method ({
200 description
=> "Unlock the VM.",
202 additionalProperties
=> 0,
204 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
207 returns
=> { type
=> 'null'},
211 my $vmid = $param->{vmid
};
213 PVE
::QemuConfig-
>lock_config ($vmid, sub {
214 my $conf = PVE
::QemuConfig-
>load_config($vmid);
215 delete $conf->{lock};
216 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
217 PVE
::QemuConfig-
>write_config($vmid, $conf);
223 __PACKAGE__-
>register_method ({
227 description
=> "Used by qmigrate - do not use manually.",
229 additionalProperties
=> 0,
232 returns
=> { type
=> 'null'},
236 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
241 print "tunnel online\n";
244 while (my $line = <>) {
246 last if $line =~ m/^quit$/;
252 __PACKAGE__-
>register_method ({
256 description
=> "Wait until the VM is stopped.",
258 additionalProperties
=> 0,
260 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
262 description
=> "Timeout in seconds. Default is to wait forever.",
269 returns
=> { type
=> 'null'},
273 my $vmid = $param->{vmid
};
274 my $timeout = $param->{timeout
};
276 my $pid = PVE
::QemuServer
::check_running
($vmid);
279 print "waiting until VM $vmid stopps (PID $pid)\n";
282 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
287 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
292 __PACKAGE__-
>register_method ({
296 description
=> "Enter Qemu Monitor interface.",
298 additionalProperties
=> 0,
300 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
303 returns
=> { type
=> 'null'},
307 my $vmid = $param->{vmid
};
309 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
311 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
313 my $term = new Term
::ReadLine
('qm');
316 while (defined ($input = $term->readline('qm> '))) {
319 next if $input =~ m/^\s*$/;
321 last if $input =~ m/^\s*q(uit)?\s*$/;
324 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
326 print "ERROR: $@" if $@;
333 __PACKAGE__-
>register_method ({
337 description
=> "Rescan all storages and update disk sizes and unused disk images.",
339 additionalProperties
=> 0,
341 vmid
=> get_standard_option
('pve-vmid', {
343 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
347 returns
=> { type
=> 'null'},
351 PVE
::QemuServer
::rescan
($param->{vmid
});
356 __PACKAGE__-
>register_method ({
360 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
362 additionalProperties
=> 0,
364 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
366 description
=> "Select the serial device. By default we simply use the first suitable device.",
369 enum
=> [qw(serial0 serial1 serial2 serial3)],
373 returns
=> { type
=> 'null'},
377 my $vmid = $param->{vmid
};
379 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
381 my $iface = $param->{iface
};
384 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
385 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
387 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
388 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
393 die "unable to find a serial interface\n" if !$iface;
396 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
398 my $socket = "/var/run/qemu-server/${vmid}.$iface";
400 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f";
402 print "starting serial terminal on interface $iface (press control-O to exit)\n";
410 my $print_agent_result = sub {
413 my $result = $data->{result
};
414 return if !defined($result);
416 my $class = ref($result);
420 return if $result =~ m/^\s*$/;
425 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
429 print to_json
($result, { pretty
=> 1, canonical
=> 1});
433 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
434 { node
=> $nodename }, sub {
437 exit 0 if (!scalar(@$vmlist));
439 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
440 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
442 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
443 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
444 $rec->{qmpstatus
} || $rec->{status
},
445 ($rec->{maxmem
} || 0)/(1024*1024),
446 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
453 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
455 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
457 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
459 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
461 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
463 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
465 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
467 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
469 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
470 { node
=> $nodename }, sub {
472 foreach my $k (sort (keys %$config)) {
473 next if $k eq 'digest';
474 my $v = $config->{$k};
475 if ($k eq 'description') {
476 $v = PVE
::Tools
::encode_text
($v);
482 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
483 { node
=> $nodename }, sub {
485 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
486 my $k = $item->{key
};
487 next if $k eq 'digest';
488 my $v = $item->{value
};
489 my $p = $item->{pending
};
490 if ($k eq 'description') {
491 $v = PVE
::Tools
::encode_text
($v) if defined($v);
492 $p = PVE
::Tools
::encode_text
($p) if defined($p);
495 if ($item->{delete}) {
496 print "del $k: $v\n";
497 } elsif (defined($p)) {
498 print "cur $k: $v\n";
499 print "new $k: $p\n";
501 print "cur $k: $v\n";
503 } elsif (defined($p)) {
504 print "new $k: $p\n";
509 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
511 status
=> [ __PACKAGE__
, 'status', ['vmid']],
513 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
515 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
517 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
520 foreach my $e (@$res) {
521 my $headline = $e->{description
} || 'no-description';
522 $headline =~ s/\n.*//sg;
523 my $parent = $e->{parent
} // 'no-parent';
524 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
528 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
530 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
532 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
534 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
536 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
538 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
540 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
542 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
544 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
546 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
548 wait => [ __PACKAGE__
, 'wait', ['vmid']],
550 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
552 rescan
=> [ __PACKAGE__
, 'rescan', []],
554 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
556 agent
=> [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
557 { node
=> $nodename }, $print_agent_result ],
559 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
561 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],