@kvbox/server 0.5.40 → 0.5.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -612,7 +612,9 @@ var MachineSchema = z3.object({
612
612
  diskBytes: z3.number().int(),
613
613
  // the box's own writable disk, as the guest sees it
614
614
  diskUsedBytes: z3.number().int(),
615
- // what that sparse file really occupies on the host
615
+ // what the box uses of it: the filesystem's own count, like the guest's `df` (#115)
616
+ /** What the disk's sparse file occupies on the host. Grows while the box runs, `kvbox stop` trims it back. Hosts before 0.5.42 do not send it. */
617
+ diskHostBytes: z3.number().int().optional(),
616
618
  base: z3.string(),
617
619
  // version of the image the box's overlay stacks on
618
620
  /** The image (#87) when it is not the base one: `postgres`. `base` is then that image's version. */
@@ -1841,7 +1843,7 @@ async function startDns(opts) {
1841
1843
 
1842
1844
  // src/core/machines.ts
1843
1845
  import { spawn } from "node:child_process";
1844
- import { closeSync as closeSync2, existsSync as existsSync6, openSync as openSync2, statSync as statSync2 } from "node:fs";
1846
+ import { closeSync as closeSync3, existsSync as existsSync6, openSync as openSync3, statSync as statSync2 } from "node:fs";
1845
1847
  import { link as link2, mkdir as mkdir6, readFile as readFile6, rename as rename5, rm as rm3, truncate as truncate2, writeFile as writeFile5 } from "node:fs/promises";
1846
1848
  import { setTimeout as sleep } from "node:timers/promises";
1847
1849
 
@@ -2568,6 +2570,44 @@ async function assertRoomFor(vcpu, memMib, force, box, by, p = paths()) {
2568
2570
  if (reason !== void 0) throw new KvboxError(reason);
2569
2571
  }
2570
2572
 
2573
+ // src/core/ext4.ts
2574
+ import { closeSync as closeSync2, openSync as openSync2, readSync } from "node:fs";
2575
+ var SUPERBLOCK_OFFSET = 1024;
2576
+ var EXT4_MAGIC = 61267;
2577
+ var INCOMPAT_META_BG = 16;
2578
+ var INCOMPAT_64BIT = 128;
2579
+ function ext4Usage(file) {
2580
+ let fd;
2581
+ try {
2582
+ fd = openSync2(file, "r");
2583
+ const sb = Buffer.alloc(1024);
2584
+ if (readSync(fd, sb, 0, sb.length, SUPERBLOCK_OFFSET) !== sb.length) return null;
2585
+ if (sb.readUInt16LE(56) !== EXT4_MAGIC) return null;
2586
+ const incompat = sb.readUInt32LE(96);
2587
+ if (incompat & INCOMPAT_META_BG) return null;
2588
+ const is64 = (incompat & INCOMPAT_64BIT) !== 0;
2589
+ const blockSize = 1024 << sb.readUInt32LE(24);
2590
+ const blocks = sb.readUInt32LE(4) + (is64 ? sb.readUInt32LE(336) * 2 ** 32 : 0);
2591
+ const firstDataBlock = sb.readUInt32LE(20);
2592
+ const blocksPerGroup = sb.readUInt32LE(32);
2593
+ const descSize = is64 ? sb.readUInt16LE(254) : 32;
2594
+ if (blocksPerGroup === 0 || descSize < 32) return null;
2595
+ const groups = Math.ceil((blocks - firstDataBlock) / blocksPerGroup);
2596
+ const table = Buffer.alloc(groups * descSize);
2597
+ if (readSync(fd, table, 0, table.length, (firstDataBlock + 1) * blockSize) !== table.length) return null;
2598
+ let free = 0;
2599
+ for (let g = 0; g < groups; g++) {
2600
+ const at = g * descSize;
2601
+ free += table.readUInt16LE(at + 12) + (descSize >= 64 ? table.readUInt16LE(at + 44) * 65536 : 0);
2602
+ }
2603
+ return { sizeBytes: blocks * blockSize, usedBytes: (blocks - free) * blockSize };
2604
+ } catch {
2605
+ return null;
2606
+ } finally {
2607
+ if (fd !== void 0) closeSync2(fd);
2608
+ }
2609
+ }
2610
+
2571
2611
  // src/core/snapshots.ts
2572
2612
  import { existsSync as existsSync4 } from "node:fs";
2573
2613
  import { link, mkdir as mkdir5, readdir as readdir5, rename as rename4, rm as rm2, stat as stat2 } from "node:fs/promises";
@@ -3015,11 +3055,16 @@ async function drawPorts(count, p) {
3015
3055
  function diskStats(disk) {
3016
3056
  try {
3017
3057
  const st = statSync2(disk);
3018
- return { diskBytes: st.size, diskUsedBytes: st.blocks * 512 };
3058
+ const diskHostBytes = st.blocks * 512;
3059
+ const fs = ext4Usage(disk);
3060
+ return { diskBytes: st.size, diskUsedBytes: fs ? Math.min(fs.usedBytes, st.size) : diskHostBytes, diskHostBytes };
3019
3061
  } catch {
3020
- return { diskBytes: 0, diskUsedBytes: 0 };
3062
+ return { diskBytes: 0, diskUsedBytes: 0, diskHostBytes: 0 };
3021
3063
  }
3022
3064
  }
3065
+ async function compactDisk(file) {
3066
+ await run(["e2fsck", "-f", "-p", "-E", "discard", file], { okExitCodes: [1, 2], timeoutMs: 10 * 6e4 });
3067
+ }
3023
3068
  function diskGibOf(meta, p) {
3024
3069
  try {
3025
3070
  return Math.round(statSync2(p.machine(meta.owner, meta.name).overlay).size / 1024 ** 3);
@@ -3160,7 +3205,7 @@ async function spawnFirecracker(ref, cfg, p) {
3160
3205
  const m = p.machine(ref.owner, ref.name);
3161
3206
  await rm3(m.sock, { force: true });
3162
3207
  await rm3(m.vsock, { force: true });
3163
- const logFd = openSync2(m.log, "a");
3208
+ const logFd = openSync3(m.log, "a");
3164
3209
  let child;
3165
3210
  try {
3166
3211
  child = spawn(cfg.firecracker, ["--api-sock", m.sock, "--id", ref.name, "--level", "Error"], {
@@ -3169,7 +3214,7 @@ async function spawnFirecracker(ref, cfg, p) {
3169
3214
  stdio: ["ignore", logFd, logFd]
3170
3215
  });
3171
3216
  } finally {
3172
- closeSync2(logFd);
3217
+ closeSync3(logFd);
3173
3218
  }
3174
3219
  child.unref();
3175
3220
  const pid = child.pid;
@@ -3604,6 +3649,7 @@ var stopMachine = locked(async function stopMachine2(ref, p = paths()) {
3604
3649
  const m = p.machine(ref.owner, ref.name);
3605
3650
  if (!vmAlive(meta) && !isHibernated(meta, p)) throw new KvboxError(`box ${shown(ref)} is already stopped`);
3606
3651
  await shutdown(meta, cfg, p, 1e4);
3652
+ await compactDisk(m.overlay).catch((err) => console.error(`warning: box ${shown(ref)}: disk not compacted: ${err.message}`));
3607
3653
  await rm3(m.hibernateDir, { recursive: true, force: true });
3608
3654
  await rm3(m.memBase, { force: true });
3609
3655
  const stopped = { ...meta, pid: null, memSnapshots: void 0, paused: void 0 };
@@ -4756,7 +4802,7 @@ function versionOf(srcFileUrl, injected) {
4756
4802
  }
4757
4803
 
4758
4804
  // src/version.ts
4759
- var VERSION = versionOf(import.meta.url, true ? "0.5.40" : void 0);
4805
+ var VERSION = versionOf(import.meta.url, true ? "0.5.42" : void 0);
4760
4806
 
4761
4807
  // src/caller.ts
4762
4808
  import { createHash as createHash2, timingSafeEqual } from "node:crypto";