2 a6199142 2025-11-21 me * Copyright (c) 2025 Tobias Heider <tobhe@openbsd.org>
4 a6199142 2025-11-21 me * Permission to use, copy, modify, and distribute this software for any
5 a6199142 2025-11-21 me * purpose with or without fee is hereby granted, provided that the above
6 a6199142 2025-11-21 me * copyright notice and this permission notice appear in all copies.
8 a6199142 2025-11-21 me * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a6199142 2025-11-21 me * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a6199142 2025-11-21 me * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a6199142 2025-11-21 me * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a6199142 2025-11-21 me * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a6199142 2025-11-21 me * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a6199142 2025-11-21 me * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a6199142 2025-11-21 me #include <err.h>
18 a6199142 2025-11-21 me #include <fcntl.h>
19 a6199142 2025-11-21 me #include <stdlib.h>
20 a6199142 2025-11-21 me #include <stdio.h>
21 a6199142 2025-11-21 me #include <string.h>
22 a6199142 2025-11-21 me #include <unistd.h>
23 a6199142 2025-11-21 me #include <util.h>
25 a6199142 2025-11-21 me #define DKTYPENAMES
26 a6199142 2025-11-21 me #include <sys/disklabel.h>
27 a6199142 2025-11-21 me #include <sys/dkio.h>
28 a6199142 2025-11-21 me #include <sys/types.h>
29 a6199142 2025-11-21 me #include <sys/sysctl.h>
30 a6199142 2025-11-21 me #include <sys/mount.h>
31 a6199142 2025-11-21 me #include <sys/ioctl.h>
32 a6199142 2025-11-21 me #include <sys/param.h>
34 a6199142 2025-11-21 me #define MEG(x) ((x) * 1024LL * (1024 / DEV_BSIZE))
35 a6199142 2025-11-21 me #define GIG(x) (MEG(x) * 1024LL)
37 a6199142 2025-11-21 me struct statfs *mntbuf;
43 a6199142 2025-11-21 me fprintf(stderr,
44 80f6ec26 2025-11-21 me "usage: disks [-h]\n"
45 80f6ec26 2025-11-21 me " disks [-h] dev\n");
50 a6199142 2025-11-21 me sysctl_disknames_get(void)
54 a6199142 2025-11-21 me int mib[2] = { CTL_HW, HW_DISKNAMES } ;
57 a6199142 2025-11-21 me ret = sysctl(mib, 2, NULL, &len, NULL, 0);
58 a6199142 2025-11-21 me if (ret == -1)
59 a6199142 2025-11-21 me err(1, "%s: sysctl", __func__);
61 a6199142 2025-11-21 me buf = malloc(len);
62 a6199142 2025-11-21 me if (buf == NULL)
65 a6199142 2025-11-21 me ret = sysctl(mib, 2, buf, &len, NULL, 0);
66 a6199142 2025-11-21 me if (ret == -1)
67 a6199142 2025-11-21 me err(1, "%s: sysctl", __func__);
73 806d289f 2025-11-21 me print_partition(const char *devname, const struct disklabel *dl, int i, int last,
76 a6199142 2025-11-21 me const struct partition *pp = &dl->d_partitions[i];
77 a6199142 2025-11-21 me char sbuf[FMT_SCALED_STRSIZE];
82 a6199142 2025-11-21 me if (DL_GETPSIZE(pp)) {
83 47e7231a 2025-11-21 me printf("\n%c-- %s%s%c\t", last ? '`' : '|',devname,
84 47e7231a 2025-11-21 me isduid(devname, OPENDEV_PART) ? "." : "",
85 806d289f 2025-11-21 me DL_PARTNUM2NAME(i));
88 a6199142 2025-11-21 me if(fmt_scaled(DL_GETPSIZE(pp) * dl->d_secsize, sbuf) == -1)
89 a6199142 2025-11-21 me err(1, "%s: fmt_scaled()", __func__);
90 a6199142 2025-11-21 me printf("%7s", sbuf);
92 a6199142 2025-11-21 me printf("%16llu", DL_GETPSIZE(pp));
94 a6199142 2025-11-21 me printf("\t%8s", fstypenames[pp->p_fstype]);
96 a6199142 2025-11-21 me dplen = asprintf(&dp, "/dev/%s%c", devname, DL_PARTNUM2NAME(i));
98 b74c2a4b 2025-11-21 me for (j = 0; j < mntsize; j++)
99 b74c2a4b 2025-11-21 me if (strncmp(mntbuf[j].f_mntfromname, dp, dplen) == 0)
100 b74c2a4b 2025-11-21 me printf("\t%s", mntbuf[j].f_mntonname);
106 a6199142 2025-11-21 me print_device(char *devname, int human)
108 a6199142 2025-11-21 me struct dk_inquiry di;
109 a6199142 2025-11-21 me struct disklabel dl;
111 806d289f 2025-11-21 me int i, last_i;
113 a6199142 2025-11-21 me /* Only root is allowed to do this */
114 a6199142 2025-11-21 me dev = opendev(devname, O_RDONLY, OPENDEV_PART, NULL);
115 a6199142 2025-11-21 me if (dev == -1)
116 079f6d8d 2025-11-21 me err(1, "failed to open %s",devname);
118 a6199142 2025-11-21 me if (ioctl(dev, DIOCINQ, &di) == -1)
119 a6199142 2025-11-21 me err(1, "%s: ioctl(DIOCINQ)", __func__);
120 a6199142 2025-11-21 me if (ioctl(dev, DIOCGDINFO, &dl) == -1)
121 a6199142 2025-11-21 me err(1, "%s: ioctl(DIOCGDINFO)", __func__);
123 edb140af 2025-11-22 me printf("%s", devname);
124 edb140af 2025-11-22 me const uint8_t duid_zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0};
125 edb140af 2025-11-22 me if (!isduid(devname, OPENDEV_PART) && (memcmp(dl.d_uid, duid_zero, sizeof(duid_zero)) != 0)) {
126 edb140af 2025-11-22 me printf(":%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
127 47e7231a 2025-11-21 me dl.d_uid[0], dl.d_uid[1], dl.d_uid[2], dl.d_uid[3],
128 47e7231a 2025-11-21 me dl.d_uid[4], dl.d_uid[5], dl.d_uid[6], dl.d_uid[7]);
130 6efb5c5d 2025-11-21 me printf(" [%s %s]", di.vendor, di.product);
133 a6199142 2025-11-21 me for (i = 0; i < dl.d_npartitions; i++)
134 806d289f 2025-11-21 me if (DL_GETPSIZE(&dl.d_partitions[i]))
137 806d289f 2025-11-21 me for (i = 0; i < dl.d_npartitions; i++)
138 806d289f 2025-11-21 me print_partition(devname, &dl, i, i == last_i, human);
139 a6199142 2025-11-21 me printf("\n\n");
143 a6199142 2025-11-21 me main(int argc, char *argv[])
145 a6199142 2025-11-21 me char *disks = sysctl_disknames_get();
147 a6199142 2025-11-21 me int ch, human = 0;
149 a6199142 2025-11-21 me while ((ch = getopt(argc, argv, "h")) != -1) {
150 a6199142 2025-11-21 me switch (ch) {
158 a6199142 2025-11-21 me argc -= optind;
159 a6199142 2025-11-21 me argv += optind;
161 a6199142 2025-11-21 me if ((mntsize = getmntinfo(&mntbuf, MNT_WAIT)) == 0)
162 a6199142 2025-11-21 me err(1, "%s: getmntinfo", __func__);
165 b74c2a4b 2025-11-21 me printf("DISK \t TOTAL\t FSTYPE\tMOUNT\n");
167 fe48607a 2025-11-21 me printf("DISK \t TOTAL\t FSTYPE\tMOUNT\n");
170 21d2a164 2025-11-21 me if (argc == 1 && argv[0] != NULL) {
171 21d2a164 2025-11-21 me print_device(argv[0], human);
175 a6199142 2025-11-21 me /* Print all of those disks*/
176 a6199142 2025-11-21 me while ((p = strsep(&disks, ",")) != NULL) {
178 a6199142 2025-11-21 me p = strsep(&p, ":");
179 a6199142 2025-11-21 me if (p == NULL)
180 a6199142 2025-11-21 me errx(1, "%s: strsep", __func__);
182 a6199142 2025-11-21 me print_device(p, human);