Skip to content

Instantly share code, notes, and snippets.

@avoidik
Last active December 12, 2024 08:06
Show Gist options
  • Save avoidik/d8fc39a372db680090edd5322d60848f to your computer and use it in GitHub Desktop.
Save avoidik/d8fc39a372db680090edd5322d60848f to your computer and use it in GitHub Desktop.
Migrate Raspberry Pi from SD to USB

Raspberry Pi from SD to USB

How to switch over from SD-card to USB-attached device (USB thumbstick, USB enclosed SSD or HDD, etc.) to have more durable storage option.

Steps

  1. Connect USB device to your RPI

  2. Make sure USB device visible by the system

    sudo fdisk -l
    sudo lsblk

    Usually mmcblk0 is your SD-card, and sda, sdb, sdX are attached USB devices, in my case it was sda. Make sure there is no sensitive data stored on the target USB device. If it's new USB device there should be no partitions reported by fdisk for it. You may want to clean target device using sudo sfdisk --delete /dev/sda (be very careful, it will delete everything on your target device)

  3. Install rpi-clone

    git clone https://github.com/billw2/rpi-clone.git 
    cd rpi-clone
    sudo cp rpi-clone rpi-clone-setup /usr/local/sbin
    sudo rpi-clone-setup -t $(hostname -s)
    sudo rpi-clone -s
  4. Clone SD-card to USB device

    sudo rpi-clone sda # replace sda here with your device name

    This may take some time depending on how much data you have, storage performance, running applications, clients connected, etc. During the process few questions will be asked, read carefully and answer. On the last step where it's asked to examine mounted clone run another connection to RPI, keep existing intact.

    If you've accidentally skipped it just remount it manually:

    • sudo mount /dev/sda6 /mnt/clone/boot.
    • sudo mount /dev/sda7 /mnt/clone
  5. Identify partitions

    Target device will have the same partitions layout as in source device. We need to find out boot and last partition ids.

    sudo lsblk -o name,mountpoint,size,partuuid /dev/sda6 # clone of mmcblk0p6 mounted as /boot
    sudo lsblk -o name,mountpoint,size,partuuid /dev/sda7 # clone of mmcblk0p7 mounted as / or root

    Write down PARTUUID column, in my case these were 3b4344a4-06 for boot and 3b4344a4-07 for root partition respectively

  6. Change cmdline.txt

    To be able to boot from the target device we have to change cmdline.txt file:

    • change root=/dev/mmcblk0p7 to root=PARTUUID=3b4344a4-07 which is the root partition id
    • append at the end of the line rootdelay=5 option to wait while attached device is initialized at boot time

    Complete example:

    console=serial0,115200 console=tty1 root=PARTUUID=3b4344a4-07 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait rootdelay=5
    

    If you'll try to boot without this change you'll get endless Waiting for root message.

    Note: there is one interesting caveat, if you'd like to use LABEL or UUID instead of PARTUUID you must create initramfs/initrd file, hence additional step will be required. Do not use PARTUUID with initrd.

    If you're going to use PARTUUID then you don't need this step (I repeat, skip it and follow the next step).

    • make sure root= set to something like root=LABEL=USB-ROOT
    • cd /mnt/clone/boot/
    • generate initrd file with mkinitramfs -o initrd.sda
    • append initramfs initrd.sda followkernel after rootdelay=5

    Complete example:

    console=serial0,115200 console=tty1 root=LABEL=USB-ROOT rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait rootdelay=5 initramfs initrd.sda followkernel
    
  7. Change fstab

    After the boot process has been completed we would need to mount correct partitions.

    Open /mnt/clone/etc/fstab and do the following:

    • change device /dev/mmcblk0p6 to PARTUUID=3b4344a4-06 (remember to use your own id)
    • change device /dev/mmcblk0p7 to PARTUUID=3b4344a4-07 (remember to use your own id)

    Complete example:

    proc            /proc           proc    defaults          0       0
    PARTUUID=3b4344a4-06  /boot           vfat    defaults          0       2
    PARTUUID=3b4344a4-07  /               ext4    defaults,noatime  0       1
    
  8. Unmount

    Now return back to the initial terminal where we've left rpi-clone process open and hit enter to unmount all partitions. Make sure you don't have the second terminal open somewhere at the mounted paths.

  9. Test

    Shutdown RPI with sudo shutdown -h now, disconnect PSU, unplug SD card, plug PSU in, verify

@andychess
Copy link

This worked for me on a Pi4 with 512Gb SSD. Thanks to everyone involved, you have saved me hours of research!

@vseibt
Copy link

vseibt commented Dec 27, 2023

Worked not that good. rpi-clone copied everything from root-partition but nothing from boot partition.
So I had to copy boot partition manually using dd (done on a different computer) and had to edit cmdline.txt there accordingly.
At least /etc/fstab was automatically rewritten correctly.
Without this thread I would never have figured out to edit cmdline.txt in the boot partition.
I was cloning from an SD card to an USB-SSD-128 GB harddrive on a raspi 4.

@int48
Copy link

int48 commented Jan 16, 2024

Copy from boot and root partition is perfect, use it with a raspi 4, micro sd card and Sandisk Extreme USB Stick.
The programm and instructions is perfect, thanks a lot.

@Madben82
Copy link

Worked flawlessly to switch from SD to USB NVME on my RPi4! Thank you very much.

Was a bit confused that the cmdline.txt in the firmware folder was hidden and the last two chages to the fstab file were already changed to the correct values of the new SSD. But I am not that experienced in all this Raspberry stuff, so thats my fault I guess.

@jorycz
Copy link

jorycz commented Feb 9, 2024

Just finished this on Raspberry Pi4 without issue on first try. Thanks. /boot/firmware/ was not mounted so before you start, mount it with
mount /dev/mmcblk0p1 /boot/firmware/

@DevinTDHa
Copy link

DevinTDHa commented Dec 9, 2024

For anyone using this guide to try to boot their Raspberry Pi 3b+ from an external drive:

Firstly, I used a more recent maintained version here: https://github.com/geerlingguy/rpi-clone

Then I tried to follow this guide, but the pi would not boot just from the attached external drive. My solution was to leave the SD card plugged in but copy /boot/cmdline.txt and /etc/fstab from the cloned partitions to the SD card. I suppose as long as /boot is ok it will boot from the external drive.

For example:

# Just in case
sudo cp /boot/cmdline.txt /boot/bak_cmdline.txt
sudo cp /etc/fstab /etc/bak_fstab

# Copy the files from sda to sd card
sudo cp /mnt/clone/boot/cmdline.txt /boot/cmdline.txt
sudo cp /mnt/clone/etc/fstab /etc/fstab

# After Reboot
sudo lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 931.5G  0 disk
├─sda1        8:1    0   256M  0 part /boot
└─sda2        8:2    0 931.3G  0 part /
mmcblk0     179:0    0  59.5G  0 disk
├─mmcblk0p1 179:1    0   256M  0 part
└─mmcblk0p2 179:2    0  59.2G  0 part

The idea is, that the pi will still initially boot from the SD card, but after the kernel is loaded, mount all the files from the external drive. I read this in the forum post Running Raspbian from USB Devices : Made Easy

The easiest and most reliable way to run Raspberry Pi OS on a USB device with any Raspberry Pi prior to the model 4 or 5 is to leave an SD card containing Raspberry Pi OS in place, but use it only for starting the Raspberry Pi OS that is residing on a USB device.

Works for me so far, as I did this because of the SD card going bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment