1. dmidecode 是解析/dev/mem 获取硬件信息
  2. 容器(docker)内无法使用/dev/mem ,因此需要挂载/dev/mem
  3. 容器(docker)无法使用dmidecode,在这里有2种解决办法,
    a. 挂载/sbin/dmidecode 到docker内
    b.docker 安装dmidecode ,yum install -y dmidecode
  4. 运行
docker run -it --privileged -v /dev/mem:/dev/mem -v /sbin/dmidecode:/sbin/dmidecode 5d0da3dc9764
[root@7a94b5a3b51b /]# dmidecode -s system-serial-number
VMware-56 4d 66 09 8c 94 f1 aa-1c 26 59 1f 79 89 12 7a
package main

import (
	"fmt"
	"os/exec"
)

func main() {
	// 获取系统序列号
	cmdSn := exec.Command("/bin/sh", "-c", `dmidecode -s  system-serial-number `)
	ssn, err := cmdSn.Output()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("system serial number:%s \n", string(ssn))

	// 获取系统id
	cmdUuid := exec.Command("/bin/sh", "-c", `dmidecode -s  system-uuid `)
	uuid, err := cmdUuid.Output()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("system uuid:%s \n", string(uuid))

	// 获取系统id
	cmdPid := exec.Command("/bin/sh", "-c", `dmidecode -t processor | grep 'ID' | awk -F ':' '{print $2}' | head -n 1`)
	pid, err := cmdPid.Output()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("system processor ID:%s \n", string(pid))

}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐