centos8环境判断当前操作系统是否虚拟机或容器
Posted architectforest
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos8环境判断当前操作系统是否虚拟机或容器相关的知识,希望对你有一定的参考价值。
一,阿里云ECS的centos环境
1,执行systemd-detect-virt
[root@yjweb ~]# systemd-detect-virt
kvm
说明阿里云的ecs是在一个kvm环境中运行
2,看dmidecode的system信息也行
[root@yjweb ~]# dmidecode -t system # dmidecode 3.2 Getting SMBios data from sysfs. SMBIOS 2.8 present. Handle 0x0100, DMI type 1, 27 bytes System Information Manufacturer: Alibaba Cloud Product Name: Alibaba Cloud ECS Version: pc-i440fx-2.1 Serial Number: f5df2afa-62a5-4c7b-89e5-0791e3ba8c92 UUID: f5df2afa-62a5-4c7b-89e5-0791e3ba8c92 Wake-up Type: Power Switch SKU Number: Not Specified Family: Not Specified Handle 0x2000, DMI type 32, 11 bytes System Boot Information Status: No errors detected
系统信息中的 Alibaba Cloud ECS 能看出机器是阿里云的机器
3,查看此ecs的centos版本
[root@yjweb ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,vmware fusion下的虚拟机
1,执行systemd-detect-virt
[root@localhost weblc]# systemd-detect-virt
vmware
2,看dmidecode的system信息
[root@localhost weblc]# dmidecode -t system # dmidecode 3.1 Getting SMBIOS data from sysfs. SMBIOS 2.7 present. Handle 0x0001, DMI type 1, 27 bytes System Information Manufacturer: VMware, Inc. Product Name: VMware Virtual Platform Version: None Serial Number: VMware-56 4d f8 f6 6c b3 ad f1-ff d5 80 97 fd ce 82 d4 UUID: F6F84D56-B36C-F1AD-FFD5-8097FDCE82D4 Wake-up Type: Power Switch SKU Number: Not Specified Family: Not Specified Handle 0x01A1, DMI type 15, 29 bytes System Event Log Area Length: 16 bytes Header Start Offset: 0x0000 Header Length: 16 bytes Data Start Offset: 0x0010 Access Method: General-purpose non-volatile data functions Access Address: 0x0000 Status: Invalid, Full Change Token: 0x00000036 Header Format: Type 1 Supported Log Type Descriptors: 3 Descriptor 1: POST error Data Format 1: POST results bitmap Descriptor 2: Single-bit ECC memory error Data Format 2: Multiple-event Descriptor 3: Multi-bit ECC memory error Data Format 3: Multiple-event Handle 0x0265, DMI type 23, 13 bytes System Reset Status: Enabled Watchdog Timer: Present Boot Option: Do Not Reboot Boot Option On Limit: Do Not Reboot Reset Count: Unknown Reset Limit: Unknown Timer Interval: Unknown Timeout: Unknown Handle 0x0268, DMI type 32, 20 bytes System Boot Information Status: No errors detected
三,docker容器:
1,如果是虚拟机中安装的操作系统中运行docker,则上面两个方法在容器中是失效的
[root@centos /]# systemd-detect-virt
vmware
这个错误,因为vmware是宿主机的虚拟平台
[root@centos /]# dmidecode -t system
返回也错误,因为返回的也是宿主机的信息
2,可以通过docker容器根分区专用的文件系统类型overlay来判断:
[root@centos /]# df -h / | grep overlay overlay 50G 25G 23G 52% /
如果根分区文件系统是ovlerlay,表示是在docker容器中
3,通过dockerenv文件判断
[root@centos /]# ls /.dockerenv /.dockerenv
如果存在/.dockerenv文件,则是在docker容器中
4,根据/proc/1/cgroup是否包含docker字样判断
[root@centos /]# cat /proc/1/cgroup | grep docker 11:pids:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 10:freezer:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 9:cpu,cpuacct:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 8:perf_event:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 7:hugetlb:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 6:cpuset:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 5:blkio:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 4:net_cls,net_prio:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 3:memory:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 2:devices:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286 1:name=systemd:/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286
/docker/ebe4fe937bb53c80227cb4772ea73888c381f639f97bee95ab06f0b976998286/init.scope
进程1的控制组是包含有docker的字样
四,systemd-detect-virt的用法
1,列出支持检测的虚拟环境
[root@centos /]# systemd-detect-virt --list none kvm qemu bochs xen uml vmware oracle microsoft zvm parallels bhyve qnx vm-other systemd-nspawn lxc-libvirt lxc openvz docker rkt container-other
2,非虚拟机或容器环境时,返回none
[root@centos /]# systemd-detect-virt
none
3,systemd-detect-virt查看版本
[root@centos /]# systemd-detect-virt --version systemd 239 +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy
4,只检测虚拟机
[root@centos /]# systemd-detect-virt -v
vmware
5,只检测容器
[root@centos /]# systemd-detect-virt -c
docker
说明:如果是虚拟机中的容器会检测不到
以上是关于centos8环境判断当前操作系统是否虚拟机或容器的主要内容,如果未能解决你的问题,请参考以下文章