NVMe
This section introduces how to use the NVMe hard drive function on the Raspberry Pi, laying the foundation for subsequent project development.
Note: The PCIe interface must be enabled before using this function.
Enabling the PCIe Interface
- Enabling the PCIe interface (PCIe Gen 2 is enabled by default)
-
Edit the config.txt file
sudo nano /boot/firmware/config.txt -
PCIe defaults to gen2. If PCIe is not already enabled, add the following to config.txt:
dtparam=pciex1 -
After modification, reboot the Raspberry Pi to recognize the device
sudo reboot -
After reboot, check the PCIe recognition status (the output may vary depending on the module)
lspci
-
NVMe Hard Drive Operations
Formatting the Hard Drive
-
If the PCIe interface is not enabled, enable the PCIe interface first.
-
Check supported filesystem types by typing
sudo mkfs.and pressing Tab to see available filesystem suffixes.sudo mkfs. -
List all drives
lsblk
Confirm the target partitionFormatting will erase all data on the target partition. This step applies to an existing target partition; if a partition has not been created yet, complete "Drive Partitioning" first. Confirm that
/dev/nvme0n1p1is the partition to be formatted, and back up your data before proceeding. -
To format
/dev/nvme0n1p1as ext4, run the following command:sudo mkfs.ext4 /dev/nvme0n1p1
Hard Drive Partitioning
-
If the PCIe interface is not enabled, enable the PCIe interface first.
-
List all drives (for more details, run
sudo fdisk -l)lsblk
-
To repartition
/dev/nvme0n1, execute the following command (note: the device number is the base device, do not addp1, asp1indicates a partition):sudo fdisk /dev/nvme0n1 -
After adding a partition with
n, finally typewto save and exit. -
fdiskcommand functions:nCreate a new partitionqQuit without savingpPrint the partition tablemPrint the menudDelete a partitionwWrite table to disk and exittChange a partition's system ID
Manual Mounting
-
If the PCIe interface is not enabled, enable the PCIe interface first.
-
Confirm the target mount directory. If the
toshibafolder does not exist in the current directory, create it:sudo mkdir toshiba -
List all drives (for more details, run
sudo fdisk -l)lsblk
-
To mount
/dev/nvme0n1to thetoshibafolder in the current directory, for example:sudo mount /dev/nvme0n1p1 ./toshiba -
Check the file system disk space usage
df -h
Auto-mount on Boot
-
Note: Auto-mounting is only used when the drive is not the system disk, i.e., when it functions as additional storage. Perform this configuration only in such cases
-
If the PCIe interface is not enabled, enable the PCIe interface first.
-
Modify the
fstabfilesudo nano /etc/fstab -
Add the following line at the end of the file (
/dev/nvme0n1p1is the device,/home/pi/toshibais the mount directory,ext4is the filesystem type,defaultsare the default mount options):/dev/nvme0n1p1 /home/pi/toshiba ext4 defaults 0 0 -
Test the mount configuration with the following command. Reboot only after confirming it works; otherwise, a mount failure may prevent the system from booting.
sudo mount -a -
After confirming the mount is successful, reboot the Raspberry Pi.
sudo reboot -
After reboot, verify the mount status
lsblk
Read/Write Test
-
If the PCIe interface is not enabled, enable the PCIe interface first.
-
Check the mount status
lsblk -
After confirming the mount is successful, navigate to the mount directory (here
/home/pi/toshiba).cd /home/pi/toshiba -
Copy data from Raspberry Pi memory to the drive (Write Test)
# Clear memory before the write testsudo sh -c "sync && echo 3 > /proc/sys/vm/drop_caches"sudo dd if=/dev/zero of=./test_write count=2000 bs=1024k -
Copy data from the drive to Raspberry Pi memory (Read Test)
# Clear memory before the read testsudo sh -c "sync && echo 3 > /proc/sys/vm/drop_caches"sudo dd if=./test_write of=/dev/null count=2000 bs=1024k -
Note: Performance varies depending on the drive and the environment on the Raspberry Pi. Some drives may be speed-limited. For accurate performance testing, use a PC.