免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1465 | 回复: 2
打印 上一主题 下一主题

[Linux资讯] 如何判断 Linux 是否运行在虚拟机上 [复制链接]

求职 : Linux运维
论坛徽章:
203
拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之约旦
日期:2015-04-05 20:08:292015年亚洲杯之澳大利亚
日期:2015-04-09 09:25:552015年亚洲杯之约旦
日期:2015-04-10 17:34:102015年亚洲杯之巴勒斯坦
日期:2015-04-10 17:35:342015年亚洲杯之日本
日期:2015-04-16 16:28:552015年亚洲杯纪念徽章
日期:2015-04-27 23:29:17操作系统版块每日发帖之星
日期:2015-06-06 22:20:00操作系统版块每日发帖之星
日期:2015-06-09 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-02-27 12:11 |只看该作者 |倒序浏览
本帖最后由 lyhabc 于 2016-02-27 12:11 编辑

    if [ -d "/proc/vz" ];then  

        ulimit -s unlimited
    fi

判断 OpenVZ/Xen PV/UML
判断 OpenVZ/Xen PV/UML 是最容易的,直接检查 /proc 下的相关目录和文件就可以知道,比如 OpenVZ VPS
上会有 /proc/vz 这个文件;Xen PV 虚拟机上会有 /proc/xen/ 这个目录,并且目录下有一些东西;UML 上打印
/proc/cpuinfo 会找到 UML 标志。写了一个简单的 Python 脚本来检测:
#!/usr/bin/python
# check if a linux system running on a virtual machine (openvz/xen pv/uml)
# written by http://www.vpsee.com

import sys, os

def main():
if os.getuid() != 0:
print "must be run as root"
sys.exit(0)

# check OpenVZ/Virtuozzo
if os.path.exists("/proc/vz":
if not os.path.exists("/proc/bc":
print "openvz container"
else:
print "openvz node"

# check Xen
if os.path.exists("/proc/xen/capabilities":
if (os.path.getsize("/proc/xen/capabilities" > 0):
print "xen dom0"
else:
print "xen domU"

# check User Mode Linux (UML)
f = open("/proc/cpuinfo", "r"; t = f.read(); f.close()
if (t.find("UML" > 0):
print "uml"

if __name__=="__main__":
main()

判断 VMware/Xen HVM/KVM
如果使用的是 VMware/Xen HVM/KVM 这样的全虚拟就更难判断一些,最准确的办法是读取 CPUID 来判断,Xen
源代码下面有一段检测是否是 Xen 的 C 语言代码 tools/misc/xen-detect.c,这段代码提供了一个很好的例子,VPSee
重写了代码,用宏替代了函数,增加了对 VMware 和 KVM 的识别,用 gcc 编译后就可以运行:
/*
* check if a linux system running on a virtual machine (vmware/xen hvm/kvm)
* written by http://www.vpsee.com
*/
#include stdio.h
#include string.h

#define HYPERVISOR_INFO 0x40000000

#define CPUID(idx, eax, ebx, ecx, edx) \
asm volatile ( \
"test %1,%1 ; jz 1f ; ud2a ; .ascii \"xen\" ; 1: cpuid" \
: "=b" (*ebx), "=a" (*eax), "=c" (*ecx), "=d" (*edx) \
: "0" (idx) );

int main(void)
{
unsigned int eax, ebx, ecx, edx;
char string[13];

CPUID(HYPERVISOR_INFO, &eax, &ebx, &ecx, &edx);
*(unsigned int *)(string+0) = ebx;
*(unsigned int *)(string+4) = ecx;
*(unsigned int *)(string+ = edx;

string[12] = 0;
if (strncmp(string, "XenVMMXenVMM", 12) == 0) {
printf("xen hvm\n";
} else if (strncmp(string, "VMwareVMware", 12) == 0) {
printf("vmware\n";
} else if (strncmp(string, "KVMKVMKVM", 12) == 0) {
printf("kvm\n";
} else
printf("bare hardware\n";

return 0;
}

判断 VirtualBox/Virtual PC
什么?这种家用桌面虚拟机自己装的还会不知道?!如果不知道的话也有办法,在 Linux 下运行 dmidecode 工具然后查找
Manufacturer: innotek GmbH, Manufacturer: Microsoft Corporation 关键字就能对应上
VirtualBox 和 Virtual PC.

例如本人买的美国vps,openvz架构

论坛徽章:
223
2022北京冬奥会纪念版徽章
日期:2015-08-10 16:30:32操作系统版块每日发帖之星
日期:2016-05-10 19:22:58操作系统版块每日发帖之星
日期:2016-02-18 06:20:00操作系统版块每日发帖之星
日期:2016-03-01 06:20:00操作系统版块每日发帖之星
日期:2016-03-02 06:20:0015-16赛季CBA联赛之上海
日期:2019-09-20 12:29:3219周年集字徽章-周
日期:2019-10-01 20:47:4815-16赛季CBA联赛之八一
日期:2020-10-23 18:30:5320周年集字徽章-20	
日期:2020-10-28 14:14:2615-16赛季CBA联赛之广夏
日期:2023-02-25 16:26:26CU十四周年纪念徽章
日期:2023-04-13 12:23:10操作系统版块每日发帖之星
日期:2016-05-10 19:22:58
2 [报告]
发表于 2016-02-27 17:24 |只看该作者
简单说,就是查看设备,如果系统的设备是虚拟机的提供商那么就可以判断是跑在虚拟机下的

论坛徽章:
41
操作系统版块每日发帖之星
日期:2016-08-21 06:20:00每日论坛发贴之星
日期:2016-05-05 06:20:00操作系统版块每日发帖之星
日期:2016-05-05 06:20:00IT运维版块每日发帖之星
日期:2016-05-05 06:20:0015-16赛季CBA联赛之山西
日期:2016-04-27 08:49:00操作系统版块每日发帖之星
日期:2016-04-25 06:20:00操作系统版块每日发帖之星
日期:2016-04-17 06:23:2815-16赛季CBA联赛之吉林
日期:2016-03-25 15:46:3415-16赛季CBA联赛之四川
日期:2016-03-25 14:26:19操作系统版块每日发帖之星
日期:2016-05-27 06:20:00操作系统版块每日发帖之星
日期:2016-05-28 06:20:00IT运维版块每日发帖之星
日期:2016-08-18 06:20:00
3 [报告]
发表于 2016-02-29 16:37 |只看该作者
lspci,如果是虚拟机,那么各种接口肯定有虚拟化平台名称,比如下面的,一看就知道跑在vmware上:
  1. [root@nagios ~]# lspci
  2. 00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01)
  3. 00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01)
  4. 00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)
  5. 00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
  6. 00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
  7. 00:07.7 System peripheral: VMware Virtual Machine Communication Interface (rev 10)
  8. 00:0f.0 VGA compatible controller: VMware SVGA II Adapter
  9. 00:10.0 SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)
  10. 00:11.0 PCI bridge: VMware PCI bridge (rev 02)
  11. 00:15.0 PCI bridge: VMware PCI Express Root Port (rev 01)
  12. 00:15.1 PCI bridge: VMware PCI Express Root Port (rev 01)
  13. 00:15.2 PCI bridge: VMware PCI Express Root Port (rev 01)
  14. 00:15.3 PCI bridge: VMware PCI Express Root Port (rev 01)
  15. 00:15.4 PCI bridge: VMware PCI Express Root Port (rev 01)
  16. 00:15.5 PCI bridge: VMware PCI Express Root Port (rev 01)
  17. 00:15.6 PCI bridge: VMware PCI Express Root Port (rev 01)
  18. 00:15.7 PCI bridge: VMware PCI Express Root Port (rev 01)
  19. 00:16.0 PCI bridge: VMware PCI Express Root Port (rev 01)
  20. 00:16.1 PCI bridge: VMware PCI Express Root Port (rev 01)
  21. 00:16.2 PCI bridge: VMware PCI Express Root Port (rev 01)
  22. 00:16.3 PCI bridge: VMware PCI Express Root Port (rev 01)
  23. 00:16.4 PCI bridge: VMware PCI Express Root Port (rev 01)
  24. 00:16.5 PCI bridge: VMware PCI Express Root Port (rev 01)
  25. 00:16.6 PCI bridge: VMware PCI Express Root Port (rev 01)
  26. 00:16.7 PCI bridge: VMware PCI Express Root Port (rev 01)
  27. 00:17.0 PCI bridge: VMware PCI Express Root Port (rev 01)
  28. 00:17.1 PCI bridge: VMware PCI Express Root Port (rev 01)
  29. 00:17.2 PCI bridge: VMware PCI Express Root Port (rev 01)
  30. 00:17.3 PCI bridge: VMware PCI Express Root Port (rev 01)
  31. 00:17.4 PCI bridge: VMware PCI Express Root Port (rev 01)
  32. 00:17.5 PCI bridge: VMware PCI Express Root Port (rev 01)
  33. 00:17.6 PCI bridge: VMware PCI Express Root Port (rev 01)
  34. 00:17.7 PCI bridge: VMware PCI Express Root Port (rev 01)
  35. 00:18.0 PCI bridge: VMware PCI Express Root Port (rev 01)
  36. 00:18.1 PCI bridge: VMware PCI Express Root Port (rev 01)
  37. 00:18.2 PCI bridge: VMware PCI Express Root Port (rev 01)
  38. 00:18.3 PCI bridge: VMware PCI Express Root Port (rev 01)
  39. 00:18.4 PCI bridge: VMware PCI Express Root Port (rev 01)
  40. 00:18.5 PCI bridge: VMware PCI Express Root Port (rev 01)
  41. 00:18.6 PCI bridge: VMware PCI Express Root Port (rev 01)
  42. 00:18.7 PCI bridge: VMware PCI Express Root Port (rev 01)
  43. 02:00.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
  44. [root@nagios ~]#
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP