[pve-devel] [PATCH qemu-server] Add qm agent.

Fabian Grünbichler f.gruenbichler at proxmox.com
Thu Nov 3 14:34:24 CET 2016


comments inline

On Thu, Nov 03, 2016 at 01:22:06PM +0100, Wolfgang Link wrote:
> This command makes it possible to run commnads on the Qemu-Guest-Agent if it is installed and running.
> ---
>  PVE/API2/Qemu.pm | 47 +++++++++++++++++++++++++++++++++++++++++++++++
>  PVE/CLI/qm.pm    |  2 ++
>  2 files changed, 49 insertions(+)
> 
> diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
> index 34a5d5d..c701e5d 100644
> --- a/PVE/API2/Qemu.pm
> +++ b/PVE/API2/Qemu.pm
> @@ -5,6 +5,7 @@ use warnings;
>  use Cwd 'abs_path';
>  use Net::SSLeay;
>  use UUID;
> +use JSON;
>  
>  use PVE::Cluster qw (cfs_read_file cfs_write_file);;
>  use PVE::SafeSyslog;
> @@ -614,6 +615,7 @@ __PACKAGE__->register_method({
>  	    { subdir => 'rrd' },
>  	    { subdir => 'rrddata' },
>  	    { subdir => 'monitor' },
> +	    { subdir => 'agent' },
>  	    { subdir => 'snapshot' },
>  	    { subdir => 'spiceproxy' },
>  	    { subdir => 'sendkey' },
> @@ -2806,6 +2808,51 @@ __PACKAGE__->register_method({
>      }});
>  
>  __PACKAGE__->register_method({
> +    name => 'agent',
> +    path => '{vmid}/agent',
> +    method => 'POST',
> +    protected => 1,
> +    proxyto => 'node',
> +    description => "Execute Qemu Guest Agent commands.",
> +    permissions => {
> +	check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
> +    },
> +    parameters => {
> +	additionalProperties => 0,
> +	properties => {
> +	    node => get_standard_option('pve-node'),
> +	    vmid => get_standard_option('pve-vmid'),
> +	    command => {
> +		type => 'string',

pattern matching for "guest-*" like in QMPClient, to make sure that only
the guest agent socket can be used?

> +		description => "The QGA command.",
> +	    }
> +	},
> +    },
> +    returns => { type => 'string' },

see below

> +    code => sub {
> +	my ($param) = @_;
> +
> +	my $vmid = $param->{vmid};
> +
> +	my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
> +
> +	die "No Qemu Guest Agent\n" if !defined($conf->{agent});
> +	die "VM $vmid is not running\n" if !PVE::QemuServer::check_running($vmid);
> +
> +	my $res = '';
> +	eval {
> +	    $res = PVE::QemuServer::vm_mon_cmd($vmid, $param->{command});
> +	};
> +
> +	if (my $err = $@) {
> +		return "ERROR: $err";
> +	} else {
> +		my $res = ref($res) eq 'HASH' ? to_json($res) : $res;
> +		return "OK: $res\n";

why? wouldn't it make more sense to return an object with status +
result hash?

> +	}
> +    }});
> +
> +__PACKAGE__->register_method({
>      name => 'resize_vm',
>      path => '{vmid}/resize',
>      method => 'PUT',
> diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
> index a0e0ba5..0599260 100755
> --- a/PVE/CLI/qm.pm
> +++ b/PVE/CLI/qm.pm
> @@ -529,6 +529,8 @@ our $cmddef = {
>  
>      monitor  => [ __PACKAGE__, 'monitor', ['vmid']],
>  
> +    agent  => [ "PVE::API2::Qemu", 'agent', ['vmid'], { node => $nodename }],
> +
>      mtunnel => [ __PACKAGE__, 'mtunnel', []],
>  
>      terminal => [ __PACKAGE__, 'terminal', ['vmid']],
> -- 
> 2.1.4
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel at pve.proxmox.com
> http://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel




More information about the pve-devel mailing list