Skip to content

Commit d17a02f

Browse files
committed
add finding default-device instead of using the hardcoded one
1 parent 3cbf9b9 commit d17a02f

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

ssdutil/main.py

+37-14
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,44 @@
1818
DEFAULT_DEVICE="/dev/sda"
1919
SYSLOG_IDENTIFIER = "ssdutil"
2020
DISK_TYPE_SSD = "0"
21+
DISK_INVALID = "-1"
2122

2223
# Global logger instance
2324
log = logger.Logger(SYSLOG_IDENTIFIER)
2425

26+
def get_default_disk():
27+
"""Check default disk"""
28+
default_device = DEFAULT_DEVICE
29+
host_mnt = '/host'
30+
cmd = "lsblk -l -n |grep {}".format(host_mnt)
31+
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
32+
out = proc.stdout.readline()
33+
if host_mnt in out:
34+
dev_nums = out.split()[1]
35+
dev_maj_num = out.split(':')[0]
36+
37+
cmd = "lsblk -l -I {} |grep disk".format(dev_maj_num)
38+
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
39+
out = proc.stdout.readline()
40+
if "disk" in out:
41+
default_device = os.path.join("/dev/", out.split()[0])
42+
43+
return default_device
44+
2545

2646
def get_disk_type(diskdev):
2747
"""Check disk type"""
2848
diskdev_name = diskdev.replace('/dev/','')
29-
cmd = "lsblk -l -n |grep disk"
49+
cmd = "lsblk -l -n |grep {}".format(diskdev_name)
3050
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
31-
outs = proc.stdout.readlines()
32-
for out in outs:
33-
if out.split()[0] is diskdev_name:
34-
cmd = "cat /sys/block/{}/queue/rotational".format(diskdev_name)
35-
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
36-
out = proc.stdout.readline()
37-
return out.rstrip()
38-
39-
print("disk {} does not exist in the device".format(diskdev_name))
40-
sys.exit(1)
51+
out = proc.stdout.readline()
52+
if diskdev_name not in out:
53+
return DISK_INVAILD
54+
cmd = "cat /sys/block/{}/queue/rotational".format(diskdev_name)
55+
proc = subprocess.Popen(cmd, shell=True, text=True, stdout=subprocess.PIPE)
56+
out = proc.stdout.readline()
57+
disk_type = out.rstrip()
58+
return disk_type
4159

4260

4361
def import_ssd_api(diskdev):
@@ -79,17 +97,22 @@ def ssdutil():
7997
sys.exit(1)
8098

8199
parser = argparse.ArgumentParser()
82-
parser.add_argument("-d", "--device", help="Device name to show health info", default=DEFAULT_DEVICE)
100+
parser.add_argument("-d", "--device", help="Device name to show health info", default=None)
83101
parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Show verbose output (some additional parameters)")
84102
parser.add_argument("-e", "--vendor", action="store_true", default=False, help="Show vendor output (extended output if provided by platform vendor)")
85103
args = parser.parse_args()
86104

87-
disk_type = get_disk_type(args.device)
105+
if args.device:
106+
disk_device = args.device
107+
else:
108+
disk_device = get_default_disk()
109+
110+
disk_type = get_disk_type(disk_device)
88111
if disk_type != DISK_TYPE_SSD:
89112
print("Disk is not SSD")
90113
sys.exit(1)
91114

92-
ssd = import_ssd_api(args.device)
115+
ssd = import_ssd_api(disk_device)
93116

94117
print("Device Model : {}".format(ssd.get_model()))
95118
if args.verbose:

0 commit comments

Comments
 (0)