@@ -14,22 +14,21 @@ import (
14
14
15
15
var (
16
16
NamePattern = `^[a-zA-Z0-9][\w\-]{1,250}$`
17
- NameRegex = regexp .MustCompile (NamePattern )
17
+ NameRegex = regexp .MustCompile (NamePattern )
18
18
)
19
19
20
20
type Manager struct {
21
- stateDir string
22
- dataDir string
23
- mountDir string
21
+ stateDir string
22
+ dataDir string
23
+ mountDir string
24
24
}
25
25
26
26
type Config struct {
27
- StateDir string
28
- DataDir string
29
- MountDir string
27
+ StateDir string
28
+ DataDir string
29
+ MountDir string
30
30
}
31
31
32
-
33
32
func (m Manager ) getVolume (name string ) (vol Volume , err error ) {
34
33
volumeDataFilePath := filepath .Join (m .dataDir , name )
35
34
@@ -49,20 +48,28 @@ func (m Manager) getVolume(name string) (vol Volume, err error) {
49
48
return
50
49
}
51
50
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
+
52
58
mountPointPath := filepath .Join (m .mountDir , name )
53
59
54
60
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 (),
61
68
}
69
+
62
70
return
63
71
}
64
72
65
-
66
73
func New (cfg Config ) (manager Manager , err error ) {
67
74
// state dir
68
75
if cfg .StateDir == "" {
@@ -131,7 +138,6 @@ func (m Manager) List() ([]Volume, error) {
131
138
return vols , nil
132
139
}
133
140
134
-
135
141
func (m Manager ) Get (name string ) (vol Volume , err error ) {
136
142
err = validateName (name )
137
143
if err != nil {
@@ -145,7 +151,6 @@ func (m Manager) Get(name string) (vol Volume, err error) {
145
151
return
146
152
}
147
153
148
-
149
154
func (m Manager ) Create (name string , sizeInBytes int64 , sparse bool , uid , gid int , mode uint32 ) error {
150
155
err := validateName (name )
151
156
if err != nil {
@@ -179,7 +184,9 @@ func (m Manager) Create(name string, sizeInBytes int64, sparse bool, uid, gid in
179
184
name , dataFilePath )
180
185
}
181
186
187
+ fmt .Println (fmt .Sprintf ("--- %v" , sparse ))
182
188
if sparse {
189
+ fmt .Println (fmt .Sprintf ("--- Creating as sparse" ))
183
190
err = dataFileInfo .Truncate (sizeInBytes )
184
191
if err != nil {
185
192
_ = os .Remove (dataFilePath ) // attempt to cleanup
@@ -188,11 +195,13 @@ func (m Manager) Create(name string, sizeInBytes int64, sparse bool, uid, gid in
188
195
name , sizeInBytes )
189
196
}
190
197
} else {
198
+ fmt .Println (fmt .Sprintf ("--- Creating as fallocate" ))
191
199
// Try using fallocate - super fast if data dir is on ext4 or xfs
192
200
errBytes , err := exec .Command ("fallocate" , "-l" , fmt .Sprint (sizeInBytes ), dataFilePath ).CombinedOutput ()
193
201
194
202
// fallocate failed - either not enough space or unsupported FS
195
203
if err != nil {
204
+ fmt .Println (fmt .Sprintf ("--- fallocate failed" ))
196
205
errStr := strings .TrimSpace (string (errBytes [:]))
197
206
198
207
// 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
209
218
errBytes , err = exec .Command (
210
219
"dd" ,
211
220
"if=/dev/zero" , of , fmt .Sprintf ("bs=%d" , bs ), fmt .Sprintf ("count=%d" , count ),
212
- ).CombinedOutput ()
221
+ ).CombinedOutput ()
213
222
214
223
// Something went wrong - likely no space on an fallocate-incompatible FS
215
224
if err != nil {
@@ -445,4 +454,4 @@ func validateName(name string) error {
445
454
name , NamePattern )
446
455
}
447
456
return nil
448
- }
457
+ }
0 commit comments