Migrate Raspberry Pi SD Card To USB

This How To Migrate Raspberry Pi SD Card to USB explains you step by step how you migrate from an SD Card to an USB storage device. Migrating actually has several benefits incl. to extend the life of the SD card due to the reduction of frequent reads and writes by the system to the SD Card. A USB device is far more reliable and much more robust for frequent reads and writes than an SD Card.

 

Migrate Raspberry Pi SD Card To USB

Migrate Raspberry Pi SD Card To USB

 

Index of How To Migrate Raspberry Pi SD Card To USB

Background of How To Migrate Raspberry Pi SD Card To USB

With this How To of How To Migrate Raspberry Pi SD Card to USB, we will be migrating an existing Raspberry Pi Model 3B+ system that leverages solely on an SD card to a fast USB stick. 

In this How To we leverage on a Corsair GTX USB stick since I am using these now for years with very good experience, but also since they are considered very fast. In case you go for an other USB stick or storage device, please consider its speed for read/wirte. A slow low quality USB stick or storage device can considerably reduce the speed of your system. Same time, a fast USB device can increase the speed compared to an SD card.

Note: I strongly recommend to backup your existing Raspberry before starting the migration. Please also test your backup whether it did work properly.

How To Migrate Raspberry Pi SD Card to USB

We go thru 4 key phases for the migration. 

  • Phase One: Existing and new storage devices
  • Phase Two: Cloning phase
  • Phase Three: Configuration phase
  • Phase Four: Test

Phase One: Existing and new storage devices

Before we can start, we need to have a clear picture on the storage devices for the Raspberry Pi in scope. To do so we do not yet plug the new USB storage device, but we fire the below command that will give us a picture of the as is situation.

sudo lsblk

This should output something like the below:

pi@raspberrypi:~ $ sudo lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
mmcblk0     179:0    0 29.8G  0 disk 
├─mmcblk0p1 179:1    0  256M  0 part /boot
└─mmcblk0p2 179:2    0 29.6G  0 part /

Now, this output tells us that there is one SD card (usually SD cards are called something like “mmcblk”) which is split into two partition. The SD card is mmcblk0 and the two partitions are mmcblk0p1 and mmcblk0p2. This information is important and you write it down, put it into your notes or keep this Terminal window open whilst opening a new one for the next step.

As a next step, plug the USB storage device now (e.g. USB stick). Once plugged, give your Raspberry Pi a couple of seconds to read and initialise it. Following this you fire again the lsblk command (see below).

sudo lsblk

The output will now look like something like this

pi@raspberrypi:~ $ sudo lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 119.2G  0 disk 
└─sda1        8:1    0 119.2G  0 part 
mmcblk0     179:0    0  29.8G  0 disk 
├─mmcblk0p1 179:1    0   256M  0 part /boot
└─mmcblk0p2 179:2    0  29.6G  0 part /

As you can see immediately, the Raspberry Pi 3B+ in the above case added an additional disk, which is called “sda” and it consists of a partition called “sda1”

Now, we know two key things that are important for the next phase:

  • sda (and sda1) is the new USB stick (USB storage device)
  • mmcblk0 (and mmcblk0p1 and mmcblk0p2) is the SD card

Phase Two: Cloning phase

First of all we install a script the helps us to get the cloning of the current SD card to the USB stick done. The script is called RPI Clone and is officially available on GitHub. 

sudo apt -y install git
sudo 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)

Now, since we have installed the cloning tool, we start cloning. You remember from the first phase, that the USB stick is called “sda”. In your case maybe this is different, if it is different in your case, then you need to replace the “sda” by whatever it is in your system. 

sudo rpi-clone sda

The above command will print something like this:

pi@raspberrypi:~/rpi-clone $ sudo rpi-clone sda

Booted disk: mmcblk0 32.0GB                Destination disk: sda 128.0GB
---------------------------------------------------------------------------
Part      Size    FS     Label           Part   Size    FS     Label  
1 /boot   256.0M  fat32  --              1      119.2G  fat32  --     
2 root     29.6G  ext4   rootfs                                       
---------------------------------------------------------------------------
== Initialize: IMAGE partition table - partition number mismatch: 2 -> 1 ==
1 /boot               (49.5M used)   : MKFS  SYNC to sda1
2 root                (4.5G used)    : RESIZE  MKFS  SYNC to sda2
---------------------------------------------------------------------------
Run setup script       : no.
Verbose mode           : no.
-----------------------:
** WARNING **          : All destination disk sda data will be overwritten!
-----------------------:

Initialize and clone to the destination disk sda?  (yes/no):

You doublecheck and if everything is like you like to have it, you confirm by “yes”. This now, depending on the amount of data, the performance of your Raspberry Pi, your SD card and your USB storage device will take a while. Leverage on the time to grab a drink….

In case you did as I hit enter at the last step, you did unmount the cloned drives. These drives however we need for phase 3. Hence we mount it again by:

sudo mount /dev/sda2 /mnt/clone
sudo mount /dev/sda1 /mnt/clone/boot

Once the cloning is complete, you jump to the next phase, phase 3.

Phase Three: Configuration phase

Identify the partitions existing and made by the cloning. We need to find the boot PARTUUID to tell the Raspberry Pi system from which partition it in the future should boot – which is obviously the USB storage device (e.g. USB stick) rather then the SD card. 

sudo lsblk -o name,mountpoint,size,partuuid /dev/sda1
sudo lsblk -o name,mountpoint,size,partuuid /dev/sda2

These two commands will tell you the PARTUUIDs. It will look like the below:

pi@raspberrypi:~/rpi-clone $ sudo lsblk -o name,mountpoint,size,partuuid /dev/sda1
NAME MOUNTPOINT       SIZE PARTUUID
sda1 /mnt/clone/boot  256M e03ba4ff-01
pi@raspberrypi:~/rpi-clone $ sudo lsblk -o name,mountpoint,size,partuuid /dev/sda2
NAME MOUNTPOINT  SIZE PARTUUID
sda2 /mnt/clone  119G e03ba4ff-02

With this I know that the PARTUUID of the boot partion is “e03ba4ff-01” and the PARTUUID of the root partition is “e03ba4ff-02”. In your case, the PARTUUIDs will most likely be different. You need to know them for the next step, hence write them down.

Now, we change the file called cmdline.txt to ensure the Raspberry Pi will be leveraging on the /root of the USB stick. For this we need the PARTUUID as found out in the above, in this case “e03ba4ff-02”. Run:

nano /mnt/clone/boot/cmdline.txt

This will show you something like this:

console=serial0,115200 console=tty1 root=PARTUUID=e03ba4ff-02 rootfstype=ext4 fsck.repair=yes rootwait rootdelay=5

Note: There is a likelihood that rpi-clone already fixed this for you, hence be not surprised if it is already the correct PARTUUID. If the PARTUUID is not the one you noted down for the root partition, then you need to update it. Furthermore, you like to add the “rootdelay=5” at the end. This asks the Raspberry to wait until the USB device is initialised. 

Now, we enable USB boot by:

echo program_usb_boot_mode=1 | sudo tee -a /mnt/clone/boot/config.txt

Next step, double check fstab. This should also been adapted automatically by rpi-clone. However, let’s check it and if it is not correct, you fix it. Again, you need shothe two PARTUUIDs you noted before. Run:

sudo nano /mnt/clone/etc/fstab

This should print something like this:

proc            /proc           proc    defaults          0       0
PARTUUID=e03ba4ff-01  /boot           vfat    defaults          0       2
PARTUUID=e03ba4ff-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

Check whether the PARTUUID’s are correct and match the one’s you noted before. If not, you change them accordingly.

Unmount the partitions of the USB storage device (USB stick):

sudo umount /dev/sda2
sudo umount /dev/sda1

Once done, shutdown the Raspberry Pi by running:

sudo shutdown -h now

Your Raspberry Pi should be shut down after a minute or so and we move to phase four.

Phase Four: Test

Now, let’s test whether you have done everything of the above well. This assumes that your Raspberry Pi now is switched off (shutdown). 

  • For newer Raspberry Pi Models you first, unplug the SD card. This means you simply take out the SD card of your Raspberry Pi.
    • Note: Older models cannot boot without SD card, hence even you moved everything to the USB stick, you still need the SD card to boot the system up. You can find out your model by running thru additional information chapter “Find Out If Your Raspberry Pi is USB Boot Ready
  • Secondly, unplug the power cable (Micro USB), wait 10 seconds, and plug it again
  • Third, wait 3-4 minutes and go back to your terminal. SSH into the Raspberry and check whether everything is as expected
    • If not, you may need to troubleshoot and consider your Raspberry version. This you can do by plugging the SD card again and get it booted this way.
    • Shutdown the Raspberry again and start with Phase Four again.

Assuming in your case everything went as nice as in my case, I leave you here enjoying your new setup.

Additional Information

Find Out If Your Raspberry Pi is USB Boot Ready

Older Raspberry Pi Model 3B+ do not support booting from USB without SD card. You can find out which model you have by executing the below commands:

pi@raspberrypi:~ $ sudo cat /sys/firmware/devicetree/base/model;echo
Raspberry Pi 3 Model B Rev 1.2
pi@raspberrypi:~ $ sudo vcgencmd otp_dump | grep 17:
17:3020000a
pi@raspberrypi:~ $

The first command should tell you a revision (Rev) equal or higher than 1.2. The second command should output “17:3020000a”. If this Is not the case, then you are likely that you cannot boot only from USB storage device (USB stick) but you still need to keep the SD card to initially boot whilst then using the USB stick for the running system as described in the above. Basically, the SD card will be needed to boot, whilst after booting the whole operations will take place on the USB stick. Check the internet for further information on this. 

RPI Clone

In this How To we have leveraged on a script called RPI Clone. On GitHub you can find out more about this script. The details of this were the basics for this How To. In case you have a special situation and the above How To does not work for you, you like to double check the more details on GitHub. Further options on rpi-clone are:

usage: rpi-clone sdN {-v|--verbose} {-f|--force-initialize} {-f2}
         {-p|--p1-size size} {-u|--unattended} {-U|--Unattended} {-q|--quiet}
         {-s|--setup host} {-e|--edit-fstab sdX } {-m|--mountdir dir }
         {-L|--label-partitions label} {-l|--leave-sd-usb-boot}
         {-a|--all-sync} {-F|--Force-sync} {-x} {-V|--version}
         {--convert-fstab-to-partuuid}
         {--exclude=PATTERN} {--exclude-from=FILE}

    -v      - verbose rsync, list all files as they are copied.
    -f      - force initialize the destination disk by imaging the booted disk
                partition structure.  File systems are then synced or imaged.
    -f2     - force initialize only the first 2 partitions to the destination.
                So a multi partition USB boot can initialize clone back to
                a 2 partition SD card.
    -p size - resize destination partition 1 to 'size' bytes. For two partition
                initialize (when first clone to blank disk or using -f2 or -f).
                Use 'sizeM' for MiB size units. eg -p 256M equals -p 268435456
    -u      - unattended clone if not initializing.  No confirmations asked,
                but abort if disk needs initializing or on error.
    -U      - unattended even if initializing. No confirmations asked,
                but abort only on errors.
    -q      - quiet mode, no output unless errors or initializing. Implies -u.
    -s host - add 'host' to args passed to script rpi-clone-setup and run it
                after cloning but before unmounting partitions. For setting
                clone disk hostname, but args can be what the script expects.
                You can give multiple '-s arg' options.
    -e sdX  - edit destination fstab to change booted device names to new
                device 'sdX'.  This is Only for fstabs that use device names.
                Used for setting up a USB bootable disk.
    -m dir  - Add dir to a custom list of mounted directories to sync.  Then
                the custom list will be synced instead of the default of all
                mounted directories.  The root directory is always synced.
                Not for when initializing.
    -L lbl  - label for ext type partitions.  If 'lbl' ends with '#', replace
                '#' with a partition number and label all ext partitions.
                Otherwise, apply label to root partition only.
    -l      - leave SD card to USB boot alone when cloning to SD card mmcblk0
                from a USB boot.  This preserves a SD card to USB boot setup
                by leaving the SD card cmdline.txt using the USB root.  When
                cloning to USB from SD card this option sets up the SD card
                cmdline.txt to boot to the USB disk.
    -a      - Sync all partitions if types compatible, not just mounted ones.
    -F      - force file system sync or image for some errors. eg:
                If source used > destination space error, do the sync anyway.
                If a source partition mount error, skip it and do other syncs.
    -x      - use set -x for very verbose bash shell script debugging
    -V      - print rpi-clone version.

  Clone a booted file system to a destination disk which is bootable.

  The destination disk is a SD card (USB card reader) or USB disk 'sdN' plugged
  into a USB port.  The 'sdN' name should be a full disk name like sda and not
  a partition name like sda1.  rpi-clone works on a Raspberry Pi and can work on
  other systems.  For a destination disk that shows up as sda, run:

    $ sudo rpi-clone sda

  Clones can be from a booted SD card or USB disk.  For a description, example
  clone runs and example usage of above options, see the README.md at:

      https://github.com/billw2/rpi-clone

  A line logging a rpi-clone run is written to /var/log/rpi-clone.log.

  Download:
    git clone https://github.com/billw2/rpi-clone

Follow me

It would be amazing if you follow myHowTo.blog. To follow leverage on

  • Click to follow me on Twitter
  • Bookmark this page and comeback from time to time

Help and Comments

I am really looking forward for you to contact me if for example you found a better option or other idea then in this how to. Also, please touch base if you found an error or anything not working or if you have something that you would love to be added to this how to. Simply click this link to touch base with me.

Linking and Recommending the HowTo or the myhowto.blog

I would love to see you are recommending this how to or link it to your website. Also, I would love if you link or recommend the whole myhowto.blog. Please feel free to do so! In case you like to touch base regarding this topic with me, then simply click this link. I look forward!