2 * Copyright (c) 2023 Benjamin Stürz <benni@stuerz.xyz>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #define _XOPEN_SOURCE 700
21 #include <dev/biovar.h>
22 #include <sys/cdefs.h>
23 #include <sys/types.h>
24 #include <sys/disklabel.h>
25 #include <sys/sysctl.h>
26 #include <sys/ioctl.h>
27 #include <sys/mount.h>
43 static int diskcount (void)
45 const int mib[2] = { CTL_HW, HW_DISKCOUNT };
47 size_t len = sizeof diskcount;
49 if (sysctl (mib, 2, &diskcount, &len, NULL, 0) == -1)
50 err (1, "sysctl(hw.diskcount)");
55 static char *disknames (void)
57 const int num = diskcount ();
58 const int mib[2] = { CTL_HW, HW_DISKNAMES };
59 size_t len = 32 * num;
60 char *buffer = malloc (len);
62 if (sysctl (mib, 2, buffer, &len, NULL, 0) == -1)
63 err (1, "sysctl(hw.disknames)");
68 static char *stripdisk (char *n)
70 const char sufx[] = " disk";
71 const size_t ln = strnlen (n, 16);
72 const size_t ls = sizeof sufx - 1;
74 if (!memcmp (n + ln - ls, sufx, ls)) {
81 static void print_size (uint64_t sz)
88 const struct unit units[] = {
100 for (const struct unit *u = &units[0]; u->factor; ++u) {
101 if (sz >= (u->factor * 9 / 10)) {
108 const unsigned scaled10 = sz * 10 / factor;
109 const unsigned scaled = sz / factor;
110 if (scaled10 >= 1000) {
111 printf ("%u", scaled);
112 } else if (scaled10 >= 100) {
113 printf (" %u", scaled);
115 printf ("%u.%u", scaled, scaled10 % 10);
129 FIELD_COMMENT = 0x40,
131 FIELD_DEFAULT = FIELD_NAME | FIELD_SIZE | FIELD_TYPE | FIELD_COMMENT,
136 OPT_NOUNICODE = 0x02,
149 const struct my_diskinfo *sub; // If this is part of a RAID
150 const char *raidstatus; // Only available if (sub != NULL)
161 struct my_partinfo parts[MAXPARTITIONS];
162 const char *raidstatus; // If this is a RAID device
175 static void print_header (int fields, const struct padding *p)
177 if (fields & FIELD_NAME)
178 printf ("%-*s ", p->name, "NAME");
180 if (fields & FIELD_DUID)
181 printf ("%-18s ", "DUID");
183 if (fields & FIELD_SIZE)
184 printf ("%-4s ", "SIZE");
186 if (fields & FIELD_USED)
187 printf ("%-4s ", "USED");
189 if (fields & FIELD_FREE)
190 printf ("%-4s ", "FREE");
192 if (fields & FIELD_TYPE)
193 printf ("%-*s ", p->type, "TYPE");
195 if (fields & FIELD_COMMENT)
196 printf ("%-*s ", p->comment, "COMMENT");
205 static void print_duid (const u_char *duid)
207 for (size_t i = 0; i < 8; ++i) {
208 printf ("%02x", duid[i]);
212 static void print_disk (const struct my_diskinfo *, int, int, const char *, const struct padding *);
213 static void print_part (
214 const struct my_diskinfo *disk,
215 const struct my_partinfo *part,
219 const struct padding *p
221 if (fields & FIELD_NAME) {
222 const char *prefix = (options & OPT_NOUNICODE) ? " " : (last ? "└─" : "├─");
228 p->name - 2 - (int)strlen (disk->name) - 1,
233 if (fields & FIELD_DUID) {
234 print_duid (disk->duid);
235 printf (".%c ", part->letter);
238 if (fields & FIELD_SIZE)
239 print_size (part->size);
241 if (fields & FIELD_USED) {
243 print_size (part->fssize - part->free);
249 if (fields & FIELD_FREE) {
251 print_size (part->free);
257 if (fields & FIELD_TYPE)
258 printf ("%-*s ", p->type, part->fstype);
260 if (fields & FIELD_COMMENT)
261 printf ("%-*s ", p->comment, part->mount ? part->mount : "");
270 print_disk (part->sub, fields, options, part->raidstatus, p);
274 static void print_disk (
275 const struct my_diskinfo *disk,
278 const char *raidstatus,
279 const struct padding *p
281 if (fields & FIELD_NAME) {
282 const char *prefix = raidstatus ? "│ └─" : "";
287 p->name - (int)strlen (prefix),
292 if (fields & FIELD_DUID) {
293 print_duid (disk->duid);
297 if (fields & FIELD_SIZE)
298 print_size (disk->size);
300 if (fields & FIELD_USED)
301 print_size (disk->used);
303 if (fields & FIELD_FREE)
304 print_size (disk->size - disk->used);
306 if (fields & FIELD_TYPE)
307 printf ("%-*.16s ", p->type, disk->type);
309 if (fields & FIELD_COMMENT) {
311 printf ("%-*s ", p->comment, raidstatus);
312 } else if (disk->raidstatus) {
317 p->comment - (int)strnlen (disk->label, sizeof disk->label) - (int)strlen (disk->raidstatus) - 3,
321 printf ("%-*.16s ", p->comment, disk->label);
332 for (uint8_t i = 0; i < disk->num_parts; ++i)
333 print_part (disk, &disk->parts[i], fields, options, i == (disk->num_parts - 1), p);
337 static const struct statfs *find_mount (const char *dev)
339 static struct statfs *mounts = NULL;
343 n_mounts = getmntinfo (&mounts, MNT_NOWAIT);
345 err (1, "getmntinfo()");
348 for (int i = 0; i < n_mounts; ++i) {
349 if (!strcmp (dev, mounts[i].f_mntfromname))
356 static struct my_diskinfo read_disk (const char *name)
358 struct my_diskinfo disk;
359 struct disklabel label;
360 char *ppath, *letter;
362 bzero (&disk, sizeof disk);
368 fd = opendev (name, O_RDONLY, OPENDEV_PART | OPENDEV_BLCK, &ppath);
370 err (1, "opendev(%s)", name);
372 if (ioctl (fd, DIOCGDINFO, &label) < 0)
373 err (1, "ioctl(%s, DIOCGDINFO)", name);
376 len = strlen (ppath);
377 letter = ppath + len - 1;
380 strlcpy (disk.name, name, sizeof disk.name);
381 disk.size = DL_GETDSIZE (&label) * label.d_secsize;
382 memcpy (disk.type, label.d_typename, sizeof disk.type);
383 stripdisk (disk.type);
384 memcpy (disk.label, label.d_packname, sizeof disk.label);
385 memcpy (disk.duid, label.d_uid, sizeof disk.duid);
388 for (uint16_t i = 0; i < label.d_npartitions; ++i) {
389 const struct partition *p = &label.d_partitions[i];
390 const struct statfs *mnt;
391 struct my_partinfo part;
393 bzero (&part, sizeof part);
395 part.size = DL_GETPSIZE (p) * label.d_secsize;
400 part.letter = 'a' + i;
401 *letter = part.letter;
402 mnt = find_mount (ppath);
405 const uint64_t bs = mnt->f_bsize;
406 part.mount = mnt->f_mntonname;
407 part.fssize = mnt->f_blocks * bs;
408 part.free = mnt->f_bfree * bs;
411 part.fstype = fstypenames[p->p_fstype];
413 disk.used += part.size;
414 disk.parts[disk.num_parts++] = part;
420 static const char *bd_statusstr (int status) {
422 case BIOC_SDONLINE: return BIOC_SDONLINE_S;
423 case BIOC_SDOFFLINE: return BIOC_SDOFFLINE_S;
424 case BIOC_SDFAILED: return BIOC_SDFAILED_S;
425 case BIOC_SDREBUILD: return BIOC_SDREBUILD_S;
426 case BIOC_SDHOTSPARE: return BIOC_SDHOTSPARE_S;
427 case BIOC_SDUNUSED: return BIOC_SDUNUSED_S;
428 case BIOC_SDSCRUB: return BIOC_SDSCRUB_S;
429 case BIOC_SDINVALID: return BIOC_SDINVALID_S;
430 default: return "Unknown";
434 static const char *bv_statusstr (int status) {
436 case BIOC_SVONLINE: return BIOC_SVONLINE_S;
437 case BIOC_SVOFFLINE: return BIOC_SVOFFLINE_S;
438 case BIOC_SVDEGRADED: return BIOC_SVDEGRADED_S;
439 case BIOC_SVBUILDING: return BIOC_SVBUILDING_S;
440 case BIOC_SVSCRUB: return BIOC_SVSCRUB_S;
441 case BIOC_SVREBUILD: return BIOC_SVREBUILD_S;
442 case BIOC_SVINVALID: return BIOC_SVINVALID_S;
443 default: return "Unknown";
447 static void read_raid (
448 struct my_diskinfo *disk,
449 struct my_diskinfo *disks,
455 fd = opendev (disk->name, O_RDONLY, OPENDEV_PART | OPENDEV_BLCK, NULL);
457 warn ("read_raid(): opendev(%s)", disk->name);
461 bzero (&bi, sizeof bi);
463 if (ioctl (fd, BIOCINQ, &bi) == -1)
466 for (int i = 0; i < bi.bi_novol; ++i) {
469 bzero (&bv, sizeof bv);
470 memcpy (&bv.bv_bio, &bi.bi_bio, sizeof bi.bi_bio);
473 if (ioctl (fd, BIOCVOL, &bv) == -1) {
474 warn ("read_raid(%s): BIOCVOL(%d)", disk->name, i);
478 if (strcmp (disk->name, bv.bv_dev) != 0)
481 disk->raidstatus = bv_statusstr (bv.bv_status);
483 for (int j = 0; j < bv.bv_nodisk; ++j) {
488 bzero (&bd, sizeof bd);
489 memcpy (&bd.bd_bio, &bi.bi_bio, sizeof bi.bi_bio);
493 if (ioctl (fd, BIOCDISK, &bd) == -1) {
494 warn ("read_raid(%s): BIOCDISK(%d, %d)", disk->name, i, j);
498 len_vendor = strlen (bd.bd_vendor);
499 if (len_vendor != 4) {
500 warnx ("read_raid(%s): unexpected vendor string: %.32s", disk->name, bd.bd_vendor);
503 letter = bd.bd_vendor[len_vendor - 1];
505 for (size_t k = 0; k < num_disks; ++k) {
506 if (!memcmp (bd.bd_vendor, disks[k].name, 3)) {
507 for (size_t l = 0; l < disks[k].num_parts; ++l) {
508 if (letter == disks[k].parts[l].letter) {
509 disks[k].parts[l].sub = disk;
510 disks[k].parts[l].raidstatus = bd_statusstr(bd.bd_status);
523 static int usage (void)
525 fputs ("Usage: lsblk [-abinUuV] [disk...]\n", stderr);
529 static void pad_update (int *pad, int newval)
535 static void pad_disk (struct padding *p, const struct my_diskinfo *disk)
540 len_disk = strnlen (disk->name, sizeof disk->name);
541 comment = strnlen (disk->label, sizeof disk->label);
542 if (disk->raidstatus)
543 comment += strlen (disk->raidstatus) + 3;
545 pad_update (&p->name, len_disk);
546 pad_update (&p->type, strnlen (disk->type, sizeof disk->type));
547 pad_update (&p->comment, comment);
549 for (int i = 0; i < disk->num_parts; ++i) {
550 const struct my_partinfo *part = &disk->parts[i];
552 pad_update (&p->name, len_disk + 3);
553 pad_update (&p->type, strlen (part->fstype));
556 pad_update (&p->name, strnlen (part->sub->name, sizeof part->sub->name) + 4);
557 pad_update (&p->comment, strlen (part->raidstatus));
558 } else if (part->mount) {
559 pad_update (&p->comment, strlen (part->mount));
565 static int compare_disk (const void *p1, const void *p2)
567 const struct my_diskinfo *d1 = p1;
568 const struct my_diskinfo *d2 = p2;
570 return strcmp (d1->name, d2->name);
573 int main (int argc, char *argv[])
576 int fields = FIELD_DEFAULT;
579 if (unveil ("/dev", "r") == -1)
580 err (1, "unveil(/dev)");
582 if (unveil (NULL, NULL) == -1)
585 while ((option = getopt (argc, argv, ":abinUuV")) != -1) {
591 options |= OPT_NOBIO;
594 options |= OPT_NOUNICODE;
597 options |= OPT_NOHEADER;
600 fields |= FIELD_DUID;
603 fields |= FIELD_USED | FIELD_FREE;
606 puts ("lsblk-" VERSION);
616 char *names = argc == 0 ? disknames () : NULL;
618 if (pledge ("stdio rpath disklabel", NULL) == -1)
623 const char *s = names;
625 while ((s = strchr (s, ',')) != NULL)
631 struct my_diskinfo *disks = calloc (cap_disks, sizeof (struct my_diskinfo));
632 size_t num_disks = 0;
635 for (char *disk; (disk = strsep (&names, ",")) != NULL; ) {
636 char *colon = strchr (disk, ':');
639 disks[num_disks++] = read_disk (disk);
644 for (int i = 0; i < argc; ++i) {
645 char *disk = basename (argv[i]);
646 disks[num_disks++] = read_disk (disk);
652 mergesort (disks, num_disks, sizeof *disks, compare_disk);
654 if (!(options & OPT_NOBIO)) {
655 for (size_t i = 0; i < num_disks; ++i) {
656 read_raid (&disks[i], disks, num_disks);
661 .name = strlen ("NAME"),
662 .type = strlen ("TYPE"),
663 .comment = strlen ("COMMENT"),
666 for (size_t i = 0; i < num_disks; ++i)
667 pad_disk (&p, &disks[i]);
669 if (!(options & OPT_NOHEADER))
670 print_header (fields, &p);
672 for (size_t i = 0; i < num_disks; ++i) {
673 print_disk (&disks[i], fields, options, NULL, &p);