[pve-devel] [PATCH qemu-server 1/2] add termproxy api call

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Dec 4 10:40:53 CET 2017


On 12/01/2017 09:29 AM, Dominik Csapak wrote:
> for xtermjs web client
> 
> Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
> ---
>  PVE/API2/Qemu.pm | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 96 insertions(+)
> 
> diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
> index 5f9d105..20306fc 100644
> --- a/PVE/API2/Qemu.pm
> +++ b/PVE/API2/Qemu.pm
> @@ -609,6 +609,7 @@ __PACKAGE__->register_method({
>  	    { subdir => 'status' },
>  	    { subdir => 'unlink' },
>  	    { subdir => 'vncproxy' },
> +	    { subdir => 'termproxy' },
>  	    { subdir => 'migrate' },
>  	    { subdir => 'resize' },
>  	    { subdir => 'move' },
> @@ -1483,6 +1484,101 @@ __PACKAGE__->register_method({
>      }});
>  
>  __PACKAGE__->register_method({
> +    name => 'termproxy',
> +    path => '{vmid}/termproxy',
> +    method => 'POST',
> +    protected => 1,
> +    permissions => {
> +	check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
> +    },
> +    description => "Creates a TCP proxy connections.",
> +    parameters => {
> +    	additionalProperties => 0,
space before tab

> +	properties => {
> +	    node => get_standard_option('pve-node'),
> +	    vmid => get_standard_option('pve-vmid'),
> +	    serial=> {
> +		optional => 1,
> +		type => 'string',
> +		enum => [qw(serial0 serial1 serial2 serial3)],
> +		description => "opens a serial terminal (defaults to display)",
> +	    },
> +	},
> +    },
> +    returns => {
> +    	additionalProperties => 0,
space before tab

> +	properties => {
> +	    user => { type => 'string' },
> +	    ticket => { type => 'string' },
> +	    port => { type => 'integer' },
> +	    upid => { type => 'string' },
> +	},
> +    },
> +    code => sub {
> +	my ($param) = @_;
> +
> +	my $rpcenv = PVE::RPCEnvironment::get();
> +
> +	my $authuser = $rpcenv->get_user();
> +
> +	my $vmid = $param->{vmid};
> +	my $node = $param->{node};
> +	my $serial = $param->{serial};
> +
> +	my $conf = PVE::QemuConfig->load_config($vmid, $node); # check if VM exists
> +
> +	if (!defined($serial)) {
> +	    if ($conf->{vga} && $conf->{vga} =~ m/^serial\d+$/) {
> +		$serial = $conf->{vga};
> +	    }
> +	}
> +
> +	my $authpath = "/vms/$vmid";
> +
> +	my $ticket = PVE::AccessControl::assemble_vnc_ticket($authuser, $authpath);
> +
> +	my ($remip, $family);
> +
> +	if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
> +	    ($remip, $family) = PVE::Cluster::remote_node_ip($node);
> +	} else {
> +	    $family = PVE::Tools::get_host_address_family($node);
> +	}
> +
> +	my $port = PVE::Tools::next_vnc_port($family);
> +
> +	my $remcmd = $remip ?
> +	    ['/usr/bin/ssh', '-e', 'none', '-t', $remip] : [];
> +
> +	my $termcmd = [ '/usr/sbin/qm', 'terminal', $vmid];
> +	push @$termcmd, '-iface', $serial if $serial;
> +
> +	my $realcmd = sub {
> +	    my $upid = shift;
> +
> +	    syslog('info', "starting qemu termproxy $upid\n");
> +
> +	    my $cmd = ['/usr/bin/termproxy', $port, '--'];
> +	    push @$cmd, @$remcmd, @$termcmd;
> +
> +	    PVE::Tools::run_command($cmd);
> +
> +	    return;
nit: useless return

> +	};
> +
> +	my $upid = $rpcenv->fork_worker('vncproxy', $vmid, $authuser, $realcmd, 1);
> +
> +	PVE::Tools::wait_for_vnc_port($port);

I found out that this does not dies but returns undef if the port could not be
found in the timeout period.
As we have this method already in a few places I would rather fix it by letting it
die, not by introducing checks for the return value on all those places using it.
Further we need to add a $family parameter to it, else we may get a false positive.

I'll look into this, and as that does not has to do with your changes:
Reviewed-by: Thomas Lamprecht <t.lamprecht at proxmox.com>

> +
> +	return {
> +	    user => $authuser,
> +	    ticket => $ticket,
> +	    port => $port,
> +	    upid => $upid,
> +	};
> +    }});
> +
> +__PACKAGE__->register_method({
>      name => 'vncwebsocket',
>      path => '{vmid}/vncwebsocket',
>      method => 'GET',
> 





More information about the pve-devel mailing list