# dmidecode -t system System Information Manufacturer: HP Product Name: ProLiant DL380 Gen9 Version: Not Specified Serial Number: 6CU6468KKD UUID: 30393137-3136-4336-5536-3436384B4B44 Wake-up Type: Power Switch SKU Number: 719061-B21 Family: ProLiant
# dmidecode -t system # dmidecode 3.1 Getting SMBIOS data from sysfs. SMBIOS 2.8 present.
Handle 0x0100, DMI type 1, 27 bytes System Information Manufacturer: OpenStack Foundation Product Name: OpenStack Nova Version: 15.0.1 Serial Number: 00310981-9899-e411-906e-00163566263e UUID: 1a48e29f-a023-48b8-b06b-afa63a9cff00 Wake-up Type: Power Switch SKU Number: Not Specified Family: Virtual Machine
$ sudo dmidecode -t system System Information Manufacturer: Xen Product Name: HVM domU Version: 4.2.amazon Serial Number: ec24ea61-ebbd-d428-e3d0-50ca37b49074 UUID: ec24ea61-ebbd-d428-e3d0-50ca37b49074 Wake-up Type: Power Switch SKU Number: Not Specified Family: Not Specified
在 Version 中标明了amazon字样。 阿里云虚拟机如下:
1 2 3 4 5 6 7 8 9 10
# dmidecode -t system System Information Manufacturer: Alibaba Cloud Product Name: Alibaba Cloud ECS Version: pc-i440fx-2.1 Serial Number: f1099eb2-f7b6-4b8f-a02e-0a004b66dc6a UUID: F1099EB2-F7B6-4B8F-A02E-0A004B66DC6A Wake-up Type: Power Switch SKU Number: Not Specified Family: Not Specified
可见虽然可以从 system 信息中获取云厂商的线索,但其实虚拟机的 system 信息并没有统一的标准,有的在version中体现,有的在Product Name中表现,完全取决于云厂商自己的配置。
detect_container() { if which systemd-detect-virt >/dev/null 2>&1; then TYPE=$(systemd-detect-virt -c) if [ "$TYPE" = "none" ]; then return 1 else echo "Container: $TYPE" return 0 fi fi if [ -n "$container" ]; then echo "Container: $container" return 0 fi if grep -qi docker /proc/1/cgroup; then echo "Container: Docker" return 0 fi if test -f /.dockerenv; then echo "Container: Docker" return 0 fi if grep -qi 'machine-rkt' /proc/1/cgroup; then echo "Container: rkt" return 0 fi # Other container type detect here return 1 }
detect_physical() { if ! lscpu | grep -qi 'Hypervisor vendor'; then echo "Physical: $(cat /sys/class/dmi/id/product_name)" return 0 fi return 1 }
detect_virtual_machine() { if lscpu | grep -qi 'Hypervisor vendor'; then HYPER_TYPE=$(lscpu | grep -i "Hypervisor vendor" \ | cut -d ':' -f 2 | sed 's/^ *//g') if dmidecode -t system | grep -qi 'amazon'; then echo "Virtual Machine: AWS/$HYPER_TYPE" elif dmidecode -t system | grep -qi 'openstack'; then echo "Virtual Machine: OpenStack/$HYPER_TYPE" elif dmidecode -t system | grep -qi 'alibaba'; then echo "Virtual Machine: Aliyun/$HYPER_TYPE" else Manufacturer=$(dmidecode -t system | grep 'Manufacturer' \ | cut -d ':' -f 2 | sed 's/^ *//g') ProductName=$(dmidecode -t system | grep 'Product Name' \ | cut -d ':' -f 2 | sed 's/^ *//g') Version=$(dmidecode -t system | grep 'Version' \ | cut -d ':' -f 2 | sed 's/^ *//g') echo "Virtual Machine: $Manufacturer $ProductName($Version)/$HYPER_TYPE" fi return 0 fi return 1 }