Skip to content

Commit de076c9

Browse files
committed
count space taken on disk
1 parent d353305 commit de076c9

File tree

4 files changed

+52
-42
lines changed

4 files changed

+52
-42
lines changed

driver/driver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ func (d Driver) Get(req *v.GetRequest) (*v.GetResponse, error) {
215215
CreatedAt: fmt.Sprintf(vol.CreatedAt.Format(time.RFC3339)),
216216
Mountpoint: vol.MountPointPath,
217217
Status: map[string]interface{}{
218-
"size": strconv.FormatUint(vol.SizeInBytes, 10),
218+
"size-max": strconv.FormatUint(vol.MaxSizeInBytes, 10),
219+
"size-current": strconv.FormatUint(vol.CurrentSizeInBytes, 10),
219220
},
220221
}
221222

main.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ const (
1616
)
1717

1818
type config struct {
19-
StateDir string `arg:"--state-dir,env:STATE_DIR,help:dir used to keep track of currently mounted volumes"`
20-
DataDir string `arg:"--data-dir,env:DATA_DIR,help:dir used to store actual volume data"`
21-
MountDir string `arg:"--mount-dir,env:MOUNT_DIR,help:dir used to create mount-points"`
22-
DefaultSize string `arg:"--default-size,env:DEFAULT_SIZE,help:default size for volumes created"`
23-
Debug bool `arg:"env:DEBUG,help:enable debug logs"`
19+
StateDir string `arg:"--state-dir,env:STATE_DIR,help:dir used to keep track of currently mounted volumes"`
20+
DataDir string `arg:"--data-dir,env:DATA_DIR,help:dir used to store actual volume data"`
21+
MountDir string `arg:"--mount-dir,env:MOUNT_DIR,help:dir used to create mount-points"`
22+
DefaultSize string `arg:"--default-size,env:DEFAULT_SIZE,help:default size for volumes created"`
23+
Debug bool `arg:"env:DEBUG,help:enable debug logs"`
2424
}
2525

2626
var (
27-
logger = zerolog.New(os.Stdout)
28-
args = &config{
29-
StateDir: "/run/docker-volume-loopback",
30-
DataDir: "/var/lib/docker-volume-loopback",
31-
MountDir: "/mnt",
27+
logger = zerolog.New(os.Stdout)
28+
args = &config{
29+
StateDir: "/run/docker-volume-loopback",
30+
DataDir: "/var/lib/docker-volume-loopback",
31+
MountDir: "/mnt",
3232
DefaultSize: "1G",
33-
Debug: false,
33+
Debug: false,
3434
}
3535
)
3636

@@ -47,10 +47,10 @@ func main() {
4747
}
4848

4949
d, err := driver.NewDriver(driver.Config{
50-
StateDir: args.StateDir,
51-
DataDir: args.DataDir,
52-
MountDir: args.MountDir,
53-
DefaultSize: args.DefaultSize,
50+
StateDir: args.StateDir,
51+
DataDir: args.DataDir,
52+
MountDir: args.MountDir,
53+
DefaultSize: args.DefaultSize,
5454
})
5555
if err != nil {
5656
logger.Fatal().

manager/manager.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@ import (
1414

1515
var (
1616
NamePattern = `^[a-zA-Z0-9][\w\-]{1,250}$`
17-
NameRegex = regexp.MustCompile(NamePattern)
17+
NameRegex = regexp.MustCompile(NamePattern)
1818
)
1919

2020
type Manager struct {
21-
stateDir string
22-
dataDir string
23-
mountDir string
21+
stateDir string
22+
dataDir string
23+
mountDir string
2424
}
2525

2626
type Config struct {
27-
StateDir string
28-
DataDir string
29-
MountDir string
27+
StateDir string
28+
DataDir string
29+
MountDir string
3030
}
3131

32-
3332
func (m Manager) getVolume(name string) (vol Volume, err error) {
3433
volumeDataFilePath := filepath.Join(m.dataDir, name)
3534

@@ -49,20 +48,28 @@ func (m Manager) getVolume(name string) (vol Volume, err error) {
4948
return
5049
}
5150

51+
details, ok := volumeDataFileInfo.Sys().(*syscall.Stat_t)
52+
if !ok {
53+
err = errors.Errorf(
54+
"An issue occurred while retrieving details about volume '%s' - cannot stat '%s'",
55+
name, volumeDataFilePath)
56+
}
57+
5258
mountPointPath := filepath.Join(m.mountDir, name)
5359

5460
vol = Volume{
55-
Name: name,
56-
SizeInBytes: uint64(volumeDataFileInfo.Size()),
57-
StateDir: filepath.Join(m.stateDir, name),
58-
DataFilePath: volumeDataFilePath,
59-
MountPointPath: mountPointPath,
60-
CreatedAt: volumeDataFileInfo.ModTime(),
61+
Name: name,
62+
CurrentSizeInBytes: uint64(details.Blocks * 512),
63+
MaxSizeInBytes: uint64(details.Size),
64+
StateDir: filepath.Join(m.stateDir, name),
65+
DataFilePath: volumeDataFilePath,
66+
MountPointPath: mountPointPath,
67+
CreatedAt: volumeDataFileInfo.ModTime(),
6168
}
69+
6270
return
6371
}
6472

65-
6673
func New(cfg Config) (manager Manager, err error) {
6774
// state dir
6875
if cfg.StateDir == "" {
@@ -131,7 +138,6 @@ func (m Manager) List() ([]Volume, error) {
131138
return vols, nil
132139
}
133140

134-
135141
func (m Manager) Get(name string) (vol Volume, err error) {
136142
err = validateName(name)
137143
if err != nil {
@@ -145,7 +151,6 @@ func (m Manager) Get(name string) (vol Volume, err error) {
145151
return
146152
}
147153

148-
149154
func (m Manager) Create(name string, sizeInBytes int64, sparse bool, uid, gid int, mode uint32) error {
150155
err := validateName(name)
151156
if err != nil {
@@ -179,7 +184,9 @@ func (m Manager) Create(name string, sizeInBytes int64, sparse bool, uid, gid in
179184
name, dataFilePath)
180185
}
181186

187+
fmt.Println(fmt.Sprintf("--- %v", sparse))
182188
if sparse {
189+
fmt.Println(fmt.Sprintf("--- Creating as sparse"))
183190
err = dataFileInfo.Truncate(sizeInBytes)
184191
if err != nil {
185192
_ = os.Remove(dataFilePath) // attempt to cleanup
@@ -188,11 +195,13 @@ func (m Manager) Create(name string, sizeInBytes int64, sparse bool, uid, gid in
188195
name, sizeInBytes)
189196
}
190197
} else {
198+
fmt.Println(fmt.Sprintf("--- Creating as fallocate"))
191199
// Try using fallocate - super fast if data dir is on ext4 or xfs
192200
errBytes, err := exec.Command("fallocate", "-l", fmt.Sprint(sizeInBytes), dataFilePath).CombinedOutput()
193201

194202
// fallocate failed - either not enough space or unsupported FS
195203
if err != nil {
204+
fmt.Println(fmt.Sprintf("--- fallocate failed"))
196205
errStr := strings.TrimSpace(string(errBytes[:]))
197206

198207
// If there is not enough space then we just error out
@@ -209,7 +218,7 @@ func (m Manager) Create(name string, sizeInBytes int64, sparse bool, uid, gid in
209218
errBytes, err = exec.Command(
210219
"dd",
211220
"if=/dev/zero", of, fmt.Sprintf("bs=%d", bs), fmt.Sprintf("count=%d", count),
212-
).CombinedOutput()
221+
).CombinedOutput()
213222

214223
// Something went wrong - likely no space on an fallocate-incompatible FS
215224
if err != nil {
@@ -445,4 +454,4 @@ func validateName(name string) error {
445454
name, NamePattern)
446455
}
447456
return nil
448-
}
457+
}

manager/volume.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
)
99

1010
type Volume struct {
11-
Name string
12-
SizeInBytes uint64
13-
StateDir string
14-
DataFilePath string
15-
MountPointPath string
16-
CreatedAt time.Time
17-
11+
Name string
12+
CurrentSizeInBytes uint64
13+
MaxSizeInBytes uint64
14+
StateDir string
15+
DataFilePath string
16+
MountPointPath string
17+
CreatedAt time.Time
1818
}
1919

2020
func (v Volume) IsMounted() (mounted bool, err error) {

0 commit comments

Comments
 (0)