This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
forked from juju/juju
/
tmpfs.go
259 lines (226 loc) · 7.66 KB
/
tmpfs.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provider
import (
"fmt"
"os"
"path/filepath"
"github.com/juju/errors"
"github.com/juju/names"
"github.com/juju/utils"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/storage"
)
const (
TmpfsProviderType = storage.ProviderType("tmpfs")
)
// tmpfsProviders create storage sources which provide access to filesystems.
type tmpfsProvider struct {
// run is a function type used for running commands on the local machine.
run runCommandFunc
}
var (
_ storage.Provider = (*tmpfsProvider)(nil)
)
// ValidateConfig is defined on the Provider interface.
func (p *tmpfsProvider) ValidateConfig(cfg *storage.Config) error {
// Tmpfs 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 *tmpfsProvider) 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 *tmpfsProvider) VolumeSource(environConfig *config.Config, providerConfig *storage.Config) (storage.VolumeSource, error) {
return nil, errors.NotSupportedf("volumes")
}
// FilesystemSource is defined on the Provider interface.
func (p *tmpfsProvider) 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 &tmpfsFilesystemSource{
&osDirFuncs{p.run},
p.run,
storageDir,
}, nil
}
// Supports is defined on the Provider interface.
func (*tmpfsProvider) Supports(k storage.StorageKind) bool {
return k == storage.StorageKindFilesystem
}
// Scope is defined on the Provider interface.
func (*tmpfsProvider) Scope() storage.Scope {
return storage.ScopeMachine
}
// Dynamic is defined on the Provider interface.
func (*tmpfsProvider) Dynamic() bool {
return true
}
type tmpfsFilesystemSource struct {
dirFuncs dirFuncs
run runCommandFunc
storageDir string
}
var _ storage.FilesystemSource = (*tmpfsFilesystemSource)(nil)
// ValidateFilesystemParams is defined on the FilesystemSource interface.
func (s *tmpfsFilesystemSource) 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 *tmpfsFilesystemSource) 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
}
var getpagesize = os.Getpagesize
func (s *tmpfsFilesystemSource) createFilesystem(params storage.FilesystemParams) (*storage.Filesystem, error) {
if err := s.ValidateFilesystemParams(params); err != nil {
return nil, errors.Trace(err)
}
// Align size to the page size in MiB.
sizeInMiB := params.Size
pageSizeInMiB := uint64(getpagesize()) / (1024 * 1024)
if pageSizeInMiB > 0 {
x := (sizeInMiB + pageSizeInMiB - 1)
sizeInMiB = x - x%pageSizeInMiB
}
info := storage.FilesystemInfo{
FilesystemId: params.Tag.String(),
Size: sizeInMiB,
}
// Creating the mount is the responsibility of AttachFilesystems.
// AttachFilesystems needs to know the size so it can pass it onto
// "mount"; write the size of the filesystem to a file in the
// storage directory.
if err := s.writeFilesystemInfo(params.Tag, info); err != nil {
return nil, err
}
return &storage.Filesystem{params.Tag, params.Volume, info}, nil
}
// DestroyFilesystems is defined on the FilesystemSource interface.
func (s *tmpfsFilesystemSource) DestroyFilesystems(filesystemIds []string) ([]error, error) {
// DestroyFilesystems is a no-op; there is nothing to destroy,
// since the filesystem is ephemeral and disappears once
// detached.
return make([]error, len(filesystemIds)), nil
}
// AttachFilesystems is defined on the FilesystemSource interface.
func (s *tmpfsFilesystemSource) 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 *tmpfsFilesystemSource) attachFilesystem(arg storage.FilesystemAttachmentParams) (*storage.FilesystemAttachment, error) {
path := arg.Path
if path == "" {
return nil, errNoMountPoint
}
info, err := s.readFilesystemInfo(arg.Filesystem)
if err != nil {
return nil, err
}
if err := ensureDir(s.dirFuncs, path); err != nil {
return nil, errors.Trace(err)
}
// Check if the mount already exists.
source, err := s.dirFuncs.mountPointSource(path)
if err != nil {
return nil, errors.Trace(err)
}
if source != arg.Filesystem.String() {
if err := ensureEmptyDir(s.dirFuncs, path); err != nil {
return nil, err
}
options := fmt.Sprintf("size=%dm", info.Size)
if arg.ReadOnly {
options += ",ro"
}
if _, err := s.run(
"mount", "-t", "tmpfs", arg.Filesystem.String(), path, "-o", options,
); err != nil {
os.Remove(path)
return nil, errors.Annotate(err, "cannot mount tmpfs")
}
}
return &storage.FilesystemAttachment{
arg.Filesystem,
arg.Machine,
storage.FilesystemAttachmentInfo{
Path: path,
ReadOnly: arg.ReadOnly,
},
}, nil
}
// DetachFilesystems is defined on the FilesystemSource interface.
func (s *tmpfsFilesystemSource) 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
}
func (s *tmpfsFilesystemSource) writeFilesystemInfo(tag names.FilesystemTag, info storage.FilesystemInfo) error {
filename := s.filesystemInfoFile(tag)
if _, err := os.Stat(filename); err == nil {
return errors.Errorf("filesystem %v already exists", tag.Id())
}
if err := ensureDir(s.dirFuncs, filepath.Dir(filename)); err != nil {
return errors.Trace(err)
}
err := utils.WriteYaml(filename, filesystemInfo{&info.Size})
if err != nil {
return errors.Annotate(err, "writing filesystem info to disk")
}
return err
}
func (s *tmpfsFilesystemSource) readFilesystemInfo(tag names.FilesystemTag) (storage.FilesystemInfo, error) {
var info filesystemInfo
if err := utils.ReadYaml(s.filesystemInfoFile(tag), &info); err != nil {
return storage.FilesystemInfo{}, errors.Annotate(err, "reading filesystem info from disk")
}
if info.Size == nil {
return storage.FilesystemInfo{}, errors.New("invalid filesystem info: missing size")
}
return storage.FilesystemInfo{
FilesystemId: tag.String(),
Size: *info.Size,
}, nil
}
func (s *tmpfsFilesystemSource) filesystemInfoFile(tag names.FilesystemTag) string {
return filepath.Join(s.storageDir, tag.Id()+".info")
}
type filesystemInfo struct {
Size *uint64 `yaml:"size,omitempty"`
}