Skip to main content

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

      Query the PCIe devices connected

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.

    Query supported formats

  • List all drives

    lsblk

    List all drives

    Confirm the target partition

    Formatting 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/nvme0n1p1 is the partition to be formatted, and back up your data before proceeding.

  • To format /dev/nvme0n1p1 as ext4, run the following command:

    sudo mkfs.ext4 /dev/nvme0n1p1

    Format the hard drive


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

    List all drives

  • To repartition /dev/nvme0n1, execute the following command (note: the device number is the base device, do not add p1, as p1 indicates a partition):

    sudo fdisk /dev/nvme0n1
  • After adding a partition with n, finally type w to save and exit.

  • fdisk command functions:

    • n Create a new partition
    • q Quit without saving
    • p Print the partition table
    • m Print the menu
    • d Delete a partition
    • w Write table to disk and exit
    • t Change 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 toshiba folder does not exist in the current directory, create it:

    sudo mkdir toshiba
  • List all drives (for more details, run sudo fdisk -l)

    lsblk

    List all drives

  • To mount /dev/nvme0n1 to the toshiba folder 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 fstab file

    sudo nano /etc/fstab
  • Add the following line at the end of the file (/dev/nvme0n1p1 is the device, /home/pi/toshiba is the mount directory, ext4 is the filesystem type, defaults are 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 test
    sudo sh -c "sync && echo 3 > /proc/sys/vm/drop_caches"
    sudo dd if=/dev/zero of=./test_write count=2000 bs=1024k

    Write Test Result

  • Copy data from the drive to Raspberry Pi memory (Read Test)

    # Clear memory before the read test
    sudo sh -c "sync && echo 3 > /proc/sys/vm/drop_caches"
    sudo dd if=./test_write of=/dev/null count=2000 bs=1024k

    Read Test Result

  • 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.