Skip to content

Mount options does not split by comma #1858

@NitroCao

Description

@NitroCao

Describe the bug
In PartitionsWithContext(), the value ofPartitionStat.Opts doesn't use , when splition. So the options are in one string. As contrast, mount options are split when using /proc/1/mountinfo.

gopsutil/disk/disk_linux.go

Lines 292 to 325 in 34ac457

if useMounts {
fields := strings.Fields(line)
d = PartitionStat{
Device: fields[0],
Mountpoint: unescapeFstab(fields[1]),
Fstype: fields[2],
Opts: strings.Fields(fields[3]),
}
if !all {
if d.Device == "none" || !common.StringsHas(fs, d.Fstype) {
continue
}
}
} else {
// a line of 1/mountinfo has the following structure:
// 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
// (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
// split the mountinfo line by the separator hyphen
parts := strings.Split(line, " - ")
if len(parts) != 2 {
return nil, fmt.Errorf("found invalid mountinfo line in file %s: %s ", filename, line)
}
fields := strings.Fields(parts[0])
blockDeviceID := fields[2]
mountPoint := fields[4]
mountOpts := strings.Split(fields[5], ",")
if rootDir := fields[3]; rootDir != "" && rootDir != "/" {
mountOpts = append(mountOpts, "bind")
}

To Reproduce

package main

import (
	"fmt"

	"github.com/shirou/gopsutil/v4/disk"
)

func main() {
	partitions, err := disk.Partitions(true)
	if err != nil {
		log.Fatalln(err)
	}
	for _, each := range partitions {
		fmt.Printf("+%v\n", each)
	}
}

Expected behavior
Mount options of each mountpoint should be split by ,.

Environment (please complete the following information):

  • Windows: [paste the result of ver]
  • Linux:
  • Mac OS: [paste the result of sw_vers and uname -a
  • FreeBSD: [paste the result of freebsd-version -k -r -u and uname -a]
  • OpenBSD: [paste the result of uname -a]

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions