Skip to content

Commit cf63093

Browse files
authored
Merge pull request #1272 from jdstrand/jdstrand/add-env-force-workaround
[disk][linux] add HOST_PROC_MOUNTINFO
2 parents 78577a7 + 9e6e627 commit cf63093

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ environment variable.
9090
You can set an alternative location to `/dev` by setting the `HOST_DEV`
9191
environment variable.
9292

93+
You can set an alternative location to `/proc/N/mountinfo` by setting the
94+
`HOST_PROC_MOUNTINFO` environment variable.
95+
9396
## Documentation
9497

9598
see http://godoc.org/github.com/shirou/gopsutil

disk/disk_linux.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ var fsTypeMap = map[int64]string{
235235
ZFS_SUPER_MAGIC: "zfs", /* 0x2FC12FC1 local */
236236
}
237237

238-
// readMountFile reads mountinfo or mounts file under /proc/1 or /proc/self
238+
// readMountFile reads mountinfo or mounts file under the specified root path
239+
// (eg, /proc/1, /proc/self, etc)
239240
func readMountFile(root string) (lines []string, useMounts bool, filename string, err error) {
240-
filename = common.HostProc(path.Join(root, "mountinfo"))
241+
filename = path.Join(root, "mountinfo")
241242
lines, err = common.ReadLines(filename)
242243
if err != nil {
243244
var pathErr *os.PathError
@@ -246,7 +247,7 @@ func readMountFile(root string) (lines []string, useMounts bool, filename string
246247
}
247248
// if kernel does not support 1/mountinfo, fallback to 1/mounts (<2.6.26)
248249
useMounts = true
249-
filename = common.HostProc(path.Join(root, "mounts"))
250+
filename = path.Join(root, "mounts")
250251
lines, err = common.ReadLines(filename)
251252
if err != nil {
252253
return
@@ -257,10 +258,22 @@ func readMountFile(root string) (lines []string, useMounts bool, filename string
257258
}
258259

259260
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
260-
lines, useMounts, filename, err := readMountFile("1")
261+
// by default, try "/proc/1/..." first
262+
root := common.HostProc(path.Join("1"))
263+
264+
// force preference for dirname of HOST_PROC_MOUNTINFO, if set #1271
265+
hpmPath := os.Getenv("HOST_PROC_MOUNTINFO")
266+
if hpmPath != "" {
267+
root = filepath.Dir(hpmPath)
268+
}
269+
270+
lines, useMounts, filename, err := readMountFile(root)
261271
if err != nil {
262-
// fallback to "/proc/self/mountinfo" #1159
263-
lines, useMounts, filename, err = readMountFile("self")
272+
if hpmPath != "" { // don't fallback with HOST_PROC_MOUNTINFO
273+
return nil, err
274+
}
275+
// fallback to "/proc/self/..." #1159
276+
lines, useMounts, filename, err = readMountFile(common.HostProc(path.Join("self")))
264277
if err != nil {
265278
return nil, err
266279
}

0 commit comments

Comments
 (0)