Improve GetMountedPartitions function and added 'hidden_partitions' and 'hidden_filesystems' config options
This commit is contained in:
parent
ca17578cac
commit
47fd83f8cd
@ -4,4 +4,7 @@ ansii_colors: []
|
||||
force_config_ansii: false
|
||||
dependency_warning: true
|
||||
show_fs_type: true
|
||||
hidden_partitions: []
|
||||
# Hiding squashfs prevents snaps from showing up
|
||||
hidden_filesystems: ["squashfs"]
|
||||
hidden_gpus: []
|
||||
|
1
go.mod
1
go.mod
@ -6,7 +6,6 @@ require (
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a
|
||||
github.com/jackmordaunt/ghw v1.0.4
|
||||
github.com/mitchellh/go-ps v1.0.0
|
||||
github.com/moby/sys/mountinfo v0.7.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
|
3
go.sum
3
go.sum
@ -15,10 +15,7 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
|
||||
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
|
||||
github.com/moby/sys/mountinfo v0.7.1 h1:/tTvQaSJRr2FshkhXiIpux6fQ2Zvc4j7tAhMTStAG2g=
|
||||
github.com/moby/sys/mountinfo v0.7.1/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
25
main.go
25
main.go
@ -28,18 +28,21 @@ var config = StormfetchConfig{
|
||||
ForceConfigAnsii: false,
|
||||
DependencyWarning: true,
|
||||
ShowFSType: false,
|
||||
HiddenPartitions: make([]string, 0),
|
||||
HiddenGPUS: make([]int, 0),
|
||||
}
|
||||
|
||||
type StormfetchConfig struct {
|
||||
Ascii string `yaml:"distro_ascii"`
|
||||
DistroName string `yaml:"distro_name"`
|
||||
FetchScript string `yaml:"fetch_script"`
|
||||
AnsiiColors []int `yaml:"ansii_colors"`
|
||||
ForceConfigAnsii bool `yaml:"force_config_ansii"`
|
||||
DependencyWarning bool `yaml:"dependency_warning"`
|
||||
ShowFSType bool `yaml:"show_fs_type"`
|
||||
HiddenGPUS []int `yaml:"hidden_gpus"`
|
||||
Ascii string `yaml:"distro_ascii"`
|
||||
DistroName string `yaml:"distro_name"`
|
||||
FetchScript string `yaml:"fetch_script"`
|
||||
AnsiiColors []int `yaml:"ansii_colors"`
|
||||
ForceConfigAnsii bool `yaml:"force_config_ansii"`
|
||||
DependencyWarning bool `yaml:"dependency_warning"`
|
||||
ShowFSType bool `yaml:"show_fs_type"`
|
||||
HiddenPartitions []string `yaml:"hidden_partitions"`
|
||||
HiddenFilesystems []string `yaml:"hidden_filesystems"`
|
||||
HiddenGPUS []int `yaml:"hidden_gpus"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -143,7 +146,7 @@ func SetupFetchEnv(showTimeTaken bool) []string {
|
||||
env["MEM_FREE"] = strconv.Itoa(memory.MemAvailable)
|
||||
}
|
||||
start = time.Now().UnixMilli()
|
||||
partitions := getMountedPartitions()
|
||||
partitions := GetMountedPartitions(config.HiddenPartitions, config.HiddenFilesystems)
|
||||
end = time.Now().UnixMilli()
|
||||
if showTimeTaken {
|
||||
fmt.Println(fmt.Sprintf("Setting '%s' took %d milliseconds", "PARTITION_*", end-start))
|
||||
@ -156,8 +159,8 @@ func SetupFetchEnv(showTimeTaken bool) []string {
|
||||
if part.Label != "" {
|
||||
env["PARTITION"+strconv.Itoa(i+1)+"_LABEL"] = part.Label
|
||||
}
|
||||
if part.Type != "" && config.ShowFSType {
|
||||
env["PARTITION"+strconv.Itoa(i+1)+"_TYPE"] = part.Type
|
||||
if part.FileystemType != "" && config.ShowFSType {
|
||||
env["PARTITION"+strconv.Itoa(i+1)+"_TYPE"] = part.FileystemType
|
||||
}
|
||||
env["PARTITION"+strconv.Itoa(i+1)+"_TOTAL_SIZE"] = FormatBytes(part.TotalSize)
|
||||
env["PARTITION"+strconv.Itoa(i+1)+"_USED_SIZE"] = FormatBytes(part.UsedSize)
|
||||
|
119
partitions.go
Normal file
119
partitions.go
Normal file
@ -0,0 +1,119 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type partition struct {
|
||||
Device string
|
||||
MountPoint string
|
||||
Label string
|
||||
FileystemType string
|
||||
TotalSize uint64
|
||||
UsedSize uint64
|
||||
FreeSize uint64
|
||||
}
|
||||
|
||||
func GetMountedPartitions(hiddenPartitions, hiddenFilesystems []string) []partition {
|
||||
// Get all filesystem and partition labels
|
||||
fslabels, err := os.ReadDir("/dev/disk/by-label")
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
partlabels, err := os.ReadDir("/dev/disk/by-partlabel")
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
labels := make(map[string]string)
|
||||
for _, entry := range partlabels {
|
||||
link, err := filepath.EvalSymlinks(filepath.Join("/dev/disk/by-partlabel/", entry.Name()))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
labels[link] = entry.Name()
|
||||
}
|
||||
for _, entry := range fslabels {
|
||||
link, err := filepath.EvalSymlinks(filepath.Join("/dev/disk/by-label/", entry.Name()))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
labels[link] = entry.Name()
|
||||
}
|
||||
|
||||
// Get all mounted partitions
|
||||
file, err := os.ReadFile("/proc/mounts")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var partitions []partition
|
||||
for _, entry := range strings.Split(string(file), "\n") {
|
||||
fields := strings.Fields(entry)
|
||||
if entry == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip virtual partitions not under /dev
|
||||
if !strings.HasPrefix(fields[0], "/dev") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip partition if explicitly hidden
|
||||
if slices.Contains(hiddenPartitions, fields[0]) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip filesystem if explicitely hidden
|
||||
if slices.Contains(hiddenFilesystems, fields[2]) {
|
||||
continue
|
||||
}
|
||||
|
||||
p := partition{
|
||||
fields[0],
|
||||
fields[1],
|
||||
"",
|
||||
fields[2],
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
}
|
||||
|
||||
// Skip already added partitions
|
||||
skip := false
|
||||
for _, part := range partitions {
|
||||
if part.Device == p.Device {
|
||||
skip = true
|
||||
}
|
||||
}
|
||||
if skip {
|
||||
continue
|
||||
}
|
||||
|
||||
// Set partition label if available
|
||||
if value, ok := labels[p.Device]; ok {
|
||||
p.Label = value
|
||||
}
|
||||
|
||||
// Get partition total, used and free space
|
||||
buf := new(syscall.Statfs_t)
|
||||
err = syscall.Statfs(p.MountPoint, buf)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
totalBlocks := buf.Blocks
|
||||
freeBlocks := buf.Bfree
|
||||
usedBlocks := totalBlocks - freeBlocks
|
||||
blockSize := uint64(buf.Bsize)
|
||||
|
||||
p.TotalSize = totalBlocks * blockSize
|
||||
p.FreeSize = freeBlocks * blockSize
|
||||
p.UsedSize = usedBlocks * blockSize
|
||||
|
||||
partitions = append(partitions, p)
|
||||
}
|
||||
return partitions
|
||||
}
|
81
utils.go
81
utils.go
@ -6,7 +6,6 @@ import (
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/jackmordaunt/ghw"
|
||||
"github.com/mitchellh/go-ps"
|
||||
"github.com/moby/sys/mountinfo"
|
||||
"log"
|
||||
"math"
|
||||
"net"
|
||||
@ -18,7 +17,6 @@ import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type DistroInfo struct {
|
||||
@ -324,85 +322,6 @@ func getMonitorResolution() []string {
|
||||
return monitors
|
||||
}
|
||||
|
||||
type partition struct {
|
||||
Device string
|
||||
MountPoint string
|
||||
Label string
|
||||
Type string
|
||||
TotalSize uint64
|
||||
UsedSize uint64
|
||||
FreeSize uint64
|
||||
}
|
||||
|
||||
func getMountedPartitions() []partition {
|
||||
mounts, err := mountinfo.GetMounts(func(info *mountinfo.Info) (skip, stop bool) {
|
||||
return !strings.HasPrefix(info.Source, "/dev/"), false
|
||||
})
|
||||
fslabels, err := os.ReadDir("/dev/disk/by-label")
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
partlabels, err := os.ReadDir("/dev/disk/by-partlabel")
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
labels := make(map[string]string)
|
||||
for _, entry := range partlabels {
|
||||
link, err := filepath.EvalSymlinks(filepath.Join("/dev/disk/by-partlabel/", entry.Name()))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
labels[link] = entry.Name()
|
||||
}
|
||||
for _, entry := range fslabels {
|
||||
link, err := filepath.EvalSymlinks(filepath.Join("/dev/disk/by-label/", entry.Name()))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
labels[link] = entry.Name()
|
||||
}
|
||||
var partitions []partition
|
||||
for _, entry := range mounts {
|
||||
p := partition{
|
||||
entry.Source,
|
||||
entry.Mountpoint,
|
||||
"",
|
||||
entry.FSType,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
}
|
||||
skip := false
|
||||
for _, part := range partitions {
|
||||
if part.Device == p.Device {
|
||||
skip = true
|
||||
}
|
||||
}
|
||||
if skip {
|
||||
continue
|
||||
}
|
||||
if value, ok := labels[entry.Source]; ok {
|
||||
p.Label = value
|
||||
}
|
||||
buf := new(syscall.Statfs_t)
|
||||
err = syscall.Statfs(p.MountPoint, buf)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
totalBlocks := buf.Blocks
|
||||
freeBlocks := buf.Bfree
|
||||
usedBlocks := totalBlocks - freeBlocks
|
||||
blockSize := uint64(buf.Bsize)
|
||||
|
||||
p.TotalSize = totalBlocks * blockSize
|
||||
p.FreeSize = freeBlocks * blockSize
|
||||
p.UsedSize = usedBlocks * blockSize
|
||||
|
||||
partitions = append(partitions, p)
|
||||
}
|
||||
return partitions
|
||||
}
|
||||
|
||||
func GetInitSystem() string {
|
||||
runCommand := func(command string) string {
|
||||
cmd := exec.Command("/bin/bash", "-c", command)
|
||||
|
Loading…
x
Reference in New Issue
Block a user