Use os.File#Seek() to get the size of a block device

Instead of calling blockdev(1), this change uses os.File#Seek which
would be more effecient.

https://github.com/firecracker-microvm/firecracker/pull/1371

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato 2021-03-25 10:26:39 -07:00
parent 1b05b605c8
commit e1f51ba73d

View File

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