Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide information about running in a Virtual Machine #1103

Open
jelly opened this issue Jun 21, 2017 · 6 comments
Open

Provide information about running in a Virtual Machine #1103

jelly opened this issue Jun 21, 2017 · 6 comments

Comments

@jelly
Copy link
Contributor

jelly commented Jun 21, 2017

I would like to see something as psutil.virtualized added to psutil if that falls in the use case of the library.

An ugly example implementation:

virtualized = False
for line in open('/proc/cpuinfo'):
    if line.startswith('flags') and 'hypervisor' in line:
        virtualized = True
        break

This however does not determine the virtualisation method such as vmware, kvm, lxc.

There are some alternatives available such as virt-what, dmidecode or systemd-detect-virt.

Some background info.
http://unix.stackexchange.com/questions/89714/easy-way-to-determine-virtualization-technology

@giampaolo
Copy link
Owner

giampaolo commented Aug 4, 2017

I doubt there is a standardized way to do this on Linux, let alone on other platforms. Also, we're dealing with different independent technologies (virtualbox, xen, etc.), so each one is free to provide its own method to determine this, and that same method (such as relying on dmidecode) may even change with newer versions. In summary, I can't see a reliable way of implementing this.

@jelly
Copy link
Contributor Author

jelly commented Sep 27, 2021

I can't recall why I opened this issue, but these days systemd-detect-virt can be used to detect virtualisation. systemd does all the logic for you.

@giampaolo
Copy link
Owner

giampaolo commented Sep 28, 2021

@giampaolo
Copy link
Owner

@giampaolo
Copy link
Owner

giampaolo commented Dec 31, 2021

Possible fallback / solution to detect vmware in a "cross-platform" fashion (utility needs to be installed though):

import subprocess, shutil

if shutil.which("vmware-checkvm"):
    code = subprocess.call(["vmware-checkvm"], stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    if code == 0:
        return "vmware"

Source code: https://github.com/vmware/open-vm-tools/blob/master/open-vm-tools/checkvm/checkvm.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants