/
osutils_linux.go
64 lines (54 loc) · 1.92 KB
/
osutils_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2019 NetApp, Inc. All Rights Reserved.
package utils
import (
"fmt"
"os"
"syscall"
"unsafe"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
// getFilesystemSize returns the size of the filesystem for the given path.
// The caller of the func is responsible for verifying the mountPoint existence and readiness.
func getFilesystemSize(path string) (int64, error) {
log.Debug(">>>> osutils_linux.getFilesystemSize")
defer log.Debug("<<<< osutils_linux.getFilesystemSize")
// Warning: syscall.Statfs_t uses types that are OS and arch dependent. The following code has been
// confirmed to work with Linux/amd64 and Darwin/amd64.
var buf syscall.Statfs_t
err := syscall.Statfs(path, &buf)
if err != nil {
log.WithField("path", path).Errorf("Failed to statfs: %s", err)
return 0, fmt.Errorf("couldn't get filesystem stats %s: %s", path, err)
}
size := int64(buf.Blocks) * buf.Bsize
log.WithFields(log.Fields{
"path": path,
"size": size,
"bsize": buf.Bsize,
"blocks": buf.Blocks,
"avail": buf.Bavail,
"free": buf.Bfree,
}).Debug("Filesystem size information")
return size, nil
}
// getISCSIDiskSize queries the current block size in bytes
func getISCSIDiskSize(devicePath string) (int64, error) {
fields := log.Fields{"devicePath": devicePath}
log.WithFields(fields).Debug(">>>> osutils_linux.getISCSIDiskSize")
defer log.WithFields(fields).Debug("<<<< osutils_linux.getISCSIDiskSize")
disk, err := os.Open(devicePath)
if err != nil {
log.Error("Failed to open disk.")
return 0, fmt.Errorf("failed to open disk %s: %s", devicePath, err)
}
defer disk.Close()
var size int64
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, disk.Fd(), unix.BLKGETSIZE64, uintptr(unsafe.Pointer(&size)))
if errno != 0 {
err := os.NewSyscallError("ioctl", errno)
log.Error("BLKGETSIZE64 ioctl failed")
return 0, fmt.Errorf("BLKGETSIZE64 ioctl failed %s: %s", devicePath, err)
}
return size, nil
}