From 5d9c4910d6f556c29640f5f1fa171be0405f34e7 Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Sat, 5 Dec 2026 15:07:10 -0500 Subject: [PATCH 1/3] Remove a cut-off compact's file next to disks outside the data folder The startup cleanup from #151 only looked in the disks folder, but a compact writes its temporary file next to the disk, and a VM's config can point at a disk anywhere. For each VM it now also removes .compact.tmp if it is there. It checks only that exact path, so it never scans the user's other folders. In the disks folder it also catches a disk that isn't named .qcow2, which the folder scan skips. --- src/main/disks.ts | 24 +++++++++++++++++++++++- src/main/ipc.ts | 4 ++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/disks.ts b/src/main/disks.ts index dc8b0c5..0bdebc3 100644 --- a/src/main/disks.ts +++ b/src/main/disks.ts @@ -21,7 +21,7 @@ export const COMPACT_SUFFIX = '.compact.tmp' * * Only files it knows it wrote: a disk in here with no VM may well be one the user kept on * purpose, because deleting a VM leaves its disk. A disk the config points at outside this - * folder keeps its leftover until the next compact of that VM replaces it. + * folder is handled by removeUnfinishedCompacts. */ export function removeUnfinishedDisks(disksDir: string): string[] { let names: string[] @@ -42,6 +42,28 @@ export function removeUnfinishedDisks(disksDir: string): string[] { }) } +/** + * The compact leftovers removeUnfinishedDisks can't see: a compact writes its temporary file + * next to the disk, and a VM's disk can be anywhere. Looks only at the exact path a compact of + * each VM would have written, never through those folders. Call it before any VM is started: + * a compact only runs with its VM off. Returns the paths it removed. + */ +export function removeUnfinishedCompacts(configs: VmConfig[]): string[] { + const removed: string[] = [] + for (const cfg of configs) { + // in the disks folder this also catches a disk not named .qcow2, which the folder scan skips + const tmp = `${cfg.diskPath}${COMPACT_SUFFIX}` + if (!existsSync(tmp)) continue + try { + rmSync(tmp) + removed.push(tmp) + } catch { + // in use or no permission: the next compact of this VM overwrites it + } + } + return removed +} + function needOff(runner: VmRunner, cfg: VmConfig, what: string): void { if (runner.isRunning(cfg.name)) throw new Error(`Power off ${cfg.name} before you ${what}.`) } diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 05520d1..98eb764 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -5,7 +5,7 @@ import { isAbsolute } from 'node:path' import { defaultConfig, validPort, type VmConfig } from '@shared/config' import { workText, type AppSettings, type AutostartIssue, type DiskInfo, type DiskWork, type HostInfo, type PowerAction, type Profile, type SaveStateResult, type StartResult, type VmWork, type VmWorkState } from '@shared/ipc' import { agentHibernate } from './agent' -import { cloneVm, compactDisk, removeUnfinishedDisks, resizeDisk } from './disks' +import { cloneVm, compactDisk, removeUnfinishedCompacts, removeUnfinishedDisks, resizeDisk } from './disks' import { runAutostart } from './autostart' import { GuestSetupServer } from './guestSetup' import { importIso, listIsos, removeUnfinishedCopies, watchFolder } from './isos' @@ -66,7 +66,7 @@ export function registerIpc(ctx: IpcContext): Backend { if (leftovers.length) console.log(`removed unfinished ISO copies: ${leftovers.join(', ')}`) // the same for a clone or a compact the app was quit in the middle of, which can be tens of GB - const halfDisks = removeUnfinishedDisks(paths.disks) + const halfDisks = [...removeUnfinishedDisks(paths.disks), ...removeUnfinishedCompacts(listConfigs(paths))] if (halfDisks.length) console.log(`removed unfinished disk files: ${halfDisks.join(', ')}`) // new or removed ISOs show up without restarting the app From 5ce191e9ea9b5867cd4b6aca9ec6b43c552a1287 Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Sat, 5 Dec 2026 15:07:23 -0500 Subject: [PATCH 2/3] Test the compact cleanup outside the disks folder, and say what quitting leaves The Still working question said an unfinished clone or compact stays behind. Both are deleted at the next start now, so it says that. --- src/main/quitFlow.ts | 4 ++-- tests/clones.test.ts | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/quitFlow.ts b/src/main/quitFlow.ts index c9a037f..5ec39b3 100644 --- a/src/main/quitFlow.ts +++ b/src/main/quitFlow.ts @@ -63,8 +63,8 @@ const cutOff: Record = { saving: 'no profile is made and the VM is not started again', launching: 'the VM may be rolled back to the profile without being started', removing: 'the profile may not be deleted', - cloning: 'the unfinished copy of the disk stays in the disks folder', - compacting: 'the unfinished compacted copy stays next to the disk until the next compact', + cloning: 'the unfinished copy of the disk is deleted the next time the app starts', + compacting: 'the unfinished compacted copy is deleted the next time the app starts', growing: 'the disk may not get its new size' } diff --git a/tests/clones.test.ts b/tests/clones.test.ts index 702617f..65917dc 100644 --- a/tests/clones.test.ts +++ b/tests/clones.test.ts @@ -3,7 +3,7 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { defaultConfig, type PortForward } from '@shared/config' -import { CLONE_SUFFIX, COMPACT_SUFFIX, removeUnfinishedDisks, shiftForwards } from '../src/main/disks' +import { CLONE_SUFFIX, COMPACT_SUFFIX, removeUnfinishedCompacts, removeUnfinishedDisks, shiftForwards } from '../src/main/disks' import { suggestCloneName } from '../src/renderer/src/components/CloneDialog' const withFwd = (name: string, forwards: PortForward[]) => ({ ...defaultConfig(name, 'C:\\VMs\\disks'), forwards }) @@ -67,3 +67,35 @@ describe('removeUnfinishedDisks', () => { expect(removeUnfinishedDisks(join(dir, 'nope'))).toEqual([]) }) }) + +describe('removeUnfinishedCompacts', () => { + let dir: string + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'vmm-elsewhere-')) + }) + afterEach(() => rmSync(dir, { recursive: true, force: true })) + + it("removes a VM's own compact file next to a disk outside the disks folder", () => { + const disk = join(dir, 'Parrot.qcow2') + writeFileSync(disk, 'disk') + writeFileSync(`${disk}${COMPACT_SUFFIX}`, 'half a compact') + // another program's files in the same folder, and a compact file of a disk no VM uses + writeFileSync(join(dir, 'notes.txt'), 'not ours') + writeFileSync(join(dir, `other.qcow2${COMPACT_SUFFIX}`), 'no VM points here') + const cfg = { ...defaultConfig('Parrot', dir), diskPath: disk } + expect(removeUnfinishedCompacts([cfg])).toEqual([`${disk}${COMPACT_SUFFIX}`]) + expect(readdirSync(dir).sort()).toEqual(['Parrot.qcow2', 'notes.txt', `other.qcow2${COMPACT_SUFFIX}`]) + }) + + it('catches a disk not named .qcow2, which the folder scan skips', () => { + const disk = join(dir, 'legacy.img') + writeFileSync(`${disk}${COMPACT_SUFFIX}`, 'half a compact') + expect(removeUnfinishedDisks(dir)).toEqual([]) + expect(removeUnfinishedCompacts([{ ...defaultConfig('legacy', dir), diskPath: disk }])).toEqual([`${disk}${COMPACT_SUFFIX}`]) + }) + + it('does nothing when there is no leftover', () => { + const cfg = { ...defaultConfig('Kali', dir), diskPath: join(dir, 'Kali.qcow2') } + expect(removeUnfinishedCompacts([cfg])).toEqual([]) + }) +}) From 4332af5472c2a1fa47a177f8782c6e984a220e5c Mon Sep 17 00:00:00 2001 From: Ari <149393150+A21-devs@users.noreply.github.com> Date: Sat, 5 Dec 2026 15:09:26 -0500 Subject: [PATCH 3/3] Still working: don't promise a cut-off linked clone is harmless Only a full clone writes a separate file the startup cleanup removes. A linked clone stopped between its rename and its overlays leaves the source VM without a disk (#156), so say so. --- src/main/quitFlow.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/quitFlow.ts b/src/main/quitFlow.ts index 5ec39b3..231ce6b 100644 --- a/src/main/quitFlow.ts +++ b/src/main/quitFlow.ts @@ -63,7 +63,7 @@ const cutOff: Record = { saving: 'no profile is made and the VM is not started again', launching: 'the VM may be rolled back to the profile without being started', removing: 'the profile may not be deleted', - cloning: 'the unfinished copy of the disk is deleted the next time the app starts', + cloning: 'an unfinished full copy of the disk is deleted the next time the app starts, but a linked clone cut off while it sets up can leave the VM without its disk', compacting: 'the unfinished compacted copy is deleted the next time the app starts', growing: 'the disk may not get its new size' }