purpleidea / mgmt

Next generation distributed, event-driven, parallel config management!
https://purpleidea.com/tags/mgmtconfig/
GNU General Public License v3.0
3.6k stars 312 forks source link

lang: funcs: core: os: Add is_virtual function #737

Open purpleidea opened 8 months ago

purpleidea commented 8 months ago

Problem statement:

We'd like to have an is_virtual function, so that we could detect if we're running on iron or in a virtual machine. We should implement this in pure golang if possible.

Goal:

At least initially, let's just support Linux, as we can add other OS support in the future with go build drop-ins like we do for other similar functions (see: uptime or load for an example)

Implementation details:

Of note, this can be a pure function using the simple function API and doesn't require any concurrency! Even though this is really a fact (in puppet terminology) we should probably just use the function API instead, since we'll probably get rid of the dedicated fact API.

If we can do this all in pure golang without having to exec a separate binary (eg: systemd-detect-virt or virt-what) then that is preferred. If we do however have to exec something, please cache the result for future calls.

Testing

I don't have a good story for how we would test this, since we don't have test runners in a variety of different setups yet. Ideas welcome. At a minimum run the function on any platform and make sure it doesn't error.

Edge cases

What about containers? Does an nspawn container running this return true or false for is_virtual? My guess is maybe false

Future work

What about is_container? What about virtual_type()? Do we want constants in $const.func.core.os.virtual_type.kvm ? And so on...

Events?

We don't think we need to worry about the case of this value changing at runtime, so this is okay as-is for now.

jfindlay commented 8 months ago

on it

manojks1999 commented 5 months ago

@purpleidea Is this closed?

purpleidea commented 5 months ago

Send a patch =D

bearrito commented 5 months ago

@purpleidea Can you assign this to me. I'm basically just go to port what is done in https://github.com/systemd/systemd/blob/main/src/basic/virt.c#L674

I'm going to start with Docker, Podman, EC2 and GCE

purpleidea commented 5 months ago

Can you assign this to me.

No need to assign anything, just send a patch and we'll be happy to review it.