[lxc-devel] [lxd/master] LVM custom volume snapshots fixes

stgraber on Github lxc-bot at linuxcontainers.org
Wed Apr 3 14:51:35 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/20190403/76a242d5/attachment.bin>
-------------- next part --------------
From f5be4a5b2bda81bad3c0b8e1eb5dda285f48ca29 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Tue, 2 Apr 2019 11:11:02 +0200
Subject: [PATCH 1/3] storage/lvm: Fix LV naming

This makes it possible to distinguish between volumes and volume
snapshots.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 lxd/storage_lvm.go | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/lxd/storage_lvm.go b/lxd/storage_lvm.go
index e9053d762a..51655cfaf1 100644
--- a/lxd/storage_lvm.go
+++ b/lxd/storage_lvm.go
@@ -481,6 +481,7 @@ func (s *storageLvm) StoragePoolVolumeCreate() error {
 	logger.Infof("Creating LVM storage volume \"%s\" on storage pool \"%s\"", s.volume.Name, s.pool.Name)
 	tryUndo := true
 
+	volumeLvmName := containerNameToLVName(s.volume.Name)
 	poolName := s.getOnDiskPoolName()
 	thinPoolName := s.getLvmThinpoolName()
 	lvFsType := s.getLvmFilesystem()
@@ -501,7 +502,7 @@ func (s *storageLvm) StoragePoolVolumeCreate() error {
 		}
 	}
 
-	err = lvmCreateLv("default", poolName, thinPoolName, s.volume.Name, lvFsType, lvSize, volumeType, s.useThinpool)
+	err = lvmCreateLv("default", poolName, thinPoolName, volumeLvmName, lvFsType, lvSize, volumeType, s.useThinpool)
 	if err != nil {
 		return fmt.Errorf("Error Creating LVM LV for new image: %v", err)
 	}
@@ -539,9 +540,10 @@ func (s *storageLvm) StoragePoolVolumeCreate() error {
 func (s *storageLvm) StoragePoolVolumeDelete() error {
 	logger.Infof("Deleting LVM storage volume \"%s\" on storage pool \"%s\"", s.volume.Name, s.pool.Name)
 
+	volumeLvmName := containerNameToLVName(s.volume.Name)
 	poolName := s.getOnDiskPoolName()
 	customLvmDevPath := getLvmDevPath("default", poolName,
-		storagePoolVolumeAPIEndpointCustom, s.volume.Name)
+		storagePoolVolumeAPIEndpointCustom, volumeLvmName)
 	lvExists, _ := storageLVExists(customLvmDevPath)
 
 	if lvExists {
@@ -557,7 +559,7 @@ func (s *storageLvm) StoragePoolVolumeDelete() error {
 	}
 
 	if lvExists {
-		err = removeLV("default", poolName, volumeType, s.volume.Name)
+		err = removeLV("default", poolName, volumeType, volumeLvmName)
 		if err != nil {
 			return err
 		}
@@ -587,6 +589,7 @@ func (s *storageLvm) StoragePoolVolumeDelete() error {
 func (s *storageLvm) StoragePoolVolumeMount() (bool, error) {
 	logger.Debugf("Mounting LVM storage volume \"%s\" on storage pool \"%s\"", s.volume.Name, s.pool.Name)
 
+	volumeLvmName := containerNameToLVName(s.volume.Name)
 	customPoolVolumeMntPoint := getStoragePoolVolumeMountPoint(s.pool.Name, s.volume.Name)
 	poolName := s.getOnDiskPoolName()
 	lvFsType := s.getLvmFilesystem()
@@ -594,7 +597,7 @@ func (s *storageLvm) StoragePoolVolumeMount() (bool, error) {
 	if err != nil {
 		return false, err
 	}
-	lvmVolumePath := getLvmDevPath("default", poolName, volumeType, s.volume.Name)
+	lvmVolumePath := getLvmDevPath("default", poolName, volumeType, volumeLvmName)
 
 	customMountLockID := getCustomMountLockID(s.pool.Name, s.volume.Name)
 	lxdStorageMapLock.Lock()
@@ -898,7 +901,10 @@ func (s *storageLvm) StoragePoolVolumeRename(newName string) error {
 			s.volume.Name, s.pool.Name)
 	}
 
-	err = s.renameLVByPath("default", s.volume.Name, newName,
+	sourceLVName := containerNameToLVName(s.volume.Name)
+	targetLVName := containerNameToLVName(newName)
+
+	err = s.renameLVByPath("default", sourceLVName, targetLVName,
 		storagePoolVolumeAPIEndpointCustom)
 	if err != nil {
 		return fmt.Errorf(`Failed to rename logical volume from "%s" to "%s": %s`,
@@ -2314,8 +2320,10 @@ func (s *storageLvm) StoragePoolVolumeSnapshotCreate(target *api.StorageVolumeSn
 		return fmt.Errorf("Not a snapshot")
 	}
 
+	sourceLvmName := containerNameToLVName(sourceOnlyName)
 	targetLvmName := containerNameToLVName(target.Name)
-	_, err := s.createSnapshotLV("default", poolName, sourceOnlyName, storagePoolVolumeAPIEndpointCustom, targetLvmName, storagePoolVolumeAPIEndpointCustom, true, s.useThinpool)
+
+	_, err := s.createSnapshotLV("default", poolName, sourceLvmName, storagePoolVolumeAPIEndpointCustom, targetLvmName, storagePoolVolumeAPIEndpointCustom, true, s.useThinpool)
 	if err != nil {
 		return fmt.Errorf("Failed to create snapshot logical volume %s", err)
 	}
@@ -2393,7 +2401,10 @@ func (s *storageLvm) StoragePoolVolumeSnapshotRename(newName string) error {
 	}
 	fullSnapshotName := fmt.Sprintf("%s%s%s", sourceName, shared.SnapshotDelimiter, newName)
 
-	err = s.renameLVByPath("default", s.volume.Name, fullSnapshotName, storagePoolVolumeAPIEndpointCustom)
+	sourceLVName := containerNameToLVName(s.volume.Name)
+	targetLVName := containerNameToLVName(fullSnapshotName)
+
+	err = s.renameLVByPath("default", sourceLVName, targetLVName, storagePoolVolumeAPIEndpointCustom)
 	if err != nil {
 		return fmt.Errorf("Failed to rename logical volume from \"%s\" to \"%s\": %s", s.volume.Name, newName, err)
 	}

From 4232e86ebd9aa4d6c6919edb11e574ea8085c6a5 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Tue, 2 Apr 2019 12:08:50 +0200
Subject: [PATCH 2/3] patches: Fix names of pool volume LVs

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

diff --git a/lxd/patches.go b/lxd/patches.go
index 93b2ab8e5f..a1d5e932a7 100644
--- a/lxd/patches.go
+++ b/lxd/patches.go
@@ -68,6 +68,7 @@ var patches = []patch{
 	{name: "move_backups", run: patchMoveBackups},
 	{name: "storage_api_rename_container_snapshots_dir", run: patchStorageApiRenameContainerSnapshotsDir},
 	{name: "storage_api_rename_container_snapshots_links", run: patchStorageApiUpdateContainerSnapshots},
+	{name: "fix_lvm_pool_volume_names", run: patchRenameCustomVolumeLVs},
 }
 
 type patch struct {
@@ -121,6 +122,55 @@ func patchesApplyAll(d *Daemon) error {
 }
 
 // Patches begin here
+func patchRenameCustomVolumeLVs(name string, d *Daemon) error {
+	// Ignore the error since it will also fail if there are no pools.
+	pools, _ := d.cluster.StoragePools()
+
+	for _, poolName := range pools {
+		poolID, pool, err := d.cluster.StoragePoolGet(poolName)
+		if err != nil {
+			return err
+		}
+
+		sType, err := storageStringToType(pool.Driver)
+		if err != nil {
+			return err
+		}
+
+		if sType != storageTypeLvm {
+			continue
+		}
+
+		volumes, err := d.cluster.StoragePoolNodeVolumesGetType(storagePoolVolumeTypeCustom, poolID)
+		if err != nil {
+			return err
+		}
+
+		for _, volume := range volumes {
+			oldName := fmt.Sprintf("%s/custom_%s", poolName, volume)
+			newName := fmt.Sprintf("%s/custom_%s", poolName, containerNameToLVName(volume))
+
+			exists, err := storageLVExists(newName)
+			if err != nil {
+				return err
+			}
+
+			if exists || oldName == newName {
+				continue
+			}
+
+			err = lvmLVRename(poolName, oldName, newName)
+			if err != nil {
+				return err
+			}
+
+			logger.Info("Successfully renamed LV", log.Ctx{"old_name": oldName, "new_name": newName})
+		}
+	}
+
+	return nil
+}
+
 func patchLeftoverProfileConfig(name string, d *Daemon) error {
 	return d.cluster.ProfileCleanupLeftover()
 }

From c47057038e931649e4810a985d9173865a4506ff Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 3 Apr 2019 08:11:05 +0200
Subject: [PATCH 3/3] storage: Add helper function to get volume snapshots

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

diff --git a/lxd/storage_volumes_utils.go b/lxd/storage_volumes_utils.go
index f8d4ab5066..5691da3614 100644
--- a/lxd/storage_volumes_utils.go
+++ b/lxd/storage_volumes_utils.go
@@ -708,3 +708,17 @@ func storagePoolVolumeSnapshotDBCreateInternal(state *state.State, dbArgs *db.St
 
 	return s, nil
 }
+
+func storagePoolVolumeSnapshotsGet(s *state.State, pool string, volume string, volType int) ([]string, error) {
+	poolID, err := s.Cluster.StoragePoolGetID(pool)
+	if err != nil {
+		return nil, err
+	}
+
+	snapshots, err := s.Cluster.StoragePoolVolumeSnapshotsGetType(volume, volType, poolID)
+	if err != nil {
+		return nil, err
+	}
+
+	return snapshots, nil
+}


More information about the lxc-devel mailing list