Skip to content

Commit e1f51ba

Browse files
author
Kazuyoshi Kato
committed
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. firecracker-microvm/firecracker#1371 Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
1 parent 1b05b60 commit e1f51ba

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

snapshots/devmapper/dmsetup/dmsetup.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package dmsetup
2020

2121
import (
2222
"fmt"
23+
"io"
24+
"os"
2325
"os/exec"
2426
"strconv"
2527
"strings"
@@ -327,15 +329,18 @@ func GetFullDevicePath(deviceName string) string {
327329
}
328330

329331
// BlockDeviceSize returns size of block device in bytes
330-
func BlockDeviceSize(devicePath string) (uint64, error) {
331-
data, err := exec.Command("blockdev", "--getsize64", "-q", devicePath).CombinedOutput()
332-
output := string(data)
332+
func BlockDeviceSize(path string) (int64, error) {
333+
f, err := os.Open(path)
333334
if err != nil {
334-
return 0, errors.Wrapf(err, output)
335+
return 0, err
335336
}
337+
defer f.Close()
336338

337-
output = strings.TrimSuffix(output, "\n")
338-
return strconv.ParseUint(output, 10, 64)
339+
size, err := f.Seek(0, io.SeekEnd)
340+
if err != nil {
341+
return 0, errors.Wrapf(err, "failed to seek on %q", path)
342+
}
343+
return size, nil
339344
}
340345

341346
func dmsetup(args ...string) (string, error) {

0 commit comments

Comments
 (0)