This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
docker_quota_linux.go
100 lines (80 loc) · 2.36 KB
/
docker_quota_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//go:build linux && !noroot
// +build linux,!noroot
package docker
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"runtime"
"unsafe"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
/*
struct dqinfo
{
__uint64_t dqi_bgrace;
__uint64_t dqi_igrace;
__uint32_t dqi_flags;
__uint32_t dqi_valid;
};
*/
const q_getinfo = 0x800005 // nolint
const prjquota = 2
const subcmdshift = 8
const subcmdmask = 0x00ff
type dqinfo struct { // nolint
dqi_bgrace uint64 // nolint
dqi_igrace uint64 // nolint
dqi_flags uint32 // nolint
dqi_valid uint32 // nolint
}
func qcmd(_cmd, _type int) int {
return (((_cmd) << subcmdshift) | ((_type) & subcmdmask))
}
func hasProjectQuotasEnabled(rootDir string) bool {
tempdir, err := ioutil.TempDir("", "docker-quota-detector")
if err != nil {
panic(err)
}
defer func() {
if err2 := os.RemoveAll(tempdir); err2 != nil {
log.Warning("Unable to remove tempdir: ", err2)
}
}()
dev, err := makeBackingFsDev(rootDir, tempdir)
if err != nil {
panic(err)
}
var dqi dqinfo
devcstr := []byte(dev + "\x00") // This is to cast it into a cstr
devcstrPointer := uintptr(unsafe.Pointer(&devcstr[0])) // nolint: gosec
dqiPointer := uintptr(unsafe.Pointer(&dqi)) // nolint: gosec
r1, _, errno := syscall.Syscall6(syscall.SYS_QUOTACTL, uintptr(qcmd(q_getinfo, prjquota)), devcstrPointer, 0, dqiPointer, 0, 0)
runtime.KeepAlive(devcstr)
runtime.KeepAlive(dqi)
if errno == syscall.Errno(0) && int(r1) != -1 {
return true
}
if errno != unix.ESRCH {
log.WithField("rootDir", rootDir).Warning("Got unexpected error: ", errno)
}
return false
}
// Code borrowed from Moby (Docker), here: https://github.com/moby/moby/blob/1009e6a40b295187e038b67e184e9c0384d95538/daemon/graphdriver/quota/projectquota.go
// Get the backing block device of the driver home directory
// and create a block device node under the home directory
// to be used by quotactl commands
func makeBackingFsDev(home, tempdir string) (string, error) {
var stat unix.Stat_t
if err := unix.Stat(home, &stat); err != nil {
return "", err
}
backingFsBlockDev := filepath.Join(tempdir, "backingFsBlockDev")
if err := unix.Mknod(backingFsBlockDev, unix.S_IFBLK|0600, int(stat.Dev)); err != nil {
return "", fmt.Errorf("Failed to mknod %s: %v", backingFsBlockDev, err)
}
return backingFsBlockDev, nil
}