寻找一种好方法来检测我是否在虚拟机(Linux)中运行

发布于 2024-08-10 06:02:10 字数 181 浏览 10 评论 0原文

在虚拟机中,操作系统提供的实时调度往往不可靠。对于我的应用程序,我希望能够检测我是否在虚拟机上运行(仅限 Linux)。

因此,我正在寻找一种很好的方法来检测(用 C 语言)我是否处于虚拟化环境中。根据所使用的 VM,似乎使用了各种 DMI 和 CPUID 字符串。不过,我主要对通用方式感兴趣。

有人有什么想法吗?

In VMs OS-provided real-time scheduling tends not to be reliable. For my application I'd like to be able to detect whether I am running on a VM or not (Linux-only).

So I am looking for a nice way to detect (in C) whether I am in a virtualized environment. Depending on the VM used there seem to be various DMI and CPUID strings in use. I am primarily interested in a generic way though.

Anyone got any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

明媚如初 2024-08-17 06:02:10

facter 和 imvirt 都会检测一些虚拟化

facter and imvirt will both detect some virtualizations

你是年少的欢喜 2024-08-17 06:02:10

看来您真正想要回答的问题是“实时调度工作不可靠吗?”。那么为什么不编写一个测试来检查那个呢?

It seems that the real question you want answered is "Is real-time scheduling working unreliably?". So why not write a test that checks for that?

宛菡 2024-08-17 06:02:10

我认为你必须启发式地这样做。虚拟化产品的部分目标是让虚拟机实例相信它正在真实的硬件上运行。每个虚拟化产品都会模拟特定的硬件,因此我的解决方案是创建一个库,您可以询问“我在虚拟机上吗”,并在幕后维护一些虚拟机存在证据的搜索。这样,您仍然可以相对隔离于检测虚拟机的具体细节。

I think you're going to have to do this heuristically. Part of the goal of virtualization products is to make the vm instance believe it's running on real hardware. Each virtualization product is going to simulate specific hardware, so my solution would be to make a library that you can ask "am I on a vm" and just maintain under the hood some search for evidence of vm presence. This way you still remain relatively isolated from the nitty gritty of detecting the vm.

鹿港巷口少年归 2024-08-17 06:02:10

您还可以在 scsi 设备中查找 VMware:

cat /proc/scsi/scsi | grep VMware

可能仅在 VM 上成功


VM 上的示例输出:

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: VMware   Model: Virtual disk     Rev: 1.0
  Type:   Direct-Access                    ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
  Vendor: VMware   Model: Virtual disk     Rev: 1.0
  Type:   Direct-Access                    ANSI SCSI revision: 02

真实计算机上的示例输出:

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: TSSTcorp Model: CDRW/DVD TSL462D Rev: DE01
  Type:   CD-ROM                           ANSI  SCSI revision: 05
Host: scsi6 Channel: 00 Id: 08 Lun: 00
  Vendor: DP       Model: BACKPLANE        Rev: 1.05
  Type:   Enclosure                        ANSI  SCSI revision: 05
Host: scsi6 Channel: 02 Id: 00 Lun: 00
  Vendor: DELL     Model: PERC 5/i         Rev: 1.03
  Type:   Direct-Access                    ANSI  SCSI revision: 05

You can also look for VMware in the scsi devices:

cat /proc/scsi/scsi | grep VMware

will probably succeed only on VMs


example output on VM:

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: VMware   Model: Virtual disk     Rev: 1.0
  Type:   Direct-Access                    ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
  Vendor: VMware   Model: Virtual disk     Rev: 1.0
  Type:   Direct-Access                    ANSI SCSI revision: 02

example output on real machine:

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: TSSTcorp Model: CDRW/DVD TSL462D Rev: DE01
  Type:   CD-ROM                           ANSI  SCSI revision: 05
Host: scsi6 Channel: 00 Id: 08 Lun: 00
  Vendor: DP       Model: BACKPLANE        Rev: 1.05
  Type:   Enclosure                        ANSI  SCSI revision: 05
Host: scsi6 Channel: 02 Id: 00 Lun: 00
  Vendor: DELL     Model: PERC 5/i         Rev: 1.03
  Type:   Direct-Access                    ANSI  SCSI revision: 05
风月客 2024-08-17 06:02:10

查找仅在您位于虚拟机中时才显示的特定设备。例如,标记为“Parallels”或“VMWare”的显示设备可能很好地表明您位于虚拟机中。

当然,这仅适用于您了解的虚拟机,因此不是很通用。

Look for specific devices that only show up while you're in a VM. For instance, a display device marked "Parallels" or "VMWare" might be a good indication that you're in a VM.

Of course this only works for VMs that you know about and thus isn't very generic.

匿名。 2024-08-17 06:02:10

下面是一个代码示例: http://www.codeproject.com/KB/system/ VmDetect.aspxhttp://mark.michaelis.net/Blog/HowToDetectVirtualMachineExecution。 aspx (但这是从 2005 年开始的)

并且在一些杂志中我读到可以使用硬件集检测虚拟机,因为 VM 使用有限的模拟硬件集。

Here is a code example: http://www.codeproject.com/KB/system/VmDetect.aspx , http://mark.michaelis.net/Blog/HowToDetectVirtualMachineExecution.aspx (but this is from year 2005)

And in some magazine I've read that virtual machine can be detected with the hardware set because VM use the limited set of emulated hardware.

橘虞初梦 2024-08-17 06:02:10

虽然不是明确的,但您也可以检查您的接口名称... ifconfig 也会吐出“venet0”而不是“eth0”

,“df”也会透露一些信息:
vmware - /dev/vzfs
citrix/xen - /dev/xvda1

though not definitive, you can also check your interface names... ifconfig would spit out "venet0" rather than "eth0"

also, 'df' will give away some tells:
vmware - /dev/vzfs
citrix/xen - /dev/xvda1

鸢与 2024-08-17 06:02:10

ifconfig 获取 MAC 地址,然后查找供应商代码(google:mac 地址查找)。如果您提前知道使用的虚拟化平台会有所帮助。

ifconfig to get the MAC address and then look up the vendor code (google: mac address lookup). Helps if you know in advance what virtualization platform is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文