[lxc-devel] [lxd/master] lxd/storage: Lock during the whole image replace

stgraber on Github lxc-bot at linuxcontainers.org
Tue Oct 27 23:21:25 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201027/488c9d8b/attachment.bin>
-------------- next part --------------
From 6b4533a92cfe6eacabe6a1393270b7d7949d2b2c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 27 Oct 2020 18:23:03 -0400
Subject: [PATCH] lxd/storage: Lock during the whole image replace
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/storage/backend_lxd.go | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lxd/storage/backend_lxd.go b/lxd/storage/backend_lxd.go
index b2c1272991..f3d8f83273 100644
--- a/lxd/storage/backend_lxd.go
+++ b/lxd/storage/backend_lxd.go
@@ -1039,6 +1039,11 @@ func (b *lxdBackend) CreateInstanceFromImage(inst instance.Instance, fingerprint
 		// by creating a new cached image volume using the pool's current settings (including volume.size).
 		if errors.Cause(err) == drivers.ErrCannotBeShrunk {
 			logger.Debug("Cached image volume is larger than new volume and cannot be shrunk, regenerating image volume")
+
+			// Lock during the entire process to avoid attempts at creating while the image is gone.
+			unlock := locking.Lock(drivers.OperationLockName(b.name, string(drivers.VolumeTypeImage), fmt.Sprintf("ReplaceImage_%v", fingerprint)))
+			defer unlock()
+
 			err = b.DeleteImage(fingerprint, op)
 			if err != nil {
 				return err
@@ -1055,7 +1060,6 @@ func (b *lxdBackend) CreateInstanceFromImage(inst instance.Instance, fingerprint
 			}
 		} else if err != nil {
 			return err
-
 		}
 	}
 


More information about the lxc-devel mailing list