forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mountinfo.go
159 lines (135 loc) · 4.51 KB
/
mountinfo.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2018 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mountinfo
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
const (
// FilesystemType names for filesystem which are used in /proc/pid/mountinfo
FilesystemTypeBPFFS = "bpf"
FilesystemTypeCgroup2 = "cgroup2"
mountInfoFilepath = "/proc/self/mountinfo"
)
// MountInfo is a struct representing information from /proc/pid/mountinfo. More
// information about file syntax:
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
type MountInfo struct {
MountID int64
ParentID int64
StDev string
Root string
MountPoint string
MountOptions string
OptionalFields []string
FilesystemType string
MountSource string
SuperOptions string
}
// parseMountInfoFile returns a slice of *MountInfo with information parsed from
// the given reader
func parseMountInfoFile(r io.Reader) ([]*MountInfo, error) {
var result []*MountInfo
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
mountInfoRaw := scanner.Text()
// Optional fields (which are on the 7th position) are separated
// from the rest of fields by "-" character. The number of
// optional fields can be greater or equal to 1.
mountInfoSeparated := strings.Split(mountInfoRaw, " - ")
if len(mountInfoSeparated) != 2 {
return nil, fmt.Errorf("invalid mountinfo entry which has more that one '-' separator: %s", mountInfoRaw)
}
// Extract fields from both sides of mountinfo
mountInfoLeft := strings.Split(strings.TrimSpace(mountInfoSeparated[0]), " ")
mountInfoRight := strings.Split(strings.TrimSpace(mountInfoSeparated[1]), " ")
// Before '-' separator there should be 6 fields and unknown
// number of optional fields
if len(mountInfoLeft) < 6 {
return nil, fmt.Errorf("invalid mountinfo entry: %s", mountInfoRaw)
}
// After '-' separator there should be 3 fields
if len(mountInfoRight) != 3 {
return nil, fmt.Errorf("invalid mountinfo entry: %s", mountInfoRaw)
}
mountID, err := strconv.ParseInt(mountInfoLeft[0], 10, 64)
if err != nil {
return nil, err
}
parentID, err := strconv.ParseInt(mountInfoLeft[1], 10, 64)
if err != nil {
return nil, err
}
// Extract optional fields, which start from 7th position
var optionalFields []string
for i := 6; i < len(mountInfoLeft); i++ {
optionalFields = append(optionalFields, mountInfoLeft[i])
}
result = append(result, &MountInfo{
MountID: mountID,
ParentID: parentID,
StDev: mountInfoLeft[2],
Root: mountInfoLeft[3],
MountPoint: mountInfoLeft[4],
MountOptions: mountInfoLeft[5],
OptionalFields: optionalFields,
FilesystemType: mountInfoRight[0],
MountSource: mountInfoRight[1],
SuperOptions: mountInfoRight[2],
})
}
return result, nil
}
// GetMountInfo returns a slice of *MountInfo with information parsed from
// /proc/self/mountinfo
func GetMountInfo() ([]*MountInfo, error) {
fMounts, err := os.Open(mountInfoFilepath)
if err != nil {
return nil, fmt.Errorf("failed to open mount information at %s: %s", mountInfoFilepath, err)
}
defer fMounts.Close()
return parseMountInfoFile(fMounts)
}
func isMountFS(mountInfos []*MountInfo, mntType string, mapRoot string) (bool, bool) {
var mapRootMountInfo *MountInfo
for _, mountInfo := range mountInfos {
if mountInfo.MountPoint == mapRoot {
mapRootMountInfo = mountInfo
break
}
}
if mapRootMountInfo == nil {
return false, false
}
if mapRootMountInfo.FilesystemType == mntType {
return true, true
}
return true, false
}
// IsMountFS returns two boolean values:checks whether the current mapRoot:
// - whether the current mapRoot has any mount
// - whether that mount's filesystem is of type mntType
func IsMountFS(mntType string, mapRoot string) (bool, bool, error) {
mountInfos, err := GetMountInfo()
if err != nil {
return false, false, err
}
mounted, mntTypeInstance := isMountFS(mountInfos, mntType, mapRoot)
return mounted, mntTypeInstance, nil
}