Skip to content

[ssdutil] Inform if the disk is not SSD #1957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ssdutil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
try:
import argparse
import os
import subprocess
import sys

from sonic_py_common import device_info, logger
Expand All @@ -16,11 +17,29 @@

DEFAULT_DEVICE="/dev/sda"
SYSLOG_IDENTIFIER = "ssdutil"
DISK_TYPE_SSD = "0"

# Global logger instance
log = logger.Logger(SYSLOG_IDENTIFIER)


def get_disk_type(diskdev):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to validate if the listed diskdev is valid as listed by "lsblk" otherwise the utility will crash. you can use

lsblk -l -n | grep 'disk' 

to get the list of all disk names available in the host

"""Check disk type"""
diskdev_name = diskdev.replace('/dev/','')
cmd = "lsblk -l -n |grep disk"
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
outs = proc.stdout.readlines()
for out in outs:
if out.split()[0] is diskdev_name:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if diskdev_name is one of ['sda', 'sdb', 'sdc'], but you seem to be checking only the first element

cmd = "cat /sys/block/{}/queue/rotational".format(diskdev_name)
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
out = proc.stdout.readline()
return out.rstrip()

print("disk {} does not exist in the device".format(diskdev_name))
sys.exit(1)


def import_ssd_api(diskdev):
"""
Loads platform specific or generic ssd_util module from source
Expand Down Expand Up @@ -65,6 +84,11 @@ def ssdutil():
parser.add_argument("-e", "--vendor", action="store_true", default=False, help="Show vendor output (extended output if provided by platform vendor)")
args = parser.parse_args()

disk_type = get_disk_type(args.device)
if disk_type != DISK_TYPE_SSD:
print("Disk is not SSD")
sys.exit(1)

ssd = import_ssd_api(args.device)

print("Device Model : {}".format(ssd.get_model()))
Expand Down