1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package diskinfo
- import (
- "errors"
- "os"
- "imuslab.com/bokofs/bokofsd/mod/diskinfo/blkid"
- "imuslab.com/bokofs/bokofsd/mod/diskinfo/lsblk"
- )
- type Disk struct {
- UUID string `json:"uuid"`
- Name string `json:"name"`
- Path string `json:"path"`
- Size int64 `json:"size"`
- BlockSize int `json:"blocksize"`
- BlockType string `json:"blocktype"`
- FsType string `json:"fstype"`
- MountPoint string `json:"mountpoint,omitempty"`
- }
- func NewDiskFromDevicePath(devpath string) (*Disk, error) {
- if _, err := os.Stat(devpath); errors.Is(err, os.ErrNotExist) {
- return nil, errors.New("device path does not exist")
- }
-
- thisDisk := &Disk{
- Path: devpath,
- }
-
- err := thisDisk.UpdateProperties()
- if err != nil {
- return nil, err
- }
- return thisDisk, nil
- }
- func (d *Disk) UpdateProperties() error {
-
- blockDeviceInfo, err := lsblk.GetBlockDeviceInfoFromDevicePath(d.Path)
- if err != nil {
- return err
- }
-
- d.Name = blockDeviceInfo.Name
- d.Size = blockDeviceInfo.Size
- d.BlockType = blockDeviceInfo.Type
- d.MountPoint = blockDeviceInfo.MountPoint
- if d.BlockType == "disk" {
-
-
- return nil
- }
-
- diskIdInfo, err := blkid.GetPartitionIDFromDevicePath(d.Path)
- if err != nil {
- return err
- }
-
- d.UUID = diskIdInfo.UUID
- d.FsType = diskIdInfo.Type
- d.BlockSize = diskIdInfo.BlockSize
- return nil
- }
|