Open i4ki opened 7 years ago
Nice.
You'll need to use the paths in /proc/mounts to call the syscall statfs(2). Take a look here: https://github.com/StalkR/goircbot/blob/master/lib/disk/space_unix.go#L11
[]'s i4k
Thanks!
scall.go
package main
import (
"fmt"
"os"
"syscall"
)
func main() {
path := os.Args[1]
scall := syscall.Statfs_t{}
err := syscall.Statfs(path, &scall)
if err != nil {
panic(fmt.Sprintf("Error[%s] doing syscall to fs for path[%s]", err, path))
}
total := scall.Bsize * int64(scall.Blocks)
free := scall.Bsize * int64(scall.Bfree)
used := total - free
usedPercentage := (float64(used) * 100) / float64(total)
fmt.Printf("Total[%v], Free[%v], Used[%v], Used[%v]", total, free, used, usedPercentage)
}
Running:
go run scall.go /home
I just did a test comparing the output between the script and the df
command:
df
output:
Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda5 736781456 18992812 680339276 3% /home
Script output:
Total[754464210944], Free[735013040128], Used[19451170816], Used[2.578143606263619]%
The output has a considerable difference. What do you think?
[]'s
@tiago4orion
The file that you passed me(
/proc/partitions
) returns:major minor #blocks name 8 0 976762584 sda 8 1 210453504 sda1 8 2 1 sda2 8 5 748659712 sda5 8 6 17643744 sda6 11 0 1048575 sr0
Another one that maybe is usefull is
/proc/mounts
and returns:/dev/sda5 /home ext4 rw,noatime,data=ordered 0 0 /dev/sda1 / ext4 rw,noatime,data=ordered 0 0
So... from the partition name we can found out where it is mounted.
However I'll continue searching for a file maintained by kernel that can give us the disk usage.