qemu-ga: add support for guest command execution
cover qemu-ga: add support for guest command execution
The code is still in rough shape, but while we're on the topic of guest agents I wanted to put out a working example of how exec functionality can be added to qemu-ga to provide a mechansim for building arbitrarilly high-level interfaces.
The hope is that by allowing qemu-ga to execute commands in the guest, paired with file read/write access, we can instrument a guest "on the fly" to support any type of hyperviser functionality, and do so without dramatically enlarging the role qemu-ga plays as a small, QEMU-specific agent that is tightly integrated with QEMU/QMP/libvirt.
These patches add the following interfaces:
guest-file-open-pipe
guest-exec
guest-exec-status
The guest-file-open-pipe interface is analagous to the existing guest-file-open interface (it might be best to roll it into it actually): it returns a handle that can be handled via the existing guest-file-{read,write,flush,close} interface. Internally it creates a FIFO pair that we can use to associate handles to the stdin/stdout/stderr of a guest-exec spawned process. We can also also use them to redirect output into other processes, giving us the basic tools to build a basic shell (or a full-blown one if we add TTY support) using a single qemu-ga.
Theoretically we can even deploy other agents, including session-level agents, and communicate with them via these same handles. Thus, ovirt could deploy and run an agent via qemu-ga, Spice could deploy vdagent, etc. Since the interface is somewhat tedious, I'm working on a wrapper script to try out some of these scenarios, but a basic use case using the raw QMP interface is included below.
Any thoughts/comments on this approach are appreciated.
EXAMPLE USAGE (execute top -b -n1):
{'execute': 'guest-file-open-pipe'}
{'return': 6}
{'execute': 'guest-exec', \
'arguments': {'detach': True, \
'handle_stdout': 6, \
'params': [{'param': '-b'}, \
{'param': '-n1'}], \
'path': 'top'}}
{'return': {'exit-code': 0, \
'exited': False, \
'handle_stderr': -1, \
'handle_stdin': -1, \
'handle_stdout': 6, \
'pid': 14267}}{'execute': 'guest-file-read', \
'arguments': {'count': 65536, \
'handle': 6}}
{'return': {'buf-b64': '', \
'count': 0, \
'eof': False}}{'execute': 'guest-file-read', \
'arguments': {'count': 65536, \
'handle': 6}}
{'return': {'buf-b64': 'dG9wIC0gMjI6N...', \
'count': 11064, \
'eof': True}}/*
top - 22:41:49 up 1 day, 4:30, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 114 total, 1 running, 113 sleeping, 0 stopped, 0 zombie Cpu(s): 0.2%us, 0.2%sy, 0.0%ni, 99.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 504848k total, 445664k used, 59184k free, 49100k buffers Swap: 323580k total, 224k used, 323356k free, 256392k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
14267 root 20 0 19272 1248 924 R 2 0.2 0:00.02 top
1 root 20 0 24008 2048 1280 S 0 0.4 0:00.85 init
2 root 20 0 0 0 0 S 0 0.0 0:00.30 kthreadd
3 root 20 0 0 0 0 S 0 0.0 0:01.09 ksoftirqd/0...
*/
{'execute': 'guest-exec-status', \
'arguments': {'pid': 14267}}
{'return': {'exit-code': 0, \
'exited': True, \
'handle_stderr': -1, \
'handle_stdin': -1, \
'handle_stdout': 6, \
'pid': 14267}}{'execute': 'guest-file-close'} \
'arguments': {'handle': 6}}
{'return': {}}
Michael Roth (2):
guest agent: add guest-file-open-pipe
guest agent: add guest-exec and guest-exec-status interfaces
qapi-schema-guest.json | 79 +++++++- qga/guest-agent-commands.c | 478 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 531 insertions(+), 26 deletions(-)
Signature · 1 line
1.7.4.1
PATCH 1/2 guest agent: add guest-file-open-pipe
Creates a FIFO pair that can be used with existing file read/write interfaces to communicate with processes spawned via the forthcoming guest-file-exec interface.
--- qapi-schema-guest.json | 24 ++++++- qga/guest-agent-commands.c | 179 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 177 insertions(+), 26 deletions(-)
qapi-schema-guest.json +23 -1
@@ -80,18 +80,40 @@ 'returns': 'int' } ## +# @guest-file-open-pipe +# +# Open a pipe to in the guest to associated with a qga-spawned processes +# for communication. +# +# Returns: Guest file handle on success, as per guest-file-open. This +# handle is useable with the same interfaces as a handle returned by +# guest-file-open. +# +# Since: 1.0.50 +## +{ 'command': 'guest-file-open-pipe', + 'returns': 'int' } + +## # @guest-file-close: # # Close an open file in the guest # # @handle: filehandle returned by guest-file-open +# @pipe-end: #optional GuestFilePipeEnd value ("rw"/"w"/"r") to specify +# which end of the pipe to close. Please note that closing the write +# side of a pipe will block until the read side is closed. If you've +# passed the read-side of the pipe to a qga-spawned process, make sure +# the process as exited before attempting to close the write side. # # Returns: Nothing on success. # # Since: 0.15.0 ## +{ 'enum': 'GuestFilePipeEnd', + 'data': [ 'r', 'w', 'rw' ] } { 'command': 'guest-file-close', - 'data': { 'handle': 'int' } } + 'data': { 'handle': 'int', '*pipe-end': 'GuestFilePipeEnd' } } ## # @guest-file-read:
qga/guest-agent-commands.c +154 -25
258 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH 2/2 guest agent: add guest-exec and guest-exec-status interfaces
In reply to Michael RothInterfaces to execute/manage processes in the guest. Child process' stdin/stdout/stderr can be associated with handles for communication via read/write interfaces.
--- qapi-schema-guest.json | 55 ++++++++ qga/guest-agent-commands.c | 299 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 354 insertions(+), 0 deletions(-)
qapi-schema-guest.json +55 -0
@@ -237,3 +237,58 @@ ## { 'command': 'guest-fsfreeze-thaw', 'returns': 'int' } + +## +# @guest-exec-status +# +# Check status of process associated with PID retrieved via guest-exec. +# Reap the process and associated metadata if it has exited. +# +# @pid: pid returned from guest-exec +# @wait: #optional whether to wait for completion or simply poll once. +# Waiting is disallowed if a stdin/stdout/stderr handle was supplied +# to guest-exec +# +# Returns: GuestExecStatus on success +# +{ 'type': 'GuestExecStatus', + 'data': { 'pid': 'int', 'exited': 'bool', 'exit-code': 'int', + 'handle_stdin': 'int', 'handle_stdout': 'int', + 'handle_stderr': 'int' } } +{ 'command': 'guest-exec-status', + 'data': { 'pid': 'int', '*wait': 'bool' }, + 'returns': 'GuestExecStatus' } + +## +# @guest-exec: +# +# Execute a command in the guest +# +# If a pipe is associated with the resulting process, the +# read/write/write sides of the process' stdin/stdout/stderr will +# be transferred automatically, so no need to close them from the +# client. If no handle is passed in for stdin/stdout/stderr, they +# will be closed before executing the command. +# +# @path: path or executable name to execute +# @params: #optional parameter list to pass to executable +# @handle_stdin: #optional handle to associate with process' stdin. +# @handle_stdout: #optional handle to associate with process' stdout +# @handle_stderr: #optional handle to associate with process' stderr +# @detach: #optional whether to detach the process or execute it +# synchronously. If synchronous, passing of stdin/stdout/stderr handles +# will be disallowed, since process can block on closing them and cause +# a deadlock in the guest agent. +# +# Returns: GuestExecStatus on success. +# +# Since: 1.0.50 +## +{ 'type': 'GuestExecParam', + 'data': { 'param': 'str' } } +{ 'command': 'guest-exec', + 'data': { 'path': 'str', '*params': ['GuestExecParam'], + '*handle_stdin': 'int', '*handle_stdout': 'int', + '*handle_stderr': 'int', + '*detach': 'bool' }, + 'returns': 'GuestExecStatus' }
qga/guest-agent-commands.c +299 -0
309 lines not on this page · raw message
Signature · 1 line
1.7.4.1
Quoted 40 lines · Michael Roth
The code is still in rough shape, but while we're on the topic of guest agents I wanted to put out a working example of how exec functionality can be added to qemu-ga to provide a mechansim for building arbitrarilly high-level interfaces.
The hope is that by allowing qemu-ga to execute commands in the guest, paired with file read/write access, we can instrument a guest "on the fly" to support any type of hyperviser functionality, and do so without dramatically enlarging the role qemu-ga plays as a small, QEMU-specific agent that is tightly integrated with QEMU/QMP/libvirt.
These patches add the following interfaces:
guest-file-open-pipe
guest-exec
guest-exec-statusThe guest-file-open-pipe interface is analagous to the existing guest-file-open interface (it might be best to roll it into it actually): it returns a handle that can be handled via the existing guest-file-{read,write,flush,close} interface. Internally it creates a FIFO pair that we can use to associate handles to the stdin/stdout/stderr of a guest-exec spawned process. We can also also use them to redirect output into other processes, giving us the basic tools to build a basic shell (or a full-blown one if we add TTY support) using a single qemu-ga.
Theoretically we can even deploy other agents, including session-level agents, and communicate with them via these same handles. Thus, ovirt could deploy and run an agent via qemu-ga, Spice could deploy vdagent, etc. Since the interface is somewhat tedious, I'm working on a wrapper script to try out some of these scenarios, but a basic use case using the raw QMP interface is included below.
Any thoughts/comments on this approach are appreciated.
EXAMPLE USAGE (execute
top -b -n1):{'execute': 'guest-file-open-pipe'}
{'return': 6}
Quoted 6 lines
{'execute': 'guest-exec', \
'arguments': {'detach': True, \
'handle_stdout': 6, \
'params': [{'param': '-b'}, \
{'param': '-n1'}], \
'path': 'top'}}This feels like a rather verbose way of specifying the ARGV. Why not just allow
{'execute': 'guest-exec', \
'arguments': {'detach': True, \
'handle_stdout': 6, \
'params': ['-b', '-n1'], \
'path': 'top'}}Or even
{'execute': 'guest-exec', \
'arguments': {'detach': True, \
'handle_stdout': 6, \
'argv': ['top', '-b', '-n1']}} \and just use the first element of argv as the binary to execute. Also you might need to set env variables for some tools, so we'd want
{'execute': 'guest-exec', \
'arguments': {'detach': True, \
'handle_stdout': 6, \
'argv': ['top', '-b', '-n1'], \
'env' : ['TMPDIR=/wibble']}}and perhaps also you might want to run as a non-root user, so allow a username/groupname ?
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Quoted 57 lines · Daniel P. Berrange
Quoted 40 lines · Michael Roth
The code is still in rough shape, but while we're on the topic of guest agents I wanted to put out a working example of how exec functionality can be added to qemu-ga to provide a mechansim for building arbitrarilly high-level interfaces.
The hope is that by allowing qemu-ga to execute commands in the guest, paired with file read/write access, we can instrument a guest "on the fly" to support any type of hyperviser functionality, and do so without dramatically enlarging the role qemu-ga plays as a small, QEMU-specific agent that is tightly integrated with QEMU/QMP/libvirt.
These patches add the following interfaces:
guest-file-open-pipe
guest-exec
guest-exec-statusThe guest-file-open-pipe interface is analagous to the existing guest-file-open interface (it might be best to roll it into it actually): it returns a handle that can be handled via the existing guest-file-{read,write,flush,close} interface. Internally it creates a FIFO pair that we can use to associate handles to the stdin/stdout/stderr of a guest-exec spawned process. We can also also use them to redirect output into other processes, giving us the basic tools to build a basic shell (or a full-blown one if we add TTY support) using a single qemu-ga.
Theoretically we can even deploy other agents, including session-level agents, and communicate with them via these same handles. Thus, ovirt could deploy and run an agent via qemu-ga, Spice could deploy vdagent, etc. Since the interface is somewhat tedious, I'm working on a wrapper script to try out some of these scenarios, but a basic use case using the raw QMP interface is included below.
Any thoughts/comments on this approach are appreciated.
EXAMPLE USAGE (execute
top -b -n1):{'execute': 'guest-file-open-pipe'}
{'return': 6}Quoted 6 lines
{'execute': 'guest-exec', \
'arguments': {'detach': True, \ 'handle_stdout': 6, \ 'params': [{'param': '-b'}, \ {'param': '-n1'}], \ 'path': 'top'}}This feels like a rather verbose way of specifying the ARGV. Why not just allow
{'execute': 'guest-exec', \ 'arguments': {'detach': True, \ 'handle_stdout': 6, \ 'params': ['-b', '-n1'], \ 'path': 'top'}}
Quoted 8 lines
Or even
{'execute': 'guest-exec', \ 'arguments': {'detach': True, \ 'handle_stdout': 6, \ 'argv': ['top', '-b', '-n1']}} \
Agreed, this would look way nicer and is what I was shooting for initially. Unfortunately, the QAPI-generated QMP marshalling code, and the visitor interfaces it uses, expects lists to be of structured types. We might be able to special-case it for int and char* arrays though and open-code it though...
The problem I ran into when I looked at it though is that the QMP request is a QObject at that point that's contained inside a Visitor. So to manipulate it we'd have to extract the QList and manipulate it outside the visitor, which would be difficult since the Visitor is using a stack to manage it's traversal of the QObject based on the visitor calls we make, so it's difficult to pull it out of there...
Although, maybe we could just add a qmp_input_vistor_stack_top() that gives us the top of the stack, then call it at the appropriate point in the marshalling code and key into it to get qobj{'argv'} and whatnot. It's kinda hacky, but it's QMP-specific, and useful, so it might be worth it.
Alternatively, we could introduce another Visitor interface for handling lists of int64_t and char*.
So maybe it's not so bad, I'll look into it more.
Quoted 4 lines
and just use the first element of argv as the binary to execute. Also you might need to set env variables for some tools, so we'd want
Quoted 9 lines
{'execute': 'guest-exec', \ 'arguments': {'detach': True, \ 'handle_stdout': 6, \ 'argv': ['top', '-b', '-n1'], \ 'env' : ['TMPDIR=/wibble']}}and perhaps also you might want to run as a non-root user, so allow a username/groupname ?
Good idea, this would open up a lot of potential use cases (X session/display management, copy/paste, etc).
Quoted 2 lines
Regards,
Daniel
Quoted 28 lines · Michael Roth
Quoted 27 lines · Daniel P. Berrange
Quoted 26 lines · Michael Roth
The code is still in rough shape, but while we're on the topic of guest agents I wanted to put out a working example of how exec functionality can be added to qemu-ga to provide a mechansim for building arbitrarilly high-level interfaces.
The hope is that by allowing qemu-ga to execute commands in the guest, paired with file read/write access, we can instrument a guest "on the fly" to support any type of hyperviser functionality, and do so without dramatically enlarging the role qemu-ga plays as a small, QEMU-specific agent that is tightly integrated with QEMU/QMP/libvirt.
These patches add the following interfaces:
guest-file-open-pipe
guest-exec
guest-exec-statusThe guest-file-open-pipe interface is analagous to the existing guest-file-open interface (it might be best to roll it into it actually): it returns a handle that can be handled via the existing guest-file-{read,write,flush,close} interface. Internally it creates a FIFO pair that we can use to associate handles to the stdin/stdout/stderr of a guest-exec spawned process. We can also also use them to redirect output into other processes, giving us the basic tools to build a basic shell (or a full-blown one if we add TTY support) using a single qemu-ga.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Theoretically we can even deploy other agents, including session-level agents, and communicate with them via these same handles. Thus, ovirt could deploy and run an agent via qemu-ga, Spice could deploy vdagent, etc. Since the interface is somewhat tedious, I'm working on a wrapper script to try out some of these scenarios, but a basic use case using the raw QMP interface is included below.
Any thoughts/comments on this approach are appreciated.
Is there a seperate transport (another virtio channel) for the launched guest process? I'm thinking as usual on the copy/paste functionallity, when passing a large buffer, I would not want to block any other future qemu-ga command. Also I would like to avoid having to uuencode/decode the traffic.
Quoted 77 lines
Quoted 22 lines
Quoted 6 lines
EXAMPLE USAGE (execute
top -b -n1):{'execute': 'guest-file-open-pipe'}
{'return': 6}Quoted 6 lines
{'execute': 'guest-exec', \ 'arguments': {'detach': True, \ 'handle_stdout': 6, \ 'params': [{'param': '-b'}, \ {'param': '-n1'}], \ 'path': 'top'}}This feels like a rather verbose way of specifying the ARGV. Why not just allow
{'execute': 'guest-exec', \ 'arguments': {'detach': True, \ 'handle_stdout': 6, \ 'params': ['-b', '-n1'], \ 'path': 'top'}}Quoted 8 lines
Or even
{'execute': 'guest-exec', \ 'arguments': {'detach': True, \ 'handle_stdout': 6, \ 'argv': ['top', '-b', '-n1']}} \Agreed, this would look way nicer and is what I was shooting for initially. Unfortunately, the QAPI-generated QMP marshalling code, and the visitor interfaces it uses, expects lists to be of structured types. We might be able to special-case it for int and char* arrays though and open-code it though...
The problem I ran into when I looked at it though is that the QMP request is a QObject at that point that's contained inside a Visitor. So to manipulate it we'd have to extract the QList and manipulate it outside the visitor, which would be difficult since the Visitor is using a stack to manage it's traversal of the QObject based on the visitor calls we make, so it's difficult to pull it out of there...
Although, maybe we could just add a
qmp_input_vistor_stack_top()that gives us the top of the stack, then call it at the appropriate point in the marshalling code and key into it to get qobj{'argv'} and whatnot. It's kinda hacky, but it's QMP-specific, and useful, so it might be worth it.Alternatively, we could introduce another Visitor interface for handling lists of
int64_tand char*.So maybe it's not so bad, I'll look into it more.
Quoted 4 lines
and just use the first element of argv as the binary to execute. Also you might need to set env variables for some tools, so we'd want
Quoted 9 lines
{'execute': 'guest-exec', \ 'arguments': {'detach': True, \ 'handle_stdout': 6, \ 'argv': ['top', '-b', '-n1'], \ 'env' : ['TMPDIR=/wibble']}}and perhaps also you might want to run as a non-root user, so allow a username/groupname ?
Good idea, this would open up a lot of potential use cases (X session/display management, copy/paste, etc).
Quoted 2 lines
Regards,
Daniel