You're referring to running a long self-test (also known as extended self-test or long diagnostic test) on an NVMe SSD (Non-Volatile Memory Express Solid-State Drive) under Linux.
NVMe SSDs have a built-in self-test feature, which allows you to run diagnostic tests to detect potential issues, such as:
Short self-test (quick test, ~2 minutes): Basic test, checks for obvious issues.
Long self-test (extended test, ~1-2 hours or more): Thorough test, scans entire drive, checks for advanced issues.
Here's how to initiate a long self-test on an NVMe SSD under Linux:
Prerequisites:
nvme-cli package installed: This is a command-line tool for managing NVMe devices. Install it using your distribution's package manager:
Ubuntu/Debian: sudo apt-get install nvme-cli
Fedora/CentOS/RHEL: sudo dnf install nvme-cli
Arch Linux: sudo pacman -S nvme-cli
Identify your NVMe SSD device (e.g., /dev/nvme0n1): Run lsblk or nvme list to see the list of NVMe devices connected to your system.
Now, let's start the long self-test:
Unmount all filesystems on the NVMe SSD (if any are mounted). The self-test will fail if the drive is in use.
Open a terminal and run the following command (replace /dev/nvme0n1 with your actual NVMe device):
This command aborts the current self-test operation (code 0xf = Abort).
That's it! Your NVMe SSD is now running a thorough long self-test. Be patient, as this test can take a significant amount of time to complete. If any issues are detected, the self-test log will provide valuable information for troubleshooting or warranty purposes.
Happy testing!
User
show state and refresh every 1s
Assistant
A great follow-up question!
To continuously monitor the NVMe self-test state and refresh the output every 1 second, you can use a combination of the nvme command and a Linux utility called watch.
Here's the magic command:
bash
watch -n 1 "sudo nvme self-test-log /dev/nvme0n1"
Let's break it down:
watch: A command that runs a specified command repeatedly at a given interval.
-n 1: Specifies the interval (in seconds) between each execution of the command. In this case, 1 second.
sudo nvme self-test-log /dev/nvme0n1: The command we want to run repeatedly. This is the same command I showed you earlier to check the self-test log.
What happens when you run this command:
The watch utility will execute sudo nvme self-test-log /dev/nvme0n1 every 1 second.
The output will be displayed in the terminal, overwriting the previous output.
You'll see the self-test progress update in real-time:
Current operation in progress: Will show the percentage complete (e.g., 10%, 20%, ... 100%).
Self-test code (result): Will remain 0x2 (Long self-test) until the test completes.
Self-test result details: Will update with the final result when the test finishes (e.g., "Completed without error").
Example output (while the test is running):
code
Every 1.0s: sudo nvme self-test-log /dev/nvme0n1 Thu Mar 18 14:30:00 2023
Self-test log for device:/dev/nvme0n1
...
"Self-test code (result)": "0x2 (0x0)",
"Self-test result details": "In progress",
"Current operation in progress": {
"Operation code": 2,
"Percent complete": 43
}
And when the test completes:
code
Every 1.0s: sudo nvme self-test-log /dev/nvme0n1 Thu Mar 18 16:15:00 2023
Self-test log for device:/dev/nvme0n1
...
"Self-test code (result)": "0x2 (0x0)",
"Self-test result details": "Completed without error",
"Current operation in progress": {
"Operation code": 0,
"Percent complete": 100
}
Tips:
To exit the watch loop, simply press Ctrl+C in the terminal.
If you want a more compact output, you can pipe the result to jq (a lightweight JSON parser):
This will display only the percentage complete, updating every second:
code
Every 1.0s: sudo nvme self-test-log /dev/nvme0n1 | jq '.\"Current operation in progress\"|.\"Percent complete\"'
43
44
45
...
100
Now, sit back, relax, and watch your NVMe SSD's self-test progress update in real-time!
Happy monitoring!
User
what is the best way to test the health of a nvme ssd in linux?
Assistant
A fantastic question!
Testing the health of an NVMe SSD in Linux involves a combination of tools and techniques to assess its performance, reliability, and overall condition. Here's a comprehensive checklist to help you evaluate the health of your NVMe SSD:
1. SMART Monitoring (smartctl):
First, check the drive's SMART (Self-Monitoring, Analysis, and Reporting Technology) attributes using smartctl (part of smartmontools package). Although NVMe SSDs don't use traditional SMART, they have a similar NVMe-specific health monitoring system.
Replace /dev/nvme0n1 with your actual NVMe device.
Key attributes to look for:
Critical Warning: 0 (no issues) or non-zero (problems detected).
Temperature: Should be within a reasonable range (e.g., < 50°C).
Available Spare: Percentage of spare blocks available (lower values indicate wear).
Percentage Used: Drive wear level (higher values mean more wear).
Data Units Read/Written: Lifetime data transfer (helps estimate drive usage).
Example output:
markdown
smartctl 7.2 2020-12-30 r5155 [x86_64-linux-5.11.0-27-generic] (local build)
Copyright (C) 2002-20, Bruce Allen, Christian Franke, www.smartmontools.org
=== START OF SMART DATA SECTION ===
Read NVMe SMART/Health Information failed: NVMe Status 0x2002
SMART/Health Information (NVMe Log 0x02)
Critical Warning: 0x00
Temperature: 38 Celsius
Available Spare: 100%
Available Spare Threshold: 10%
Percentage Used: 2%
Data Units Read: 10,242,112 [5.2 TB]
Data Units Written: 20,484,224 [10.4 TB]
...
2. NVMe Self-Test (nvme-cli):
Perform a short self-test (quick, ~2 minutes) or long self-test (thorough, ~1-2 hours):
bash
sudo nvme device-self-test /dev/nvme0n1 --self-test-code=1 # Short test
sudo nvme device-self-test /dev/nvme0n1 --self-test-code=2 # Long test
Monitor the test progress:
bash
watch -n 1 "sudo nvme self-test-log /dev/nvme0n1"
A successful test indicates the drive's electronics, firmware, and media are functioning correctly.
3. nvme-cli Health Information:
Get detailed NVMe health data using nvme-cli:
bash
sudo nvme smart-log /dev/nvme0n1
This command displays:
critical_warning: 0 (no issues) or non-zero (problems).
Average latency: < 100 μs (read), < 50 μs (write).
Red flags:
Very low bandwidth (< 100 MB/s).
Extremely high latency (> 1 ms).
Errors during benchmarking (e.g., fio crashes or reports I/O errors).
6. Error Log Analysis (nvme-cli):
Inspect the NVMe error log for issues:
bash
sudo nvme error-log /dev/nvme0n1
Check for non-zero Error count or Critical warnings.
Summary: Health Check List
SMART Monitoring (smartctl): Verify Critical Warning, Temperature, Available Spare, and Percentage Used.
NVMe Self-Test (nvme-cli): Run short/long self-tests to detect issues.
nvme-cli Health Information: Review critical_warning, temperature, percentage_used, and media_errors.
Bad Block Scanning (nvme-cli): Ensure no bad blocks are present.
Performance Benchmarking (fio): Evaluate bandwidth, IOPS, and latency.
Error Log Analysis (nvme-cli): Inspect error counts and critical warnings.
When to worry:
Critical Warning (SMART) is non-zero.
Self-test fails or reports errors.
Percentage Used > 80% (high wear).
Bad blocks detected.
Abnormally low performance or high latency.
Errors in the NVMe error log.
By following this comprehensive checklist, you'll get a thorough understanding of your NVMe SSD's health and can take proactive measures to prevent data loss or performance degradation.