]> git.proxmox.com Git - qemu-server.git/commitdiff
implement file-write via guest-agent in the api
authorDominik Csapak <d.csapak@proxmox.com>
Tue, 26 Jun 2018 12:15:49 +0000 (14:15 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 11 Jul 2018 08:29:45 +0000 (10:29 +0200)
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@proxmox.com>
PVE/API2/Qemu/Agent.pm

index 4ff39fd57b0182533ffad9f482eb40c33d2ce5d7..75a58ce46a629952bcd50777ccf0daf04398c080 100644 (file)
@@ -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;