探測linux是否運行xen

來源:互聯網
上載者:User

檢測linux主機是否運行  xen,其原理是讀取 CPUID 來判斷,Xen 原始碼下面有一段檢測是否是 Xen 的 C 語言代碼 tools/misc/xen-detect.c,這段代碼提供了一個很好的例子,重寫了代碼,用宏替代了函數。xentest.c

#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) ); void main(){        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+8) = edx;         string[12] = 0;        if (strncmp(string, "XenVMMXenVMM",12) == 0) {                printf("xen hvm\n");        } else          printf("bare hardware\n"); }

此程式在ubuntu xen4.12 環境下測試通過,如果在沒有安裝xen的環境下運行則會出現 “Illegal instruction (core dumped)” 錯誤,檢測 kvm功能有待進一步研究完善,與xen有區別不能通用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.