[lxc-devel] [lxd/master] Storage: Check for existing directory during pool create

tomponline on Github lxc-bot at linuxcontainers.org
Tue Feb 18 16:16:48 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200218/e1a8e885/attachment.bin>
-------------- next part --------------
From 8daad23611b3d975dcaea437e220d845a3102eba Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 18 Feb 2020 15:28:34 +0000
Subject: [PATCH 1/2] lxd/storage/backend/lxd: Adds logging for
 CreateInstanceFromBackup post hook

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

diff --git a/lxd/storage/backend_lxd.go b/lxd/storage/backend_lxd.go
index 3ec19c2bd9..0e17f60b55 100644
--- a/lxd/storage/backend_lxd.go
+++ b/lxd/storage/backend_lxd.go
@@ -528,6 +528,9 @@ func (b *lxdBackend) CreateInstanceFromBackup(srcBackup backup.Info, srcData io.
 	// containing the instance's root disk device's config so that the driver's post hook function can access
 	// that config to perform any post instance creation setup.
 	postHook = func(inst instance.Instance) error {
+		logger.Debug("CreateInstanceFromBackup post hook started")
+		defer logger.Debug("CreateInstanceFromBackup post hook finished")
+
 		// Get the root disk device config.
 		rootDiskConf, err := b.instanceRootVolumeConfig(inst)
 		if err != nil {

From a1cfabade4c26cd16d98440aee28067ecfe61b55 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 18 Feb 2020 15:57:38 +0000
Subject: [PATCH 2/2] lxd/storage/backend/lxd: Refuse to create storage pool if
 dir exists on disk

If the directory `/var/lib/lxd/storage-pools/<pool>` exists on disk, but pool doesn't exist in DB, if you try and create a storage pool of the same name and it fails (likely because the existing dir is non-empty) then the entire storage pool directory will be removed.

Changes check to refuse to create storage pool if directory already exists on disk.

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

diff --git a/lxd/storage/backend_lxd.go b/lxd/storage/backend_lxd.go
index 0e17f60b55..b42036f22d 100644
--- a/lxd/storage/backend_lxd.go
+++ b/lxd/storage/backend_lxd.go
@@ -65,16 +65,21 @@ func (b *lxdBackend) MigrationTypes(contentType drivers.ContentType, refresh boo
 func (b *lxdBackend) create(localOnly bool, op *operations.Operation) error {
 	logger := logging.AddContext(b.logger, log.Ctx{"config": b.db.Config, "description": b.db.Description, "localOnly": localOnly})
 	logger.Debug("create started")
-	defer logger.Debug("created finished")
+	defer logger.Debug("create finished")
 
 	revert := revert.New()
 	defer revert.Fail()
 
-	// Create the storage path.
 	path := drivers.GetPoolMountPath(b.name)
+
+	if shared.IsDir(path) {
+		return fmt.Errorf("Storage pool directory %q already exists", path)
+	}
+
+	// Create the storage path.
 	err := os.MkdirAll(path, 0711)
-	if err != nil && !os.IsExist(err) {
-		return errors.Wrapf(err, "Failed to create directory '%s'", path)
+	if err != nil {
+		return errors.Wrapf(err, "Failed to create storage pool directory %q", path)
 	}
 
 	revert.Add(func() { os.RemoveAll(path) })


More information about the lxc-devel mailing list