Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// The Disk applet (#tb-disk, its panel #tb-disk-panel), only with a data// disk and from sm up. The item: a cylinder and a bar filled to the data// disk's use (the store's blocks ever written; amber from 85 %, red from// 95 %), where it is kept in its tooltip. The panel: /data's share as its// pill, /data's use (df in the guest, exact; else the blocks written), the// root's own changes when they are not on /data, the host's block I/O// history and rates (every disk: the data disk and the distro image), where// the data disk is kept (saved in this browser or in memory only) and its// state as Settings says it, the root image. Monitor (the header) opens System's// Overview, Settings opens Settings' Storage section.import { fmtBytes, fmtRate, fmtSizeShort } from "../../format.ts";import { KW } from "../../kw.ts";import { useStats } from "../../stats.ts";import { useStore } from "../../store.ts";import { Key, SECOND, Spark, TONE } from "../Charts.tsx";import type { PopoverProps } from "../ControlCenter.tsx";import { diskUse, latest } from "../Glance.tsx";import { DiskIcon } from "../icons.tsx";import { AppletButton, AppletFacts, AppletNote, AppletPanel, AppletSection, Bars, ICON, shareTone, type AppletItemProps } from "./Applet.tsx";import { CHART_H, Usage } from "./Resource.tsx";
const MiB = 1048576;
export function DiskItem(props: AppletItemProps) { const disk = useStore((s) => s.disk); if (!disk) return null; const share = disk.size > 0 ? disk.used / disk.size : null; const used = `${fmtSizeShort(disk.used)} of ${fmtSizeShort(disk.size)}${disk.size > 0 ? ` (${Math.round((disk.used / disk.size) * 100)}%)` : ""}`; const title = `Data disk (/data): ${used} in use, ${disk.saved ? "saved in this browser" : "in memory only"}; counts blocks ever written, df in the guest is exact`; return ( <AppletButton id="disk" title={title} tone={shareTone(share)} wide meter {...props}> <DiskIcon size={ICON} /><Bars values={[share]} /> </AppletButton> );}
export function DiskPanel(props: PopoverProps) { const st = useStats(); const cfg = KW.config; const disk = useStore((s) => s.disk); const status = useStore((s) => s.dataStatus); const detail = useStore((s) => s.dataDetail); const distro = useStore((s) => s.distro); const du = diskUse(st, disk); const df = (st.df ?? []).filter((r) => r.size > 0); const data = df.find((r) => r.mount === "/data"), root = df.find((r) => r.mount === "/"); // An overlay root whose changes are on /data reports /data's file system: shown once. const rootOwn = root && !(data && root.size === data.size && Math.abs(root.used - data.used) < 4 * MiB) ? root : undefined; const rd = latest(st.points, (p) => p.rd), wr = latest(st.points, (p) => p.wr); const f = du ? du.used / du.size : null; const pill = !disk ? { state: "no data disk", tone: "off" as const } : f != null ? { state: `${Math.round(f * 100)}% used`, tone: shareTone(f) ?? "pending" } : { state: "attached", tone: "pending" as const }; const statusTone = detail.tone === "err" ? "text-err" : detail.tone === "warn" ? "text-warn" : "text-fg"; return ( <AppletPanel id="disk" title="Disk" icon={<DiskIcon size={16} />} {...pill} monitor="status" settings="storage" {...props}> <AppletSection> {du ? <Usage name="/data" used={du.used} size={du.size} color={TONE.disk} note={du.exact ? undefined : "written"} /> : <AppletNote>{disk ? "/data's use comes with the next sample" : "No data disk this boot: changes are lost when the page closes"}</AppletNote>} {rootOwn && <Usage name="/ (changes)" used={rootOwn.used} size={rootOwn.size} color={TONE.disk} />} </AppletSection> <AppletSection title="Activity" aside="all disks" className="border-t border-ink/10"> <Spark points={st.points} height={CHART_H} fmt={fmtRate} label="Disk I/O" series={[ { get: (p) => p.rd, label: "read", color: TONE.disk, area: true }, { get: (p) => p.wr, label: "write", color: SECOND }, ]} /> <div className="flex gap-4 text-[11px] text-faint"><Key color={TONE.disk}>read</Key><Key color={SECOND}>write</Key></div> <AppletFacts items={[ ["Read", rd != null ? fmtRate(rd) : "–"], ["Write", wr != null ? fmtRate(wr) : "–"], ]} /> </AppletSection> <AppletSection className="border-t border-ink/10"> <AppletFacts items={[ disk && ["Kept", disk.saved ? "saved in this browser" : "in memory only"], disk && du?.exact && ["In the browser", `${fmtBytes(disk.used)} of ${fmtBytes(disk.size)}`], ["Root image", cfg.distro ? <span title={distro?.url}>{distro?.image || cfg.distro.name}, {fmtBytes(cfg.distro.size)}</span> : "base initramfs"], ]} /> {status && <p className={`font-sans text-[11.5px] leading-snug break-words ${statusTone}`} title={detail.detail}>{status}</p>} </AppletSection> </AppletPanel> );}