/
driver_cephfs.go
460 lines (388 loc) · 12.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package drivers
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/lxc/incus/v6/internal/linux"
"github.com/lxc/incus/v6/internal/migration"
"github.com/lxc/incus/v6/internal/revert"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
localMigration "github.com/lxc/incus/v6/internal/server/migration"
"github.com/lxc/incus/v6/internal/server/operations"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/subprocess"
"github.com/lxc/incus/v6/shared/util"
"github.com/lxc/incus/v6/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 := subprocess.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,
DefaultVMBlockFilesystemSize: deviceConfig.DefaultVMBlockFilesystemSize,
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 {
revert := revert.New()
defer revert.Fail()
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]
}
// If the filesystem already exists, disallow keys associated to creating the filesystem.
fsExists, err := d.fsExists(d.config["cephfs.cluster_name"], d.config["cephfs.user.name"], fsName)
if err != nil {
return fmt.Errorf("Failed to check if %q CephFS exists: %w", fsName, err)
}
if fsExists {
for _, key := range []string{"create_missing", "osd_pg_num", "meta_pool", "data_pool"} {
cephfsSourceKey := fmt.Sprintf("cephfs.%s", key)
if d.config[cephfsSourceKey] != "" {
return fmt.Errorf("Invalid config key %q: CephFS filesystem already exists", cephfsSourceKey)
}
}
} else {
createMissing := util.IsTrue(d.config["cephfs.create_missing"])
if !createMissing {
return fmt.Errorf("The requested %q CephFS doesn't exist", fsName)
}
// Set the pg_num to 32 because we need to specify something, but ceph will automatically change it if necessary.
pgNum := d.config["cephfs.osd_pg_num"]
if pgNum == "" {
d.config["cephfs.osd_pg_num"] = "32"
}
// Create the meta and data pools if necessary.
for _, key := range []string{"cephfs.meta_pool", "cephfs.data_pool"} {
pool := d.config[key]
if pool == "" {
return fmt.Errorf("Missing required key %q for creating cephfs osd pool", key)
}
osdPoolExists, err := d.osdPoolExists(d.config["cephfs.cluster_name"], d.config["cephfs.user.name"], pool)
if err != nil {
return fmt.Errorf("Failed to check if %q OSD Pool exists: %w", pool, err)
}
if !osdPoolExists {
// Create new osd pool.
_, err := subprocess.RunCommand("ceph",
"--name", fmt.Sprintf("client.%s", d.config["cephfs.user.name"]),
"--cluster", d.config["cephfs.cluster_name"],
"osd",
"pool",
"create",
pool,
d.config["cephfs.osd_pg_num"],
)
if err != nil {
return fmt.Errorf("Failed to create ceph OSD pool %q: %w", pool, err)
}
revert.Add(func() {
// Delete the OSD pool.
_, _ = subprocess.RunCommand("ceph",
"--name", fmt.Sprintf("client.%s", d.config["cephfs.user.name"]),
"--cluster", d.config["cephfs.cluster_name"],
"osd",
"pool",
"delete",
pool,
pool,
"--yes-i-really-really-mean-it",
)
})
}
}
// Create the filesystem.
_, err := subprocess.RunCommand("ceph",
"--name", fmt.Sprintf("client.%s", d.config["cephfs.user.name"]),
"--cluster", d.config["cephfs.cluster_name"],
"fs",
"new",
fsName,
d.config["cephfs.meta_pool"],
d.config["cephfs.data_pool"],
)
if err != nil {
return fmt.Errorf("Failed to create CephFS %q: %w", fsName, err)
}
revert.Add(func() {
// Set the FS to fail so that we can remove it.
_, _ = subprocess.RunCommand("ceph",
"--name", fmt.Sprintf("client.%s", d.config["cephfs.user.name"]),
"--cluster", d.config["cephfs.cluster_name"],
"fs",
"fail",
fsName,
)
// Delete the FS.
_, _ = subprocess.RunCommand("ceph",
"--name", fmt.Sprintf("client.%s", d.config["cephfs.user.name"]),
"--cluster", d.config["cephfs.cluster_name"],
"fs",
"rm",
fsName,
"--yes-i-really-mean-it",
)
})
}
// Create a temporary mountpoint.
mountPath, err := os.MkdirTemp("", "incus_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, _ := internalUtil.PathIsEmpty(filepath.Join(mountPoint, fsPath))
if !ok {
return fmt.Errorf("Only empty CephFS paths can be used as a storage pool")
}
revert.Success()
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("", "incus_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 util.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,
"cephfs.create_missing": validate.Optional(validate.IsBool),
"cephfs.osd_pg_num": validate.Optional(validate.IsInt64),
"cephfs.meta_pool": validate.IsAny,
"cephfs.data_pool": 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 linux.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 util.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) []localMigration.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 util.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 []localMigration.Type{
{
FSType: migration.MigrationFSType_RSYNC,
Features: rsyncFeatures,
},
}
}