[lxc-devel] [lxd/master] Storage tweaks

tomponline on Github lxc-bot at linuxcontainers.org
Fri Jan 10 10:30:22 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 538 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200110/3a4919a0/attachment.bin>
-------------- next part --------------
From 2aa10814a02e083b2f601130abb9341f9992b2d3 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 10 Jan 2020 10:23:08 +0000
Subject: [PATCH 1/5] lxd/storage/backend/lxd: Adds errors.Wrapf around os. and
 unix. function errors

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/storage/backend_lxd.go | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/lxd/storage/backend_lxd.go b/lxd/storage/backend_lxd.go
index 41409f25eb..83bcda7619 100644
--- a/lxd/storage/backend_lxd.go
+++ b/lxd/storage/backend_lxd.go
@@ -74,7 +74,7 @@ func (b *lxdBackend) create(localOnly bool, op *operations.Operation) error {
 	path := drivers.GetPoolMountPath(b.name)
 	err := os.MkdirAll(path, 0711)
 	if err != nil && !os.IsExist(err) {
-		return err
+		return errors.Wrapf(err, "Failed to create directory '%s'", path)
 	}
 
 	revert.Add(func() { os.RemoveAll(path) })
@@ -209,7 +209,7 @@ func (b *lxdBackend) Delete(localOnly bool, op *operations.Operation) error {
 	path := shared.VarPath("storage-pools", b.name)
 	err := os.Remove(path)
 	if err != nil {
-		return err
+		return errors.Wrapf(err, "Failed to remove directory '%s'", path)
 	}
 
 	return nil
@@ -246,14 +246,14 @@ func (b *lxdBackend) ensureInstanceSymlink(instanceType instancetype.Type, proje
 	if shared.PathExists(symlinkPath) {
 		err := os.Remove(symlinkPath)
 		if err != nil {
-			return err
+			return errors.Wrapf(err, "Failed to remove symlink '%s'", symlinkPath)
 		}
 	}
 
 	// Create new symlink.
 	err := os.Symlink(mountPath, symlinkPath)
 	if err != nil {
-		return err
+		return errors.Wrapf(err, "Failed to create symlink from '%s' to '%s'", mountPath, symlinkPath)
 	}
 
 	return nil
@@ -266,7 +266,7 @@ func (b *lxdBackend) removeInstanceSymlink(instanceType instancetype.Type, proje
 	if shared.PathExists(symlinkPath) {
 		err := os.Remove(symlinkPath)
 		if err != nil {
-			return err
+			return errors.Wrapf(err, "Failed to remove symlink '%s'", symlinkPath)
 		}
 	}
 
@@ -292,14 +292,14 @@ func (b *lxdBackend) ensureInstanceSnapshotSymlink(instanceType instancetype.Typ
 	if shared.PathExists(snapshotSymlink) {
 		err = os.Remove(snapshotSymlink)
 		if err != nil {
-			return err
+			return errors.Wrapf(err, "Failed to remove symlink '%s'", snapshotSymlink)
 		}
 	}
 
 	// Create new symlink.
 	err = os.Symlink(snapshotTargetPath, snapshotSymlink)
 	if err != nil {
-		return err
+		return errors.Wrapf(err, "Failed to create symlink from '%s' to '%s'", snapshotTargetPath, snapshotSymlink)
 	}
 
 	return nil
@@ -326,7 +326,7 @@ func (b *lxdBackend) removeInstanceSnapshotSymlinkIfUnused(instanceType instance
 		if shared.PathExists(snapshotSymlink) {
 			err := os.Remove(snapshotSymlink)
 			if err != nil {
-				return err
+				return errors.Wrapf(err, "Failed to remove symlink '%s'", snapshotSymlink)
 			}
 		}
 	}
@@ -2725,9 +2725,10 @@ func (b *lxdBackend) RestoreCustomVolume(volName string, snapshotName string, op
 func (b *lxdBackend) createStorageStructure(path string) error {
 	for _, volType := range b.driver.Info().VolumeTypes {
 		for _, name := range drivers.BaseDirectories[volType] {
-			err := os.MkdirAll(filepath.Join(path, name), 0711)
+			path := filepath.Join(path, name)
+			err := os.MkdirAll(path, 0711)
 			if err != nil && !os.IsExist(err) {
-				return err
+				return errors.Wrapf(err, "Failed to create directory '%s'", path)
 			}
 		}
 	}
@@ -2809,9 +2810,10 @@ func (b *lxdBackend) UpdateInstanceBackupFile(inst instance.Instance, op *operat
 	// Update pool information in the backup.yaml file.
 	err = vol.MountTask(func(mountPath string, op *operations.Operation) error {
 		// Write the YAML
-		f, err := os.Create(filepath.Join(inst.Path(), "backup.yaml"))
+		path := filepath.Join(inst.Path(), "backup.yaml")
+		f, err := os.Create(path)
 		if err != nil {
-			return err
+			return errors.Wrapf(err, "Failed to create file '%s'", path)
 		}
 		defer f.Close()
 

From c62626d06307930b6526ca58ed7c8ea54dd0d425 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 10 Jan 2020 10:23:40 +0000
Subject: [PATCH 2/5] lxd/storage/drivers/driver/btrfs/volumes: tmpVolSuffix
 usage

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/storage/drivers/driver_btrfs_volumes.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lxd/storage/drivers/driver_btrfs_volumes.go b/lxd/storage/drivers/driver_btrfs_volumes.go
index c8826b447f..c613d466c1 100644
--- a/lxd/storage/drivers/driver_btrfs_volumes.go
+++ b/lxd/storage/drivers/driver_btrfs_volumes.go
@@ -723,7 +723,7 @@ func (d *btrfs) VolumeSnapshots(vol Volume, op *operations.Operation) ([]string,
 // RestoreVolume restores a volume from a snapshot.
 func (d *btrfs) RestoreVolume(vol Volume, snapshotName string, op *operations.Operation) error {
 	// Create a backup so we can revert.
-	backupSubvolume := fmt.Sprintf("%s.tmp", vol.MountPath())
+	backupSubvolume := fmt.Sprintf("%s%s", vol.MountPath(), tmpVolSuffix)
 	err := os.Rename(vol.MountPath(), backupSubvolume)
 	if err != nil {
 		return err

From 7cfef1c9f39e6f761c5c0e60196e40dad7e2eb05 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 10 Jan 2020 10:24:28 +0000
Subject: [PATCH 3/5] lxd/storage/drivers/volume: Adds tmpVolSuffix const

Also changes existing var constants to proper constants and adds comments.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/storage/drivers/volume.go | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lxd/storage/drivers/volume.go b/lxd/storage/drivers/volume.go
index ce968b7645..d7e8d6e648 100644
--- a/lxd/storage/drivers/volume.go
+++ b/lxd/storage/drivers/volume.go
@@ -9,12 +9,17 @@ import (
 	"github.com/lxc/lxd/shared"
 )
 
-var defaultBlockSize = "10GB"
+// tmpVolSuffix Suffix to use for any temporary volumes created by LXD.
+const tmpVolSuffix = ".lxdtmp"
+
+// defaultBlockSize Default size of block volumes.
+const defaultBlockSize = "10GB"
 
 // DefaultFilesystem filesytem to use for block devices by default.
-var DefaultFilesystem = "ext4"
+const DefaultFilesystem = "ext4"
 
-var volIDQuotaSkip = int64(-1)
+// volIDQuotaSkip is used to indicate to drivers that quotas should not be setup, used during backup import.
+const volIDQuotaSkip = int64(-1)
 
 // VolumeType represents a storage volume type.
 type VolumeType string

From 820ceb6c74d7785ba4ffb67e52e8a313a91a8ebe Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 10 Jan 2020 10:25:19 +0000
Subject: [PATCH 4/5] lxd/storage/drivers/utils: Adds errors.Wrapf to
 mount/unmount functions

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/storage/drivers/utils.go | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/lxd/storage/drivers/utils.go b/lxd/storage/drivers/utils.go
index 19a877d1c1..54fe57a3df 100644
--- a/lxd/storage/drivers/utils.go
+++ b/lxd/storage/drivers/utils.go
@@ -8,6 +8,7 @@ import (
 	"strings"
 	"time"
 
+	"github.com/pkg/errors"
 	"golang.org/x/sys/unix"
 
 	"github.com/lxc/lxd/lxd/operations"
@@ -41,18 +42,18 @@ func forceUnmount(path string) (bool, error) {
 	unmounted := false
 
 	for {
-		// Check if already unmounted
+		// Check if already unmounted.
 		if !shared.IsMountPoint(path) {
 			return unmounted, nil
 		}
 
-		// Try a clean unmount first
+		// Try a clean unmount first.
 		err := TryUnmount(path, 0)
 		if err != nil {
-			// Fallback to lazy unmounting
+			// Fallback to lazy unmounting.
 			err = unix.Unmount(path, unix.MNT_DETACH)
 			if err != nil {
-				return false, err
+				return false, errors.Wrapf(err, "Failed to unmount '%s'", path)
 			}
 		}
 
@@ -139,7 +140,7 @@ func TryMount(src string, dst string, fs string, flags uintptr, options string)
 	}
 
 	if err != nil {
-		return err
+		return errors.Wrapf(err, "Failed to mount '%s' on '%s'", src, dst)
 	}
 
 	return nil
@@ -159,7 +160,7 @@ func TryUnmount(path string, flags int) error {
 	}
 
 	if err != nil {
-		return err
+		return errors.Wrapf(err, "Failed to unmount '%s'", path)
 	}
 
 	return nil

From f83951eaa4066e54ee5fa3b145ad78955abf9142 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 10 Jan 2020 10:25:43 +0000
Subject: [PATCH 5/5] lxd/storage/drivers/utils: Adds
 renegerateFilesystemUUIDNeeded

And makes regenerateFilesystemUUID return an error for unsupported filesystems.

This gives more control to calling code.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/storage/drivers/utils.go | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lxd/storage/drivers/utils.go b/lxd/storage/drivers/utils.go
index 54fe57a3df..cf26c5aa80 100644
--- a/lxd/storage/drivers/utils.go
+++ b/lxd/storage/drivers/utils.go
@@ -470,6 +470,18 @@ func growFileSystem(fsType string, devPath string, vol Volume) error {
 	}, nil)
 }
 
+// renegerateFilesystemUUIDNeeded returns true if fsType requires UUID regeneration, false if not.
+func renegerateFilesystemUUIDNeeded(fsType string) bool {
+	switch fsType {
+	case "btrfs":
+		return true
+	case "xfs":
+		return true
+	}
+
+	return false
+}
+
 // regenerateFilesystemUUID changes the filesystem UUID to a new randomly generated one if the fsType requires it.
 // Otherwise this function does nothing.
 func regenerateFilesystemUUID(fsType, devPath string) error {
@@ -480,7 +492,7 @@ func regenerateFilesystemUUID(fsType, devPath string) error {
 		return regenerateFilesystemXFSUUID(devPath)
 	}
 
-	return nil
+	return fmt.Errorf("Filesystem not supported")
 }
 
 // regenerateFilesystemBTRFSUUID changes the BTRFS filesystem UUID to a new randomly generated one.


More information about the lxc-devel mailing list