Skip to content
Open
Changes from all 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
45 changes: 44 additions & 1 deletion 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,10 +17,46 @@

DEFAULT_DEVICE="/dev/sda"
SYSLOG_IDENTIFIER = "ssdutil"
DISK_TYPE_SSD = "0"
DISK_INVALID = "-1"

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

def get_default_disk():
"""Check default disk"""
default_device = DEFAULT_DEVICE
host_mnt = '/host'
cmd = "lsblk -l -n |grep {}".format(host_mnt)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You may be able to do this cleaner with df

admin@r-lionfish-16:~$ df --output=source /host
Filesystem
/dev/sda3

proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
out = proc.stdout.readline()
if host_mnt in out:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this check necessary? grep should take care of this. Perhaps check if the result is not empty (or grep return code)?

dev_nums = out.split()[1]
dev_maj_num = dev_nums.split(':')[0]

cmd = "lsblk -l -I {} |grep disk".format(dev_maj_num)
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
out = proc.stdout.readline()
if "disk" in out:
default_device = os.path.join("/dev/", out.split()[0])

return default_device


def get_disk_type(diskdev):
Copy link
Copy Markdown
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 {}".format(diskdev_name)
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
out = proc.stdout.readline()
if diskdev_name not in out:
return DISK_INVAILD
cmd = "cat /sys/block/{}/queue/rotational".format(diskdev_name)
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Forking to cat here seems unnecessary. Is there a reason you chose not to use native python io here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is also more pythonic to do this and add error handling in order to handle the DISK_INVALID case rather than forking lsblk as well.

out = proc.stdout.readline()
disk_type = out.rstrip()
return disk_type


def import_ssd_api(diskdev):
"""
Expand Down Expand Up @@ -60,13 +97,19 @@ def ssdutil():
sys.exit(1)

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--device", help="Device name to show health info", default=DEFAULT_DEVICE)
parser.add_argument("-d", "--device", help="Device name to show health info", default=get_default_disk())
parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Show verbose output (some additional parameters)")
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_SSD not in disk_type:
print("Disk type is not SSD")

ssd = import_ssd_api(args.device)


print("Device Model : {}".format(ssd.get_model()))
if args.verbose:
print("Firmware : {}".format(ssd.get_firmware()))
Expand Down