github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/internal/machineid/id_linux.go (about)

     1  //go:build linux
     2  
     3  package machineid
     4  
     5  import (
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  const (
    11  	// dbusPath is the default path for dbus machine id.
    12  	dbusPath = "/var/lib/dbus/machine-id"
    13  
    14  	// dbusPathEtc is the default path for dbus machine id located in /etc.
    15  	// Some systems (like Fedora 20) only know this path.
    16  	// Sometimes it's the other way round.
    17  	dbusPathEtc = "/etc/machine-id"
    18  
    19  	// Use the boot_id generated on each boot as fallback.
    20  	bootIDPath = "/proc/sys/kernel/random/boot_id"
    21  )
    22  
    23  func readPlatformMachineID() (string, error) {
    24  	b, err := os.ReadFile(dbusPath)
    25  	if err != nil || len(b) == 0 {
    26  		b, err = os.ReadFile(dbusPathEtc)
    27  		if err != nil || len(b) == 0 {
    28  			b, err = os.ReadFile(bootIDPath)
    29  		}
    30  	}
    31  	return strings.TrimSpace(string(b)), err
    32  }