@kvbox/server 0.6.20 → 0.6.21

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.
@@ -4089,7 +4089,7 @@ import { setTimeout as sleep3 } from "node:timers/promises";
4089
4089
  // src/core/agent.ts
4090
4090
  import { connect } from "node:net";
4091
4091
  var AGENT_PORT = 52;
4092
- async function agentCall(udsPath, req, timeoutMs = 5e3) {
4092
+ async function agentCall(udsPath, req, timeoutMs = 5e3, connectMs = timeoutMs) {
4093
4093
  const reply = await new Promise((resolve3, reject) => {
4094
4094
  const sock = connect(udsPath);
4095
4095
  let buf = "";
@@ -4098,7 +4098,7 @@ async function agentCall(udsPath, req, timeoutMs = 5e3) {
4098
4098
  sock.destroy();
4099
4099
  reject(err);
4100
4100
  };
4101
- sock.setTimeout(timeoutMs, () => fail(new KvboxError(handshaken ? `kvbox-agent did not answer within ${timeoutMs} ms` : `the guest did not accept the vsock connection within ${timeoutMs} ms`)));
4101
+ sock.setTimeout(connectMs, () => fail(new KvboxError(handshaken ? `kvbox-agent did not answer within ${timeoutMs} ms` : `the guest did not accept the vsock connection within ${connectMs} ms`)));
4102
4102
  sock.once("error", (err) => fail(new KvboxError(`kvbox-agent unreachable: ${err.message}`)));
4103
4103
  sock.once("connect", () => sock.write(`CONNECT ${AGENT_PORT}
4104
4104
  `));
@@ -4111,6 +4111,7 @@ async function agentCall(udsPath, req, timeoutMs = 5e3) {
4111
4111
  if (!handshaken) {
4112
4112
  if (!line.startsWith("OK ")) return fail(new KvboxError(`vsock connect refused: ${line}`));
4113
4113
  handshaken = true;
4114
+ sock.setTimeout(timeoutMs);
4114
4115
  sock.write(JSON.stringify(req) + "\n");
4115
4116
  } else {
4116
4117
  sock.end();
@@ -4181,6 +4182,19 @@ async function runOutput(udsPath, id, stream, offset, limit, timeoutMs = 15e3) {
4181
4182
  if (typeof size2 !== "number" || !Number.isInteger(size2) || size2 < 0 || typeof data !== "string") throw new KvboxError("kvbox-agent sent run output this server does not understand");
4182
4183
  return { size: size2, offset, data };
4183
4184
  }
4185
+ var FREEZE_BASE = "v0.19.0";
4186
+ var FREEZE_TIMEOUT_MS = 2e4;
4187
+ var FREEZE_CONNECT_MS = 3e3;
4188
+ var isAgentRefusal = (err) => /^kvbox-agent: /.test(err.message ?? "");
4189
+ async function freezeDisks(udsPath, disks, thawAfterSec, timeoutMs = FREEZE_TIMEOUT_MS) {
4190
+ const reply = await agentCall(udsPath, { op: "freeze", box: disks.box === true, volumes: [...disks.volumes ?? []], thawAfterSec }, timeoutMs, FREEZE_CONNECT_MS);
4191
+ const frozen = reply["frozen"];
4192
+ return Array.isArray(frozen) ? frozen.filter((m) => typeof m === "string") : [];
4193
+ }
4194
+ async function thawDisks(udsPath, timeoutMs) {
4195
+ const thawed = (await agentCall(udsPath, { op: "thaw" }, timeoutMs))["thawed"];
4196
+ return Array.isArray(thawed) ? thawed.filter((m) => typeof m === "string") : [];
4197
+ }
4184
4198
  var isUnknownOp = (err) => /unknown op/.test(err.message ?? "");
4185
4199
  var isGuestBooting = (err) => /did not accept the vsock connection/.test(err.message ?? "");
4186
4200
  var isNoAgent = (err) => /vsock connect refused|vsock connection closed before the guest accepted|did not accept the vsock connection|kvbox-agent unreachable: connect (ENOENT|ECONNREFUSED)/.test(err.message ?? "");
@@ -6407,7 +6421,7 @@ async function stageBackup(ref, req, p = paths()) {
6407
6421
  const meta2 = await readBox2(ref, p);
6408
6422
  const m = p.machine(meta2.owner, meta2.name);
6409
6423
  await assertRoom(s.dir, 0, `a backup of box ${shown(ref)}`, p);
6410
- await frozenClone(meta2, m.overlay, s.disk, p);
6424
+ await frozenClone(meta2, m.overlay, s.disk, { box: true }, p);
6411
6425
  const envText2 = existsSync13(m.env) ? await readFile9(m.env, "utf8") : null;
6412
6426
  return { meta: meta2, envText: envText2 };
6413
6427
  });
@@ -6429,13 +6443,33 @@ async function stageBackup(ref, req, p = paths()) {
6429
6443
  throw err;
6430
6444
  }
6431
6445
  }
6432
- async function frozenClone(box, from, to, p) {
6446
+ var THAW_AFTER_SEC = 30;
6447
+ async function frozenClone(box, from, to, disks, p) {
6433
6448
  if (!vmAlive(box) || box.paused) return driver().disk.clone(from, to);
6434
- await driver().vm.pause(box, p);
6449
+ const vsock = p.machine(box.owner, box.name).vsock;
6450
+ const freeze = await freezeForCopy(box, vsock, disks);
6435
6451
  try {
6436
- await driver().disk.clone(from, to);
6452
+ await driver().vm.pause(box, p);
6453
+ try {
6454
+ await driver().disk.clone(from, to);
6455
+ } finally {
6456
+ await resumeAfterCopy(box, p);
6457
+ }
6437
6458
  } finally {
6438
- await resumeAfterCopy(box, p);
6459
+ if (freeze !== "none") await thawDisks(vsock, freeze === "unsure" ? FREEZE_TIMEOUT_MS + 5e3 : void 0).catch((err) => log.warn(`box ${shown(box)}: thaw after a backup's copy failed (${err.message}); its guest thaws by itself within ${THAW_AFTER_SEC} s`));
6460
+ }
6461
+ }
6462
+ async function freezeForCopy(box, vsock, disks) {
6463
+ try {
6464
+ return (await freezeDisks(vsock, disks, THAW_AFTER_SEC)).length > 0 ? "frozen" : "none";
6465
+ } catch (err) {
6466
+ if (isUnknownOp(err)) {
6467
+ log.debug(`box ${shown(box)}: its image is from before base ${FREEZE_BASE}, which cannot freeze a disk; copied as it is`);
6468
+ return "none";
6469
+ }
6470
+ if (isNoAgent(err)) return "none";
6471
+ log.warn(`box ${shown(box)}: its disk was not frozen for a backup's copy (${err.message}); copied as it is`);
6472
+ return isAgentRefusal(err) ? "none" : "unsure";
6439
6473
  }
6440
6474
  }
6441
6475
  async function stageVolumeBackup(ref, req, p = paths()) {
@@ -6460,7 +6494,7 @@ async function stageVolumeBackup(ref, req, p = paths()) {
6460
6494
  await withBoxLock(box, async () => {
6461
6495
  const meta = await readBox2(box, p);
6462
6496
  if (!meta.volumes?.includes(ref.name)) throw new KvboxError(`volume ${shown(ref)} was just detached from box ${holder}; try again`);
6463
- await frozenClone(meta, from, s.disk, p);
6497
+ await frozenClone(meta, from, s.disk, { volumes: [ref.name] }, p);
6464
6498
  });
6465
6499
  }
6466
6500
  const { sizeBytes, chunks } = await chunkNames(s.disk, keys);
@@ -7775,7 +7809,7 @@ function versionOf(srcFileUrl, injected) {
7775
7809
  }
7776
7810
 
7777
7811
  // src/version.ts
7778
- var VERSION = versionOf(import.meta.url, true ? "0.6.20" : void 0);
7812
+ var VERSION = versionOf(import.meta.url, true ? "0.6.21" : void 0);
7779
7813
 
7780
7814
  // src/caller.ts
7781
7815
  import { createHash as createHash4, timingSafeEqual as timingSafeEqual2 } from "node:crypto";