BlockDiskSizeBytes returns the size of a block disk (path can be either block device or raw file).
(blockDiskPath string)
| 803 | |
| 804 | // BlockDiskSizeBytes returns the size of a block disk (path can be either block device or raw file). |
| 805 | func BlockDiskSizeBytes(blockDiskPath string) (int64, error) { |
| 806 | if linux.IsBlockdevPath(blockDiskPath) { |
| 807 | // Attempt to open the device path. |
| 808 | f, err := os.Open(blockDiskPath) |
| 809 | if err != nil { |
| 810 | return -1, err |
| 811 | } |
| 812 | |
| 813 | defer logger.WarnOnError(f.Close, "Failed to close file") |
| 814 | fd := int(f.Fd()) |
| 815 | |
| 816 | // Retrieve the block device size. |
| 817 | res, err := unix.IoctlGetInt(fd, unix.BLKGETSIZE64) |
| 818 | if err != nil { |
| 819 | return -1, err |
| 820 | } |
| 821 | |
| 822 | return int64(res), nil |
| 823 | } |
| 824 | |
| 825 | // Block device is assumed to be a raw file. |
| 826 | fi, err := os.Lstat(blockDiskPath) |
| 827 | if err != nil { |
| 828 | return -1, err |
| 829 | } |
| 830 | |
| 831 | return fi.Size(), nil |
| 832 | } |
| 833 | |
| 834 | // GetPhysicalBlockSize returns the physical block size for the device. |
| 835 | func GetPhysicalBlockSize(blockDiskPath string) (int, error) { |
no test coverage detected
searching dependent graphs…