在 WebHostingTalk 论坛上有些国外奸商会把虚拟机当作独立服务器卖,去年7月份的时候就有一位中国同胞上当受骗,并在 WHT 上发帖声讨,证据确凿,甚至连服务商自己也承认,回帖达355篇。这家独立服务器/VPS 提供商 HostATree.com 居然大胆的把 OpenVZ VPS 这种一看就知道是虚拟机的虚拟机当作独立服务器卖,晕,至少也要弄个 VMWare/KVM/Xen HVM 吧(更难发现是虚拟机),用 OpenVZ 这种容器也太欺负人了:)昨天恰好收到网友一封邮件问到了如何判断自己买的是独立服务器还是虚拟机的问题。这里 VPSee 简单介绍一下市面上常用虚拟技术(包括容器技术)的判别小技巧。

判断 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.comimport sys, osdef main():if os.getuid() != 0:print "must be run as root"sys.exit(0)# check OpenVZ/Virtuozzoif os.path.exists("/proc/vz"):if not os.path.exists("/proc/bc"):print "openvz container"else:print "openvz node"# check Xenif 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 编译后就可以运行:

  /******************************************************************************
  * xen_detect.c
  *
  * Simple GNU C / POSIX application to detect execution on Xen VMM platform.
  *
  * Copyright (c) 2007, XenSource Inc.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
  * deal in the Software without restriction, including without limitation the
  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  * sell copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  */
   
  #include <stdint.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <sys/types.h>
  #include <sys/wait.h>
  #include <unistd.h>
   
  static int pv_context;
   
  static void cpuid(uint32_t idx,
  uint32_t *eax,
  uint32_t *ebx,
  uint32_t *ecx,
  uint32_t *edx)
  {
  asm volatile (
  "test %1,%1 ; jz 1f ; ud2a ; .ascii \"xen\" ; 1: cpuid"
  : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
  : "0" (idx), "1" (pv_context) );
  }
   
  static int check_for_xen(void)
  {
  uint32_t eax, ebx, ecx, edx;
  char signature[13];
  uint32_t base;
   
  for ( base = 0x40000000; base < 0x40001000; base += 0x100 )
  {
  cpuid(base, &eax, &ebx, &ecx, &edx);
   
  *(uint32_t *)(signature + 0) = ebx;
  *(uint32_t *)(signature + 4) = ecx;
  *(uint32_t *)(signature + 8) = edx;
  signature[12] = '\0';
   
  if ( !strcmp("XenVMMXenVMM", signature) && (eax >= (base + 2)) )
  goto found;
  }
   
  return 0;
   
  found:
  cpuid(base + 1, &eax, &ebx, &ecx, &edx);
  printf("Running in %s context on Xen v%d.%d.\n",
  pv_context ? "PV" : "HVM", (uint16_t)(eax >> 16), (uint16_t)eax);
  return 1;
  }
   
  int main(void)
  {
  pid_t pid;
  int status;
  uint32_t dummy;
   
  /* Check for execution in HVM context. */
  if ( check_for_xen() )
  return 0;
   
  /* Now we check for execution in PV context. */
  pv_context = 1;
   
  /*
  * Fork a child to test the paravirtualised CPUID instruction.
  * If executed outside Xen PV context, the extended opcode will fault.
  */
  pid = fork();
  switch ( pid )
  {
  case 0:
  /* Child: test paravirtualised CPUID opcode and then exit cleanly. */
  cpuid(0x40000000, &dummy, &dummy, &dummy, &dummy);
  exit(0);
  case -1:
  fprintf(stderr, "Fork failed.\n");
  return 0;
  }
   
  /*
  * Parent waits for child to terminate and checks for clean exit.
  * Only if the exit is clean is it safe for us to try the extended CPUID.
  */
  waitpid(pid, &status, 0);
  if ( WIFEXITED(status) && check_for_xen() )
  return 0;
   
  printf("Not running on Xen.\n");
  return 0;
  }

判断 VirtualBox/Virtual PC

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

如何判断 Linux 是否运行在虚拟机上相关推荐

  1. win10系统下安装Linux虚拟机以及在虚拟机上安装Ubuntu

    一.前期准备工作 1.成功安装完成VMware软件: 链接:https://pan.baidu.com/s/1gWinLJpfWdAQ8AyEkZxpfg 密码:i2ap 2.下载Ubuntu 镜像文 ...

  2. 【Linux Mint】VMware虚拟机上安装Linux并配置并下载pycharm

    前言: 最近新购置了一台电脑,在这台电脑的初始化过程中,我想把开发python项目的pycharm和开发java项目的IDEA放到虚拟机的Linux上来运行.然后自己的Windows11就放个pyth ...

  3. vmware linux系统 ip,修改虚拟机上Linux系统的IP地址

    JDBC 常用词汇以及初步思想 ####JDBC程序访问数据库步骤 Why NSAttributedString import html must be on main thread? The HTM ...

  4. 为什么虚拟机上一运行就显示程序停止_五分钟学Java:如何学习Java面试必考的JVM虚拟机...

    原创声明 本文首发于微信公众号[程序员黄小斜] 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 本文思维导图 为什么要学习JVM虚拟机 最近的你有没有参加Java面试呢?你有没有发现,Java ...

  5. 为什么虚拟机上一运行就显示程序停止_五分钟学Java:如何学习Java面试必考的JVM虚拟机||CSDN博文精选...

    作者:黄小斜 原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 本文思维导图 为什么要学习JVM虚拟机 最近的你有没有参加Java面试呢?你有没有发现,Java面试中总是爱考一类问题, ...

  6. 数据系列:如何在Windows Azure虚拟机上设置SQL Server

    编者按: 今天的帖子来自我们的客户体验团队高级技术作家Rick Byham.这篇文章提供的详细指导文档概述的链接在底部 将数据库操作移动到云上的速度最快的方法之一是通过移动您的数据库到Windows ...

  7. 容器部署在物理机还是虚拟机上?

    如果运行在物理机上,那么资源会最大程度的得到利用,但是会牺牲一定的隔离性及安全性,特别是在企业没有规范的CI/CD镜像交付流程时,不同租户的容器会相互影响. 如果运行在虚拟机上,那么隔离性和安全性都会 ...

  8. 如何在VMware虚拟机上安装Linux操作系统(Ubuntu)

    作为初学者想变为计算机大牛非一朝一夕,但掌握基本的计算机操作和常识却也不是多么难的事情.所以作为一名工科男,为了把握住接近女神的机会,也为了避免当白痴,学会装系统吧!of course为避免把自己的电 ...

  9. RHEV平台中如何在 RED HAT ENTERPRISE LINUX 虚拟机上安装 GUEST 代理和驱动

    使用 Red Hat Enterprise Virtualization Agent 软件仓库所提供的 rhevm-guest-agent 软件包可以在 Red Hat Enterprise Linu ...

最新文章

  1. pthread多线程编程的学习小结
  2. 【Python】七段数码管绘制日期年月日
  3. android html图片点击事件,Android TextView加载HTMl图文之添加点击事件和查看图片
  4. 快捷配置mysql_windows下的mysql的快捷安装方法和简单配置
  5. python 清华镜像pip install
  6. 询问区间内出现次数最多的数出现的次数
  7. clientWidth,clientHeight 窗口宽高获取异常问题记录
  8. 数据分析-信用卡反欺诈模型
  9. 风格迁移应用_图像风格迁移
  10. Mac破解软件 “XXX”意外退出 奔溃解决方法
  11. 跳一跳html小游戏代码,100 行代码实现『跳一跳』辅助
  12. 博士“申请考核制”经验
  13. win10找不到网络里的计算机,Win10专业版找不到网络中的其他电脑
  14. 六线小县城实录美食篇
  15. Git Tower 3.2 - 最好用的代码管理工具
  16. 什么是嵌入式设备?/ 嵌入式设备的定义
  17. 既然阻塞 I/O 会使线程休眠,为什么 Java 线程状态却是 RUNNABLE?
  18. 帝国cms e loop php,帝国CMS灵动标签e:loop的使用 标签循环
  19. 如何原封不动的保存并安全回显输入内容
  20. 计算机无法ghost安装系统安装系统安装,电脑中无法安装VMware虚拟机Ghost系统的解决方法...

热门文章

  1. 【Excel】Excel根据单元格背景色求和
  2. jira4.4.5下载与汉化
  3. 需求的推动力-网线啥的
  4. 在ubuntu下安显卡驱动
  5. 微软聘请游说公司为收购雅虎作势
  6. Spring中@Pattern的使用
  7. 这年头居然连MSDN Library都靠不住呀
  8. 运营商助力新型智慧城市建设
  9. 《ANTLR 4权威指南》——2.1节从ANTLR元语言开始
  10. HDU1081:To The Max(最大子矩阵,线性DP)