|
18 | 18 | DEFAULT_DEVICE="/dev/sda"
|
19 | 19 | SYSLOG_IDENTIFIER = "ssdutil"
|
20 | 20 | DISK_TYPE_SSD = "0"
|
| 21 | +DISK_INVALID = "-1" |
21 | 22 |
|
22 | 23 | # Global logger instance
|
23 | 24 | log = logger.Logger(SYSLOG_IDENTIFIER)
|
24 | 25 |
|
| 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 | + |
25 | 45 |
|
26 | 46 | def get_disk_type(diskdev):
|
27 | 47 | """Check disk type"""
|
28 | 48 | diskdev_name = diskdev.replace('/dev/','')
|
29 |
| - cmd = "lsblk -l -n |grep disk" |
| 49 | + cmd = "lsblk -l -n |grep {}".format(diskdev_name) |
30 | 50 | 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 |
41 | 59 |
|
42 | 60 |
|
43 | 61 | def import_ssd_api(diskdev):
|
@@ -79,17 +97,22 @@ def ssdutil():
|
79 | 97 | sys.exit(1)
|
80 | 98 |
|
81 | 99 | 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) |
83 | 101 | parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Show verbose output (some additional parameters)")
|
84 | 102 | parser.add_argument("-e", "--vendor", action="store_true", default=False, help="Show vendor output (extended output if provided by platform vendor)")
|
85 | 103 | args = parser.parse_args()
|
86 | 104 |
|
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) |
88 | 111 | if disk_type != DISK_TYPE_SSD:
|
89 | 112 | print("Disk is not SSD")
|
90 | 113 | sys.exit(1)
|
91 | 114 |
|
92 |
| - ssd = import_ssd_api(args.device) |
| 115 | + ssd = import_ssd_api(disk_device) |
93 | 116 |
|
94 | 117 | print("Device Model : {}".format(ssd.get_model()))
|
95 | 118 | if args.verbose:
|
|
0 commit comments