Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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