Fix first-run setup timing out on the kernel download

On a Mac without the Linux kernel, setup ran `container system start
--enable-kernel-install` with a 120 s limit. That command downloads a
697 MB archive without printing anything, so most connections hit the
limit and the window showed `timedOut([...])`.

- Start the service with --disable-kernel-install (10 min: a first
  start still pulls the 69 MB base filesystem).
- Download the kernel in its own step with progress, then hand the file
  to `container system kernel set --tar ... --digest ...`, which checks
  it against the runtime's digest.
- Opening the setup window no longer starts the download; Set Up does.
- Timeouts and runner errors read as sentences.
- A rebuild deletes the image it replaced, which frees about 2 GB each
  time, and deletes or stops the builder VM unless it was already
  running (it kept about 2.8 GB of memory).
- Setup asks for 10 GB free, measured on a real setup.
This commit is contained in:
aculix committed 2026-09-29 16:25:50 +05:30
1 parent 4b67728cce
commit 9fd654298a
13 files changed
+559 -128

No files matched your search

@@ -3,6 +3,8 @@ import Synchronization
public final class ContainerCLIBackend: VMBackend {
public static let image = "local/knotchy-vm:latest"
/// The image a rebuild replaces, named so it can be deleted afterwards.
static let previousImage = "local/knotchy-vm:previous"
public static let staleAfterDays = 14
private let cli: CLIRunner
@@ -271,30 +273,164 @@ public final class ContainerCLIBackend: VMBackend {
// MARK: Setup actions
/// A first start also pulls the runtime's base filesystem, about 70 MB.
static let serviceTimeout: TimeInterval = 10 * 60
/// A first build downloads about 470 MB: the builder, the Debian base and its packages.
static let buildTimeout: TimeInterval = 60 * 60
static let kernelFolderPrefix = "knotchy-kernel-"
public func fix(_ issue: SetupIssue, output: @escaping @Sendable (String) -> Void) async throws {
switch issue {
case .serviceStopped, .kernelMissing:
let result = try await cli.run(["system", "start", "--enable-kernel-install"], timeout: 120, line: output)
case .serviceStopped:
// The kernel is its own step: `--enable-kernel-install` downloads 700 MB without showing progress.
let result = try await step(["system", "start", "--disable-kernel-install"], timeout: Self.serviceTimeout,
output: output, named: "Starting the container service")
guard result.status == 0 else { throw VMBackendError.failed("Couldn't start the service: \(result.lastLine)") }
case .kernelMissing:
try await installKernel(output: output)
case .imageMissing, .imageOutdated, .imageStale:
guard let vmSources else { throw VMBackendError.failed("The VM sources are missing from the app.") }
let now = Date()
let day = DateFormatter()
day.dateFormat = "yyyy-MM-dd"
let args = [
"build", "--cpus", "4", "--memory", "4g", "--dns", "1.1.1.1", "--pull",
"--build-arg", "APT_REFRESH=\(day.string(from: now))",
"--build-arg", "BUILD_DATE=\(ISO8601DateFormatter().string(from: now))",
"-f", vmSources.appendingPathComponent("Containerfile").path,
"-t", Self.image, vmSources.path,
]
let result = try await cli.run(args, timeout: 15 * 60, line: output)
guard result.status == 0 else { throw VMBackendError.failed("The image build failed: \(result.lastLine)") }
try await buildImage(output: output)
case .cliMissing, .cliOutdated, .lowDisk:
throw VMBackendError.failed("There's no automatic fix for this.")
}
}
/// Leaves nothing behind but the new image: not the one it replaced, and not a builder it started.
private func buildImage(output: @escaping @Sendable (String) -> Void) async throws {
guard let vmSources else { throw VMBackendError.failed("The VM sources are missing from the app.") }
let now = Date()
let day = DateFormatter()
day.dateFormat = "yyyy-MM-dd"
let args = [
"build", "--cpus", "4", "--memory", "4g", "--dns", "1.1.1.1", "--pull",
"--build-arg", "APT_REFRESH=\(day.string(from: now))",
"--build-arg", "BUILD_DATE=\(ISO8601DateFormatter().string(from: now))",
"-f", vmSources.appendingPathComponent("Containerfile").path,
"-t", Self.image, vmSources.path,
]
let builderBefore = (try? await cli.run(["builder", "status", "--format", "json"], timeout: 15))
.flatMap { Self.builderState(in: $0.text) }
// The runtime keeps a replaced image's files, about 2 GB, until an image is deleted. Deleting the
// old one by name frees them, along with anything earlier builds left.
let named = (try? await cli.run(["image", "tag", Self.image, Self.previousImage], timeout: 30))?.status == 0
let built: Result<CLIRunner.Output, any Error>
do {
built = .success(try await step(args, timeout: Self.buildTimeout, output: output, named: "Building the VM image"))
} catch {
built = .failure(error)
}
// After a failed build the old image keeps its usual name too, so this deletes nothing it needs.
if named { _ = try? await cli.run(["image", "delete", Self.previousImage], timeout: 5 * 60) }
// The builder VM keeps about 3 GB of memory and a 4 GB cache. One the user had running stays as it was.
switch builderBefore {
case .absent?: _ = try? await cli.run(["builder", "delete", "--force"], timeout: 60)
case .stopped?: _ = try? await cli.run(["builder", "stop"], timeout: 60)
case .running?, nil: break
}
let result = try built.get()
guard result.status == 0 else { throw VMBackendError.failed("The image build failed: \(result.lastLine)") }
}
enum BuilderState: Equatable {
case absent
case stopped
case running
}
/// From `builder status --format json`: `[]` when there's no builder.
static func builderState(in json: String) -> BuilderState? {
struct Entry: Decodable {
struct Status: Decodable {
let state: String?
}
let status: Status?
}
guard let entries = try? JSONDecoder().decode([Entry].self, from: Data(json.utf8)) else { return nil }
guard let first = entries.first else { return .absent }
return first.status?.state == "running" ? .running : .stopped
}
/// A timeout reads as a sentence, not as the command that ran.
private func step(_ args: [String], timeout: TimeInterval, output: @escaping @Sendable (String) -> Void,
named name: String) async throws -> CLIRunner.Output {
do {
return try await cli.run(args, timeout: timeout, line: output)
} catch CLIRunner.Failure.timedOut {
throw VMBackendError.failed("\(name) took over \(Int(timeout / 60)) minutes, so Knotchy stopped it. Check your connection and try again.")
}
}
public static func removeKernelDownloads() {
FileDownload.removeFolders(prefix: kernelFolderPrefix)
}
/// `kernel set --recommended` prints nothing while it downloads 700 MB, so Knotchy downloads the archive
/// itself, with progress, and hands it over. The runtime still checks it against the digest it publishes.
private func installKernel(output: @escaping @Sendable (String) -> Void) async throws {
let properties = try? await cli.run(["system", "property", "list", "--format", "json"], timeout: 15)
guard let kernel = properties.flatMap({ Self.recommendedKernel(in: $0.text) }) else {
let result = try await step(["system", "kernel", "set", "--recommended", "--force"], timeout: 60 * 60,
output: output, named: "Downloading the Linux kernel")
guard result.status == 0 else { throw VMBackendError.failed("Couldn't install the Linux kernel: \(result.lastLine)") }
return
}
Self.removeKernelDownloads()
let folder = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(Self.kernelFolderPrefix + UUID().uuidString, isDirectory: true)
guard (try? FileManager.default.createDirectory(at: folder, withIntermediateDirectories: false,
attributes: [.posixPermissions: 0o700])) != nil
else { throw VMBackendError.failed("Couldn't make a folder for the Linux kernel download.") }
defer { try? FileManager.default.removeItem(at: folder) }
let archive = folder.appendingPathComponent(kernel.url.lastPathComponent)
output("Downloading the Linux kernel…")
let throttle = ProgressThrottle()
do {
try await FileDownload.fetch(kernel.url, to: archive) { written, expected in
// One line a percent, so the log keeps what came before the download.
let step = expected.map { Int(written * 100 / max($0, 1)) } ?? Int(written / 10_000_000)
if throttle.advance(to: step) { output(Self.kernelProgress(written, of: expected)) }
}
} catch {
throw VMBackendError.failed("Couldn't download the Linux kernel. Check your connection and try again.")
}
output("Installing the Linux kernel…")
let result = try await step(["system", "kernel", "set", "--tar", archive.path, "--binary", kernel.binaryPath,
"--digest", kernel.digest, "--force"],
timeout: 10 * 60, output: output, named: "Installing the Linux kernel")
guard result.status == 0 else { throw VMBackendError.failed("Couldn't install the Linux kernel: \(result.lastLine)") }
}
struct RecommendedKernel: Equatable {
let url: URL
let binaryPath: String
let digest: String
}
/// From `system property list --format json`; nil when the runtime doesn't say.
static func recommendedKernel(in json: String) -> RecommendedKernel? {
struct Properties: Decodable {
struct Kernel: Decodable {
let url: String?
let binaryPath: String?
let digest: String?
}
let kernel: Kernel?
}
guard let kernel = (try? JSONDecoder().decode(Properties.self, from: Data(json.utf8)))?.kernel,
let url = kernel.url.flatMap(URL.init(string:)),
let binaryPath = kernel.binaryPath, !binaryPath.isEmpty,
let digest = kernel.digest, digest.hasPrefix("sha256:")
else { return nil }
return RecommendedKernel(url: url, binaryPath: binaryPath, digest: digest)
}
static func kernelProgress(_ written: Int64, of expected: Int64?) -> String {
func megabytes(_ bytes: Int64) -> Int { Int((Double(bytes) / 1_000_000).rounded()) }
guard let expected else { return "Downloading the Linux kernel… \(megabytes(written)) MB" }
return "Downloading the Linux kernel… \(megabytes(written)) of \(megabytes(expected)) MB"
}
// MARK: Quit path
public func destroyBlocking(ids: [String], deadline: Duration) {
@@ -309,3 +445,16 @@ public final class ContainerCLIBackend: VMBackend {
}
}
}
private final class ProgressThrottle: Sendable {
private let last = Mutex(-1)
/// True the first time each step is reached.
func advance(to step: Int) -> Bool {
last.withLock { last in
guard step > last else { return false }
last = step
return true
}
}
}