EDITED: as @abhink pointed out, was not invoking Size()
.
I tried two different go methods, and then compared to df
. Of course, all 3 give different results:
package main
import (
"os"
"syscall"
"fmt"
)
func main() {
disk := "/dev/sda1"
statout, err := os.Stat(disk)
if err != nil {
fmt.Errorf("Error %x", err)
os.Exit(1)
}
println("os.Stat Size : ", statout.Size())
var stat syscall.Statfs_t
syscall.Statfs(disk, &stat)
println("syscall.Statfs_t Type: ", stat.Type)
println("syscall.Statfs_t Bsize: ", stat.Bsize)
println("syscall.Statfs_t Blocks: ", stat.Blocks)
}
Running the programs:
$ main
os.Stat Size : 0
syscall.Statfs_t Type: 16914836
syscall.Statfs_t Bsize: 4096
syscall.Statfs_t Blocks: 2560
And df:
$ df /dev/sda1
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 65792556 43694068 18726712 70% /var
Net:
-
os.Stat()
gives0
which it is not, but might be an OS issue. -
syscall.Statfs()
gives 2560 blocks * 4096 block size =10,485,760
. More realistic, but still incorrect -
df
gives 65792556 1K-blocks * 1024 bytes / K =67,371,577,344
How do I reliably get the size of a block device without mounting it?
Essentially, I am looking for the equivalent of the ioctl
call on the device.
ioctl(fd,BLKGETSIZE64,&size)