[pve-devel] [PATCH qemu-server v2 7/7] implement file-write via guest-agent in the api

Dominik Csapak d.csapak at proxmox.com
Tue Jun 26 14:15:49 CEST 2018


writes the given content to the file

the size is at the moment limited by the max post size of the
pveproxy/daemon, so we set the maxLength to 60k

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 PVE/API2/Qemu/Agent.pm | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/PVE/API2/Qemu/Agent.pm b/PVE/API2/Qemu/Agent.pm
index 4ff39fd..75a58ce 100644
--- a/PVE/API2/Qemu/Agent.pm
+++ b/PVE/API2/Qemu/Agent.pm
@@ -118,6 +118,7 @@ __PACKAGE__->register_method({
 	    exec
 	    exec-status
 	    file-read
+	    file-write
 	    set-user-password
 	);
 
@@ -438,4 +439,43 @@ __PACKAGE__->register_method({
 	return $result;
     }});
 
+__PACKAGE__->register_method({
+    name => 'file-write',
+    path => 'file-write',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Writes the given file via guest agent.",
+    permissions => { check => [ 'perm', '/vms/{vmid}', [ 'VM.Monitor' ]]},
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid', {
+		    completion => \&PVE::QemuServer::complete_vmid_running }),
+	    file => {
+		type => 'string',
+		description => 'The path to the file.'
+	    },
+	    content => {
+		type => 'string',
+		maxLength => 60*1024, # 60k, smaller than our 64k POST limit
+		description => "The content to write into the file."
+	    }
+	},
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	my $vmid = $param->{vmid};
+	my $buf = encode_base64($param->{content});
+
+	my $qgafh = agent_cmd($vmid, "file-open",  { path => $param->{file}, mode => 'wb' }, "can't open file");
+	my $write = agent_cmd($vmid, "file-write", { handle => $qgafh, 'buf-b64' => $buf }, "can't write to file");
+	my $res = agent_cmd($vmid, "file-close", { handle => $qgafh }, "can't close file");
+
+	return undef;
+    }});
+
 1;
-- 
2.11.0





More information about the pve-devel mailing list