74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package system
|
|
|
|
import (
|
|
"syscall"
|
|
|
|
"nadir/internal/mounts"
|
|
)
|
|
|
|
type DiskInfo struct {
|
|
Mountpoint string `json:"mountpoint" example:"/"`
|
|
Filesystem string `json:"filesystem" example:"/dev/nvme0n1p2" doc:"Backing device"`
|
|
FSType string `json:"fstype" example:"btrfs"`
|
|
TotalBytes uint64 `json:"total_bytes" example:"512000000000"`
|
|
FreeBytes uint64 `json:"free_bytes" example:"256000000000" doc:"Space available to unprivileged users"`
|
|
UsedBytes uint64 `json:"used_bytes" example:"256000000000"`
|
|
}
|
|
|
|
func diskInfo() []DiskInfo {
|
|
entries, err := mounts.Proc()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
disks := []DiskInfo{}
|
|
seen := map[string]bool{}
|
|
for _, e := range entries {
|
|
if pseudoFS[e.FSType] || seen[e.Mountpoint] {
|
|
continue
|
|
}
|
|
var st syscall.Statfs_t
|
|
if syscall.Statfs(e.Mountpoint, &st) != nil || st.Blocks == 0 {
|
|
continue
|
|
}
|
|
seen[e.Mountpoint] = true
|
|
bs := uint64(st.Bsize)
|
|
disks = append(disks, DiskInfo{
|
|
Mountpoint: e.Mountpoint,
|
|
Filesystem: e.Device,
|
|
FSType: e.FSType,
|
|
TotalBytes: st.Blocks * bs,
|
|
FreeBytes: st.Bavail * bs,
|
|
UsedBytes: (st.Blocks - st.Bfree) * bs,
|
|
})
|
|
}
|
|
return disks
|
|
}
|
|
|
|
var pseudoFS = map[string]bool{
|
|
"autofs": true,
|
|
"binfmt_misc": true,
|
|
"bpf": true,
|
|
"cgroup": true,
|
|
"cgroup2": true,
|
|
"configfs": true,
|
|
"debugfs": true,
|
|
"devpts": true,
|
|
"devtmpfs": true,
|
|
"fuse.gvfsd-fuse": true,
|
|
"fuse.lxcfs": true,
|
|
"fusectl": true,
|
|
"hugetlbfs": true,
|
|
"mqueue": true,
|
|
"nsfs": true,
|
|
"overlay": true,
|
|
"proc": true,
|
|
"pstore": true,
|
|
"ramfs": true,
|
|
"rpc_pipefs": true,
|
|
"securityfs": true,
|
|
"squashfs": true,
|
|
"sysfs": true,
|
|
"tmpfs": true,
|
|
"tracefs": true,
|
|
}
|