Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blockdevice): added sysblockdevicesize method and test #650

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions blockdevice/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ const (
sysBlockQueue = "queue"
sysBlockDM = "dm"
sysUnderlyingDev = "slaves"
sysBlockSize = "size"
)

// FS represents the pseudo-filesystems proc and sys, which provides an
Expand Down Expand Up @@ -474,3 +475,13 @@ func (fs FS) SysBlockDeviceUnderlyingDevices(device string) (UnderlyingDeviceInf
return UnderlyingDeviceInfo{DeviceNames: underlying}, nil

}

// SysBlockDeviceSizeBytes returns the size of the block device from /sys/block/<device>/size
// in bytes by multiplying the value by the Linux sector length of 512.
func (fs FS) SysBlockDeviceSizeBytes(device string) (uint64, error) {
size, err := util.ReadUintFromFile(fs.sys.Path(sysBlockPath, device, sysBlockSize))
if err != nil {
return 0, err
}
return 512 * size, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a const SectorSize

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
19 changes: 19 additions & 0 deletions blockdevice/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,22 @@ func TestSysBlockDeviceUnderlyingDevices(t *testing.T) {
t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", underlying0Expected, underlying0)
}
}

func TestSysBlockDeviceSize(t *testing.T) {
blockdevice, err := NewFS("testdata/fixtures/proc", "testdata/fixtures/sys")
if err != nil {
t.Fatalf("failed to access blockdevice fs: %v", err)
}
devices, err := blockdevice.SysBlockDevices()
if err != nil {
t.Fatal(err)
}
sizeBytes7, err := blockdevice.SysBlockDeviceSizeBytes(devices[7])
if err != nil {
t.Fatal(err)
}
sizeBytes7Expected := uint64(3750748848)
if sizeBytes7 != sizeBytes7Expected {
t.Errorf("Incorrect BlockDeviceSize, expected: \n%+v, got: \n%+v", sizeBytes7Expected, sizeBytes7)
}
}
1 change: 1 addition & 0 deletions testdata/fixtures/sys/block/sda/size
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3750748848