257 lines
9.4 KiB
TypeScript
257 lines
9.4 KiB
TypeScript
import { Copy } from 'lucide-react'
|
|
import React, { useEffect, useRef, useState } from 'react'
|
|
import { I18nKey, parseAppError, t } from '../../i18n'
|
|
import { formatBytes } from '../../lib/format'
|
|
import { DiskCompactResult, DiskResizeResult, Locale, VMStats } from '../../types'
|
|
import { Button, IconButton } from '../../ui/controls'
|
|
import { SetRow } from '../../ui/settings'
|
|
import { api } from '../../wails'
|
|
|
|
interface DiskOptions {
|
|
locale: Locale
|
|
driverID: string
|
|
vm: string
|
|
diskPath?: string
|
|
running: boolean
|
|
stats?: VMStats | null
|
|
onError: (message: string) => void
|
|
onDiskChanged?: () => void
|
|
}
|
|
|
|
function isWindowsRawDevicePath(disk?: string): boolean {
|
|
const s = (disk ?? '').trim().replace(/\//g, '\\')
|
|
return /^\\\\\.\\(physicaldrive|harddiskvolume|harddisk|volume\{)|^\\\\\?\\volume\{/i.test(s)
|
|
}
|
|
|
|
/** Resizing, compacting and optimizing the VM's disk, one at a time. */
|
|
export function useDiskMaintenance({ locale, driverID, vm, diskPath, running, stats, onError, onDiskChanged }: DiskOptions) {
|
|
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
|
const activeVMNameRef = useRef(vm)
|
|
activeVMNameRef.current = vm
|
|
const [resizeInput, setResizeInput] = useState('+64G')
|
|
const [diskOpBusy, setDiskOpBusy] = useState<'resize' | 'compact' | 'optimize' | ''>('')
|
|
const [resizeResult, setResizeResult] = useState<DiskResizeResult | null>(null)
|
|
const [compactResult, setCompactResult] = useState<DiskCompactResult | null>(null)
|
|
const [optimizeResult, setOptimizeResult] = useState<DiskCompactResult | null>(null)
|
|
|
|
useEffect(() => {
|
|
setResizeResult(null)
|
|
setCompactResult(null)
|
|
setOptimizeResult(null)
|
|
}, [driverID, vm])
|
|
|
|
const handleResizeDisk = async () => {
|
|
if (running || diskOpBusy || !resizeInput.trim()) return
|
|
onError('')
|
|
setDiskOpBusy('resize')
|
|
try {
|
|
const res = await api.ResizeVMDisk(driverID, vm, resizeInput.trim())
|
|
if (activeVMNameRef.current === vm) {
|
|
setResizeResult(res)
|
|
}
|
|
onDiskChanged?.()
|
|
} catch (e) {
|
|
if (activeVMNameRef.current === vm) {
|
|
onError(parseAppError(e).summary)
|
|
}
|
|
} finally {
|
|
setDiskOpBusy('')
|
|
}
|
|
}
|
|
|
|
const isRawDevice = isWindowsRawDevicePath(diskPath)
|
|
const resizeDisabled = running || isRawDevice
|
|
const resizeReason = isRawDevice ? tr('diskResizeRawDisabled') : tr('stopBeforeDiskOp')
|
|
const isQcow2 = !isRawDevice && (!stats?.disk_format || stats.disk_format === 'qcow2')
|
|
const diskRewriteBaseReason = running ? tr('stopBeforeDiskOp')
|
|
: (stats?.disk_snapshot_count ?? 0) > 0 ? tr('diskRewriteSnapshots')
|
|
: (stats?.disk_backing_depth ?? 0) > 0 ? tr('diskRewriteOverlay')
|
|
: !isQcow2 ? tr('diskCompactQcow2Only')
|
|
: undefined
|
|
const compactRowReason = diskRewriteBaseReason ?? (diskOpBusy && diskOpBusy !== 'compact' ? tr('diskRewriteBusy') : undefined)
|
|
const optimizeRowReason = diskRewriteBaseReason ?? (diskOpBusy && diskOpBusy !== 'optimize' ? tr('diskRewriteBusy') : undefined)
|
|
|
|
const handleCompactDisk = async (optimize: boolean) => {
|
|
if (diskRewriteBaseReason || diskOpBusy) return
|
|
onError('')
|
|
setDiskOpBusy(optimize ? 'optimize' : 'compact')
|
|
try {
|
|
if (optimize) {
|
|
const res = await api.OptimizeVMDisk(driverID, vm)
|
|
if (activeVMNameRef.current === vm) {
|
|
setOptimizeResult(res)
|
|
setCompactResult(null)
|
|
}
|
|
} else {
|
|
const res = await api.CompactVMDisk(driverID, vm)
|
|
if (activeVMNameRef.current === vm) {
|
|
setCompactResult(res)
|
|
}
|
|
}
|
|
onDiskChanged?.()
|
|
} catch (e) {
|
|
if (activeVMNameRef.current === vm) {
|
|
onError(parseAppError(e).summary)
|
|
}
|
|
} finally {
|
|
setDiskOpBusy('')
|
|
}
|
|
}
|
|
|
|
const extendedL2 = stats?.disk_extended_l2 || optimizeResult?.extended_l2_enabled
|
|
|
|
return {
|
|
resizeInput, setResizeInput, diskOpBusy, resizeResult, compactResult, optimizeResult, extendedL2,
|
|
resizeDisabled, resizeReason, compactRowReason, optimizeRowReason, handleResizeDisk, handleCompactDisk,
|
|
}
|
|
}
|
|
|
|
interface RowsProps {
|
|
locale: Locale
|
|
running: boolean
|
|
state: ReturnType<typeof useDiskMaintenance>
|
|
onCopy?: (text: string) => void
|
|
}
|
|
|
|
export const DiskMaintenanceRows: React.FC<RowsProps> = ({ locale, running: _running, state, onCopy }) => {
|
|
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
|
const { resizeInput, setResizeInput, diskOpBusy, resizeResult, compactResult, optimizeResult, extendedL2, resizeDisabled, resizeReason, compactRowReason, optimizeRowReason, handleResizeDisk, handleCompactDisk } = state
|
|
return (
|
|
<>
|
|
<SetRow
|
|
title={tr('diskResizeTitle')}
|
|
desc={tr('diskResizeDesc')}
|
|
disabled={resizeDisabled}
|
|
reason={resizeReason}
|
|
block={
|
|
<div className="stack-2">
|
|
<div className="hstack">
|
|
<div className="presets">
|
|
{['+32G', '+64G', '+128G', '256G', '512G'].map((v) => (
|
|
<button
|
|
key={v}
|
|
type="button"
|
|
className="preset"
|
|
aria-pressed={resizeInput === v}
|
|
disabled={resizeDisabled || !!diskOpBusy}
|
|
onClick={() => setResizeInput(v)}
|
|
>
|
|
{v}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
<div className="hstack">
|
|
<input
|
|
className="input rg-p-input mono input-compact"
|
|
aria-label={tr('diskResizeTitle')}
|
|
value={resizeInput}
|
|
spellCheck={false}
|
|
autoComplete="off"
|
|
disabled={resizeDisabled || !!diskOpBusy}
|
|
onChange={(e) => setResizeInput(e.target.value)}
|
|
placeholder="+64G / 256G"
|
|
/>
|
|
<Button
|
|
variant="secondary"
|
|
busy={diskOpBusy === 'resize'}
|
|
disabled={resizeDisabled || !!diskOpBusy || !resizeInput.trim()}
|
|
onClick={handleResizeDisk}
|
|
>
|
|
{tr('diskResizeAction')}
|
|
</Button>
|
|
</div>
|
|
{resizeResult && (
|
|
<div className="notice">
|
|
<div>{tr('diskResizeDone', { size: formatBytes(resizeResult.new_virtual_bytes, locale) })}</div>
|
|
<div className="cmd-line cmd-line-spaced">
|
|
<code>{resizeResult.guest_expand_cmd}</code>
|
|
{onCopy && (
|
|
<IconButton small label={tr('copy')} icon={<Copy size={14} />} onClick={() => onCopy(resizeResult.guest_expand_cmd)} />
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
}
|
|
/>
|
|
<SetRow
|
|
title={tr('diskCompactTitle')}
|
|
desc={compactResult
|
|
? compactResult.reclaimed_bytes > 0
|
|
? tr('diskCompactDone', {
|
|
before: formatBytes(compactResult.before_bytes, locale),
|
|
after: formatBytes(compactResult.after_bytes, locale),
|
|
reclaimed: formatBytes(compactResult.reclaimed_bytes, locale),
|
|
})
|
|
: tr('diskCompactNoGain')
|
|
: tr('diskCompactDesc')}
|
|
disabled={!!compactRowReason}
|
|
reason={compactRowReason}
|
|
control={
|
|
<Button
|
|
variant="secondary"
|
|
busy={diskOpBusy === 'compact'}
|
|
disabled={!!compactRowReason || !!diskOpBusy}
|
|
onClick={() => handleCompactDisk(false)}
|
|
>
|
|
{tr('diskCompactAction')}
|
|
</Button>
|
|
}
|
|
/>
|
|
<SetRow
|
|
title={tr('diskOptimizeTitle')}
|
|
desc={optimizeResult ? tr('diskOptimizeDone', {
|
|
before: formatBytes(optimizeResult.before_bytes, locale),
|
|
after: formatBytes(optimizeResult.after_bytes, locale),
|
|
reclaimed: formatBytes(optimizeResult.reclaimed_bytes, locale),
|
|
status: tr(extendedL2 ? 'diskOptimizeEnabled' : 'diskOptimizeInactive'),
|
|
}) : tr(extendedL2 ? 'diskOptimizeEnabled' : 'diskOptimizeDesc')}
|
|
disabled={!extendedL2 && !!optimizeRowReason}
|
|
reason={extendedL2 ? undefined : optimizeRowReason}
|
|
control={extendedL2 ? undefined : (
|
|
<Button variant="secondary" busy={diskOpBusy === 'optimize'} disabled={!!optimizeRowReason || !!diskOpBusy} onClick={() => handleCompactDisk(true)}>
|
|
{tr('diskOptimizeAction')}
|
|
</Button>
|
|
)}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
interface DefenderProps {
|
|
locale: Locale
|
|
dataRoot: string
|
|
onCopy?: (text: string) => void
|
|
onError: (message: string) => void
|
|
}
|
|
|
|
export const DefenderExclusionRow: React.FC<DefenderProps> = ({ locale, dataRoot, onCopy, onError }) => {
|
|
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
|
const handleDefender = async (copy: boolean) => {
|
|
onError('')
|
|
try {
|
|
if (copy) {
|
|
const command = await api.GetDefenderExclusionCommand()
|
|
if (command) onCopy?.(command)
|
|
} else {
|
|
await api.OpenWindowsSecurityExclusions()
|
|
}
|
|
} catch (e) {
|
|
onError(parseAppError(e).summary)
|
|
}
|
|
}
|
|
return (
|
|
<SetRow
|
|
title={tr('defenderExclusionTitle')}
|
|
desc={tr('defenderExclusionDesc', { path: dataRoot })}
|
|
control={
|
|
<div className="hstack">
|
|
<Button variant="secondary" onClick={() => handleDefender(false)}>{tr('defenderOpenSecurity')}</Button>
|
|
{onCopy && <Button variant="secondary" onClick={() => handleDefender(true)}>{tr('defenderCopyCommand')}</Button>}
|
|
</div>
|
|
}
|
|
/>
|
|
)
|
|
}
|