[lxc-devel] [lxd/master] Always pass Volume to driver functions

monstermunchkin on Github lxc-bot at linuxcontainers.org
Thu Dec 12 11:16:37 UTC 2019


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/20191212/3a75fd63/attachment-0001.bin>
-------------- next part --------------
From 84db47f63dea75c9b180fb5a35b8d7096aa08738 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 12 Dec 2019 11:12:07 +0100
Subject: [PATCH 1/3] lxd/storage/drivers: Always pass Volume argument

This changes the drivers interface to always take a Volume argument
instead of separate volType and volName.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/storage/drivers/interface.go | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/lxd/storage/drivers/interface.go b/lxd/storage/drivers/interface.go
index 0732ebeb69..c250ff864b 100644
--- a/lxd/storage/drivers/interface.go
+++ b/lxd/storage/drivers/interface.go
@@ -23,7 +23,7 @@ type Driver interface {
 	// Internal.
 	Config() map[string]string
 	Info() Info
-	HasVolume(volType VolumeType, volName string) bool
+	HasVolume(vol Volume) bool
 
 	// Pool.
 	Create() error
@@ -39,33 +39,33 @@ type Driver interface {
 	CreateVolume(vol Volume, filler *VolumeFiller, op *operations.Operation) error
 	CreateVolumeFromCopy(vol Volume, srcVol Volume, copySnapshots bool, op *operations.Operation) error
 	RefreshVolume(vol Volume, srcVol Volume, srcSnapshots []Volume, op *operations.Operation) error
-	DeleteVolume(volType VolumeType, volName string, op *operations.Operation) error
-	RenameVolume(volType VolumeType, volName string, newName string, op *operations.Operation) error
+	DeleteVolume(vol Volume, op *operations.Operation) error
+	RenameVolume(vol Volume, newName string, op *operations.Operation) error
 	UpdateVolume(vol Volume, changedConfig map[string]string) error
-	GetVolumeUsage(volType VolumeType, volName string) (int64, error)
-	SetVolumeQuota(volType VolumeType, volName, size string, op *operations.Operation) error
-	GetVolumeDiskPath(volType VolumeType, volName string) (string, error)
+	GetVolumeUsage(vol Volume) (int64, error)
+	SetVolumeQuota(vol Volume, size string, op *operations.Operation) error
+	GetVolumeDiskPath(vol Volume) (string, error)
 
 	// MountVolume mounts a storage volume, returns true if we caused a new mount, false if
 	// already mounted.
-	MountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error)
+	MountVolume(vol Volume, op *operations.Operation) (bool, error)
 
 	// MountVolumeSnapshot mounts a storage volume snapshot as readonly, returns true if we
 	// caused a new mount, false if already mounted.
-	MountVolumeSnapshot(volType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error)
+	MountVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) (bool, error)
 
 	// UnmountVolume unmounts a storage volume, returns true if unmounted, false if was not
 	// mounted.
-	UnmountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error)
+	UnmountVolume(vol Volume, op *operations.Operation) (bool, error)
 
 	// UnmountVolume unmounts a storage volume snapshot, returns true if unmounted, false if was
 	// not mounted.
-	UnmountVolumeSnapshot(VolumeType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error)
+	UnmountVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) (bool, error)
 
-	CreateVolumeSnapshot(volType VolumeType, volName string, newSnapshotName string, op *operations.Operation) error
-	DeleteVolumeSnapshot(volType VolumeType, volName string, snapshotName string, op *operations.Operation) error
-	RenameVolumeSnapshot(volType VolumeType, volName string, snapshotName string, newSnapshotName string, op *operations.Operation) error
-	VolumeSnapshots(volType VolumeType, volName string, op *operations.Operation) ([]string, error)
+	CreateVolumeSnapshot(vol Volume, newSnapshotName string, op *operations.Operation) error
+	DeleteVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) error
+	RenameVolumeSnapshot(vol Volume, snapshotName string, newSnapshotName string, op *operations.Operation) error
+	VolumeSnapshots(vol Volume, op *operations.Operation) ([]string, error)
 	RestoreVolume(vol Volume, snapshotName string, op *operations.Operation) error
 
 	// Migration.

From ea66c9b4039a148d43745e2d8bdeb8f4519eb1e9 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 12 Dec 2019 11:12:55 +0100
Subject: [PATCH 2/3] lxd/storage/drivers: Use new driver interface

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/storage/drivers/driver_cephfs.go | 110 +++++++++++++--------------
 lxd/storage/drivers/driver_dir.go    |  88 +++++++++++----------
 lxd/storage/drivers/volume.go        |  13 ++--
 3 files changed, 105 insertions(+), 106 deletions(-)

diff --git a/lxd/storage/drivers/driver_cephfs.go b/lxd/storage/drivers/driver_cephfs.go
index fa1c759458..dcb5f2ff93 100644
--- a/lxd/storage/drivers/driver_cephfs.go
+++ b/lxd/storage/drivers/driver_cephfs.go
@@ -67,8 +67,8 @@ func (d *cephfs) Info() Info {
 	}
 }
 
-func (d *cephfs) HasVolume(volType VolumeType, volName string) bool {
-	if shared.PathExists(GetVolumeMountPath(d.name, volType, volName)) {
+func (d *cephfs) HasVolume(vol Volume) bool {
+	if shared.PathExists(GetVolumeMountPath(d.name, vol.volType, vol.name)) {
 		return true
 	}
 
@@ -289,7 +289,7 @@ func (d *cephfs) ValidateVolume(vol Volume, removeUnknownKeys bool) error {
 }
 
 // GetVolumeDiskPath returns the location of a root disk block device.
-func (d *cephfs) GetVolumeDiskPath(volType VolumeType, volName string) (string, error) {
+func (d *cephfs) GetVolumeDiskPath(vol Volume) (string, error) {
 	return "", ErrNotImplemented
 }
 
@@ -355,7 +355,7 @@ func (d *cephfs) CreateVolumeFromCopy(vol Volume, srcVol Volume, copySnapshots b
 
 		// Remove any paths created if we are reverting.
 		for _, snapName := range revertSnaps {
-			d.DeleteVolumeSnapshot(vol.volType, vol.name, snapName, op)
+			d.DeleteVolumeSnapshot(vol, snapName, op)
 		}
 
 		os.RemoveAll(volPath)
@@ -382,7 +382,7 @@ func (d *cephfs) CreateVolumeFromCopy(vol Volume, srcVol Volume, copySnapshots b
 				}, op)
 
 				// Create the snapshot itself.
-				err = d.CreateVolumeSnapshot(vol.volType, vol.name, snapName, op)
+				err = d.CreateVolumeSnapshot(vol, snapName, op)
 				if err != nil {
 					return err
 				}
@@ -393,7 +393,7 @@ func (d *cephfs) CreateVolumeFromCopy(vol Volume, srcVol Volume, copySnapshots b
 		}
 
 		// Apply the volume quota if specified.
-		err = d.SetVolumeQuota(vol.volType, vol.name, vol.config["size"], op)
+		err = d.SetVolumeQuota(vol, vol.config["size"], op)
 		if err != nil {
 			return err
 		}
@@ -416,12 +416,12 @@ func (d *cephfs) RefreshVolume(vol Volume, srcVol Volume, srcSnapshots []Volume,
 	return ErrNotImplemented
 }
 
-func (d *cephfs) DeleteVolume(volType VolumeType, volName string, op *operations.Operation) error {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) DeleteVolume(vol Volume, op *operations.Operation) error {
+	if vol.volType != VolumeTypeCustom {
 		return fmt.Errorf("Volume type not supported")
 	}
 
-	snapshots, err := d.VolumeSnapshots(volType, volName, op)
+	snapshots, err := d.VolumeSnapshots(vol, op)
 	if err != nil {
 		return err
 	}
@@ -430,7 +430,7 @@ func (d *cephfs) DeleteVolume(volType VolumeType, volName string, op *operations
 		return fmt.Errorf("Cannot remove a volume that has snapshots")
 	}
 
-	volPath := GetVolumeMountPath(d.name, volType, volName)
+	volPath := GetVolumeMountPath(d.name, vol.volType, vol.name)
 
 	// If the volume doesn't exist, then nothing more to do.
 	if !shared.PathExists(volPath) {
@@ -445,7 +445,7 @@ func (d *cephfs) DeleteVolume(volType VolumeType, volName string, op *operations
 
 	// Although the volume snapshot directory should already be removed, lets remove it here
 	// to just in case the top-level directory is left.
-	snapshotDir := GetVolumeSnapshotDir(d.name, volType, volName)
+	snapshotDir := GetVolumeSnapshotDir(d.name, vol.volType, vol.name)
 
 	err = os.RemoveAll(snapshotDir)
 	if err != nil {
@@ -455,15 +455,13 @@ func (d *cephfs) DeleteVolume(volType VolumeType, volName string, op *operations
 	return nil
 }
 
-func (d *cephfs) RenameVolume(volType VolumeType, volName string, newName string, op *operations.Operation) error {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) RenameVolume(vol Volume, newName string, op *operations.Operation) error {
+	if vol.volType != VolumeTypeCustom {
 		return fmt.Errorf("Volume type not supported")
 	}
 
-	vol := NewVolume(d, d.name, volType, ContentTypeFS, volName, nil)
-
 	// Create new snapshots directory.
-	snapshotDir := GetVolumeSnapshotDir(d.name, volType, newName)
+	snapshotDir := GetVolumeSnapshotDir(d.name, vol.volType, newName)
 
 	err := os.MkdirAll(snapshotDir, 0711)
 	if err != nil {
@@ -495,10 +493,10 @@ func (d *cephfs) RenameVolume(volType VolumeType, volName string, newName string
 	}()
 
 	// Rename the snapshot directory first.
-	srcSnapshotDir := GetVolumeSnapshotDir(d.name, volType, volName)
+	srcSnapshotDir := GetVolumeSnapshotDir(d.name, vol.volType, vol.name)
 
 	if shared.PathExists(srcSnapshotDir) {
-		targetSnapshotDir := GetVolumeSnapshotDir(d.name, volType, newName)
+		targetSnapshotDir := GetVolumeSnapshotDir(d.name, vol.volType, newName)
 
 		err = os.Rename(srcSnapshotDir, targetSnapshotDir)
 		if err != nil {
@@ -517,16 +515,16 @@ func (d *cephfs) RenameVolume(volType VolumeType, volName string, newName string
 		return err
 	}
 
-	sourcePath := GetVolumeMountPath(d.name, volType, newName)
-	targetPath := GetVolumeMountPath(d.name, volType, newName)
+	sourcePath := GetVolumeMountPath(d.name, vol.volType, newName)
+	targetPath := GetVolumeMountPath(d.name, vol.volType, newName)
 
 	for _, snapshot := range snapshots {
 		// Figure out the snapshot paths.
 		_, snapName, _ := shared.InstanceGetParentAndSnapshotName(snapshot.name)
 		oldCephSnapPath := filepath.Join(sourcePath, ".snap", snapName)
 		newCephSnapPath := filepath.Join(targetPath, ".snap", snapName)
-		oldPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, snapName))
-		newPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(newName, snapName))
+		oldPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, snapName))
+		newPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(newName, snapName))
 
 		// Update the symlink.
 		err = os.Symlink(newCephSnapPath, newPath)
@@ -541,8 +539,8 @@ func (d *cephfs) RenameVolume(volType VolumeType, volName string, newName string
 		})
 	}
 
-	oldPath := GetVolumeMountPath(d.name, volType, volName)
-	newPath := GetVolumeMountPath(d.name, volType, newName)
+	oldPath := GetVolumeMountPath(d.name, vol.volType, vol.name)
+	newPath := GetVolumeMountPath(d.name, vol.volType, newName)
 	err = os.Rename(oldPath, newPath)
 	if err != nil {
 		return err
@@ -563,11 +561,11 @@ func (d *cephfs) UpdateVolume(vol Volume, changedConfig map[string]string) error
 		return nil
 	}
 
-	return d.SetVolumeQuota(vol.volType, vol.name, value, nil)
+	return d.SetVolumeQuota(vol, value, nil)
 }
 
-func (d *cephfs) GetVolumeUsage(volType VolumeType, volName string) (int64, error) {
-	out, err := shared.RunCommand("getfattr", "-n", "ceph.quota.max_bytes", "--only-values", GetVolumeMountPath(d.name, volType, volName))
+func (d *cephfs) GetVolumeUsage(vol Volume) (int64, error) {
+	out, err := shared.RunCommand("getfattr", "-n", "ceph.quota.max_bytes", "--only-values", GetVolumeMountPath(d.name, vol.volType, vol.name))
 	if err != nil {
 		return -1, err
 	}
@@ -580,7 +578,7 @@ func (d *cephfs) GetVolumeUsage(volType VolumeType, volName string) (int64, erro
 	return size, nil
 }
 
-func (d *cephfs) SetVolumeQuota(volType VolumeType, volName, size string, op *operations.Operation) error {
+func (d *cephfs) SetVolumeQuota(vol Volume, size string, op *operations.Operation) error {
 	if size == "" || size == "0" {
 		size = d.config["volume.size"]
 	}
@@ -590,49 +588,49 @@ func (d *cephfs) SetVolumeQuota(volType VolumeType, volName, size string, op *op
 		return err
 	}
 
-	_, err = shared.RunCommand("setfattr", "-n", "ceph.quota.max_bytes", "-v", fmt.Sprintf("%d", sizeBytes), GetVolumeMountPath(d.name, volType, volName))
+	_, err = shared.RunCommand("setfattr", "-n", "ceph.quota.max_bytes", "-v", fmt.Sprintf("%d", sizeBytes), GetVolumeMountPath(d.name, vol.volType, vol.name))
 	return err
 }
 
-func (d *cephfs) MountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error) {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) MountVolume(vol Volume, op *operations.Operation) (bool, error) {
+	if vol.volType != VolumeTypeCustom {
 		return false, fmt.Errorf("Volume type not supported")
 	}
 
 	return false, nil
 }
 
-func (d *cephfs) MountVolumeSnapshot(volType VolumeType, VolName, snapshotName string, op *operations.Operation) (bool, error) {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) MountVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) (bool, error) {
+	if vol.volType != VolumeTypeCustom {
 		return false, fmt.Errorf("Volume type not supported")
 	}
 
 	return false, nil
 }
 
-func (d *cephfs) UnmountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error) {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) UnmountVolume(vol Volume, op *operations.Operation) (bool, error) {
+	if vol.volType != VolumeTypeCustom {
 		return false, fmt.Errorf("Volume type not supported")
 	}
 
 	return false, nil
 }
 
-func (d *cephfs) UnmountVolumeSnapshot(volType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error) {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) UnmountVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) (bool, error) {
+	if vol.volType != VolumeTypeCustom {
 		return false, fmt.Errorf("Volume type not supported")
 	}
 
 	return false, nil
 }
 
-func (d *cephfs) CreateVolumeSnapshot(volType VolumeType, volName string, newSnapshotName string, op *operations.Operation) error {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) CreateVolumeSnapshot(vol Volume, newSnapshotName string, op *operations.Operation) error {
+	if vol.volType != VolumeTypeCustom {
 		return fmt.Errorf("Volume type not supported")
 	}
 
 	// Create the snapshot.
-	sourcePath := GetVolumeMountPath(d.name, volType, volName)
+	sourcePath := GetVolumeMountPath(d.name, vol.volType, vol.name)
 	cephSnapPath := filepath.Join(sourcePath, ".snap", newSnapshotName)
 
 	err := os.Mkdir(cephSnapPath, 0711)
@@ -640,7 +638,7 @@ func (d *cephfs) CreateVolumeSnapshot(volType VolumeType, volName string, newSna
 		return err
 	}
 
-	targetPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, newSnapshotName))
+	targetPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, newSnapshotName))
 
 	err = os.MkdirAll(filepath.Dir(targetPath), 0711)
 	if err != nil {
@@ -655,13 +653,13 @@ func (d *cephfs) CreateVolumeSnapshot(volType VolumeType, volName string, newSna
 	return nil
 }
 
-func (d *cephfs) DeleteVolumeSnapshot(volType VolumeType, volName string, snapshotName string, op *operations.Operation) error {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) DeleteVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) error {
+	if vol.volType != VolumeTypeCustom {
 		return fmt.Errorf("Volume type not supported")
 	}
 
 	// Delete the snapshot itself.
-	sourcePath := GetVolumeMountPath(d.name, volType, volName)
+	sourcePath := GetVolumeMountPath(d.name, vol.volType, vol.name)
 	cephSnapPath := filepath.Join(sourcePath, ".snap", snapshotName)
 
 	err := os.Remove(cephSnapPath)
@@ -670,7 +668,7 @@ func (d *cephfs) DeleteVolumeSnapshot(volType VolumeType, volName string, snapsh
 	}
 
 	// Remove the symlink.
-	snapPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, snapshotName))
+	snapPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, snapshotName))
 	err = os.Remove(snapPath)
 	if err != nil {
 		return err
@@ -679,12 +677,12 @@ func (d *cephfs) DeleteVolumeSnapshot(volType VolumeType, volName string, snapsh
 	return nil
 }
 
-func (d *cephfs) RenameVolumeSnapshot(volType VolumeType, volName string, snapshotName string, newSnapshotName string, op *operations.Operation) error {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) RenameVolumeSnapshot(vol Volume, snapshotName string, newSnapshotName string, op *operations.Operation) error {
+	if vol.volType != VolumeTypeCustom {
 		return fmt.Errorf("Volume type not supported")
 	}
 
-	sourcePath := GetVolumeMountPath(d.name, volType, volName)
+	sourcePath := GetVolumeMountPath(d.name, vol.volType, vol.name)
 	oldCephSnapPath := filepath.Join(sourcePath, ".snap", snapshotName)
 	newCephSnapPath := filepath.Join(sourcePath, ".snap", newSnapshotName)
 
@@ -694,13 +692,13 @@ func (d *cephfs) RenameVolumeSnapshot(volType VolumeType, volName string, snapsh
 	}
 
 	// Re-generate the snapshot symlink.
-	oldPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, snapshotName))
+	oldPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, snapshotName))
 	err = os.Remove(oldPath)
 	if err != nil {
 		return err
 	}
 
-	newPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, newSnapshotName))
+	newPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, newSnapshotName))
 	err = os.Symlink(newCephSnapPath, newPath)
 	if err != nil {
 		return err
@@ -709,12 +707,12 @@ func (d *cephfs) RenameVolumeSnapshot(volType VolumeType, volName string, snapsh
 	return nil
 }
 
-func (d *cephfs) VolumeSnapshots(volType VolumeType, volName string, op *operations.Operation) ([]string, error) {
-	if volType != VolumeTypeCustom {
+func (d *cephfs) VolumeSnapshots(vol Volume, op *operations.Operation) ([]string, error) {
+	if vol.volType != VolumeTypeCustom {
 		return nil, fmt.Errorf("Volume type not supported")
 	}
 
-	snapshotDir := GetVolumeSnapshotDir(d.name, volType, volName)
+	snapshotDir := GetVolumeSnapshotDir(d.name, vol.volType, vol.name)
 	snapshots := []string{}
 
 	ents, err := ioutil.ReadDir(snapshotDir)
@@ -834,7 +832,7 @@ func (d *cephfs) CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser,
 
 		// Remove any paths created if we are reverting.
 		for _, snapName := range revertSnaps {
-			d.DeleteVolumeSnapshot(vol.volType, vol.name, snapName, op)
+			d.DeleteVolumeSnapshot(vol, snapName, op)
 		}
 
 		os.RemoveAll(volPath)
@@ -858,7 +856,7 @@ func (d *cephfs) CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser,
 			}
 
 			// Create the snapshot itself.
-			err = d.CreateVolumeSnapshot(vol.volType, vol.name, snapName, op)
+			err = d.CreateVolumeSnapshot(vol, snapName, op)
 			if err != nil {
 				return err
 			}
@@ -868,7 +866,7 @@ func (d *cephfs) CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser,
 		}
 
 		// Apply the volume quota if specified.
-		err = d.SetVolumeQuota(vol.volType, vol.name, vol.config["size"], op)
+		err = d.SetVolumeQuota(vol, vol.config["size"], op)
 		if err != nil {
 			return err
 		}
diff --git a/lxd/storage/drivers/driver_dir.go b/lxd/storage/drivers/driver_dir.go
index 88de930d29..ca792876ea 100644
--- a/lxd/storage/drivers/driver_dir.go
+++ b/lxd/storage/drivers/driver_dir.go
@@ -138,15 +138,15 @@ func (d *dir) GetResources() (*api.ResourcesStoragePool, error) {
 }
 
 // GetVolumeUsage returns the disk space used by the volume.
-func (d *dir) GetVolumeUsage(volType VolumeType, volName string) (int64, error) {
-	volPath := GetVolumeMountPath(d.name, volType, volName)
+func (d *dir) GetVolumeUsage(vol Volume) (int64, error) {
+	volPath := GetVolumeMountPath(d.name, vol.volType, vol.name)
 	ok, err := quota.Supported(volPath)
 	if err != nil || !ok {
 		return 0, nil
 	}
 
 	// Get the volume ID for the volume to access quota.
-	volID, err := d.getVolID(volType, volName)
+	volID, err := d.getVolID(vol.volType, vol.name)
 	if err != nil {
 		return -1, err
 	}
@@ -168,8 +168,8 @@ func (d *dir) ValidateVolume(vol Volume, removeUnknownKeys bool) error {
 }
 
 // HasVolume indicates whether a specific volume exists on the storage pool.
-func (d *dir) HasVolume(volType VolumeType, volName string) bool {
-	if shared.PathExists(GetVolumeMountPath(d.name, volType, volName)) {
+func (d *dir) HasVolume(vol Volume) bool {
+	if shared.PathExists(GetVolumeMountPath(d.name, vol.volType, vol.name)) {
 		return true
 	}
 
@@ -177,8 +177,8 @@ func (d *dir) HasVolume(volType VolumeType, volName string) bool {
 }
 
 // GetVolumeDiskPath returns the location of a disk volume.
-func (d *dir) GetVolumeDiskPath(volType VolumeType, volName string) (string, error) {
-	return filepath.Join(GetVolumeMountPath(d.name, volType, volName), "root.img"), nil
+func (d *dir) GetVolumeDiskPath(vol Volume) (string, error) {
+	return filepath.Join(GetVolumeMountPath(d.name, vol.volType, vol.name), "root.img"), nil
 }
 
 // setupInitialQuota enables quota on a new volume and sets with an initial quota from config.
@@ -246,7 +246,7 @@ func (d *dir) CreateVolume(vol Volume, filler *VolumeFiller, op *operations.Oper
 	rootBlockPath := ""
 	if vol.contentType == ContentTypeBlock {
 		// We expect the filler to copy the VM image into this path.
-		rootBlockPath, err = d.GetVolumeDiskPath(vol.volType, vol.name)
+		rootBlockPath, err = d.GetVolumeDiskPath(vol)
 		if err != nil {
 			return err
 		}
@@ -389,7 +389,7 @@ func (d *dir) CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser, vol
 
 		// Remove any paths created if we are reverting.
 		for _, snapName := range revertSnaps {
-			d.DeleteVolumeSnapshot(vol.volType, vol.name, snapName, op)
+			d.DeleteVolumeSnapshot(vol, snapName, op)
 		}
 
 		os.RemoveAll(volPath)
@@ -424,7 +424,7 @@ func (d *dir) CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser, vol
 			}
 
 			// Create the snapshot itself.
-			err = d.CreateVolumeSnapshot(vol.volType, vol.name, snapName, op)
+			err = d.CreateVolumeSnapshot(vol, snapName, op)
 			if err != nil {
 				return err
 			}
@@ -531,7 +531,7 @@ func (d *dir) copyVolume(vol Volume, srcVol Volume, srcSnapshots []Volume, op *o
 
 		// Remove any paths created if we are reverting.
 		for _, snapName := range revertSnaps {
-			d.DeleteVolumeSnapshot(vol.volType, vol.name, snapName, op)
+			d.DeleteVolumeSnapshot(vol, snapName, op)
 		}
 
 		os.RemoveAll(volPath)
@@ -552,7 +552,7 @@ func (d *dir) copyVolume(vol Volume, srcVol Volume, srcSnapshots []Volume, op *o
 				}, op)
 
 				// Create the snapshot itself.
-				err = d.CreateVolumeSnapshot(vol.volType, vol.name, snapName, op)
+				err = d.CreateVolumeSnapshot(vol, snapName, op)
 				if err != nil {
 					return err
 				}
@@ -589,8 +589,8 @@ func (d *dir) copyVolume(vol Volume, srcVol Volume, srcSnapshots []Volume, op *o
 }
 
 // VolumeSnapshots returns a list of snapshots for the volume.
-func (d *dir) VolumeSnapshots(volType VolumeType, volName string, op *operations.Operation) ([]string, error) {
-	snapshotDir := GetVolumeSnapshotDir(d.name, volType, volName)
+func (d *dir) VolumeSnapshots(vol Volume, op *operations.Operation) ([]string, error) {
+	snapshotDir := GetVolumeSnapshotDir(d.name, vol.volType, vol.name)
 	snapshots := []string{}
 
 	ents, err := ioutil.ReadDir(snapshotDir)
@@ -642,11 +642,9 @@ func (d *dir) UpdateVolume(vol Volume, changedConfig map[string]string) error {
 }
 
 // RenameVolume renames a volume and its snapshots.
-func (d *dir) RenameVolume(volType VolumeType, volName string, newVolName string, op *operations.Operation) error {
-	vol := NewVolume(d, d.name, volType, ContentTypeFS, volName, nil)
-
+func (d *dir) RenameVolume(vol Volume, newVolName string, op *operations.Operation) error {
 	// Create new snapshots directory.
-	snapshotDir := GetVolumeSnapshotDir(d.name, volType, newVolName)
+	snapshotDir := GetVolumeSnapshotDir(d.name, vol.volType, newVolName)
 
 	err := os.MkdirAll(snapshotDir, 0711)
 	if err != nil {
@@ -694,8 +692,8 @@ func (d *dir) RenameVolume(volType VolumeType, volName string, newVolName string
 		})
 	}
 
-	oldPath := GetVolumeMountPath(d.name, volType, volName)
-	newPath := GetVolumeMountPath(d.name, volType, newVolName)
+	oldPath := GetVolumeMountPath(d.name, vol.volType, vol.name)
+	newPath := GetVolumeMountPath(d.name, vol.volType, newVolName)
 	err = os.Rename(oldPath, newPath)
 	if err != nil {
 		return err
@@ -707,7 +705,7 @@ func (d *dir) RenameVolume(volType VolumeType, volName string, newVolName string
 	})
 
 	// Remove old snapshots directory.
-	oldSnapshotDir := GetVolumeSnapshotDir(d.name, volType, volName)
+	oldSnapshotDir := GetVolumeSnapshotDir(d.name, vol.volType, vol.name)
 
 	err = os.RemoveAll(oldSnapshotDir)
 	if err != nil {
@@ -739,8 +737,8 @@ func (d *dir) RestoreVolume(vol Volume, snapshotName string, op *operations.Oper
 
 // DeleteVolume deletes a volume of the storage device. If any snapshots of the volume remain then
 // this function will return an error.
-func (d *dir) DeleteVolume(volType VolumeType, volName string, op *operations.Operation) error {
-	snapshots, err := d.VolumeSnapshots(volType, volName, op)
+func (d *dir) DeleteVolume(vol Volume, op *operations.Operation) error {
+	snapshots, err := d.VolumeSnapshots(vol, op)
 	if err != nil {
 		return err
 	}
@@ -749,7 +747,7 @@ func (d *dir) DeleteVolume(volType VolumeType, volName string, op *operations.Op
 		return fmt.Errorf("Cannot remove a volume that has snapshots")
 	}
 
-	volPath := GetVolumeMountPath(d.name, volType, volName)
+	volPath := GetVolumeMountPath(d.name, vol.volType, vol.name)
 
 	// If the volume doesn't exist, then nothing more to do.
 	if !shared.PathExists(volPath) {
@@ -757,7 +755,7 @@ func (d *dir) DeleteVolume(volType VolumeType, volName string, op *operations.Op
 	}
 
 	// Get the volume ID for the volume, which is used to remove project quota.
-	volID, err := d.getVolID(volType, volName)
+	volID, err := d.getVolID(vol.volType, vol.name)
 	if err != nil {
 		return err
 	}
@@ -776,7 +774,7 @@ func (d *dir) DeleteVolume(volType VolumeType, volName string, op *operations.Op
 
 	// Although the volume snapshot directory should already be removed, lets remove it here
 	// to just in case the top-level directory is left.
-	err = deleteParentSnapshotDirIfEmpty(d.name, volType, volName)
+	err = deleteParentSnapshotDirIfEmpty(d.name, vol.volType, vol.name)
 	if err != nil {
 		return err
 	}
@@ -786,32 +784,32 @@ func (d *dir) DeleteVolume(volType VolumeType, volName string, op *operations.Op
 
 // MountVolume simulates mounting a volume. As dir driver doesn't have volumes to mount it returns
 // false indicating that there is no need to issue an unmount.
-func (d *dir) MountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error) {
+func (d *dir) MountVolume(vol Volume, op *operations.Operation) (bool, error) {
 	return false, nil
 }
 
 // MountVolumeSnapshot sets up a read-only mount on top of the snapshot to avoid accidental modifications.
-func (d *dir) MountVolumeSnapshot(volType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error) {
-	snapPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, snapshotName))
+func (d *dir) MountVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) (bool, error) {
+	snapPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, snapshotName))
 	return mountReadOnly(snapPath, snapPath)
 }
 
 // UnmountVolume simulates unmounting a volume. As dir driver doesn't have volumes to unmount it
 // returns false indicating the volume was already unmounted.
-func (d *dir) UnmountVolume(volType VolumeType, volName string, op *operations.Operation) (bool, error) {
+func (d *dir) UnmountVolume(vol Volume, op *operations.Operation) (bool, error) {
 	return false, nil
 }
 
 // UnmountVolumeSnapshot removes the read-only mount placed on top of a snapshot.
-func (d *dir) UnmountVolumeSnapshot(volType VolumeType, volName, snapshotName string, op *operations.Operation) (bool, error) {
-	snapPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, snapshotName))
+func (d *dir) UnmountVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) (bool, error) {
+	snapPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, snapshotName))
 	return forceUnmount(snapPath)
 }
 
 // SetVolumeQuota sets the quota on the volume.
-func (d *dir) SetVolumeQuota(volType VolumeType, volName, size string, op *operations.Operation) error {
-	volPath := GetVolumeMountPath(d.name, volType, volName)
-	volID, err := d.getVolID(volType, volName)
+func (d *dir) SetVolumeQuota(vol Volume, size string, op *operations.Operation) error {
+	volPath := GetVolumeMountPath(d.name, vol.volType, vol.name)
+	volID, err := d.getVolID(vol.volType, vol.name)
 	if err != nil {
 		return err
 	}
@@ -903,10 +901,10 @@ func (d *dir) deleteQuota(path string, volID int64) error {
 }
 
 // CreateVolumeSnapshot creates a snapshot of a volume.
-func (d *dir) CreateVolumeSnapshot(volType VolumeType, volName string, newSnapshotName string, op *operations.Operation) error {
-	srcPath := GetVolumeMountPath(d.name, volType, volName)
-	fullSnapName := GetSnapshotVolumeName(volName, newSnapshotName)
-	snapVol := NewVolume(d, d.name, volType, ContentTypeFS, fullSnapName, nil)
+func (d *dir) CreateVolumeSnapshot(vol Volume, newSnapshotName string, op *operations.Operation) error {
+	srcPath := GetVolumeMountPath(d.name, vol.volType, vol.name)
+	fullSnapName := GetSnapshotVolumeName(vol.name, newSnapshotName)
+	snapVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, fullSnapName, nil)
 	snapPath := snapVol.MountPath()
 
 	// Create snapshot directory.
@@ -936,8 +934,8 @@ func (d *dir) CreateVolumeSnapshot(volType VolumeType, volName string, newSnapsh
 
 // DeleteVolumeSnapshot removes a snapshot from the storage device. The volName and snapshotName
 // must be bare names and should not be in the format "volume/snapshot".
-func (d *dir) DeleteVolumeSnapshot(volType VolumeType, volName string, snapshotName string, op *operations.Operation) error {
-	snapPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, snapshotName))
+func (d *dir) DeleteVolumeSnapshot(vol Volume, snapshotName string, op *operations.Operation) error {
+	snapPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, snapshotName))
 
 	// Remove the snapshot from the storage device.
 	err := os.RemoveAll(snapPath)
@@ -946,7 +944,7 @@ func (d *dir) DeleteVolumeSnapshot(volType VolumeType, volName string, snapshotN
 	}
 
 	// Remove the parent snapshot directory if this is the last snapshot being removed.
-	err = deleteParentSnapshotDirIfEmpty(d.name, volType, volName)
+	err = deleteParentSnapshotDirIfEmpty(d.name, vol.volType, vol.name)
 	if err != nil {
 		return err
 	}
@@ -955,9 +953,9 @@ func (d *dir) DeleteVolumeSnapshot(volType VolumeType, volName string, snapshotN
 }
 
 // RenameVolumeSnapshot renames a volume snapshot.
-func (d *dir) RenameVolumeSnapshot(volType VolumeType, volName string, snapshotName string, newSnapshotName string, op *operations.Operation) error {
-	oldPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, snapshotName))
-	newPath := GetVolumeMountPath(d.name, volType, GetSnapshotVolumeName(volName, newSnapshotName))
+func (d *dir) RenameVolumeSnapshot(vol Volume, snapshotName string, newSnapshotName string, op *operations.Operation) error {
+	oldPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, snapshotName))
+	newPath := GetVolumeMountPath(d.name, vol.volType, GetSnapshotVolumeName(vol.name, newSnapshotName))
 	err := os.Rename(oldPath, newPath)
 	if err != nil {
 		return err
diff --git a/lxd/storage/drivers/volume.go b/lxd/storage/drivers/volume.go
index 9bfb687a44..195d4bd695 100644
--- a/lxd/storage/drivers/volume.go
+++ b/lxd/storage/drivers/volume.go
@@ -115,7 +115,10 @@ func (v Volume) MountTask(task func(mountPath string, op *operations.Operation)
 	if isSnap {
 		unlock := lock(mountLockID)
 
-		ourMount, err := v.driver.MountVolumeSnapshot(v.volType, parentName, snapName, op)
+		// Override volume name
+		v.name = parentName
+
+		ourMount, err := v.driver.MountVolumeSnapshot(v, snapName, op)
 		if err != nil {
 			unlock()
 			return err
@@ -126,14 +129,14 @@ func (v Volume) MountTask(task func(mountPath string, op *operations.Operation)
 		if ourMount {
 			defer func() {
 				unlock := lock(umountLockID)
-				v.driver.UnmountVolumeSnapshot(v.volType, parentName, snapName, op)
+				v.driver.UnmountVolumeSnapshot(v, snapName, op)
 				unlock()
 			}()
 		}
 	} else {
 		unlock := lock(mountLockID)
 
-		ourMount, err := v.driver.MountVolume(v.volType, v.name, op)
+		ourMount, err := v.driver.MountVolume(v, op)
 		if err != nil {
 			unlock()
 			return err
@@ -144,7 +147,7 @@ func (v Volume) MountTask(task func(mountPath string, op *operations.Operation)
 		if ourMount {
 			defer func() {
 				unlock := lock(umountLockID)
-				v.driver.UnmountVolume(v.volType, v.name, op)
+				v.driver.UnmountVolume(v, op)
 				unlock()
 			}()
 		}
@@ -159,7 +162,7 @@ func (v Volume) Snapshots(op *operations.Operation) ([]Volume, error) {
 		return nil, fmt.Errorf("Volume is a snapshot")
 	}
 
-	snapshots, err := v.driver.VolumeSnapshots(v.volType, v.name, op)
+	snapshots, err := v.driver.VolumeSnapshots(v, op)
 	if err != nil {
 		return nil, err
 	}

From 3c43e0e739a175e7575df8c3a9cb67bc708e2aa8 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 12 Dec 2019 12:09:21 +0100
Subject: [PATCH 3/3] lxd/storage: Always pass Volume to drivers

This updates the calls to the driver functions in that it now always
passes a Volume.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/storage/backend_lxd.go | 201 ++++++++++++++++++++++++++++---------
 1 file changed, 152 insertions(+), 49 deletions(-)

diff --git a/lxd/storage/backend_lxd.go b/lxd/storage/backend_lxd.go
index 151434ab6c..30c1184ad0 100644
--- a/lxd/storage/backend_lxd.go
+++ b/lxd/storage/backend_lxd.go
@@ -502,21 +502,18 @@ func (b *lxdBackend) CreateInstanceFromCopy(inst instance.Instance, src instance
 
 	contentType := InstanceContentType(inst)
 
-	if b.driver.HasVolume(volType, project.Prefix(inst.Project(), inst.Name())) {
-		return fmt.Errorf("Cannot create volume, already exists on target")
-	}
-
 	// Get the root disk device config.
 	_, rootDiskConf, err := shared.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
 	if err != nil {
 		return err
 	}
 
-	// Get the volume name on storage.
-	volStorageName := project.Prefix(inst.Project(), inst.Name())
+	vol := b.newVolume(volType, contentType, project.Prefix(inst.Project(), inst.Name()),
+		rootDiskConf)
 
-	// Initialise a new volume containing the root disk config supplied in the new instance.
-	vol := b.newVolume(volType, contentType, volStorageName, rootDiskConf)
+	if b.driver.HasVolume(vol) {
+		return fmt.Errorf("Cannot create volume, already exists on target")
+	}
 
 	// Get the src volume name on storage.
 	srcVolStorageName := project.Prefix(src.Project(), src.Name())
@@ -878,7 +875,10 @@ func (b *lxdBackend) CreateInstanceFromMigration(inst instance.Instance, conn io
 
 	contentType := InstanceContentType(inst)
 
-	volExists := b.driver.HasVolume(volType, project.Prefix(inst.Project(), inst.Name()))
+	// There's no need to pass config as it's not needed in HasVolume().
+	vol := b.newVolume(volType, contentType, project.Prefix(inst.Project(), inst.Name()), nil)
+
+	volExists := b.driver.HasVolume(vol)
 	if args.Refresh && !volExists {
 		return fmt.Errorf("Cannot refresh volume, doesn't exist on target")
 	} else if !args.Refresh && volExists {
@@ -898,7 +898,7 @@ func (b *lxdBackend) CreateInstanceFromMigration(inst instance.Instance, conn io
 	// Get the volume name on storage.
 	volStorageName := project.Prefix(inst.Project(), args.Name)
 
-	vol := b.newVolume(volType, contentType, volStorageName, args.Config)
+	vol = b.newVolume(volType, contentType, volStorageName, args.Config)
 
 	var preFiller drivers.VolumeFiller
 
@@ -1040,7 +1040,11 @@ func (b *lxdBackend) RenameInstance(inst instance.Instance, newName string, op *
 	// Rename the volume and its snapshots on the storage device.
 	volStorageName := project.Prefix(inst.Project(), inst.Name())
 	newVolStorageName := project.Prefix(inst.Project(), newName)
-	err = b.driver.RenameVolume(volType, volStorageName, newVolStorageName, op)
+
+	// There's no need to pass config as it's not needed when renaming a volume.
+	vol := b.newVolume(volType, InstanceContentType(inst), volStorageName, nil)
+
+	err = b.driver.RenameVolume(vol, newVolStorageName, op)
 	if err != nil {
 		return err
 	}
@@ -1108,10 +1112,13 @@ func (b *lxdBackend) DeleteInstance(inst instance.Instance, op *operations.Opera
 	// Get the volume name on storage.
 	volStorageName := project.Prefix(inst.Project(), inst.Name())
 
+	// There's no need to pass config as it's not needed when deleting a volume.
+	vol := b.newVolume(volType, InstanceContentType(inst), volStorageName, nil)
+
 	// Delete the volume from the storage device. Must come after snapshots are removed.
 	// Must come before DB StoragePoolVolumeDelete so that the volume ID is still available.
 	logger.Debug("Deleting instance volume", log.Ctx{"volName": volStorageName})
-	err = b.driver.DeleteVolume(volType, volStorageName, op)
+	err = b.driver.DeleteVolume(vol, op)
 	if err != nil {
 		return err
 	}
@@ -1212,7 +1219,12 @@ func (b *lxdBackend) GetInstanceUsage(inst instance.Instance) (int64, error) {
 	defer logger.Debug("GetInstanceUsage finished")
 
 	if inst.Type() == instancetype.Container {
-		return b.driver.GetVolumeUsage(drivers.VolumeTypeContainer, inst.Name())
+		// There's no need to pass config as it's not needed when retrieving
+		// the volume usage.
+		vol := b.newVolume(drivers.VolumeTypeContainer, InstanceContentType(inst),
+			inst.Name(), nil)
+
+		return b.driver.GetVolumeUsage(vol)
 	}
 
 	return -1, ErrNotImplemented
@@ -1236,10 +1248,12 @@ func (b *lxdBackend) SetInstanceQuota(inst instance.Instance, size string, op *o
 		return err
 	}
 
-	// Get the volume name on storage.
-	volStorageName := project.Prefix(inst.Project(), inst.Name())
+	// Get the volume.
+	// There's no need to pass config as it's not needed when setting quotas.
+	vol := b.newVolume(volType, InstanceContentType(inst),
+		project.Prefix(inst.Project(), inst.Name()), nil)
 
-	return b.driver.SetVolumeQuota(volType, volStorageName, size, op)
+	return b.driver.SetVolumeQuota(vol, size, op)
 }
 
 // MountInstance mounts the instance's root volume.
@@ -1254,10 +1268,17 @@ func (b *lxdBackend) MountInstance(inst instance.Instance, op *operations.Operat
 		return false, err
 	}
 
-	// Get the volume name on storage.
-	volStorageName := project.Prefix(inst.Project(), inst.Name())
+	// Get the root disk device config.
+	_, rootDiskConf, err := shared.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
+	if err != nil {
+		return false, err
+	}
 
-	return b.driver.MountVolume(volType, volStorageName, op)
+	// Get the volume.
+	vol := b.newVolume(volType, InstanceContentType(inst),
+		project.Prefix(inst.Project(), inst.Name()), rootDiskConf)
+
+	return b.driver.MountVolume(vol, op)
 }
 
 // UnmountInstance unmounts the instance's root volume.
@@ -1272,10 +1293,17 @@ func (b *lxdBackend) UnmountInstance(inst instance.Instance, op *operations.Oper
 		return false, err
 	}
 
-	// Get the volume name on storage.
-	volStorageName := project.Prefix(inst.Project(), inst.Name())
+	// Get the root disk device config.
+	_, rootDiskConf, err := shared.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
+	if err != nil {
+		return false, err
+	}
+
+	// Get the volume.
+	vol := b.newVolume(volType, InstanceContentType(inst),
+		project.Prefix(inst.Project(), inst.Name()), rootDiskConf)
 
-	return b.driver.UnmountVolume(volType, volStorageName, op)
+	return b.driver.UnmountVolume(vol, op)
 }
 
 // GetInstanceDisk returns the location of the disk.
@@ -1290,11 +1318,14 @@ func (b *lxdBackend) GetInstanceDisk(inst instance.Instance) (string, error) {
 		return "", err
 	}
 
-	// Get the volume name on storage.
-	volStorageName := project.Prefix(inst.Project(), inst.Name())
+	// Get the volume.
+	// There's no need to pass config as it's not needed when getting the
+	// location of the disk block device.
+	vol := b.newVolume(volType, InstanceContentType(inst),
+		project.Prefix(inst.Project(), inst.Name()), nil)
 
 	// Get the location of the disk block device.
-	diskPath, err := b.driver.GetVolumeDiskPath(volType, volStorageName)
+	diskPath, err := b.driver.GetVolumeDiskPath(vol)
 	if err != nil {
 		return "", err
 	}
@@ -1340,10 +1371,13 @@ func (b *lxdBackend) CreateInstanceSnapshot(inst instance.Instance, src instance
 		return fmt.Errorf("Volume name must be a snapshot")
 	}
 
-	// Get the volume name on storage.
-	volStorageName := project.Prefix(inst.Project(), parentName)
+	// Get the volume.
+	// There's no need to pass config as it's not needed when creating volume
+	// snapshots.
+	vol := b.newVolume(volType, InstanceContentType(inst),
+		project.Prefix(inst.Project(), parentName), nil)
 
-	err = b.driver.CreateVolumeSnapshot(volType, volStorageName, snapName, op)
+	err = b.driver.CreateVolumeSnapshot(vol, snapName, op)
 	if err != nil {
 		return err
 	}
@@ -1387,8 +1421,12 @@ func (b *lxdBackend) RenameInstanceSnapshot(inst instance.Instance, newName stri
 	}
 
 	// Rename storage volume snapshot.
-	volStorageName := project.Prefix(inst.Project(), parentName)
-	err = b.driver.RenameVolumeSnapshot(volType, volStorageName, oldSnapshotName, newName, op)
+	// There's no need to pass config as it's not needed when renaming a volume
+	// snapshot.
+	vol := b.newVolume(volType, InstanceContentType(inst),
+		project.Prefix(inst.Project(), parentName), nil)
+
+	err = b.driver.RenameVolumeSnapshot(vol, oldSnapshotName, newName, op)
 	if err != nil {
 		return err
 	}
@@ -1397,7 +1435,12 @@ func (b *lxdBackend) RenameInstanceSnapshot(inst instance.Instance, newName stri
 	err = b.state.Cluster.StoragePoolVolumeRename(inst.Project(), inst.Name(), newVolName, volDBType, b.ID())
 	if err != nil {
 		// Revert rename.
-		b.driver.RenameVolumeSnapshot(drivers.VolumeTypeCustom, parentName, newName, oldSnapshotName, op)
+		// There's no need to pass config as it's not needed when renaming a
+		// volume snapshot.
+		vol := b.newVolume(drivers.VolumeTypeCustom, InstanceContentType(inst),
+			parentName, nil)
+
+		b.driver.RenameVolumeSnapshot(vol, newName, oldSnapshotName, op)
 		return err
 	}
 
@@ -1432,7 +1475,12 @@ func (b *lxdBackend) DeleteInstanceSnapshot(inst instance.Instance, op *operatio
 	// Delete the snapshot from the storage device.
 	// Must come before DB StoragePoolVolumeDelete so that the volume ID is still available.
 	logger.Debug("Deleting instance snapshot volume", log.Ctx{"volName": parentStorageName, "snapshotName": snapName})
-	err = b.driver.DeleteVolumeSnapshot(volType, parentStorageName, snapName, op)
+
+	// There's no need to pass config as it's not needed when deleting a volume
+	// snapshot.
+	vol := b.newVolume(volType, InstanceContentType(inst), parentStorageName, nil)
+
+	err = b.driver.DeleteVolumeSnapshot(vol, snapName, op)
 	if err != nil {
 		return err
 	}
@@ -1524,13 +1572,20 @@ func (b *lxdBackend) MountInstanceSnapshot(inst instance.Instance, op *operation
 		return false, err
 	}
 
+	// Get the root disk device config.
+	_, rootDiskConf, err := shared.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
+	if err != nil {
+		return false, err
+	}
+
 	// Get the parent and snapshot name.
 	parentName, snapName, _ := shared.InstanceGetParentAndSnapshotName(inst.Name())
 
-	// Get the volume name on storage.
-	volStorageName := project.Prefix(inst.Project(), parentName)
+	// Get the volume.
+	vol := b.newVolume(volType, InstanceContentType(inst),
+		project.Prefix(inst.Project(), parentName), rootDiskConf)
 
-	return b.driver.MountVolumeSnapshot(volType, volStorageName, snapName, op)
+	return b.driver.MountVolumeSnapshot(vol, snapName, op)
 }
 
 // UnmountInstanceSnapshot unmounts an instance snapshot.
@@ -1549,13 +1604,20 @@ func (b *lxdBackend) UnmountInstanceSnapshot(inst instance.Instance, op *operati
 		return false, err
 	}
 
+	// Get the root disk device config.
+	_, rootDiskConf, err := shared.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
+	if err != nil {
+		return false, err
+	}
+
 	// Get the parent and snapshot name.
 	parentName, snapName, _ := shared.InstanceGetParentAndSnapshotName(inst.Name())
 
-	// Get the volume name on storage.
-	volStorageName := project.Prefix(inst.Project(), parentName)
+	// Get the volume.
+	vol := b.newVolume(volType, InstanceContentType(inst),
+		project.Prefix(inst.Project(), parentName), rootDiskConf)
 
-	return b.driver.UnmountVolumeSnapshot(volType, volStorageName, snapName, op)
+	return b.driver.UnmountVolumeSnapshot(vol, snapName, op)
 }
 
 // EnsureImage creates an optimized volume of the image if supported by the storage pool driver and
@@ -1569,8 +1631,12 @@ func (b *lxdBackend) EnsureImage(fingerprint string, op *operations.Operation) e
 		return nil // Nothing to do for drivers that don't support optimized images volumes.
 	}
 
+	// There's no need to pass the content type or config. Both are not needed
+	// when checking the existence of volumes.
+	vol := b.newVolume(drivers.VolumeTypeImage, "", fingerprint, nil)
+
 	// Check if we already have a suitable volume.
-	if b.driver.HasVolume(drivers.VolumeTypeImage, fingerprint) {
+	if b.driver.HasVolume(vol) {
 		return nil
 	}
 
@@ -1618,7 +1684,11 @@ func (b *lxdBackend) DeleteImage(fingerprint string, op *operations.Operation) e
 		return fmt.Errorf("Invalid fingerprint")
 	}
 
-	err = b.driver.DeleteVolume(drivers.VolumeTypeImage, fingerprint, op)
+	// There's no need to pass the content type or config. Both are not needed
+	// when removing an image.
+	vol := b.newVolume(drivers.VolumeTypeImage, "", fingerprint, nil)
+
+	err = b.driver.DeleteVolume(vol, op)
 	if err != nil {
 		return err
 	}
@@ -1972,7 +2042,9 @@ func (b *lxdBackend) RenameCustomVolume(volName string, newVolName string, op *o
 		oldName: volName,
 	})
 
-	err = b.driver.RenameVolume(drivers.VolumeTypeCustom, volName, newVolName, op)
+	vol := b.newVolume(drivers.VolumeTypeCustom, "", volName, nil)
+
+	err = b.driver.RenameVolume(vol, newVolName, op)
 	if err != nil {
 		return err
 	}
@@ -2101,8 +2173,11 @@ func (b *lxdBackend) DeleteCustomVolume(volName string, op *operations.Operation
 		}
 	}
 
+	// There's no need to pass config as it's not needed when deleting a volume.
+	vol := b.newVolume(drivers.VolumeTypeCustom, drivers.ContentTypeFS, volName, nil)
+
 	// Delete the volume from the storage device. Must come after snapshots are removed.
-	err = b.driver.DeleteVolume(drivers.VolumeTypeCustom, volName, op)
+	err = b.driver.DeleteVolume(vol, op)
 	if err != nil {
 		return err
 	}
@@ -2118,7 +2193,11 @@ func (b *lxdBackend) DeleteCustomVolume(volName string, op *operations.Operation
 
 // GetCustomVolumeUsage returns the disk space used by the custom volume.
 func (b *lxdBackend) GetCustomVolumeUsage(volName string) (int64, error) {
-	return b.driver.GetVolumeUsage(drivers.VolumeTypeCustom, volName)
+	// There's no need to pass config as it's not needed when getting the volume
+	// usage.
+	vol := b.newVolume(drivers.VolumeTypeCustom, drivers.ContentTypeFS, volName, nil)
+
+	return b.driver.GetVolumeUsage(vol)
 }
 
 // MountCustomVolume mounts a custom volume.
@@ -2127,7 +2206,14 @@ func (b *lxdBackend) MountCustomVolume(volName string, op *operations.Operation)
 	logger.Debug("MountCustomVolume started")
 	defer logger.Debug("MountCustomVolume finished")
 
-	return b.driver.MountVolume(drivers.VolumeTypeCustom, volName, op)
+	_, volume, err := b.state.Cluster.StoragePoolNodeVolumeGetType(volName, db.StoragePoolVolumeTypeCustom, b.id)
+	if err != nil {
+		return false, err
+	}
+
+	vol := b.newVolume(drivers.VolumeTypeCustom, drivers.ContentTypeFS, volName, volume.Config)
+
+	return b.driver.MountVolume(vol, op)
 }
 
 // UnmountCustomVolume unmounts a custom volume.
@@ -2136,7 +2222,14 @@ func (b *lxdBackend) UnmountCustomVolume(volName string, op *operations.Operatio
 	logger.Debug("UnmountCustomVolume started")
 	defer logger.Debug("UnmountCustomVolume finished")
 
-	return b.driver.UnmountVolume(drivers.VolumeTypeCustom, volName, op)
+	_, volume, err := b.state.Cluster.StoragePoolNodeVolumeGetType(volName, db.StoragePoolVolumeTypeCustom, b.id)
+	if err != nil {
+		return false, err
+	}
+
+	vol := b.newVolume(drivers.VolumeTypeCustom, drivers.ContentTypeFS, volName, volume.Config)
+
+	return b.driver.UnmountVolume(vol, op)
 }
 
 // CreateCustomVolumeSnapshot creates a snapshot of a custom volume.
@@ -2188,8 +2281,11 @@ func (b *lxdBackend) CreateCustomVolumeSnapshot(volName string, newSnapshotName
 		}
 	}()
 
+	vol := b.newVolume(drivers.VolumeTypeCustom, drivers.ContentTypeFS, volName,
+		parentVol.Config)
+
 	// Create the snapshot on the storage device.
-	err = b.driver.CreateVolumeSnapshot(drivers.VolumeTypeCustom, volName, newSnapshotName, op)
+	err = b.driver.CreateVolumeSnapshot(vol, newSnapshotName, op)
 	if err != nil {
 		return err
 	}
@@ -2213,7 +2309,10 @@ func (b *lxdBackend) RenameCustomVolumeSnapshot(volName string, newSnapshotName
 		return fmt.Errorf("Invalid new snapshot name")
 	}
 
-	err := b.driver.RenameVolumeSnapshot(drivers.VolumeTypeCustom, parentName, oldSnapshotName, newSnapshotName, op)
+	// There's no need to pass config as it's not needed when renaming a volume.
+	vol := b.newVolume(drivers.VolumeTypeCustom, drivers.ContentTypeFS, parentName, nil)
+
+	err := b.driver.RenameVolumeSnapshot(vol, oldSnapshotName, newSnapshotName, op)
 	if err != nil {
 		return err
 	}
@@ -2222,7 +2321,7 @@ func (b *lxdBackend) RenameCustomVolumeSnapshot(volName string, newSnapshotName
 	err = b.state.Cluster.StoragePoolVolumeRename("default", volName, newVolName, db.StoragePoolVolumeTypeCustom, b.ID())
 	if err != nil {
 		// Revert rename.
-		b.driver.RenameVolumeSnapshot(drivers.VolumeTypeCustom, parentName, newSnapshotName, oldSnapshotName, op)
+		b.driver.RenameVolumeSnapshot(vol, newSnapshotName, oldSnapshotName, op)
 		return err
 	}
 
@@ -2240,9 +2339,13 @@ func (b *lxdBackend) DeleteCustomVolumeSnapshot(volName string, op *operations.O
 		return fmt.Errorf("Volume name must be a snapshot")
 	}
 
+	// There's no need to pass config as it's not needed when deleting a volume
+	// snapshot.
+	vol := b.newVolume(drivers.VolumeTypeCustom, drivers.ContentTypeFS, parentName, nil)
+
 	// Delete the snapshot from the storage device.
 	// Must come before DB StoragePoolVolumeDelete so that the volume ID is still available.
-	err := b.driver.DeleteVolumeSnapshot(drivers.VolumeTypeCustom, parentName, snapName, op)
+	err := b.driver.DeleteVolumeSnapshot(vol, snapName, op)
 	if err != nil {
 		return err
 	}


More information about the lxc-devel mailing list