Files
mcde/lib/McdeSys/disk_netbsd.c
T
lotherkandClaude Opus 5.5 cfc5c3f22c mcde: McdeSys knows the disks, and the permissions the system should have
mcde_sys_disks lists the drives, each with its partitions: size, model,
file system, label, removable, the system's (/, /usr, /var, /home, swap),
where mounted and how full.  NetBSD: hw.disknames, dkctl getgeometry and
listwedges, atactl identify, getmntinfo, swapctl -l; Linux: lsblk -P -b.
mcde_sys_disk_mount (/media/LABEL; Linux through udisksctl),
_unmount, _check (fsck -n) and _erase (root; a drive with a system
partition or a mounted one is refused): NetBSD gpt destroy/create/add,
dkctl makewedges, newfs -O 2 or newfs_msdos -F 32; Linux sfdisk and
mkfs.ext4/vfat.  docs/reference/disks.txt.

mcde_sys_permissions compares owner, group and mode of the system's files
with the specification sysinst keeps for every installed set,
/etc/mtree/set.* (set.makronix too), and repairs them as root (mtree -e
-U).  mtree -k did not stop it from comparing sizes and checksums, so the
specification is narrowed to type, uname, gname, mode and link first.
Linux keeps no such list: not supported there.
docs/reference/permissions.txt.

Measured: on the Linux host the NVMe drive and its three partitions, all
the system's; erasing as a user refused.  In the build VM as root,
listing only: wd0 (QEMU HARDDISK, 68.7 GB) with dk0 on / and dk1 swap, cd0;
verifying the permissions reported 207 differences, all under /usr/pkg,
which that VM built itself instead of unpacking the set.  Nothing was
mounted, erased or repaired.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:12:14 +00:00

486 lines
14 KiB
C

/*
* disk_netbsd.c -- MCDE: the disks of a NetBSD system (McdeSys)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* NetBSD's own tools and calls (docs/reference/disks.txt in the Makronix
* tree): the drives from sysctl hw.disknames, their size from dkctl(8)
* getgeometry, their partitions -- wedges, made from GPT or MBR by the
* kernel -- from dkctl listwedges, a drive's model from atactl(8)
* identify; what is mounted from getmntinfo(3), the swap from swapctl(8)
* -l. A drive that holds /, /usr, /var, /home or swap is the system's and
* is never erased. Erasing is gpt(8) destroy, create and add, dkctl
* makewedges, then newfs(8) -O 2 or newfs_msdos(8) -F 32.
*/
#define _NETBSD_SOURCE
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/sysctl.h>
#include <Mcde/McdeSys.h>
#include "private.h"
static char dkctl[] = "/sbin/dkctl";
static char gptp[] = "/sbin/gpt";
static unsigned long long
Number(const char *s)
{
return strtoull(s, NULL, 10);
}
/* "/dev/rwd0: 68719476736 bytes in 134217728 blocks of 512 bytes" */
static void
Geometry(const char *name, unsigned long long *bytes, unsigned long *bsize)
{
char out[512], *p;
char *argv[] = { dkctl, (char *) name, "getgeometry", NULL };
*bytes = 0;
*bsize = 512;
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK ||
(p = strchr(out, ':')) == NULL)
return;
*bytes = Number(p + 1);
if ((p = strstr(out, "blocks of ")) != NULL)
*bsize = (unsigned long) Number(p + 10);
}
static void
Model(const char *name, char *buf, size_t len)
{
char out[2048], *p, *e;
char *argv[] = { "/sbin/atactl", (char *) name, "identify", NULL };
buf[0] = '\0';
if (strncmp(name, "cd", 2) == 0) {
snprintf(buf, len, "CD/DVD drive");
return;
}
if (strncmp(name, "wd", 2) != 0 ||
mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK ||
(p = strstr(out, "Model: ")) == NULL)
return;
p += 7;
if ((e = strstr(p, ", Rev")) == NULL)
e = strchr(p, '\n');
snprintf(buf, len, "%.*s", (int) (e != NULL ? e - p : (long) strlen(p)),
p);
}
/* where every device is mounted, and the swap */
static void
MountOf(const char *name, McdeSysDisk *d)
{
struct statvfs *mnt;
char dev[64];
int n, i;
snprintf(dev, sizeof dev, "/dev/%s", name);
n = getmntinfo(&mnt, MNT_NOWAIT);
for (i = 0; i < n; i++)
if (strcmp(mnt[i].f_mntfromname, dev) == 0) {
snprintf(d->mountpoint, sizeof d->mountpoint, "%s",
mnt[i].f_mntonname);
d->used = (unsigned long long) (mnt[i].f_blocks -
mnt[i].f_bfree) * mnt[i].f_frsize;
d->avail = (unsigned long long) mnt[i].f_bavail *
mnt[i].f_frsize;
if (strcmp(d->mountpoint, "/") == 0 ||
strcmp(d->mountpoint, "/usr") == 0 ||
strcmp(d->mountpoint, "/var") == 0 ||
strcmp(d->mountpoint, "/home") == 0)
d->system = 1;
return;
}
}
static int
IsSwap(const char *name)
{
char out[2048], dev[64];
char *argv[] = { "/sbin/swapctl", "-l", NULL };
snprintf(dev, sizeof dev, "/dev/%s ", name);
return mcde_sys_capture(argv, out, sizeof out) == MCDE_SYS_OK &&
strstr(out, dev) != NULL;
}
int
mcde_sys_disks(McdeSysDisk *d, size_t max)
{
char names[1024], *tok, *last = NULL;
size_t len = sizeof names, n = 0;
if (sysctlbyname("hw.disknames", names, &len, NULL, 0) != 0)
return mcde_sys_fail(MCDE_SYS_EFAIL, "No list of disks "
"(hw.disknames).");
for (tok = strtok_r(names, " ", &last); tok != NULL && n < max;
tok = strtok_r(NULL, " ", &last)) {
McdeSysDisk *drive = &d[n];
unsigned long bsize;
char out[4096], *line, *l2 = NULL;
char *argv[] = { dkctl, tok, "listwedges", NULL };
size_t first;
/* the wedges come with their drive; floppies are left out */
if (strncmp(tok, "dk", 2) == 0 || strncmp(tok, "fd", 2) == 0)
continue;
memset(drive, 0, sizeof *drive);
snprintf(drive->name, sizeof drive->name, "%s", tok);
Geometry(tok, &drive->bytes, &bsize);
Model(tok, drive->model, sizeof drive->model);
drive->removable = strncmp(tok, "sd", 2) == 0 ||
strncmp(tok, "cd", 2) == 0;
if (strncmp(tok, "cd", 2) == 0 && drive->bytes > 0)
snprintf(drive->fstype, sizeof drive->fstype, "cd9660");
first = ++n;
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
continue;
/* "dk0: LABEL, 113246080 blocks at 64, type: ffs" */
for (line = strtok_r(out, "\n", &l2); line != NULL && n < max;
line = strtok_r(NULL, "\n", &l2)) {
McdeSysDisk *p = &d[n];
char *colon = strchr(line, ':'), *comma, *type;
if (strncmp(line, "dk", 2) != 0 || colon == NULL ||
(comma = strrchr(line, ',')) == NULL)
continue;
memset(p, 0, sizeof *p);
snprintf(p->name, sizeof p->name, "%.*s",
(int) (colon - line), line);
snprintf(p->parent, sizeof p->parent, "%s", tok);
{
/* the label: up to the comma before the count */
char *c2 = strstr(colon, " blocks at ");
char *lab_end = c2;
while (lab_end != NULL && lab_end > colon &&
*lab_end != ',')
lab_end--;
if (lab_end != NULL && lab_end > colon + 2)
snprintf(p->label, sizeof p->label,
"%.*s", (int) (lab_end - colon - 2),
colon + 2);
if (c2 != NULL) {
char *b = c2;
while (b > colon && isdigit((unsigned
char) b[-1]))
b--;
p->bytes = Number(b) * bsize;
}
}
if ((type = strstr(comma, "type: ")) != NULL)
snprintf(p->fstype, sizeof p->fstype, "%s",
type + 6);
/* a GPT label of only a UUID says nothing */
if (strlen(p->label) == 36 && p->label[8] == '-')
p->label[0] = '\0';
MountOf(p->name, p);
if (strcmp(p->fstype, "swap") == 0 && IsSwap(p->name))
p->system = 1;
if (p->system)
drive->system = 1;
n++;
}
(void) first;
MountOf(tok, drive); /* a CD, or a disk without wedges */
}
return (int) n;
}
static int
Find(const char *name, McdeSysDisk *out)
{
static McdeSysDisk all[64];
int n = mcde_sys_disks(all, 64), i;
for (i = 0; i < n; i++)
if (strcmp(all[i].name, name) == 0) {
*out = all[i];
return 1;
}
return 0;
}
int
mcde_sys_disk_mount(const char *name, char *mountpoint, size_t len)
{
McdeSysDisk d;
char dev[64], mp[256], prog[64];
const char *base;
char *argv[5];
size_t i;
if (!Find(name, &d))
return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no disk %s.",
name);
if (d.mountpoint[0] != '\0') {
snprintf(mountpoint, len, "%s", d.mountpoint);
return MCDE_SYS_OK;
}
if (strcmp(d.fstype, "ffs") != 0 && strcmp(d.fstype, "msdos") != 0 &&
strcmp(d.fstype, "ext2fs") != 0 && strcmp(d.fstype, "cd9660") != 0 &&
strcmp(d.fstype, "ntfs") != 0)
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "A %s partition cannot "
"be mounted.", d.fstype[0] != '\0' ? d.fstype : "blank");
snprintf(prog, sizeof prog, "/sbin/mount_%s", d.fstype);
snprintf(dev, sizeof dev, strcmp(d.fstype, "cd9660") == 0 ?
"/dev/%sa" : "/dev/%s", name);
/* /media/LABEL, or /media/NAME: letters, digits and - only */
base = d.label[0] != '\0' ? d.label : name;
snprintf(mp, sizeof mp, "/media/");
for (i = 0; base[i] != '\0' && strlen(mp) + 2 < sizeof mp; i++) {
size_t l = strlen(mp);
mp[l] = isalnum((unsigned char) base[i]) ? base[i] : '-';
mp[l + 1] = '\0';
}
(void) mkdir("/media", 0755);
(void) mkdir(mp, 0755);
argv[0] = prog;
argv[1] = dev;
argv[2] = mp;
argv[3] = NULL;
if (mcde_sys_run(argv) != MCDE_SYS_OK)
return MCDE_SYS_EFAIL;
snprintf(mountpoint, len, "%s", mp);
return MCDE_SYS_OK;
}
int
mcde_sys_disk_unmount(const char *name)
{
McdeSysDisk d;
char *argv[3];
if (!Find(name, &d) || d.mountpoint[0] == '\0')
return mcde_sys_fail(MCDE_SYS_ENOENT, "%s is not mounted.",
name);
if (d.system)
return mcde_sys_fail(MCDE_SYS_EPERM, "%s is the system's.",
name);
argv[0] = "/sbin/umount";
argv[1] = d.mountpoint;
argv[2] = NULL;
return mcde_sys_run(argv);
}
int
mcde_sys_disk_check(const char *name, char *log, size_t len)
{
McdeSysDisk d;
char prog[64], dev[64];
char *argv[4];
log[0] = '\0';
if (!Find(name, &d))
return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no disk %s.",
name);
if (strcmp(d.fstype, "ffs") != 0 && strcmp(d.fstype, "msdos") != 0 &&
strcmp(d.fstype, "ext2fs") != 0)
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "A %s partition cannot "
"be checked.", d.fstype[0] != '\0' ? d.fstype : "blank");
snprintf(prog, sizeof prog, "/sbin/fsck_%s", d.fstype);
snprintf(dev, sizeof dev, "/dev/r%s", name);
argv[0] = prog;
argv[1] = "-n"; /* look, change nothing (fsck_ffs(8)) */
argv[2] = dev;
argv[3] = NULL;
return mcde_sys_capture(argv, log, len);
}
int
mcde_sys_disk_erase(const char *name, const char *fstype, const char *label,
char *part, size_t len)
{
static McdeSysDisk all[64];
char out[2048], rdev[64], *p;
const char *gtype;
int n, i;
if (geteuid() != 0)
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
"can erase a disk.");
if (strcmp(fstype, "ffs") == 0)
gtype = "ffs";
else if (strcmp(fstype, "msdos") == 0)
gtype = "windows";
else
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "No %s here.", fstype);
if (label == NULL || label[0] == '\0' || strlen(label) > 32 ||
strspn(label, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789 _-") != strlen(label))
return mcde_sys_fail(MCDE_SYS_EINVAL, "A name has letters, "
"digits, blanks, - and _, at most 32.");
n = mcde_sys_disks(all, 64);
for (i = 0; i < n; i++)
if (strcmp(all[i].name, name) == 0)
break;
if (i == n || all[i].parent[0] != '\0')
return mcde_sys_fail(MCDE_SYS_EINVAL, "%s is no drive.", name);
if (all[i].system)
return mcde_sys_fail(MCDE_SYS_EPERM, "%s holds the system and "
"is not erased.", name);
for (i = 0; i < n; i++)
if ((strcmp(all[i].name, name) == 0 ||
strcmp(all[i].parent, name) == 0) &&
all[i].mountpoint[0] != '\0')
return mcde_sys_fail(MCDE_SYS_EPERM, "%s is mounted on "
"%s: unmount it first.", all[i].name,
all[i].mountpoint);
{
char *destroy[] = { gptp, "destroy", (char *) name, NULL };
char *create[] = { gptp, "create", "-f", (char *) name, NULL };
char *add[] = { gptp, "add", "-t", (char *) gtype, "-l",
(char *) label, (char *) name, NULL };
char *make[] = { dkctl, (char *) name, "makewedges", NULL };
char *list[] = { dkctl, (char *) name, "listwedges", NULL };
(void) mcde_sys_run(destroy); /* none there: that is fine */
if (mcde_sys_run(create) != MCDE_SYS_OK ||
mcde_sys_run(add) != MCDE_SYS_OK ||
mcde_sys_run(make) != MCDE_SYS_OK ||
mcde_sys_capture(list, out, sizeof out) != MCDE_SYS_OK)
return MCDE_SYS_EFAIL;
}
if ((p = strstr(out, "\ndk")) == NULL)
return mcde_sys_fail(MCDE_SYS_EFAIL, "The new partition did "
"not appear.");
p++;
snprintf(part, len, "%.*s", (int) strcspn(p, ":"), p);
snprintf(rdev, sizeof rdev, "/dev/r%s", part);
if (strcmp(fstype, "ffs") == 0) {
char *newfs[] = { "/sbin/newfs", "-O", "2", rdev, NULL };
return mcde_sys_run(newfs);
} else {
char *newfs[] = { "/sbin/newfs_msdos", "-F", "32", "-L",
(char *) label, rdev, NULL };
return mcde_sys_run(newfs);
}
}
/* ---- the permissions: mtree(8) against the specification sysinst left
* for every installed set, /etc/mtree/set.NAME -- files not in the set left
* alone (-e), -U to set them back (docs/reference/permissions.txt). Only
* owner, group and mode count: -k did not keep mtree from comparing the
* sizes and checksums the specification names (every changed file in /etc
* and every log was reported, measured), so the specification is copied
* with only type, uname, gname, mode and link. */
#include <dirent.h>
static int
Narrow(const char *from, const char *to)
{
FILE *in = fopen(from, "r"), *out;
char line[4096];
if (in == NULL)
return 0;
if ((out = fopen(to, "w")) == NULL) {
fclose(in);
return 0;
}
while (fgets(line, sizeof line, in) != NULL) {
char *tok, *last = NULL;
int first = 1;
line[strcspn(line, "\n")] = '\0';
if (line[0] == '#' || line[0] == '\0') {
fprintf(out, "%s\n", line);
continue;
}
for (tok = strtok_r(line, " \t", &last); tok != NULL;
tok = strtok_r(NULL, " \t", &last)) {
/* the path (or /set, ..) and the keywords that count */
if (first || strncmp(tok, "type=", 5) == 0 ||
strncmp(tok, "uname=", 6) == 0 ||
strncmp(tok, "gname=", 6) == 0 ||
strncmp(tok, "mode=", 5) == 0 ||
strncmp(tok, "link=", 5) == 0)
fprintf(out, "%s%s", first ? "" : " ", tok);
first = 0;
}
fputc('\n', out);
}
fclose(in);
return fclose(out) == 0;
}
int
mcde_sys_permissions(int repair, char *log, size_t len)
{
DIR *dir = opendir("/etc/mtree");
struct dirent *de;
static char out[65536];
size_t used = 0;
int lines = 0;
log[0] = '\0';
if (dir == NULL)
return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no /etc/mtree.");
if (repair && geteuid() != 0) {
closedir(dir);
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
"can repair permissions.");
}
while ((de = readdir(dir)) != NULL) {
char full[300], spec[64], *line, *last = NULL;
char *argv[10];
int k = 0, fd;
if (strncmp(de->d_name, "set.", 4) != 0)
continue;
snprintf(full, sizeof full, "/etc/mtree/%s", de->d_name);
snprintf(spec, sizeof spec, "/tmp/mcde-mtree.XXXXXX");
if ((fd = mkstemp(spec)) < 0)
continue;
close(fd);
if (!Narrow(full, spec)) {
unlink(spec);
continue;
}
argv[k++] = "/usr/sbin/mtree";
argv[k++] = "-e";
if (repair)
argv[k++] = "-U";
argv[k++] = "-f";
argv[k++] = spec;
argv[k++] = "-p";
argv[k++] = "/";
argv[k] = NULL;
/* mtree ends with a non-zero status when something differs:
* its report is what is wanted either way */
(void) mcde_sys_capture(argv, out, sizeof out);
unlink(spec);
for (line = strtok_r(out, "\n", &last); line != NULL;
line = strtok_r(NULL, "\n", &last)) {
/* the report's own lines that name a file, indented
* lines tell what differed */
if (line[0] == '\0')
continue;
if (used + strlen(line) + 2 < len) {
used += (size_t) snprintf(log + used,
len - used, "%s\n", line);
}
if (line[0] != '\t' && line[0] != ' ')
lines++;
}
}
closedir(dir);
return lines;
}