/
kernel.go
84 lines (67 loc) · 1.81 KB
/
kernel.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package util
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/canonical/lxd/shared"
)
// LoadModule loads the kernel module with the given name, by invoking
// modprobe. This respects any modprobe configuration on the system.
func LoadModule(module string) error {
if shared.PathExists(fmt.Sprintf("/sys/module/%s", module)) {
return nil
}
_, err := shared.RunCommand("modprobe", "-b", module)
return err
}
// SupportsFilesystem checks whether a given filesystem is already supported
// by the kernel. Note that if the filesystem is a module, you may need to
// load it first.
func SupportsFilesystem(filesystem string) bool {
file, err := os.Open("/proc/filesystems")
if err != nil {
return false
}
defer func() { _ = file.Close() }()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fields := strings.Fields(strings.TrimSpace(scanner.Text()))
entry := fields[len(fields)-1]
if entry == filesystem {
return true
}
}
return false
}
// HugepagesPath attempts to locate the mount point of the hugepages filesystem.
func HugepagesPath() (string, error) {
// Find the source mount of the path
file, err := os.Open("/proc/mounts")
if err != nil {
return "", err
}
defer func() { _ = file.Close() }()
scanner := bufio.NewScanner(file)
matches := []string{}
for scanner.Scan() {
line := scanner.Text()
cols := strings.Fields(line)
if len(cols) < 3 {
continue
}
if cols[2] == "hugetlbfs" {
matches = append(matches, cols[1])
}
}
if len(matches) == 0 {
return "", fmt.Errorf("No hugetlbfs mount found, can't use hugepages")
}
if len(matches) > 1 {
if shared.ValueInSlice("/dev/hugepages", matches) {
return "/dev/hugepages", nil
}
return "", fmt.Errorf("More than one hugetlbfs instance found and none at standard /dev/hugepages")
}
return matches[0], nil
}