11// Copyright 2024 NetApp, Inc. All Rights Reserved.
22
33//go:generate mockgen -destination=../../mocks/mock_utils/mock_devices/mock_devices_client.go github.com/netapp/trident/utils/devices Devices
4+ //go:generate mockgen -destination=../../mocks/mock_utils/mock_devices/mock_size_getter_client.go github.com/netapp/trident/utils/devices SizeGetter
45
56package devices
67
@@ -74,17 +75,28 @@ type Devices interface {
7475 ) error
7576}
7677
78+ type SizeGetter interface {
79+ GetDiskSize (ctx context.Context , devicePath string ) (int64 , error )
80+ }
81+
82+ type DiskSizeGetter struct {}
83+
84+ func NewDiskSizeGetter () * DiskSizeGetter {
85+ return & DiskSizeGetter {}
86+ }
87+
7788type Client struct {
7889 chrootPathPrefix string
7990 command exec.Command
8091 osFs afero.Afero
92+ SizeGetter
8193}
8294
8395func New () * Client {
84- return NewDetailed (exec .NewCommand (), afero .NewOsFs ())
96+ return NewDetailed (exec .NewCommand (), afero .NewOsFs (), NewDiskSizeGetter () )
8597}
8698
87- func NewDetailed (command exec.Command , osFs afero.Fs ) * Client {
99+ func NewDetailed (command exec.Command , osFs afero.Fs , diskSizeGetter SizeGetter ) * Client {
88100 chrootPathPrefix := ""
89101 if os .Getenv ("DOCKER_PLUGIN_MODE" ) != "" {
90102 chrootPathPrefix = "/host"
@@ -93,6 +105,7 @@ func NewDetailed(command exec.Command, osFs afero.Fs) *Client {
93105 chrootPathPrefix : chrootPathPrefix ,
94106 command : command ,
95107 osFs : afero.Afero {Fs : osFs },
108+ SizeGetter : diskSizeGetter ,
96109 }
97110}
98111
@@ -297,7 +310,7 @@ func (c *Client) FindDevicesForMultipathDevice(ctx context.Context, device strin
297310 devices := make ([]string , 0 )
298311
299312 slavesDir := c .chrootPathPrefix + "/sys/block/" + device + "/slaves"
300- if dirs , err := os .ReadDir (slavesDir ); err == nil {
313+ if dirs , err := c . osFs .ReadDir (slavesDir ); err == nil {
301314 for _ , f := range dirs {
302315 name := f .Name ()
303316 if strings .HasPrefix (name , "sd" ) {
@@ -585,7 +598,7 @@ func (c *Client) GetMultipathDeviceDisks(
585598 multipathDevice := strings .TrimPrefix (multipathDevicePath , "/dev/" )
586599
587600 diskPath := c .chrootPathPrefix + fmt .Sprintf ("/sys/block/%s/slaves/" , multipathDevice )
588- diskDirs , err := os .ReadDir (diskPath )
601+ diskDirs , err := c . osFs .ReadDir (diskPath )
589602 if err != nil {
590603 Logc (ctx ).WithError (err ).Errorf ("Could not read %s" , diskPath )
591604 return nil , fmt .Errorf ("failed to identify multipath device disks; unable to read '%s'" , diskPath )
@@ -607,7 +620,7 @@ func (c *Client) GetMultipathDeviceDisks(
607620func (c * Client ) GetMultipathDeviceBySerial (ctx context.Context , hexSerial string ) (string , error ) {
608621 sysPath := c .chrootPathPrefix + "/sys/block/"
609622
610- blockDirs , err := os .ReadDir (sysPath )
623+ blockDirs , err := c . osFs .ReadDir (sysPath )
611624 if err != nil {
612625 Logc (ctx ).WithError (err ).Errorf ("Could not read %s" , sysPath )
613626 return "" , fmt .Errorf ("failed to find multipath device by serial; unable to read '%s'" , sysPath )
@@ -647,12 +660,12 @@ func (c *Client) GetMultipathDeviceUUID(multipathDevicePath string) (string, err
647660
648661 deviceUUIDPath := c .chrootPathPrefix + fmt .Sprintf ("/sys/block/%s/dm/uuid" , multipathDevice )
649662
650- exists , err := PathExists (deviceUUIDPath )
663+ exists , err := PathExists (c . osFs , deviceUUIDPath )
651664 if ! exists || err != nil {
652665 return "" , errors .NotFoundError ("multipath device '%s' UUID not found" , multipathDevice )
653666 }
654667
655- UUID , err := os .ReadFile (deviceUUIDPath )
668+ UUID , err := c . osFs .ReadFile (deviceUUIDPath )
656669 if err != nil {
657670 return "" , err
658671 }
@@ -666,15 +679,15 @@ func (c *Client) RemoveDevice(ctx context.Context, devices []string, ignoreError
666679 defer Logc (ctx ).Debug ("<<<< devices.removeDevice" )
667680
668681 var (
669- f * os .File
682+ f afero .File
670683 err error
671684 )
672685
673686 c .ListAllDevices (ctx )
674687 for _ , deviceName := range devices {
675688
676689 filename := fmt .Sprintf (c .chrootPathPrefix + "/sys/block/%s/device/delete" , deviceName )
677- if f , err = os .OpenFile (filename , os .O_APPEND | os .O_WRONLY , 0o200 ); err != nil {
690+ if f , err = c . osFs .OpenFile (filename , os .O_APPEND | os .O_WRONLY , 0o200 ); err != nil {
678691 Logc (ctx ).WithField ("file" , filename ).Warning ("Could not open file for writing." )
679692 if ignoreErrors {
680693 continue
@@ -746,7 +759,7 @@ func (c *Client) GetLUKSDeviceForMultipathDevice(multipathDevice string) (string
746759 dmDevice := strings .TrimSuffix (strings .TrimPrefix (multipathDevice , "/dev/" ), "/" )
747760
748761 // Get holder of mpath device
749- dirents , err := os .ReadDir (fmt .Sprintf ("/sys/block/%s/holders/" , dmDevice ))
762+ dirents , err := c . osFs .ReadDir (fmt .Sprintf ("/sys/block/%s/holders/" , dmDevice ))
750763 if err != nil {
751764 return "" , err
752765 }
@@ -759,7 +772,7 @@ func (c *Client) GetLUKSDeviceForMultipathDevice(multipathDevice string) (string
759772 holder := dirents [0 ].Name ()
760773
761774 // Verify holder is LUKS device
762- b , err := os .ReadFile (fmt .Sprintf ("/sys/block/%s/dm/uuid" , holder ))
775+ b , err := c . osFs .ReadFile (fmt .Sprintf ("/sys/block/%s/dm/uuid" , holder ))
763776 if err != nil {
764777 return "" , err
765778 }
0 commit comments