forked from longhorn/longhorn-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos.go
46 lines (39 loc) · 1.31 KB
/
os.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package util
import (
"bufio"
"fmt"
"strings"
"github.com/pkg/errors"
iscsiutil "github.com/longhorn/go-iscsi-helper/util"
)
const (
OsReleasePath = "/etc/os-release"
)
// GetHostKernelRelease retrieves the kernel release version of the host.
func GetHostKernelRelease() (string, error) {
initiatorNSPath := iscsiutil.GetHostNamespacePath(HostProcPath)
mountPath := fmt.Sprintf("--mount=%s/mnt", initiatorNSPath)
output, err := Execute([]string{}, "nsenter", mountPath, "uname", "-r")
if err != nil {
return "", err
}
return RemoveNewlines(output), nil
}
// GetHostOSDistro retrieves the operating system distribution of the host.
func GetHostOSDistro() (string, error) {
initiatorNSPath := iscsiutil.GetHostNamespacePath(HostProcPath)
mountPath := fmt.Sprintf("--mount=%s/mnt", initiatorNSPath)
output, err := Execute([]string{}, "nsenter", mountPath, "cat", OsReleasePath)
if err != nil {
return "", errors.Wrapf(err, "failed to read %v on host", OsReleasePath)
}
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "ID=") {
osDistro := RemoveNewlines(strings.TrimPrefix(line, "ID="))
return strings.ReplaceAll(osDistro, `"`, ""), nil
}
}
return "", fmt.Errorf("failed to find ID field in %v", OsReleasePath)
}