Skip to content

Commit 9793a63

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

File tree

6 files changed

+76
-1
lines changed

6 files changed

+76
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ephem
2+
3+
import (
4+
"encoding/json"
5+
"regexp"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/moby/sys/mountinfo"
10+
)
11+
12+
var specialFsRegex = regexp.MustCompile(`^(/proc|/dev|/sys|/run|/var/lib/docker|/var/lib/nfs/rpc_pipefs).*`)
13+
14+
// MountInfo represents the information about a mount point.
15+
// It is just a type alias for mountinfo.Info to allow us to add JSON marshalling.
16+
type MountInfo struct {
17+
mountinfo.Info
18+
}
19+
20+
func (i MountInfo) MarshalJSON() ([]byte, error) {
21+
return json.Marshal(map[string]string{
22+
"id": strconv.Itoa(i.ID),
23+
"parent_id": strconv.Itoa(i.Parent),
24+
"major": strconv.Itoa(i.Major),
25+
"minor": strconv.Itoa(i.Minor),
26+
"root": i.Root,
27+
"mount_point": i.Mountpoint,
28+
"mount_options": i.Options,
29+
"optional": i.Optional,
30+
"filesystem_type": i.FSType,
31+
"mount_source": i.Source,
32+
"super_options": i.VFSOptions,
33+
})
34+
}
35+
36+
func Mounts() ([]*MountInfo, error) {
37+
info, err := mountinfo.GetMounts(mountFilter)
38+
if err != nil {
39+
return nil, err
40+
}
41+
var result []*MountInfo
42+
for idx := range info {
43+
result = append(result, &MountInfo{Info: *info[idx]})
44+
}
45+
return result, nil
46+
}
47+
48+
func mountFilter(info *mountinfo.Info) (skip, stop bool) {
49+
if skip = specialFsRegex.MatchString(info.Mountpoint); skip {
50+
return true, false
51+
}
52+
53+
// skip the read-only bind-mount on /nix/store
54+
// https://github.com/NixOS/nixpkgs/blob/dac9cdf8c930c0af98a63cbfe8005546ba0125fb/nixos/modules/installer/tools/nixos-generate-config.pl#L395
55+
if info.Mountpoint == "/nix/store" && strings.Contains(info.VFSOptions, "rw") && strings.Contains(info.Options, "ro") {
56+
return true, false
57+
}
58+
59+
return false, false
60+
}

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)