[lxc-devel] [lxd/master] Storage Backend EnsureImage

tomponline on Github lxc-bot at linuxcontainers.org
Mon Nov 4 08:26:41 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 609 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20191104/c8d727e0/attachment.bin>
-------------- next part --------------
From 08471dc07a5c30491f0ea86722613e3681ade5a5 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 4 Nov 2019 08:09:37 +0000
Subject: [PATCH 1/5] lxd/images: Updates imageCreateInPool to use EnsureImage

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

diff --git a/lxd/images.go b/lxd/images.go
index e413d52846..6600d8d623 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -634,7 +634,7 @@ func imageCreateInPool(d *Daemon, info *api.Image, storagePool string) error {
 			return err
 		}
 
-		err = pool.CreateImage(info.Fingerprint, nil)
+		err = pool.EnsureImage(info.Fingerprint, nil)
 		if err != nil {
 			return err
 		}

From 30dd5468fa31f8c06761e62a017f353ab2d08bec Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 4 Nov 2019 08:10:24 +0000
Subject: [PATCH 2/5] lxd/storage/backend/lxd: Updates EnsureImage usage and
 adds more comments

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

diff --git a/lxd/storage/backend_lxd.go b/lxd/storage/backend_lxd.go
index cc65a07531..c0cd4bd96c 100644
--- a/lxd/storage/backend_lxd.go
+++ b/lxd/storage/backend_lxd.go
@@ -186,6 +186,8 @@ func (b *lxdBackend) createInstanceSymlink(inst Instance, mountPath string) erro
 	return nil
 }
 
+// imageFiller returns a function that can be used as a filler function with CreateVolume(). This
+// function will unpack the specified image archive into the specified mount path of the volume.
 func (b *lxdBackend) imageFiller(fingerprint string, op *operations.Operation) func(mountPath string) error {
 	return func(instanceMountPath string) error {
 		var tracker *ioprogress.ProgressTracker
@@ -224,21 +226,25 @@ func (b *lxdBackend) CreateInstanceFromImage(inst Instance, fingerprint string,
 	}()
 
 	vol := b.newVolume(volType, drivers.ContentTypeFS, project.Prefix(inst.Project(), inst.Name()), nil)
+
+	// If the driver doesn't support optimized image volumes then create a new empty volume and
+	// populate it with the contents of the image archive.
 	if !b.driver.Info().OptimizedImages {
 		err = b.driver.CreateVolume(vol, b.imageFiller(fingerprint, op), op)
 		if err != nil {
 			return err
 		}
 	} else {
-		if !b.driver.HasVolume(drivers.VolumeTypeImage, fingerprint) {
-			err := b.CreateImage(fingerprint, op)
-			if err != nil {
-				return err
-			}
+		// If the driver does support optimized images then ensure the optimized image
+		// volume has been created for the archive's fingerprint and then proceed to create
+		// a new volume by copying the optimized image volume.
+		err := b.EnsureImage(fingerprint, op)
+		if err != nil {
+			return err
 		}
 
 		imgVol := b.newVolume(drivers.VolumeTypeImage, drivers.ContentTypeFS, fingerprint, nil)
-		err := b.driver.CreateVolumeFromCopy(vol, imgVol, false, op)
+		err = b.driver.CreateVolumeFromCopy(vol, imgVol, false, op)
 		if err != nil {
 			return err
 		}
@@ -339,8 +345,9 @@ func (b *lxdBackend) UnmountInstanceSnapshot(inst Instance) (bool, error) {
 	return true, ErrNotImplemented
 }
 
-// CreateImage creates an optimized volume of the image if supported by the storage pool driver.
-func (b *lxdBackend) CreateImage(fingerprint string, op *operations.Operation) error {
+// EnsureImage creates an optimized volume of the image if supported by the storage pool driver and
+// the volume doesn't already exist.
+func (b *lxdBackend) EnsureImage(fingerprint string, op *operations.Operation) error {
 	logger := logging.AddContext(b.logger, log.Ctx{"fingerprint": fingerprint})
 	logger.Debug("CreateImage started")
 	defer logger.Debug("CreateImage finished")
@@ -349,12 +356,12 @@ func (b *lxdBackend) CreateImage(fingerprint string, op *operations.Operation) e
 		return nil // Nothing to do for drivers that don't support optimized images volumes.
 	}
 
-	// Check if we already have a suitable volume
+	// Check if we already have a suitable volume.
 	if b.driver.HasVolume(drivers.VolumeTypeImage, fingerprint) {
 		return nil
 	}
 
-	// Create the new image volume
+	// Create the new image volume.
 	vol := b.newVolume(drivers.VolumeTypeImage, drivers.ContentTypeFS, fingerprint, nil)
 	err := b.driver.CreateVolume(vol, b.imageFiller(fingerprint, op), op)
 	if err != nil {

From fd4c3e55cc62e1d329851697a0ac7da6f460baf8 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 4 Nov 2019 08:10:44 +0000
Subject: [PATCH 3/5] lxd/storage/backend/mock: Updates with EnsureImage

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

diff --git a/lxd/storage/backend_mock.go b/lxd/storage/backend_mock.go
index 5cbab80ec3..9f20fc1b55 100644
--- a/lxd/storage/backend_mock.go
+++ b/lxd/storage/backend_mock.go
@@ -142,7 +142,7 @@ func (b *mockBackend) UnmountInstanceSnapshot(i Instance) (bool, error) {
 	return true, nil
 }
 
-func (b *mockBackend) CreateImage(fingerprint string, op *operations.Operation) error {
+func (b *mockBackend) EnsureImage(fingerprint string, op *operations.Operation) error {
 	return nil
 }
 

From d2fa3779e816a02ce6f327105f2e11553c5a3317 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 4 Nov 2019 08:11:01 +0000
Subject: [PATCH 4/5] lxd/storage/interfaces: Renames CreateImage to
 EnsureImage

 - This indicates that the function will return nil if the optimized image volume already exists or is not needed on the storage pool.

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

diff --git a/lxd/storage/interfaces.go b/lxd/storage/interfaces.go
index d8853c0096..592b28b14f 100644
--- a/lxd/storage/interfaces.go
+++ b/lxd/storage/interfaces.go
@@ -67,7 +67,7 @@ type Pool interface {
 	UnmountInstanceSnapshot(i Instance) (bool, error)
 
 	// Images.
-	CreateImage(fingerprint string, op *operations.Operation) error
+	EnsureImage(fingerprint string, op *operations.Operation) error
 	DeleteImage(fingerprint string, op *operations.Operation) error
 
 	// Custom volumes.

From 9932a14c6012ebe1689cef2c16a48827abfe1158 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 4 Nov 2019 08:25:28 +0000
Subject: [PATCH 5/5] lxd/storage/load: Adds comments

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

diff --git a/lxd/storage/load.go b/lxd/storage/load.go
index 4cb903baff..d2f1e24072 100644
--- a/lxd/storage/load.go
+++ b/lxd/storage/load.go
@@ -34,6 +34,10 @@ func volIDFuncMake(state *state.State, poolID int64) func(volType drivers.Volume
 		// encoding format, so if there is no underscore in the volume name then we assume
 		// the project is default.
 		project := "default"
+
+		// Currently only Containers and VMs support project level volumes.
+		// This means that other volume types may have underscores in their names that don't
+		// indicate the project name.
 		if volType == drivers.VolumeTypeContainer || volType == drivers.VolumeTypeVM {
 			volParts := strings.SplitN(volName, "_", 2)
 			if len(volParts) > 1 {


More information about the lxc-devel mailing list