Bhupal Sapkota Software Consultant Helping founders build
and launch their ideas with code & AI

Bhupal's Newsletter

I send out occassional email with programming resources, summaries of my readings on art, science, and commerce behind technology, my writings on growing an online business etc. Feel free to subscribe, no spam!

Home

Raspberry Pi SSD Optimization

Optimize Your Raspberry Pi to Run Fully from an SSD

Cloning your Raspberry Pi OS from an SD card to an SSD can dramatically improve speed, reliability, and disk longevity. In this guide, we’ll walk you through a complete setup, from cloning to SSD optimizations.

Step 1: Boot from the SSD

After cloning your SD card to the SSD, confirm that your Pi is booting from the SSD:

mount | grep " / "
lsblk

You should see /dev/sda2 as the root filesystem and full disk size.

Step 2: Expand the Root Partition

To use the full SSD capacity, expand the root partition and filesystem:

sudo growpart /dev/sda 2
sudo resize2fs /dev/sda2
df -h /

Now your root filesystem should show the full SSD size.

Step 3: Remove the Old SD Card

If the SD card shows no mountpoints, you can safely remove it:

lsblk
# confirm mmcblk0 has no MOUNTPOINT
sudo reboot

Step 4: Verify Apache2 and MySQL Services

sudo systemctl status apache2
sudo systemctl status mysql

If inactive, start and enable them:

sudo systemctl enable apache2 mysql
sudo systemctl start apache2 mysql

Step 5: Enable TRIM for SSD Longevity

TRIM ensures that deleted blocks on your SSD are cleaned up properly, which helps maintain speed and lifespan.

Check if TRIM works on your SSD:

sudo fstrim -v /

To automate weekly TRIM:

sudo systemctl enable fstrim.timer
sudo systemctl start fstrim.timer

Step 6: Create and Enable Swap

If swapon --show shows nothing, create a swap file on SSD:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# make permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Recommended swap size for a 4 GB Raspberry Pi: 1 GB

Step 7: Reduce SSD Writes with /tmp in RAM

Using tmpfs for /tmp moves temporary files to RAM, reducing SSD wear:

tmpfs /tmp tmpfs defaults,noatime,nosuid,size=200M 0 0

Add this line to /etc/fstab. This uses only 200 MB of RAM, safe for a 4 GB Pi.

Step 8: Final /etc/fstab for SSD Setup


LABEL=writable  /       ext4    defaults        0       1
LABEL=system-boot /boot/firmware vfat defaults 0 1
/swapfile none swap sw 0 0
tmpfs /tmp tmpfs defaults,noatime,nosuid,size=200M 0 0
  

Step 9: Reboot and Verify

After editing /etc/fstab and setting up TRIM, reboot to apply all changes:

sudo reboot

Check:

  • Root and boot on SSD: mount | grep " / "
  • Swap active: swapon --show
  • /tmp using tmpfs: df -h /tmp
  • TRIM works: sudo fstrim -v /

Awesome, You now have a fully SSD-booted Raspberry Pi with:

  • Expanded root filesystem to use full SSD space
  • TRIM enabled for SSD longevity
  • Swap configured efficiently on SSD
  • /tmp in RAM to reduce SSD wear
  • Apache2 and MySQL running smoothly

This setup is faster, more reliable, and optimized for longevity, perfect for web servers, databases, and daily Raspberry Pi use.

Category: technology
Tagged with:

Copy & Share

Raspberry Pi SSD Optimization
https://bhupalsapkota.com/raspberry-pi-ssd-optimization/

Your thoughts? Please leave a comment

Your email address will not be published. Required fields are marked *