From 3ac184af53644caa5b6a699f61a07d36eee8766d Mon Sep 17 00:00:00 2001 From: sujinmkang Date: Wed, 1 Dec 2021 14:40:46 -0800 Subject: [PATCH 1/6] Inform if the disk is not SSD --- ssdutil/main.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ssdutil/main.py b/ssdutil/main.py index 62f43037e7..eedac43d35 100755 --- a/ssdutil/main.py +++ b/ssdutil/main.py @@ -8,6 +8,7 @@ try: import argparse import os + import subprocess import sys from sonic_py_common import device_info, logger @@ -16,11 +17,20 @@ DEFAULT_DEVICE="/dev/sda" SYSLOG_IDENTIFIER = "ssdutil" +DISK_TYPE_SSD = "0" # Global logger instance log = logger.Logger(SYSLOG_IDENTIFIER) +def get_disk_type(diskdev): + """Check disk type""" + cmd = "cat /sys/block/{}/queue/rotational".format(diskdev.replace('/dev/','')) + proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE) + out = proc.stdout.readline() + return out.rstrip() + + def import_ssd_api(diskdev): """ Loads platform specific or generic ssd_util module from source @@ -65,6 +75,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())) From 217079be3bc35f8d9adcafcd396b3f336415ba78 Mon Sep 17 00:00:00 2001 From: sujinmkang Date: Sat, 4 Dec 2021 17:18:30 -0800 Subject: [PATCH 2/6] check the disk available --- ssdutil/main.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ssdutil/main.py b/ssdutil/main.py index eedac43d35..c2c1c867a2 100755 --- a/ssdutil/main.py +++ b/ssdutil/main.py @@ -25,10 +25,19 @@ def get_disk_type(diskdev): """Check disk type""" - cmd = "cat /sys/block/{}/queue/rotational".format(diskdev.replace('/dev/','')) + diskdev_name = diskdev.replace('/dev/','') + cmd = "lsblk -l -n |grep disk" proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE) - out = proc.stdout.readline() - return out.rstrip() + outs = proc.stdout.readlines() + for out in outs: + if out.split()[0] in diskdev_name: + 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): From 3cbf9b9247d742c889a237b1bb6d654034e9402c Mon Sep 17 00:00:00 2001 From: sujinmkang Date: Mon, 6 Dec 2021 09:50:28 -0800 Subject: [PATCH 3/6] disk name should be same --- ssdutil/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssdutil/main.py b/ssdutil/main.py index c2c1c867a2..48ae75e796 100755 --- a/ssdutil/main.py +++ b/ssdutil/main.py @@ -30,7 +30,7 @@ def get_disk_type(diskdev): proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE) outs = proc.stdout.readlines() for out in outs: - if out.split()[0] in diskdev_name: + if out.split()[0] is diskdev_name: cmd = "cat /sys/block/{}/queue/rotational".format(diskdev_name) proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE) out = proc.stdout.readline() From d17a02fc4e8622021b613faadd6240229d50f494 Mon Sep 17 00:00:00 2001 From: sujinmkang Date: Mon, 6 Dec 2021 21:44:27 -0800 Subject: [PATCH 4/6] add finding default-device instead of using the hardcoded one --- ssdutil/main.py | 51 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/ssdutil/main.py b/ssdutil/main.py index 48ae75e796..411d0b6a3d 100755 --- a/ssdutil/main.py +++ b/ssdutil/main.py @@ -18,26 +18,44 @@ 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) + proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE) + out = proc.stdout.readline() + if host_mnt in out: + dev_nums = out.split()[1] + dev_maj_num = out.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): """Check disk type""" diskdev_name = diskdev.replace('/dev/','') - cmd = "lsblk -l -n |grep disk" + cmd = "lsblk -l -n |grep {}".format(diskdev_name) 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: - 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) + 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) + out = proc.stdout.readline() + disk_type = out.rstrip() + return disk_type def import_ssd_api(diskdev): @@ -79,17 +97,22 @@ 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=None) 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 args.device: + disk_device = args.device + else: + disk_device = get_default_disk() + + disk_type = get_disk_type(disk_device) if disk_type != DISK_TYPE_SSD: print("Disk is not SSD") sys.exit(1) - ssd = import_ssd_api(args.device) + ssd = import_ssd_api(disk_device) print("Device Model : {}".format(ssd.get_model())) if args.verbose: From 35b5cf1b89b866c5ecfabfa3f696fd1b576bda95 Mon Sep 17 00:00:00 2001 From: sujinmkang Date: Mon, 6 Dec 2021 23:20:28 -0800 Subject: [PATCH 5/6] fix lgtm --- ssdutil/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ssdutil/main.py b/ssdutil/main.py index 411d0b6a3d..6dd734eaaf 100755 --- a/ssdutil/main.py +++ b/ssdutil/main.py @@ -32,7 +32,7 @@ def get_default_disk(): out = proc.stdout.readline() if host_mnt in out: dev_nums = out.split()[1] - dev_maj_num = out.split(':')[0] + 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) @@ -109,7 +109,7 @@ def ssdutil(): disk_type = get_disk_type(disk_device) if disk_type != DISK_TYPE_SSD: - print("Disk is not SSD") + print("Disk type is not SSD") sys.exit(1) ssd = import_ssd_api(disk_device) From 237caf77535f8cf2d2cb90102340d38972efb595 Mon Sep 17 00:00:00 2001 From: sujinmkang Date: Thu, 20 Jan 2022 15:43:01 -0800 Subject: [PATCH 6/6] Update disk check --- ssdutil/main.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/ssdutil/main.py b/ssdutil/main.py index 6dd734eaaf..39d9530703 100755 --- a/ssdutil/main.py +++ b/ssdutil/main.py @@ -97,22 +97,18 @@ def ssdutil(): sys.exit(1) parser = argparse.ArgumentParser() - parser.add_argument("-d", "--device", help="Device name to show health info", default=None) + 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() - if args.device: - disk_device = args.device - else: - disk_device = get_default_disk() - disk_type = get_disk_type(disk_device) - if disk_type != DISK_TYPE_SSD: + disk_type = get_disk_type(args.device) + if DISK_TYPE_SSD not in disk_type: print("Disk type is not SSD") - sys.exit(1) - ssd = import_ssd_api(disk_device) + ssd = import_ssd_api(args.device) + print("Device Model : {}".format(ssd.get_model())) if args.verbose: