feat: add 'vol' command

This commit is contained in:
daniel committed 2026-09-19 08:54:15 +02:00
1 parent 420a7c996c
commit 80b5beb17d
17 files changed
+284 -17

No files matched your search

+1
View File
@@ -62,6 +62,7 @@ void cmd_help() {
montauk::print(" edit [file] Text editor\n");
montauk::print(" whoami Print current username\n");
montauk::print(" info Show system information\n");
montauk::print(" vol List mounted volumes\n");
montauk::print(" date Show current date and time\n");
montauk::print(" uptime Show uptime\n");
montauk::print(" clear Clear the screen\n");
+105
View File
@@ -0,0 +1,105 @@
/*
* main.cpp
* vol - List mounted volumes and the disks they live on
* Copyright (c) 2026 Daniel Hammer
*/
#include <montauk/syscall.h>
#include <libc/stdio.h>
static constexpr int MaxVolumes = 64;
static const char* kind_name(unsigned kind) {
switch (kind) {
case 1: return "SATA";
case 2: return "SATAPI";
case 3: return "NVMe";
case 4: return "USB";
default: return "-";
}
}
static void format_size(uint64_t bytes, char* out, int outMax) {
static const char* units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };
int unit = 0;
uint64_t whole = bytes;
uint64_t rem = 0;
while (whole >= 1024 && unit < 5) {
rem = whole % 1024;
whole /= 1024;
unit++;
}
if (unit == 0) {
snprintf(out, outMax, "%llu B", (unsigned long long)whole);
return;
}
unsigned tenths = (unsigned)((rem * 10) / 1024);
snprintf(out, outMax, "%llu.%u %s", (unsigned long long)whole, tenths, units[unit]);
}
extern "C" void _start() {
montauk::abi::VolInfo vols[MaxVolumes] = {};
int n = montauk::volinfo(vols, MaxVolumes);
if (n <= 0) {
montauk::print("No volumes are mounted.\n");
montauk::exit(n < 0 ? 1 : 0);
}
printf("%-5s %-8s %-18s %10s %5s %5s %s\n",
"Vol", "Type", "Label", "Size", "Disk", "Part", "Partition");
for (int i = 0; i < n; i++) {
const montauk::abi::VolInfo& v = vols[i];
char drive[8];
snprintf(drive, sizeof(drive), "%d:/", v.drive);
char size[16];
if (v.sectorCount != 0 && v.sectorSize != 0) {
format_size(v.sectorCount * (uint64_t)v.sectorSize, size, sizeof(size));
} else {
snprintf(size, sizeof(size), "-");
}
char disk[8];
char part[8];
if (v.blockDev >= 0) snprintf(disk, sizeof(disk), "%d", v.blockDev);
else snprintf(disk, sizeof(disk), "-");
if (v.partIndex >= 0) snprintf(part, sizeof(part), "%d", v.partIndex);
else snprintf(part, sizeof(part), "-");
printf("%-5s %-8s %-18s %10s %5s %5s %s\n",
drive,
v.fsType[0] ? v.fsType : "-",
v.label[0] ? v.label : "-",
size, disk, part,
v.partName[0] ? v.partName : "-");
}
bool printedHeader = false;
for (int i = 0; i < n; i++) {
if (vols[i].blockDev < 0) continue;
bool seen = false;
for (int j = 0; j < i; j++) {
if (vols[j].blockDev == vols[i].blockDev) { seen = true; break; }
}
if (seen) continue;
if (!printedHeader) {
montauk::print("\nDisks:\n");
printedHeader = true;
}
printf(" %d %-6s %s\n",
vols[i].blockDev,
kind_name(vols[i].kind),
vols[i].model[0] ? vols[i].model : "(unknown)");
}
montauk::exit(0);
}