]>
git.proxmox.com Git - qemu-server.git/blob - PVE/API2/Qemu/Agent.pm
1 package PVE
::API2
::Qemu
::Agent
;
7 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::QemuServer
::Agent
qw(agent_available);
10 use MIME
::Base64
qw(encode_base64 decode_base64);
13 use base
qw(PVE::RESTHandler);
16 # will generate one api endpoint per command
17 # needs a 'method' property and optionally a 'perms' property (default VM.Monitor)
18 my $guest_agent_commands = {
28 'fsfreeze-status' => {
31 'fsfreeze-freeze' => {
40 'network-get-interfaces' => {
49 'get-memory-blocks' => {
52 'get-memory-block-info' => {
67 # added since qemu 2.9
82 __PACKAGE__-
>register_method({
87 description
=> "Qemu Agent command index.",
92 additionalProperties
=> 1,
94 node
=> get_standard_option
('pve-node'),
95 vmid
=> get_standard_option
('pve-vmid', {
96 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
105 links
=> [ { rel
=> 'child', href
=> '{name}' } ],
106 description
=> "Returns the list of Qemu Agent commands",
113 my $cmds = [keys %$guest_agent_commands];
118 for my $cmd ( sort @$cmds) {
119 push @$result, { name
=> $cmd };
125 sub register_command
{
126 my ($class, $command, $method, $perm) = @_;
128 die "no method given\n" if !$method;
129 die "no command given\n" if !defined($command);
133 if (ref($perm) eq 'HASH') {
136 $perm //= 'VM.Monitor';
137 $permission = { check
=> [ 'perm', '/vms/{vmid}', [ $perm ]]};
141 additionalProperties
=> 0,
143 node
=> get_standard_option
('pve-node'),
144 vmid
=> get_standard_option
('pve-vmid', {
145 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
148 description
=> "The QGA command.",
149 enum
=> [ sort keys %$guest_agent_commands ],
154 my $description = "Execute Qemu Guest Agent commands.";
157 if ($command ne '') {
158 $description = "Execute $command.";
160 delete $parameters->{properties
}->{command
};
163 __PACKAGE__-
>register_method({
169 description
=> $description,
170 permissions
=> $permission,
171 parameters
=> $parameters,
174 description
=> "Returns an object with a single `result` property.",
179 my $vmid = $param->{vmid
};
181 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
183 agent_available
($vmid, $conf);
185 my $cmd = $param->{command
} // $command;
186 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-$cmd");
188 return { result
=> $res };
192 # old {vmid}/agent POST endpoint, here for compatibility
193 __PACKAGE__-
>register_command('', 'POST');
195 for my $cmd (sort keys %$guest_agent_commands) {
196 my $props = $guest_agent_commands->{$cmd};
197 __PACKAGE__-
>register_command($cmd, $props->{method}, $props->{perms
});
200 # commands with parameters are complicated and we want to register them manually
201 __PACKAGE__-
>register_method({
202 name
=> 'set-user-password',
203 path
=> 'set-user-password',
207 description
=> "Sets the password for the given user to the given password",
208 permissions
=> { check
=> [ 'perm', '/vms/{vmid}', [ 'VM.Monitor' ]]},
210 additionalProperties
=> 0,
212 node
=> get_standard_option
('pve-node'),
213 vmid
=> get_standard_option
('pve-vmid', {
214 completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
217 description
=> 'The user to set the password for.'
221 description
=> 'The new password.',
227 description
=> 'set to 1 if the password has already been passed through crypt()',
235 description
=> "Returns an object with a single `result` property.",
240 my $vmid = $param->{vmid
};
242 my $crypted = $param->{crypted
} // 0;
244 username
=> $param->{username
},
245 password
=> encode_base64
($param->{password
}),
246 crypted
=> $crypted ? JSON
::true
: JSON
::false
,
248 my $res = agent_cmd
($vmid, "set-user-password", %$args, 'cannot set user password');
250 return { result
=> $res };