fix: disk.Partitions改为false防止未知分区 / 添加zfs去重避免重复计算磁盘空间

This commit is contained in:
xxxizum1kxxx
2025-11-30 13:47:46 +08:00
parent e8835d135a
commit 1547184903

View File

@@ -14,7 +14,7 @@ type DiskInfo struct {
func Disk() DiskInfo {
diskinfo := DiskInfo{}
usage, err := disk.Partitions(true)
usage, err := disk.Partitions(false)
if err != nil {
diskinfo.Total = 0
diskinfo.Used = 0
@@ -36,17 +36,39 @@ func Disk() DiskInfo {
}
} else {
// 使用默认逻辑,排除临时文件系统和网络驱动器
deviceMap := make(map[string]*disk.UsageStat)
for _, part := range usage {
if isPhysicalDisk(part) {
u, err := disk.Usage(part.Mountpoint)
if err != nil {
continue
}
deviceID := part.Device
// ZFS去重: 基于 pool 名称 (例如 pool/dataset -> pool)
if strings.ToLower(part.Fstype) == "zfs" {
if idx := strings.Index(deviceID, "/"); idx != -1 {
deviceID = deviceID[:idx]
}
}
// 如果该设备已存在,且当前挂载点的 Total 更大,则替换(处理 quota 等情况)
// 否则保留现有的(通常我们希望统计物理 pool 的总量)
if existing, ok := deviceMap[deviceID]; ok {
if u.Total > existing.Total {
deviceMap[deviceID] = u
}
} else {
diskinfo.Total += u.Total
diskinfo.Used += u.Used
deviceMap[deviceID] = u
}
}
}
for _, u := range deviceMap {
diskinfo.Total += u.Total
diskinfo.Used += u.Used
}
}
}
return diskinfo