Files
mcde/lib/McdeSys/disk_linux.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

275 lines
7.9 KiB
C

/*
* disk_linux.c -- MCDE: the disks of a Linux system (McdeSys)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* lsblk(8) -P -b (KEY="value" pairs, sizes in bytes) for the drives and
* partitions; udisksctl(1) mounts and unmounts where it is there (as the
* user, with the system's permission), else mount(8); fsck(8) -n looks;
* erasing is sfdisk(8) (one GPT partition) and mkfs.ext4 or mkfs.vfat.
* Loop devices, LVM and crypto layers are not drives of their own here.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include <Mcde/McdeSys.h>
#include "private.h"
static const char *const lsblkPaths[] = { "/usr/bin/lsblk", "/bin/lsblk",
NULL };
static const char *const udisksPaths[] = { "/usr/bin/udisksctl", NULL };
/* the value of KEY="..." in LINE */
static void
Pair(const char *line, const char *key, char *buf, size_t len)
{
char pat[32];
const char *p, *e;
buf[0] = '\0';
snprintf(pat, sizeof pat, " %s=\"", key);
if (strncmp(line, pat + 1, strlen(pat + 1)) == 0)
p = line + strlen(pat + 1);
else if ((p = strstr(line, pat)) != NULL)
p += strlen(pat);
else
return;
if ((e = strchr(p, '"')) == NULL)
return;
snprintf(buf, len, "%.*s", (int) (e - p), p);
}
int
mcde_sys_disks(McdeSysDisk *d, size_t max)
{
static char out[65536];
const char *lb = mcde_sys_which(lsblkPaths);
char *argv[] = { NULL, "-P", "-b", "-o",
"NAME,PKNAME,TYPE,SIZE,MODEL,FSTYPE,LABEL,MOUNTPOINT,RM", NULL };
char *line, *last = NULL, type[16], size[32], rm[8];
size_t n = 0;
if (lb == NULL)
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "lsblk is not there.");
argv[0] = (char *) lb;
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
return -1;
for (line = strtok_r(out, "\n", &last); line != NULL && n < max;
line = strtok_r(NULL, "\n", &last)) {
McdeSysDisk *x = &d[n];
Pair(line, "TYPE", type, sizeof type);
if (strcmp(type, "disk") != 0 && strcmp(type, "part") != 0 &&
strcmp(type, "rom") != 0)
continue;
memset(x, 0, sizeof *x);
Pair(line, "NAME", x->name, sizeof x->name);
if (strcmp(type, "part") == 0)
Pair(line, "PKNAME", x->parent, sizeof x->parent);
Pair(line, "MODEL", x->model, sizeof x->model);
Pair(line, "FSTYPE", x->fstype, sizeof x->fstype);
Pair(line, "LABEL", x->label, sizeof x->label);
Pair(line, "MOUNTPOINT", x->mountpoint, sizeof x->mountpoint);
Pair(line, "SIZE", size, sizeof size);
Pair(line, "RM", rm, sizeof rm);
x->bytes = strtoull(size, NULL, 10);
x->removable = strcmp(rm, "1") == 0 || strcmp(type, "rom") == 0;
if (strcmp(x->mountpoint, "[SWAP]") == 0) {
x->mountpoint[0] = '\0';
x->system = 1;
}
if (strcmp(x->mountpoint, "/") == 0 ||
strcmp(x->mountpoint, "/boot") == 0 ||
strcmp(x->mountpoint, "/boot/efi") == 0 ||
strcmp(x->mountpoint, "/usr") == 0 ||
strcmp(x->mountpoint, "/home") == 0 ||
strcmp(x->fstype, "crypto_LUKS") == 0 ||
strcmp(x->fstype, "LVM2_member") == 0)
x->system = 1;
if (x->mountpoint[0] != '\0') {
struct statvfs sv;
if (statvfs(x->mountpoint, &sv) == 0) {
x->used = (unsigned long long) (sv.f_blocks -
sv.f_bfree) * sv.f_frsize;
x->avail = (unsigned long long) sv.f_bavail *
sv.f_frsize;
}
}
/* a system partition makes its drive the system's */
if (x->system && x->parent[0] != '\0') {
size_t k;
for (k = 0; k < n; k++)
if (strcmp(d[k].name, x->parent) == 0)
d[k].system = 1;
}
n++;
}
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], out[512];
const char *ud = mcde_sys_which(udisksPaths);
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 (ud == NULL)
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "udisksctl is not "
"there.");
snprintf(dev, sizeof dev, "/dev/%s", name);
{
char *argv[] = { (char *) ud, "mount", "-b", dev, NULL };
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
return MCDE_SYS_EFAIL;
}
/* "Mounted /dev/sdb1 at /media/user/STICK" */
if (!Find(name, &d) || d.mountpoint[0] == '\0')
return mcde_sys_fail(MCDE_SYS_EFAIL, "%s", out);
snprintf(mountpoint, len, "%s", d.mountpoint);
return MCDE_SYS_OK;
}
int
mcde_sys_disk_unmount(const char *name)
{
McdeSysDisk d;
char dev[64];
const char *ud = mcde_sys_which(udisksPaths);
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);
snprintf(dev, sizeof dev, "/dev/%s", name);
if (ud != NULL) {
char *argv[] = { (char *) ud, "unmount", "-b", dev, NULL };
return mcde_sys_run(argv);
} else {
char *argv[] = { "/bin/umount", d.mountpoint, NULL };
return mcde_sys_run(argv);
}
}
int
mcde_sys_disk_check(const char *name, char *log, size_t len)
{
McdeSysDisk d;
char dev[64];
char *argv[] = { "/sbin/fsck", "-n", NULL, NULL };
log[0] = '\0';
if (!Find(name, &d) || d.fstype[0] == '\0' ||
strcmp(d.fstype, "swap") == 0)
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "%s cannot be checked.",
name);
snprintf(dev, sizeof dev, "/dev/%s", name);
argv[2] = dev;
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 dev[64], pdev[80];
int n, i;
if (geteuid() != 0)
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
"can erase a disk.");
if (label == NULL || label[0] == '\0' || strlen(label) > 11 ||
strspn(label, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789_-") != strlen(label))
return mcde_sys_fail(MCDE_SYS_EINVAL, "A name has letters, "
"digits, - and _, at most 11.");
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);
snprintf(dev, sizeof dev, "/dev/%s", name);
{
char *argv[] = { "/sbin/sfdisk", "--quiet", dev, NULL };
/* one GPT partition over the whole drive (sfdisk(8)) */
if (mcde_sys_run_input(argv, "label: gpt\n,,L\n") != MCDE_SYS_OK)
return MCDE_SYS_EFAIL;
}
/* sda -> sda1, nvme0n1 -> nvme0n1p1 */
snprintf(part, len, "%s%s1", name,
name[strlen(name) - 1] >= '0' && name[strlen(name) - 1] <= '9' ?
"p" : "");
snprintf(pdev, sizeof pdev, "/dev/%s", part);
if (strcmp(fstype, "msdos") == 0) {
char *argv[] = { "/sbin/mkfs.vfat", "-F", "32", "-n",
(char *) label, pdev, NULL };
return mcde_sys_run(argv);
} else {
char *argv[] = { "/sbin/mkfs.ext4", "-q", "-L", (char *) label,
pdev, NULL };
return mcde_sys_run(argv);
}
}
/* ---- the permissions: Linux distributions keep no list of how their
* files' permissions should be (dpkg --verify checks contents) */
int
mcde_sys_permissions(int repair, char *log, size_t len)
{
(void) repair;
if (len > 0)
log[0] = '\0';
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "This system keeps no list of "
"how its files' permissions should be.");
}