如何检测Linux Guest VM使用的哪种虚拟化技术

如何检测Linux Guest VM使用的哪种虚拟化技术

本篇文章给大家分享的是有关如何检测Linux Guest VM使用的哪种虚拟化技术,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

写在前面的话

如果我有一台基于云的虚拟机,并且运行了Ubuntu或CentOS的Linux服务器系统,而这些设备可能是虚拟化的,也可能不是虚拟化的,那我们应该如何去判断这台Linux Guest系统设备使用的是哪种虚拟化技术(VMWARE/ KVM/ XEN/ VirtualBox/ Container/lxc/Hyper-V等等)呢?那我们如何才能识别一台CentOS 7 Linux VPS所使用的虚拟化技术类型呢?

技术分析

我们需要使用virt-what程序(一个Shell脚本)来检测目标设备系统所使用的虚拟化技术类型,它可以打印出每一条关于目标设备虚拟化技术的相关信息。我们将介绍如何去判断Linux Guest VM虚拟化技术类型。

判断Linux Guest VM虚拟化技术类型

正如我们刚才说的,我们需要使用virt-what Linux命令来判断一个程序当前是否在一台虚拟机设备上运行。该脚本支持各种虚拟机管理程序,接下来,我们看看如何在不同的Linux发行版系统中安装virt-what。

Debian或Ubuntu Linux安装

首先,运行下列apt命令/apt-get命令:

$sudoapt-getinstallvirt-what

或者

$sudoaptinstallvirt-what

在RHEL/CentOS/Scientific Linux VM中安装virt-what

我们可以使用yum命令完成安装:

$sudoyuminstallvirt-what

Fedora Linux VM安装virt-what来检测当前环境是否为虚拟机环境

执行dnf命令:

$sudodnfinstallvirt-what

Suse/OpenSUSE安装

运行zypper命令:

sudozypperinvirt-what

判断远程服务器是否为虚拟机环境

我们可以直接输入并运行下列命令:

$$sudovirt-whatxenxen-domU

输出结果:

$$sudovirt-whatkvm

其他输出结果:

$$sudovirt-whatlxc

下面给出的是AWS返回的结果:

$sudovirt-whatxenxen-hvmaws

其他可能的值

1、hyperv : 这是微软的Hyper-V管理程序;

2、parallels : 访客用户正在Parallels虚拟平台(Parallels Desktop、Parallels Server)中运行;

3、powervm_lx86 : 访客用户正在IBM powervm lx86 Linux/x86模拟器中运行;

4、qemu : 这是使用软件模拟技术的QEMU管理程序;

5、virtualpc : 访客用户正在微软VirualPC上运行;

6、xen-hvm : 这是一个Xen Guest完全虚拟化平台(HVM);

7、uml : 这是一个用户模式Linux(UML)客户机;

8、openvz : 访客用户正在OpenVZ或Virtuozzo容器中运行;

9、linux_vserver : 此进程正在Linux VServer容器中运行;

10、ibm_systemz : 这是一个IBM SystemZ硬件分区系统;

如果没有任何输出结果,那意味着我们的程序可能是在裸机上运行的,或者程序是在一种我们无法检测到的虚拟机系统中运行的。

检测脚本源代码

下面给出的是脚本的源代码:

#!/bin/sh-#virt-what.Generatedfromvirt-what.inbyconfigure.#Copyright(C)2008-2017RedHatInc.##Thisprogramisfreesoftware;youcanredistributeitand/ormodify#itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby#theFreeSoftwareFoundation;eitherversion2oftheLicense,or#(atyouroption)anylaterversion.##Thisprogramisdistributedinthehopethatitwillbeuseful,#butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof#MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe#GNUGeneralPublicLicenseformoredetails.##YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense#alongwiththisprogram;ifnot,writetotheFreeSoftware#Foundation,Inc.,675MassAve,Cambridge,MA02139,USA.#'virt-what'triestodetectthetypeofvirtualizationbeing#used(ornoneatallifwe'rerunningonbare-metal).Itprints#outoneormorelineseachbeinga'fact'aboutthevirtualization.##Pleaseseealsothemanualpagevirt-what(1).#Thisscriptshouldberunasroot.##Thefollowingresourceswereusefulinwritingthisscript:#.http://dmo.ca/blog/detecting-virtualization-on-linux/#Donotallowunsetvariables,andsetdefaults.set-uroot=''skip_qemu_kvm=falseskip_lkvm=falseVERSION="1.20"have_cpuinfo(){test-e"${root}/proc/cpuinfo"}use_sysctl(){#Lacking/proc,onsomesystemssysctlcanbeusedinstead.OS=$(uname)||fail"failedtogetoperatingsystemname"["$OS"="OpenBSD"]}fail(){echo"virt-what:$1">&2exit1}usage(){echo"virt-what[options]"echo"Options:"echo"--helpDisplaythishelp"echo"--versionDisplayversionandexit"exit0}#Handlethecommandlinearguments,ifany.whiletest$#-gt0;docase"$1"in--help)usage;;--test-root=*)#Deliberatelyundocumented:usedfor'makecheck'.root=$(echo"$1"|sed's/.*=//')shift1test-z"$root"&&fail"--test-rootoptionrequiresavalue";;-v|--version)echo"$VERSION";exit0;;--)shift;break;;*)fail"unrecognizedoption'$1'";;esacdonetest$#-gt0&&fail"extraoperand'$1'"#Add/sbinand/usr/sbintothepathsowecanfindsystem#binarieslikedmidecode.#Add/usr/libexectothepathsowecanfindthehelperbinary.prefix=/usr/localexec_prefix=${prefix}PATH="${root}${exec_prefix}/libexec:${root}/sbin:${root}/usr/sbin:${PATH}"exportPATH#Checkwe'rerunningasroot.EFFUID=$(id-u)||fail"failedtogetcurrentuserid"if["x$root"="x"]&&["$EFFUID"-ne0];thenfail"thisscriptmustberunasroot"fi#TrytolocatetheCPU-IDhelperprogramCPUID_HELPER=$(whichvirt-what-cpuid-helper2>/dev/null)if[-z"$CPUID_HELPER"];thenfail"virt-what-cpuid-helperprogramnotfoundin\$PATH"fi#ManyfullvirthypervisorsgiveanindicationthroughCPUID.Usethe#helperprogramtogetthisinformation.cpuid=$(virt-what-cpuid-helper)#CheckforvariousproductsintheBIOSinformation.#Notethatdmidecodedoesn'texistonallarchitectures.Ontheones#itdoesnot,thenthiswillreturnanerror,whichisignored(error#messageredirectedintothe$dmivariable).dmi=$(LANG=Cdmidecode2>&1)#Architecture.#Noteforthepurposeoftesting,weonlycallunamewith-moption.arch=$(uname-m|sed-e's/i.86/i386/'|sed-e's/arm.*/arm/')#CheckforVMware.#cpuidcheckaddedbyChetanLoke.if["$cpuid"="VMwareVMware"];thenechovmwareelifecho"$dmi"|grep-q'Manufacturer:VMware';thenechovmwarefi#CheckforHyper-V.#http://blogs.msdn.com/b/sqlosteam/archive/2010/10/30/is-this-real-the-metaphysics-of-hardware-virtualization.aspxif["$cpuid"="MicrosoftHv"];thenechohypervfi#CheckforVirtualPC.#ThenegativecheckforcpuidistodistinguishthisfromHyper-V#whichalsohasthesamemanufacturerstringintheSM-BIOSdata.if["$cpuid"!="MicrosoftHv"]&&echo"$dmi"|grep-q'Manufacturer:MicrosoftCorporation';thenechovirtualpcfi#CheckforVirtualBox.#AddedbyLaurentLéonard.ifecho"$dmi"|grep-q'Manufacturer:innotekGmbH';thenechovirtualboxfi#Checkforbhyve.if["$cpuid"="bhyvebhyve"];thenechobhyveelifecho"$dmi"|grep-q"Vendor:BHYVE";thenechobhyvefi#CheckforOpenVZ/Virtuozzo.#AddedbyEvgeniySokolov.#/proc/vz-alwaysexistsifOpenVZkernelisrunning(insideandoutside#container)#/proc/bc-existsonnode,butnotinsidecontainer.if[-d"${root}/proc/vz"-a!-d"${root}/proc/bc"];thenechoopenvzfi#CheckforLXCcontainers#http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface#AddedbyMarcFournierif[-e"${root}/proc/1/environ"]&&cat"${root}/proc/1/environ"|tr'\000''\n'|grep-Eiq'^container=';thenecholxcfi#CheckforLinux-VServeriftest-e"${root}/proc/self/status"\&&cat"${root}/proc/self/status"|grep-q"VxID:[0-9]*";thenecholinux_vserverifgrep-q"VxID:0$""${root}/proc/self/status";thenecholinux_vserver-hostelseecholinux_vserver-guestfifi#CheckforUML.#AddedbyLaurentLéonard.ifhave_cpuinfo&&grep-q'UML'"${root}/proc/cpuinfo";thenechoumlfi#CheckforIBMPowerVMLx86Linux/x86emulator.ifhave_cpuinfo&&grep-q'^vendor_id.*PowerVMLx86'"${root}/proc/cpuinfo"thenechopowervm_lx86fi#CheckforHitachiVirtualizationManager(HVM)Virtagelogicalpartitioning.ifecho"$dmi"|grep-q'Manufacturer.*HITACHI'&&echo"$dmi"|grep-q'Product.*LPAR';thenechovirtagefi#CheckforIBMSystemZ.ifhave_cpuinfo&&grep-q'^vendor_id.*IBM/S390'"${root}/proc/cpuinfo";thenechoibm_systemzif[-f"${root}/proc/sysinfo"];thenifgrep-q'VM.*ControlProgram.*KVM/Linux'"${root}/proc/sysinfo";thenechoibm_systemz-kvmelifgrep-q'VM.*ControlProgram.*z/VM'"${root}/proc/sysinfo";thenechoibm_systemz-zvmelifgrep-q'^LPAR'"${root}/proc/sysinfo";thenechoibm_systemz-lparelse#Thisisunlikelytobecorrect.echoibm_systemz-directfififi#CheckforParallels.ifecho"$dmi"|grep-q'Vendor:Parallels';thenechoparallelsskip_qemu_kvm=truefi#CheckforoVirt/RHEV.ifecho"$dmi"|grep-q'Manufacturer:oVirt';thenechoovirtfiifecho"$dmi"|grep-q'ProductName:RHEVHypervisor';thenechorhevfi#CheckforXen.if["$cpuid"="XenVMMXenVMM"]&&!echo"$dmi"|grep-q'NoSMBIOSnorDMIentrypointfound,sorry';thenechoxen;echoxen-hvmskip_qemu_kvm=trueelif[-d"${root}/proc/xen"];thenechoxenifgrep-q"control_d""${root}/proc/xen/capabilities"2>/dev/null;thenechoxen-dom0elseechoxen-domUfiskip_qemu_kvm=trueskip_lkvm=trueelif[-f"${root}/sys/hypervisor/type"]&&grep-q"xen""${root}/sys/hypervisor/type";then#Ordinarykernelwithpv_ops.Theredoesnotseemtobe#enoughinformationatpresenttotellwhetherthisisdom0#ordomU.XXXechoxenelif["$arch"="arm"]||["$arch"="aarch74"];thenif[-d"${root}/proc/device-tree/hypervisor"]&&grep-q"xen""${root}/proc/device-tree/hypervisor/compatible";thenechoxenskip_qemu_kvm=trueskip_lkvm=truefielif["$arch"="ia64"];thenif[-d"${root}/sys/bus/xen"-a!-d"${root}/sys/bus/xen-backend"];then#PV-on-HVMdriversinstalledinaXenguest.echoxenechoxen-hvmelse#ThereisnovirtleafonIA64HVM.Thisisalast-ditch#attempttodetectsomethingisvirtualizedbyusinga#timingattack.virt-what-ia64-xen-rdtsc-test>/dev/null2>&1case"$?"in0);;#notvirtual1)#Couldbesomesortofvirt,orcouldjustbeabitslow.echovirtesacfifi#CheckforQEMU/KVM.##ParallelsexportsKVMKVMKVMleaf,soskipthistestifwe'vealready#seenthatit'sParallels.XenusesQEMUasthedevicemodel,so#skipthistestifweknowitisXen.if!"$skip_qemu_kvm";thenif["$cpuid"="KVMKVMKVM"];thenechokvmelif["$cpuid"="TCGTCGTCGTCG"];thenechoqemuskip_lkvm=trueelifecho"$dmi"|grep-q'ProductName:KVM';thenechokvmskip_lkvm=trueelifecho"$dmi"|grep-q'Manufacturer:QEMU';then#ThetestforKVMabovefailed,sonowweknowwe're#notusingKVMacceleration.echoqemuskip_lkvm=trueelif["$arch"="arm"]||["$arch"="aarch74"];thenif[-d"${root}/proc/device-tree"]&&ls"${root}/proc/device-tree"|grep-q"fw-cfg";then#Wedon'thaveenoughinformationtodetermineifwe're#usingKVMaccelerationornot.echoqemuskip_lkvm=truefielif[-d${root}/proc/device-tree/hypervisor]&&grep-q"linux,kvm"/proc/device-tree/hypervisor/compatible;then#WearerunningasaspaprKVMguestonppc64echokvmskip_lkvm=trueelifuse_sysctl;then#SmartOSKVMproduct=$(sysctl-nhw.product)ifecho"$product"|grep-q'SmartDCHVM';thenechokvmfielse#Thisisknowntofailforqemuwiththeexplicit-cpu#option,since/proc/cpuinfowillnotcontaintheQEMU#string.QEMU2.10addedanewCPUIDleaf,sothis#problemonlytriggeredforolderQEMUifhave_cpuinfo&&grep-q'QEMU'"${root}/proc/cpuinfo";thenechoqemufififiif!"$skip_lkvm";thenif["$cpuid"="LKVMLKVMLKVM"];thenecholkvmelif["$arch"="arm"]||["$arch"="aarch74"];thenif[-d"${root}/proc/device-tree"]&&grep-q"dummy-virt""${root}/proc/device-tree/compatible";thenecholkvmfififi#CheckforDocker.if[-f"${root}/.dockerinit"];thenechodockerfi#Checkppc64lpar,kvmorpowerkvm#example/proc/cpuinfolineindicating'notbaremetal'#platform:pSeries##example/proc/ppc64/lparcfgsystemtypeline#system_type=IBMpSeries(emulatedbyqemu)if["$arch"="ppc64"]||["$arch"="ppc64le"];thenifhave_cpuinfo&&grep-q'platform.**pSeries'"${root}/proc/cpuinfo";thenifgrep-q'model.*emulatedbyqemu'"${root}/proc/cpuinfo";thenechoibm_power-kvmelse#AssumeLPAR,nowdetectsharedordedicatedifgrep-q'shared_processor_mode=1'"${root}/proc/ppc64/lparcfg";thenechoibm_power-lpar_sharedelseechoibm_power-lpar_dedicatedfi#detectpowerkvm?fififi#CheckforOpenBSD/VMMif["$cpuid"="OpenBSDVMM58"];thenechovmmfi#CheckforLDomsif["${arch#sparc}"!="$arch"]&&[-e"${root}/dev/mdesc"];thenecholdomsif[-d"${root}/sys/class/vlds/ctrl"]&&\[-d"${root}/sys/class/vlds/sp"];thenecholdoms-controlelseecholdoms-guestfiMDPROP="${root}/usr/lib/ldoms/mdprop.py"if[-x"${MDPROP}"];thenif[-n"$($MDPROP-viodevicedevice-type=pciex)"];thenecholdoms-rootecholdoms-ioelif[-n"$($MDPROP-viov-devicevf-id=0)"];thenecholdoms-iofififi#CheckforAWS.#AWSonXen.ifecho"$dmi"|grep-q'Version:[0-9]\.[0-9]\.amazon';thenechoaws#AWSonbaremetalorKVM.elifecho"$dmi"|grep-q'Vendor:AmazonEC2';thenechoawsfi

如何使用dmidecode命令来寻找相同的信息

配合Bash for循环并使用dmidecode命令判断目标虚拟化技术:

foriinsystem-manufacturersystem-product-namedosudodmidecode-s$idone

样本输出:

RedHatKVM

其他用来判断虚拟化技术的命令

我们还可以使用systemd-detect-virt命令来检测虚拟化环境:

systemd-detect-virt

以上就是如何检测Linux Guest VM使用的哪种虚拟化技术,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注恰卡编程网行业资讯频道。

发布于 2021-12-23 21:10:47
收藏
分享
海报
0 条评论
29
上一篇:如何绕过Slack后端的SSRF防护机制 下一篇:如何挖到多个D-LINK高危漏洞
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码