NVMe Secure Erase in Linux Using nvme-cli
This article provides an overview of how the Linux operating system
handles NVMe secure erase operations using the native
nvme-cli utility. It details the interaction between
user-space commands, the Linux kernel, and the NVMe hardware controller.
Readers will learn the core mechanisms behind NVMe formatting and
sanitization, the distinction between cryptographic and user data
erasures, and the practical steps required to safely execute these
operations on Linux.
The Architecture: How Linux Interacts with NVMe Hardware
When you manage an NVMe SSD in Linux, the operating system exposes
the drive through character and block device nodes in
/dev/:
/dev/nvmeX: The controller character device, used for administrative actions that affect the hardware controller./dev/nvmeXnY: The block device representing a specific namespace (storage partition space) on that controller.
The nvme-cli package is a user-space management tool.
When a command like format or sanitize is run, nvme-cli
does not manually overwrite sectors one by one as legacy tools (like
dd) do. Instead, it constructs standard NVMe specification
commands and delivers them to the Linux NVMe kernel driver using the
ioctl() system call (specifically via
NVME_IOCTL_ADMIN_CMD).
The kernel driver forwards these commands across the PCIe bus directly to the NVMe SSD's onboard controller. The SSD controller processes the operation at the firmware level, managing the underlying NAND flash cells independently of system CPU and RAM.
Secure Erase Methods in NVMe
The NVMe specification provides two primary mechanisms for securely erasing data: Format NVM and Sanitize.
1. Format NVM
(nvme format)
The Format command is typically executed per namespace or across all
namespaces. It supports the Secure Erase Settings (--ses)
parameter:
- No Secure Erase (
--ses=0): Formats the media without performing a dedicated secure erase operation. - User Data Erase (
--ses=1): Erases all user data across the specified namespace. The SSD controller marks all physical blocks as invalid or resets the flash cells. - Cryptographic Erase (
--ses=2): Deletes or regenerates the internal encryption key used to protect data on self-encrypting drives (SEDs). This instantly renders all previously written ciphertext unrecoverable without needing to rewrite every flash cell.
2. Sanitize
(nvme sanitize)
Sanitize is a controller-level operation designed for more stringent declassification standards. Unlike Format, Sanitize cannot be aborted, affects the entire drive (all namespaces), and persists even across system reboots or power cycles until the process finishes.
Sanitize supports:
- Block Erase (
-a 2or--action=start-block-erase): A low-level electrical wipe of all NAND blocks. - Crypto Erase (
-a 4or--action=start-crypto-erase): Destroys the drive's internal cryptographic keys. - Overwrite (
-a 3or--action=start-overwrite): Writes a user-defined pattern across the entire storage array.
Executing Secure Erase
Using nvme-cli
Step 1: Identify the Drive
Before wiping data, verify the target drive and namespace:
sudo nvme listInspect controller capabilities to see which erase actions are supported:
sudo nvme id-ctrl /dev/nvme0Look for the fna (Format NVM Attributes) or
sanicap (Sanitize Capabilities) fields in the output to
determine support for cryptographic erase, block erase, and
per-namespace operations.
Step 2: Unmount Filesystems
The Linux kernel will encounter I/O errors or filesystem corruption if an active partition is wiped. Unmount all partitions and deactivate any LVM or software RAID configurations mapped to the drive:
sudo umount /dev/nvme0n1p*Step 3: Run the Format or Sanitize Command
Option A: Cryptographic Erase via Format To initiate a cryptographic erase across namespace 1:
sudo nvme format /dev/nvme0n1 --ses=2Option B: User Data Erase via Format To physically wipe user data blocks on namespace 1:
sudo nvme format /dev/nvme0n1 --ses=1Option C: Controller-Wide Block Sanitize To perform an irreversible block-level sanitize across the entire controller:
sudo nvme sanitize /dev/nvme0 -a start-block-eraseStep 4: Monitor Progress (Sanitize Only)
Because Sanitize runs asynchronously in drive firmware, you must check its status:
sudo nvme sanitize-log /dev/nvme0The output indicates the progress percentage and whether the operation has successfully finished.
Post-Erase Kernel Handling
Once the SSD controller signals completion, the Linux kernel must refresh its view of the storage device. If the drive geometry or namespace layout was altered, inform the kernel by re-reading the partition table:
sudo partprobe /dev/nvme0n1Or trigger a hardware namespace rescan:
sudo nvme ns-rescan /dev/nvme0At this stage, the NVMe SSD is returned to a clean, factory-like state with all target data permanently unrecoverable.