Skip to contentSkip to conversation

RFC: Proposal to add QEMU "Guest Environment Variables"

Qemu-devel Gabriel L. Somlo 8 participants 20 messages Feb 3, 2015 – Feb 4, 2015

lore
Share & tools
lore
#0 lore raw

Hi,

I'm interested in adding a way for a host to pass environment variables into a qemu guest VM -- analogous to setting environment variables for a process to access via getenv() and friends.

The QEMU Guest Agent (QGA) does not appear to quite fit the bill, at least not in its current form: The agent must have been successfully started on the guest before the host would have to connect to it (in a separate act from just starting the guest in the first place), and get it to execute any hypothetical commands to configure or otherwise influence the guest.

For this functionality to really resemble the way environment variables are used, environment information should be provided as part of the QEMU command line, without the requirement to make a separate/subsequent connection to the guest agent. For example:

    qemu-system-x86_64 -guest-env="VAR1=value1;VAR2=value 2" -hda image.qcow2

Once the guest is started, it should be possible to query the guest environment with something like:

    $ qemu-guest-env get VAR1
    value1
    $ qemu-guest-env get VAR2
    value 2

So far, the environment would be read-only from the guest perspective. A nice additional feature would be to allow setting additional guest-env variables, or replacing values for existing ones:

    $ qemu-guest-env set VAR3 "foo bar"
    $ qemu-guest-env set VAR4 "blah"

The actual QGA could then be taught to export guest-env set and get commands, so that querying/setting data in the guest environment could be allowed from the outside, via e.g. 'virsh qemu-agent-command'.

The 'qemu-guest-env' functionality could be incorporated into 'qemu-ga', or left as a separate (but closely related) binary, most likely packaged together with qemu-ga.

In terms of the mechanics, I'll need to figure out the following:

1. How would -guest-env="..." actually pass data into the guest?

Off the top of my head, we could piggyback on top of smbios (by using a type 11, "OEM strings" structure), which could then be parsed by qemu-guest-env from the guest side.

2. How to support "set-env" (in addition to "get-env") functionality?

set-env means access to a writable guest memory area, and the least inconvenient way I can think of accomplishing this would be to put QGA in charge of it. When starting up for the first time, QGA would grab the host-supplied environment (e.g. by parsing smbios type 11), then respond to subsequent get and set requests from its in-memory environment key-value store. Changes to the environment made after the guest was started would not be persistent, but that's not something currently offered by any other platforms either :)

Ultimately, if this makes it into QEMU, I'd like to also expose the functionality through libvirt/virsh/virt-manager (so users could set the initial environment variables as part of the guest VM xml template, via the gui, etc.)

So, my question for the QEMU dev team:

1. Would you consider this feature a useful addition to QEMU ? I.e., would this be acceptable (of interest) to the upstream project?

2. Is anything similar already being worked on (so I could either join that effort, or back off, as the case may be) ? :)

3. Any technical advice on how to (better) implement it ?

Thanks much,
--Gabriel

#1 lore raw
Quoted 74 lines · Gabriel L. Somlo

Hi,

I'm interested in adding a way for a host to pass environment variables into a qemu guest VM -- analogous to setting environment variables for a process to access via getenv() and friends.

The QEMU Guest Agent (QGA) does not appear to quite fit the bill, at least not in its current form: The agent must have been successfully started on the guest before the host would have to connect to it (in a separate act from just starting the guest in the first place), and get it to execute any hypothetical commands to configure or otherwise influence the guest.

For this functionality to really resemble the way environment variables are used, environment information should be provided as part of the QEMU command line, without the requirement to make a separate/subsequent connection to the guest agent. For example:

     qemu-system-x86_64 -guest-env="VAR1=value1;VAR2=value 2" -hda image.qcow2

Once the guest is started, it should be possible to query the guest environment with something like:

     $ qemu-guest-env get VAR1
     value1
     $ qemu-guest-env get VAR2
     value 2

So far, the environment would be read-only from the guest perspective. A nice additional feature would be to allow setting additional guest-env variables, or replacing values for existing ones:

     $ qemu-guest-env set VAR3 "foo bar"
     $ qemu-guest-env set VAR4 "blah"

The actual QGA could then be taught to export guest-env set and get commands, so that querying/setting data in the guest environment could be allowed from the outside, via e.g. 'virsh qemu-agent-command'.

The 'qemu-guest-env' functionality could be incorporated into 'qemu-ga', or left as a separate (but closely related) binary, most likely packaged together with qemu-ga.

In terms of the mechanics, I'll need to figure out the following:

1. How would -guest-env="..." actually pass data into the guest?

Off the top of my head, we could piggyback on top of smbios (by using a type 11, "OEM strings" structure), which could then be parsed by qemu-guest-env from the guest side.

2. How to support "set-env" (in addition to "get-env") functionality?

set-env means access to a writable guest memory area, and the least inconvenient way I can think of accomplishing this would be to put QGA in charge of it. When starting up for the first time, QGA would grab the host-supplied environment (e.g. by parsing smbios type 11), then respond to subsequent get and set requests from its in-memory environment key-value store. Changes to the environment made after the guest was started would not be persistent, but that's not something currently offered by any other platforms either :)

Ultimately, if this makes it into QEMU, I'd like to also expose the functionality through libvirt/virsh/virt-manager (so users could set the initial environment variables as part of the guest VM xml template, via the gui, etc.)

So, my question for the QEMU dev team:

1. Would you consider this feature a useful addition to QEMU ? I.e., would this be acceptable (of interest) to the upstream project?

Quoted 8 lines

2. Is anything similar already being worked on (so I could either join that effort, or back off, as the case may be) ? :)

3. Any technical advice on how to (better) implement it ?

Thanks much,
--Gabriel

I think that you can do this through guest write/guest exec commands. You can configure service start to perform it is a proper way through guest read/guest write and after that restart the agent.

Unfortunately guest exec functionality is still pending...

#2 lore raw

Hi Denis,

Quoted 26 lines · Denis V. Lunev
Quoted 25 lines · Gabriel L. Somlo

I'm interested in adding a way for a host to pass environment variables into a qemu guest VM -- analogous to setting environment variables for a process to access via getenv() and friends.

The QEMU Guest Agent (QGA) does not appear to quite fit the bill, at least not in its current form: The agent must have been successfully started on the guest before the host would have to connect to it (in a separate act from just starting the guest in the first place), and get it to execute any hypothetical commands to configure or otherwise influence the guest.

For this functionality to really resemble the way environment variables are used, environment information should be provided as part of the QEMU command line, without the requirement to make a separate/subsequent connection to the guest agent. For example:

    qemu-system-x86_64 -guest-env="VAR1=value1;VAR2=value 2" -hda image.qcow2

Once the guest is started, it should be possible to query the guest environment with something like:

    $ qemu-guest-env get VAR1
    value1
Quoted 7 lines
Quoted 3 lines
    $ qemu-guest-env get VAR2
    value 2
[...]

I think that you can do this through guest write/guest exec commands. You can configure service start to perform it is a proper way through guest read/guest write and after that restart the agent.

Are these supposed to be commands issued from the qemu monitor prompt (or qmp) ? My Google-fu didn't help too much either :)

The main idea is to be able to provide environment data to a guest at start time (on the qemu command line), and not have to worry about connecting back to it (via monitor prompt, qmp, if/when it starts the guest agent, etc) to pass more information to it later on.

I'm probably misunderstanding what you said, though, so please clarify...

Thanks much,
--Gabriel

#3 lore raw
Quoted 45 lines · Gabriel L. Somlo

Hi Denis,

Quoted 26 lines · Denis V. Lunev
Quoted 25 lines · Gabriel L. Somlo

I'm interested in adding a way for a host to pass environment variables into a qemu guest VM -- analogous to setting environment variables for a process to access via getenv() and friends.

The QEMU Guest Agent (QGA) does not appear to quite fit the bill, at least not in its current form: The agent must have been successfully started on the guest before the host would have to connect to it (in a separate act from just starting the guest in the first place), and get it to execute any hypothetical commands to configure or otherwise influence the guest.

For this functionality to really resemble the way environment variables are used, environment information should be provided as part of the QEMU command line, without the requirement to make a separate/subsequent connection to the guest agent. For example:

     qemu-system-x86_64 -guest-env="VAR1=value1;VAR2=value 2" -hda image.qcow2

Once the guest is started, it should be possible to query the guest environment with something like:

     $ qemu-guest-env get VAR1
     value1
Quoted 7 lines
Quoted 3 lines
     $ qemu-guest-env get VAR2
     value 2
[...]

I think that you can do this through guest write/guest exec commands. You can configure service start to perform it is a proper way through guest read/guest write and after that restart the agent.

Are these supposed to be commands issued from the qemu monitor prompt (or qmp) ? My Google-fu didn't help too much either :)

The main idea is to be able to provide environment data to a guest at start time (on the qemu command line), and not have to worry about connecting back to it (via monitor prompt, qmp, if/when it starts the guest agent, etc) to pass more information to it later on.

Quoted 6 lines

I'm probably misunderstanding what you said, though, so please clarify...

Thanks much,
--Gabriel

QEMU guest agent starts as a service inside the guest. Its startup is controlled through standard Unix scripts. You can modify these scrips from the host side and (hopefully) restart agent.

The modification could be made using

##
# @GuestFileRead
#
# Result of guest agent file-read operation
#
# @count: number of bytes read (note: count is *before*
# base64-encoding is applied)
#
# @buf-b64: base64-encoded bytes read
#
# @eof: whether EOF was encountered during read operation. #
# Since: 0.15.0
##
{ 'type': 'GuestFileRead',
'data': { 'count': 'int', 'buf-b64': 'str', 'eof': 'bool' } }

##
# @GuestFileWrite
#
# Result of guest agent file-write operation
#
# @count: number of bytes written (note: count is actual bytes
# written, after base64-decoding of provided buffer)
#
# @eof: whether EOF was encountered during write operation. #
# Since: 0.15.0
##
{ 'type': 'GuestFileWrite',
'data': { 'count': 'int', 'eof': 'bool' } }

Den

#4 lore raw
In reply to Gabriel L. Somlo
Quoted 6 lines · Gabriel L. Somlo

Hi,

I'm interested in adding a way for a host to pass environment variables into a qemu guest VM -- analogous to setting environment variables for a process to access via getenv() and friends.

Quoted 6 lines

The QEMU Guest Agent (QGA) does not appear to quite fit the bill, at least not in its current form: The agent must have been successfully started on the guest before the host would have to connect to it (in a separate act from just starting the guest in the first place), and get it to execute any hypothetical commands to configure or otherwise influence the guest.

Have you considered passing SMBIOS information to the guest? Right now, qemu supports passing specific pieces of information through table 1 and 2, and arbitrary pre-populated binary files for other tables. SMBIOS data is directly readable by the guest when it boots.

Quoted 7 lines

In terms of the mechanics, I'll need to figure out the following:

1. How would -guest-env="..." actually pass data into the guest?

Off the top of my head, we could piggyback on top of smbios (by using a type 11, "OEM strings" structure), which could then be parsed by qemu-guest-env from the guest side.

Ah, so you have thought about it. The question then becomes writing guest software to conveniently take advantage of whatever you arbitrarily pass down, as well as adding sugar on the host side to make it easier to package the information to be sent into the guest (easier than a binary SMBIOS blob, at any rate).

Quoted 11 lines

2. How to support "set-env" (in addition to "get-env") functionality?

set-env means access to a writable guest memory area, and the least inconvenient way I can think of accomplishing this would be to put QGA in charge of it. When starting up for the first time, QGA would grab the host-supplied environment (e.g. by parsing smbios type 11), then respond to subsequent get and set requests from its in-memory environment key-value store. Changes to the environment made after the guest was started would not be persistent, but that's not something currently offered by any other platforms either :)

Are you proposing that the host can change the environment that the guest would re-query on the fly, or that the host's contribution is startup-only, and all further setting of variables is guest-side only? Or some hybrid where the initial environment is host->guest, but then QGA allows either side to communicate further changes in either direction?

Quoted 10 lines

Ultimately, if this makes it into QEMU, I'd like to also expose the functionality through libvirt/virsh/virt-manager (so users could set the initial environment variables as part of the guest VM xml template, via the gui, etc.)

So, my question for the QEMU dev team:

1. Would you consider this feature a useful addition to QEMU ? I.e., would this be acceptable (of interest) to the upstream project?

Seems like it is reasonable, from my point of view.

Quoted 4 lines

2. Is anything similar already being worked on (so I could either join that effort, or back off, as the case may be) ? :)

Not that I'm aware of, although the work on making qga able to request arbitrary process exec within the guest might be a piece of the puzzle for letting the host easily call into your proposed qemu-guest-env program.

Quoted 1 line

3. Any technical advice on how to (better) implement it ?

Your ideas seem reasonable as a first place to start. You may still want to consider setting up a file in the guest, rather than relying on in-memory representation, so that things in the guest can survive a guest-agent restart. It would certainly be wise to propose a spec of the initial SMBIOS string passing and subsequent QGA commands involved for coordinating the host->guest relationship, as a first patch in any proposed series.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw
Quoted 14 lines · Eric Blake
Quoted 6 lines · Gabriel L. Somlo

Hi,

I'm interested in adding a way for a host to pass environment variables into a qemu guest VM -- analogous to setting environment variables for a process to access via getenv() and friends.

Quoted 6 lines

The QEMU Guest Agent (QGA) does not appear to quite fit the bill, at least not in its current form: The agent must have been successfully started on the guest before the host would have to connect to it (in a separate act from just starting the guest in the first place), and get it to execute any hypothetical commands to configure or otherwise influence the guest.

Quoted 4 lines

Have you considered passing SMBIOS information to the guest? Right now, qemu supports passing specific pieces of information through table 1 and 2, and arbitrary pre-populated binary files for other tables. SMBIOS data is directly readable by the guest when it boots.

It can do piece-wise table types 2, 3, 4, and 17 as well (oops, I should send a follow-up patch to make that visible on the '--help' usage output :)

Quoted 8 lines
Quoted 7 lines

In terms of the mechanics, I'll need to figure out the following:

1. How would -guest-env="..." actually pass data into the guest?

Off the top of my head, we could piggyback on top of smbios (by using a type 11, "OEM strings" structure), which could then be parsed by qemu-guest-env from the guest side.

Quoted 5 lines

Ah, so you have thought about it. The question then becomes writing guest software to conveniently take advantage of whatever you arbitrarily pass down, as well as adding sugar on the host side to make it easier to package the information to be sent into the guest (easier than a binary SMBIOS blob, at any rate).

OK, I had a hunch about this being probably the cleanest, least-resistance way to do it; the fact that you also thought "smbios" gives me some extra confidence :)

Quoted 12 lines
Quoted 11 lines

2. How to support "set-env" (in addition to "get-env") functionality?

set-env means access to a writable guest memory area, and the least inconvenient way I can think of accomplishing this would be to put QGA in charge of it. When starting up for the first time, QGA would grab the host-supplied environment (e.g. by parsing smbios type 11), then respond to subsequent get and set requests from its in-memory environment key-value store. Changes to the environment made after the guest was started would not be persistent, but that's not something currently offered by any other platforms either :)

Quoted 5 lines

Are you proposing that the host can change the environment that the guest would re-query on the fly, or that the host's contribution is startup-only, and all further setting of variables is guest-side only? Or some hybrid where the initial environment is host->guest, but then QGA allows either side to communicate further changes in either direction?

I'm really interested in startup-only host->guest, but for completenes (and feature parity with other platforms) I'd like the hybrid approach. The key-value store of environment data resides on the guest, but QGA allows subsequent changes to be initiated from either side.

Quoted 7 lines
Quoted 3 lines

2. Is anything similar already being worked on (so I could either join that effort, or back off, as the case may be) ? :)

Not that I'm aware of, although the work on making qga able to request arbitrary process exec within the guest might be a piece of the puzzle for letting the host easily call into your proposed qemu-guest-env program.

Some guest-side process should be responsible for maintaining a writeable version of the environment (for the part where we allow further changes after startup). If that's QGA itself, then arbitrary process exec is kinda orthogonal.

Quoted 7 lines
Quoted 2 lines

3. Any technical advice on how to (better) implement it ?

Your ideas seem reasonable as a first place to start. You may still want to consider setting up a file in the guest, rather than relying on in-memory representation, so that things in the guest can survive a guest-agent restart.

That's something worth thinking about. The semantics get a bit complicated -- distinguishing between a qga restart and a first start which finds a left-over environment file from a previous execution of the guest, in which case how do we resolve any conflicts between what's in smbios and the leftover environment file :) But definitely worth pondering...

Quoted 4 lines

It would certainly be wise to propose a spec of
the initial SMBIOS string passing and subsequent QGA commands involved for coordinating the host->guest relationship, as a first patch in any proposed series.

Thanks for the feedback!

--Gabriel

#6 lore raw
In reply to Gabriel L. Somlo

Quoting Gabriel L. Somlo (2015-02-03 13:09:22)

Quoted 69 lines

Hi,

I'm interested in adding a way for a host to pass environment variables into a qemu guest VM -- analogous to setting environment variables for a process to access via getenv() and friends.

The QEMU Guest Agent (QGA) does not appear to quite fit the bill, at least not in its current form: The agent must have been successfully started on the guest before the host would have to connect to it (in a separate act from just starting the guest in the first place), and get it to execute any hypothetical commands to configure or otherwise influence the guest.

For this functionality to really resemble the way environment variables are used, environment information should be provided as part of the QEMU command line, without the requirement to make a separate/subsequent connection to the guest agent. For example:

    qemu-system-x86_64 -guest-env="VAR1=value1;VAR2=value 2" -hda image.qcow2

Once the guest is started, it should be possible to query the guest environment with something like:

    $ qemu-guest-env get VAR1
    value1
    $ qemu-guest-env get VAR2
    value 2

So far, the environment would be read-only from the guest perspective. A nice additional feature would be to allow setting additional guest-env variables, or replacing values for existing ones:

    $ qemu-guest-env set VAR3 "foo bar"
    $ qemu-guest-env set VAR4 "blah"

The actual QGA could then be taught to export guest-env set and get commands, so that querying/setting data in the guest environment could be allowed from the outside, via e.g. 'virsh qemu-agent-command'.

The 'qemu-guest-env' functionality could be incorporated into 'qemu-ga', or left as a separate (but closely related) binary, most likely packaged together with qemu-ga.

In terms of the mechanics, I'll need to figure out the following:

1. How would -guest-env="..." actually pass data into the guest?

Off the top of my head, we could piggyback on top of smbios (by using a type 11, "OEM strings" structure), which could then be parsed by qemu-guest-env from the guest side.

2. How to support "set-env" (in addition to "get-env") functionality?

set-env means access to a writable guest memory area, and the least inconvenient way I can think of accomplishing this would be to put QGA in charge of it. When starting up for the first time, QGA would grab the host-supplied environment (e.g. by parsing smbios type 11), then respond to subsequent get and set requests from its in-memory environment key-value store. Changes to the environment made after the guest was started would not be persistent, but that's not something currently offered by any other platforms either :)

Ultimately, if this makes it into QEMU, I'd like to also expose the functionality through libvirt/virsh/virt-manager (so users could set the initial environment variables as part of the guest VM xml template, via the gui, etc.)

Quoted 4 lines

So, my question for the QEMU dev team:

1. Would you consider this feature a useful addition to QEMU ? I.e., would this be acceptable (of interest) to the upstream project?

This does seem like useful functionality, but I think I'd like to know more about the actual use-cases being looked at.

Is this mostly about executing initial activation scripts? Because after that point, a key-value store can be managed through the guest-file-read/write interfaces for anything on the guest-side that's interested in these variables.

Even activation could be done using this approach, where the scripts start QGA and wait for the host to coordinate the initial creation of the file containing those variables, then setting a file marker that allows activation to proceed. And if that seems wonky, I'm fairly sure you could script the creation of the initial key-value store prior to starting the guest using libguestfs:

http://libguestfs.org/

I think we'd need a very strong argument to bake what seems to be high-level guest management tasks into QEMU. If that can avoided with some automated image modifications beforehand that seems to me the more reasonable approach. Libvirt could ostensibly even handle the task of writing those XML strings into the image's key-value store to make management easier, but I suspect even that is a bit too low in the stack for this level of management.

Quoted 8 lines

2. Is anything similar already being worked on (so I could either join that effort, or back off, as the case may be) ? :)

3. Any technical advice on how to (better) implement it ?

Thanks much,
--Gabriel

#7 lore raw
Quoted 3 lines · Michael Roth

This does seem like useful functionality, but I think I'd like to know more about the actual use-cases being looked at.

The proposed functionality is mostly equivalent to that offered by "GuestInfo variables". So yes, initial activation scripts :)

Quoted 5 lines

Is this mostly about executing initial activation scripts? Because after that point, a key-value store can be managed through the guest-file-read/write interfaces for anything on the guest-side that's interested in these variables.

Quoted 8 lines

Even activation could be done using this approach, where the scripts start QGA and wait for the host to coordinate the initial creation of the file containing those variables, then setting a file marker that allows activation to proceed. And if that seems wonky, I'm fairly sure you could script the creation of the initial key-value store prior to starting the guest using libguestfs:

http://libguestfs.org/

Specifically, I'm trying to port to QEMU a simulation/training setup where multiple VMs are started from the same base image, and guestinfo environment variables help each instance determine its "personality".

Editing the disk image is not feasible, since the idea is to share the base disk image across multiple VMs. And needing to connect to each VM after having started it, wait for it to bring up the QGA, then get it to accept environment variables, that's precisely the wonkiness I'm trying to avoid :)

I can certainly start small and implement read-only, host->guest startup time values (the smbios type11 strings plus a way to read them via a guest-side binary associated with a guest-tools package), and we can decide whether we want to support "set-env" operations and exporting set-env and get-env via the agent at a later stage. That functionality is available with GuestInfo variables, but the system I'm trying to port to QEMU doesn't require it as far as I can tell.

Thanks much,
--Gabriel

Quoted 8 lines

I think we'd need a very strong argument to bake what seems to be high-level guest management tasks into QEMU. If that can avoided with some automated image modifications beforehand that seems to me the more reasonable approach. Libvirt could ostensibly even handle the task of writing those XML strings into the image's key-value store to make management easier, but I suspect even that is a bit too low in the stack for this level of management.

#8 lore raw
Quoted 32 lines · Gabriel L. Somlo
Quoted 3 lines · Michael Roth

This does seem like useful functionality, but I think I'd like to know more about the actual use-cases being looked at.

The proposed functionality is mostly equivalent to that offered by "GuestInfo variables". So yes, initial activation scripts :)

Quoted 5 lines

Is this mostly about executing initial activation scripts? Because after that point, a key-value store can be managed through the guest-file-read/write interfaces for anything on the guest-side that's interested in these variables.

Quoted 8 lines

Even activation could be done using this approach, where the scripts start QGA and wait for the host to coordinate the initial creation of the file containing those variables, then setting a file marker that allows activation to proceed. And if that seems wonky, I'm fairly sure you could script the creation of the initial key-value store prior to starting the guest using libguestfs:

http://libguestfs.org/

Specifically, I'm trying to port to QEMU a simulation/training setup where multiple VMs are started from the same base image, and guestinfo environment variables help each instance determine its "personality".

Editing the disk image is not feasible, since the idea is to share the base disk image across multiple VMs. And needing to connect to each VM after having started it, wait for it to bring up the QGA, then get it to accept environment variables, that's precisely the wonkiness I'm trying to avoid :)

Quoted 8 lines

I can certainly start small and implement read-only, host->guest startup time values (the smbios type11 strings plus a way to read them via a guest-side binary associated with a guest-tools package), and we can decide whether we want to support "set-env" operations and exporting set-env and get-env via the agent at a later stage. That functionality is available with GuestInfo variables, but the system I'm trying to port to QEMU doesn't require it as far as I can tell.

guest exec with ability to pass an environment could solve your problem even without read/write. Boot guest, wait guest agent startup, start something you need from agent with desired environment.

this is a quote from the patchset being discussed at the moment.
[PATCH v2 0/8] qemu: guest agent: implement guest-exec command for Linux

+##
+# @guest-exec:
+#
+# Execute a command in the guest
+#
+# @path: path or executable name to execute
+# @params: #optional parameter list to pass to executable +# @env: #optional environment variables 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 +#
+# Returns: PID on success.
+#
+# Since: 2.3
+##
+{ 'command': 'guest-exec',
+ 'data': { 'path': 'str', '*params': ['str'], '*env': ['str'],
+ '*handle_stdin': 'int', '*handle_stdout': 'int',
+ '*handle_stderr': 'int' },
+ 'returns': 'int' }

Quoted 14 lines

Thanks much,
--Gabriel

Quoted 8 lines

I think we'd need a very strong argument to bake what seems to be high-level guest management tasks into QEMU. If that can avoided with some automated image modifications beforehand that seems to me the more reasonable approach. Libvirt could ostensibly even handle the task of writing those XML strings into the image's key-value store to make management easier, but I suspect even that is a bit too low in the stack for this level of management.

#9 lore raw
Quoted 42 lines · Denis V. Lunev
Quoted 32 lines · Gabriel L. Somlo
Quoted 3 lines · Michael Roth

This does seem like useful functionality, but I think I'd like to know more about the actual use-cases being looked at.

The proposed functionality is mostly equivalent to that offered by "GuestInfo variables". So yes, initial activation scripts :)

Quoted 5 lines

Is this mostly about executing initial activation scripts? Because after that point, a key-value store can be managed through the guest-file-read/write interfaces for anything on the guest-side that's interested in these variables.

Quoted 8 lines

Even activation could be done using this approach, where the scripts start QGA and wait for the host to coordinate the initial creation of the file containing those variables, then setting a file marker that allows activation to proceed. And if that seems wonky, I'm fairly sure you could script the creation of the initial key-value store prior to starting the guest using libguestfs:

http://libguestfs.org/

Specifically, I'm trying to port to QEMU a simulation/training setup where multiple VMs are started from the same base image, and guestinfo environment variables help each instance determine its "personality".

Editing the disk image is not feasible, since the idea is to share the base disk image across multiple VMs. And needing to connect to each VM after having started it, wait for it to bring up the QGA, then get it to accept environment variables, that's precisely the wonkiness I'm trying to avoid :)

Quoted 8 lines

I can certainly start small and implement read-only, host->guest startup time values (the smbios type11 strings plus a way to read them via a guest-side binary associated with a guest-tools package), and we can decide whether we want to support "set-env" operations and exporting set-env and get-env via the agent at a later stage. That functionality is available with GuestInfo variables, but the system I'm trying to port to QEMU doesn't require it as far as I can tell.

Quoted 4 lines

guest exec with ability to pass an environment could solve your problem even without read/write. Boot guest, wait guest agent startup, start something you need from agent with desired environment.

I'm trying as hard as I can to avoid the bit where I have to "wait guest agent startup, connect, run a bunch of stuff on the guest through the agent"... :)

The application I'm trying to port has a bunch of VM templates with some guestinfo/environment variables in them, many of them sharing the same disk image (vmdk) file. We start them all, and that's it. Fire and forget, no further fuss. If a VM hangs during startup, that's the application USER's problem, they can restart it, or whatever.

With your suggestion, I'd have to write a bunch of additional logic to monitor each starting VM, connect to it, handle errors and exceptions (what if the QGA doesn't start, what if it takes a long time to start, etc.) Now dealing with a failed boot is suddenly the application's problem, since I'm sitting there waiting to connect to the agent, and have to do something if the agent isn't coming up.

That's currently not necessary when using "that other hypervisor" from which I'm trying to migrate, so QEMU is at a bit of a disadvantage. I think there's value making it easy to port stuff over without imposing a major redesign of the application being ported...

Thanks,
--Gabriel

Quoted 42 lines

this is a quote from the patchset being discussed at the moment.
[PATCH v2 0/8] qemu: guest agent: implement guest-exec command for Linux

(quoted diff) +21 -0
+##
+# @guest-exec:
+#
+# Execute a command in the guest
+#
+# @path: path or executable name to execute
+# @params: #optional parameter list to pass to executable
+# @env: #optional environment variables 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
+#
+# Returns: PID on success.
+#
+# Since: 2.3
+##
+{ 'command': 'guest-exec',
+  'data':    { 'path': 'str', '*params': ['str'], '*env': ['str'],
+               '*handle_stdin': 'int', '*handle_stdout': 'int',
+               '*handle_stderr': 'int' },
+  'returns': 'int' }
Quoted 16 lines

Thanks much,
--Gabriel

Quoted 8 lines

I think we'd need a very strong argument to bake what seems to be high-level guest management tasks into QEMU. If that can avoided with some automated image modifications beforehand that seems to me the more reasonable approach. Libvirt could ostensibly even handle the task of writing those XML strings into the image's key-value store to make management easier, but I suspect even that is a bit too low in the stack for this level of management.

#10 lore raw
In reply to Gabriel L. Somlo

Quoting Gabriel L. Somlo (2015-02-03 15:38:59)

Quoted 20 lines · Michael Roth
Quoted 3 lines

This does seem like useful functionality, but I think I'd like to know more about the actual use-cases being looked at.

The proposed functionality is mostly equivalent to that offered by "GuestInfo variables". So yes, initial activation scripts :)

Quoted 12 lines

Is this mostly about executing initial activation scripts? Because after that point, a key-value store can be managed through the guest-file-read/write interfaces for anything on the guest-side that's interested in these variables.

Even activation could be done using this approach, where the scripts start QGA and wait for the host to coordinate the initial creation of the file containing those variables, then setting a file marker that allows activation to proceed. And if that seems wonky, I'm fairly sure you could script the creation of the initial key-value store prior to starting the guest using libguestfs:

Quoted 8 lines
Quoted 1 line

http://libguestfs.org/

Specifically, I'm trying to port to QEMU a simulation/training setup where multiple VMs are started from the same base image, and guestinfo environment variables help each instance determine its "personality".

Editing the disk image is not feasible, since the idea is to share the base disk image across multiple VMs. And needing to connect to each VM

Well, I assume by shared a base image you mean using a template image as the backing image for a COW image allocated for each guest prior to activation? As long as the editing is done against the COW image rather than the backing image it should work. Maybe it's not ideal, but it's feasible.

I hadn't really considered the SMBIOS approach though. That might be more straightforward to get the initial store to the guest.

Quoted 3 lines

after having started it, wait for it to bring up the QGA, then get it to accept environment variables, that's precisely the wonkiness I'm trying to avoid :)

Understandable :)

Quoted 8 lines

I can certainly start small and implement read-only, host->guest startup time values (the smbios type11 strings plus a way to read them via a guest-side binary associated with a guest-tools package), and we can decide whether we want to support "set-env" operations and exporting set-env and get-env via the agent at a later stage. That functionality is available with GuestInfo variables, but the system I'm trying to port to QEMU doesn't require it as far as I can tell.

Seems like a reasonable start to me.

Quoted 12 lines

Thanks much,
--Gabriel

Quoted 8 lines

I think we'd need a very strong argument to bake what seems to be high-level guest management tasks into QEMU. If that can avoided with some automated image modifications beforehand that seems to me the more reasonable approach. Libvirt could ostensibly even handle the task of writing those XML strings into the image's key-value store to make management easier, but I suspect even that is a bit too low in the stack for this level of management.

#11 lore raw
In reply to Gabriel L. Somlo
Quoted 26 lines · Gabriel L. Somlo
Quoted 3 lines · Michael Roth

This does seem like useful functionality, but I think I'd like to know more about the actual use-cases being looked at.

The proposed functionality is mostly equivalent to that offered by "GuestInfo variables". So yes, initial activation scripts :)

Quoted 5 lines

Is this mostly about executing initial activation scripts? Because after that point, a key-value store can be managed through the guest-file-read/write interfaces for anything on the guest-side that's interested in these variables.

Quoted 8 lines

Even activation could be done using this approach, where the scripts start QGA and wait for the host to coordinate the initial creation of the file containing those variables, then setting a file marker that allows activation to proceed. And if that seems wonky, I'm fairly sure you could script the creation of the initial key-value store prior to starting the guest using libguestfs:

http://libguestfs.org/

Specifically, I'm trying to port to QEMU a simulation/training setup where multiple VMs are started from the same base image, and guestinfo environment variables help each instance determine its "personality".

Quoted 5 lines

Editing the disk image is not feasible, since the idea is to share the base disk image across multiple VMs. And needing to connect to each VM after having started it, wait for it to bring up the QGA, then get it to accept environment variables, that's precisely the wonkiness I'm trying to avoid :)

AFAICT, you're describing the exact scenario that cloud-init solves. You have a generic cloud base image, and you provide metadata to the guest (either via a transient CDROM image generated at boot, or via a speciall network interface with well known IP addr + HTTP service) which is uses to configure itself for a specific task as boot up.

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 :|
#12 lore raw

Hi Daniel,

Quoted 33 lines · Daniel P. Berrange
Quoted 26 lines · Gabriel L. Somlo
Quoted 3 lines · Michael Roth

This does seem like useful functionality, but I think I'd like to know more about the actual use-cases being looked at.

The proposed functionality is mostly equivalent to that offered by "GuestInfo variables". So yes, initial activation scripts :)

Quoted 5 lines

Is this mostly about executing initial activation scripts? Because after that point, a key-value store can be managed through the guest-file-read/write interfaces for anything on the guest-side that's interested in these variables.

Quoted 8 lines

Even activation could be done using this approach, where the scripts start QGA and wait for the host to coordinate the initial creation of the file containing those variables, then setting a file marker that allows activation to proceed. And if that seems wonky, I'm fairly sure you could script the creation of the initial key-value store prior to starting the guest using libguestfs:

http://libguestfs.org/

Specifically, I'm trying to port to QEMU a simulation/training setup where multiple VMs are started from the same base image, and guestinfo environment variables help each instance determine its "personality".

Quoted 5 lines

Editing the disk image is not feasible, since the idea is to share the base disk image across multiple VMs. And needing to connect to each VM after having started it, wait for it to bring up the QGA, then get it to accept environment variables, that's precisely the wonkiness I'm trying to avoid :)

Quoted 5 lines

AFAICT, you're describing the exact scenario that cloud-init solves. You have a generic cloud base image, and you provide metadata to the guest (either via a transient CDROM image generated at boot, or via a speciall network interface with well known IP addr + HTTP service) which is uses to configure itself for a specific task as boot up.

Thanks for the pointer, cloud-init does indeed sound very interesting, and I'll definitely keep it in mind when creating new content (new VM images) for the application.

However, looking at this:

https://cloudinit.readthedocs.org/en/latest/topics/capabilities.html

I found the following:

"Cloud-init's behavior can be configured via user-data [...] which
can be given by the user at instance launch time. This is done via the --user-data or --user-data-file argument to ec2-run instances for example.

* Check your local clients documentation for how to provide a user-data string or user-data file for usage by cloud-init on instance creation"

I guess what I'm proposing is a low-overhead, flexible way to pas such a user-data string into qemu, via its command line, and without adding the non-trivial requirement that whatever I'm doing must happen within the context of a cloud infrastructure such as ec2, openstack, etc., which is how user-data is currently provided to cloud-init on the starting VMs.

Please kick me if I totally missed something obvious! :)

Thanks much,
--Gabriel

#13 lore raw
Quoted 61 lines · Gabriel L. Somlo

Hi Daniel,

Quoted 33 lines · Daniel P. Berrange
Quoted 26 lines · Gabriel L. Somlo
Quoted 3 lines · Michael Roth

This does seem like useful functionality, but I think I'd like to know more about the actual use-cases being looked at.

The proposed functionality is mostly equivalent to that offered by "GuestInfo variables". So yes, initial activation scripts :)

Quoted 5 lines

Is this mostly about executing initial activation scripts? Because after that point, a key-value store can be managed through the guest-file-read/write interfaces for anything on the guest-side that's interested in these variables.

Quoted 8 lines

Even activation could be done using this approach, where the scripts start QGA and wait for the host to coordinate the initial creation of the file containing those variables, then setting a file marker that allows activation to proceed. And if that seems wonky, I'm fairly sure you could script the creation of the initial key-value store prior to starting the guest using libguestfs:

http://libguestfs.org/

Specifically, I'm trying to port to QEMU a simulation/training setup where multiple VMs are started from the same base image, and guestinfo environment variables help each instance determine its "personality".

Quoted 5 lines

Editing the disk image is not feasible, since the idea is to share the base disk image across multiple VMs. And needing to connect to each VM after having started it, wait for it to bring up the QGA, then get it to accept environment variables, that's precisely the wonkiness I'm trying to avoid :)

Quoted 5 lines

AFAICT, you're describing the exact scenario that cloud-init solves. You have a generic cloud base image, and you provide metadata to the guest (either via a transient CDROM image generated at boot, or via a speciall network interface with well known IP addr + HTTP service) which is uses to configure itself for a specific task as boot up.

Thanks for the pointer, cloud-init does indeed sound very interesting, and I'll definitely keep it in mind when creating new content (new VM images) for the application.

However, looking at this:

https://cloudinit.readthedocs.org/en/latest/topics/capabilities.html

I found the following:

"Cloud-init's behavior can be configured via user-data [...] which
can be given by the user at instance launch time. This is done via the --user-data or --user-data-file argument to ec2-run instances for example.

* Check your local clients documentation for how to provide a user-data string or user-data file for usage by cloud-init on instance creation"

Quoted 6 lines

I guess what I'm proposing is a low-overhead, flexible way to pas such a user-data string into qemu, via its command line, and without adding the non-trivial requirement that whatever I'm doing must happen within the context of a cloud infrastructure such as ec2, openstack, etc., which is how user-data is currently provided to cloud-init on the starting VMs.

Yes, there is some overhead in setting up QEMU on the host to provide the data cloud-init needs, but it isn't all that difficult. For example Rich Jones describes how to setup a virtual disk for cloud-init

https://rwmj.wordpress.com/2013/12/10/creating-a-cloud-init-config-disk-for-non-cloud-boots/

Given the prevalence of cloud-init across distros, I think you'll be hard pressed to get them to support an alternative method, even if it is a bit simpler at the QEMU setup level.

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 :|
#14 lore raw
Quoted 9 lines · Daniel P. Berrange

Yes, there is some overhead in setting up QEMU on the host to provide the data cloud-init needs, but it isn't all that difficult. For example Rich Jones describes how to setup a virtual disk for cloud-init

https://rwmj.wordpress.com/2013/12/10/creating-a-cloud-init-config-disk-for-non-cloud-boots/

Given the prevalence of cloud-init across distros, I think you'll be hard pressed to get them to support an alternative method, even if it is a bit simpler at the QEMU setup level.

Have a look at this:

http://www.vmware.com/pdf/Scripting_API_23.pdf

(page 36, GuestInfo Variables).

It's a rather generic, flexible feature. Some people use it for early boot, but that's just one of the many use cases, so maybe focusing on "how to implement early boot configuration" is going a bit off on a tangent :)

I'd like to add that mechanism (or at least the host->guest bits) to qemu, for "feature parity". That should be a good thing, making it easier to port "whatever" to qemu, without having to massively rearchitect it to work around a missing feature...

Also, it's really non-intrusive. At this point, I could implement it by passing environment strings in via "-smbios file=./Type11EnvStrBlob", and read them on the guest side via 'dmidecode -t11', with some grep and awk magic thrown in for good measure :)

But I'd really love to wrap it in some syntactic sugar, so that it could eventually be exposed via virsh and virt-manager, so that people used to VMWare and guestinfo variables can migrate things over to qemu/kvm in a relatively painless way. And some of them will use it for early boot, but hey, who am I to argue. I'm all about "mechanism", and prefer leaving "policy" to the user :)

Thanks much,
--Gabriel

#15 lore raw
Quoted 27 lines · Gabriel L. Somlo
Quoted 9 lines · Daniel P. Berrange

Yes, there is some overhead in setting up QEMU on the host to provide the data cloud-init needs, but it isn't all that difficult. For example Rich Jones describes how to setup a virtual disk for cloud-init

https://rwmj.wordpress.com/2013/12/10/creating-a-cloud-init-config-disk-for-non-cloud-boots/

Given the prevalence of cloud-init across distros, I think you'll be hard pressed to get them to support an alternative method, even if it is a bit simpler at the QEMU setup level.

Have a look at this:

http://www.vmware.com/pdf/Scripting_API_23.pdf

(page 36, GuestInfo Variables).

It's a rather generic, flexible feature. Some people use it for early boot, but that's just one of the many use cases, so maybe focusing on "how to implement early boot configuration" is going a bit off on a tangent :)

I'd like to add that mechanism (or at least the host->guest bits) to qemu, for "feature parity". That should be a good thing, making it easier to port "whatever" to qemu, without having to massively rearchitect it to work around a missing feature...

Quoted 4 lines

Also, it's really non-intrusive. At this point, I could implement it by passing environment strings in via "-smbios file=./Type11EnvStrBlob", and read them on the guest side via 'dmidecode -t11', with some grep and awk magic thrown in for good measure :)

Perhaps cloud-init project would be interested in using the custom smbios field as a data source for its needs. It already has the concept of pluggable data sources, so adding another could be interesting.

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 :|
#16 lore raw
In reply to Gabriel L. Somlo
Quoted 13 lines · Gabriel L. Somlo

Hi,

I'm interested in adding a way for a host to pass environment variables into a qemu guest VM -- analogous to setting environment variables for a process to access via getenv() and friends.

The QEMU Guest Agent (QGA) does not appear to quite fit the bill, at least not in its current form: The agent must have been successfully started on the guest before the host would have to connect to it (in a separate act from just starting the guest in the first place), and get it to execute any hypothetical commands to configure or otherwise influence the guest.

Quoted 7 lines

So, my question for the QEMU dev team:

1. Would you consider this feature a useful addition to QEMU ? I.e., would this be acceptable (of interest) to the upstream project?

2. Is anything similar already being worked on (so I could either join that effort, or back off, as the case may be) ? :)

IMHO this is already a solved problem via the cloud-init project which is the widely used standard for injecting information into guest OS at boot time. Any OS distro shipping cloud images is already going to have cloud-init provided.

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 :|
#17 lore raw
In reply to Gabriel L. Somlo

Hi Gabriel,

Summary up top, why I like the approach below.

Quoted 8 lines · Gabriel L. Somlo

Hi,

For this functionality to really resemble the way environment variables are used, environment information should be provided as part of the QEMU command line, without the requirement to make a separate/subsequent connection to the guest agent. For example:

    qemu-system-x86_64 -guest-env="VAR1=value1;VAR2=value 2" -hda image.qcow2

qemu-system-x86_64 -append "myvars=VAR1=value1;VAR2=value2" -hda image.qcow2

Quoted 8 lines

Once the guest is started, it should be possible to query the guest environment with something like:

    $ qemu-guest-env get VAR1
    value1
    $ qemu-guest-env get VAR2
    value 2

eval sed -nr 's/myvars=(.*)/\1/' /proc/cmdline
echo $VAR1
echo $VAR2

I have two use cases that I've enabled or would like to enable across a variety of platforms and architectures:

A) Script workload(s) that should run from a regular "cold" boot.
B) On slow systems, script the workload(s) that should be run immediately following a checkpoint restore ("warm" boot).

Originally I was using the ARM Angel semihosting command line for both A and B. But that required a JTAG debugger be attached to hardware devices, so I switched to appending foo=bar strings to /proc/cmdline, which has the additional benefit of being portable across architectures. Does QEMU's -append flag for setting and something like sed -nr 's/myopts=(.*)/\1/' /proc/cmdline for getting work for your use case?

I've wondered about something portable across architectures like QMP or VirtIO-something for the B use case, but that hasn't really progressed beyond thought experiment.

Chris

Signature · 3 lines
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#18 lore raw
In reply to Gabriel L. Somlo
Quoted 1 line · Gabriel L. Somlo
    qemu-system-x86_64 -guest-env="VAR1=value1;VAR2=value 2" -hda image.qcow2

libguestfs does this already by passing a few environment variables on the kernel command line. This requires a Linux guest kernel and that you're using the -kernel and -append options.

For a more general purpose approach, I'd suggest something with virtio-serial.

What problem is this actually trying to solve?

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org
#19 lore raw
Quoted 6 lines · Richard W.M. Jones
Quoted 1 line · Gabriel L. Somlo
    qemu-system-x86_64 -guest-env="VAR1=value1;VAR2=value 2" -hda image.qcow2

libguestfs does this already by passing a few environment variables on the kernel command line. This requires a Linux guest kernel and that you're using the -kernel and -append options.

Right, lots of the VMs I'm concerned about are Windows, of various vintages.

Quoted 2 lines

For a more general purpose approach, I'd suggest something with virtio-serial.

That requires that either the host or guest actively reach out and push or pull something to/from the other, respectively, as a separate step from simply starting the guest.

Quoted 1 line

What problem is this actually trying to solve?

Feature-parity with vmware's guestinfo variables (see
http://www.vmware.com/pdf/Scripting_API_23.pdf, pp.36)

Think "environment variables" from host to guest, without requiring one to connect to anything and mount/download/push or otherwise transfer stuff before the guest can know what it's supposed to do.

Thanks much,
--Gabriel