Merge pull request #5270 from kzys/remove-blockdev

Use os.File#Seek() to get the size of a block device
This commit is contained in:
Phil Estes 2021-03-31 14:33:45 -04:00 committed by GitHub
commit 9a9bd09756
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,8 @@ package dmsetup
import (
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
@ -327,15 +329,18 @@ func GetFullDevicePath(deviceName string) string {
}
// BlockDeviceSize returns size of block device in bytes
func BlockDeviceSize(devicePath string) (uint64, error) {
data, err := exec.Command("blockdev", "--getsize64", "-q", devicePath).CombinedOutput()
output := string(data)
func BlockDeviceSize(path string) (int64, error) {
f, err := os.Open(path)
if err != nil {
return 0, errors.Wrapf(err, output)
return 0, err
}
defer f.Close()
output = strings.TrimSuffix(output, "\n")
return strconv.ParseUint(output, 10, 64)
size, err := f.Seek(0, io.SeekEnd)
if err != nil {
return 0, errors.Wrapf(err, "failed to seek on %q", path)
}
return size, nil
}
func dmsetup(args ...string) (string, error) {