hostinfo_linux.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build linux && !android
  4. package hostinfo
  5. import (
  6. "bytes"
  7. "os"
  8. "strings"
  9. "golang.org/x/sys/unix"
  10. "tailscale.com/types/ptr"
  11. "tailscale.com/util/lineread"
  12. "tailscale.com/version/distro"
  13. )
  14. func init() {
  15. osVersion = lazyOSVersion.Get
  16. packageType = packageTypeLinux
  17. distroName = distroNameLinux
  18. distroVersion = distroVersionLinux
  19. distroCodeName = distroCodeNameLinux
  20. if v := linuxDeviceModel(); v != "" {
  21. SetDeviceModel(v)
  22. }
  23. }
  24. var (
  25. lazyVersionMeta = &lazyAtomicValue[versionMeta]{f: ptr.To(linuxVersionMeta)}
  26. lazyOSVersion = &lazyAtomicValue[string]{f: ptr.To(osVersionLinux)}
  27. )
  28. type versionMeta struct {
  29. DistroName string
  30. DistroVersion string
  31. DistroCodeName string // "jammy", etc (VERSION_CODENAME from /etc/os-release)
  32. }
  33. func distroNameLinux() string {
  34. return lazyVersionMeta.Get().DistroName
  35. }
  36. func distroVersionLinux() string {
  37. return lazyVersionMeta.Get().DistroVersion
  38. }
  39. func distroCodeNameLinux() string {
  40. return lazyVersionMeta.Get().DistroCodeName
  41. }
  42. func linuxDeviceModel() string {
  43. for _, path := range []string{
  44. // First try the Synology-specific location.
  45. // Example: "DS916+-j"
  46. "/proc/sys/kernel/syno_hw_version",
  47. // Otherwise, try the Devicetree model, usually set on
  48. // ARM SBCs, etc.
  49. // Example: "Raspberry Pi 4 Model B Rev 1.2"
  50. // Example: "WD My Cloud Gen2: Marvell Armada 375"
  51. "/sys/firmware/devicetree/base/model", // Raspberry Pi 4 Model B Rev 1.2"
  52. } {
  53. b, _ := os.ReadFile(path)
  54. if s := strings.Trim(string(b), "\x00\r\n\t "); s != "" {
  55. return s
  56. }
  57. }
  58. return ""
  59. }
  60. func getQnapQtsVersion(versionInfo string) string {
  61. for _, field := range strings.Fields(versionInfo) {
  62. if suffix, ok := strings.CutPrefix(field, "QTSFW_"); ok {
  63. return suffix
  64. }
  65. }
  66. return ""
  67. }
  68. func osVersionLinux() string {
  69. var un unix.Utsname
  70. unix.Uname(&un)
  71. return unix.ByteSliceToString(un.Release[:])
  72. }
  73. func linuxVersionMeta() (meta versionMeta) {
  74. dist := distro.Get()
  75. meta.DistroName = string(dist)
  76. propFile := "/etc/os-release"
  77. switch dist {
  78. case distro.Synology:
  79. propFile = "/etc.defaults/VERSION"
  80. case distro.OpenWrt:
  81. propFile = "/etc/openwrt_release"
  82. case distro.Unraid:
  83. propFile = "/etc/unraid-version"
  84. case distro.WDMyCloud:
  85. slurp, _ := os.ReadFile("/etc/version")
  86. meta.DistroVersion = string(bytes.TrimSpace(slurp))
  87. return
  88. case distro.QNAP:
  89. slurp, _ := os.ReadFile("/etc/version_info")
  90. meta.DistroVersion = getQnapQtsVersion(string(slurp))
  91. return
  92. }
  93. m := map[string]string{}
  94. lineread.File(propFile, func(line []byte) error {
  95. eq := bytes.IndexByte(line, '=')
  96. if eq == -1 {
  97. return nil
  98. }
  99. k, v := string(line[:eq]), strings.Trim(string(line[eq+1:]), `"'`)
  100. m[k] = v
  101. return nil
  102. })
  103. if v := m["VERSION_CODENAME"]; v != "" {
  104. meta.DistroCodeName = v
  105. }
  106. if v := m["VERSION_ID"]; v != "" {
  107. meta.DistroVersion = v
  108. }
  109. id := m["ID"]
  110. if id != "" {
  111. meta.DistroName = id
  112. }
  113. switch id {
  114. case "debian":
  115. // Debian's VERSION_ID is just like "11". But /etc/debian_version has "11.5" normally.
  116. // Or "bookworm/sid" on sid/testing.
  117. slurp, _ := os.ReadFile("/etc/debian_version")
  118. if v := string(bytes.TrimSpace(slurp)); v != "" {
  119. if '0' <= v[0] && v[0] <= '9' {
  120. meta.DistroVersion = v
  121. } else if meta.DistroCodeName == "" {
  122. meta.DistroCodeName = v
  123. }
  124. }
  125. case "", "centos": // CentOS 6 has no /etc/os-release, so its id is ""
  126. if meta.DistroVersion == "" {
  127. if cr, _ := os.ReadFile("/etc/centos-release"); len(cr) > 0 { // "CentOS release 6.10 (Final)
  128. meta.DistroVersion = string(bytes.TrimSpace(cr))
  129. }
  130. }
  131. }
  132. if v := m["PRETTY_NAME"]; v != "" && meta.DistroVersion == "" && !strings.HasSuffix(v, "/sid") {
  133. meta.DistroVersion = v
  134. }
  135. switch dist {
  136. case distro.Synology:
  137. meta.DistroVersion = m["productversion"]
  138. case distro.OpenWrt:
  139. meta.DistroVersion = m["DISTRIB_RELEASE"]
  140. case distro.Unraid:
  141. meta.DistroVersion = m["version"]
  142. }
  143. return
  144. }
  145. func packageTypeLinux() string {
  146. // Report whether this is in a snap.
  147. // See https://snapcraft.io/docs/environment-variables
  148. // We just look at two somewhat arbitrarily.
  149. if os.Getenv("SNAP_NAME") != "" && os.Getenv("SNAP") != "" {
  150. return "snap"
  151. }
  152. return ""
  153. }