Using a USB drive as OS root on a Raspberry Pi


2012-12-29

This is how you change your root file system to use a USB drive on a Raspberry Pi.

First, let's make sure you know which partition is your root file system right now. Enter this command in a terminal on your Raspberry Pi:

df -h

It should say something like this:

treddell@penelope ~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
rootfs           15G  1.9G   13G  14% /
/dev/mmcblk0p2   15G  1.9G   13G  14% /
devtmpfs        243M     0  243M   0% /dev
tmpfs            49M  224K   49M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs            98M     0   98M   0% /run/shm
/dev/mmcblk0p1   56M   17M   40M  30% /boot

Notice that /dev/mmcblk0p2 is th same size as rootfs? That's your root partition.

Second, you need to copy your current root partition from the SD card to the USB drive. Execute this command with only your single USB drive inserted, no other drives (other than the SD card):

sudo dd if=/dev/mmcblk0p2 of=/dev/sda1 bs=4M

Depending on how large your SD card is, this could take a long time.

Next, we want to check the USB drive for errors with this command:

sudo e2fsck /dev/sda1

If that turns out okay, we can resize your new root partition to fill your USB drive:

sudo resize2fs /dev/sda1

Now you should mount your USB drive so that we can edit your future fstab file:

sudo mount /dev/sda1 /mnt

To edit the fstab, use nano with this command:

sudo nano /mnt/etc/fstab

It will look something like this:

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, so no using swapon|off...

You need to change it to look like this:

proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/sda1       /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, so no using swapon|off...

So that /dev/sda1 is mounted as root (/).

Next, unmount the USB drive:

sudo umount /dev/sda1

And edit the cmdline.txt file in your boot partition with this command:

sudo nano /boot/cmdline.txt

You will want it to look like this:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline rootwait

Notice that root=/dev/sda1.

Now you can reboot (sudo reboot) and you should be good to go with your new root partition on the USB drive. You can check it with the df -h command we used earlier and see the new size of rootfs.