Skip to content

feat(vm): vm resize sets a stopped VM's vCPUs, memory and disk size for its next cold boot - #343

Open
tonicmuroq wants to merge 3 commits into
masterfrom
vm/resize-stopped
Open

tonicmuroq wants to merge 3 commits into
masterfrom
vm/resize-stopped

Conversation

@tonicmuroq

@tonicmuroq tonicmuroq commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What

cocoon vm resize VM [--cpu N] [--memory SIZE] [--storage SIZE] changes the size a stopped VM boots at. At least one flag is required and an omitted one keeps the current value. The next vm start cold-boots from the VM's own disks at the new size.

  • vCPUs and memory are rewritten in the record under the VM's ops lock; start already builds the VMM config from the record.
  • --storage grows the COW disk and never shrinks it.
    • OCI VM (raw disk): cocoon grows the file and the ext4 it formatted at create (e2fsck -fp, then resize2fs), so the guest sees the space at boot.
    • Cloud-image VM (qcow2): cocoon grows the virtual size only. The partition and filesystem are grown inside the guest, and the CLI prints the commands. cloud-init does not do it, because the cidata disk is gone after the first boot.
    • Data disks keep their size.
  • Refusals. A running VM, one still being created, and one whose last run has not settled (the record still claims it as up, or a VMM is alive) are refused with run vm stop first. The new size must pass the create-time floors, the host core count, and the cgroup knobs the VM keeps.
  • Order. The disk grows first and the record changes last, so a resize that fails or is killed part-way keeps the old size on record and is safe to run again. A file that is already larger than the request is refused instead of truncated.
  • Metering. A resize closes the VM's storage interval at the old shape and opens one at the new shape, both with the new reason resize. A resize to the size the VM already has writes nothing.

Why

A box created at 2 vCPU / 4 GB has to move to 4 vCPU / 8 GB with its whole disk. Clone and restore cannot do it: they rebuild the guest from the snapshot's memory image, which fixes vCPUs and memory (their --cpu/--memory were removed in #27 for that reason). sandboxd drives this verb from cocoonstack/sandbox#644, on hibernated claims, with --cpu and --memory; that call form is unchanged.

Storage is in the same verb because a size class is three numbers, and a box that moves up a class otherwise keeps the smaller disk.

Changes against the first version of this PR

  • --storage, with the two disk growers.
  • A VM that is not settled is refused. The first version checked the record's state only, so an error record left by a failed stop (compute interval still open, sometimes with the VMM alive) was resized: the later compute.stop then carried the new shape against a start with the old one.
  • The size checks of vm create apply (validateHostCPU, the cgroup knobs, the floors); the floors now have one owner, VMConfig.ValidateSize.
  • The storage interval rotates, so the ledger's shape stays true.
  • --cpu and --memory are optional.
  • Resize is a method of the Hypervisor interface: both backends take it from Backend, so an optional interface had no backend that lacked it.
  • ExpandQcow2 moves from the Cloud Hypervisor package to hypervisor/disks.go, next to the raw COW helpers. cliutil.FlagSize replaces three copies of the size-flag parse.

For downstream

  • Metering: a new reason value, resize, on a vm.storage.stop / vm.storage.start pair. A consumer that rejects unknown reasons needs it added.
  • Restore: vm restore from a snapshot taken before the resize brings that snapshot's size and disks back. This is by design; the docs say so.
  • Cloud-image guests grow the partition themselves after a --storage resize: growpart /dev/vda 1 && resize2fs /dev/vda1 on an Ubuntu image, Disk Management on Windows.

Size and cost

  • Production code: +229 / -32, net +197. Tests +181. Docs +25 / -4.
    • hypervisor/resize.go +90: the verb, the stopped check, the size checks.
    • hypervisor/disks.go +47: growCOW (+28) and ExpandQcow2, which moved here from cloudhypervisor/utils.go (-18 there).
    • cmd/vm +52: the command and its handler.
    • cmd/cliutil +15, cmd/core -1: FlagSize.
    • types/vm.go +8: ValidateSize.
  • Hot paths: none. Create, clone, start, stop, restore and hibernate are untouched; vm resize is an offline verb.
  • Cost of the verb on bare metal: 39 ms for --cpu alone; 94 ms for vCPUs, memory and a 10G to 20G disk; 91 ms to grow a disk holding 3 GiB of data from 10G to 40G. The grow writes filesystem metadata only (allocated size stays 3.0G).

Gates on the pushed head: golangci-lint 0 issues on linux and darwin, fmt, vet, go mod tidy -diff, asl on both; go test -race on darwin (39 packages) and in a linux container with e2fsprogs and qemu-utils (40 packages), where the raw-disk test runs the real mkfs.ext4, e2fsck and resize2fs.

Coverage

Hardware: bare metal, x86_64, kernel 6.12, XFS; Cloud Hypervisor and Firecracker from the cocoonstack fork dev builds; two runs, 148 checks, 1 failure that was a wrong expectation of the test (see the cloud-image row).

Case How Result
OCI guest on Cloud Hypervisor, 2 vCPU / 4G / 10G to 4 / 8G / 20G hardware, checked inside the guest nproc 4, MemTotal 8.2 GB, root filesystem 21.0 GB, marker file kept
OCI guest on Firecracker, same sizes hardware, inside the guest same
Hibernated VM, resize --cpu 4 --memory 8G, then vm start (the sandboxd flow) hardware cold boot at 4 vCPU / 8G, marker kept
Refusals: running, smaller disk, memory under the floor, more vCPUs than the host, no flag hardware each refused with its own message, record unchanged
error record with the run still open hardware; the record state set in the test root's own meta store refused; the VMM stays alive and the ledger reads start 2/4G, stop 2/4G. The first version of this PR accepts it and the ledger reads start 2/4G, stop 4/8G
stopped record with a live VMM hardware, same injection refused, disk untouched; vm stop kills the VMM and the resize then passes
Resize killed after the file grew, before the record changed hardware; a resize2fs stand-in kills the CLI record keeps 10G, file 20G, filesystem 10G; the VM boots; a 15G request is refused; the rerun finishes at 20G
Disk holding 3 GiB of data, 10G to 40G hardware 91 ms, data intact
Metering hardware, ledger read back vm.storage.stop at the old shape and vm.storage.start at the new one, reason resize, same timestamp; a no-op resize adds no line
Restore of a snapshot taken before the resize hardware record and guest back at 2 / 4G / 10G
Ubuntu 22.04 cloud image, --storage 10G to 20G hardware, over ssh disk and qcow2 at 20G, nproc and memory as set; the root filesystem stays 10.2 GB until growpart and resize2fs run in the guest, then 20.6 GB. The first run expected cloud-init to grow it and failed on that check; the hint and docs were corrected and the case rerun
Burst above the new quota, same-size no-op, creating record unit tests covered
Windows cloud-image guest not run the qcow2 path is the same as the Ubuntu case; the in-guest step is manual
e2fsck leaving errors it cannot fix not run the resize fails before the record changes
Data disks not in scope they keep their size

tonicmuroq and others added 3 commits November 27, 2026 11:20
…ot settled

vm resize takes --cpu, --memory and --storage, each optional. --storage grows the COW disk and never shrinks it: a raw disk with its ext4 (e2fsck, resize2fs), a qcow2 by virtual size. The disk grows before the record changes, so an interrupted resize is safe to rerun.

A VM whose record or live VMM still claims it as up is refused, not only a running one: an error record left by a failed stop kept its compute interval open, and the resize changed the shape under it. The new size passes the create-time floors, the host core count and the kept cgroup knobs.

Metering closes the storage interval at the old shape and opens one at the new shape with reason resize. Resize joins the Hypervisor interface since both backends take it from Backend; ExpandQcow2 moves next to the raw COW helpers, and cliutil.FlagSize replaces three copies of the size-flag parse.
… not regrow a cloud-image disk

Hardware showed the root filesystem of an Ubuntu cloud-image guest unchanged after a --storage resize: cocoon drops the cidata disk after the first boot, so cloud-init does not run again.
@CMGS
CMGS force-pushed the vm/resize-stopped branch from 5107345 to 28de016 Compare November 27, 2026 06:21
@CMGS CMGS changed the title feat(vm): vm resize sets a stopped VM's vCPUs and memory for its next cold boot feat(vm): vm resize sets a stopped VM's vCPUs, memory and disk size for its next cold boot Nov 27, 2026

This branch has not been deployed

No deployments
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.

2 participants