]>
git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu/Agent.pm
51fb0d8e089a1544fe75da1a2ae852fe81644d83
1 package PVE
::API2
::Qemu
::Agent
;
7 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::QemuServer
::Agent
qw(agent_available agent_cmd check_agent_error);
10 use MIME
::Base64
qw(encode_base64 decode_base64);
13 use base
qw(PVE::RESTHandler);
15 # max size for file-read over the api
16 my $MAX_READ_SIZE = 16 * 1024 * 1024; # 16 MiB
19 # will generate one api endpoint per command
20 # needs a 'method' property and optionally a 'perms' property (default VM.Monitor)
21 my $guest_agent_commands = {
31 'fsfreeze-status' => {
34 'fsfreeze-freeze' => {
43 'network-get-interfaces' => {
52 'get-memory-blocks' => {
55 'get-memory-block-info' => {
70 # added since qemu 2.9
85 __PACKAGE__-
>register_method({
90 description
=> "Qemu Agent command index.",
95 additionalProperties
=> 1,
97 node
=> get_standard_option
('pve-node'),
98 vmid
=> get_standard_option
('pve-vmid', {
99 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
108 links
=> [ { rel
=> 'child', href
=> '{name}' } ],
109 description
=> "Returns the list of Qemu Agent commands",
116 my $cmds = [keys %$guest_agent_commands];
125 for my $cmd ( sort @$cmds) {
126 push @$result, { name
=> $cmd };
132 sub register_command
{
133 my ($class, $command, $method, $perm) = @_;
135 die "no method given\n" if !$method;
136 die "no command given\n" if !defined($command);
140 if (ref($perm) eq 'HASH') {
143 $perm //= 'VM.Monitor';
144 $permission = { check
=> [ 'perm', '/vms/{vmid}', [ $perm ]]};
148 additionalProperties
=> 0,
150 node
=> get_standard_option
('pve-node'),
151 vmid
=> get_standard_option
('pve-vmid', {
152 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
155 description
=> "The QGA command.",
156 enum
=> [ sort keys %$guest_agent_commands ],
161 my $description = "Execute Qemu Guest Agent commands.";
164 if ($command ne '') {
165 $description = "Execute $command.";
167 delete $parameters->{properties
}->{command
};
170 __PACKAGE__-
>register_method({
176 description
=> $description,
177 permissions
=> $permission,
178 parameters
=> $parameters,
181 description
=> "Returns an object with a single `result` property.",
186 my $vmid = $param->{vmid
};
188 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
190 agent_available
($vmid, $conf);
192 my $cmd = $param->{command
} // $command;
193 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-$cmd");
195 return { result
=> $res };
199 # old {vmid}/agent POST endpoint, here for compatibility
200 __PACKAGE__-
>register_command('', 'POST');
202 for my $cmd (sort keys %$guest_agent_commands) {
203 my $props = $guest_agent_commands->{$cmd};
204 __PACKAGE__-
>register_command($cmd, $props->{method}, $props->{perms
});
207 # commands with parameters are complicated and we want to register them manually
208 __PACKAGE__-
>register_method({
209 name
=> 'set-user-password',
210 path
=> 'set-user-password',
214 description
=> "Sets the password for the given user to the given password",
215 permissions
=> { check
=> [ 'perm', '/vms/{vmid}', [ 'VM.Monitor' ]]},
217 additionalProperties
=> 0,
219 node
=> get_standard_option
('pve-node'),
220 vmid
=> get_standard_option
('pve-vmid', {
221 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
224 description
=> 'The user to set the password for.'
228 description
=> 'The new password.',
234 description
=> 'set to 1 if the password has already been passed through crypt()',
242 description
=> "Returns an object with a single `result` property.",
247 my $vmid = $param->{vmid
};
249 my $crypted = $param->{crypted
} // 0;
251 username
=> $param->{username
},
252 password
=> encode_base64
($param->{password
}),
253 crypted
=> $crypted ? JSON
::true
: JSON
::false
,
255 my $res = agent_cmd
($vmid, "set-user-password", $args, 'cannot set user password');
257 return { result
=> $res };
260 __PACKAGE__-
>register_method({
266 description
=> "Executes the given command in the vm via the guest-agent and returns an object with the pid.",
267 permissions
=> { check
=> [ 'perm', '/vms/{vmid}', [ 'VM.Monitor' ]]},
269 additionalProperties
=> 0,
271 node
=> get_standard_option
('pve-node'),
272 vmid
=> get_standard_option
('pve-vmid', {
273 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
276 format
=> 'string-alist',
277 description
=> 'The command as a list of program + arguments',
286 description
=> "The PID of the process started by the guest-agent.",
293 my $vmid = $param->{vmid
};
294 my $cmd = [PVE
::Tools
::split_list
($param->{command
})];
296 my $res = PVE
::QemuServer
::Agent
::qemu_exec
($vmid, $cmd);
300 __PACKAGE__-
>register_method({
301 name
=> 'exec-status',
302 path
=> 'exec-status',
306 description
=> "Gets the status of the given pid started by the guest-agent",
307 permissions
=> { check
=> [ 'perm', '/vms/{vmid}', [ 'VM.Monitor' ]]},
309 additionalProperties
=> 0,
311 node
=> get_standard_option
('pve-node'),
312 vmid
=> get_standard_option
('pve-vmid', {
313 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
316 description
=> 'The PID to query'
325 description
=> 'Tells if the given command has exited yet.',
330 description
=> 'process exit code if it was normally terminated.',
335 description
=> 'signal number or exception code if the process was abnormally terminated.',
340 description
=> 'stdout of the process',
345 description
=> 'stderr of the process',
350 description
=> 'true if stdout was not fully captured',
355 description
=> 'true if stderr was not fully captured',
362 my $vmid = $param->{vmid
};
363 my $pid = int($param->{pid
});
365 my $res = PVE
::QemuServer
::Agent
::qemu_exec_status
($vmid, $pid);
370 __PACKAGE__-
>register_method({
376 description
=> "Reads the given file via guest agent. Is limited to $MAX_READ_SIZE bytes.",
377 permissions
=> { check
=> [ 'perm', '/vms/{vmid}', [ 'VM.Monitor' ]]},
379 additionalProperties
=> 0,
381 node
=> get_standard_option
('pve-node'),
382 vmid
=> get_standard_option
('pve-vmid', {
383 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
386 description
=> 'The path to the file'
392 description
=> "Returns an object with a `content` property.",
396 description
=> "The content of the file, maximum $MAX_READ_SIZE",
401 description
=> "If set to 1, the output is truncated and not complete"
408 my $vmid = $param->{vmid
};
410 my $qgafh = agent_cmd
($vmid, "file-open", { path
=> $param->{file
} }, "can't open file");
412 my $bytes_left = $MAX_READ_SIZE;
414 my $read_size = 1024*1024;
417 while ($bytes_left > 0 && !$eof) {
418 my $read = PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-file-read", handle
=> $qgafh, count
=> int($read_size));
419 check_agent_error
($read, "can't read from file");
421 $content .= decode_base64
($read->{'buf-b64'});
422 $bytes_left -= $read->{count
};
423 $eof = $read->{eof} // 0;
426 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-file-close", handle
=> $qgafh);
427 check_agent_error
($res, "can't close file", 1);
431 'bytes-read' => ($MAX_READ_SIZE-$bytes_left),
435 warn "agent file-read: reached maximum read size: $MAX_READ_SIZE bytes. output might be truncated.\n";
436 $result->{truncated
} = 1;
442 __PACKAGE__-
>register_method({
443 name
=> 'file-write',
444 path
=> 'file-write',
448 description
=> "Writes the given file via guest agent.",
449 permissions
=> { check
=> [ 'perm', '/vms/{vmid}', [ 'VM.Monitor' ]]},
451 additionalProperties
=> 0,
453 node
=> get_standard_option
('pve-node'),
454 vmid
=> get_standard_option
('pve-vmid', {
455 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
458 description
=> 'The path to the file.'
462 maxLength
=> 60*1024, # 60k, smaller than our 64k POST limit
463 description
=> "The content to write into the file."
467 returns
=> { type
=> 'null' },
471 my $vmid = $param->{vmid
};
472 my $buf = encode_base64
($param->{content
});
474 my $qgafh = agent_cmd
($vmid, "file-open", { path
=> $param->{file
}, mode
=> 'wb' }, "can't open file");
475 my $write = agent_cmd
($vmid, "file-write", { handle
=> $qgafh, 'buf-b64' => $buf }, "can't write to file");
476 my $res = agent_cmd
($vmid, "file-close", { handle
=> $qgafh }, "can't close file");