|
@@ -0,0 +1,197 @@
|
|
|
+package diskinfo
|
|
|
+
|
|
|
+import (
|
|
|
+ "errors"
|
|
|
+ "path/filepath"
|
|
|
+ "strings"
|
|
|
+
|
|
|
+ "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 DevicePathIsValidDisk(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 "/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")
|
|
|
+}
|