From 4c21ef3645a6063d421d0f2b518a886336868e8c Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Mon, 8 Jun 2020 03:18:32 -0400 Subject: [PATCH] test/cuse: revert NVMe namespaces to default state If for any reason the cuse tests do not revert state of NVMe namespaces to the original state, the test machines would be stuck in until manual intervention. Assumed default state is single namespace, encompassing available space formatted as 4k block size. This patch adds nvme_namespace_revert(), which searches for NVMe devices that support Namespace Managment and have some space unallocated. When such device is found, all existing namespaces are removed and single one created. Fix #1435 Signed-off-by: Tomasz Zawadzki Change-Id: Ifc3b04b3f166bf450b884674fa6a482e2fbc4c29 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/2829 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Shuhei Matsumoto Reviewed-by: Changpeng Liu Reviewed-by: Karol Latecki Reviewed-by: Ben Walker --- autotest.sh | 3 +++ test/common/autotest_common.sh | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/autotest.sh b/autotest.sh index b3cd18f06..b24dbffea 100755 --- a/autotest.sh +++ b/autotest.sh @@ -110,6 +110,9 @@ if [ $(uname -s) = Linux ]; then fi fi +# Revert NVMe namespaces to default state +nvme_namespace_revert + # Delete all leftover lvols and gpt partitions # Matches both /dev/nvmeXnY on Linux and /dev/nvmeXnsY on BSD # Filter out nvme with partitions - the "p*" suffix diff --git a/test/common/autotest_common.sh b/test/common/autotest_common.sh index 2e043b184..866d4b493 100755 --- a/test/common/autotest_common.sh +++ b/test/common/autotest_common.sh @@ -1212,6 +1212,48 @@ function get_first_nvme_bdf() { head -1 <<< "$(get_nvme_bdfs)" } +function nvme_namespace_revert() { + $rootdir/scripts/setup.sh + sleep 1 + bdfs=$(get_nvme_bdfs) + + $rootdir/scripts/setup.sh reset + sleep 1 + + for bdf in $bdfs; do + nvme_ctrlr=/dev/$(get_nvme_ctrlr_from_bdf ${bdf}) + if [[ -z "$nvme_ctrlr" ]]; then + continue + fi + + # Check Optional Admin Command Support for Namespace Management + oacs=$(nvme id-ctrl ${nvme_ctrlr} | grep oacs | cut -d: -f2) + oacs_ns_manage=$((oacs & 0x8)) + + if [[ "$oacs_ns_manage" -ne 0 ]]; then + # This assumes every NVMe controller contains single namespace, + # encompassing Total NVM Capacity and formatted as 4k block size. + + unvmcap=$(nvme id-ctrl ${nvme_ctrlr} | grep unvmcap | cut -d: -f2) + if [[ "$unvmcap" -eq 0 ]]; then + # All available space already used + continue + fi + tnvmcap=$(nvme id-ctrl ${nvme_ctrlr} | grep tnvmcap | cut -d: -f2) + blksize=4096 + + size=$((tnvmcap / blksize)) + + nvme detach-ns ${nvme_ctrlr} -n 0xffffffff -c 0 || true + nvme delete-ns ${nvme_ctrlr} -n 0xffffffff || true + nvme create-ns ${nvme_ctrlr} -s ${size} -c ${size} -b ${blksize} + nvme attach-ns ${nvme_ctrlr} -n 1 -c 0 + nvme reset ${nvme_ctrlr} + waitforblk "${nvme_ctrlr}n1" + fi + done +} + # Define temp storage for all the tests. Look for 2GB at minimum set_test_storage "${TEST_MIN_STORAGE_SIZE:-$((1 << 31))}"