/
rootfs.go
310 lines (278 loc) · 9.08 KB
/
rootfs.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provider
import (
"os"
"path/filepath"
"github.com/juju/errors"
"github.com/juju/names"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/storage"
)
const (
RootfsProviderType = storage.ProviderType("rootfs")
)
// rootfsProviders create storage sources which provide access to filesystems.
type rootfsProvider struct {
// run is a function type used for running commands on the local machine.
run runCommandFunc
}
var (
_ storage.Provider = (*rootfsProvider)(nil)
)
// ValidateConfig is defined on the Provider interface.
func (p *rootfsProvider) ValidateConfig(cfg *storage.Config) error {
// Rootfs provider has no configuration.
return nil
}
// validateFullConfig validates a fully-constructed storage config,
// combining the user-specified config and any internally specified
// config.
func (p *rootfsProvider) validateFullConfig(cfg *storage.Config) error {
if err := p.ValidateConfig(cfg); err != nil {
return err
}
storageDir, ok := cfg.ValueString(storage.ConfigStorageDir)
if !ok || storageDir == "" {
return errors.New("storage directory not specified")
}
return nil
}
// VolumeSource is defined on the Provider interface.
func (p *rootfsProvider) VolumeSource(environConfig *config.Config, providerConfig *storage.Config) (storage.VolumeSource, error) {
return nil, errors.NotSupportedf("volumes")
}
// FilesystemSource is defined on the Provider interface.
func (p *rootfsProvider) FilesystemSource(environConfig *config.Config, sourceConfig *storage.Config) (storage.FilesystemSource, error) {
if err := p.validateFullConfig(sourceConfig); err != nil {
return nil, err
}
// storageDir is validated by validateFullConfig.
storageDir, _ := sourceConfig.ValueString(storage.ConfigStorageDir)
return &rootfsFilesystemSource{
&osDirFuncs{p.run},
p.run,
storageDir,
}, nil
}
// Supports is defined on the Provider interface.
func (*rootfsProvider) Supports(k storage.StorageKind) bool {
return k == storage.StorageKindFilesystem
}
// Scope is defined on the Provider interface.
func (*rootfsProvider) Scope() storage.Scope {
return storage.ScopeMachine
}
// Dynamic is defined on the Provider interface.
func (*rootfsProvider) Dynamic() bool {
return true
}
type rootfsFilesystemSource struct {
dirFuncs dirFuncs
run runCommandFunc
storageDir string
}
// ensureDir ensures the specified path is a directory, or
// if it does not exist, that a directory can be created there.
func ensureDir(d dirFuncs, path string) error {
// If path already exists, we check that it is empty.
// It is up to the storage provisioner to ensure that any
// shared storage constraints and attachments with the same
// path are validated etc. So the check here is more a sanity check.
fi, err := d.lstat(path)
if err == nil {
if !fi.IsDir() {
return errors.Errorf("path %q must be a directory", path)
}
return nil
}
if !os.IsNotExist(err) {
return errors.Trace(err)
}
if err := d.mkDirAll(path, 0755); err != nil {
return errors.Annotate(err, "could not create directory")
}
return nil
}
// ensureEmptyDir ensures the specified directory is empty.
func ensureEmptyDir(d dirFuncs, path string) error {
fileCount, err := d.fileCount(path)
if err != nil {
return errors.Annotate(err, "could not read directory")
}
if fileCount > 0 {
return errors.Errorf("%q is not empty", path)
}
return nil
}
var _ storage.FilesystemSource = (*rootfsFilesystemSource)(nil)
// ValidateFilesystemParams is defined on the FilesystemSource interface.
func (s *rootfsFilesystemSource) ValidateFilesystemParams(params storage.FilesystemParams) error {
// ValidateFilesystemParams may be called on a machine other than the
// machine where the filesystem will be mounted, so we cannot check
// available size until we get to CreateFilesystem.
return nil
}
// CreateFilesystems is defined on the FilesystemSource interface.
func (s *rootfsFilesystemSource) CreateFilesystems(args []storage.FilesystemParams) ([]storage.CreateFilesystemsResult, error) {
results := make([]storage.CreateFilesystemsResult, len(args))
for i, arg := range args {
filesystem, err := s.createFilesystem(arg)
if err != nil {
results[i].Error = err
continue
}
results[i].Filesystem = filesystem
}
return results, nil
}
func (s *rootfsFilesystemSource) createFilesystem(params storage.FilesystemParams) (*storage.Filesystem, error) {
if err := s.ValidateFilesystemParams(params); err != nil {
return nil, errors.Trace(err)
}
path := filepath.Join(s.storageDir, params.Tag.Id())
if err := ensureDir(s.dirFuncs, path); err != nil {
return nil, errors.Trace(err)
}
if err := ensureEmptyDir(s.dirFuncs, path); err != nil {
return nil, errors.Trace(err)
}
sizeInMiB, err := s.dirFuncs.calculateSize(s.storageDir)
if err != nil {
os.Remove(path)
return nil, errors.Trace(err)
}
if sizeInMiB < params.Size {
os.Remove(path)
return nil, errors.Errorf("filesystem is not big enough (%dM < %dM)", sizeInMiB, params.Size)
}
return &storage.Filesystem{
params.Tag,
names.VolumeTag{},
storage.FilesystemInfo{
FilesystemId: params.Tag.Id(),
Size: sizeInMiB,
},
}, nil
}
// DestroyFilesystems is defined on the FilesystemSource interface.
func (s *rootfsFilesystemSource) DestroyFilesystems(filesystemIds []string) ([]error, error) {
// DestroyFilesystems is a no-op; we leave the storage directory
// in tact for post-mortems and such.
return make([]error, len(filesystemIds)), nil
}
// AttachFilesystems is defined on the FilesystemSource interface.
func (s *rootfsFilesystemSource) AttachFilesystems(args []storage.FilesystemAttachmentParams) ([]storage.AttachFilesystemsResult, error) {
results := make([]storage.AttachFilesystemsResult, len(args))
for i, arg := range args {
attachment, err := s.attachFilesystem(arg)
if err != nil {
results[i].Error = err
continue
}
results[i].FilesystemAttachment = attachment
}
return results, nil
}
func (s *rootfsFilesystemSource) attachFilesystem(arg storage.FilesystemAttachmentParams) (*storage.FilesystemAttachment, error) {
mountPoint := arg.Path
if mountPoint == "" {
return nil, errNoMountPoint
}
// The filesystem is created at <storage-dir>/<storage-id>.
// If it is different to the attachment path, bind mount.
if err := s.mount(arg.Filesystem, mountPoint); err != nil {
return nil, err
}
return &storage.FilesystemAttachment{
arg.Filesystem,
arg.Machine,
storage.FilesystemAttachmentInfo{
Path: mountPoint,
},
}, nil
}
func (s *rootfsFilesystemSource) mount(tag names.FilesystemTag, target string) error {
fsPath := filepath.Join(s.storageDir, tag.Id())
if target == fsPath {
return nil
}
logger.Debugf("mounting filesystem %q at %q", fsPath, target)
if err := ensureDir(s.dirFuncs, target); err != nil {
return errors.Trace(err)
}
mounted, err := s.tryBindMount(fsPath, target)
if err != nil {
return errors.Trace(err)
}
if mounted {
return nil
}
// We couldn't bind-mount over the designated directory;
// carry on and check if it's on the same filesystem. If
// it is, and it's empty, then claim it as our own.
if err := s.validateSameMountPoints(fsPath, target); err != nil {
return err
}
// The first time we try to take the existing directory, we'll
// ensure that it's empty and create a file to "claim" it.
// Future attachments will simply ensure that the claim file
// exists.
targetClaimPath := filepath.Join(fsPath, "juju-target-claimed")
_, err = s.dirFuncs.lstat(targetClaimPath)
if err == nil {
return nil
} else if !os.IsNotExist(err) {
return errors.Trace(err)
}
if err := ensureEmptyDir(s.dirFuncs, target); err != nil {
return errors.Trace(err)
}
if err := s.dirFuncs.mkDirAll(targetClaimPath, 0755); err != nil {
return errors.Annotate(err, "writing claim file")
}
return nil
}
func (s *rootfsFilesystemSource) tryBindMount(source, target string) (bool, error) {
targetSource, err := s.dirFuncs.mountPointSource(target)
if err != nil {
return false, errors.Annotate(err, "getting target mount-point source")
}
if targetSource == source {
// Already bind mounted.
return true, nil
}
if err := s.dirFuncs.bindMount(source, target); err != nil {
logger.Debugf("cannot bind-mount: %v", err)
} else {
return true, nil
}
return false, nil
}
func (s *rootfsFilesystemSource) validateSameMountPoints(source, target string) error {
sourceMountPoint, err := s.dirFuncs.mountPoint(source)
if err != nil {
return errors.Trace(err)
}
targetMountPoint, err := s.dirFuncs.mountPoint(target)
if err != nil {
return errors.Trace(err)
}
if sourceMountPoint != targetMountPoint {
return errors.Errorf(
"%q (%q) and %q (%q) are on different filesystems",
source, sourceMountPoint, target, targetMountPoint,
)
}
return nil
}
// DetachFilesystems is defined on the FilesystemSource interface.
func (s *rootfsFilesystemSource) DetachFilesystems(args []storage.FilesystemAttachmentParams) ([]error, error) {
results := make([]error, len(args))
for i, arg := range args {
if err := maybeUnmount(s.run, s.dirFuncs, arg.Path); err != nil {
results[i] = err
}
}
return results, nil
}