How to Move root to a USB drive

You may have gotten burned by an SD card failing in one of your Raspberries after putting in a lot of hard work. Frustrating, I know. Don’t ask how I know! SD cards trade speed for reliability. If you want something that can stand constant reading and writing, you’re look for a USB drive. I think it’s best to boot off the SD card and then mount the USB drive as your root (“/”).

Here’s the process to follow to get Raspbian moved over to more robust storage:

First, do a fresh install and configure your Raspi making sure not to expand the filesystem or upgrade. Then, install some tools we’ll need later.

sudo apt-get update
sudo apt-get install rsync gdisk

Check to see if usb is recognized and check the partitions.

pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0930:6545 Toshiba Corp. Kingston DataTraveler 102 Flash Drive
pi@raspberrypi ~ $ sudo lsblk
sda           8:0 1 14.6G 0 disk
└─sda1        8:1 1 14.6G 0 part
mmcblk0     179:0 0  3.8G 0 disk
├─mmcblk0p1 179:1 0   56M 0 part /boot
└─mmcblk0p2 179:2 0  2.7G 0 part /

As you can from lsusb the USB stick is recognized on device 004 and lsblk tells us that the USB stick is device sda. From this point on I’ll use /dev/sda for the USB drive.

Next step, start up gdisk and setup partition table.

pi@raspberrypi ~ $ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.5
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help):

Use p to list the current partitions if there are any…

Command (? for help): p
Disk /dev/sda: 30489408 sectors, 14.5 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): FF07443F-D359-4DAF-9399-BFCD862CF37F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 30489374
Partitions will be aligned on 2048-sector boundaries
Total free space is 2813 sectors (1.4 MiB)
Number Start (sector) End (sector) Size Code Name
1 2048 30488575 14.5 GiB 0700

Delete old partition(s) with d
Make your new partition with n , when prompted to enter information hit Enter to use defaults.

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-30489374, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-30489374, default = 30489374) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Get the partition information with i and save your Partition unique GUID to a text file temporarily.

Command (? for help): i
Using 1
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: FBEC95EC-C8A6-4F66-9BA3-0661B1359D0A
First sector: 2048 (at 1024.0 KiB)
Last sector: 30489374 (at 14.5 GiB)
Partition size: 30487327 sectors (14.5 GiB)
Attribute flags: 0000000000000000
Partition name: 'Linux filesystem'
The partition GUID is highlighted in bold.

Write the changes with w

Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.

Format the partition and mount it

sudo mke2fs -t ext4 -L rootfs /dev/sda1
sudo mount /dev/sda1 /mnt

Copy the filesystem over to your new partition with rsync

sudo rsync -axv / /mnt

This is going to take 15-25 minutes depending on your sd card and USB speeds so take this chance to go get yourself a drink.
When the copy process (rsync) is complete, you’ll need to do a little editing to tell the OS how to use the USB. First, backup your cmdline.txt file and then edit it to include your partition.

sudo cp /boot/cmdline.txt /boot/cmdline.backup
sudo nano /boot/cmdline.txt

Edit the file and change your “root=” to point to your new filesystem partition.

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=PARTUUID=FBEC95EC-C8A6-4F66-9BA3-0661B1359D0A rootfstype=ext4 elevator=deadline rootwait

Use the unique GUID you saved earlier. Use blkid to get your partition UUID for fstab

pi@raspberrypi ~ $ sudo blkid
/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="993B-8922" TYPE="vfat"
/dev/mmcblk0p2: UUID="fc254b57-8fff-4f96-9609-ea202d871acf" TYPE="ext4"
/dev/sda1: LABEL="rootfs" UUID="bd9300dd-1d44-4a1b-8316-fbd5407fd9f7" TYPE="ext4"

Open the fstab file on your new filesystem

pi@raspberrypi ~ $ sudo vi /mnt/etc/fstab

proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
#/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
/dev/disk/by-uuid/YOUR-UUID / ext4 defaults,noatime 0 1
root@salt:~# blkid
/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="9969-E3D2" TYPE="vfat" PARTUUID="97709164-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="8f2a74a4-809c-471e-b4ad-a91bfd51d7c3" TYPE="ext4" PARTUUID="97709164-02"
/dev/mmcblk0: PTUUID="97709164" PTTYPE="dos"
/dev/sda1: LABEL="rootfs" UUID="5d0760f3-c4a4-4939-ac13-f40da151674a" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="3c1c16ad-be65-4493-ad0c-eab912a6ff7b"
root@salt:~# vi /etc/fstab
root@salt:~# cat /etc/fstab
proc /proc proc defaults 0 0
PARTUUID=97709164-01 /boot vfat defaults 0 2
PARTUUID=97709164-02 / ext4 defaults,noatime 0 1
a swapfile is not a swap partition, no line here
use dphys-swapfile swap[on|off] for that

Apparently, my UUID is “5d0760f3-c4a4-4939-ac13-f40da151674a” so I’ll put that in my /etc/fstab in the format:

/dev/disk/by-uuid/YOUR-UUID / ext4 defaults,noatime 0 1

So now I’ve got:

/dev/disk/by-uuid/5d0760f3-c4a4-4939-ac13-f40da151674a / ext4 defaults,noatime 0 1

Save and Reboot!

If all went well your Raspberry Pi should be up and running using your new filesystem on your USB stick. To double check that you’re running off the USB stick use the command lsblk .

pi@raspberrypi ~ $ sudo lsblk
sda 8:0 1 14.6G 0 disk
└─sda1 8:1 1 14.6G 0 part /
mmcblk0 179:0 0 3.8G 0 disk
├─mmcblk0p1 179:1 0 56M 0 part /boot
└─mmcblk0p2 179:2 0 2.7G 0 part

Notice the / to the right of our sda1 partition which indicates that the root file system is mounted there. All is well! NO more fried SD cards!


One thought on “How to Move root to a USB drive”

  1. Thank you very much, John! This solved it after couple of hours trying.
    I figure that the fstab entry ON THE USB FILESYSTEM was the bit I was missing.
    Best Regards, Jannis

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.