commit 0cfa8f6cf8ce19a61e4951bee75dacb39bfd9b0a
parent c9b9b5509c398a2e097b530ad3a644ab888ecb96
Author: xplshn <anto@xplshn.com.ar>
Date: Tue, 24 Nov 2026 14:32:11 -0300
diskutil: a backend per kernel
Diffstat:
8 files changed, 521 insertions(+), 387 deletions(-)
diff --git a/ninqu.rules b/ninqu.rules
@@ -40,7 +40,9 @@
(rule LIBUTIL_OBJ
(glob "shared/libutil/*.c")
- (skip "filesystem_linux.c" "filesystem_other.c"
+ (skip "diskutil_freebsd.c" "diskutil_linux.c" "diskutil_netbsd.c"
+ "diskutil_openbsd.c" "diskutil_other.c"
+ "filesystem_linux.c" "filesystem_other.c"
"net_linux.c" "net_other.c"
"passwd_dragonfly.c" "passwd_freebsd.c" "passwd_linux.c"
"passwd_netbsd.c" "passwd_openbsd.c" "passwd_other.c"
@@ -52,7 +54,12 @@
(exec $(CC) $(CPPFLAGS_LIBUTIL) -Ishared/libutil $(CFLAGS) -c -o $(OUT) $(MATCH)))
(rule LIBUTIL_OS_OBJ
- (glob "shared/libutil/filesystem_linux.c"
+ (glob "shared/libutil/diskutil_freebsd.c"
+ "shared/libutil/diskutil_linux.c"
+ "shared/libutil/diskutil_netbsd.c"
+ "shared/libutil/diskutil_openbsd.c"
+ "shared/libutil/diskutil_other.c"
+ "shared/libutil/filesystem_linux.c"
"shared/libutil/filesystem_other.c"
"shared/libutil/net_linux.c"
"shared/libutil/net_other.c"
@@ -66,7 +73,7 @@
"shared/libutil/proc_other.c"
"shared/libutil/sysinfo_linux.c"
"shared/libutil/sysinfo_other.c")
- (dep $(MATCH) "shared/filesystem.h" "shared/net.h" "shared/passwd.h"
+ (dep $(MATCH) "shared/diskutil.h" "shared/filesystem.h" "shared/net.h" "shared/passwd.h"
"shared/proc.h" "shared/sysinfo.h" FEATURES_H)
(after NOFORK_LIST_H)
(out "shared/libutil/$(BASESTEM).o")
diff --git a/shared/diskutil.h b/shared/diskutil.h
@@ -46,4 +46,11 @@ int blockdev_detect_fs(
struct BlockDevInfo *blockdev_get_list(void);
void blockdev_free_list(struct BlockDevInfo *list);
+/* a backend per kernel: the size of a device, rereading a table written
+ * to it, and the list of disks. blockdev_probe_list opens the names most
+ * systems give disks, for a kernel that has no way to list them */
+void blockdev_sys_size(struct BlockDev *dev);
+void blockdev_sys_reread(struct BlockDev *dev);
+struct BlockDevInfo *blockdev_probe_list(void);
+
#endif
diff --git a/shared/libutil/diskutil.c b/shared/libutil/diskutil.c
@@ -11,23 +11,6 @@
#include <sys/types.h>
#include <unistd.h>
-#if defined(__linux__)
-#include <linux/fs.h>
-#include <sys/ioctl.h>
-#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__Apple__)
-#include <sys/disk.h>
-#include <sys/ioctl.h>
-#if defined(__OpenBSD__) || defined(__NetBSD__)
-#include <sys/disklabel.h>
-#include <sys/dkio.h>
-#include <sys/sysctl.h>
-#elif defined(__FreeBSD__)
-#include <dirent.h>
-#include <sys/disk.h>
-#include <sys/sysctl.h>
-#endif
-#endif
-
int
blockdev_open(struct BlockDev *dev, const char *path, int writable)
{
@@ -49,32 +32,8 @@ blockdev_open(struct BlockDev *dev, const char *path, int writable)
dev->sec_size = 512;
dev->dirty = 0;
- if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
-#if defined(__linux__)
- uint64_t size_bytes = 0;
- int sec_size = 0;
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Woverflow"
- if (ioctl(fd, BLKGETSIZE64, &size_bytes) >= 0)
- dev->size = size_bytes;
- if (ioctl(fd, BLKSSZGET, &sec_size) >= 0)
- dev->sec_size = sec_size;
-#pragma GCC diagnostic pop
-#elif defined(__FreeBSD__)
- off_t size_bytes = 0;
- unsigned int sec_size = 0;
- if (ioctl(fd, DIOCGMEDIASIZE, &size_bytes) >= 0)
- dev->size = size_bytes;
- if (ioctl(fd, DIOCGSECTORSIZE, &sec_size) >= 0)
- dev->sec_size = sec_size;
-#elif defined(__OpenBSD__) || defined(__NetBSD__)
- struct disklabel dl;
- if (ioctl(fd, DIOCGDINFO, &dl) >= 0) {
- dev->size = (uint64_t)dl.d_secsize * DL_GETDSIZE(&dl);
- dev->sec_size = dl.d_secsize;
- }
-#endif
- }
+ if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
+ blockdev_sys_size(dev);
return 0;
}
@@ -117,354 +76,17 @@ blockdev_write(struct BlockDev *dev, uint64_t sector, const void *buf, size_t se
void
blockdev_close(struct BlockDev *dev)
{
-#if defined(__linux__)
- struct stat st;
-
- // the kernel keeps the partition table it read when the disk appeared:
- // a table written here has no device nodes until it is asked to read
- // again, which fails harmlessly while a partition is in use
- if (dev->dirty && dev->fd >= 0 && fstat(dev->fd, &st) == 0 && S_ISBLK(st.st_mode))
- ioctl(dev->fd, BLKRRPART);
-#endif
+ if (dev->dirty && dev->fd >= 0)
+ blockdev_sys_reread(dev);
if (dev->fd >= 0) {
close(dev->fd);
dev->fd = -1;
}
}
-#if defined(__linux__)
-/* the model the disk reports, without the blanks firmware pads it with */
-static void
-disk_model(const char *name, char *out, size_t n)
-{
- char path[128];
- FILE *f;
- size_t len;
-
- out[0] = '\0';
- snprintf(path, sizeof(path), "/sys/block/%s/device/model", name);
- if (!(f = fopen(path, "r")))
- return;
- if (fgets(out, (int)n, f)) {
- out[strcspn(out, "\n")] = '\0';
- for (len = strlen(out); len > 0 && out[len - 1] == ' '; len--)
- out[len - 1] = '\0';
- }
- fclose(f);
-}
-#endif
-
struct BlockDevInfo *
-blockdev_get_list(void)
+blockdev_probe_list(void)
{
-#if defined(__linux__)
- FILE *fp;
- FILE *mfile;
- char line[256];
- char mline[512];
- char devpath[128];
- char mnt[256];
- char fs[64];
- struct BlockDevInfo *head = NULL;
- struct BlockDevInfo *tail = NULL;
- struct BlockDevInfo *info;
- struct BlockDevInfo *curr;
- unsigned int major, minor;
- unsigned long long blocks;
- char name[64];
- int is_part;
-
- fp = fopen(ARUU_LINUX_PATH_PROC_PARTITIONS, "r");
- if (!fp)
- return NULL;
-
- while (fgets(line, sizeof(line), fp)) {
- if (sscanf(line, " %u %u %llu %63s", &major, &minor, &blocks, name) != 4)
- continue;
-
- info = ecalloc(1, sizeof(*info));
-
- strlcpy(info->name, name, sizeof(info->name));
- snprintf(info->path, sizeof(info->path), "%s/%s", ARUU_PATH_DEV, name);
- info->size = blocks * 1024;
- info->major = major;
- info->minor = minor;
-
- /* search tree topology to link partition node under proper
- * target parent disk */
- is_part = 0;
- for (curr = head; curr; curr = curr->next) {
- size_t len = strlen(curr->name);
- if (strncmp(name, curr->name, len) == 0) {
- char c = name[len];
- if (curr->name[len - 1] >= '0' && curr->name[len - 1] <= '9') {
- if (c == 'p' && name[len + 1] >= '0' && name[len + 1] <= '9')
- is_part = 1;
- } else {
- if (c >= '0' && c <= '9')
- is_part = 1;
- }
- if (is_part) {
- strlcpy(info->type, "part", sizeof(info->type));
- struct BlockDevInfo *p = curr->parts;
- if (!p) {
- curr->parts = info;
- } else {
- while (p->next)
- p = p->next;
- p->next = info;
- }
- break;
- }
- }
- }
-
- if (!is_part) {
- strlcpy(info->type, "disk", sizeof(info->type));
- disk_model(info->name, info->model, sizeof(info->model));
- if (!head) {
- head = info;
- } else {
- tail->next = info;
- }
- tail = info;
- }
- }
- fclose(fp);
-
- mfile = fopen(ARUU_LINUX_PATH_PROC_MOUNTS, "r");
- if (mfile) {
- while (fgets(mline, sizeof(mline), mfile)) {
- if (sscanf(mline, "%127s %255s %63s", devpath, mnt, fs) == 3) {
- char *devname = devpath;
- size_t dev_len = strlen(ARUU_PATH_DEV);
- if (strncmp(devpath, ARUU_PATH_DEV, dev_len) == 0 && devpath[dev_len] == '/')
- devname = devpath + dev_len + 1;
- struct BlockDevInfo *curr;
- for (curr = head; curr; curr = curr->next) {
- if (strcmp(curr->name, devname) == 0) {
- strlcpy(curr->mountpoint, mnt, sizeof(curr->mountpoint));
- strlcpy(curr->fstype, fs, sizeof(curr->fstype));
- }
- struct BlockDevInfo *part;
- for (part = curr->parts; part; part = part->next) {
- if (strcmp(part->name, devname) == 0) {
- strlcpy(part->mountpoint, mnt, sizeof(part->mountpoint));
- strlcpy(part->fstype, fs, sizeof(part->fstype));
- }
- }
- }
- }
- }
- fclose(mfile);
- }
-
- return head;
-
-#elif defined(__FreeBSD__)
- char disks[1024];
- char devpath[128];
- char *tok;
- char *ptr;
- struct BlockDevInfo *head = NULL;
- struct BlockDevInfo *tail = NULL;
- struct BlockDevInfo *info;
- struct BlockDevInfo *part;
- struct BlockDevInfo *p;
- struct BlockDev dev;
- DIR *dir;
- struct dirent *de;
- size_t disk_len;
- size_t len;
- struct statfs *mntbuf;
- int mntsize;
- int i;
-
- len = sizeof(disks);
- if (sysctlbyname("kern.disks", disks, &len, NULL, 0) < 0)
- return NULL;
-
- ptr = disks;
- while ((tok = strsep(&ptr, " \t")) != NULL) {
- if (!*tok)
- continue;
-
- info = ecalloc(1, sizeof(*info));
-
- strlcpy(info->name, tok, sizeof(info->name));
- snprintf(info->path, sizeof(info->path), "%s/%s", ARUU_PATH_DEV, tok);
- strlcpy(info->type, "disk", sizeof(info->type));
-
- snprintf(devpath, sizeof(devpath), "%s/%s", ARUU_PATH_DEV, tok);
- if (blockdev_open(&dev, devpath, 0) == 0) {
- info->size = dev.size;
- blockdev_close(&dev);
- }
-
- dir = opendir(ARUU_PATH_DEV);
- if (dir) {
- disk_len = strlen(tok);
- while ((de = readdir(dir)) != NULL) {
- if (strncmp(de->d_name, tok, disk_len) == 0) {
- char c = de->d_name[disk_len];
- if (c == 's' || c == 'p') {
- part = ecalloc(1, sizeof(*part));
- strlcpy(part->name, de->d_name, sizeof(part->name));
- snprintf(part->path, sizeof(part->path), "%s/%s", ARUU_PATH_DEV, de->d_name);
- strlcpy(part->type, "part", sizeof(part->type));
- snprintf(devpath, sizeof(devpath), "%s/%s", ARUU_PATH_DEV, de->d_name);
- if (blockdev_open(&dev, devpath, 0) == 0) {
- part->size = dev.size;
- blockdev_close(&dev);
- }
- p = info->parts;
- if (!p) {
- info->parts = part;
- } else {
- while (p->next)
- p = p->next;
- p->next = part;
- }
- }
- }
- }
- closedir(dir);
- }
-
- if (!head) {
- head = info;
- } else {
- tail->next = info;
- }
- tail = info;
- }
-
- mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
- for (i = 0; i < mntsize; i++) {
- char *frompath = mntbuf[i].f_mntfromname;
- char *devname = frompath;
- size_t dev_len = strlen(ARUU_PATH_DEV);
- if (strncmp(frompath, ARUU_PATH_DEV, dev_len) == 0 && frompath[dev_len] == '/')
- devname = frompath + dev_len + 1;
- struct BlockDevInfo *curr;
- for (curr = head; curr; curr = curr->next) {
- if (strcmp(curr->name, devname) == 0) {
- strlcpy(curr->mountpoint, mntbuf[i].f_mntonname, sizeof(curr->mountpoint));
- strlcpy(curr->fstype, mntbuf[i].f_fstypename, sizeof(curr->fstype));
- }
- struct BlockDevInfo *part;
- for (part = curr->parts; part; part = part->next) {
- if (strcmp(part->name, devname) == 0) {
- strlcpy(part->mountpoint, mntbuf[i].f_mntonname, sizeof(part->mountpoint));
- strlcpy(part->fstype, mntbuf[i].f_fstypename, sizeof(part->fstype));
- }
- }
- }
- }
-
- return head;
-
-#elif defined(__OpenBSD__)
- char disknames[1024];
- char devpath[128];
- char *tok;
- char *ptr;
- char *colon;
- struct BlockDevInfo *head = NULL;
- struct BlockDevInfo *tail = NULL;
- struct BlockDevInfo *info;
- struct BlockDevInfo *part;
- struct BlockDevInfo *p;
- struct BlockDev dev;
- size_t len;
- int mib[2];
- char c;
- struct statfs *mntbuf;
- int mntsize;
- int i;
-
- mib[0] = CTL_HW;
- mib[1] = HW_DISKNAMES;
- len = sizeof(disknames);
- if (sysctl(mib, 2, disknames, &len, NULL, 0) < 0)
- return NULL;
-
- ptr = disknames;
- while ((tok = strsep(&ptr, ",")) != NULL) {
- colon = strchr(tok, ':');
- if (colon)
- *colon = '\0';
- if (!*tok)
- continue;
-
- info = ecalloc(1, sizeof(*info));
-
- strlcpy(info->name, tok, sizeof(info->name));
- snprintf(info->path, sizeof(info->path), "%s/r%sc", ARUU_PATH_DEV, tok);
- strlcpy(info->type, "disk", sizeof(info->type));
-
- snprintf(devpath, sizeof(devpath), "%s/r%sc", ARUU_PATH_DEV, tok);
- if (blockdev_open(&dev, devpath, 0) == 0) {
- info->size = dev.size;
- blockdev_close(&dev);
- }
-
- for (c = 'a'; c <= 'p'; c++) {
- if (c == 'c')
- continue;
- snprintf(devpath, sizeof(devpath), "%s/r%s%c", ARUU_PATH_DEV, tok, c);
- if (blockdev_open(&dev, devpath, 0) == 0) {
- part = ecalloc(1, sizeof(*part));
- snprintf(part->name, sizeof(part->name), "%s%c", tok, c);
- snprintf(part->path, sizeof(part->path), "%s/r%s%c", ARUU_PATH_DEV, tok, c);
- strlcpy(part->type, "part", sizeof(part->type));
- part->size = dev.size;
- p = info->parts;
- if (!p) {
- info->parts = part;
- } else {
- while (p->next)
- p = p->next;
- p->next = part;
- }
- blockdev_close(&dev);
- }
- }
-
- if (!head) {
- head = info;
- } else {
- tail->next = info;
- }
- tail = info;
- }
-
- mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
- for (i = 0; i < mntsize; i++) {
- char *frompath = mntbuf[i].f_mntfromname;
- char *devname = frompath;
- size_t dev_len = strlen(ARUU_PATH_DEV);
- if (strncmp(frompath, ARUU_PATH_DEV, dev_len) == 0 && frompath[dev_len] == '/')
- devname = frompath + dev_len + 1;
- struct BlockDevInfo *curr;
- for (curr = head; curr; curr = curr->next) {
- if (strcmp(curr->name, devname) == 0) {
- strlcpy(curr->mountpoint, mntbuf[i].f_mntonname, sizeof(curr->mountpoint));
- strlcpy(curr->fstype, mntbuf[i].f_fstypename, sizeof(curr->fstype));
- }
- struct BlockDevInfo *part;
- for (part = curr->parts; part; part = part->next) {
- if (strcmp(part->name, devname) == 0) {
- strlcpy(part->mountpoint, mntbuf[i].f_mntonname, sizeof(part->mountpoint));
- strlcpy(part->fstype, mntbuf[i].f_fstypename, sizeof(part->fstype));
- }
- }
- }
- }
-
- return head;
-
-#else
struct BlockDevInfo *head = NULL;
struct BlockDevInfo *tail = NULL;
struct BlockDevInfo *info;
@@ -492,7 +114,6 @@ blockdev_get_list(void)
}
}
return head;
-#endif
}
void
diff --git a/shared/libutil/diskutil_freebsd.c b/shared/libutil/diskutil_freebsd.c
@@ -0,0 +1,140 @@
+/* see LICENSE file for copyright and license details */
+#include "../diskutil.h"
+#include "../util.h"
+
+#if defined(__FreeBSD__) && !defined(ARUU_FORCE_OTHER)
+#include <dirent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/disk.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/types.h>
+
+void
+blockdev_sys_size(struct BlockDev *dev)
+{
+ off_t size_bytes = 0;
+ unsigned int sec_size = 0;
+
+ if (ioctl(dev->fd, DIOCGMEDIASIZE, &size_bytes) >= 0)
+ dev->size = size_bytes;
+ if (ioctl(dev->fd, DIOCGSECTORSIZE, &sec_size) >= 0)
+ dev->sec_size = sec_size;
+}
+
+void
+blockdev_sys_reread(struct BlockDev *dev)
+{
+ (void)dev;
+}
+
+struct BlockDevInfo *
+blockdev_get_list(void)
+{
+ char disks[1024];
+ char devpath[128];
+ char *tok;
+ char *ptr;
+ struct BlockDevInfo *head = NULL;
+ struct BlockDevInfo *tail = NULL;
+ struct BlockDevInfo *info;
+ struct BlockDevInfo *part;
+ struct BlockDevInfo *p;
+ struct BlockDev dev;
+ DIR *dir;
+ struct dirent *de;
+ size_t disk_len;
+ size_t len;
+ struct statfs *mntbuf;
+ int mntsize;
+ int i;
+
+ len = sizeof(disks);
+ if (sysctlbyname("kern.disks", disks, &len, NULL, 0) < 0)
+ return NULL;
+
+ ptr = disks;
+ while ((tok = strsep(&ptr, " \t")) != NULL) {
+ if (!*tok)
+ continue;
+
+ info = ecalloc(1, sizeof(*info));
+
+ strlcpy(info->name, tok, sizeof(info->name));
+ snprintf(info->path, sizeof(info->path), "%s/%s", ARUU_PATH_DEV, tok);
+ strlcpy(info->type, "disk", sizeof(info->type));
+
+ snprintf(devpath, sizeof(devpath), "%s/%s", ARUU_PATH_DEV, tok);
+ if (blockdev_open(&dev, devpath, 0) == 0) {
+ info->size = dev.size;
+ blockdev_close(&dev);
+ }
+
+ dir = opendir(ARUU_PATH_DEV);
+ if (dir) {
+ disk_len = strlen(tok);
+ while ((de = readdir(dir)) != NULL) {
+ if (strncmp(de->d_name, tok, disk_len) == 0) {
+ char c = de->d_name[disk_len];
+ if (c == 's' || c == 'p') {
+ part = ecalloc(1, sizeof(*part));
+ strlcpy(part->name, de->d_name, sizeof(part->name));
+ snprintf(part->path, sizeof(part->path), "%s/%s", ARUU_PATH_DEV, de->d_name);
+ strlcpy(part->type, "part", sizeof(part->type));
+ snprintf(devpath, sizeof(devpath), "%s/%s", ARUU_PATH_DEV, de->d_name);
+ if (blockdev_open(&dev, devpath, 0) == 0) {
+ part->size = dev.size;
+ blockdev_close(&dev);
+ }
+ p = info->parts;
+ if (!p) {
+ info->parts = part;
+ } else {
+ while (p->next)
+ p = p->next;
+ p->next = part;
+ }
+ }
+ }
+ }
+ closedir(dir);
+ }
+
+ if (!head) {
+ head = info;
+ } else {
+ tail->next = info;
+ }
+ tail = info;
+ }
+
+ mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+ for (i = 0; i < mntsize; i++) {
+ char *frompath = mntbuf[i].f_mntfromname;
+ char *devname = frompath;
+ size_t dev_len = strlen(ARUU_PATH_DEV);
+ if (strncmp(frompath, ARUU_PATH_DEV, dev_len) == 0 && frompath[dev_len] == '/')
+ devname = frompath + dev_len + 1;
+ struct BlockDevInfo *curr;
+ for (curr = head; curr; curr = curr->next) {
+ if (strcmp(curr->name, devname) == 0) {
+ strlcpy(curr->mountpoint, mntbuf[i].f_mntonname, sizeof(curr->mountpoint));
+ strlcpy(curr->fstype, mntbuf[i].f_fstypename, sizeof(curr->fstype));
+ }
+ struct BlockDevInfo *part;
+ for (part = curr->parts; part; part = part->next) {
+ if (strcmp(part->name, devname) == 0) {
+ strlcpy(part->mountpoint, mntbuf[i].f_mntonname, sizeof(part->mountpoint));
+ strlcpy(part->fstype, mntbuf[i].f_fstypename, sizeof(part->fstype));
+ }
+ }
+ }
+ }
+
+ return head;
+}
+#endif
diff --git a/shared/libutil/diskutil_linux.c b/shared/libutil/diskutil_linux.c
@@ -0,0 +1,166 @@
+/* see LICENSE file for copyright and license details */
+#include "../diskutil.h"
+#include "../util.h"
+
+#if defined(__linux__) && !defined(ARUU_FORCE_OTHER)
+#include <linux/fs.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+
+void
+blockdev_sys_size(struct BlockDev *dev)
+{
+ uint64_t size_bytes = 0;
+ int sec_size = 0;
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Woverflow"
+ if (ioctl(dev->fd, BLKGETSIZE64, &size_bytes) >= 0)
+ dev->size = size_bytes;
+ if (ioctl(dev->fd, BLKSSZGET, &sec_size) >= 0)
+ dev->sec_size = sec_size;
+#pragma GCC diagnostic pop
+}
+
+void
+blockdev_sys_reread(struct BlockDev *dev)
+{
+ struct stat st;
+
+ // the kernel keeps the partition table it read when the disk appeared:
+ // a table written here has no device nodes until it is asked to read
+ // again, which fails harmlessly while a partition is in use
+ if (fstat(dev->fd, &st) == 0 && S_ISBLK(st.st_mode))
+ ioctl(dev->fd, BLKRRPART);
+}
+
+/* the model the disk reports, without the blanks firmware pads it with */
+static void
+disk_model(const char *name, char *out, size_t n)
+{
+ char path[128];
+ FILE *f;
+ size_t len;
+
+ out[0] = '\0';
+ snprintf(path, sizeof(path), "/sys/block/%s/device/model", name);
+ if (!(f = fopen(path, "r")))
+ return;
+ if (fgets(out, (int)n, f)) {
+ out[strcspn(out, "\n")] = '\0';
+ for (len = strlen(out); len > 0 && out[len - 1] == ' '; len--)
+ out[len - 1] = '\0';
+ }
+ fclose(f);
+}
+
+struct BlockDevInfo *
+blockdev_get_list(void)
+{
+ FILE *fp;
+ FILE *mfile;
+ char line[256];
+ char mline[512];
+ char devpath[128];
+ char mnt[256];
+ char fs[64];
+ struct BlockDevInfo *head = NULL;
+ struct BlockDevInfo *tail = NULL;
+ struct BlockDevInfo *info;
+ struct BlockDevInfo *curr;
+ unsigned int major, minor;
+ unsigned long long blocks;
+ char name[64];
+ int is_part;
+
+ fp = fopen(ARUU_LINUX_PATH_PROC_PARTITIONS, "r");
+ if (!fp)
+ return NULL;
+
+ while (fgets(line, sizeof(line), fp)) {
+ if (sscanf(line, " %u %u %llu %63s", &major, &minor, &blocks, name) != 4)
+ continue;
+
+ info = ecalloc(1, sizeof(*info));
+
+ strlcpy(info->name, name, sizeof(info->name));
+ snprintf(info->path, sizeof(info->path), "%s/%s", ARUU_PATH_DEV, name);
+ info->size = blocks * 1024;
+ info->major = major;
+ info->minor = minor;
+
+ /* search tree topology to link partition node under proper
+ * target parent disk */
+ is_part = 0;
+ for (curr = head; curr; curr = curr->next) {
+ size_t len = strlen(curr->name);
+ if (strncmp(name, curr->name, len) == 0) {
+ char c = name[len];
+ if (curr->name[len - 1] >= '0' && curr->name[len - 1] <= '9') {
+ if (c == 'p' && name[len + 1] >= '0' && name[len + 1] <= '9')
+ is_part = 1;
+ } else {
+ if (c >= '0' && c <= '9')
+ is_part = 1;
+ }
+ if (is_part) {
+ strlcpy(info->type, "part", sizeof(info->type));
+ struct BlockDevInfo *p = curr->parts;
+ if (!p) {
+ curr->parts = info;
+ } else {
+ while (p->next)
+ p = p->next;
+ p->next = info;
+ }
+ break;
+ }
+ }
+ }
+
+ if (!is_part) {
+ strlcpy(info->type, "disk", sizeof(info->type));
+ disk_model(info->name, info->model, sizeof(info->model));
+ if (!head) {
+ head = info;
+ } else {
+ tail->next = info;
+ }
+ tail = info;
+ }
+ }
+ fclose(fp);
+
+ mfile = fopen(ARUU_LINUX_PATH_PROC_MOUNTS, "r");
+ if (mfile) {
+ while (fgets(mline, sizeof(mline), mfile)) {
+ if (sscanf(mline, "%127s %255s %63s", devpath, mnt, fs) == 3) {
+ char *devname = devpath;
+ size_t dev_len = strlen(ARUU_PATH_DEV);
+ if (strncmp(devpath, ARUU_PATH_DEV, dev_len) == 0 && devpath[dev_len] == '/')
+ devname = devpath + dev_len + 1;
+ struct BlockDevInfo *curr;
+ for (curr = head; curr; curr = curr->next) {
+ if (strcmp(curr->name, devname) == 0) {
+ strlcpy(curr->mountpoint, mnt, sizeof(curr->mountpoint));
+ strlcpy(curr->fstype, fs, sizeof(curr->fstype));
+ }
+ struct BlockDevInfo *part;
+ for (part = curr->parts; part; part = part->next) {
+ if (strcmp(part->name, devname) == 0) {
+ strlcpy(part->mountpoint, mnt, sizeof(part->mountpoint));
+ strlcpy(part->fstype, fs, sizeof(part->fstype));
+ }
+ }
+ }
+ }
+ }
+ fclose(mfile);
+ }
+
+ return head;
+}
+#endif
diff --git a/shared/libutil/diskutil_netbsd.c b/shared/libutil/diskutil_netbsd.c
@@ -0,0 +1,32 @@
+/* see LICENSE file for copyright and license details */
+#include "../diskutil.h"
+
+#if defined(__NetBSD__) && !defined(ARUU_FORCE_OTHER)
+#include <sys/types.h>
+#include <sys/disklabel.h>
+#include <sys/dkio.h>
+#include <sys/ioctl.h>
+
+void
+blockdev_sys_size(struct BlockDev *dev)
+{
+ struct disklabel dl;
+
+ if (ioctl(dev->fd, DIOCGDINFO, &dl) >= 0) {
+ dev->size = (uint64_t)dl.d_secsize * DL_GETDSIZE(&dl);
+ dev->sec_size = dl.d_secsize;
+ }
+}
+
+void
+blockdev_sys_reread(struct BlockDev *dev)
+{
+ (void)dev;
+}
+
+struct BlockDevInfo *
+blockdev_get_list(void)
+{
+ return blockdev_probe_list();
+}
+#endif
diff --git a/shared/libutil/diskutil_openbsd.c b/shared/libutil/diskutil_openbsd.c
@@ -0,0 +1,136 @@
+/* see LICENSE file for copyright and license details */
+#include "../diskutil.h"
+#include "../util.h"
+
+#if defined(__OpenBSD__) && !defined(ARUU_FORCE_OTHER)
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/disklabel.h>
+#include <sys/dkio.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+
+void
+blockdev_sys_size(struct BlockDev *dev)
+{
+ struct disklabel dl;
+
+ if (ioctl(dev->fd, DIOCGDINFO, &dl) >= 0) {
+ dev->size = (uint64_t)dl.d_secsize * DL_GETDSIZE(&dl);
+ dev->sec_size = dl.d_secsize;
+ }
+}
+
+void
+blockdev_sys_reread(struct BlockDev *dev)
+{
+ (void)dev;
+}
+
+struct BlockDevInfo *
+blockdev_get_list(void)
+{
+ char disknames[1024];
+ char devpath[128];
+ char *tok;
+ char *ptr;
+ char *colon;
+ struct BlockDevInfo *head = NULL;
+ struct BlockDevInfo *tail = NULL;
+ struct BlockDevInfo *info;
+ struct BlockDevInfo *part;
+ struct BlockDevInfo *p;
+ struct BlockDev dev;
+ size_t len;
+ int mib[2];
+ char c;
+ struct statfs *mntbuf;
+ int mntsize;
+ int i;
+
+ mib[0] = CTL_HW;
+ mib[1] = HW_DISKNAMES;
+ len = sizeof(disknames);
+ if (sysctl(mib, 2, disknames, &len, NULL, 0) < 0)
+ return NULL;
+
+ ptr = disknames;
+ while ((tok = strsep(&ptr, ",")) != NULL) {
+ colon = strchr(tok, ':');
+ if (colon)
+ *colon = '\0';
+ if (!*tok)
+ continue;
+
+ info = ecalloc(1, sizeof(*info));
+
+ strlcpy(info->name, tok, sizeof(info->name));
+ snprintf(info->path, sizeof(info->path), "%s/r%sc", ARUU_PATH_DEV, tok);
+ strlcpy(info->type, "disk", sizeof(info->type));
+
+ snprintf(devpath, sizeof(devpath), "%s/r%sc", ARUU_PATH_DEV, tok);
+ if (blockdev_open(&dev, devpath, 0) == 0) {
+ info->size = dev.size;
+ blockdev_close(&dev);
+ }
+
+ for (c = 'a'; c <= 'p'; c++) {
+ if (c == 'c')
+ continue;
+ snprintf(devpath, sizeof(devpath), "%s/r%s%c", ARUU_PATH_DEV, tok, c);
+ if (blockdev_open(&dev, devpath, 0) == 0) {
+ part = ecalloc(1, sizeof(*part));
+ snprintf(part->name, sizeof(part->name), "%s%c", tok, c);
+ snprintf(part->path, sizeof(part->path), "%s/r%s%c", ARUU_PATH_DEV, tok, c);
+ strlcpy(part->type, "part", sizeof(part->type));
+ part->size = dev.size;
+ p = info->parts;
+ if (!p) {
+ info->parts = part;
+ } else {
+ while (p->next)
+ p = p->next;
+ p->next = part;
+ }
+ blockdev_close(&dev);
+ }
+ }
+
+ if (!head) {
+ head = info;
+ } else {
+ tail->next = info;
+ }
+ tail = info;
+ }
+
+ mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+ for (i = 0; i < mntsize; i++) {
+ char *frompath = mntbuf[i].f_mntfromname;
+ char *devname = frompath;
+ size_t dev_len = strlen(ARUU_PATH_DEV);
+ if (strncmp(frompath, ARUU_PATH_DEV, dev_len) == 0 && frompath[dev_len] == '/')
+ devname = frompath + dev_len + 1;
+ struct BlockDevInfo *curr;
+ for (curr = head; curr; curr = curr->next) {
+ if (strcmp(curr->name, devname) == 0) {
+ strlcpy(curr->mountpoint, mntbuf[i].f_mntonname, sizeof(curr->mountpoint));
+ strlcpy(curr->fstype, mntbuf[i].f_fstypename, sizeof(curr->fstype));
+ }
+ struct BlockDevInfo *part;
+ for (part = curr->parts; part; part = part->next) {
+ if (strcmp(part->name, devname) == 0) {
+ strlcpy(part->mountpoint, mntbuf[i].f_mntonname, sizeof(part->mountpoint));
+ strlcpy(part->fstype, mntbuf[i].f_fstypename, sizeof(part->fstype));
+ }
+ }
+ }
+ }
+
+ return head;
+}
+#endif
diff --git a/shared/libutil/diskutil_other.c b/shared/libutil/diskutil_other.c
@@ -0,0 +1,25 @@
+/* see LICENSE file for copyright and license details */
+#include "../diskutil.h"
+
+/* a kernel with no backend: a device's size is what fstat says, and the
+ * list of disks is what opens under the names most systems give them */
+#if !(defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)) \
+ || defined(ARUU_FORCE_OTHER)
+void
+blockdev_sys_size(struct BlockDev *dev)
+{
+ (void)dev;
+}
+
+void
+blockdev_sys_reread(struct BlockDev *dev)
+{
+ (void)dev;
+}
+
+struct BlockDevInfo *
+blockdev_get_list(void)
+{
+ return blockdev_probe_list();
+}
+#endif