From c7e2c4f8d04ad347e79d02408a99d282b32350e2 Mon Sep 17 00:00:00 2001 From: Tom Nabarro Date: Tue, 2 Jun 2026 22:22:00 +0100 Subject: [PATCH] X Signed-off-by: Tom Nabarro --- src/control/cmd/dmg/storage.go | 43 +++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/control/cmd/dmg/storage.go b/src/control/cmd/dmg/storage.go index da2802c2e8f..2c3bbbf09d9 100644 --- a/src/control/cmd/dmg/storage.go +++ b/src/control/cmd/dmg/storage.go @@ -1,6 +1,6 @@ // // (C) Copyright 2019-2023 Intel Corporation. -// (C) Copyright 2025 Hewlett Packard Enterprise Development LP +// (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP // // SPDX-License-Identifier: BSD-2-Clause-Patent // @@ -8,6 +8,7 @@ package main import ( + "context" "strings" "github.com/pkg/errors" @@ -121,9 +122,31 @@ func (cmd *storageFormatCmd) Execute(args []string) (err error) { resp, err := control.StorageFormat(ctx, cmd.ctlInvoker, req) if err != nil { + // If replace operation failed, wipe storage to prevent partial formatting. + if cmd.Replace { + cmd.Errorf("Storage format with --replace failed: %v", err) + cmd.Info("Attempting to erase partially formatted storage...") + if eraseErr := cmd.eraseStorageOnFailure(ctx); eraseErr != nil { + cmd.Errorf("Failed to erase storage after format failure: %v", eraseErr) + return errors.Wrap(err, "storage format failed and cleanup also failed") + } + cmd.Info("Storage successfully erased after format failure") + } return err } + // Check if there were any format errors in the response for replace operation. + if cmd.Replace && resp.Errors() != nil { + cmd.Errorf("Storage format with --replace encountered errors") + cmd.Info("Attempting to erase partially formatted storage...") + if eraseErr := cmd.eraseStorageOnFailure(ctx); eraseErr != nil { + cmd.Errorf("Failed to erase storage after format failure: %v", eraseErr) + // Still return the original format error. + } else { + cmd.Info("Storage successfully erased after format failure") + } + } + if cmd.JSONOutputEnabled() { return cmd.OutputJSON(resp, resp.Errors()) } @@ -131,6 +154,24 @@ func (cmd *storageFormatCmd) Execute(args []string) (err error) { return cmd.printFormatResp(resp) } +// eraseStorageOnFailure attempts to wipe storage using SystemErase API when format fails. +// This ensures that partially formatted storage doesn't leave the system in an inconsistent state. +func (cmd *storageFormatCmd) eraseStorageOnFailure(ctx context.Context) error { + cmd.Debugf("Invoking SystemErase to clean up after failed format operation") + + eraseReq := &control.SystemEraseReq{} + eraseResp, err := control.SystemErase(ctx, cmd.ctlInvoker, eraseReq) + if err != nil { + return errors.Wrap(err, "system erase") + } + + if eraseResp.Errors() != nil { + return errors.Wrap(eraseResp.Errors(), "system erase reported errors") + } + + return nil +} + func (cmd *storageFormatCmd) printFormatResp(resp *control.StorageFormatResp) error { var outErr strings.Builder if err := pretty.PrintResponseErrors(resp, &outErr); err != nil {