-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathlinux.go
344 lines (288 loc) · 7.41 KB
/
linux.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// +build linux
/*
Package linux is the OS driver for linux. In order to reduce external
dependencies, this package borrows the following packages:
- github.com/docker/docker/pkg/mount
- github.com/opencontainers/runc/libcontainer/label
*/
package linux
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
log "github.com/Sirupsen/logrus"
gofigCore "github.com/akutz/gofig"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/goof"
"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/registry"
"github.com/codedellemc/libstorage/api/types"
)
const driverName = "linux"
var (
errUnknownOS = goof.New("unknown OS")
errUnknownFileSystem = goof.New("unknown file system")
errUnsupportedFileSystem = goof.New("unsupported file system")
)
func init() {
registry.RegisterOSDriver(driverName, newDriver)
r := gofigCore.NewRegistration("Linux")
r.Key(gofig.Int, "", 0700, "", "linux.volume.filemode")
r.Key(gofig.String, "", "/data", "", "linux.volume.rootpath")
gofigCore.Register(r)
}
type driver struct {
config gofig.Config
}
func newDriver() types.OSDriver {
return &driver{}
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
if runtime.GOOS != "linux" {
return errUnknownOS
}
d.config = config
return nil
}
func (d *driver) Name() string {
return driverName
}
func (d *driver) Mounts(
ctx types.Context,
deviceName, mountPoint string,
opts types.Store) ([]*types.MountInfo, error) {
mounts, err := d.getMounts(ctx, opts)
if err != nil {
return nil, err
}
if mountPoint == "" && deviceName == "" {
return mounts, nil
} else if mountPoint != "" && deviceName != "" {
return nil, goof.New("cannot specify mountPoint and deviceName")
}
matchedMounts := []*types.MountInfo{}
for _, m := range mounts {
if m.MountPoint == mountPoint || m.Source == deviceName {
matchedMounts = append(matchedMounts, m)
}
}
return matchedMounts, nil
}
func (d *driver) getMounts(
ctx types.Context,
opts types.Store) ([]*types.MountInfo, error) {
// see if we should use the executor?
if client, ok := context.Client(ctx); ok {
if _, ok := context.ServiceName(ctx); ok {
if client.Executor() != nil {
lsxSO, err := client.Executor().Supported(ctx, opts)
if err != nil {
return nil, err
}
if lsxSO.Mounts() {
return client.Executor().Mounts(ctx, opts)
}
}
}
}
return getMounts()
}
func (d *driver) Mount(
ctx types.Context,
deviceName, mountPoint string,
opts *types.DeviceMountOpts) error {
// see if we should use the executor?
if client, ok := context.Client(ctx); ok {
if _, ok := context.ServiceName(ctx); ok {
if client.Executor() != nil {
lsxSO, err := client.Executor().Supported(ctx, opts.Opts)
if err != nil {
return err
}
if lsxSO.Mount() {
if err := client.Executor().Mount(
ctx, deviceName, mountPoint, opts); err != nil {
return err
}
mp := d.volumeMountPath(mountPoint)
fm := d.fileModeMountPath()
os.MkdirAll(mp, fm)
os.Chmod(mp, fm)
return nil
}
}
}
}
if d.isNfsDevice(deviceName) {
if err := d.nfsMount(deviceName, mountPoint); err != nil {
return err
}
os.MkdirAll(d.volumeMountPath(mountPoint), d.fileModeMountPath())
os.Chmod(d.volumeMountPath(mountPoint), d.fileModeMountPath())
return nil
}
fsType := opts.FsType
if opts.FsType == "" {
var err error
fsType, err = probeFsType(deviceName)
if err != nil {
return err
}
}
options := fmt.Sprintf("%s,%s", opts.MountOptions, opts.MountLabel)
if fsType == "xfs" {
options = fmt.Sprintf("%s,nouuid", opts.MountLabel)
}
if err := mount(deviceName, mountPoint, fsType, options); err != nil {
return goof.WithFieldsE(goof.Fields{
"deviceName": deviceName,
"mountPoint": mountPoint,
}, "error mounting directory", err)
}
os.MkdirAll(d.volumeMountPath(mountPoint), d.fileModeMountPath())
os.Chmod(d.volumeMountPath(mountPoint), d.fileModeMountPath())
return nil
}
func (d *driver) Unmount(
ctx types.Context,
mountPoint string,
opts types.Store) error {
// see if we should use the executor?
if client, ok := context.Client(ctx); ok {
if _, ok := context.ServiceName(ctx); ok {
if client.Executor() != nil {
lsxSO, err := client.Executor().Supported(ctx, opts)
if err != nil {
return err
}
if lsxSO.Umount() {
return client.Executor().Unmount(ctx, mountPoint, opts)
}
}
}
}
return unmount(mountPoint)
}
func (d *driver) IsMounted(
ctx types.Context,
mountPoint string,
opts types.Store) (bool, error) {
mounts, err := d.getMounts(ctx, opts)
if err != nil {
return false, err
}
for _, m := range mounts {
if m.MountPoint == mountPoint {
return true, nil
}
}
return false, nil
}
func (d *driver) Format(
ctx types.Context,
deviceName string,
opts *types.DeviceFormatOpts) error {
fsType, err := probeFsType(deviceName)
if err != nil && err != errUnknownFileSystem {
return err
}
fsDetected := fsType != ""
ctx.WithFields(log.Fields{
"fsDetected": fsDetected,
"fsType": fsType,
"deviceName": deviceName,
"overwriteFs": opts.OverwriteFS,
"driverName": driverName}).Info("probe information")
if opts.OverwriteFS || !fsDetected {
switch opts.NewFSType {
case "ext4":
if err := exec.Command(
"mkfs.ext4", "-F", deviceName).Run(); err != nil {
return goof.WithFieldE(
"deviceName", deviceName,
"error creating filesystem",
err)
}
case "xfs":
if err := exec.Command(
"mkfs.xfs", "-f", deviceName).Run(); err != nil {
return goof.WithFieldE(
"deviceName", deviceName,
"error creating filesystem",
err)
}
default:
return errUnsupportedFileSystem
}
}
return nil
}
func (d *driver) isNfsDevice(device string) bool {
return strings.Contains(device, ":")
}
func (d *driver) nfsMount(device, target string) error {
command := exec.Command("mount", device, target)
output, err := command.CombinedOutput()
if err != nil {
return goof.WithError(fmt.Sprintf("failed mounting: %s", output), err)
}
return nil
}
func (d *driver) fileModeMountPath() (fileMode os.FileMode) {
return os.FileMode(d.volumeFileMode())
}
// from github.com/docker/docker/daemon/graphdriver/devmapper/
// this should be abstracted outside of graphdriver but within Docker package,
// here temporarily
type probeData struct {
fsName string
magic string
offset uint64
}
func probeFsType(device string) (string, error) {
probes := []probeData{
{"btrfs", "_BHRfS_M", 0x10040},
{"ext4", "\123\357", 0x438},
{"xfs", "XFSB", 0},
}
maxLen := uint64(0)
for _, p := range probes {
l := p.offset + uint64(len(p.magic))
if l > maxLen {
maxLen = l
}
}
file, err := os.Open(device)
if err != nil {
return "", err
}
defer file.Close()
buffer := make([]byte, maxLen)
l, err := file.Read(buffer)
if err != nil {
return "", err
}
if uint64(l) != maxLen {
return "", goof.WithField(
"device", device, "error detecting filesystem")
}
for _, p := range probes {
if bytes.Equal(
[]byte(p.magic), buffer[p.offset:p.offset+uint64(len(p.magic))]) {
return p.fsName, nil
}
}
return "", errUnknownFileSystem
}
func (d *driver) volumeMountPath(target string) string {
return fmt.Sprintf("%s%s", target, d.volumeRootPath())
}
func (d *driver) volumeFileMode() int {
return d.config.GetInt("linux.volume.filemode")
}
func (d *driver) volumeRootPath() string {
return d.config.GetString("linux.volume.rootpath")
}