Skip to content

Put right a linked clone the app was stopped in the middle of - #164

Merged
A21-devs merged 3 commits into
mainfrom
fix/linked-clone-recovery
Dec 5, 2026
Merged

A21-devs merged 3 commits into
mainfrom
fix/linked-clone-recovery

Conversation

@A21-devs

@A21-devs A21-devs commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Closes #156

A linked clone renames the source's disk to <name>.base-<id>.qcow2 before it creates the two overlays. If the app stops in between (Quit anyway, a crash, power loss), the source VM's config points at a file that isn't there, and the next Power on creates a blank disk at that path.

How

  • Before the rename, cloneVm writes a small JSON marker in the disks folder, <base>.vmm-linking, naming the source disk, the base and the new overlay. The disks folder is used even when the source disk lives elsewhere, so startup only has to look in one place.
  • The marker is removed only after the new VM's config is saved. For that, cloneVm now takes the save as a callback (the IPC handler passes saveConfig) instead of saving after it returns. The default just returns the config, so the integration tests' calls are unchanged.
  • At startup recoverLinkedClones reads each marker:
    • source disk missing: move the base back, remove the marker. The clone never happened.
    • source overlay present: the source works on top of the base, so it makes the base read-only like a finished clone does, and removes the new overlay if no VM's config uses it.
    • a marker it can't parse is left alone. If a move fails (file in use), the marker stays and the next start tries again.
  • If the clone fails in-process and the original can't be moved back (the case that already had its own error message), the marker now stays too, so the next start retries the move.
  • The Still working question can now say a cut-off clone is undone at the next start, for both kinds.

Testing

  • 4 new tests in tests/clones.test.ts with a fake qemu-img whose overlay never finishes, the way a quit leaves it: cut off before the overlays (disk moved back), cut off before the config save (base made read-only, unsaved overlay removed), a finished clone (marker present during the save and gone after, recovery does nothing), and an unreadable marker / missing folder
  • npm run typecheck, npm test (199 pass), npm run test:qemu (216 pass, includes real linked clones that boot), npm run build
  • Hidden Electron smoke run on a temp data folder with a VM whose disk was missing next to a .base-abc.qcow2 and its marker: the log says put right unfinished linked clones: moved ...Kali.base-abc.qcow2 back to ...Kali.qcow2, the disk is back with its contents, SMOKE OK

A linked clone moves the source's disk to the shared base before it makes
the two overlays. If the app stops in between, the source VM points at a
disk that isn't there, and its next Power on makes a blank one while the
real data sits in the .base file.

The clone now writes <base>.vmm-linking in the disks folder first, naming
the source disk, the base and the new overlay, and removes it once the
new VM's config is saved. To get that order, cloneVm takes the save as a
callback instead of the IPC handler saving after it returns.

At startup, recoverLinkedClones reads each marker. With the source disk
missing it moves the base back. With the source overlay in place the
clone is usable, so it makes the base read-only like a finished clone and
removes the new overlay if no VM was saved with it. A marker it can't act
on yet (base in use) stays for the next start.
Uses a fake qemu-img that never finishes, the way a quit leaves it, and
checks what the next start puts right. The Still working question can
now say a cut-off clone of either kind is undone at the next start.
@A21-devs A21-devs added bug Something isn't working backend labels Dec 5, 2026

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read the marker format, the save callback ordering and the recovery branches. The ordering is right: marker before the rename, removed only after saveConfig, and the in-process failure path keeps it when it can't move the disk back. One gap in how recovery decides the clone got far enough.

Comment thread src/main/disks.ts
}
tryRemove(m.target) // an overlay is only made after the source's, so this is a no-op
} else {
if (existsSync(m.base)) chmodSync(m.base, 0o444)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch takes "a file exists at diskPath" to mean the source overlay is good. But qemu-img create writes that file in place, so if the app (and with it the qemu-img child) is killed while it runs, diskPath can hold a zero-length or half-written qcow2. Recovery then keeps it, locks the base and drops the marker, and the source VM fails to boot with its real data sitting in the base, which is the same situation this PR is fixing, only rarer.

Cheapest way out is in cloneVm: create each overlay under a temporary name and renameSync it into place. A rename is atomic on the same volume, so a file at diskPath is then always a finished overlay. The overlay stores the base's path, not its own, so renaming it is safe.

@A21-devs

A21-devs commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner Author

Good catch, that was real. 1c2d9d0 creates each overlay as <file>.vmm-clone and renames it into place, so a file at the source's disk path is always a whole overlay. Recovery now also removes the temporary overlay for both paths in a marker (the disks folder scan would get the new VM's, but not the source's when its disk lives elsewhere), and the in-process failure path removes them too.

Two new tests make the fake qemu-img leave a half-written file and hang, once on the source overlay and once on the new one, and check the next start ends with only the original disk, or the source overlay plus the locked base. npm run typecheck, npm test (201 pass), npm run test:qemu (218 pass, real linked clones that boot after the rename), npm run build.

qemu-img create writes the overlay in place, so a quit while it runs can
leave half a qcow2 at the source's disk path, which the startup recovery
would take for a finished overlay and keep. Each overlay is now created
as <file>.vmm-clone and renamed into place, and the recovery removes any
temporary overlay a marker names.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the follow-up against the first review. Fixed: overlay() creates <file>.vmm-clone and renames it into place, so existsSync(m.diskPath) in the recovery now really means a finished overlay. The startup order works out too: removeUnfinishedDisks runs first and takes any temporary overlay inside the disks folder, and the recovery's own tryRemove covers a source disk that lives elsewhere. The two hang tests leave a half-written file the way a killed qemu-img would, and both end with the right files. Nothing else open.

@A21-devs
A21-devs merged commit 0bba4b9 into main Dec 5, 2026
1 check passed
@A21-devs
A21-devs deleted the fix/linked-clone-recovery branch December 5, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A linked clone cut off mid-way leaves the source VM without a disk

1 participant