-
Notifications
You must be signed in to change notification settings - Fork 947
/
quota.go
367 lines (315 loc) · 11.1 KB
/
quota.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// +build linux
package quota
import (
"context"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"syscall"
"time"
"github.com/alibaba/pouch/pkg/exec"
"github.com/alibaba/pouch/pkg/kernel"
"github.com/alibaba/pouch/pkg/log"
"github.com/alibaba/pouch/pkg/system"
"github.com/pkg/errors"
)
const (
// QuotaMinID represents the minimize quota id.
// The value is unit32(2^24).
QuotaMinID = uint32(16777216)
// procMountFile represent the mounts file in proc virtual file system.
procMountFile = "/proc/mounts"
)
var (
// GQuotaDriver represents global quota driver.
GQuotaDriver = NewQuotaDriver("")
)
// BaseQuota defines the quota operation interface.
// It abstracts the common operation ways a quota driver should implement.
type BaseQuota interface {
// EnforceQuota is used to enforce disk quota effect on specified directory.
EnforceQuota(dir string) (*MountInfo, error)
// SetDiskQuota uses the following two parameters to set disk quota for a directory.
// * quota size: a byte size of requested quota.
// * quota ID: an ID represent quota attr which is used in the global scope.
SetDiskQuota(dir string, size string, quotaID uint32) error
// CheckMountpoint is used to check mount point.
// It returns mointpoint, enable quota and filesystem type of the device.
CheckMountpoint(devID uint64) (string, bool, string)
// GetQuotaIDInFileAttr gets attributes of the file which is in the inode.
// The returned result is quota ID.
GetQuotaIDInFileAttr(dir string) uint32
// SetQuotaIDInFileAttr sets file attributes of quota ID for the input directory.
// The input attributes is quota ID.
SetQuotaIDInFileAttr(dir string, quotaID uint32) error
// GetNextQuotaID gets next quota ID in global scope of host.
GetNextQuotaID() (uint32, error)
// SetFileAttrRecursive set the file attr by recursively.
SetFileAttrRecursive(dir string, quotaID uint32) error
}
// NewQuotaDriver returns a quota instance.
func NewQuotaDriver(name string) BaseQuota {
var quota BaseQuota
switch name {
case "grpquota":
quota = &GrpQuotaDriver{
quotaIDs: make(map[uint32]struct{}),
}
case "prjquota":
quota = &PrjQuotaDriver{
quotaIDs: make(map[uint32]struct{}),
}
default:
kernelVersion, err := kernel.GetKernelVersion()
if err == nil && kernelVersion.Kernel >= 4 {
quota = &PrjQuotaDriver{
quotaIDs: make(map[uint32]struct{}),
}
} else {
quota = &GrpQuotaDriver{
quotaIDs: make(map[uint32]struct{}),
}
}
}
return quota
}
// SetQuotaDriver is used to set global quota driver.
func SetQuotaDriver(name string) {
GQuotaDriver = NewQuotaDriver(name)
}
// SetDiskQuota is used to set quota for directory.
func SetDiskQuota(dir string, size string, quotaID uint32) error {
log.With(nil).Infof("set disk quota, dir(%s), size(%s), quotaID(%d)", dir, size, quotaID)
if isRegular, err := CheckRegularFile(dir); err != nil || !isRegular {
log.With(nil).Debugf("set quota skip not regular file: %s", dir)
return err
}
return GQuotaDriver.SetDiskQuota(dir, size, quotaID)
}
// CheckMountpoint is used to check mount point.
func CheckMountpoint(devID uint64) (string, bool, string) {
return GQuotaDriver.CheckMountpoint(devID)
}
// GetQuotaIDInFileAttr returns the directory attributes of quota ID.
func GetQuotaIDInFileAttr(dir string) uint32 {
return GQuotaDriver.GetQuotaIDInFileAttr(dir)
}
//GetNextQuotaID returns the next available quota id.
func GetNextQuotaID() (uint32, error) {
return GQuotaDriver.GetNextQuotaID()
}
// GetQuotaID returns the quota id of directory,
// if no quota id, it will alloc the next available quota id.
func GetQuotaID(dir string) (uint32, error) {
id := GetQuotaIDInFileAttr(dir)
if id > 0 {
return id, nil
}
id, err := GetNextQuotaID()
if err != nil {
return 0, errors.Wrapf(err, "failed to get file(%s) quota id", dir)
}
return id, nil
}
// SetRootfsDiskQuota is to set container rootfs dir disk quota.
func SetRootfsDiskQuota(basefs, size string, quotaID uint32, update bool) (uint32, error) {
overlayMountInfo, err := getOverlayMountInfo(basefs)
if err != nil {
return 0, errors.Wrapf(err, "failed to get overlay(%s) mount info", basefs)
}
for _, dir := range []string{overlayMountInfo.Upper, overlayMountInfo.Work} {
if quotaID == 0 {
quotaID, err = GetQuotaID(dir)
if err != nil {
return 0, errors.Wrapf(err, "failed to get dir(%s) quota id", dir)
}
}
if err := SetDiskQuota(dir, size, quotaID); err != nil {
return 0, errors.Wrapf(err, "failed to set dir(%s) disk quota", dir)
}
if update {
go SetFileAttrRecursive(dir, quotaID)
} else if err := SetFileAttrRecursive(dir, quotaID); err != nil {
return 0, errors.Wrapf(err, "failed to set dir(%s) quota recursively", dir)
}
}
return quotaID, nil
}
// SetFileAttrRecursive set the file attr by recursively.
func SetFileAttrRecursive(dir string, quotaID uint32) error {
return GQuotaDriver.SetFileAttrRecursive(dir, quotaID)
}
// CheckRegularFile is used to check the file is regular file or directory.
func CheckRegularFile(file string) (bool, error) {
fd, err := os.Lstat(file)
if err != nil {
log.With(nil).Warnf("failed to check file: %s, err: %v", file, err)
return false, err
}
mode := fd.Mode()
if mode&(os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) == 0 {
return true, nil
}
return false, nil
}
// IsSetQuotaID returns whether set quota id
func IsSetQuotaID(id string) bool {
return id != "" && id != "0"
}
// getOverlayMountInfo gets overlayFS informantion from /proc/mounts.
// upperdir, mergeddir and workdir would be dealt.
func getOverlayMountInfo(basefs string) (*OverlayMount, error) {
output, err := ioutil.ReadFile(procMountFile)
if err != nil {
log.With(nil).Warnf("failed to read file(%s), err(%v)", procMountFile, err)
return nil, err
}
var lowerDir, upperDir, workDir string
for _, line := range strings.Split(string(output), "\n") {
parts := strings.Split(string(line), " ")
if len(parts) != 6 {
continue
}
if parts[1] != basefs || parts[2] != "overlay" {
continue
}
// the expected format is like following:
// overlay /var/lib/pouch/containerd/state/io.containerd.runtime.v1.linux/default/8d849ee68c8698531a2575f890be027dbd4dcb64f39cce37d7d22a703cbb362b/rootfs overlay rw,relatime,lowerdir=/var/lib/pouch/containerd/root/io.containerd.snapshotter.v1.overlayfs/snapshots/1/fs,upperdir=/var/lib/pouch/containerd/root/io.containerd.snapshotter.v1.overlayfs/snapshots/274/fs,workdir=/var/lib/pouch/containerd/root/io.containerd.snapshotter.v1.overlayfs/snapshots/274/work 0 0
// In part[3], it stored lowerdir, upperdir and workdir.
mountParams := strings.Split(parts[3], ",")
for _, p := range mountParams {
switch {
case strings.Contains(p, "lowerdir"):
if s := strings.Split(p, "="); len(s) == 2 {
lowerDir = s[1]
}
case strings.Contains(p, "upperdir"):
if s := strings.Split(p, "="); len(s) == 2 {
upperDir = s[1]
}
case strings.Contains(p, "workdir"):
if s := strings.Split(p, "="); len(s) == 2 {
workDir = s[1]
}
}
}
}
if lowerDir == "" || upperDir == "" || workDir == "" {
return nil, fmt.Errorf("failed to get OverlayFs Mount Info: lowerdir, upperdir, workdir must be non-empty")
}
return &OverlayMount{
Lower: lowerDir,
Upper: upperDir,
Work: workDir,
}, nil
}
// loadQuotaIDs loads quota IDs for quota driver from reqquota execution result.
// This function utils `repquota` which summarizes quotas for a filesystem.
// see http://man7.org/linux/man-pages/man8/repquota.8.html
//
// $ repquota -Pan
// Project used soft hard grace used soft hard grace
// ----------------------------------------------------------------------
// #0 -- 220 0 0 25 0 0
// #123 -- 4 0 88589934592 1 0 0
// #8888 -- 8 0 0 2 0 0
//
// Or
//
// $ repquota -gan
// Group used soft hard grace used soft hard grace
// ----------------------------------------------------------------------
// #0 -- 494472 0 0 938 0 0
// #54 -- 8 0 0 2 0 0
// #4 -- 16 0 0 4 0 0
// #22 -- 28 0 0 4 0 0
// #16777220 +- 2048576 0 2048575 9 0 0
// #500 -- 47504 0 0 101 0 0
// #16777221 -- 3048576 0 3048576 8 0 0
func loadQuotaIDs(repquotaOpt string) (map[uint32]struct{}, uint32, error) {
quotaIDs := make(map[uint32]struct{})
minID := QuotaMinID
exit, output, stderr, err := exec.Run(0, "repquota", repquotaOpt)
if err != nil {
return nil, 0, errors.Wrapf(err, "failed to execute [repquota %s], stdout: (%s), stderr: (%s), exit: (%d)",
repquotaOpt, output, stderr, exit)
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if len(line) == 0 || line[0] != '#' {
continue
}
// find all lines with prefix '#'
parts := strings.Split(line, " ")
// part[0] is "#123456"
if len(parts[0]) <= 1 {
continue
}
id, err := strconv.Atoi(parts[0][1:])
quotaID := uint32(id)
if err == nil && quotaID > QuotaMinID {
quotaIDs[quotaID] = struct{}{}
if quotaID > minID {
minID = quotaID
}
}
}
log.With(nil).Infof("Load repquota ids(%d), list(%v)", len(quotaIDs), quotaIDs)
return quotaIDs, minID, nil
}
// getDevLimit returns the device storage upper limit.
func getDevLimit(info *MountInfo) (uint64, error) {
mp := info.MountPoint
devID := info.DeviceID
newDevID, _ := system.GetDevID(mp)
if newDevID != devID {
return 0, errors.Errorf("failed to set device limit, no such device id(%d), checked id(%d)",
devID, newDevID)
}
// get storage upper limit of the device which the dir is on.
var stfs syscall.Statfs_t
if err := syscall.Statfs(mp, &stfs); err != nil {
log.With(nil).Errorf("failed to get path(%s) limit, err(%v)", mp, err)
return 0, errors.Wrapf(err, "failed to get path(%s) limit", mp)
}
limit := stfs.Blocks * uint64(stfs.Bsize)
log.With(nil).Debugf("get device limit size, mountpoint(%s), limit(%v) B", mp, limit)
return limit, nil
}
// checkDevLimit checks if the device on which the input dir lies has already been recorded in driver.
func checkDevLimit(mountInfo *MountInfo, size uint64) error {
mp := mountInfo.MountPoint
limit, err := getDevLimit(mountInfo)
if err != nil {
return errors.Wrapf(err, "failed to get device(%s) limit", mp)
}
if limit < size {
return fmt.Errorf("dir %s quota limit %v must be less than %v", mp, size, limit)
}
log.With(nil).Debugf("succeeded in checkDevLimit (dir %s quota limit %v B) with size %v B", mp, limit, size)
return nil
}
func getDevID(dir string) (uint64, error) {
// ensure stat syscall don't timeout
idChan := make(chan uint64)
errChan := make(chan error)
timeoutChan := time.After(time.Second * 5)
go func() {
id, err := system.GetDevID(dir)
if err != nil {
errChan <- err
return
}
idChan <- id
}()
select {
case err := <-errChan:
return 0, err
case id := <-idChan:
return id, nil
case <-timeoutChan:
return 0, context.DeadlineExceeded
}
}