mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-12 19:24:40 +00:00
58a6c9b2b8
Now: /tmp/breakglass3929186798 # /user/tailscale debug hostinfo { "IPNVersion": "1.23.0-date.20220107", "OS": "linux", "OSVersion": "Gokrazy; kernel=5.16.11", "DeviceModel": "Raspberry Pi 4 Model B Rev 1.2", "Hostname": "gokrazy", "GoArch": "arm64" } Also, cache the distro lookup. It doesn't change while the program is running: name old time/op new time/op delta Get-6 5.21µs ± 5% 0.00µs ± 3% -99.91% (p=0.008 n=5+5) name old alloc/op new alloc/op delta Get-6 792B ± 0% 0B -100.00% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Get-6 8.00 ± 0% 0.00 -100.00% (p=0.008 n=5+5) Updates #1866 Change-Id: Ifb9a63b94287010d3f4c8bfeb6b78119e8a9b203 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
131 lines
3.1 KiB
Go
131 lines
3.1 KiB
Go
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build linux && !android
|
|
// +build linux,!android
|
|
|
|
package hostinfo
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"tailscale.com/util/lineread"
|
|
"tailscale.com/version/distro"
|
|
)
|
|
|
|
func init() {
|
|
osVersion = osVersionLinux
|
|
packageType = packageTypeLinux
|
|
|
|
if v := linuxDeviceModel(); v != "" {
|
|
SetDeviceModel(v)
|
|
}
|
|
}
|
|
|
|
func linuxDeviceModel() string {
|
|
for _, path := range []string{
|
|
// First try the Synology-specific location.
|
|
// Example: "DS916+-j"
|
|
"/proc/sys/kernel/syno_hw_version",
|
|
|
|
// Otherwise, try the Devicetree model, usually set on
|
|
// ARM SBCs, etc.
|
|
// Example: "Raspberry Pi 4 Model B Rev 1.2"
|
|
"/sys/firmware/devicetree/base/model", // Raspberry Pi 4 Model B Rev 1.2"
|
|
} {
|
|
b, _ := os.ReadFile(path)
|
|
if s := strings.Trim(string(b), "\x00\r\n\t "); s != "" {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func osVersionLinux() string {
|
|
dist := distro.Get()
|
|
propFile := "/etc/os-release"
|
|
switch dist {
|
|
case distro.Synology:
|
|
propFile = "/etc.defaults/VERSION"
|
|
case distro.OpenWrt:
|
|
propFile = "/etc/openwrt_release"
|
|
}
|
|
|
|
m := map[string]string{}
|
|
lineread.File(propFile, func(line []byte) error {
|
|
eq := bytes.IndexByte(line, '=')
|
|
if eq == -1 {
|
|
return nil
|
|
}
|
|
k, v := string(line[:eq]), strings.Trim(string(line[eq+1:]), `"'`)
|
|
m[k] = v
|
|
return nil
|
|
})
|
|
|
|
var un syscall.Utsname
|
|
syscall.Uname(&un)
|
|
|
|
var attrBuf strings.Builder
|
|
attrBuf.WriteString("; kernel=")
|
|
for _, b := range un.Release {
|
|
if b == 0 {
|
|
break
|
|
}
|
|
attrBuf.WriteByte(byte(b))
|
|
}
|
|
if inContainer() {
|
|
attrBuf.WriteString("; container")
|
|
}
|
|
if env := GetEnvType(); env != "" {
|
|
fmt.Fprintf(&attrBuf, "; env=%s", env)
|
|
}
|
|
attr := attrBuf.String()
|
|
|
|
id := m["ID"]
|
|
|
|
switch id {
|
|
case "debian":
|
|
slurp, _ := ioutil.ReadFile("/etc/debian_version")
|
|
return fmt.Sprintf("Debian %s (%s)%s", bytes.TrimSpace(slurp), m["VERSION_CODENAME"], attr)
|
|
case "ubuntu":
|
|
return fmt.Sprintf("Ubuntu %s%s", m["VERSION"], attr)
|
|
case "", "centos": // CentOS 6 has no /etc/os-release, so its id is ""
|
|
if cr, _ := ioutil.ReadFile("/etc/centos-release"); len(cr) > 0 { // "CentOS release 6.10 (Final)
|
|
return fmt.Sprintf("%s%s", bytes.TrimSpace(cr), attr)
|
|
}
|
|
fallthrough
|
|
case "fedora", "rhel", "alpine", "nixos":
|
|
// Their PRETTY_NAME is fine as-is for all versions I tested.
|
|
fallthrough
|
|
default:
|
|
if v := m["PRETTY_NAME"]; v != "" {
|
|
return fmt.Sprintf("%s%s", v, attr)
|
|
}
|
|
}
|
|
switch dist {
|
|
case distro.Synology:
|
|
return fmt.Sprintf("Synology %s%s", m["productversion"], attr)
|
|
case distro.OpenWrt:
|
|
return fmt.Sprintf("OpenWrt %s%s", m["DISTRIB_RELEASE"], attr)
|
|
case distro.Gokrazy:
|
|
return fmt.Sprintf("Gokrazy%s", attr)
|
|
}
|
|
return fmt.Sprintf("Other%s", attr)
|
|
}
|
|
|
|
func packageTypeLinux() string {
|
|
// Report whether this is in a snap.
|
|
// See https://snapcraft.io/docs/environment-variables
|
|
// We just look at two somewhat arbitrarily.
|
|
if os.Getenv("SNAP_NAME") != "" && os.Getenv("SNAP") != "" {
|
|
return "snap"
|
|
}
|
|
return ""
|
|
}
|