Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/vm/clonehints.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func printPostCloneHints(vm *types.VM) {
fmt.Println()
fmt.Println(` netsh interface ipv4 set address name="<ADAPTER>" static <IP> <MASK> <GATEWAY>`)
fmt.Println()
fmt.Println("The guest resumes at the snapshot's date and time. Set them inside the guest:")
fmt.Println()
fmt.Println(` powershell -nop -c "Set-Date '<DATE TIME>'"`)
fmt.Println()
return
}

Expand Down
10 changes: 10 additions & 0 deletions cmd/vm/clonehints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ func TestPostCloneHintsGateBalloonRelease(t *testing.T) {
})
}
}

func TestPostCloneHintsTellAWindowsGuestToSetItsClock(t *testing.T) {
for _, windows := range []bool{true, false} {
vm := &types.VM{Hypervisor: "cloud-hypervisor", Config: types.VMConfig{Name: "c", Memory: 4 << 30, Windows: windows, ImageType: types.ImageTypeCloudImg}}
out := clitest.CaptureStdout(t, func() { printPostCloneHints(vm) })
if got := strings.Contains(out, "Set-Date"); got != windows {
t.Errorf("windows=%v: Set-Date hint = %v (output: %q)", windows, got, out)
}
}
}
8 changes: 8 additions & 0 deletions docs/windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ Pre-built images are published as split OCI artifacts and imported with `oras pu
- **DHCP networks**: the clone comes up on hot-swapped NICs with new MACs; if a NIC is left without IPv4, run the `Get-PnpDevice` rebind that `cocoon vm clone` prints
- **Static IP**: after the rebind, run the printed `netsh interface ipv4 set address ...` inside the guest (SAC serial console via `cocoon vm console`). Windows keeps a static IPv4 setting per PCI slot, so a clone NIC that lands in the source's slot re-applies the source's address and collides with any running VM that holds it; prefer DHCP for clone sources, or reset the adapter's settings in the clone

### Clock After Clone and Restore

`cocoon vm clone` and `cocoon vm restore` resume a Windows guest from the snapshot's clock (`clock_restore_mode=Preserve` on Cloud Hypervisor's restore). The guest's uptime continues from the snapshot, and its date and time are behind by the snapshot's age until something sets them in the guest, for example `Set-Date` over `cocoon vm exec`; `vm clone` prints the command. A hibernated Windows VM wakes the same way, behind by the time it slept.

Cloud Hypervisor's default advances the clock by the snapshot's age, which moves a Windows guest's uptime forward by as much. A jump of 2^31 to 2^32 ms (24.9 to 49.7 days) can leave the guest unable to retransmit TCP ([cloud-hypervisor#9](https://github.com/cocoonstack/cloud-hypervisor/issues/9)), and a snapshot taken from such a guest hands the fault to its clones. Linux guests keep the default, and so does a `vm migrate` receive, where the gap is the seconds the handoff took.

This needs a Cloud Hypervisor that knows `clock_restore_mode`: upstream from 2026-11-23, the cocoonstack `dev` build from v56.0.0-dev-5fd961f7. An older one ignores the field and advances the clock.

For more details, see the [Cloud Hypervisor Windows documentation](https://github.com/cloud-hypervisor/cloud-hypervisor/blob/main/docs/windows.md).
2 changes: 1 addition & 1 deletion hypervisor/cloudhypervisor/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (ch *CloudHypervisor) restoreAndResumeClone(ctx context.Context, pid int, s

hc := utils.NewSocketHTTPClient(sockPath)

if err = restoreVM(ctx, hc, runDir, opts.vmCfg.RestoreMode); err != nil {
if err = restoreVM(ctx, hc, runDir, opts.vmCfg.RestoreMode, restoreClockMode(opts.vmCfg, opts.keepPaused)); err != nil {
return fmt.Errorf("vm.restore: %w", err)
}

Expand Down
41 changes: 41 additions & 0 deletions hypervisor/cloudhypervisor/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,47 @@ func TestCloneBlobIDsCloudimgWithoutBackingKeepsTheReservation(t *testing.T) {
}
}

func TestRestoreAndResumeCloneKeepsAWindowsGuestOnTheSnapshotClock(t *testing.T) {
tests := []struct {
name string
windows bool
keepPaused bool
want any
}{
{name: "a linux clone advances"},
{name: "a windows clone preserves", windows: true, want: "Preserve"},
{name: "a windows migration receive advances", windows: true, keepPaused: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
mu sync.Mutex
body map[string]any
)
sock := serveCHHandler(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if filepath.Base(r.URL.Path) == "vm.restore" {
mu.Lock()
_ = json.UnmarshalRead(r.Body, &body)
mu.Unlock()
}
w.WriteHeader(http.StatusNoContent)
}))
restoreTestClone(t, sock, &cloneResumeOpts{
vmID: "T",
vmCfg: &types.VMConfig{CPU: 2, Windows: tt.windows},
directBoot: true,
snapshotCfg: &chVMConfig{},
keepPaused: tt.keepPaused,
})
mu.Lock()
defer mu.Unlock()
if got := body["clock_restore_mode"]; got != tt.want {
t.Errorf("vm.restore clock_restore_mode = %v, want %v (body %v)", got, tt.want, body)
}
})
}
}

func TestRestoreAndResumeCloneHotplugsByRoleWithQueueCPUs(t *testing.T) {
var (
mu sync.Mutex
Expand Down
2 changes: 1 addition & 1 deletion hypervisor/cloudhypervisor/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (ch *CloudHypervisor) restoreAfterExtract(ctx context.Context, vmID string,

hc := utils.NewSocketHTTPClient(sockPath)

if err = restoreVM(ctx, hc, rec.RunDir, vmCfg.RestoreMode); err != nil {
if err = restoreVM(ctx, hc, rec.RunDir, vmCfg.RestoreMode, restoreClockMode(vmCfg, false)); err != nil {
return nil, fmt.Errorf("vm.restore: %w", err)
}
if err = ch.ArmCPUQuota(vmID, &vmCfg.Config); err != nil {
Expand Down
19 changes: 17 additions & 2 deletions hypervisor/cloudhypervisor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ const (
chMemoryRestoreOnDemand chMemoryRestoreMode = "OnDemand"
// chMemoryRestoreMmap maps the snapshot file copy-on-write, sharing page cache across clones of one snapshot.
chMemoryRestoreMmap chMemoryRestoreMode = "CopyOnWrite"

// chClockPreserve resumes the guest from the snapshot's clock; CH's default advances it by the snapshot's age.
chClockPreserve chClockRestoreMode = "Preserve"
)

var runtimeFiles = []string{hypervisor.APISocketName, pidFileName, hypervisor.ConsoleSockName, hypervisor.ConsolePTYName, cmdlineFileName, hypervisor.VsockSockName, snapDiskPrefix + "*"}

type chMemoryRestoreMode string

type chClockRestoreMode string

type chSnapshotConfig struct {
DestinationURL string `json:"destination_url"`
SnapshotType string `json:"snapshot_type,omitempty"`
Expand All @@ -56,6 +61,7 @@ type chSnapshotConfig struct {
type chRestoreConfig struct {
SourceURL string `json:"source_url"`
MemoryRestoreMode chMemoryRestoreMode `json:"memory_restore_mode,omitempty"`
ClockRestoreMode chClockRestoreMode `json:"clock_restore_mode,omitempty"`
}

func (ch *CloudHypervisor) saveCmdline(ctx context.Context, rec *hypervisor.VMRecord, args []string) {
Expand Down Expand Up @@ -146,11 +152,12 @@ func endSnapshotSeries(ctx context.Context, hc *http.Client) error {
return err
}

func restoreVM(ctx context.Context, hc *http.Client, sourceDir, restoreMode string) error {
func restoreVM(ctx context.Context, hc *http.Client, sourceDir, restoreMode string, clock chClockRestoreMode) error {
hc.Timeout = hypervisor.VMMemTransferTimeout
defer func() { hc.Timeout = utils.HTTPTimeout }()
cfg := chRestoreConfig{
SourceURL: "file://" + sourceDir,
SourceURL: "file://" + sourceDir,
ClockRestoreMode: clock,
}
switch restoreMode {
case "", restoreModeCopy: // eager copy is CH's default; leave MemoryRestoreMode unset
Expand All @@ -164,6 +171,14 @@ func restoreVM(ctx context.Context, hc *http.Client, sourceDir, restoreMode stri
return vmPutJSON(ctx, hc, "vm.restore", "restore request", cfg, http.StatusNoContent)
}

// restoreClockMode keeps a Windows guest on the snapshot's clock: an uptime jump of 2^31 to 2^32 ms can stop its TCP retransmits. A migration receive resumes seconds after the pause, so it advances.
func restoreClockMode(vmCfg *types.VMConfig, migrationReceive bool) chClockRestoreMode {
if vmCfg.Windows && !migrationReceive {
return chClockPreserve
}
return ""
}

func addDiskVM(ctx context.Context, hc *http.Client, disk chDisk) error {
return vmPutJSON(ctx, hc, "vm.add-disk", "add-disk request", disk, http.StatusOK, http.StatusNoContent)
}
Expand Down