Raspberry Pi How To: Run from an external HDD

This author of the following article is Ted B Hale. The article is reproduced from this website for the purpose of grouping radio related RaPi articles together on  this site. Although the article refers to MySQL server, the situation is very similar for a RaPi used as repeater controller if the user want's to enable logging (and hence cause lots of disk write access.

Running From an External Hard Drive

The SD card of the Raspberry Pi is required for booting.  While you may not be able to boot from an external hard drive, moving the / partition there will significantly speed up the Raspberry Pi.  There are many uses where this configuration particularly useful.  File servers and media servers are good examples.  I need a system like this for a MySQL database server.

There are a number of variations to the process, but basically, the steps are:
o  Install OS on SD Card and boot 
o  Plug in external hard drive
o  Partition and Format hard drive
o  Copy system to hard drive
o  Mount the new system and modify configuration to use the hard drive

I decided not to re-invent the wheel.  The following instructions are just my variations on the instructions provided by Rattus here: 

Note about the sudo command:  All of the following must be done as root.  You must put sudo before each of the commands.  Or, you can follow the (not recommended, bad practice) habit I have and just enter sudo bash to run a shell as root and skip saying sudo all the time.
Load SD Card with Raspbian in the usual way. Update the system software and firmware with the following three commands.  This can take quite a while to complete.
apt-get update
apt-get upgrade



List the partitions  This should show only the /boot and / partitions on the SD card.

fdisk –l

Plug external hard drive into USB and list the partitions again.  This should show another HD probably at /dev/sda

Modify the partition table for the external drive.    
fdisk /dev/sda
Delete partitions by entering “n” and then the number of the partition to delete.  Delete all the partitions.  Entering “p” will list the partitions.  

At this point, you have a few options.  
a)  Create a single partition and move the root there.  This will leave the swap file on the SD card.
b)  Create a root partition that fills most of the drive and a small swap partition.
c)  Create a partition for the OS, a small swap partition, and a large partition for application data. 

I am using the third option.  The following instructions are fairly easy to change to use one of the other options.  While still in the fdisk program, do these steps. 

- Create the root partition:  I made mine 16GB.  Enter “n” to create a partition, “p” to select primary partition and then “1” to select partition number one.  Select the default start sector.  Enter +16G to specify the size.  
- Create the swap partition:  Enter “n” to create a partition, “p” to select primary partition and then “2” to select partition number two.  Select the default start sector.  Enter +2G to specify the size.  Enter “t” to set the type of a partition.  Enter “2” and then “82” to make this one a swap partition. 
- Create the data partition:  Enter “n” to create a partition, “p” to select primary partition and then “3” to select partition number three.  Select the default start sector and default end sector to fill up the rest of the disk.  
- Enter “p” to list the partitions and verify things are like you expect. 
- Enter “w” to write the new partition table and exit. 

Create (format) the data partition with an EXTv4 file system.  This command takes a few minutes to finish.  There is no need to format the first partition since we are going to do a raw copy of an existing file system over it anyway. 
mkfs.ext4 /dev/sda3
Initialize the swap partition

mkswap /dev/sda2

Copy the second partition of the SD card to the first partition of your hard disk (mmcblk0p2 may not be correct for you see the output of fdisk earlier.)  This also takes a while.
dd if=/dev/mmcblk0p2 of=/dev/sda1 bs=32M conv=noerror,sync

Check the file system for errors.  Press “y” if any errors are found. 
e2fsck -f /dev/sda1
[VK3ERW: If you get lots and lots of errors, something has gone wrong go back to the pervious step]

The file system that was copied still looks like a small one to the system.  Do the following to re-size the file system to fill the partition.
resize2fs /dev/sda1

Use your editor of choice (I  use vi) to modify the boot configuration to use the new root partition.  First make a backup copy.
cp /boot/cmdline.txt /boot/cmdline.orig
vi /boot/cmdline.txt

Change  /dev/mmcblk0p2 /  to be   /dev/sda1

Change the mount configuration file on the new root.  It has to be mounted first. 
mount /dev/sda1 /mnt
vi /mnt/etc/fstab

Change the root device  /dev/mmcblk0p2 /   to be  /dev/sda1
Add /dev/sda2  as  a swap partition by adding this line.
/dev/sda2    none      swap    sw           0       0

Finally, stop the system from using the swap file that it normally uses. 
rm /etc/rc2.d/ S02dphys-swapfile 

Flush the disk and reboot.sync

The Pi should now be running from the external hard drive.  If it fails, use another machine to 
copy /boot/cmdline.orig   to  /boot/cmdline.txt  and then reboot again.

One problem that some people have run into is controllers that take too long to complete the USB discovery.  This can be compensated for by adding delays in the /boot/cmdline.txt file with the bootdelay and rootdelay options.