Skip to content

Commit 5d02f06

Browse files
committed
Managing NVMe subsystem I/O policies in Linux:
1. get_nvme_subsystem_io_policy(subsystem): This function reads the current I/O policy for a specified NVMe subsystem from the /sys/class/nvme-subsystem/<subsystem>/iopolicy file and returns it as a string. It uses the process.run() function to execute the cat command with sudo privileges and ignores the command's status. 2. change_nvme_subsystem_io_policy(subsystem, io_policy): This function changes the I/O policy of an NVMe subsystem to the specified policy. It first checks if the current policy matches the desired policy and returns True if they are the same. If not, it executes the echo command to set the new policy. The function raises a NvmeException if the command fails and returns True if the policy was changed successfully. Both functions use the process.run() function from the process module to execute shell commands with sudo privileges and ignore their status. The get_nvme_subsystem_io_policy() function does not take any action if the current policy matches the desired policy, while the change_nvme_subsystem_io_policy() function raises an exception or returns True based on the outcome of the policy change. Signed-off-by: Maram Srimannarayana Murthy <[email protected]>
1 parent 10f12f0 commit 5d02f06

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

avocado/utils/nvme.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,39 @@ def get_subsystem_using_ctrl_name(ctrl):
529529
if ctrl in ctrls:
530530
return get_subsys_name_with_nqn(device_nqn)
531531
return ""
532+
533+
534+
def get_nvme_subsystem_io_policy(subsystem):
535+
"""
536+
Read io policy parameter from sysfs folder and returns a string
537+
538+
:param subsystem: Name of the subsystem
539+
:rtype: String
540+
"""
541+
cmd = f"cat /sys/class/nvme-subsystem/{subsystem}/iopolicy"
542+
return process.run(
543+
cmd, shell=True, sudo=True, ignore_status=True
544+
).stdout_text.strip()
545+
546+
547+
def change_nvme_subsystem_io_policy(subsystem, io_policy):
548+
"""
549+
Changes io policy of nvme device to specified io policy
550+
Change in io policy is not persistent
551+
552+
:param subsystem: Name of the subsystem
553+
:param io_policy: Name of the io_policy to which we want to set
554+
:raises: NvmeException on command failures
555+
:rtype: Boolean
556+
"""
557+
cmd = f"echo {io_policy} > /sys/class/nvme-subsystem/{subsystem}/iopolicy"
558+
if get_nvme_subsystem_io_policy(subsystem) == io_policy:
559+
LOGGER.info("Returning True as iopolicy is same as current")
560+
return True
561+
if process.system(cmd, shell=True, sudo=True, ignore_status=True):
562+
raise NvmeException(f"Changing nvme subsystem iopolicy is failed: {cmd}")
563+
cmd = f"cat /sys/class/nvme-subsystem/{subsystem}/iopolicy"
564+
output = process.run(
565+
cmd, shell=True, sudo=True, ignore_status=True
566+
).stdout_text.strip()
567+
return output == io_policy

0 commit comments

Comments
 (0)