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>
44 static int diskcount (void)
46 const int mib[2] = { CTL_HW, HW_DISKCOUNT };
48 size_t len = sizeof diskcount;
50 if (sysctl (mib, 2, &diskcount, &len, NULL, 0) == -1)
51 err (1, "sysctl(hw.diskcount)");
56 static char *disknames (void)
58 const int num = diskcount ();
59 const int mib[2] = { CTL_HW, HW_DISKNAMES };
60 size_t len = 32 * num;
61 char *buffer = malloc (len);
63 if (sysctl (mib, 2, buffer, &len, NULL, 0) == -1)
64 err (1, "sysctl(hw.disknames)");
69 static char *stripdisk (char *n)
71 const char sufx[] = " disk";
72 const size_t ln = strnlen (n, 16);
73 const size_t ls = sizeof sufx - 1;
75 if (!memcmp (n + ln - ls, sufx, ls)) {
82 static void print_size (uint64_t sz)
89 const struct unit units[] = {
101 for (const struct unit *u = &units[0]; u->factor; ++u) {
102 if (sz >= (u->factor * 9 / 10)) {
109 const unsigned scaled10 = sz * 10 / factor;
110 const unsigned scaled = sz / factor;
111 if (scaled10 >= 1000) {
112 printf ("%u", scaled);
113 } else if (scaled10 >= 100) {
114 printf (" %u", scaled);
116 printf ("%u.%u", scaled, scaled10 % 10);
130 FIELD_COMMENT = 0x40,
132 FIELD_DEFAULT = FIELD_NAME | FIELD_SIZE | FIELD_TYPE | FIELD_COMMENT,
137 OPT_NOUNICODE = 0x02,
150 const struct my_diskinfo *sub; // If this is part of a RAID
151 const char *raidstatus; // Only available if (sub != NULL)
162 struct my_partinfo parts[MAXPARTITIONS];
163 const char *raidstatus; // If this is a RAID device
176 static void print_header (int fields, const struct padding *p)
178 if (fields & FIELD_NAME)
179 printf ("%-*s ", p->name, "NAME");
181 if (fields & FIELD_DUID)
182 printf ("%-18s ", "DUID");
184 if (fields & FIELD_SIZE)
185 printf ("%-4s ", "SIZE");
187 if (fields & FIELD_USED)
188 printf ("%-4s ", "USED");
190 if (fields & FIELD_FREE)
191 printf ("%-4s ", "FREE");
193 if (fields & FIELD_TYPE)
194 printf ("%-*s ", p->type, "TYPE");
196 if (fields & FIELD_COMMENT)
197 printf ("%-*s ", p->comment, "COMMENT");
206 static void print_duid (const u_char *duid)
208 for (size_t i = 0; i < 8; ++i) {
209 printf ("%02x", duid[i]);
213 static void print_disk (const struct my_diskinfo *, int, int, const char *, const struct padding *);
214 static void print_part (
215 const struct my_diskinfo *disk,
216 const struct my_partinfo *part,
220 const struct padding *p
222 if (fields & FIELD_NAME) {
223 const char *prefix = (options & OPT_NOUNICODE) ? " " : (last ? "└─" : "├─");
229 p->name - 2 - (int)strlen (disk->name) - 1,
234 if (fields & FIELD_DUID) {
235 print_duid (disk->duid);
236 printf (".%c ", part->letter);
239 if (fields & FIELD_SIZE)
240 print_size (part->size);
242 if (fields & FIELD_USED) {
244 print_size (part->fssize - part->free);
250 if (fields & FIELD_FREE) {
252 print_size (part->free);
258 if (fields & FIELD_TYPE)
259 printf ("%-*s ", p->type, part->fstype);
261 if (fields & FIELD_COMMENT)
262 printf ("%-*s ", p->comment, part->mount ? part->mount : "");
271 print_disk (part->sub, fields, options, part->raidstatus, p);
275 static void print_disk (
276 const struct my_diskinfo *disk,
279 const char *raidstatus,
280 const struct padding *p
282 if (fields & FIELD_NAME) {
283 const char *prefix = raidstatus ? (options & OPT_NOUNICODE ? " " : "│ └─") : "";
288 p->name - (raidstatus ? 4 : 0),
293 if (fields & FIELD_DUID) {
294 print_duid (disk->duid);
298 if (fields & FIELD_SIZE)
299 print_size (disk->size);
301 if (fields & FIELD_USED)
302 print_size (disk->used);
304 if (fields & FIELD_FREE)
305 print_size (disk->size - disk->used);
307 if (fields & FIELD_TYPE)
308 printf ("%-*.16s ", p->type, disk->type);
310 if (fields & FIELD_COMMENT) {
312 printf ("%-*s ", p->comment, raidstatus);
313 } else if (disk->raidstatus) {
318 p->comment - (int)strnlen (disk->label, sizeof disk->label) - (int)strlen (disk->raidstatus) - 3,
322 printf ("%-*.16s ", p->comment, disk->label);
333 for (uint8_t i = 0; i < disk->num_parts; ++i)
334 print_part (disk, &disk->parts[i], fields, options, i == (disk->num_parts - 1), p);
338 static const struct statfs *find_mount (const char *dev)
340 static struct statfs *mounts = NULL;
344 n_mounts = getmntinfo (&mounts, MNT_NOWAIT);
346 err (1, "getmntinfo()");
349 for (int i = 0; i < n_mounts; ++i) {
350 if (!strcmp (dev, mounts[i].f_mntfromname))
357 static struct my_diskinfo read_disk (const char *name)
359 struct my_diskinfo disk;
360 struct disklabel label;
361 char *ppath, *letter;
363 bzero (&disk, sizeof disk);
369 fd = opendev (name, O_RDONLY, OPENDEV_PART | OPENDEV_BLCK, &ppath);
371 err (1, "opendev(%s)", name);
373 if (ioctl (fd, DIOCGDINFO, &label) < 0)
374 err (1, "ioctl(%s, DIOCGDINFO)", name);
377 len = strlen (ppath);
378 letter = ppath + len - 1;
381 strlcpy (disk.name, name, sizeof disk.name);
382 disk.size = DL_GETDSIZE (&label) * label.d_secsize;
383 memcpy (disk.type, label.d_typename, sizeof disk.type);
384 stripdisk (disk.type);
385 memcpy (disk.label, label.d_packname, sizeof disk.label);
386 memcpy (disk.duid, label.d_uid, sizeof disk.duid);
389 for (uint16_t i = 0; i < label.d_npartitions; ++i) {
390 const struct partition *p = &label.d_partitions[i];
391 const struct statfs *mnt;
392 struct my_partinfo part;
394 bzero (&part, sizeof part);
396 part.size = DL_GETPSIZE (p) * label.d_secsize;
401 part.letter = 'a' + i;
402 *letter = part.letter;
403 mnt = find_mount (ppath);
406 const uint64_t bs = mnt->f_bsize;
407 part.mount = mnt->f_mntonname;
408 part.fssize = mnt->f_blocks * bs;
409 part.free = mnt->f_bfree * bs;
412 part.fstype = fstypenames[p->p_fstype];
414 disk.used += part.size;
415 disk.parts[disk.num_parts++] = part;
421 static const char *bd_statusstr (int status) {
423 case BIOC_SDONLINE: return BIOC_SDONLINE_S;
424 case BIOC_SDOFFLINE: return BIOC_SDOFFLINE_S;
425 case BIOC_SDFAILED: return BIOC_SDFAILED_S;
426 case BIOC_SDREBUILD: return BIOC_SDREBUILD_S;
427 case BIOC_SDHOTSPARE: return BIOC_SDHOTSPARE_S;
428 case BIOC_SDUNUSED: return BIOC_SDUNUSED_S;
429 case BIOC_SDSCRUB: return BIOC_SDSCRUB_S;
430 case BIOC_SDINVALID: return BIOC_SDINVALID_S;
431 default: return "Unknown";
435 static const char *bv_statusstr (int status) {
437 case BIOC_SVONLINE: return BIOC_SVONLINE_S;
438 case BIOC_SVOFFLINE: return BIOC_SVOFFLINE_S;
439 case BIOC_SVDEGRADED: return BIOC_SVDEGRADED_S;
440 case BIOC_SVBUILDING: return BIOC_SVBUILDING_S;
441 case BIOC_SVSCRUB: return BIOC_SVSCRUB_S;
442 case BIOC_SVREBUILD: return BIOC_SVREBUILD_S;
443 case BIOC_SVINVALID: return BIOC_SVINVALID_S;
444 default: return "Unknown";
448 static void read_raid (
449 struct my_diskinfo *disk,
450 struct my_diskinfo *disks,
456 fd = opendev (disk->name, O_RDONLY, OPENDEV_PART | OPENDEV_BLCK, NULL);
458 warn ("read_raid(): opendev(%s)", disk->name);
462 bzero (&bi, sizeof bi);
464 if (ioctl (fd, BIOCINQ, &bi) == -1)
467 for (int i = 0; i < bi.bi_novol; ++i) {
470 bzero (&bv, sizeof bv);
471 memcpy (&bv.bv_bio, &bi.bi_bio, sizeof bi.bi_bio);
474 if (ioctl (fd, BIOCVOL, &bv) == -1) {
475 warn ("read_raid(%s): BIOCVOL(%d)", disk->name, i);
479 if (strcmp (disk->name, bv.bv_dev) != 0)
482 disk->raidstatus = bv_statusstr (bv.bv_status);
484 for (int j = 0; j < bv.bv_nodisk; ++j) {
489 bzero (&bd, sizeof bd);
490 memcpy (&bd.bd_bio, &bi.bi_bio, sizeof bi.bi_bio);
494 if (ioctl (fd, BIOCDISK, &bd) == -1) {
495 warn ("read_raid(%s): BIOCDISK(%d, %d)", disk->name, i, j);
499 len_vendor = strlen (bd.bd_vendor);
500 if (len_vendor < 4 || len_vendor > 8) {
501 warnx ("read_raid(%s): unexpected vendor string: %.32s", disk->name, bd.bd_vendor);
504 letter = bd.bd_vendor[len_vendor - 1];
506 for (size_t k = 0; k < num_disks; ++k) {
507 if (!memcmp (bd.bd_vendor, disks[k].name, 3)) {
508 for (size_t l = 0; l < disks[k].num_parts; ++l) {
509 if (letter == disks[k].parts[l].letter) {
510 disks[k].parts[l].sub = disk;
511 disks[k].parts[l].raidstatus = bd_statusstr(bd.bd_status);
524 static int usage (void)
526 fputs ("Usage: lsblk [-abinUuV] [disk...]\n", stderr);
530 static void pad_update (int *pad, int newval)
536 static void pad_disk (struct padding *p, const struct my_diskinfo *disk)
541 len_disk = strnlen (disk->name, sizeof disk->name);
542 comment = strnlen (disk->label, sizeof disk->label);
543 if (disk->raidstatus)
544 comment += strlen (disk->raidstatus) + 3;
546 pad_update (&p->name, len_disk);
547 pad_update (&p->type, strnlen (disk->type, sizeof disk->type));
548 pad_update (&p->comment, comment);
550 for (int i = 0; i < disk->num_parts; ++i) {
551 const struct my_partinfo *part = &disk->parts[i];
553 pad_update (&p->name, len_disk + 3);
554 pad_update (&p->type, strlen (part->fstype));
557 pad_update (&p->name, strnlen (part->sub->name, sizeof part->sub->name) + 4);
558 pad_update (&p->comment, strlen (part->raidstatus));
559 } else if (part->mount) {
560 pad_update (&p->comment, strlen (part->mount));
566 static int compare_disk (const void *p1, const void *p2)
568 const struct my_diskinfo *d1 = p1;
569 const struct my_diskinfo *d2 = p2;
571 return strcmp (d1->name, d2->name);
574 int main (int argc, char *argv[])
577 int fields = FIELD_DEFAULT;
580 if (unveil ("/dev", "r") == -1)
581 err (1, "unveil(/dev)");
583 if (unveil (NULL, NULL) == -1)
586 while ((option = getopt (argc, argv, ":abinUuV")) != -1) {
592 options |= OPT_NOBIO;
595 options |= OPT_NOUNICODE;
598 options |= OPT_NOHEADER;
601 fields |= FIELD_DUID;
604 fields |= FIELD_USED | FIELD_FREE;
607 puts ("lsblk-" VERSION);
617 char *names = argc == 0 ? disknames () : NULL;
619 if (pledge ("stdio rpath disklabel", NULL) == -1)
624 const char *s = names;
626 while ((s = strchr (s, ',')) != NULL)
632 struct my_diskinfo *disks = calloc (cap_disks, sizeof (struct my_diskinfo));
633 size_t num_disks = 0;
636 for (char *disk; (disk = strsep (&names, ",")) != NULL; ) {
637 char *colon = strchr (disk, ':');
640 disks[num_disks++] = read_disk (disk);
645 for (int i = 0; i < argc; ++i) {
646 char *disk = basename (argv[i]);
647 char *last = disk + strlen (disk) - 1;
648 if (isalpha (*last)) {
649 warnx ("%s: specifying a partition is not supported, using the drive.", disk);
652 disks[num_disks++] = read_disk (disk);
658 mergesort (disks, num_disks, sizeof *disks, compare_disk);
660 if (!(options & OPT_NOBIO)) {
661 for (size_t i = 0; i < num_disks; ++i) {
662 read_raid (&disks[i], disks, num_disks);
667 .name = strlen ("NAME"),
668 .type = strlen ("TYPE"),
669 .comment = strlen ("COMMENT"),
672 for (size_t i = 0; i < num_disks; ++i)
673 pad_disk (&p, &disks[i]);
675 if (!(options & OPT_NOHEADER))
676 print_header (fields, &p);
678 for (size_t i = 0; i < num_disks; ++i) {
679 print_disk (&disks[i], fields, options, NULL, &p);