Skip to content

Commit 128a9ad

Browse files
committed
Refactor mountinfo parsing
Signed-off-by: Ben Kochie <superq@gmail.com>
1 parent ef5efb7 commit 128a9ad

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

collector/filesystem_linux.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131

3232
"github.com/alecthomas/kingpin/v2"
3333
"golang.org/x/sys/unix"
34+
35+
"github.com/prometheus/procfs"
3436
)
3537

3638
const (
@@ -179,25 +181,27 @@ func stuckMountWatcher(mountPoint string, success chan struct{}, logger *slog.Lo
179181
}
180182

181183
func mountPointDetails(logger *slog.Logger) ([]filesystemLabels, error) {
182-
file, err := os.Open(procFilePath("1/mountinfo"))
184+
fs, err := procfs.NewFS(*procPath)
185+
if err != nil {
186+
return nil, fmt.Errorf("failed to open procfs: %w", err)
187+
}
188+
mountInfo, err := fs.GetProcMounts(1)
183189
if errors.Is(err, os.ErrNotExist) {
184190
// Fallback to `/proc/self/mountinfo` if `/proc/1/mountinfo` is missing due hidepid.
185191
logger.Debug("Reading root mounts failed, falling back to self mounts", "err", err)
186-
file, err = os.Open(procFilePath("self/mountinfo"))
192+
mountInfo, err = fs.GetMounts()
187193
}
188194
if err != nil {
189195
return nil, err
190196
}
191-
defer file.Close()
192197

193198
return parseFilesystemLabels(file)
194199
}
195200

196-
func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) {
201+
func parseFilesystemLabels(mountInfo []*procfs.MountInfo) ([]filesystemLabels, error) {
197202
var filesystems []filesystemLabels
198203

199-
scanner := bufio.NewScanner(r)
200-
for scanner.Scan() {
204+
for mount in range mountInfo {
201205
parts := strings.Fields(scanner.Text())
202206

203207
if len(parts) < 11 {

0 commit comments

Comments
 (0)