Running Rasbian From a USB Hard Drive

Published Published December 27th, 2016


This is a copy of a post I made on the FlightAware.com ADS-B Flight Tracking Forum a little over a year ago. I figured I would copy over the process to my website as well being I needed to refernce it for a new setup and do not wish to have to search through a bunch of posts I made on their forums in the past pertaining to USB drives and Raspbian in order touch find it again if/when I need to create this setup again.

Once again had to replace a burned out SD card on my receiver today. Kind of getting sick of swapping out SD cards every month or so, so this time I decided to move the root partition off the SD card and on to a USB drive. Supposedly this should give the set up some much needed durability as well as a performance gain. I am starting over from scratch but this method of moving the root directory over to a USB drive should work with any existing setup. If you decide to do this with an existing setup please BACKUP OR IMAGE YOUR EXISTING SD CARD BEFORE STARTING.

Boot up your running Raspberry Pi and plug in your USB drive then execute the following command.

sudo fdisk –l

The USB drive will generally show up as /dev/sda but make sure by comparing it to the list presented by the previous command. Once you have confirmed the name of the USB drive you wish to use you will need to partition the USB drive which we will do so by running the following command.

sudo fdisk /dev/sda

Delete any existing partitions by entering "d". If more than one partition is present you will need to specify the number of the partition repeating this until they are all gone. Do not exit fdisk after doing this we are not done using it yet we will need to create a couple new partitions one being an ext4 file system which will contain the root partition from here on out and another which will be used as a swap partition by the operating system.

First create the partition to be used for swap:

Enter "n" to create the partition, "p" to set it as a primary partition and then "2" to set the partition number to two. Press enter to select the default start sector then enter +2G to specify the size.

Now create the soon to be root partition:

Enter "n" to create the partition, "p" to set it as a primary partition and then "1" to set the partition number to one. Press enter to select the default start sector then select the default end sector to use up the entire disk.

Finish up by setting up a swap partions:

To set up the swap partition by enter "t" after which we will need to enter "2" as the partition number we wish to work with then enter "82" to make this a Linux Swap partition.

Confirm then write the partions:

Enter "p" to list the partitions to be written and confirm everything is right. Enter "w" to write the new partition table to the drive once everything is confirmed to be in order.

Now format the new root partition by using the following command.

sudo mkfs.ext4 /dev/sda1

Initialize the new swap partition.

sudo mkswap /dev/sda2

Mount the new root partition.

sudo mount /dev/sda1 /mnt

Install rsync and copy the files from the SD card to the USB drive.

sudo apt-get install rsync
sudo rsync -avz --exclude '/mnt' / /mnt

Now open the file /mnt/etc/fstab in your favorite text editor.

sudo nano /mnt/etc/fstab

Change the the root device from /dev/mmcblk0p2 to /dev/sda1 then add the following line to the file.

/dev/sda2 none swap sw 0 0

Next stop the system from using the swap file it has in the past.

sudo rm /mnt/etc/rc?.d/*dphys-swapfile

Then delete the old swap file.

sudo rm /mnt/var/swap

Open the file /boot/cmdline.txt in your favorite text editor.

sudo nano /boot/cmdline.txt

And change /dev/mmcblk0p2 to /dev/sda1

Now reboot your device so the changes take place.

sudo reboot

I have used these steps on multiple occasions using different hardware with things booting up perfectly. You should seem nearly no activity on your SD card with your USB drive flashing away happily instead.