Skip to content

Commit 638c94e

Browse files
committed
wip: capture filesystems
Signed-off-by: Brian McGee <[email protected]>
1 parent 090246f commit 638c94e

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ func init() {
8686

8787
// Options for optional ephemeral system properties.
8888
f.BoolVarP(&scanner.Swap, "swap", "s", false, "capture swap entries")
89-
f.BoolVarP(&scanner.Ephemeral, "ephemeral", "e", false, "capture all ephemeral properties e.g. swap, filesystems and so on")
89+
f.BoolVarP(&scanner.Mounts, "mounts", "m", false, "capture filesystem mounts")
90+
f.BoolVarP(&scanner.Ephemeral, "ephemeral", "e", false, "capture all ephemeral properties e.g. swap, mounts and so on")
9091

9192
// We currently support all probe features at a high level as they share some generic information,
9293
// but we do not have mappings for all of their detail sections.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/mattn/go-isatty v0.0.18 // indirect
2424
github.com/mattn/go-runewidth v0.0.15 // indirect
2525
github.com/mitchellh/mapstructure v1.5.0 // indirect
26+
github.com/moby/sys/mountinfo v0.7.2 // indirect
2627
github.com/muesli/reflow v0.3.0 // indirect
2728
github.com/muesli/termenv v0.15.2 // indirect
2829
github.com/pelletier/go-toml/v2 v2.2.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
3838
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
3939
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
4040
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
41+
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
42+
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
4143
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
4244
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
4345
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=

nix/packages/nixos-facter/gomod2nix.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ schema = 3
4343
[mod."github.com/mitchellh/mapstructure"]
4444
version = "v1.5.0"
4545
hash = "sha256-ztVhGQXs67MF8UadVvG72G3ly0ypQW0IRDdOOkjYwoE="
46+
[mod."github.com/moby/sys/mountinfo"]
47+
version = "v0.7.2"
48+
hash = "sha256-G0K/wHnhvJ+00gjjtvScvnttQYxPaDru5HniwBM0+vA="
4649
[mod."github.com/muesli/reflow"]
4750
version = "v0.3.0"
4851
hash = "sha256-Pou2ybE9SFSZG6YfZLVV1Eyfm+X4FuVpDPLxhpn47Cc="

pkg/ephem/fs.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ephem
2+
3+
import (
4+
"encoding/json"
5+
"regexp"
6+
"strconv"
7+
8+
"github.com/moby/sys/mountinfo"
9+
)
10+
11+
var specialFsRegex = regexp.MustCompile(`^(/proc|/dev|/sys|/run|/nix/store|/var/lib/docker|/var/lib/nfs/rpc_pipefs).*`)
12+
13+
// MountInfo represents the information about a mount point.
14+
// It is just a type alias for mountinfo.Info to allow us to add JSON marshalling.
15+
type MountInfo struct {
16+
mountinfo.Info
17+
}
18+
19+
func (i MountInfo) MarshalJSON() ([]byte, error) {
20+
return json.Marshal(map[string]string{
21+
"id": strconv.Itoa(i.ID),
22+
"parent": strconv.Itoa(i.Parent),
23+
"major": strconv.Itoa(i.Major),
24+
"minor": strconv.Itoa(i.Minor),
25+
"root": i.Root,
26+
"mountpoint": i.Mountpoint,
27+
"options": i.Options,
28+
"optional": i.Optional,
29+
"fstype": i.FSType,
30+
"source": i.Source,
31+
"vfs_options": i.VFSOptions,
32+
})
33+
}
34+
35+
func Mounts() ([]*MountInfo, error) {
36+
info, err := mountinfo.GetMounts(mountFilter)
37+
if err != nil {
38+
return nil, err
39+
}
40+
var result []*MountInfo
41+
for idx := range info {
42+
result = append(result, &MountInfo{Info: *info[idx]})
43+
}
44+
return result, nil
45+
}
46+
47+
func mountFilter(info *mountinfo.Info) (skip, stop bool) {
48+
return specialFsRegex.MatchString(info.Mountpoint), false
49+
}

pkg/facter/report.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
type Report struct {
1414
Hardware []*hwinfo.HardwareItem `json:"hardware"`
15+
Mounts []*ephem.MountInfo `json:"mounts,omitempty"`
1516
Smbios []hwinfo.Smbios `json:"smbios,omitempty"`
1617
Swap []*ephem.SwapEntry `json:"swap,omitempty"`
1718
System string `json:"system"`
@@ -20,6 +21,7 @@ type Report struct {
2021

2122
type Scanner struct {
2223
Swap bool
24+
Mounts bool
2325
Ephemeral bool
2426
Features []hwinfo.ProbeFeature
2527
}
@@ -48,5 +50,11 @@ func (s *Scanner) Scan() (*Report, error) {
4850
}
4951
}
5052

53+
if s.Ephemeral || s.Mounts {
54+
if report.Mounts, err = ephem.Mounts(); err != nil {
55+
return nil, fmt.Errorf("failed to detect mounts: %w", err)
56+
}
57+
}
58+
5159
return &report, nil
5260
}

0 commit comments

Comments
 (0)