Skip to content

Commit

Permalink
Merge pull request #208 from gbregman/devel
Browse files Browse the repository at this point in the history
When deleting bdev also delete any namespace using it from OMAP.
  • Loading branch information
gbregman authored Sep 4, 2023
2 parents 09cb4ad + 2fd2ef4 commit 9b3b31b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
3 changes: 2 additions & 1 deletion control/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ def create_bdev(self, args):

@cli.cmd([
argument("-b", "--bdev", help="Bdev name", required=True),
argument("-f", "--force", help="Delete any namespace using this bdev before deleting bdev", action='store_true', required=False),
])
def delete_bdev(self, args):
"""Deletes a bdev."""
req = pb2.delete_bdev_req(bdev_name=args.bdev)
req = pb2.delete_bdev_req(bdev_name=args.bdev, force=args.force)
ret = self.stub.delete_bdev(req)
self.logger.info(f"Deleted bdev {args.bdev}: {ret.status}")

Expand Down
31 changes: 31 additions & 0 deletions control/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,38 @@ def delete_bdev(self, request, context=None):
"""Deletes a bdev."""

self.logger.info(f"Received request to delete bdev {request.bdev_name}")
use_excep = None
req_get_subsystems = pb2.get_subsystems_req()
ret = self.get_subsystems(req_get_subsystems, context)
subsystems = json.loads(ret.subsystems)
for subsystem in subsystems:
for namespace in subsystem['namespaces']:
if namespace['bdev_name'] == request.bdev_name:
# We found a namespace still using this bdev. If --force was used we will try to remove this namespace.
# Otherwise fail with EBUSY
if request.force:
self.logger.info(f"Will remove namespace {namespace['nsid']} from {subsystem['nqn']} as it is using bdev {request.bdev_name}")
try:
req_rm_ns = pb2.remove_namespace_req(subsystem_nqn=subsystem['nqn'], nsid=namespace['nsid'])
ret = self.remove_namespace(req_rm_ns, context)
self.logger.info(
f"Removed namespace {namespace['nsid']} from {subsystem['nqn']}: {ret.status}")
except Exception as ex:
self.logger.error(f"Error removing namespace {namespace['nsid']} from {subsystem['nqn']}, will delete bdev {request.bdev_name} anyway: {ex}")
pass
else:
self.logger.error(f"Namespace {namespace['nsid']} from {subsystem['nqn']} is still using bdev {request.bdev_name}. You need to either remove it or use the '--force' command line option")
req = {"name": request.bdev_name, "method": "bdev_rbd_delete", "req_id": 0}
ret = {"code": -16, "message": "Device or resource busy"}
msg = "\n".join(["request:", "%s" % json.dumps(req, indent=2),
"Got JSON-RPC error response",
"response:",
json.dumps(ret, indent=2)])
use_excep = Exception(msg)

try:
if use_excep:
raise use_excep
ret = rpc_bdev.bdev_rbd_delete(
self.spdk_rpc_client,
request.bdev_name,
Expand Down
1 change: 1 addition & 0 deletions control/proto/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ message create_bdev_req {

message delete_bdev_req {
string bdev_name = 1;
bool force = 2;
}

message create_subsystem_req {
Expand Down
9 changes: 8 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
image = "mytestdevimage"
pool = "rbd"
bdev = "Ceph0"
bdev1 = "Ceph1"
subsystem = "nqn.2016-06.io.spdk:cnode1"
serial = "SPDK00000000000001"
host_list = ["nqn.2016-06.io.spdk:host1", "*"]
Expand Down Expand Up @@ -40,6 +41,8 @@ class TestCreate:
def test_create_bdev(self, caplog, gateway):
cli(["create_bdev", "-i", image, "-p", pool, "-b", bdev])
assert "Failed to create" not in caplog.text
cli(["create_bdev", "-i", image, "-p", pool, "-b", bdev1])
assert "Failed to create" not in caplog.text

def test_create_subsystem(self, caplog, gateway):
cli(["create_subsystem", "-n", subsystem, "-s", serial])
Expand All @@ -48,6 +51,8 @@ def test_create_subsystem(self, caplog, gateway):
def test_add_namespace(self, caplog, gateway):
cli(["add_namespace", "-n", subsystem, "-b", bdev])
assert "Failed to add" not in caplog.text
cli(["add_namespace", "-n", subsystem, "-b", bdev1])
assert "Failed to add" not in caplog.text

@pytest.mark.parametrize("host", host_list)
def test_add_host(self, caplog, host):
Expand Down Expand Up @@ -76,7 +81,9 @@ def test_remove_namespace(self, caplog, gateway):
assert "Failed to remove" not in caplog.text

def test_delete_bdev(self, caplog, gateway):
cli(["delete_bdev", "-b", bdev])
cli(["delete_bdev", "-b", bdev, "-f"])
assert "Failed to delete" not in caplog.text
cli(["delete_bdev", "-b", bdev1, "--force"])
assert "Failed to delete" not in caplog.text

def test_delete_subsystem(self, caplog, gateway):
Expand Down

0 comments on commit 9b3b31b

Please sign in to comment.