Move your Raspberry Pi system to USB in 10 steps

By SparkFun Electronics from Boulder, USA (Raspberry Pi - Model A) [CC BY 2.0], via Wikimedia Commons
Photo by Sparkfun Electronics / [CC BY 2.0]
Moving your filesystem to USB is a great way to improve reliability, speed and storage space on your Raspberry Pi.  If you’re running something that is writing a lot of data constantly then having your filesystem on USB is safer because it’s less likely to get corrupted or stop working with a high amount of write cycles.

This tutorial is using a USB stick which doesn’t require any additional external power, if you want to use a larger USB hdd you will have to also get a powered USB hub because you’re Raspberry Pi doesn’t have enough juice to power it.

I’m using Raspbian for my operating system and I’ll assume you know how to install and configure that yourself.

 

1. Do a fresh install and configure your Raspi making sure not to expand the filesystem or update.
2. Install some tools we’ll need later.
sudo apt-get update
sudo apt-get install rsync gdisk
3. Check to see if usb is recognized and check the partitions.
lsusb
sudo lsblk
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
 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
 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 on sda. From this point on I’ll use /dev/sda for the USB drive.

4. Start up gdisk and setup partition table.
sudo gdisk /dev/sda
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):
4.1 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
4.2 Delete old partition(s) with d
4.3 Make your new partition with , 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'
4.4 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.

4.5 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.
5. Format the partition and mount it
sudo mke2fs -t ext4 -L rootfs /dev/sda1
sudo mount /dev/sda1 /mnt
6. 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.

7. Backup your cmdline.txt file and edit it to include your partition.
sudo cp /boot/cmdline.txt /boot/cmdline.backup
sudo nano /boot/cmdline.txt
7.1 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.

8. 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"
9. Open the fstab file on your new filesystem
sudo nano /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

# a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that

Comment out the old filesystem at /dev/mmcblk0p2 and add the new line pointing to your new filesystem to the bottom of your file and add your UUID where indicated.

10. 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
NAME         MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
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 filesystem is mounted there.

And that’s all there is to it.