Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missing df #7

Open
i4ki opened this issue Sep 22, 2016 · 4 comments
Open

missing df #7

i4ki opened this issue Sep 22, 2016 · 4 comments

Comments

@i4ki
Copy link
Collaborator

i4ki commented Sep 22, 2016

No description provided.

@charlessachet
Copy link

@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.

@i4ki
Copy link
Collaborator Author

i4ki commented Nov 8, 2016

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

@charlessachet
Copy link

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:

  • dfoutput:

    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

@i4ki
Copy link
Collaborator Author

i4ki commented Nov 8, 2016

Found this: http://stackoverflow.com/questions/21693673/differ-in-output-of-df-and-statfs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants