Skip to content

Commit a90db03

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 5ea82e1 commit a90db03

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
@@ -427,6 +428,7 @@ def __init__(
427428
self._resource_cache_dirty = True
428429
self._volume_info_cache = None
429430
self._volume_info_cache_dirty = True
431+
self._resources_info_cache = None
430432
self._build_volumes(repair=repair)
431433

432434
@property
@@ -686,6 +688,13 @@ def destroy_volume(self, volume_uuid):
686688
self._ensure_volume_exists(volume_uuid)
687689
self.ensure_volume_is_not_locked(volume_uuid)
688690

691+
is_volume_in_use = any(node["in-use"] for node in self.get_resource_info(volume_uuid)["nodes"].values())
692+
if is_volume_in_use:
693+
raise LinstorVolumeManagerError(
694+
f"Could not destroy volume `{volume_uuid}` as it is currently in use",
695+
LinstorVolumeManagerError.ERR_VOLUME_DESTROY
696+
)
697+
689698
# Mark volume as destroyed.
690699
volume_properties = self._get_volume_properties(volume_uuid)
691700
volume_properties[self.PROP_NOT_EXISTS] = self.STATE_NOT_EXISTS
@@ -1683,6 +1692,9 @@ def get_resources_info(self):
16831692
Give all resources of current group name.
16841693
:rtype: dict(str, list)
16851694
"""
1695+
if self._resources_info_cache and not self._resource_cache_dirty:
1696+
return self._resources_info_cache
1697+
16861698
resources = {}
16871699
resource_list = self._get_resource_cache()
16881700
volume_names = self.get_volumes_with_name()
@@ -1739,7 +1751,23 @@ def get_resources_info(self):
17391751
if resource:
17401752
resource['uuid'] = volume_uuid
17411753

1742-
return resources
1754+
self._resources_info_cache = resources
1755+
return self._resources_info_cache
1756+
1757+
def get_resource_info(self, volume_uuid: str) -> Dict[str, Any]:
1758+
"""
1759+
Give all resources info with provided uuid on current group name.
1760+
:param volume_uuid str: volume uuid to search for
1761+
:rtype: dict
1762+
"""
1763+
for volume in self.get_resources_info().values():
1764+
if volume["uuid"] == volume_uuid:
1765+
return volume
1766+
1767+
raise LinstorVolumeManagerError(
1768+
f"Could not find info about volume `{volume_uuid}`",
1769+
LinstorVolumeManagerError.ERR_VOLUME_NOT_EXISTS
1770+
)
17431771

17441772
def get_database_path(self):
17451773
"""

0 commit comments

Comments
 (0)