Skip to content

Commit 336c03f

Browse files
committed
fix(linstorvolumemanager): don't allow InUse volumes to be deleted
Check for usage status before deleting a linstor volume and raise an appropriate error if this happens Signed-off-by: Antoine Bartuccio <antoine.bartuccio@vates.tech>
1 parent d2c0f99 commit 336c03f

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

drivers/linstorvolumemanager.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
#
1717

18-
from sm_typing import override
18+
from sm_typing import Any, Dict, override
1919

2020
import errno
2121
import json
@@ -302,7 +302,8 @@ class LinstorVolumeManager(object):
302302
'_base_group_name', '_group_name', '_ha_group_name',
303303
'_volumes', '_storage_pools', '_storage_pools_time',
304304
'_kv_cache', '_resource_cache', '_volume_info_cache',
305-
'_kv_cache_dirty', '_resource_cache_dirty', '_volume_info_cache_dirty'
305+
'_kv_cache_dirty', '_resource_cache_dirty', '_volume_info_cache_dirty',
306+
'_resources_info_cache',
306307
)
307308

308309
DEV_ROOT_PATH = DRBD_BY_RES_PATH
@@ -439,6 +440,7 @@ def __init__(
439440
self._resource_cache_dirty = True
440441
self._volume_info_cache = None
441442
self._volume_info_cache_dirty = True
443+
self._resources_info_cache = None
442444
self._build_volumes(repair=repair)
443445

444446
@property
@@ -698,6 +700,13 @@ def destroy_volume(self, volume_uuid):
698700
self._ensure_volume_exists(volume_uuid)
699701
self.ensure_volume_is_not_locked(volume_uuid)
700702

703+
is_volume_in_use = any(node["in-use"] for node in self.get_resource_info(volume_uuid)["nodes"].values())
704+
if is_volume_in_use:
705+
raise LinstorVolumeManagerError(
706+
f"Could not destroy volume `{volume_uuid}` as it is currently in use",
707+
LinstorVolumeManagerError.ERR_VOLUME_DESTROY
708+
)
709+
701710
# Mark volume as destroyed.
702711
volume_properties = self._get_volume_properties(volume_uuid)
703712
volume_properties[self.PROP_NOT_EXISTS] = self.STATE_NOT_EXISTS
@@ -1701,6 +1710,9 @@ def get_resources_info(self):
17011710
Give all resources of current group name.
17021711
:rtype: dict(str, list)
17031712
"""
1713+
if self._resources_info_cache and not self._resource_cache_dirty:
1714+
return self._resources_info_cache
1715+
17041716
resources = {}
17051717
resource_list = self._get_resource_cache()
17061718
volume_names = self.get_volumes_with_name()
@@ -1757,7 +1769,23 @@ def get_resources_info(self):
17571769
if resource:
17581770
resource['uuid'] = volume_uuid
17591771

1760-
return resources
1772+
self._resources_info_cache = resources
1773+
return self._resources_info_cache
1774+
1775+
def get_resource_info(self, volume_uuid: str) -> Dict[str, Any]:
1776+
"""
1777+
Give all resource info related to provided UUID in the current group.
1778+
:param volume_uuid str: volume uuid to search for
1779+
:rtype: dict(str, any)
1780+
"""
1781+
for volume in self.get_resources_info().values():
1782+
if volume["uuid"] == volume_uuid:
1783+
return volume
1784+
1785+
raise LinstorVolumeManagerError(
1786+
f"Could not find info about volume `{volume_uuid}`",
1787+
LinstorVolumeManagerError.ERR_VOLUME_NOT_EXISTS
1788+
)
17611789

17621790
def get_database_path(self):
17631791
"""

0 commit comments

Comments
 (0)