Skip to content

Commit

Permalink
Merge pull request #1270 from shirou/feature/disk_add_fallback_to_sel…
Browse files Browse the repository at this point in the history
…f_mountinfo

[disk][linux] add fallback to /proc/self
  • Loading branch information
shirou authored Mar 8, 2022
2 parents 7de7d48 + 49037dd commit 4b988f3
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions disk/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -221,20 +222,32 @@ var fsTypeMap = map[int64]string{
ZFS_SUPER_MAGIC: "zfs", /* 0x2FC12FC1 local */
}

func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
useMounts := false

filename := common.HostProc("1/mountinfo")
lines, err := common.ReadLines(filename)
// readMountFile reads mountinfo or mounts file under /proc/1 or /proc/self
func readMountFile(root string) (lines []string, useMounts bool, filename string, err error) {
filename = common.HostProc(path.Join(root, "mountinfo"))
lines, err = common.ReadLines(filename)
if err != nil {
var pathErr *os.PathError
if !errors.As(err, &pathErr) {
return nil, err
return
}
// if kernel does not support 1/mountinfo, fallback to 1/mounts (<2.6.26)
useMounts = true
filename = common.HostProc("1/mounts")
filename = common.HostProc(path.Join(root, "mounts"))
lines, err = common.ReadLines(filename)
if err != nil {
return
}
return
}
return
}

func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
lines, useMounts, filename, err := readMountFile("1")
if err != nil {
// fallback to "/proc/self/mountinfo" #1159
lines, useMounts, filename, err = readMountFile("self")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4b988f3

Please sign in to comment.