[lxc-devel] [lxd/master] Storage fixes and refactoring

stgraber on Github lxc-bot at linuxcontainers.org
Thu Dec 12 21:47:00 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 468 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20191212/2768440e/attachment-0001.bin>
-------------- next part --------------
From 39fd96ee317334026363fd4720a81b078eff1196 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 28 Nov 2019 19:47:57 +0100
Subject: [PATCH 1/6] lxd/storage: Create image volume DB entry

This creates an image volume DB entry if the backend supports optimized
images.

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

diff --git a/lxd/storage/backend_lxd.go b/lxd/storage/backend_lxd.go
index 151434ab6c..7d3f265458 100644
--- a/lxd/storage/backend_lxd.go
+++ b/lxd/storage/backend_lxd.go
@@ -1600,6 +1600,11 @@ func (b *lxdBackend) EnsureImage(fingerprint string, op *operations.Operation) e
 		return err
 	}
 
+	err = VolumeDBCreate(b.state, b.name, fingerprint, "", db.StoragePoolVolumeTypeNameImage, false, nil)
+	if err != nil {
+		return err
+	}
+
 	return nil
 }
 

From dc1bdad831d1e4507bebc4fbdf6beb5f425b246a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 12 Dec 2019 14:45:09 -0500
Subject: [PATCH 2/6] lxd/images: Port to new storage functions
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/images.go | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/lxd/images.go b/lxd/images.go
index 989c89cbc9..bcc0c1b5b1 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -1367,6 +1367,21 @@ func pruneExpiredImages(ctx context.Context, d *Daemon) error {
 }
 
 func doDeleteImageFromPool(state *state.State, fingerprint string, storagePool string) error {
+	// New storage pool handling.
+	pool, err := storagePools.GetPoolByName(state, storagePool)
+	if err != storageDrivers.ErrUnknownDriver {
+		if err != nil {
+			return err
+		}
+
+		err = pool.DeleteImage(fingerprint, nil)
+		if err != nil {
+			return err
+		}
+
+		return nil
+	}
+
 	// Initialize a new storage interface.
 	s, err := storagePoolVolumeImageInit(state, storagePool, fingerprint)
 	if err != nil {

From 6e166ff9b06d02a96557b334aebe2e1c39f239ff Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 14 Nov 2019 10:59:51 +0100
Subject: [PATCH 3/6] lxd/storage: Move storage_cgo.go to drivers package

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/storage/{storage_cgo.go => drivers/driver_cgo.go} | 6 +++---
 lxd/storage_btrfs.go                                  | 3 ++-
 lxd/storage_lvm.go                                    | 7 ++++---
 3 files changed, 9 insertions(+), 7 deletions(-)
 rename lxd/storage/{storage_cgo.go => drivers/driver_cgo.go} (98%)

diff --git a/lxd/storage/storage_cgo.go b/lxd/storage/drivers/driver_cgo.go
similarity index 98%
rename from lxd/storage/storage_cgo.go
rename to lxd/storage/drivers/driver_cgo.go
index 879e94ff54..fd066095e1 100644
--- a/lxd/storage/storage_cgo.go
+++ b/lxd/storage/drivers/driver_cgo.go
@@ -1,7 +1,7 @@
 // +build linux
 // +build cgo
 
-package storage
+package drivers
 
 import (
 	"fmt"
@@ -29,8 +29,8 @@ import (
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include "../include/macro.h"
-#include "../include/memory_utils.h"
+#include "../../include/macro.h"
+#include "../../include/memory_utils.h"
 
 #define LXD_MAXPATH 4096
 #define LXD_NUMSTRLEN64 21
diff --git a/lxd/storage_btrfs.go b/lxd/storage_btrfs.go
index 60eebee01e..fcceb6859a 100644
--- a/lxd/storage_btrfs.go
+++ b/lxd/storage_btrfs.go
@@ -25,6 +25,7 @@ import (
 	"github.com/lxc/lxd/lxd/rsync"
 	"github.com/lxc/lxd/lxd/state"
 	driver "github.com/lxc/lxd/lxd/storage"
+	"github.com/lxc/lxd/lxd/storage/drivers"
 	"github.com/lxc/lxd/lxd/util"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
@@ -428,7 +429,7 @@ func (s *storageBtrfs) StoragePoolMount() (bool, error) {
 			// Since we mount the loop device LO_FLAGS_AUTOCLEAR is
 			// fine since the loop device will be kept around for as
 			// long as the mount exists.
-			loopF, loopErr := driver.PrepareLoopDev(source, driver.LoFlagsAutoclear)
+			loopF, loopErr := drivers.PrepareLoopDev(source, drivers.LoFlagsAutoclear)
 			if loopErr != nil {
 				return false, loopErr
 			}
diff --git a/lxd/storage_lvm.go b/lxd/storage_lvm.go
index 51b46e935b..f9a5880221 100644
--- a/lxd/storage_lvm.go
+++ b/lxd/storage_lvm.go
@@ -20,6 +20,7 @@ import (
 	"github.com/lxc/lxd/lxd/project"
 	"github.com/lxc/lxd/lxd/rsync"
 	driver "github.com/lxc/lxd/lxd/storage"
+	"github.com/lxc/lxd/lxd/storage/drivers"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
 	"github.com/lxc/lxd/shared/ioprogress"
@@ -397,7 +398,7 @@ func (s *storageLvm) StoragePoolDelete() error {
 	if s.loopInfo != nil {
 		// Set LO_FLAGS_AUTOCLEAR before we remove the loop file
 		// otherwise we will get EBADF.
-		err = driver.SetAutoclearOnLoopDev(int(s.loopInfo.Fd()))
+		err = drivers.SetAutoclearOnLoopDev(int(s.loopInfo.Fd()))
 		if err != nil {
 			logger.Warnf("Failed to set LO_FLAGS_AUTOCLEAR on loop device: %s, manual cleanup needed", err)
 		}
@@ -469,12 +470,12 @@ func (s *storageLvm) StoragePoolMount() (bool, error) {
 
 	if filepath.IsAbs(source) && !shared.IsBlockdevPath(source) {
 		// Try to prepare new loop device.
-		loopF, loopErr := driver.PrepareLoopDev(source, 0)
+		loopF, loopErr := drivers.PrepareLoopDev(source, 0)
 		if loopErr != nil {
 			return false, loopErr
 		}
 		// Make sure that LO_FLAGS_AUTOCLEAR is unset.
-		loopErr = driver.UnsetAutoclearOnLoopDev(int(loopF.Fd()))
+		loopErr = drivers.UnsetAutoclearOnLoopDev(int(loopF.Fd()))
 		if loopErr != nil {
 			return false, loopErr
 		}

From c8ebc4301d48026b10c15d93bea813d6fbc20ad9 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 4 Dec 2019 10:48:02 +0100
Subject: [PATCH 4/6] lxd/storage/drivers: Add FS and mount functions

This adds some filesystem and mount functions to the drivers package.

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

diff --git a/lxd/storage/drivers/utils.go b/lxd/storage/drivers/utils.go
index 54cb77c16f..3acc916a2f 100644
--- a/lxd/storage/drivers/utils.go
+++ b/lxd/storage/drivers/utils.go
@@ -5,6 +5,7 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"strings"
 	"time"
 
 	"golang.org/x/sys/unix"
@@ -14,6 +15,11 @@ import (
 	"github.com/lxc/lxd/shared/api"
 )
 
+// MkfsOptions represents options for filesystem creation.
+type MkfsOptions struct {
+	Label string
+}
+
 func wipeDirectory(path string) error {
 	// List all entries
 	entries, err := ioutil.ReadDir(path)
@@ -235,3 +241,93 @@ func createSparseFile(filePath string, sizeBytes int64) error {
 
 	return nil
 }
+
+// MakeFSType creates the provided filesystem.
+func MakeFSType(path string, fsType string, options *MkfsOptions) (string, error) {
+	var err error
+	var msg string
+
+	fsOptions := options
+	if fsOptions == nil {
+		fsOptions = &MkfsOptions{}
+	}
+
+	cmd := []string{fmt.Sprintf("mkfs.%s", fsType), path}
+	if fsOptions.Label != "" {
+		cmd = append(cmd, "-L", fsOptions.Label)
+	}
+
+	if fsType == "ext4" {
+		cmd = append(cmd, "-E", "nodiscard,lazy_itable_init=0,lazy_journal_init=0")
+	}
+
+	msg, err = shared.TryRunCommand(cmd[0], cmd[1:]...)
+	if err != nil {
+		return msg, err
+	}
+
+	return "", nil
+}
+
+// Export the mount options map since we might find it useful in other parts of
+// LXD.
+type mountOptions struct {
+	capture bool
+	flag    uintptr
+}
+
+// mountOptions represents a list of possible mount options.
+var mountOptions = map[string]mountOptions{
+	"async":         {false, unix.MS_SYNCHRONOUS},
+	"atime":         {false, unix.MS_NOATIME},
+	"bind":          {true, unix.MS_BIND},
+	"defaults":      {true, 0},
+	"dev":           {false, unix.MS_NODEV},
+	"diratime":      {false, unix.MS_NODIRATIME},
+	"dirsync":       {true, unix.MS_DIRSYNC},
+	"exec":          {false, unix.MS_NOEXEC},
+	"lazytime":      {true, unix.MS_LAZYTIME},
+	"mand":          {true, unix.MS_MANDLOCK},
+	"noatime":       {true, unix.MS_NOATIME},
+	"nodev":         {true, unix.MS_NODEV},
+	"nodiratime":    {true, unix.MS_NODIRATIME},
+	"noexec":        {true, unix.MS_NOEXEC},
+	"nomand":        {false, unix.MS_MANDLOCK},
+	"norelatime":    {false, unix.MS_RELATIME},
+	"nostrictatime": {false, unix.MS_STRICTATIME},
+	"nosuid":        {true, unix.MS_NOSUID},
+	"rbind":         {true, unix.MS_BIND | unix.MS_REC},
+	"relatime":      {true, unix.MS_RELATIME},
+	"remount":       {true, unix.MS_REMOUNT},
+	"ro":            {true, unix.MS_RDONLY},
+	"rw":            {false, unix.MS_RDONLY},
+	"strictatime":   {true, unix.MS_STRICTATIME},
+	"suid":          {false, unix.MS_NOSUID},
+	"sync":          {true, unix.MS_SYNCHRONOUS},
+}
+
+// ResolveMountOptions resolves the provided mount options.
+func ResolveMountOptions(options string) (uintptr, string) {
+	mountFlags := uintptr(0)
+	tmp := strings.SplitN(options, ",", -1)
+	for i := 0; i < len(tmp); i++ {
+		opt := tmp[i]
+		do, ok := MountOptions[opt]
+		if !ok {
+			continue
+		}
+
+		if do.capture {
+			mountFlags |= do.flag
+		} else {
+			mountFlags &= ^do.flag
+		}
+
+		copy(tmp[i:], tmp[i+1:])
+		tmp[len(tmp)-1] = ""
+		tmp = tmp[:len(tmp)-1]
+		i--
+	}
+
+	return mountFlags, strings.Join(tmp, ",")
+}

From 05c7d265279a9205b88d3f649e859bd449dd7ae9 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 4 Dec 2019 10:49:04 +0100
Subject: [PATCH 5/6] lxd: Use FS and mount functions from drivers package

This changes the storage code to use the filesystem and mount functions
from the drivers package.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/storage_btrfs.go      |  8 ++++----
 lxd/storage_ceph.go       | 11 ++++++-----
 lxd/storage_ceph_utils.go |  7 ++++---
 lxd/storage_lvm.go        |  8 ++++----
 lxd/storage_lvm_utils.go  |  3 ++-
 5 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/lxd/storage_btrfs.go b/lxd/storage_btrfs.go
index fcceb6859a..556e50e917 100644
--- a/lxd/storage_btrfs.go
+++ b/lxd/storage_btrfs.go
@@ -163,7 +163,7 @@ func (s *storageBtrfs) StoragePoolCreate() error {
 			return fmt.Errorf("Failed to create sparse file %s: %s", source, err)
 		}
 
-		output, err := driver.MakeFSType(source, "btrfs", &driver.MkfsOptions{Label: s.pool.Name})
+		output, err := drivers.MakeFSType(source, "btrfs", &drivers.MkfsOptions{Label: s.pool.Name})
 		if err != nil {
 			return fmt.Errorf("Failed to create the BTRFS pool: %v (%s)", err, output)
 		}
@@ -174,7 +174,7 @@ func (s *storageBtrfs) StoragePoolCreate() error {
 		if filepath.IsAbs(source) {
 			isBlockDev = shared.IsBlockdevPath(source)
 			if isBlockDev {
-				output, err := driver.MakeFSType(source, "btrfs", &driver.MkfsOptions{Label: s.pool.Name})
+				output, err := drivers.MakeFSType(source, "btrfs", &drivers.MkfsOptions{Label: s.pool.Name})
 				if err != nil {
 					return fmt.Errorf("Failed to create the BTRFS pool: %v (%s)", err, output)
 				}
@@ -222,7 +222,7 @@ func (s *storageBtrfs) StoragePoolCreate() error {
 
 	var err1 error
 	var devUUID string
-	mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getBtrfsMountOptions())
+	mountFlags, mountOptions := drivers.ResolveMountOptions(s.getBtrfsMountOptions())
 	mountFlags |= s.remount
 	if isBlockDev && filepath.IsAbs(source) {
 		devUUID, _ = shared.LookupUUIDByBlockDevPath(source)
@@ -415,7 +415,7 @@ func (s *storageBtrfs) StoragePoolMount() (bool, error) {
 		return false, nil
 	}
 
-	mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getBtrfsMountOptions())
+	mountFlags, mountOptions := drivers.ResolveMountOptions(s.getBtrfsMountOptions())
 	mountSource := source
 	isBlockDev := shared.IsBlockdevPath(source)
 	if filepath.IsAbs(source) {
diff --git a/lxd/storage_ceph.go b/lxd/storage_ceph.go
index 6596bdbd5d..750112b03c 100644
--- a/lxd/storage_ceph.go
+++ b/lxd/storage_ceph.go
@@ -23,6 +23,7 @@ import (
 	"github.com/lxc/lxd/lxd/project"
 	"github.com/lxc/lxd/lxd/rsync"
 	driver "github.com/lxc/lxd/lxd/storage"
+	storageDrivers "github.com/lxc/lxd/lxd/storage/drivers"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
 	"github.com/lxc/lxd/shared/ioprogress"
@@ -372,7 +373,7 @@ func (s *storageCeph) StoragePoolVolumeCreate() error {
 	RBDFilesystem := s.getRBDFilesystem()
 	logger.Debugf(`Retrieved filesystem type "%s" of RBD storage volume "%s" on storage pool "%s"`, RBDFilesystem, s.volume.Name, s.pool.Name)
 
-	output, err := driver.MakeFSType(RBDDevPath, RBDFilesystem, nil)
+	output, err := storageDrivers.MakeFSType(RBDDevPath, RBDFilesystem, nil)
 	if err != nil {
 		logger.Errorf(`Failed to create filesystem type "%s" on device path "%s" for RBD storage volume "%s" on storage pool "%s": %v (%s)`, RBDFilesystem, RBDDevPath, s.volume.Name, s.pool.Name, err, output)
 		return err
@@ -522,7 +523,7 @@ func (s *storageCeph) StoragePoolVolumeMount() (bool, error) {
 		RBDDevPath, ret = getRBDMappedDevPath(s.ClusterName, s.OSDPoolName,
 			storagePoolVolumeTypeNameCustom, s.volume.Name, true,
 			s.UserName)
-		mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getRBDMountOptions())
+		mountFlags, mountOptions := storageDrivers.ResolveMountOptions(s.getRBDMountOptions())
 		customerr = driver.TryMount(
 			RBDDevPath,
 			volumeMntPoint,
@@ -1833,7 +1834,7 @@ func (s *storageCeph) ContainerSnapshotStart(c instance.Instance) (bool, error)
 
 	containerMntPoint := driver.GetSnapshotMountPoint(c.Project(), s.pool.Name, containerName)
 	RBDFilesystem := s.getRBDFilesystem()
-	mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getRBDMountOptions())
+	mountFlags, mountOptions := storageDrivers.ResolveMountOptions(s.getRBDMountOptions())
 	if RBDFilesystem == "xfs" {
 		idx := strings.Index(mountOptions, "nouuid")
 		if idx < 0 {
@@ -2105,7 +2106,7 @@ func (s *storageCeph) ImageCreate(fingerprint string, tracker *ioprogress.Progre
 
 		// get filesystem
 		RBDFilesystem := s.getRBDFilesystem()
-		output, err := driver.MakeFSType(RBDDevPath, RBDFilesystem, nil)
+		output, err := storageDrivers.MakeFSType(RBDDevPath, RBDFilesystem, nil)
 		if err != nil {
 			logger.Errorf(`Failed to create filesystem "%s" for RBD storage volume for image "%s" on storage pool "%s": %v (%s)`, RBDFilesystem, fingerprint, s.pool.Name, err, output)
 			return err
@@ -2340,7 +2341,7 @@ func (s *storageCeph) ImageMount(fingerprint string) (bool, error) {
 
 	RBDFilesystem := s.getRBDFilesystem()
 	RBDMountOptions := s.getRBDMountOptions()
-	mountFlags, mountOptions := driver.LXDResolveMountoptions(RBDMountOptions)
+	mountFlags, mountOptions := storageDrivers.ResolveMountOptions(RBDMountOptions)
 	RBDDevPath, ret := getRBDMappedDevPath(s.ClusterName, s.OSDPoolName,
 		storagePoolVolumeTypeNameImage, fingerprint, true, s.UserName)
 	errMsg := fmt.Sprintf("Failed to mount RBD device %s onto %s",
diff --git a/lxd/storage_ceph_utils.go b/lxd/storage_ceph_utils.go
index f5d5b75f74..0a2abbdd98 100644
--- a/lxd/storage_ceph_utils.go
+++ b/lxd/storage_ceph_utils.go
@@ -20,6 +20,7 @@ import (
 	"github.com/lxc/lxd/lxd/project"
 	"github.com/lxc/lxd/lxd/rsync"
 	driver "github.com/lxc/lxd/lxd/storage"
+	"github.com/lxc/lxd/lxd/storage/drivers"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
 	"github.com/lxc/lxd/shared/logger"
@@ -1684,7 +1685,7 @@ func (s *storageCeph) cephRBDVolumeBackupCreate(tmpPath string, backup backup.Ba
 	}
 
 	// Mount the volume
-	mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getRBDMountOptions())
+	mountFlags, mountOptions := drivers.ResolveMountOptions(s.getRBDMountOptions())
 	err = driver.TryMount(RBDDevPath, tmpContainerMntPoint, RBDFilesystem, mountFlags, mountOptions)
 	if err != nil {
 		logger.Errorf("Failed to mount RBD device %s onto %s: %s", RBDDevPath, tmpContainerMntPoint, err)
@@ -1771,7 +1772,7 @@ func (s *storageCeph) doContainerCreate(projectName, name string, privileged boo
 
 	// get filesystem
 	RBDFilesystem := s.getRBDFilesystem()
-	output, err := driver.MakeFSType(RBDDevPath, RBDFilesystem, nil)
+	output, err := drivers.MakeFSType(RBDDevPath, RBDFilesystem, nil)
 	if err != nil {
 		logger.Errorf(`Failed to create filesystem type "%s" on device path "%s" for RBD storage volume for container "%s" on storage pool "%s": %v (%s)`, RBDFilesystem, RBDDevPath, name, s.pool.Name, err, output)
 		return err
@@ -1838,7 +1839,7 @@ func (s *storageCeph) doContainerMount(projectName string, name string) (bool, e
 			s.OSDPoolName, storagePoolVolumeTypeNameContainer,
 			volumeName, true, s.UserName)
 		if ret >= 0 {
-			mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getRBDMountOptions())
+			mountFlags, mountOptions := drivers.ResolveMountOptions(s.getRBDMountOptions())
 			mounterr = driver.TryMount(RBDDevPath, containerMntPoint,
 				RBDFilesystem, mountFlags, mountOptions)
 			ourMount = true
diff --git a/lxd/storage_lvm.go b/lxd/storage_lvm.go
index f9a5880221..2a8d924972 100644
--- a/lxd/storage_lvm.go
+++ b/lxd/storage_lvm.go
@@ -629,7 +629,7 @@ func (s *storageLvm) StoragePoolVolumeMount() (bool, error) {
 	var customerr error
 	ourMount := false
 	if !shared.IsMountPoint(customPoolVolumeMntPoint) {
-		mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getLvmMountOptions())
+		mountFlags, mountOptions := drivers.ResolveMountOptions(s.getLvmMountOptions())
 		customerr = driver.TryMount(lvmVolumePath, customPoolVolumeMntPoint, lvFsType, mountFlags, mountOptions)
 		ourMount = true
 	}
@@ -1271,7 +1271,7 @@ func (s *storageLvm) doContainerMount(project, name string, snap bool) (bool, er
 	var mounterr error
 	ourMount := false
 	if !shared.IsMountPoint(containerMntPoint) {
-		mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getLvmMountOptions())
+		mountFlags, mountOptions := drivers.ResolveMountOptions(s.getLvmMountOptions())
 		if snap && lvFsType == "xfs" {
 			idx := strings.Index(mountOptions, "nouuid")
 			if idx < 0 {
@@ -1602,7 +1602,7 @@ func (s *storageLvm) ContainerSnapshotStart(container instance.Instance) (bool,
 	containerMntPoint := driver.GetSnapshotMountPoint(container.Project(), s.pool.Name, containerName)
 	if !shared.IsMountPoint(containerMntPoint) {
 		mntOptString := s.getLvmMountOptions()
-		mountFlags, mountOptions := driver.LXDResolveMountoptions(mntOptString)
+		mountFlags, mountOptions := drivers.ResolveMountOptions(mntOptString)
 
 		if lvFsType == "xfs" {
 			idx := strings.Index(mountOptions, "nouuid")
@@ -2023,7 +2023,7 @@ func (s *storageLvm) ImageMount(fingerprint string) (bool, error) {
 
 	poolName := s.getOnDiskPoolName()
 	lvmVolumePath := getLvmDevPath("default", poolName, storagePoolVolumeAPIEndpointImages, fingerprint)
-	mountFlags, mountOptions := driver.LXDResolveMountoptions(s.getLvmMountOptions())
+	mountFlags, mountOptions := drivers.ResolveMountOptions(s.getLvmMountOptions())
 	err := driver.TryMount(lvmVolumePath, imageMntPoint, lvmFstype, mountFlags, mountOptions)
 	if err != nil {
 		logger.Errorf(fmt.Sprintf("Error mounting image LV for unpacking: %s", err))
diff --git a/lxd/storage_lvm_utils.go b/lxd/storage_lvm_utils.go
index cec7c223f9..ae3085f8e6 100644
--- a/lxd/storage_lvm_utils.go
+++ b/lxd/storage_lvm_utils.go
@@ -17,6 +17,7 @@ import (
 	"github.com/lxc/lxd/lxd/rsync"
 	"github.com/lxc/lxd/lxd/state"
 	driver "github.com/lxc/lxd/lxd/storage"
+	storageDrivers "github.com/lxc/lxd/lxd/storage/drivers"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
 	"github.com/lxc/lxd/shared/logger"
@@ -856,7 +857,7 @@ func lvmCreateLv(projectName, vgName string, thinPoolName string, lvName string,
 
 	fsPath := getLvmDevPath(projectName, vgName, volumeType, lvName)
 
-	output, err = driver.MakeFSType(fsPath, lvFsType, nil)
+	output, err = storageDrivers.MakeFSType(fsPath, lvFsType, nil)
 	if err != nil {
 		logger.Errorf("Filesystem creation failed: %v (%s)", err, output)
 		return fmt.Errorf("Error making filesystem on image LV: %v (%s)", err, output)

From a3db7d19ec49341b1b8ece8dd0fdbb5f54467cda Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 4 Dec 2019 10:49:51 +0100
Subject: [PATCH 6/6] lxd/storage: Remove FS and mount functions

This removes some filesystem and mount function from the storage package
which are now located in the storage/drivers package.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/storage/utils.go | 58 --------------------------------------------
 1 file changed, 58 deletions(-)

diff --git a/lxd/storage/utils.go b/lxd/storage/utils.go
index db4eb0f1ff..1ec383dcbc 100644
--- a/lxd/storage/utils.go
+++ b/lxd/storage/utils.go
@@ -31,11 +31,6 @@ var baseDirectories = map[drivers.VolumeType][]string{
 // VolumeUsedByInstancesWithProfiles returns a slice containing the names of instances using a volume.
 var VolumeUsedByInstancesWithProfiles func(s *state.State, poolName string, volumeName string, volumeTypeName string, runningOnly bool) ([]string, error)
 
-// MkfsOptions represents options for filesystem creation.
-type MkfsOptions struct {
-	Label string
-}
-
 // Export the mount options map since we might find it useful in other parts of
 // LXD.
 type mountOptions struct {
@@ -73,32 +68,6 @@ var MountOptions = map[string]mountOptions{
 	"sync":          {true, unix.MS_SYNCHRONOUS},
 }
 
-// LXDResolveMountoptions resolves the provided mount options.
-func LXDResolveMountoptions(options string) (uintptr, string) {
-	mountFlags := uintptr(0)
-	tmp := strings.SplitN(options, ",", -1)
-	for i := 0; i < len(tmp); i++ {
-		opt := tmp[i]
-		do, ok := MountOptions[opt]
-		if !ok {
-			continue
-		}
-
-		if do.capture {
-			mountFlags |= do.flag
-		} else {
-			mountFlags &= ^do.flag
-		}
-
-		copy(tmp[i:], tmp[i+1:])
-		tmp[len(tmp)-1] = ""
-		tmp = tmp[:len(tmp)-1]
-		i--
-	}
-
-	return mountFlags, strings.Join(tmp, ",")
-}
-
 // TryMount tries mounting a filesystem multiple times. This is useful for unreliable backends.
 func TryMount(src string, dst string, fs string, flags uintptr, options string) error {
 	var err error
@@ -225,33 +194,6 @@ func LXDUsesPool(dbObj *db.Cluster, onDiskPoolName string, driver string, onDisk
 	return false, "", nil
 }
 
-// MakeFSType creates the provided filesystem.
-func MakeFSType(path string, fsType string, options *MkfsOptions) (string, error) {
-	var err error
-	var msg string
-
-	fsOptions := options
-	if fsOptions == nil {
-		fsOptions = &MkfsOptions{}
-	}
-
-	cmd := []string{fmt.Sprintf("mkfs.%s", fsType), path}
-	if fsOptions.Label != "" {
-		cmd = append(cmd, "-L", fsOptions.Label)
-	}
-
-	if fsType == "ext4" {
-		cmd = append(cmd, "-E", "nodiscard,lazy_itable_init=0,lazy_journal_init=0")
-	}
-
-	msg, err = shared.TryRunCommand(cmd[0], cmd[1:]...)
-	if err != nil {
-		return msg, err
-	}
-
-	return "", nil
-}
-
 // FSGenerateNewUUID generates a UUID for the given path for btrfs and xfs filesystems.
 func FSGenerateNewUUID(fstype string, lvpath string) (string, error) {
 	switch fstype {


More information about the lxc-devel mailing list