Skip to content

Commit 3f98efb

Browse files
Alex Lau (AvengerMoJo)pgier
Alex Lau (AvengerMoJo)
authored andcommitted
Add sysfs handle and remove path from Get backstore function
Update the struct assignment, test case, replace global path name Add sysfs path alone with configfs for better clearity Signed-off-by: Alex Lau (AvengerMoJo) <[email protected]>
1 parent 6a34993 commit 3f98efb

File tree

3 files changed

+59
-46
lines changed

3 files changed

+59
-46
lines changed

iscsi/get.go

+26-27
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,13 @@ func ReadWriteOPS(iqnPath string, tpgt string, lun string) (readmb uint64,
137137

138138
// GetFileioUdev is getting the actual info to build up
139139
// the FILEIO data and match with the enable target
140-
func (fileio FILEIO) GetFileioUdev(targetCorePath string, fileioNumber string,
141-
objectName string) (fio *FILEIO, err error) {
142-
143-
fileio.Name = "fileio_" + fileioNumber
144-
fileio.Fnumber = fileioNumber
145-
fileio.ObjectName = objectName
146-
147-
udevPath := filepath.Join(targetCorePath, fileio.Name, fileio.ObjectName, "udev_path")
140+
func (fs FS) GetFileioUdev(fileioNumber string, objectName string) (*FILEIO, error) {
141+
fileio := FILEIO{
142+
Name: "fileio_" + fileioNumber,
143+
Fnumber: fileioNumber,
144+
ObjectName: objectName,
145+
}
146+
udevPath := fs.configfs.Path(targetCore, fileio.Name, fileio.ObjectName, "udev_path")
148147

149148
if _, err := os.Stat(udevPath); os.IsNotExist(err) {
150149
return nil, fmt.Errorf("iscsi: GetFileioUdev: fileio_%s is missing file name", fileio.Fnumber)
@@ -160,14 +159,13 @@ func (fileio FILEIO) GetFileioUdev(targetCorePath string, fileioNumber string,
160159

161160
// GetIblockUdev is getting the actual info to build up
162161
// the IBLOCK data and match with the enable target
163-
func (iblock IBLOCK) GetIblockUdev(targetCorePath string, iblockNumber string,
164-
objectName string) (ib *IBLOCK, err error) {
165-
166-
iblock.Name = "iblock_" + iblockNumber
167-
iblock.Bnumber = iblockNumber
168-
iblock.ObjectName = objectName
169-
170-
udevPath := filepath.Join(targetCorePath, iblock.Name, iblock.ObjectName, "udev_path")
162+
func (fs FS) GetIblockUdev(iblockNumber string, objectName string) (*IBLOCK, error) {
163+
iblock := IBLOCK{
164+
Name: "iblock_" + iblockNumber,
165+
Bnumber: iblockNumber,
166+
ObjectName: objectName,
167+
}
168+
udevPath := fs.configfs.Path(targetCore, iblock.Name, iblock.ObjectName, "udev_path")
171169

172170
if _, err := os.Stat(udevPath); os.IsNotExist(err) {
173171
return nil, fmt.Errorf("iscsi: GetIBlockUdev: iblock_%s is missing file name", iblock.Bnumber)
@@ -183,12 +181,12 @@ func (iblock IBLOCK) GetIblockUdev(targetCorePath string, iblockNumber string,
183181

184182
// GetRBDMatch is getting the actual info to build up
185183
// the RBD data and match with the enable target
186-
func (rbd RBD) GetRBDMatch(sysDevicePath string, rbdNumber string, poolImage string) (r *RBD, err error) {
187-
188-
rbd.Name = "rbd_" + rbdNumber
189-
rbd.Rnumber = rbdNumber
190-
191-
systemRbds, err := filepath.Glob(filepath.Join(sysDevicePath, "/devices/rbd/[0-9]*"))
184+
func (fs FS) GetRBDMatch(rbdNumber string, poolImage string) (*RBD, error) {
185+
rbd := RBD{
186+
Name: "rbd_" + rbdNumber,
187+
Rnumber: rbdNumber,
188+
}
189+
systemRbds, err := filepath.Glob(fs.sysfs.Path(devicePath, "[0-9]*"))
192190
if err != nil {
193191
return nil, fmt.Errorf("iscsi: GetRBDMatch: Cannot find any rbd block")
194192
}
@@ -228,11 +226,12 @@ func (rbd RBD) GetRBDMatch(sysDevicePath string, rbdNumber string, poolImage str
228226
}
229227

230228
// GetRDMCPPath is getting the actual info to build up RDMCP data
231-
func (rdmcp RDMCP) GetRDMCPPath(targetCorePath string, rdmcpNumber string, objectName string) (r *RDMCP, err error) {
232-
rdmcp.Name = "rd_mcp_" + rdmcpNumber
233-
rdmcp.ObjectName = objectName
234-
235-
rdmcpPath := filepath.Join(targetCorePath, rdmcp.Name, rdmcp.ObjectName)
229+
func (fs FS) GetRDMCPPath(rdmcpNumber string, objectName string) (*RDMCP, error) {
230+
rdmcp := RDMCP{
231+
Name: "rd_mcp_" + rdmcpNumber,
232+
ObjectName: objectName,
233+
}
234+
rdmcpPath := fs.configfs.Path(targetCore, rdmcp.Name, rdmcp.ObjectName)
236235

237236
if _, err := os.Stat(rdmcpPath); os.IsNotExist(err) {
238237
return nil, fmt.Errorf("iscsi: GetRDMCPPath: %s does not exist", rdmcpPath)

iscsi/get_test.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package iscsi_test
1515

1616
import (
17-
"path/filepath"
1817
"reflect"
1918
"testing"
2019

@@ -127,11 +126,11 @@ func TestGetStats(t *testing.T) {
127126
{1504, 4733, 1234},
128127
}
129128

130-
sysfs, err := iscsi.NewFS("../fixtures/sys/kernel/config")
129+
sysconfigfs, err := iscsi.NewFS("../fixtures/sys", "../fixtures/sys/kernel/config")
131130
if err != nil {
132131
t.Fatalf("failed to access xfs fs: %v", err)
133132
}
134-
sysfsStat, err := sysfs.ISCSIStats()
133+
sysfsStat, err := sysconfigfs.ISCSIStats()
135134
statSize := len(sysfsStat)
136135
if statSize != 4 {
137136
t.Errorf("fixtures size does not match %d", statSize)
@@ -162,8 +161,7 @@ func TestGetStats(t *testing.T) {
162161
t.Errorf("unexpected iSCSI iops data :\nwant:\n%v\nhave:\n%v", readTests[i].iops, iops)
163162
}
164163
if stat.Tpgt[0].Luns[0].Backstore == "rd_mcp" {
165-
have_rdmcp := new(iscsi.RDMCP)
166-
have_rdmcp, err := have_rdmcp.GetRDMCPPath(filepath.Join("../fixtures/sys/kernel/config", iscsi.TargetCore), "119", "ramdisk_lio_1G")
164+
have_rdmcp, err := sysconfigfs.GetRDMCPPath("119", "ramdisk_lio_1G")
167165
if err != nil {
168166
t.Errorf("fail rdmcp error %v", err)
169167
}
@@ -174,8 +172,7 @@ func TestGetStats(t *testing.T) {
174172
t.Errorf("unexpected rdmcp data :\nwant:\n%v\nhave:\n%v", want_rdmcp, have_rdmcp)
175173
}
176174
} else if stat.Tpgt[0].Luns[0].Backstore == "iblock" {
177-
have_iblock := new(iscsi.IBLOCK)
178-
have_iblock, err = have_iblock.GetIblockUdev(filepath.Join("../fixtures/sys/kernel/config", iscsi.TargetCore), "0", "block_lio_rbd1")
175+
have_iblock, err := sysconfigfs.GetIblockUdev("0", "block_lio_rbd1")
179176
if err != nil {
180177
t.Errorf("fail iblock error %v", err)
181178
}
@@ -185,8 +182,7 @@ func TestGetStats(t *testing.T) {
185182
t.Errorf("unexpected iblock data :\nwant:\n%v\nhave:\n%v", want_iblock, have_iblock)
186183
}
187184
} else if stat.Tpgt[0].Luns[0].Backstore == "fileio" {
188-
have_fileio := new(iscsi.FILEIO)
189-
have_fileio, err = have_fileio.GetFileioUdev(filepath.Join("../fixtures/sys/kernel/config", iscsi.TargetCore), "1", "file_lio_1G")
185+
have_fileio, err := sysconfigfs.GetFileioUdev("1", "file_lio_1G")
190186
if err != nil {
191187
t.Errorf("fail fileio error %v", err)
192188
}
@@ -196,8 +192,7 @@ func TestGetStats(t *testing.T) {
196192
t.Errorf("unexpected fileio data :\nwant:\n%v\nhave:\n%v", want_fileio, have_fileio)
197193
}
198194
} else if stat.Tpgt[0].Luns[0].Backstore == "rbd" {
199-
have_rbd := new(iscsi.RBD)
200-
have_rbd, err = have_rbd.GetRBDMatch("../fixtures/sys", "0", "iscsi-images-demo")
195+
have_rbd, err := sysconfigfs.GetRBDMatch("0", "iscsi-images-demo")
201196
if err != nil {
202197
t.Errorf("fail rbd error %v", err)
203198
}

iscsi/iscsi.go

+27-8
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,56 @@ import (
2323

2424
// iscsi target started with /sys/kernel/config/target/iscsi/iqn*
2525
// configfs + target/iscsi/iqn*
26-
// IqnGlob is representing all the possible IQN
27-
const IqnGlob = "target/iscsi/iqn*"
26+
// iqnGlob is representing all the possible IQN
27+
const iqnGlob = "target/iscsi/iqn*"
2828

29-
// TargetCore static path /sys/kernel/config/target/core for node_exporter
29+
// targetCore static path /sys/kernel/config/target/core for node_exporter
3030
// reading runtime status
31-
const TargetCore = "target/core"
31+
const targetCore = "target/core"
32+
33+
// devicePath static path /sys/devices/rbd/[0-9]* for rbd devices to
34+
// read at runtime status
35+
const devicePath = "devices/rbd"
3236

3337
// FS represents the pseudo-filesystem configfs, which provides an interface to
34-
// iscsi kernel data structures in /sys/kernel/config.
38+
// iscsi kernel data structures in
39+
// sysfs as /sys
40+
// configfs as /sys/kernel/config
3541
type FS struct {
42+
sysfs *fs.FS
3643
configfs *fs.FS
3744
}
3845

3946
// NewFS returns a new configfs mounted under the given mount point. It will
4047
// error and return empty FS if the mount point can't be read. For the ease of
4148
// use, an empty string parameter configfsMountPoint will call internal fs for
4249
// the default sys path as /sys/kernel/config
43-
func NewFS(configfsMountPoint string) (FS, error) {
50+
func NewFS(sysfsPath string, configfsMountPoint string) (FS, error) {
51+
if strings.TrimSpace(sysfsPath) == "" {
52+
sysfsPath = fs.DefaultSysMountPoint
53+
}
54+
sysfs, err := fs.NewFS(sysfsPath)
55+
if err != nil {
56+
return FS{}, err
57+
}
4458
if strings.TrimSpace(configfsMountPoint) == "" {
4559
configfsMountPoint = fs.DefaultConfigfsMountPoint
4660
}
4761
configfs, err := fs.NewFS(configfsMountPoint)
4862
if err != nil {
4963
return FS{}, err
5064
}
51-
return FS{&configfs}, nil
65+
return FS{&sysfs, &configfs}, nil
66+
}
67+
68+
// helper function to get configfs path
69+
func (fs FS) Path(p ...string) string {
70+
return fs.configfs.Path(p...)
5271
}
5372

5473
// ISCSIStats getting iscsi runtime information
5574
func (fs FS) ISCSIStats() ([]*Stats, error) {
56-
matches, err := filepath.Glob(fs.configfs.Path(IqnGlob))
75+
matches, err := filepath.Glob(fs.configfs.Path(iqnGlob))
5776
if err != nil {
5877
return nil, err
5978
}

0 commit comments

Comments
 (0)