|
| 1 | +// Package osinfo collects the host operating system information. |
| 2 | +package osinfo |
| 3 | + |
| 4 | +import ( |
| 5 | + "bufio" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// Paths to the files describing the OS release. They are package variables so tests can override them. |
| 12 | +var ( |
| 13 | + osReleasePaths = []string{"/etc/os-release", "/usr/lib/os-release"} |
| 14 | + debianVersionPath = "/etc/debian_version" |
| 15 | +) |
| 16 | + |
| 17 | +// PrettyName returns a human-readable OS name and version derived from os-release, or an empty string |
| 18 | +// if it cannot be determined. For example, "Ubuntu 24.04.4 LTS" or "Debian 13.5". |
| 19 | +func PrettyName() string { |
| 20 | + rel := readOSRelease() |
| 21 | + if len(rel) == 0 { |
| 22 | + return "" |
| 23 | + } |
| 24 | + |
| 25 | + // Debian's os-release only carries the major version (e.g. "13"), while /etc/debian_version |
| 26 | + // holds the point release (e.g. "13.5"). Read it to report the precise version. |
| 27 | + var debianVersion string |
| 28 | + if rel["ID"] == "debian" { |
| 29 | + if data, err := os.ReadFile(debianVersionPath); err == nil { |
| 30 | + debianVersion = strings.TrimSpace(string(data)) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return buildPrettyName(rel, debianVersion) |
| 35 | +} |
| 36 | + |
| 37 | +// readOSRelease reads the first available os-release file and parses it into a key-value map. It |
| 38 | +// returns an empty map if no file is found. |
| 39 | +func readOSRelease() map[string]string { |
| 40 | + for _, path := range osReleasePaths { |
| 41 | + f, err := os.Open(path) |
| 42 | + if err != nil { |
| 43 | + continue |
| 44 | + } |
| 45 | + rel := parseOSRelease(f) |
| 46 | + f.Close() |
| 47 | + return rel |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +// parseOSRelease parses an os-release file into a key-value map. Blank lines and comments are |
| 53 | +// skipped, and surrounding quotes are stripped from values. |
| 54 | +func parseOSRelease(r io.Reader) map[string]string { |
| 55 | + rel := make(map[string]string) |
| 56 | + scanner := bufio.NewScanner(r) |
| 57 | + for scanner.Scan() { |
| 58 | + line := strings.TrimSpace(scanner.Text()) |
| 59 | + if line == "" || strings.HasPrefix(line, "#") { |
| 60 | + continue |
| 61 | + } |
| 62 | + key, value, ok := strings.Cut(line, "=") |
| 63 | + if !ok { |
| 64 | + continue |
| 65 | + } |
| 66 | + key = strings.TrimSpace(key) |
| 67 | + value = strings.Trim(strings.TrimSpace(value), `"'`) |
| 68 | + rel[key] = value |
| 69 | + } |
| 70 | + return rel |
| 71 | +} |
| 72 | + |
| 73 | +// buildPrettyName composes a human-readable OS name and version from the parsed os-release map and the |
| 74 | +// contents of /etc/debian_version (empty for non-Debian systems). |
| 75 | +func buildPrettyName(rel map[string]string, debianVersion string) string { |
| 76 | + // Debian's PRETTY_NAME is verbose ("Debian GNU/Linux 13 (trixie)") and lacks the point release, |
| 77 | + // so prefer the precise version or codename (unstable releases) from /etc/debian_version. |
| 78 | + if rel["ID"] == "debian" { |
| 79 | + return strings.Join([]string{"Debian", debianVersion}, " ") |
| 80 | + } |
| 81 | + |
| 82 | + // PRETTY_NAME is the vendor's human-readable string and already includes the point release on |
| 83 | + // most distros (e.g. "Ubuntu 24.04.4 LTS"). |
| 84 | + if pretty := rel["PRETTY_NAME"]; pretty != "" { |
| 85 | + return pretty |
| 86 | + } |
| 87 | + |
| 88 | + // Fall back to composing the name from the individual fields. |
| 89 | + name := rel["NAME"] |
| 90 | + if name == "" { |
| 91 | + name = rel["ID"] |
| 92 | + } |
| 93 | + parts := make([]string, 0, 3) |
| 94 | + if name != "" { |
| 95 | + parts = append(parts, name) |
| 96 | + } |
| 97 | + if version := rel["VERSION_ID"]; version != "" { |
| 98 | + parts = append(parts, version) |
| 99 | + } |
| 100 | + if codename := rel["VERSION_CODENAME"]; codename != "" { |
| 101 | + parts = append(parts, "("+codename+")") |
| 102 | + } |
| 103 | + return strings.Join(parts, " ") |
| 104 | +} |
0 commit comments