123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- package diskinfo
- import (
- "errors"
- "path/filepath"
- "strings"
- "log"
- "imuslab.com/bokofs/bokofsd/mod/diskinfo/blkid"
- "imuslab.com/bokofs/bokofsd/mod/diskinfo/df"
- "imuslab.com/bokofs/bokofsd/mod/diskinfo/fdisk"
- "imuslab.com/bokofs/bokofsd/mod/diskinfo/lsblk"
- )
- func GetAllDisks() ([]*Disk, error) {
- allBlockDevices, err := lsblk.GetLSBLKOutput()
- if err != nil {
- return nil, err
- }
- disks := []*Disk{}
- for _, blockDevice := range allBlockDevices {
- if blockDevice.Type == "disk" {
- thisDisk, err := GetDiskInfo(blockDevice.Name)
- if err != nil {
- return nil, err
- }
- disks = append(disks, thisDisk)
- }
- }
- return disks, nil
- }
- func DevicePathIsValidDisk(path string) bool {
-
- if !strings.HasPrefix(path, "/dev/") {
- path = "/dev/" + path
- }
- path = strings.TrimSuffix(path, "/")
- allBlockDevices, err := lsblk.GetLSBLKOutput()
- if err != nil {
- log.Println("Error getting block devices:", err)
- return false
- }
- for _, blockDevice := range allBlockDevices {
- if "/dev/"+blockDevice.Name == path {
- return blockDevice.Type == "disk"
- }
- }
- return false
- }
- func DevicePathIsValidPartition(path string) bool {
-
- if !strings.HasPrefix(path, "/dev/") {
- path = "/dev/" + path
- }
- path = strings.TrimSuffix(path, "/")
- allBlockDevices, err := lsblk.GetLSBLKOutput()
- if err != nil {
- return false
- }
- for _, blockDevice := range allBlockDevices {
- if !strings.HasPrefix(path, "/dev/"+blockDevice.Name) {
-
-
- continue
- }
- for _, child := range blockDevice.Children {
- if "/dev/"+child.Name == path {
-
-
- return true
- }
- }
- }
- return false
- }
- func GetDiskInfo(diskname string) (*Disk, error) {
- if diskname == "" {
- return nil, errors.New("disk name is empty")
- }
-
- diskname = strings.TrimPrefix(diskname, "/dev/")
-
- thisDisk := &Disk{
- Name: diskname,
- Size: 0,
- BlockType: "disk",
- Partitions: []*Partition{},
- }
-
- diskInfo, err := fdisk.GetDiskModelAndIdentifier(diskname)
- if err == nil {
- thisDisk.Model = diskInfo.Model
- thisDisk.Identifier = diskInfo.Identifier
- thisDisk.DiskLabel = diskInfo.DiskLabel
- }
-
- totalDiskUseSpace := int64(0)
-
- allBlockDevices, err := lsblk.GetLSBLKOutput()
- if err != nil {
- return nil, err
- }
- for _, blockDevice := range allBlockDevices {
- if blockDevice.Name == diskname {
- thisDisk.Size = blockDevice.Size
- for _, partition := range blockDevice.Children {
-
- partition := &Partition{
- Name: partition.Name,
- Size: partition.Size,
- Path: filepath.Join("/dev", partition.Name),
- BlockType: partition.Type,
- MountPoint: partition.MountPoint,
- }
-
- blkInfo, err := blkid.GetPartitionIDFromDevicePath(partition.Name)
- if err == nil {
- partition.UUID = blkInfo.UUID
- partition.PartUUID = blkInfo.PartUUID
- partition.PartLabel = blkInfo.PartLabel
- partition.BlockSize = blkInfo.BlockSize
- partition.BlockType = blkInfo.Type
- partition.FsType = blkInfo.Type
- }
-
- diskUsage, err := df.GetDiskUsageByPath(partition.Name)
- if err == nil {
- partition.Used = diskUsage.Used
- partition.Free = diskUsage.Available
- }
- thisDisk.Partitions = append(thisDisk.Partitions, partition)
- }
- }
- }
-
- for _, partition := range thisDisk.Partitions {
- totalDiskUseSpace += partition.Used
- }
- thisDisk.Used = totalDiskUseSpace
- thisDisk.Free = thisDisk.Size - totalDiskUseSpace
- return thisDisk, nil
- }
- func GetPartitionInfo(partitionName string) (*Partition, error) {
- partition := &Partition{
- Name: partitionName,
- }
- partInfo, err := blkid.GetPartitionIDFromDevicePath(partitionName)
- if err == nil {
- partition.UUID = partInfo.UUID
- partition.PartUUID = partInfo.PartUUID
- partition.PartLabel = partInfo.PartLabel
- partition.BlockSize = partInfo.BlockSize
- partition.BlockType = partInfo.Type
- partition.FsType = partInfo.Type
- }
-
- diskUsage, err := df.GetDiskUsageByPath(partitionName)
- if err == nil {
- partition.Used = diskUsage.Used
- partition.Free = diskUsage.Available
- partition.MountPoint = diskUsage.MountedOn
- }
- return partition, nil
- }
- func GetDevicePathFromPartitionID(diskID string) (string, error) {
- if diskID == "" {
- return "", errors.New("disk ID is empty")
- }
-
- allBlockDevices, err := lsblk.GetLSBLKOutput()
- if err != nil {
- return "", err
- }
- for _, blockDevice := range allBlockDevices {
-
- for _, child := range blockDevice.Children {
- if child.Name == diskID {
- return child.Name, nil
- }
- }
- }
- return "", errors.New("disk ID not found")
- }
|