[disk][linux] add fallback to /proc/self

This commit is contained in:
shirou 2022-03-05 03:15:26 +00:00
parent 7de7d48ef6
commit 49037dd0d8
1 changed files with 20 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -221,20 +222,32 @@ var fsTypeMap = map[int64]string{
ZFS_SUPER_MAGIC: "zfs", /* 0x2FC12FC1 local */ ZFS_SUPER_MAGIC: "zfs", /* 0x2FC12FC1 local */
} }
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { // readMountFile reads mountinfo or mounts file under /proc/1 or /proc/self
useMounts := false func readMountFile(root string) (lines []string, useMounts bool, filename string, err error) {
filename = common.HostProc(path.Join(root, "mountinfo"))
filename := common.HostProc("1/mountinfo") lines, err = common.ReadLines(filename)
lines, err := common.ReadLines(filename)
if err != nil { if err != nil {
var pathErr *os.PathError var pathErr *os.PathError
if !errors.As(err, &pathErr) { if !errors.As(err, &pathErr) {
return nil, err return
} }
// if kernel does not support 1/mountinfo, fallback to 1/mounts (<2.6.26) // if kernel does not support 1/mountinfo, fallback to 1/mounts (<2.6.26)
useMounts = true useMounts = true
filename = common.HostProc("1/mounts") filename = common.HostProc(path.Join(root, "mounts"))
lines, err = common.ReadLines(filename) 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 { if err != nil {
return nil, err return nil, err
} }