Hard Disk Setup

Below are the details on moving the root volume for an RPI from the SD card to a USB hard disk.
This can be useful when doing development work or something that requires a lot of writes to the disk.
One example would be compiling a new Linux kernel.

Note I've setup a partition for swap in case I want to use it later
but I'm avoiding using it to get better latency performance

The first step is to partition the disk using fdisk, I'm assuming a partition layout of the following:

  • /dev/sda1 - boot (vfat not used)
  • /dev/sda2 - swap (swap partition) - not used
  • /dev/sda3 - root (ext4)

It's possible to set the EEPROM to boot directly from a USB disk.
But for now I'm avoiding that so that I have the option to just boot from an SD card.

Format Partitions

Note this will destroy data on the drive.

# Format the swap parition
/sbin/mkswap /dev/sda2
# Format the HD Root partition
/sbin/mke2fs -t ext4 -L rootfs /dev/sda3

Copy root volume across

We can use rsync to copy across the root volume data.

# mount the new root volume
mkdir -p /mnt/hdroot
mount -t auto /dev/sda3 /mnt/hdroot

# Copy the root volume across
apt install rsync
rsync -axv / /mnt/hdroot

Edit fstab

Next we need to edit the fstab file for the root volume on the USB disk.
Run /sbin/blkid /dev/sda3 to get the partition UUID then substitute it within the config
Note we've commented out the swap partition here

# Get the partition unique id
# this will report a unique number, something like 939aac7c-03, 939aac7c-02
# /dev/sda3: LABEL="rootfs" UUID="3eac140f-5f87-402a-b5bf-9ff1fb881bc4" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="939aac7c-03"
/sbin/blkid /dev/sda2
/sbin/blkid /dev/sda3

# Open up the fstabl file on the usb disk
vim /mnt/hdroot/etc/fstab

# Original Example
PARTUUID=a0998a78-02  / ext4 noatime,lazytime,rw 0 1
/var/swap none swap sw

# New Example
PARTUUID=939aac7c-03  /  ext4  noatime,lazytime,rw 0 1
# We avoid enabling swap to get better latency
#PARTUUID=939aac7c-02  sw swap  none                0 0

Finally, we can unmount the root partition with unmount /mnt/hdroot

Alter the Boot Configuration

Next we need to set the configuration on the SD card to boot to the USB disk.
Instead of the root volume on the SD card.
We can do this by editing the /boot/cmdline.txt file to point to the new partition

# Make a backup of the file in case we want to switch back to the SD card
cp /boot/cmdline.txt /boot/cmdline.txt.sdcard

# Edit the file
vim /boot/cmdline.txt

# Original Example
root=PARTUUID=a0998a78-02 rootfstype=ext4 rootwait net.ifnames=0 logo.nologo console=tty1

# New Example
root=PARTUUID=939aac7c-03 rootfstype=ext4 rootwait net.ifnames=0 logo.nologo console=tty1

# Make a backup of the file
cp /boot/cmdline.txt /boot/cmdline.txt.hd

Then reboot to switch to the root volume on the hard disk.
If for any reason something goes wrong, you can just open up the SD Card boot partition
(since it's vfat) on another machine and copy the backed up file cmdline.txt.sdcard to cmdline.txt.