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
24 changes: 23 additions & 1 deletion src/main/disks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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}.`)
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/quitFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ const cutOff: Record<VmWork, string> = {
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: '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'
}

Expand Down
34 changes: 33 additions & 1 deletion tests/clones.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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([])
})
})
Loading