[lxc-devel] [lxd/master] Storage: Unifies VM block volume checks and file system instantiation

tomponline on Github lxc-bot at linuxcontainers.org
Mon Jan 13 11:02:25 UTC 2020


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/20200113/1bff3a81/attachment.bin>
-------------- next part --------------
From 5906d2801b0e39ba39d84457236b66d7d4daee85 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 13 Jan 2020 10:52:09 +0000
Subject: [PATCH 1/3] lxd/storage/drivers/volume: Adds
 NewVMBlockFilesystemVolume and IsVMBlock functions

Used to manage virtual machine associated filesystem volumes and detect when a volume is for a VM or associated image.

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

diff --git a/lxd/storage/drivers/volume.go b/lxd/storage/drivers/volume.go
index d7e8d6e648..d479c9d030 100644
--- a/lxd/storage/drivers/volume.go
+++ b/lxd/storage/drivers/volume.go
@@ -15,6 +15,9 @@ const tmpVolSuffix = ".lxdtmp"
 // defaultBlockSize Default size of block volumes.
 const defaultBlockSize = "10GB"
 
+// vmBlockFilesystemSize is the size of a VM block volume's associated filesystem volume.
+const vmBlockFilesystemSize = "50MB"
+
 // DefaultFilesystem filesytem to use for block devices by default.
 const DefaultFilesystem = "ext4"
 
@@ -276,3 +279,23 @@ func (v Volume) Type() VolumeType {
 func (v Volume) ContentType() ContentType {
 	return v.contentType
 }
+
+// IsVMBlock returns true if volume is a block volume for virtual machines or associated images.
+func (v Volume) IsVMBlock() bool {
+	return (v.volType == VolumeTypeVM || v.volType == VolumeTypeImage) && v.contentType == ContentTypeBlock
+}
+
+// NewVMBlockFilesystemVolume returns a copy of the volume with the content type set to ContentTypeFS and the
+// config "size" property set to vmBlockFilesystemSize.
+func (v Volume) NewVMBlockFilesystemVolume() Volume {
+	// Copy volume config so modifications don't affect original volume.
+	newConf := make(map[string]string, len(v.config))
+	for k, v := range v.config {
+		newConf[k] = v
+	}
+
+	// VM Block filesystems are a fixed size.
+	newConf["size"] = vmBlockFilesystemSize
+
+	return NewVolume(v.driver, v.pool, v.volType, ContentTypeFS, v.name, newConf, v.poolConfig)
+}

From 43ae14f236d5b6d736164b22c02fa8372e52f2a9 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 13 Jan 2020 10:53:13 +0000
Subject: [PATCH 2/3] lxd/storage/drivers/driver/zfs/volumes: VM block function
 usage

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

diff --git a/lxd/storage/drivers/driver_zfs_volumes.go b/lxd/storage/drivers/driver_zfs_volumes.go
index 58fcd6e2b5..17a34a4c47 100644
--- a/lxd/storage/drivers/driver_zfs_volumes.go
+++ b/lxd/storage/drivers/driver_zfs_volumes.go
@@ -97,8 +97,8 @@ func (d *zfs) CreateVolume(vol Volume, filler *VolumeFiller, op *operations.Oper
 	}
 
 	// For VM images, create a filesystem volume too.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		err := d.CreateVolume(fsVol, nil, op)
 		if err != nil {
 			return err
@@ -635,8 +635,8 @@ func (d *zfs) DeleteVolume(vol Volume, op *operations.Operation) error {
 	}
 
 	// For VMs, also delete the filesystem dataset.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		err := d.DeleteVolume(fsVol, op)
 		if err != nil {
 			return err
@@ -838,8 +838,8 @@ func (d *zfs) GetVolumeDiskPath(vol Volume) (string, error) {
 // MountVolume simulates mounting a volume.
 func (d *zfs) MountVolume(vol Volume, op *operations.Operation) (bool, error) {
 	// For VMs, also mount the filesystem dataset.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		_, err := d.MountVolume(fsVol, op)
 		if err != nil {
 			return false, err
@@ -876,8 +876,8 @@ func (d *zfs) MountVolume(vol Volume, op *operations.Operation) (bool, error) {
 // UnmountVolume simulates unmounting a volume.
 func (d *zfs) UnmountVolume(vol Volume, op *operations.Operation) (bool, error) {
 	// For VMs, also mount the filesystem dataset.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		_, err := d.UnmountVolume(fsVol, op)
 		if err != nil {
 			return false, err
@@ -943,8 +943,8 @@ func (d *zfs) RenameVolume(vol Volume, newVolName string, op *operations.Operati
 	}
 
 	// For VM images, create a filesystem volume too.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		err := d.RenameVolume(fsVol, newVolName, op)
 		if err != nil {
 			return err
@@ -1162,8 +1162,8 @@ func (d *zfs) CreateVolumeSnapshot(vol Volume, op *operations.Operation) error {
 	revert.Add(func() { d.DeleteVolumeSnapshot(vol, op) })
 
 	// For VM images, create a filesystem volume too.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		err := d.CreateVolumeSnapshot(fsVol, op)
 		if err != nil {
 			return err
@@ -1215,8 +1215,8 @@ func (d *zfs) DeleteVolumeSnapshot(vol Volume, op *operations.Operation) error {
 	}
 
 	// For VM images, create a filesystem volume too.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		err := d.DeleteVolumeSnapshot(fsVol, op)
 		if err != nil {
 			return err
@@ -1330,8 +1330,8 @@ func (d *zfs) RestoreVolume(vol Volume, snapshotName string, op *operations.Oper
 	}
 
 	// For VM images, restore the associated filesystem dataset too.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		err := d.RestoreVolume(fsVol, snapshotName, op)
 		if err != nil {
 			return err
@@ -1371,8 +1371,8 @@ func (d *zfs) RenameVolumeSnapshot(vol Volume, newSnapshotName string, op *opera
 	})
 
 	// For VM images, create a filesystem volume too.
-	if d.checkVMBlock(vol) {
-		fsVol := NewVolume(d, d.name, vol.volType, ContentTypeFS, vol.name, vol.config, vol.poolConfig)
+	if vol.IsVMBlock() {
+		fsVol := vol.NewVMBlockFilesystemVolume()
 		err := d.RenameVolumeSnapshot(fsVol, newSnapshotName, op)
 		if err != nil {
 			return err

From c8e0fb734be38aa88779cb9a53a3d8a9c249a00b Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 13 Jan 2020 11:01:04 +0000
Subject: [PATCH 3/3] lxd/storage/drivers/driver/zfs/utils: Removes unused
 checkVMBlock

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

diff --git a/lxd/storage/drivers/driver_zfs_utils.go b/lxd/storage/drivers/driver_zfs_utils.go
index a5ac0ea914..5fb81cd2a5 100644
--- a/lxd/storage/drivers/driver_zfs_utils.go
+++ b/lxd/storage/drivers/driver_zfs_utils.go
@@ -81,10 +81,6 @@ func (d *zfs) checkDataset(dataset string) bool {
 	return strings.TrimSpace(out) == dataset
 }
 
-func (d *zfs) checkVMBlock(vol Volume) bool {
-	return (vol.volType == VolumeTypeVM || vol.volType == VolumeTypeImage) && vol.contentType == ContentTypeBlock
-}
-
 func (d *zfs) getClones(dataset string) ([]string, error) {
 	out, err := shared.RunCommand("zfs", "get", "-H", "-p", "-o", "value", "-r", "clones", dataset)
 	if err != nil {


More information about the lxc-devel mailing list