-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
driver_cephfs.go
345 lines (285 loc) · 9.25 KB
/
driver_cephfs.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
345
package drivers
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/lxc/lxd/lxd/migration"
"github.com/lxc/lxd/lxd/operations"
"github.com/lxc/lxd/lxd/storage/filesystem"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/validate"
)
var cephfsVersion string
var cephfsLoaded bool
type cephfs struct {
common
}
// load is used to run one-time action per-driver rather than per-pool.
func (d *cephfs) load() error {
// Register the patches.
d.patches = map[string]func() error{
"storage_lvm_skipactivation": nil,
"storage_missing_snapshot_records": nil,
"storage_delete_old_snapshot_records": nil,
"storage_zfs_drop_block_volume_filesystem_extension": nil,
"storage_prefix_bucket_names_with_project": nil,
}
// Done if previously loaded.
if cephfsLoaded {
return nil
}
// Validate the required binaries.
for _, tool := range []string{"ceph", "rbd"} {
_, err := exec.LookPath(tool)
if err != nil {
return fmt.Errorf("Required tool '%s' is missing", tool)
}
}
// Detect and record the version.
if cephfsVersion == "" {
out, err := shared.RunCommand("rbd", "--version")
if err != nil {
return err
}
out = strings.TrimSpace(out)
fields := strings.Split(out, " ")
if strings.HasPrefix(out, "ceph version ") && len(fields) > 2 {
cephfsVersion = fields[2]
} else {
cephfsVersion = out
}
}
cephfsLoaded = true
return nil
}
// isRemote returns true indicating this driver uses remote storage.
func (d *cephfs) isRemote() bool {
return true
}
// Info returns the pool driver information.
func (d *cephfs) Info() Info {
return Info{
Name: "cephfs",
Version: cephfsVersion,
OptimizedImages: false,
PreservesInodes: false,
Remote: d.isRemote(),
VolumeTypes: []VolumeType{VolumeTypeCustom},
VolumeMultiNode: true,
BlockBacking: false,
RunningCopyFreeze: false,
DirectIO: true,
MountedRoot: true,
}
}
// FillConfig populates the storage pool's configuration file with the default values.
func (d *cephfs) FillConfig() error {
if d.config["cephfs.cluster_name"] == "" {
d.config["cephfs.cluster_name"] = CephDefaultCluster
}
if d.config["cephfs.user.name"] == "" {
d.config["cephfs.user.name"] = CephDefaultUser
}
return nil
}
// Create is called during pool creation and is effectively using an empty driver struct.
// WARNING: The Create() function cannot rely on any of the struct attributes being set.
func (d *cephfs) Create() error {
err := d.FillConfig()
if err != nil {
return err
}
// Config validation.
if d.config["source"] == "" {
return fmt.Errorf("Missing required source name/path")
}
if d.config["cephfs.path"] != "" && d.config["cephfs.path"] != d.config["source"] {
return fmt.Errorf("cephfs.path must match the source")
}
d.config["cephfs.path"] = d.config["source"]
// Parse the namespace / path.
fields := strings.SplitN(d.config["cephfs.path"], "/", 2)
fsName := fields[0]
fsPath := "/"
if len(fields) > 1 {
fsPath = fields[1]
}
// Check that the filesystem exists.
if !d.fsExists(d.config["cephfs.cluster_name"], d.config["cephfs.user.name"], fsName) {
return fmt.Errorf("The requested '%v' CEPHFS doesn't exist", fsName)
}
// Create a temporary mountpoint.
mountPath, err := os.MkdirTemp("", "lxd_cephfs_")
if err != nil {
return fmt.Errorf("Failed to create temporary directory under: %w", err)
}
defer func() { _ = os.RemoveAll(mountPath) }()
err = os.Chmod(mountPath, 0700)
if err != nil {
return fmt.Errorf("Failed to chmod '%s': %w", mountPath, err)
}
mountPoint := filepath.Join(mountPath, "mount")
err = os.Mkdir(mountPoint, 0700)
if err != nil {
return fmt.Errorf("Failed to create directory '%s': %w", mountPoint, err)
}
// Get the credentials and host.
monAddresses, userSecret, err := d.getConfig(d.config["cephfs.cluster_name"], d.config["cephfs.user.name"])
if err != nil {
return err
}
// Mount the pool.
srcPath := strings.Join(monAddresses, ",") + ":/"
err = TryMount(srcPath, mountPoint, "ceph", 0, fmt.Sprintf("name=%v,secret=%v,mds_namespace=%v", d.config["cephfs.user.name"], userSecret, fsName))
if err != nil {
return err
}
defer func() { _, _ = forceUnmount(mountPoint) }()
// Create the path if missing.
err = os.MkdirAll(filepath.Join(mountPoint, fsPath), 0755)
if err != nil {
return fmt.Errorf("Failed to create directory '%s': %w", filepath.Join(mountPoint, fsPath), err)
}
// Check that the existing path is empty.
ok, _ := shared.PathIsEmpty(filepath.Join(mountPoint, fsPath))
if !ok {
return fmt.Errorf("Only empty CEPHFS paths can be used as a LXD storage pool")
}
return nil
}
// Delete clears any local and remote data related to this driver instance.
func (d *cephfs) Delete(op *operations.Operation) error {
// Parse the namespace / path.
fields := strings.SplitN(d.config["cephfs.path"], "/", 2)
fsName := fields[0]
fsPath := "/"
if len(fields) > 1 {
fsPath = fields[1]
}
// Create a temporary mountpoint.
mountPath, err := os.MkdirTemp("", "lxd_cephfs_")
if err != nil {
return fmt.Errorf("Failed to create temporary directory under: %w", err)
}
defer func() { _ = os.RemoveAll(mountPath) }()
err = os.Chmod(mountPath, 0700)
if err != nil {
return fmt.Errorf("Failed to chmod '%s': %w", mountPath, err)
}
mountPoint := filepath.Join(mountPath, "mount")
err = os.Mkdir(mountPoint, 0700)
if err != nil {
return fmt.Errorf("Failed to create directory '%s': %w", mountPoint, err)
}
// Get the credentials and host.
monAddresses, userSecret, err := d.getConfig(d.config["cephfs.cluster_name"], d.config["cephfs.user.name"])
if err != nil {
return err
}
// Mount the pool.
srcPath := strings.Join(monAddresses, ",") + ":/"
err = TryMount(srcPath, mountPoint, "ceph", 0, fmt.Sprintf("name=%v,secret=%v,mds_namespace=%v", d.config["cephfs.user.name"], userSecret, fsName))
if err != nil {
return err
}
defer func() { _, _ = forceUnmount(mountPoint) }()
// On delete, wipe everything in the directory.
err = wipeDirectory(GetPoolMountPath(d.name))
if err != nil {
return err
}
// Delete the pool from the parent.
if shared.PathExists(filepath.Join(mountPoint, fsPath)) {
// Delete the path itself.
if fsPath != "" && fsPath != "/" {
err = os.Remove(filepath.Join(mountPoint, fsPath))
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Failed to remove directory '%s': %w", filepath.Join(mountPoint, fsPath), err)
}
}
}
// Make sure the existing pool is unmounted.
_, err = d.Unmount()
if err != nil {
return err
}
return nil
}
// Validate checks that all provide keys are supported and that no conflicting or missing configuration is present.
func (d *cephfs) Validate(config map[string]string) error {
rules := map[string]func(value string) error{
"cephfs.cluster_name": validate.IsAny,
"cephfs.fscache": validate.Optional(validate.IsBool),
"cephfs.path": validate.IsAny,
"cephfs.user.name": validate.IsAny,
"volatile.pool.pristine": validate.IsAny,
}
return d.validatePool(config, rules, nil)
}
// Update applies any driver changes required from a configuration change.
func (d *cephfs) Update(changedConfig map[string]string) error {
return nil
}
// Mount brings up the driver and sets it up to be used.
func (d *cephfs) Mount() (bool, error) {
// Check if already mounted.
if filesystem.IsMountPoint(GetPoolMountPath(d.name)) {
return false, nil
}
// Parse the namespace / path.
fields := strings.SplitN(d.config["cephfs.path"], "/", 2)
fsName := fields[0]
fsPath := ""
if len(fields) > 1 {
fsPath = fields[1]
}
// Get the credentials and host.
monAddresses, userSecret, err := d.getConfig(d.config["cephfs.cluster_name"], d.config["cephfs.user.name"])
if err != nil {
return false, err
}
// Mount options.
options := fmt.Sprintf("name=%s,secret=%s,mds_namespace=%s", d.config["cephfs.user.name"], userSecret, fsName)
if shared.IsTrue(d.config["cephfs.fscache"]) {
options += ",fsc"
}
// Mount the pool.
srcPath := strings.Join(monAddresses, ",") + ":/" + fsPath
err = TryMount(srcPath, GetPoolMountPath(d.name), "ceph", 0, options)
if err != nil {
return false, err
}
return true, nil
}
// Unmount clears any of the runtime state of the driver.
func (d *cephfs) Unmount() (bool, error) {
return forceUnmount(GetPoolMountPath(d.name))
}
// GetResources returns the pool resource usage information.
func (d *cephfs) GetResources() (*api.ResourcesStoragePool, error) {
return genericVFSGetResources(d)
}
// MigrationTypes returns the supported migration types and options supported by the driver.
func (d *cephfs) MigrationTypes(contentType ContentType, refresh bool, copySnapshots bool) []migration.Type {
var rsyncFeatures []string
// Do not pass compression argument to rsync if the associated
// config key, that is rsync.compression, is set to false.
if shared.IsFalse(d.Config()["rsync.compression"]) {
rsyncFeatures = []string{"delete", "bidirectional"}
} else {
rsyncFeatures = []string{"delete", "compress", "bidirectional"}
}
if contentType != ContentTypeFS {
return nil
}
// Do not support xattr transfer on cephfs
return []migration.Type{
{
FSType: migration.MigrationFSType_RSYNC,
Features: rsyncFeatures,
},
}
}