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
20 #include <dev/biovar.h>
21 #include <sys/cdefs.h>
22 #include <sys/types.h>
23 #include <sys/disklabel.h>
24 #include <sys/sysctl.h>
25 #include <sys/ioctl.h>
26 #include <sys/mount.h>
42 static int diskcount (void)
44 const int mib[2] = { CTL_HW, HW_DISKCOUNT };
46 size_t len = sizeof diskcount;
48 if (sysctl (mib, 2, &diskcount, &len, NULL, 0) == -1)
49 err (1, "sysctl(hw.diskcount)");
54 static char *disknames (void)
56 const int num = diskcount ();
57 const int mib[2] = { CTL_HW, HW_DISKNAMES };
58 size_t len = 32 * num;
59 char *buffer = malloc (len);
61 if (sysctl (mib, 2, buffer, &len, NULL, 0) == -1)
62 err (1, "sysctl(hw.disknames)");
67 static char *stripdisk (char *n)
69 const char sufx[] = " disk";
70 const size_t ln = strnlen (n, 16);
71 const size_t ls = sizeof sufx - 1;
73 if (!memcmp (n + ln - ls, sufx, ls)) {
80 static void print_size (uint64_t sz)
87 const struct unit units[] = {
99 for (const struct unit *u = &units[0]; u->factor; ++u) {
100 if (sz >= (u->factor * 9 / 10)) {
107 const unsigned scaled10 = sz * 10 / factor;
108 const unsigned scaled = sz / factor;
109 if (scaled10 >= 1000) {
110 printf ("%u", scaled);
111 } else if (scaled10 >= 100) {
112 printf (" %u", scaled);
114 printf ("%u.%u", scaled, scaled10 % 10);
128 FIELD_COMMENT = 0x40,
130 FIELD_DEFAULT = FIELD_NAME | FIELD_SIZE | FIELD_TYPE | FIELD_COMMENT,
135 OPT_NOUNICODE = 0x02,
148 const struct my_diskinfo *sub; // If this is part of a RAID
149 const char *raidstatus; // Only available if (sub != NULL)
160 struct my_partinfo parts[MAXPARTITIONS];
161 const char *raidstatus; // If this is a RAID device
164 static void print_header (int fields)
166 if (fields & FIELD_NAME)
167 printf ("%-7s ", "NAME");
169 if (fields & FIELD_DUID)
170 printf ("%-18s ", "DUID");
172 if (fields & FIELD_SIZE)
173 printf ("%-4s ", "SIZE");
175 if (fields & FIELD_USED)
176 printf ("%-4s ", "USED");
178 if (fields & FIELD_FREE)
179 printf ("%-4s ", "FREE");
181 if (fields & FIELD_TYPE)
182 printf ("%-8s ", "TYPE");
184 if (fields & FIELD_COMMENT)
190 static void print_duid (const u_char *duid)
192 for (size_t i = 0; i < 8; ++i) {
193 printf ("%02x", duid[i]);
197 static void print_disk (const struct my_diskinfo *disk, int fields, int options, const char *raidstatus);
198 static void print_part (
199 const struct my_diskinfo *disk,
200 const struct my_partinfo *part,
205 if (fields & FIELD_NAME) {
206 const char *prefix = (options & OPT_NOUNICODE) ? " " : (last ? "└─" : "├─");
207 printf ("%s%s%c ", prefix, disk->name, part->letter);
210 if (fields & FIELD_DUID) {
211 print_duid (disk->duid);
212 printf (".%c ", part->letter);
215 if (fields & FIELD_SIZE)
216 print_size (part->size);
218 if (fields & FIELD_USED) {
220 print_size (part->fssize - part->free);
226 if (fields & FIELD_FREE) {
228 print_size (part->free);
234 if (fields & FIELD_TYPE)
235 printf ("%-8.16s ", part->fstype);
237 if (fields & FIELD_COMMENT && part->mount)
238 printf ("%s ", part->mount);
244 print_disk (part->sub, fields, options, part->raidstatus);
248 static void print_disk (const struct my_diskinfo *disk, int fields, int options, const char *raidstatus)
250 if (fields & FIELD_NAME) {
251 printf ("%s ", disk->name);
256 if (fields & FIELD_DUID) {
257 print_duid (disk->duid);
261 if (fields & FIELD_SIZE)
262 print_size (disk->size);
264 if (fields & FIELD_USED)
265 print_size (disk->used);
267 if (fields & FIELD_FREE)
268 print_size (disk->size - disk->used);
270 // Pad only upto 8 characters because most disk types are <=8 bytes long.
271 if (fields & FIELD_TYPE)
272 printf ("%-8.16s ", disk->type);
274 if (fields & FIELD_COMMENT) {
276 printf ("%s ", raidstatus);
277 } else if (disk->raidstatus) {
278 printf ("%.16s (%s) ", disk->label, disk->raidstatus);
280 printf ("%.16s ", disk->label);
287 for (uint8_t i = 0; i < disk->num_parts; ++i)
288 print_part (disk, &disk->parts[i], fields, options, i == (disk->num_parts - 1));
292 static const struct statfs *find_mount (const char *dev)
294 static struct statfs *mounts = NULL;
298 n_mounts = getmntinfo (&mounts, MNT_NOWAIT);
300 err (1, "getmntinfo()");
303 for (int i = 0; i < n_mounts; ++i) {
304 if (!strcmp (dev, mounts[i].f_mntfromname))
311 static struct my_diskinfo read_disk (const char *name)
313 struct my_diskinfo disk;
314 struct disklabel label;
315 char *ppath, *letter;
317 bzero (&disk, sizeof disk);
323 fd = opendev (name, O_RDONLY, OPENDEV_PART | OPENDEV_BLCK, &ppath);
325 err (1, "opendev(%s)", name);
327 if (ioctl (fd, DIOCGDINFO, &label) < 0)
328 err (1, "ioctl(%s, DIOCGDINFO)", name);
331 len = strlen (ppath);
332 letter = ppath + len - 1;
335 memcpy (disk.name, name, 3);
337 disk.size = DL_GETDSIZE (&label) * label.d_secsize;
338 memcpy (disk.type, label.d_typename, 16);
339 stripdisk (disk.type);
340 memcpy (disk.label, label.d_packname, 16);
341 memcpy (disk.duid, label.d_uid, sizeof disk.duid);
344 for (uint16_t i = 0; i < label.d_npartitions; ++i) {
345 const struct partition *p = &label.d_partitions[i];
346 const struct statfs *mnt;
347 struct my_partinfo part;
349 bzero (&part, sizeof part);
351 part.size = DL_GETPSIZE (p) * label.d_secsize;
356 part.letter = 'a' + i;
357 *letter = part.letter;
358 mnt = find_mount (ppath);
361 const uint64_t bs = mnt->f_bsize;
362 part.mount = mnt->f_mntonname;
363 part.fssize = mnt->f_blocks * bs;
364 part.free = mnt->f_bfree * bs;
367 part.fstype = fstypenames[p->p_fstype];
369 disk.used += part.size;
370 disk.parts[disk.num_parts++] = part;
376 static const char *bd_statusstr (int status) {
378 case BIOC_SDONLINE: return BIOC_SDONLINE_S;
379 case BIOC_SDOFFLINE: return BIOC_SDOFFLINE_S;
380 case BIOC_SDFAILED: return BIOC_SDFAILED_S;
381 case BIOC_SDREBUILD: return BIOC_SDREBUILD_S;
382 case BIOC_SDHOTSPARE: return BIOC_SDHOTSPARE_S;
383 case BIOC_SDUNUSED: return BIOC_SDUNUSED_S;
384 case BIOC_SDSCRUB: return BIOC_SDSCRUB_S;
385 case BIOC_SDINVALID: return BIOC_SDINVALID_S;
386 default: return "Unknown";
390 static const char *bv_statusstr (int status) {
392 case BIOC_SVONLINE: return BIOC_SVONLINE_S;
393 case BIOC_SVOFFLINE: return BIOC_SVOFFLINE_S;
394 case BIOC_SVDEGRADED: return BIOC_SVDEGRADED_S;
395 case BIOC_SVBUILDING: return BIOC_SVBUILDING_S;
396 case BIOC_SVSCRUB: return BIOC_SVSCRUB_S;
397 case BIOC_SVREBUILD: return BIOC_SVREBUILD_S;
398 case BIOC_SVINVALID: return BIOC_SVINVALID_S;
399 default: return "Unknown";
403 static void read_raid (
404 struct my_diskinfo *disk,
405 struct my_diskinfo *disks,
411 fd = opendev (disk->name, O_RDONLY, OPENDEV_PART | OPENDEV_BLCK, NULL);
413 warn ("read_raid(): opendev(%s)", disk->name);
417 bzero (&bi, sizeof bi);
419 if (ioctl (fd, BIOCINQ, &bi) == -1)
422 for (int i = 0; i < bi.bi_novol; ++i) {
425 bzero (&bv, sizeof bv);
426 memcpy (&bv.bv_bio, &bi.bi_bio, sizeof bi.bi_bio);
429 if (ioctl (fd, BIOCVOL, &bv) == -1) {
430 warn ("read_raid(%s): BIOCVOL(%d)", disk->name, i);
434 if (strcmp (disk->name, bv.bv_dev) != 0)
437 disk->raidstatus = bv_statusstr (bv.bv_status);
439 for (int j = 0; j < bv.bv_nodisk; ++j) {
444 bzero (&bd, sizeof bd);
445 memcpy (&bd.bd_bio, &bi.bi_bio, sizeof bi.bi_bio);
449 if (ioctl (fd, BIOCDISK, &bd) == -1) {
450 warn ("read_raid(%s): BIOCDISK(%d, %d)", disk->name, i, j);
454 len_vendor = strlen (bd.bd_vendor);
455 if (len_vendor != 4) {
456 warnx ("read_raid(%s): unexpected vendor string: %.32s", disk->name, bd.bd_vendor);
459 letter = bd.bd_vendor[len_vendor - 1];
461 for (size_t k = 0; k < num_disks; ++k) {
462 if (!memcmp (bd.bd_vendor, disks[k].name, 3)) {
463 for (size_t l = 0; l < disks[k].num_parts; ++l) {
464 if (letter == disks[k].parts[l].letter) {
465 disks[k].parts[l].sub = disk;
466 disks[k].parts[l].raidstatus = bd_statusstr(bd.bd_status);
479 static int usage (void)
481 fputs ("Usage: lsblk [-abinUuV] [disk...]\n", stderr);
485 int main (int argc, char *argv[])
488 int fields = FIELD_DEFAULT;
491 if (unveil ("/dev", "r") == -1)
492 err (1, "unveil(/dev)");
494 if (unveil (NULL, NULL) == -1)
497 while ((option = getopt (argc, argv, ":abinUuV")) != -1) {
503 options |= OPT_NOBIO;
506 options |= OPT_NOUNICODE;
509 options |= OPT_NOHEADER;
512 fields |= FIELD_DUID;
515 fields |= FIELD_USED | FIELD_FREE;
518 puts ("lsblk-" VERSION);
528 char *names = argc == 0 ? disknames () : NULL;
530 if (pledge ("stdio rpath disklabel", NULL) == -1)
535 const char *s = names;
537 while ((s = strchr (s, ',')) != NULL)
543 struct my_diskinfo *disks = calloc (cap_disks, sizeof (struct my_diskinfo));
544 size_t num_disks = 0;
547 for (char *disk; (disk = strsep (&names, ",")) != NULL; ) {
548 char *colon = strchr (disk, ':');
551 disks[num_disks++] = read_disk (disk);
556 for (int i = 0; i < argc; ++i) {
557 char *disk = basename (argv[i]);
558 disks[num_disks++] = read_disk (disk);
564 if (!(options & OPT_NOBIO)) {
565 for (size_t i = 0; i < num_disks; ++i) {
566 read_raid (&disks[i], disks, num_disks);
570 if (!(options & OPT_NOHEADER))
571 print_header (fields);
573 for (size_t i = 0; i < num_disks; ++i) {
574 print_disk (&disks[i], fields, options, false);