diff options
Diffstat (limited to 'src/hardware/lsblk.c')
-rw-r--r-- | src/hardware/lsblk.c | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/hardware/lsblk.c b/src/hardware/lsblk.c index ad4e177..4a06f2c 100644 --- a/src/hardware/lsblk.c +++ b/src/hardware/lsblk.c @@ -33,6 +33,7 @@ void init_lsblkhdd_struct(LsblkHdd *hdd) hdd->serial = NULL; hdd->revision = NULL; hdd->vendor = NULL; + hdd->capacity = 0; } /* @@ -94,14 +95,14 @@ short lsblk_get_hdds(LsblkHdd **hdds, unsigned *hdds_nb) { short ret = -1; unsigned i, curr_hdd = 0, buffer_size = 0; - char **buffer = NULL, *path, *type; + char **buffer = NULL, *path, *type, *buf, errbuf[STRERROR_BUF_LEN]; struct stat sb; - char errbuf[STRERROR_BUF_LEN]; + unsigned long capacity = 0; lsblk_free_hdds(hdds, hdds_nb); /* get lsblk output */ - if (run_command("lsblk -dPpo NAME,TYPE,MODEL,SERIAL,REV,VENDOR", + if (run_command("lsblk -dPpo NAME,TYPE,MODEL,SIZE,SERIAL,REV,VENDOR", &buffer, &buffer_size) != 0) { goto done; } @@ -147,10 +148,30 @@ short lsblk_get_hdds(LsblkHdd **hdds, unsigned *hdds_nb) continue; } + /* size looks like: SIZE="465.8G" */ + buf = get_part_of_string_between(buffer[i], "SIZE=\"", "\""); + if (buf) { + float size = 0; + sscanf(buf, "%f", &size); + if (size) { + const char *letters = "BKMGTPE"; + char unit = buf[strlen(buf) - 1]; + unsigned long multiplier = 0; + for (int i = 0; letters[i]; i++) { + if (unit == letters[i]) { + multiplier = powl(2, 10 * i); + break; + } + } + capacity = round(size * multiplier); + } + } + init_lsblkhdd_struct(&(*hdds)[curr_hdd]); (*hdds)[curr_hdd].name = path; (*hdds)[curr_hdd].type = type; + (*hdds)[curr_hdd].capacity = capacity; (*hdds)[curr_hdd].model = get_part_of_string_between(buffer[i], "MODEL=\"", "\""); (*hdds)[curr_hdd].serial = get_part_of_string_between(buffer[i], "SERIAL=\"", "\""); (*hdds)[curr_hdd].revision = get_part_of_string_between(buffer[i], "REV=\"", "\""); |