[lxc-devel] [lxd/master] storage: extract and refactor code for resizing volumes
albertodonato on Github
lxc-bot at linuxcontainers.org
Tue Sep 26 13:34:57 UTC 2017
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/20170926/64fd5079/attachment.bin>
-------------- next part --------------
From 69b496ea4077806db9d81b07436804af5e77ff54 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Mon, 25 Sep 2017 14:31:02 -0400
Subject: [PATCH 1/4] storage/utils: add growFileSystem helper
Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
lxd/storage_lvm_utils.go | 15 +--------------
lxd/storage_utils.go | 25 +++++++++++++++++++++++++
2 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/lxd/storage_lvm_utils.go b/lxd/storage_lvm_utils.go
index e12884e0e..28850a443 100644
--- a/lxd/storage_lvm_utils.go
+++ b/lxd/storage_lvm_utils.go
@@ -57,20 +57,7 @@ func (s *storageLvm) lvExtend(lvPath string, lvSize int64, fsType string, fsMntP
`volume type %d`, volumeType)
}
- switch fsType {
- case "xfs":
- msg, err = shared.TryRunCommand("xfs_growfs", fsMntPoint)
- default:
- // default = ext4
- msg, err = shared.TryRunCommand("resize2fs", lvPath)
- }
- if err != nil {
- logger.Errorf("could not extend underlying %s filesystem for LV \"%s\": %s", fsType, lvPath, msg)
- return fmt.Errorf("could not extend underlying %s filesystem for LV \"%s\": %s", fsType, lvPath, msg)
- }
-
- logger.Debugf("extended underlying %s filesystem for LV \"%s\"", fsType, lvPath)
- return nil
+ return growFileSystem(fsType, fsMntPoint, lvPath)
}
func (s *storageLvm) lvReduce(lvPath string, lvSize int64, fsType string, fsMntPoint string, volumeType int, data interface{}) error {
diff --git a/lxd/storage_utils.go b/lxd/storage_utils.go
index 8d26f9a1f..df1df29ed 100644
--- a/lxd/storage_utils.go
+++ b/lxd/storage_utils.go
@@ -10,6 +10,7 @@ import (
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/shared"
+ "github.com/lxc/lxd/shared/logger"
)
// Options for filesystem creation
@@ -224,3 +225,27 @@ func xfsGenerateNewUUID(lvpath string) (string, error) {
return "", nil
}
+
+func growFileSystem(fsType string, devPath string, mntpoint string) error {
+ var msg string
+ var err error
+ switch fsType {
+ case "": // if not specified, default to ext4
+ fallthrough
+ case "ext4":
+ msg, err = shared.TryRunCommand("resize2fs", mntpoint)
+ case "xfs":
+ msg, err = shared.TryRunCommand("xfs_growfs", devPath)
+ default:
+ return fmt.Errorf(`Unsupported filesystem type "%s"`, fsType)
+ }
+
+ if err != nil {
+ errorMsg := fmt.Sprintf(`Could not extend underlying %s filesystem for "%s": %s`, fsType, devPath, msg)
+ logger.Errorf(errorMsg)
+ return fmt.Errorf(errorMsg)
+ }
+
+ logger.Debugf(`extended underlying %s filesystem for "%s"`, fsType, devPath)
+ return nil
+}
From 083ad93250fb7126edba9749398df933abacca2f Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Mon, 25 Sep 2017 14:55:29 -0400
Subject: [PATCH 2/4] storage/utils: add shrinkFileSystem helper
Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
lxd/storage_lvm_utils.go | 18 ++++--------------
lxd/storage_utils.go | 28 +++++++++++++++++++++++++++-
2 files changed, 31 insertions(+), 15 deletions(-)
diff --git a/lxd/storage_lvm_utils.go b/lxd/storage_lvm_utils.go
index 28850a443..546fd4241 100644
--- a/lxd/storage_lvm_utils.go
+++ b/lxd/storage_lvm_utils.go
@@ -101,21 +101,11 @@ func (s *storageLvm) lvReduce(lvPath string, lvSize int64, fsType string, fsMntP
`storage volume type %d`, volumeType)
}
- msg, err = shared.TryRunCommand("e2fsck", "-f", "-y", lvPath)
- if err != nil {
- return err
- }
+ }
- // don't assume resize2fs semantics are sane (because they
- // aren't)
- kbSize := lvSize / 1024
- ext4LvSizeString := strconv.FormatInt(kbSize, 10)
- ext4LvSizeString += "K"
- msg, err = shared.TryRunCommand("resize2fs", lvPath, ext4LvSizeString)
- if err != nil {
- logger.Errorf("could not reduce underlying %s filesystem for LV \"%s\": %s", fsType, lvPath, msg)
- return fmt.Errorf("could not reduce underlying %s filesystem for LV \"%s\": %s", fsType, lvPath, msg)
- }
+ err = shrinkFileSystem(fsType, lvPath, lvSize)
+ if err != nil {
+ return err
}
msg, err = shared.TryRunCommand(
diff --git a/lxd/storage_utils.go b/lxd/storage_utils.go
index df1df29ed..64946175f 100644
--- a/lxd/storage_utils.go
+++ b/lxd/storage_utils.go
@@ -237,7 +237,7 @@ func growFileSystem(fsType string, devPath string, mntpoint string) error {
case "xfs":
msg, err = shared.TryRunCommand("xfs_growfs", devPath)
default:
- return fmt.Errorf(`Unsupported filesystem type "%s"`, fsType)
+ return fmt.Errorf(`Growing not supported for filesystem type "%s"`, fsType)
}
if err != nil {
@@ -249,3 +249,29 @@ func growFileSystem(fsType string, devPath string, mntpoint string) error {
logger.Debugf(`extended underlying %s filesystem for "%s"`, fsType, devPath)
return nil
}
+
+func shrinkFileSystem(fsType string, devPath string, byteSize int64) error {
+ var msg string
+ var err error
+ switch fsType {
+ case "": // if not specified, default to ext4
+ fallthrough
+ case "ext4":
+ msg, err = shared.TryRunCommand("e2fsck", "-f", "-y", devPath)
+ if err != nil {
+ return err
+ }
+ kbSize := byteSize / 1024
+ msg, err = shared.TryRunCommand("resize2fs", devPath, fmt.Sprintf("%dK", kbSize))
+
+ default:
+ return fmt.Errorf(`Shrinking not supported for filesystem type "%s"`, fsType)
+ }
+
+ if err != nil {
+ errorMsg := fmt.Sprintf(`Could not reduce underlying %s filesystem for "%s": %s`, fsType, devPath, msg)
+ logger.Errorf(errorMsg)
+ return fmt.Errorf(errorMsg)
+ }
+ return nil
+}
From 871672b4325e2ff5568a4b015dd140e746d8fd6b Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Mon, 25 Sep 2017 15:00:58 -0400
Subject: [PATCH 3/4] storage/ceph: use [grow|shrink]FileSystem helpers
Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
lxd/storage_ceph_utils.go | 42 +++++-------------------------------------
1 file changed, 5 insertions(+), 37 deletions(-)
diff --git a/lxd/storage_ceph_utils.go b/lxd/storage_ceph_utils.go
index 10504fa2d..5fe5609c0 100644
--- a/lxd/storage_ceph_utils.go
+++ b/lxd/storage_ceph_utils.go
@@ -1587,26 +1587,11 @@ func (s *storageCeph) rbdShrink(path string, size int64, fsType string,
return fmt.Errorf(`Resizing not implemented for `+
`storage volume type %d`, volumeType)
}
+ }
- msg, err = shared.TryRunCommand("e2fsck", "-f", "-y", path)
- if err != nil {
- return err
- }
-
- // don't assume resize2fs semantics are sane (because they
- // aren't)
- kbSize := size / 1024
- ext4SizeString := strconv.FormatInt(kbSize, 10)
- ext4SizeString += "K"
- msg, err = shared.TryRunCommand("resize2fs", path, ext4SizeString)
- if err != nil {
- logger.Errorf(`Could not reduce underlying %s `+
- `filesystem for RBD storage volume "%s": %s`,
- fsType, path, msg)
- return fmt.Errorf(`Could not reduce underlying %s `+
- `filesystem for RBD storage volume "%s": %s`,
- fsType, path, msg)
- }
+ err = shrinkFileSystem(fsType, path, size)
+ if err != nil {
+ return err
}
msg, err = shared.TryRunCommand(
@@ -1680,22 +1665,5 @@ func (s *storageCeph) rbdGrow(path string, size int64, fsType string,
%s`, path, msg)
}
- switch fsType {
- case "xfs":
- msg, err = shared.TryRunCommand("xfs_growfs", fsMntPoint)
- default:
- // default = ext4
- msg, err = shared.TryRunCommand("resize2fs", path)
- }
- if err != nil {
- logger.Errorf(`Could not extend underlying %s `+
- `filesystem for RBD storage volume "%s": %s`,
- fsType, path, msg)
- return fmt.Errorf(`Could not extend underlying %s `+
- `filesystem for RBD storage volume "%s": %s`,
- fsType, path, msg)
- }
-
- logger.Debugf("extended underlying %s filesystem for LV \"%s\"", fsType, path)
- return nil
+ return growFileSystem(fsType, path, fsMntPoint)
}
From dc93a29be5f3748fc84fc58e7785d34b6e2f66d2 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Mon, 25 Sep 2017 16:57:39 -0400
Subject: [PATCH 4/4] storage/utils: add shrinkVolumeFilesystem helper
Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
lxd/storage_ceph_utils.go | 46 +++++++++++++---------------------------------
lxd/storage_lvm_utils.go | 37 ++++---------------------------------
lxd/storage_utils.go | 39 +++++++++++++++++++++++++++++++++++++++
3 files changed, 56 insertions(+), 66 deletions(-)
diff --git a/lxd/storage_ceph_utils.go b/lxd/storage_ceph_utils.go
index 5fe5609c0..a6510e848 100644
--- a/lxd/storage_ceph_utils.go
+++ b/lxd/storage_ceph_utils.go
@@ -1556,44 +1556,24 @@ func (s *storageCeph) rbdShrink(path string, size int64, fsType string,
`less than 1MB`)
}
- volumeTypeName := ""
- switch fsType {
- case "xfs":
- logger.Errorf("xfs filesystems cannot be shrunk: dump, mkfs, and restore are required")
- return fmt.Errorf("xfs filesystems cannot be shrunk: dump, mkfs, and restore are required")
- default:
- // default = ext4
- switch volumeType {
- case storagePoolVolumeTypeContainer:
- c := data.(container)
- ourMount, err := c.StorageStop()
- if err != nil {
- return err
- }
- if !ourMount {
- defer c.StorageStart()
- }
- volumeTypeName = storagePoolVolumeTypeNameContainer
- case storagePoolVolumeTypeCustom:
- ourMount, err := s.StoragePoolVolumeUmount()
- if err != nil {
- return err
- }
- if !ourMount {
- defer s.StoragePoolVolumeMount()
- }
- volumeTypeName = storagePoolVolumeTypeNameCustom
- default:
- return fmt.Errorf(`Resizing not implemented for `+
- `storage volume type %d`, volumeType)
- }
+ cleanupFunc, err := shrinkVolumeFilesystem(s, volumeType, fsType, path, size, data)
+ if cleanupFunc != nil {
+ defer cleanupFunc()
}
-
- err = shrinkFileSystem(fsType, path, size)
if err != nil {
return err
}
+ volumeTypeName := ""
+ switch volumeType {
+ case storagePoolVolumeTypeContainer:
+ volumeTypeName = storagePoolVolumeTypeNameContainer
+ case storagePoolVolumeTypeCustom:
+ volumeTypeName = storagePoolVolumeTypeNameCustom
+ default:
+ return fmt.Errorf(`Resizing not implemented for `+
+ `storage volume type %d`, volumeType)
+ }
msg, err = shared.TryRunCommand(
"rbd",
"resize",
diff --git a/lxd/storage_lvm_utils.go b/lxd/storage_lvm_utils.go
index 546fd4241..62e61d7c6 100644
--- a/lxd/storage_lvm_utils.go
+++ b/lxd/storage_lvm_utils.go
@@ -70,44 +70,15 @@ func (s *storageLvm) lvReduce(lvPath string, lvSize int64, fsType string, fsMntP
return fmt.Errorf(`The size of the storage volume would be ` +
`less than 1MB`)
}
-
- lvSizeString := strconv.FormatInt(lvSize, 10)
- switch fsType {
- case "xfs":
- logger.Errorf("xfs filesystems cannot be shrunk: dump, mkfs, and restore are required")
- return fmt.Errorf("xfs filesystems cannot be shrunk: dump, mkfs, and restore are required")
- default:
- // default = ext4
- switch volumeType {
- case storagePoolVolumeTypeContainer:
- c := data.(container)
- ourMount, err := c.StorageStop()
- if err != nil {
- return err
- }
- if !ourMount {
- defer c.StorageStart()
- }
- case storagePoolVolumeTypeCustom:
- ourMount, err := s.StoragePoolVolumeUmount()
- if err != nil {
- return err
- }
- if !ourMount {
- defer s.StoragePoolVolumeMount()
- }
- default:
- return fmt.Errorf(`Resizing not implemented for `+
- `storage volume type %d`, volumeType)
- }
-
+ cleanupFunc, err := shrinkVolumeFilesystem(s, volumeType, fsType, lvPath, lvSize, data)
+ if cleanupFunc != nil {
+ defer cleanupFunc()
}
-
- err = shrinkFileSystem(fsType, lvPath, lvSize)
if err != nil {
return err
}
+ lvSizeString := strconv.FormatInt(lvSize, 10)
msg, err = shared.TryRunCommand(
"lvreduce",
"-L", lvSizeString+"B",
diff --git a/lxd/storage_utils.go b/lxd/storage_utils.go
index 64946175f..81171a0fd 100644
--- a/lxd/storage_utils.go
+++ b/lxd/storage_utils.go
@@ -275,3 +275,42 @@ func shrinkFileSystem(fsType string, devPath string, byteSize int64) error {
}
return nil
}
+
+func shrinkVolumeFilesystem(s storage, volumeType int, fsType string, devPath string, byteSize int64, data interface{}) (func() (bool, error), error) {
+ var cleanupFunc func() (bool, error)
+ switch fsType {
+ case "xfs":
+ logger.Errorf("xfs filesystems cannot be shrunk: dump, mkfs, and restore are required")
+ return nil, fmt.Errorf("xfs filesystems cannot be shrunk: dump, mkfs, and restore are required")
+ case "": // if not specified, default to ext4
+ fallthrough
+ case "ext4":
+ switch volumeType {
+ case storagePoolVolumeTypeContainer:
+ c := data.(container)
+ ourMount, err := c.StorageStop()
+ if err != nil {
+ return nil, err
+ }
+ if !ourMount {
+ cleanupFunc = c.StorageStart
+ }
+ case storagePoolVolumeTypeCustom:
+ ourMount, err := s.StoragePoolVolumeUmount()
+ if err != nil {
+ return nil, err
+ }
+ if !ourMount {
+ cleanupFunc = s.StoragePoolVolumeMount
+ }
+ default:
+ return nil, fmt.Errorf(`Resizing not implemented for storage volume type %d`, volumeType)
+ }
+
+ default:
+ return nil, fmt.Errorf(`Shrinking not supported for filesystem type "%s"`, fsType)
+ }
+
+ err := shrinkFileSystem(fsType, devPath, byteSize)
+ return cleanupFunc, err
+}
More information about the lxc-devel
mailing list