diff --git a/.limetech/ai-review-markers/codex-u8-2360-onboarding-storage-reasons-8f54c2fa86a1.json b/.limetech/ai-review-markers/codex-u8-2360-onboarding-storage-reasons-8f54c2fa86a1.json new file mode 100644 index 0000000000..a5a1d13ef9 --- /dev/null +++ b/.limetech/ai-review-markers/codex-u8-2360-onboarding-storage-reasons-8f54c2fa86a1.json @@ -0,0 +1,8 @@ +{ + "disposition": "PATCH", + "forecast_commit": "b46f7f3d59cb56c3fa0c69297b43842f3df278b7", + "reviewed_sha": "cee9cad88b7be35ebc330506254d1c0016fda56a", + "schema": "limetech.ai-review-marker.v2", + "stage": "final", + "unresolved_proportionality_findings": [] +} diff --git a/api/src/core/utils/clients/emcmd.spec.ts b/api/src/core/utils/clients/emcmd.spec.ts index d1dfac5ca9..256856b501 100644 --- a/api/src/core/utils/clients/emcmd.spec.ts +++ b/api/src/core/utils/clients/emcmd.spec.ts @@ -82,6 +82,36 @@ describe('emcmd', () => { ); }); + it.each([ + [ + 'create-pool-storage-active', + { cmdCreatePool: 'apply', poolName: 'cache' }, + 'Stop storage before creating pool "cache".', + ], + [ + 'make-bootable-size', + { cmdMakeBootable: 'apply', poolName: 'cache' }, + 'Check the reserved boot size and available drive space for pool "cache" (storage slot 30).', + ], + [ + 'make-bootable-size', + { cmdCreatePool: 'apply', poolName: 'cache' }, + 'emcmd request failed with status 500', + ], + [ + 'unrecognized', + { cmdMakeBootable: 'apply', poolName: 'cache' }, + 'emcmd request failed with status 500', + ], + ] as const)('renders the matching native failure %s', async (result, command, expected) => { + vi.mocked(got.post).mockResolvedValue({ + body: 'private daemon diagnostic', + statusCode: 500, + headers: { 'x-unraid-command-result': result, 'x-unraid-storage-slot': '30' }, + } as unknown as Awaited>); + await expect(emcmd(command)).rejects.toMatchObject({ message: expected }); + }); + it('throws on http failures reported by got', async () => { vi.mocked(got.post).mockResolvedValue({ body: '', diff --git a/api/src/core/utils/clients/emcmd.ts b/api/src/core/utils/clients/emcmd.ts index 3e8ac14d8b..99242143c2 100644 --- a/api/src/core/utils/clients/emcmd.ts +++ b/api/src/core/utils/clients/emcmd.ts @@ -7,6 +7,7 @@ import retry from 'p-retry'; import { AppError } from '@app/core/errors/app-error.js'; import { appLogger } from '@app/core/log.js'; import { LooseObject } from '@app/core/types/global.js'; +import { StorageCommandError } from '@app/core/utils/clients/storage-command-error.js'; import { store } from '@app/store/index.js'; import { loadSingleStateFile } from '@app/store/modules/emhttp.js'; import { StateFileKey } from '@app/store/types.js'; @@ -121,6 +122,14 @@ export const emcmd = async ( throwHttpErrors: false, }); + if (response.statusCode === 500) { + const failure = StorageCommandError.fromReply( + commands, + response.headers?.['x-unraid-command-result'], + response.headers?.['x-unraid-storage-slot'] + ); + if (failure) throw failure; + } if (response.statusCode >= 400) { throw new Error(`emcmd request failed with status ${response.statusCode}`); } diff --git a/api/src/core/utils/clients/storage-command-error.ts b/api/src/core/utils/clients/storage-command-error.ts new file mode 100644 index 0000000000..791fbaeda8 --- /dev/null +++ b/api/src/core/utils/clients/storage-command-error.ts @@ -0,0 +1,75 @@ +/** Only a known native result for the command submitted may enter onboarding output. */ +export class StorageCommandError extends Error { + static fromReply( + commands: Record, + result: unknown, + slot: unknown + ): StorageCommandError | undefined { + const pool = commands.poolName; + const target = + typeof pool === 'string' && /^[a-z][a-z0-9_-]{0,39}$/.test(pool) + ? `pool "${pool}"` + : 'the selected pool'; + + if (commands.cmdCreatePool === 'apply') { + switch (result) { + case 'create-pool-storage-active': + return new StorageCommandError(`Stop storage before creating ${target}.`); + case 'create-pool-invalid': + return new StorageCommandError(`Check the name and slot count for ${target}.`); + case 'create-pool-failed': + return new StorageCommandError( + `Could not create ${target}. Check the boot device and pool configuration.` + ); + case 'create-pool-refresh-failed': + return new StorageCommandError( + `Storage could not refresh after creating ${target}. Reload Storage and check the pool before retrying.` + ); + } + } + + if (commands.cmdMakeBootable === 'apply') { + const device = + typeof slot === 'string' && /^(0|[1-9]\d{0,3})$/.test(slot) && Number(slot) <= 4094 + ? `${target} (storage slot ${slot})` + : target; + switch (result) { + case 'make-bootable-storage-active': + return new StorageCommandError(`Stop storage before making ${device} bootable.`); + case 'make-bootable-invalid': + return new StorageCommandError( + `Check the boot pool and reserved size for ${device}.` + ); + case 'make-bootable-ineligible': + return new StorageCommandError( + `Check boot eligibility and the assigned drives for ${device}.` + ); + case 'make-bootable-size': + return new StorageCommandError( + `Check the reserved boot size and available drive space for ${device}.` + ); + case 'make-bootable-target-missing': + return new StorageCommandError( + `The selected drive for ${device} disappeared before boot setup began. Check its connection and assignment.` + ); + case 'make-bootable-partial': + return new StorageCommandError( + `Boot setup may have changed a drive in ${device}. Check the drive partitions, boot pool and pending transfer before retrying.` + ); + case 'make-bootable-transfer-pending': + return new StorageCommandError( + `A boot transfer is already pending. Check the existing boot setup before changing ${device}.` + ); + case 'make-bootable-refresh-failed': + return new StorageCommandError( + `Storage could not refresh after boot setup for ${device}. Reload Storage and check the boot configuration before retrying.` + ); + case 'make-bootable-failed': + return new StorageCommandError( + `Boot setup did not finish for ${device}. Check the drives and boot configuration before retrying.` + ); + } + } + return undefined; + } +} diff --git a/api/src/unraid-api/graph/resolvers/onboarding/onboarding-internal-boot.service.spec.ts b/api/src/unraid-api/graph/resolvers/onboarding/onboarding-internal-boot.service.spec.ts index 26643223a0..9381d0f595 100644 --- a/api/src/unraid-api/graph/resolvers/onboarding/onboarding-internal-boot.service.spec.ts +++ b/api/src/unraid-api/graph/resolvers/onboarding/onboarding-internal-boot.service.spec.ts @@ -4,6 +4,7 @@ import { execa } from 'execa'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { emcmd } from '@app/core/utils/clients/emcmd.js'; +import { StorageCommandError } from '@app/core/utils/clients/storage-command-error.js'; import { getShares } from '@app/core/utils/shares/get-shares.js'; import { getters } from '@app/store/index.js'; import { loadStateFileSync } from '@app/store/services/state-file-loader.js'; @@ -471,4 +472,23 @@ describe('OnboardingInternalBootService', () => { expect(result.output).toContain('socket failure'); expect(internalBootStateService.invalidateCachedInternalBootDeviceState).not.toHaveBeenCalled(); }); + it('returns only the actionable native reason after a boot setup failure', async () => { + const service = createService(); + vi.mocked(emcmd).mockResolvedValueOnce({} as Awaited>); + vi.mocked(emcmd).mockRejectedValueOnce( + new StorageCommandError('Stop storage before creating pool "cache".') + ); + const result = await service.createInternalBootPool({ + poolName: 'cache', + devices: ['disk-1'], + bootSizeMiB: 8192, + updateBios: false, + }); + expect(result).toEqual({ + ok: false, + code: 1, + output: 'Stop storage before creating pool "cache".', + }); + expect(vi.mocked(emcmd)).toHaveBeenCalledTimes(2); + }); }); diff --git a/api/src/unraid-api/graph/resolvers/onboarding/onboarding-internal-boot.service.ts b/api/src/unraid-api/graph/resolvers/onboarding/onboarding-internal-boot.service.ts index e902a28ab5..5db5f70a7b 100644 --- a/api/src/unraid-api/graph/resolvers/onboarding/onboarding-internal-boot.service.ts +++ b/api/src/unraid-api/graph/resolvers/onboarding/onboarding-internal-boot.service.ts @@ -10,6 +10,7 @@ import type { OnboardingInternalBootResult, } from '@app/unraid-api/graph/resolvers/onboarding/onboarding.model.js'; import { emcmd } from '@app/core/utils/clients/emcmd.js'; +import { StorageCommandError } from '@app/core/utils/clients/storage-command-error.js'; import { withTimeout } from '@app/core/utils/misc/with-timeout.js'; import { getShares } from '@app/core/utils/shares/get-shares.js'; import { getters } from '@app/store/index.js'; @@ -605,6 +606,9 @@ export class OnboardingInternalBootService { } catch (error) { const message = error instanceof Error ? error.message : String(error); this.logger.warn(`createInternalBootPool failed with error='${message}'`); + if (error instanceof StorageCommandError) { + return { ok: false, code: 1, output: error.message }; + } output.push('mkbootpool: command failed or timed out'); output.push(message); return {