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

Dominik Csapak d.csapak at proxmox.com
Thu Jun 7 13:16:57 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

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

diff --git a/PVE/API2/Qemu/Agent.pm b/PVE/API2/Qemu/Agent.pm
index dad135c..a206050 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
 	);
 
@@ -417,4 +418,54 @@ __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',
+		description => "The content to write into the file."
+	    }
+	},
+    },
+    returns => { type => 'null' },
+    code => sub {
+	my ($param) = @_;
+
+	my $vmid = $param->{vmid};
+
+	my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
+
+	check_agent_available($vmid, $conf);
+
+	my $qgafh = PVE::QemuServer::vm_mon_cmd($vmid, "guest-file-open",  path => $param->{file}, mode => 'wb');
+
+	raise_agent_error($qgafh, "can't open file");
+
+	my $buf = encode_base64($param->{content});
+
+	my $write = PVE::QemuServer::vm_mon_cmd($vmid, "guest-file-write", handle => $qgafh, 'buf-b64' => $buf);
+
+	raise_agent_error($write, "can't write to file");
+
+	my $res = PVE::QemuServer::vm_mon_cmd($vmid, "guest-file-close", handle => $qgafh);
+	raise_agent_error($res, "can't close file");
+
+	return undef;
+    }});
+
 1;
-- 
2.11.0




More information about the pve-devel mailing list