A comprehensive guide to changing NVMe device formats, adjusting block sizes, and managing namespaces on Linux systems.
Overview
NVMe devices support multiple format configurations, including different block sizes (512B, 4KB, etc.) and metadata settings. This guide covers how to safely change these formats.
Prerequisites
- NVMe device installed in your system
- Root or sudo access
- nvme-cli tools installed
- WARNING: Formatting will destroy all data on the device
Installation
Install NVMe CLI Tools
Ubuntu/Debian:
sudo apt update
sudo apt install nvme-cli
RHEL/CentOS/Fedora:
sudo dnf install nvme-cli
Identifying Your NVMe Device
List NVMe Devices
# List all NVMe devices
sudo nvme list
# Example output:
# Node SN Model Version Namespace Usage Format
# ---------------- -------------------- -------------------- -------- --------- ------------ ------
# /dev/nvme0n1 S123ABC456789 Samsung SSD 980 PRO 1.0 1 500.11 GB / 500.11 GB 512 B + 0 B
Get Device Information
# Get detailed device info
sudo nvme id-ctrl /dev/nvme0
# Get namespace info
sudo nvme id-ns /dev/nvme0n1
Viewing Supported Formats
Check Available Formats
sudo nvme id-ns /dev/nvme0n1 -H | grep "LBA Format"
Example output:
LBA Format 0 : Metadata Size: 0 bytes - Data Size: 512 bytes - Relative Performance: 0x2 Good
LBA Format 1 : Metadata Size: 0 bytes - Data Size: 4096 bytes - Relative Performance: 0x1 Better
Changing NVMe Format
Backup Your Data
CRITICAL: Always backup data before formatting!
# Example backup (if filesystem is mounted)
sudo dd if=/dev/nvme0n1 of=/path/to/backup.img bs=4M status=progress
Format Command Syntax
sudo nvme format /dev/nvme0n1 --lbaf=<format_id>
Parameters:
--lbaf=<id>: LBA Format index (0, 1, 2, etc.)--ses=0: No secure erase--ses=1: User data erase--ses=2: Cryptographic erase
Example: Change to 4KB Block Size
# Format to 4KB blocks (typically lbaf=1)
sudo nvme format /dev/nvme0n1 --lbaf=1 --ses=1
# Verify the format
sudo nvme id-ns /dev/nvme0n1 -H | grep "in use"
Example: Change to 512B Block Size
# Format to 512B blocks (typically lbaf=0)
sudo nvme format /dev/nvme0n1 --lbaf=0 --ses=1
Advanced: Namespace Management
Delete Namespace
# Delete namespace 1
sudo nvme delete-ns /dev/nvme0 -n 1
Create New Namespace
# Create namespace with specific block size
sudo nvme create-ns /dev/nvme0 --nsze=<size_in_blocks> --ncap=<capacity> --flbas=<format>
# Example: Create 100GB namespace with 4KB blocks
sudo nvme create-ns /dev/nvme0 --nsze=26214400 --ncap=26214400 --flbas=1
Attach Namespace
# Attach namespace to controller
sudo nvme attach-ns /dev/nvme0 -n 1 -c 0
Verification
Verify Format Change
# Check current format
sudo nvme id-ns /dev/nvme0n1 -H | grep -A5 "LBA Format"
# Check block size
sudo blockdev --getbsz /dev/nvme0n1
# Verify device is ready
sudo nvme list
Test the Device
# Write test pattern
sudo dd if=/dev/zero of=/dev/nvme0n1 bs=1M count=100 status=progress
# Read test
sudo dd if=/dev/nvme0n1 of=/dev/null bs=1M count=100 status=progress
Troubleshooting
Format Command Fails
Issue: Format command returns error
Solutions:
# Check if device is mounted (unmount if necessary)
mount | grep nvme0n1
sudo umount /dev/nvme0n1
# Check if device is in use
lsof | grep nvme0n1
fuser -v /dev/nvme0n1
# Ensure no LVM/RAID is using the device
sudo pvs
sudo mdadm --detail --scan
Namespace Issues
Issue: Cannot delete or modify namespace
Solutions:
# Detach namespace first
sudo nvme detach-ns /dev/nvme0 -n 1 -c 0
# Then delete
sudo nvme delete-ns /dev/nvme0 -n 1
# Reset NVMe subsystem (last resort)
sudo nvme reset /dev/nvme0
Performance After Format
Issue: Poor performance after changing format
Check alignment:
# Check filesystem alignment
sudo parted /dev/nvme0n1 align-check optimal 1
# Recreate filesystem with proper alignment
sudo mkfs.ext4 -b 4096 /dev/nvme0n1
Best Practices
- Always backup data before any format operation
- Verify supported formats using
nvme id-nsbefore formatting - Use 4KB blocks for modern systems (better performance)
- Align filesystems to match the LBA format
- Test after formatting to ensure stability
- Document your configuration for future reference
Common Use Cases
Converting Legacy 512B to 4KB
# 1. Backup data
sudo dd if=/dev/nvme0n1 of=/backup/nvme-backup.img bs=4M
# 2. Format to 4KB
sudo nvme format /dev/nvme0n1 --lbaf=1 --ses=1
# 3. Create aligned partition
sudo parted /dev/nvme0n1 --align optimal mklabel gpt
sudo parted /dev/nvme0n1 --align optimal mkpart primary 0% 100%
# 4. Create filesystem with 4KB blocks
sudo mkfs.ext4 -b 4096 /dev/nvme0n1p1
Secure Erase and Reformat
# Cryptographic erase and format to 4KB
sudo nvme format /dev/nvme0n1 --lbaf=1 --ses=2
# Verify secure erase completed
sudo nvme smart-log /dev/nvme0 | grep "percentage_used"
Important Warnings
⚠️ Data Loss: Formatting destroys all data on the device ⚠️ No Undo: Format operations cannot be reversed ⚠️ Device Compatibility: Not all devices support all formats ⚠️ System Disruption: Never format a device in use by the system
Additional Resources
- NVMe CLI Documentation
- NVMe Specification
- Linux NVMe Subsystem Documentation
Summary
Changing NVMe format is straightforward but requires careful attention to:
- Backing up data
- Verifying supported formats
- Proper secure erase selection
- Post-format verification
Always test the new configuration thoroughly before putting the device into production use.
© 2025 Darren Soothill. All rights reserved.