/
loop.go
321 lines (290 loc) · 9.84 KB
/
loop.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provider
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/juju/errors"
"github.com/juju/names"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/storage"
)
const (
// Loop provider types.
LoopProviderType = storage.ProviderType("loop")
HostLoopProviderType = storage.ProviderType("hostloop")
)
// loopProviders create volume sources which use loop devices.
type loopProvider struct {
// run is a function used for running commands on the local machine.
run runCommandFunc
// runningInsideLXC is a function that determines whether or not
// the code is running within an LXC container.
runningInsideLXC func() (bool, error)
}
var _ storage.Provider = (*loopProvider)(nil)
// ValidateConfig is defined on the Provider interface.
func (*loopProvider) ValidateConfig(*storage.Config) error {
// Loop provider has no configuration.
return nil
}
// validateFullConfig validates a fully-constructed storage config,
// combining the user-specified config and any internally specified
// config.
func (lp *loopProvider) validateFullConfig(cfg *storage.Config) error {
if err := lp.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 (lp *loopProvider) VolumeSource(
environConfig *config.Config,
sourceConfig *storage.Config,
) (storage.VolumeSource, error) {
if err := lp.validateFullConfig(sourceConfig); err != nil {
return nil, err
}
insideLXC, err := lp.runningInsideLXC()
if err != nil {
return nil, err
}
// storageDir is validated by validateFullConfig.
storageDir, _ := sourceConfig.ValueString(storage.ConfigStorageDir)
return &loopVolumeSource{
&osDirFuncs{lp.run},
lp.run,
storageDir,
insideLXC,
}, nil
}
// FilesystemSource is defined on the Provider interface.
func (lp *loopProvider) FilesystemSource(
environConfig *config.Config,
providerConfig *storage.Config,
) (storage.FilesystemSource, error) {
return nil, errors.NotSupportedf("filesystems")
}
// Supports is defined on the Provider interface.
func (*loopProvider) Supports(k storage.StorageKind) bool {
return k == storage.StorageKindBlock
}
// Scope is defined on the Provider interface.
func (*loopProvider) Scope() storage.Scope {
return storage.ScopeMachine
}
// Dynamic is defined on the Provider interface.
func (*loopProvider) Dynamic() bool {
return true
}
// loopVolumeSource provides common functionality to handle
// loop devices for rootfs and host loop volume sources.
type loopVolumeSource struct {
dirFuncs dirFuncs
run runCommandFunc
storageDir string
runningInsideLXC bool
}
var _ storage.VolumeSource = (*loopVolumeSource)(nil)
// CreateVolumes is defined on the VolumeSource interface.
func (lvs *loopVolumeSource) CreateVolumes(args []storage.VolumeParams) ([]storage.Volume, []storage.VolumeAttachment, error) {
volumes := make([]storage.Volume, len(args))
for i, arg := range args {
volume, err := lvs.createVolume(arg)
if err != nil {
return nil, nil, errors.Annotate(err, "creating volume")
}
volumes[i] = volume
}
return volumes, nil, nil
}
func (lvs *loopVolumeSource) createVolume(params storage.VolumeParams) (storage.Volume, error) {
volumeId := params.Tag.String()
loopFilePath := lvs.volumeFilePath(params.Tag)
if err := ensureDir(lvs.dirFuncs, filepath.Dir(loopFilePath)); err != nil {
return storage.Volume{}, errors.Trace(err)
}
if err := createBlockFile(lvs.run, loopFilePath, params.Size); err != nil {
return storage.Volume{}, errors.Annotate(err, "could not create block file")
}
return storage.Volume{
params.Tag,
storage.VolumeInfo{
VolumeId: volumeId,
Size: params.Size,
// Loop devices may outlive LXC containers. If we're
// running inside an LXC container, mark the volume as
// persistent.
Persistent: lvs.runningInsideLXC,
},
}, nil
}
func (lvs *loopVolumeSource) volumeFilePath(tag names.VolumeTag) string {
return filepath.Join(lvs.storageDir, tag.String())
}
// DescribeVolumes is defined on the VolumeSource interface.
func (lvs *loopVolumeSource) DescribeVolumes(volumeIds []string) ([]storage.VolumeInfo, error) {
// TODO(axw) implement this when we need it.
return nil, errors.NotImplementedf("DescribeVolumes")
}
// DestroyVolumes is defined on the VolumeSource interface.
func (lvs *loopVolumeSource) DestroyVolumes(volumeIds []string) []error {
results := make([]error, len(volumeIds))
for i, volumeId := range volumeIds {
if err := lvs.destroyVolume(volumeId); err != nil {
results[i] = errors.Annotatef(err, "destroying %q", volumeId)
}
}
return results
}
func (lvs *loopVolumeSource) destroyVolume(volumeId string) error {
tag, err := names.ParseVolumeTag(volumeId)
if err != nil {
return errors.Errorf("invalid loop volume ID %q", volumeId)
}
loopFilePath := lvs.volumeFilePath(tag)
err = os.Remove(loopFilePath)
if err != nil && !os.IsNotExist(err) {
return errors.Annotate(err, "removing loop backing file")
}
return nil
}
// ValidateVolumeParams is defined on the VolumeSource interface.
func (lvs *loopVolumeSource) ValidateVolumeParams(params storage.VolumeParams) error {
// ValdiateVolumeParams may be called on a machine other than the
// machine where the loop device will be created, so we cannot check
// available size until we get to CreateVolumes.
return nil
}
// AttachVolumes is defined on the VolumeSource interface.
func (lvs *loopVolumeSource) AttachVolumes(args []storage.VolumeAttachmentParams) ([]storage.VolumeAttachment, error) {
attachments := make([]storage.VolumeAttachment, len(args))
for i, arg := range args {
attachment, err := lvs.attachVolume(arg)
if err != nil {
return nil, errors.Annotatef(err, "attaching volume %v", arg.Volume.Id())
}
attachments[i] = attachment
}
return attachments, nil
}
func (lvs *loopVolumeSource) attachVolume(arg storage.VolumeAttachmentParams) (storage.VolumeAttachment, error) {
loopFilePath := lvs.volumeFilePath(arg.Volume)
deviceName, err := attachLoopDevice(lvs.run, loopFilePath, arg.ReadOnly)
if err != nil {
os.Remove(loopFilePath)
return storage.VolumeAttachment{}, errors.Annotate(err, "attaching loop device")
}
return storage.VolumeAttachment{
arg.Volume,
arg.Machine,
storage.VolumeAttachmentInfo{
DeviceName: deviceName,
ReadOnly: arg.ReadOnly,
},
}, nil
}
// DetachVolumes is defined on the VolumeSource interface.
func (lvs *loopVolumeSource) DetachVolumes(args []storage.VolumeAttachmentParams) error {
for _, arg := range args {
if err := lvs.detachVolume(arg.Volume); err != nil {
return errors.Annotatef(err, "detaching volume %s", arg.Volume.Id())
}
}
return nil
}
func (lvs *loopVolumeSource) detachVolume(tag names.VolumeTag) error {
loopFilePath := lvs.volumeFilePath(tag)
deviceNames, err := associatedLoopDevices(lvs.run, loopFilePath)
if err != nil {
return errors.Annotate(err, "locating loop device")
}
if len(deviceNames) > 1 {
logger.Warningf("expected 1 loop device, got %d", len(deviceNames))
}
for _, deviceName := range deviceNames {
if err := detachLoopDevice(lvs.run, deviceName); err != nil {
return errors.Trace(err)
}
}
return nil
}
// createBlockFile creates a file at the specified path, with the
// given size in mebibytes.
func createBlockFile(run runCommandFunc, filePath string, sizeInMiB uint64) error {
// fallocate will reserve the space without actually writing to it.
_, err := run("fallocate", "-l", fmt.Sprintf("%dMiB", sizeInMiB), filePath)
if err != nil {
return errors.Annotatef(err, "allocating loop backing file %q", filePath)
}
return nil
}
// attachLoopDevice attaches a loop device to the file with the
// specified path, and returns the loop device's name (e.g. "loop0").
// losetup will create additional loop devices as necessary.
func attachLoopDevice(run runCommandFunc, filePath string, readOnly bool) (loopDeviceName string, _ error) {
devices, err := associatedLoopDevices(run, filePath)
if err != nil {
return "", err
}
if len(devices) > 0 {
// Already attached.
logger.Debugf("%s already attached to %s", filePath, devices)
return devices[0], nil
}
// -f automatically finds the first available loop-device.
// -r sets up a read-only loop-device.
// --show returns the loop device chosen on stdout.
args := []string{"-f", "--show"}
if readOnly {
args = append(args, "-r")
}
args = append(args, filePath)
stdout, err := run("losetup", args...)
if err != nil {
return "", errors.Annotatef(err, "attaching loop device to %q", filePath)
}
stdout = strings.TrimSpace(stdout)
loopDeviceName = stdout[len("/dev/"):]
return loopDeviceName, nil
}
// detachLoopDevice detaches the loop device with the specified name.
func detachLoopDevice(run runCommandFunc, deviceName string) error {
_, err := run("losetup", "-d", path.Join("/dev", deviceName))
if err != nil {
return errors.Annotatef(err, "detaching loop device %q", deviceName)
}
return err
}
// associatedLoopDevices returns the device names of the loop devices
// associated with the specified file path.
func associatedLoopDevices(run runCommandFunc, filePath string) ([]string, error) {
stdout, err := run("losetup", "-j", filePath)
if err != nil {
return nil, errors.Trace(err)
}
stdout = strings.TrimSpace(stdout)
if stdout == "" {
return nil, nil
}
// The output will be zero or more lines with the format:
// "/dev/loop0: [0021]:7504142 (/tmp/test.dat)"
lines := strings.Split(stdout, "\n")
deviceNames := make([]string, len(lines))
for i, line := range lines {
pos := strings.IndexRune(line, ':')
if pos == -1 {
return nil, errors.Errorf("unexpected output %q", line)
}
deviceName := line[:pos][len("/dev/"):]
deviceNames[i] = deviceName
}
return deviceNames, nil
}