/
driver_btrfs.go
541 lines (454 loc) · 13.9 KB
/
driver_btrfs.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package drivers
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"golang.org/x/sys/unix"
"github.com/lxc/lxd/lxd/migration"
"github.com/lxc/lxd/lxd/operations"
"github.com/lxc/lxd/lxd/revert"
"github.com/lxc/lxd/lxd/storage/filesystem"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/units"
"github.com/lxc/lxd/shared/validate"
"github.com/lxc/lxd/shared/version"
)
var btrfsVersion string
var btrfsLoaded bool
var btrfsPropertyForce bool
type btrfs struct {
common
}
// load is used to run one-time action per-driver rather than per-pool.
func (d *btrfs) 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 btrfsLoaded {
return nil
}
// Validate the required binaries.
for _, tool := range []string{"btrfs"} {
_, err := exec.LookPath(tool)
if err != nil {
return fmt.Errorf("Required tool %q is missing", tool)
}
}
// Detect and record the version.
if btrfsVersion == "" {
out, err := shared.RunCommand("btrfs", "version")
if err != nil {
return err
}
count, err := fmt.Sscanf(strings.SplitN(out, " ", 2)[1], "v%s\n", &btrfsVersion)
if err != nil || count != 1 {
return fmt.Errorf("The 'btrfs' tool isn't working properly")
}
}
// Check if we need --force to set properties.
ver5142, err := version.Parse("5.14.2")
if err != nil {
return err
}
ourVer, err := version.Parse(btrfsVersion)
if err != nil {
return err
}
// If running 5.14.2 or older, we need --force.
if ourVer.Compare(ver5142) > 0 {
btrfsPropertyForce = true
}
btrfsLoaded = true
return nil
}
// Info returns info about the driver and its environment.
func (d *btrfs) Info() Info {
return Info{
Name: "btrfs",
Version: btrfsVersion,
OptimizedImages: true,
OptimizedBackups: true,
OptimizedBackupHeader: true,
PreservesInodes: !d.state.OS.RunningInUserNS,
Remote: d.isRemote(),
VolumeTypes: []VolumeType{VolumeTypeBucket, VolumeTypeCustom, VolumeTypeImage, VolumeTypeContainer, VolumeTypeVM},
BlockBacking: false,
RunningCopyFreeze: false,
DirectIO: true,
IOUring: true,
MountedRoot: true,
Buckets: true,
}
}
// FillConfig populates the storage pool's configuration file with the default values.
func (d *btrfs) FillConfig() error {
loopPath := loopFilePath(d.name)
if d.config["source"] == "" || d.config["source"] == loopPath {
// Pick a default size of the loop file if not specified.
if d.config["size"] == "" {
defaultSize, err := loopFileSizeDefault()
if err != nil {
return err
}
d.config["size"] = fmt.Sprintf("%dGiB", defaultSize)
}
} else {
// Unset size property since it's irrelevant.
d.config["size"] = ""
}
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 *btrfs) Create() error {
// Store the provided source as we are likely to be mangling it.
d.config["volatile.initial_source"] = d.config["source"]
revert := revert.New()
defer revert.Fail()
err := d.FillConfig()
if err != nil {
return err
}
loopPath := loopFilePath(d.name)
if d.config["source"] == "" || d.config["source"] == loopPath {
// Create a loop based pool.
d.config["source"] = loopPath
// Create the loop file itself.
size, err := units.ParseByteSizeString(d.config["size"])
if err != nil {
return err
}
err = ensureSparseFile(d.config["source"], size)
if err != nil {
return fmt.Errorf("Failed to create the sparse file: %w", err)
}
revert.Add(func() { _ = os.Remove(d.config["source"]) })
// Format the file.
_, err = makeFSType(d.config["source"], "btrfs", &mkfsOptions{Label: d.name})
if err != nil {
return fmt.Errorf("Failed to format sparse file: %w", err)
}
} else if shared.IsBlockdevPath(d.config["source"]) {
// Wipe if requested.
if shared.IsTrue(d.config["source.wipe"]) {
err := wipeBlockHeaders(d.config["source"])
if err != nil {
return fmt.Errorf("Failed to wipe headers from disk %q: %w", d.config["source"], err)
}
d.config["source.wipe"] = ""
}
// Format the block device.
_, err := makeFSType(d.config["source"], "btrfs", &mkfsOptions{Label: d.name})
if err != nil {
return fmt.Errorf("Failed to format block device: %w", err)
}
// Record the UUID as the source.
devUUID, err := fsUUID(d.config["source"])
if err != nil {
return err
}
// Confirm that the symlink is appearing (give it 10s).
if tryExists(fmt.Sprintf("/dev/disk/by-uuid/%s", devUUID)) {
// Override the config to use the UUID.
d.config["source"] = devUUID
}
} else if d.config["source"] != "" {
hostPath := shared.HostPath(d.config["source"])
if d.isSubvolume(hostPath) {
// Existing btrfs subvolume.
subvols, err := d.getSubvolumes(hostPath)
if err != nil {
return fmt.Errorf("Could not determine if existing btrfs subvolume is empty: %w", err)
}
// Check that the provided subvolume is empty.
if len(subvols) > 0 {
return fmt.Errorf("Requested btrfs subvolume exists but is not empty")
}
} else {
// New btrfs subvolume on existing btrfs filesystem.
cleanSource := filepath.Clean(hostPath)
lxdDir := shared.VarPath()
if shared.PathExists(hostPath) {
hostPathFS, _ := filesystem.Detect(hostPath)
if hostPathFS != "btrfs" {
return fmt.Errorf("Provided path does not reside on a btrfs filesystem")
}
}
if strings.HasPrefix(cleanSource, lxdDir) {
if cleanSource != GetPoolMountPath(d.name) {
return fmt.Errorf("Only allowed source path under %q is %q", shared.VarPath(), GetPoolMountPath(d.name))
}
storagePoolDirFS, _ := filesystem.Detect(shared.VarPath("storage-pools"))
if storagePoolDirFS != "btrfs" {
return fmt.Errorf("Provided path does not reside on a btrfs filesystem")
}
// Delete the current directory to replace by subvolume.
err := os.Remove(cleanSource)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Failed to remove %q: %w", cleanSource, err)
}
}
// Create the subvolume.
_, err := shared.RunCommand("btrfs", "subvolume", "create", hostPath)
if err != nil {
return err
}
}
} else {
return fmt.Errorf(`Invalid "source" property`)
}
revert.Success()
return nil
}
// Delete removes the storage pool from the storage device.
func (d *btrfs) Delete(op *operations.Operation) error {
// If the user completely destroyed it, call it done.
if !shared.PathExists(GetPoolMountPath(d.name)) {
return nil
}
// Delete potential intermediate btrfs subvolumes.
for _, volType := range d.Info().VolumeTypes {
for _, dir := range BaseDirectories[volType] {
path := filepath.Join(GetPoolMountPath(d.name), dir)
if !shared.PathExists(path) {
continue
}
if !d.isSubvolume(path) {
continue
}
err := d.deleteSubvolume(path, true)
if err != nil {
return fmt.Errorf("Failed deleting btrfs subvolume %q", path)
}
}
}
// On delete, wipe everything in the directory.
mountPath := GetPoolMountPath(d.name)
err := wipeDirectory(mountPath)
if err != nil {
return fmt.Errorf("Failed removing mount path %q: %w", mountPath, err)
}
// Unmount the path.
_, err = d.Unmount()
if err != nil {
return err
}
// If the pool path is a subvolume itself, delete it.
if d.isSubvolume(mountPath) {
err := d.deleteSubvolume(mountPath, false)
if err != nil {
return err
}
// And re-create as an empty directory to make the backend happy.
err = os.Mkdir(mountPath, 0700)
if err != nil {
return fmt.Errorf("Failed creating directory %q: %w", mountPath, err)
}
}
// Delete any loop file we may have used.
loopPath := loopFilePath(d.name)
err = os.Remove(loopPath)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Failed removing loop file %q: %w", loopPath, err)
}
return nil
}
// Validate checks that all provide keys are supported and that no conflicting or missing configuration is present.
func (d *btrfs) Validate(config map[string]string) error {
rules := map[string]func(value string) error{
"size": validate.Optional(validate.IsSize),
"btrfs.mount_options": validate.IsAny,
}
return d.validatePool(config, rules, nil)
}
// Update applies any driver changes required from a configuration change.
func (d *btrfs) Update(changedConfig map[string]string) error {
// We only care about btrfs.mount_options.
val, ok := changedConfig["btrfs.mount_options"]
if ok {
// Custom mount options don't work inside containers
if d.state.OS.RunningInUserNS {
return nil
}
// Trigger a re-mount.
d.config["btrfs.mount_options"] = val
mntFlags, mntOptions := filesystem.ResolveMountOptions(strings.Split(d.getMountOptions(), ","))
mntFlags |= unix.MS_REMOUNT
err := TryMount("", GetPoolMountPath(d.name), "none", mntFlags, mntOptions)
if err != nil {
return err
}
}
size, ok := changedConfig["size"]
if ok {
// Figure out loop path
loopPath := loopFilePath(d.name)
if d.config["source"] != loopPath {
return fmt.Errorf("Cannot resize non-loopback pools")
}
// Resize loop file
f, err := os.OpenFile(loopPath, os.O_RDWR, 0600)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
sizeBytes, _ := units.ParseByteSizeString(size)
err = f.Truncate(sizeBytes)
if err != nil {
return err
}
loopDevPath, err := loopDeviceSetup(loopPath)
if err != nil {
return err
}
defer func() { _ = loopDeviceAutoDetach(loopDevPath) }()
err = loopDeviceSetCapacity(loopDevPath)
if err != nil {
return err
}
_, err = shared.RunCommand("btrfs", "filesystem", "resize", "max", GetPoolMountPath(d.name))
if err != nil {
return err
}
}
return nil
}
// Mount mounts the storage pool.
func (d *btrfs) Mount() (bool, error) {
// Check if already mounted.
if filesystem.IsMountPoint(GetPoolMountPath(d.name)) {
return false, nil
}
var err error
// Setup mount options.
loopPath := loopFilePath(d.name)
mntSrc := ""
mntDst := GetPoolMountPath(d.name)
mntFilesystem := "btrfs"
if d.config["source"] == loopPath {
mntSrc, err = loopDeviceSetup(d.config["source"])
if err != nil {
return false, err
}
defer func() { _ = loopDeviceAutoDetach(mntSrc) }()
} else if filepath.IsAbs(d.config["source"]) {
// Bring up an existing device or path.
mntSrc = shared.HostPath(d.config["source"])
if !shared.IsBlockdevPath(mntSrc) {
mntFilesystem = "none"
mntSrcFS, _ := filesystem.Detect(mntSrc)
if mntSrcFS != "btrfs" {
return false, fmt.Errorf("Source path %q isn't btrfs", mntSrc)
}
}
} else {
// Mount using UUID.
mntSrc = fmt.Sprintf("/dev/disk/by-uuid/%s", d.config["source"])
}
// Get the custom mount flags/options.
mntFlags, mntOptions := filesystem.ResolveMountOptions(strings.Split(d.getMountOptions(), ","))
// Handle bind-mounts first.
if mntFilesystem == "none" {
// Setup the bind-mount itself.
err := TryMount(mntSrc, mntDst, mntFilesystem, unix.MS_BIND, "")
if err != nil {
return false, err
}
// Custom mount options don't work inside containers
if d.state.OS.RunningInUserNS {
return true, nil
}
// Now apply the custom options.
mntFlags |= unix.MS_REMOUNT
err = TryMount("", mntDst, mntFilesystem, mntFlags, mntOptions)
if err != nil {
return false, err
}
return true, nil
}
// Handle traditional mounts.
err = TryMount(mntSrc, mntDst, mntFilesystem, mntFlags, mntOptions)
if err != nil {
return false, err
}
return true, nil
}
// Unmount unmounts the storage pool.
func (d *btrfs) Unmount() (bool, error) {
// Unmount the pool.
ourUnmount, err := forceUnmount(GetPoolMountPath(d.name))
if err != nil {
return false, err
}
return ourUnmount, nil
}
// GetResources returns the pool resource usage information.
func (d *btrfs) GetResources() (*api.ResourcesStoragePool, error) {
return genericVFSGetResources(d)
}
// MigrationType returns the type of transfer methods to be used when doing migrations between pools in preference order.
func (d *btrfs) MigrationTypes(contentType ContentType, refresh bool, copySnapshots bool) []migration.Type {
var rsyncFeatures []string
btrfsFeatures := []string{migration.BTRFSFeatureMigrationHeader, migration.BTRFSFeatureSubvolumes, migration.BTRFSFeatureSubvolumeUUIDs}
// 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{"xattrs", "delete", "bidirectional"}
} else {
rsyncFeatures = []string{"xattrs", "delete", "compress", "bidirectional"}
}
// Only offer rsync if running in an unprivileged container.
if d.state.OS.RunningInUserNS {
var transportType migration.MigrationFSType
if IsContentBlock(contentType) {
transportType = migration.MigrationFSType_BLOCK_AND_RSYNC
} else {
transportType = migration.MigrationFSType_RSYNC
}
return []migration.Type{
{
FSType: transportType,
Features: rsyncFeatures,
},
}
}
if IsContentBlock(contentType) {
return []migration.Type{
{
FSType: migration.MigrationFSType_BTRFS,
Features: btrfsFeatures,
},
{
FSType: migration.MigrationFSType_BLOCK_AND_RSYNC,
Features: rsyncFeatures,
},
}
}
if refresh && !copySnapshots {
return []migration.Type{
{
FSType: migration.MigrationFSType_RSYNC,
Features: rsyncFeatures,
},
}
}
return []migration.Type{
{
FSType: migration.MigrationFSType_BTRFS,
Features: btrfsFeatures,
},
{
FSType: migration.MigrationFSType_RSYNC,
Features: rsyncFeatures,
},
}
}