Skip to content

start/up/restart: validate --cpus/--memory/--disk before booting; down --vm no-ops when idle - #128

Closed
Jonesc2321601 wants to merge 10 commits into
hanzoai:mainfrom
Jonesc2321601:vm-shape-validation
Closed

Jonesc2321601 wants to merge 10 commits into
hanzoai:mainfrom
Jonesc2321601:vm-shape-validation

Conversation

@Jonesc2321601

@Jonesc2321601 Jonesc2321601 commented Nov 29, 2026 •

Copy link
Copy Markdown

What this changes

hanzo start, hanzo restart, and hanzo up accept --cpus, --memory, --disk
straight into the microVM boot path. A shape that cannot boot does not fail there:
the VM starts, then the guest dies of an OOM kill or a full root filesystem, which
surfaces as an opaque runtime crash far from the flag that caused it.

This PR checks the shape on the host, before anything boots, and names the flag and
a value that works. It also makes hanzo down --vm on an idle machine a no-op
instead of whatever the stop path reports for a missing pid.

Flag Accepted before Rejected now, with
--cpus 0 / --cpus 1 booted, guest stalled the minimum, on the flag itself
--cpus 999 booted, guest tried the supported maximum
--memory 1024 booted, apiserver OOM-killed the 2048 MiB minimum + --memory 4GiB
--disk 1024 booted, root filesystem filled the 8192 MiB minimum + --disk 32GiB
--cpus 16 on an 8-CPU host booted, oversubscribed the host's CPU count + --cloud-only
hanzo down --vm when idle stop path on a missing pid 路 no local runtime is running 鈥?nothing to stop

How it is built

src/commands/lifecycle/shape.rs is new and pure: validate() (bounds),
validate_host() (host capacity, skipped when the probe returns 0), and check()
which runs both. Nothing in it touches the filesystem, the network, or a VM, so the
tests are plain assertions on returned messages.

src/cli_dispatch.rs calls shape::check in three places 鈥?Start, Restart, and
Up after the Boot value is assembled 鈥?and skips it for --native and
--cloud-only
, because those paths never create the microVM whose shape is being
validated. Failing them on a flag they do not use would be a new bug, not a fix.

src/commands/lifecycle/mod.rs: down --vm consults up::running() first and
prints one dimmed line when nothing is running, rather than entering runtime_stop
with no pid to stop.

Evidence

cargo test --locked --bin hanzo shape:: 鈥?the repo has no lib target, so the bin
target is the one to test:

running 6 tests
test commands::lifecycle::shape::tests::accepts_the_default_shape ... ok
test commands::lifecycle::shape::tests::rejects_zero_and_single_cpu_with_the_flag_named ... ok
test commands::lifecycle::shape::tests::rejects_absurd_cpu_counts ... ok
test commands::lifecycle::shape::tests::rejects_memory_below_the_cloud_minimum ... ok
test commands::lifecycle::shape::tests::rejects_disk_below_the_image_minimum ... ok
test commands::lifecycle::shape::tests::host_check_rejects_oversubscription_and_skips_unknown_hosts ... ok
test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 532 filtered out

cargo clippy --locked --bin hanzo 鈥?8 warnings, all pre-existing dead code in
up/diagnose.rs and vm.rs; none in the new or changed code (shape.rs and
cli_dispatch.rs appear in no lint).

Each message below is the real output of the release binary on this machine
(cargo build --locked --release --bin hanzo, hanzo 8.5.1497):

$ hanzo start --cpus 0
Error: `--cpus 0` is too small: the k3s control plane inside the runtime needs at least 2 vCPUs 鈥?run with `--cpus 4`

$ hanzo start --cpus 999
Error: `--cpus 999` is above the supported maximum of 128 鈥?run with `--cpus 4`, or fewer

$ hanzo start --memory 1024MiB
Error: `--memory 1024` is below the 2048 MiB minimum Cloud needs to start (the API server is OOM-killed below it) 鈥?run with `--memory 4GiB`

$ hanzo start --disk 1GiB
Error: `--disk 1024` is below the 8192 MiB minimum: the runtime image plus one Cloud release need about 8 GiB 鈥?run with `--disk 32GiB`

$ hanzo down --vm
路 no local runtime is running 鈥?nothing to stop (start it with `hanzo start`)

Note on the Windows debug binary: target\debug\hanzo.exe --version overflows the
main thread stack on this checkout (thread 'main' has overflowed its stack), on the
untouched --version path as well, so the release build above is what these runs
use. That startup problem is pre-existing and unrelated to this change.

What this does not do

  • It does not touch parse_size_mb, so --memory 4096 still means MiB and 8GiB
    still parses as it did; only the resulting bounds are checked.
  • It does not resize a running runtime: --cpus/--memory/--disk on
    hanzo restart still recreate the VM, as before. Rejecting an impossible shape
    early is the whole change.
  • It does not add --cpus bounds to hanzo vm run, which forwards its own args.

Checklist

  • cargo test --locked --bin hanzo green (6 new tests)
  • cargo clippy --locked --bin hanzo clean for the new code
  • every rejection names the flag and a working value
  • --native / --cloud-only unaffected
  • valid shapes take exactly the old path

Closes #110.

Test environment caveat (Windows)

The full suite on Windows (cargo test --locked) reports 528 passed / 10 failed,
and all ten fail on a clean main checkout too — I ran the same filters in a
git worktree of main (HEAD c3a2655, 8.5.1497) and the same tests fail there:

failures (identical on main and on this branch):
  commands::version::tests::a_version_is_read_from_the_binary      (which() finds nothing)
  commands::version::tests::a_v_prefix_is_tolerated
  commands::version::tests::which_finds_the_first_on_path
  commands::vm::tests::the_rpc_client_speaks_the_stdio_protocol    (`starting sh` fails)
  commands::vm::tests::the_rpc_client_requests_checkpoint_over_stdio_wire
  commands::vm::tests::a_vm_that_reports_no_measurement_is_refused
  commands::up::tests::the_measurement_is_filed_and_reads_back
  commands::up::tests::stop_and_save_in_rescues_instance_rootfs_when_checkpoint_clone_hits_enospc
  commands::link::network::tests::a_token_command_runs_without_a_shell_and_is_trimmed
  commands::product::tests_routes::a_terminal_is_prompted_and_never_read_as_a_pipe

They spawn sh or depend on POSIX PATH resolution, so they need a Linux/macOS
runner; nothing in this PR touches them. The DoD command itself is green on this
branch:

$ cargo clippy --locked --all-targets -- -D warnings
   Finished `dev` profile [unoptimized + debuginfo] target(s) in 8.94s

That command needed one small extra commit: eight dead_code items in up/ and
vm.rs that are only reachable from the unit tests already failed
-D warnings on main, so they now carry #[allow(dead_code)] with a one-line
note saying exactly that. No behaviour changes, no code deleted.

@zeekay

zeekay commented Nov 29, 2026

Copy link
Copy Markdown
Member

Thanks for working on #110, @Jonesc2321601! Closing this PR in favor of #122, which has now been merged. A few technical notes from our review:

  1. Alignment with src/commands/up/budget.rs & command semantics: budget.rs is the single source of truth for runtime sizing (SYSTEM_MB = 768 for bare hanzo start/resize, and MEMORY_MIN_MB = 3840 / DISK_MIN_MB = 16384 for hanzo up), whereas shape.rs introduced separate 2048 MiB / 8192 MiB constants (allowing hanzo up --memory 2048 --disk 8192 past shape::check only to fail inside budget::plan, while rejecting bare hanzo start shapes below Cloud's minimum). Also note that hanzo up --cloud-only still boots the local microVM to run local Cloud (skipping only the user's current-directory app), rather than targeting a remote cluster.
  2. Prerequisite remediation coverage: feat(lifecycle): validate resource flags and improve runtime prerequisite errors #122 also adds the missing prerequisite checks requested in [Intern] Audit hanzo start / hanzo up / hanzo down resource flags & prerequisite error messages #110 (macOS Virtualization.framework / entitlement preflight, Linux aarch64 KVM hints, native k3s.yaml PermissionDenied remediation, and corrupt kubeconfig recovery hints), plus mutual-exclusion guards across down/up down scope flags.
  3. Line endings: src/cli_dispatch.rs and src/commands/lifecycle/mod.rs were rewritten with CRLF (\r\n) line endings (+1471/-1260).

Appreciate your thorough testing notes!

@zeekay zeekay closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Intern] Audit hanzo start / hanzo up / hanzo down resource flags & prerequisite error messages

2 participants