Raspberry Pi running from USB hard drive

I wanted to be able to run my Pi to store logs, monitor some of my internal network etc.
I didn't like the idea of continually writing to my SD card as we all know these will die over time. So below is how I setup my pi to boot from the SD card and run the whole system from an external USB hard drive.

I'm using raspbian for the flavor of the OS on my Pi.

Short version
Install your selected OS on the SD Card and boot.
Plug in external USB hard drive.
Partition and Format hard drive.
Copy system to hard drive.
Mount the new system adjust the configuration and reboot.

Long version
Don't plug your USB hard drive in yet.
Install Raspbian in the usual way to your SD Card.
Boot your Pi and make sure it is updated to the current.
apt-get update
apt-get upgrade
rpi-update
reboot

Login to your Pi after the reboot
fdisk -l

note the output, now plugin your new usb hard drive.
fidsk -l

This should show another HDD something like /dev/sda

Now we need to create and setup our partition table on our hard drive.
fdisk /dev/sda

Enter "p" will list any existing partitions.
Delete any partitions by entering "n" and the the number of the partition to delete.

Now we will create a small swap partition and a partition for the OS.
While still in fdisk.
Create our swap partition.
Enter "n" to create a partition, "p" to select primary partition and then "1" to select the partition number one.
Select the default start sector.
Enter +2G to specify the size.
Enter "t" to set the type of a partition.
Enter "1" and then "82" to make this one a swap partition.

Create the root partition
Enter "n" to create a partition, "p" to select primary partition and then "2" to select the partition number two.
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.

Initialize the swap partition.
mkswap /dev/sda1


There is no need to format the root partition since we are going to do a raw copy of an existing file system over to it.

Copy the second partition of the SD card to the second partition of your hard disk (mmcblk0p2 may not be the correct one for you to copy, use your fdisk -l output from earlier).
dd if=/dev/mmcblk0p2 of=/dev/sda2 bs=32M conv=noerror,sync

This will take sometime to copy over.
Once completed check the file system for errors.
e2fsk -f /dev/sda2

Press "y" if any errors are encountered.

The file system that we copied still looks like a small one to the system. We will need to resize the root filesystem. to file the partition we created.
resize2fs /dev/sda2

Again this will take some time to run.

Now we modify the boot configuration to use the new root partition on the hard drive.
cp /boot/cmdline.txt /boot/cmdline.txt.orig
vi /boot/cmdline.txt

Change the text
/dev/mmcblk0p2     /

to be
/dev/sda2          /


Update fstab for the new mount configurations. To do this we have to mount the usb hard disk to modify the /etc/fstab file.
mount /dev/sda2 /mnt
vi /mnt/etc/fstab

Change the text
/dev/mmcblk0p2

to be
/dev/sda2

Add the following line at the bottom.
/dev/sda1     none     swap     sw          0     0

Finally stop the system from using the SD for the system swap file.
rm /mnt/etc/rc2.d/S02dphys-swapfile
sync
reboot

If all goes to plan your Pi should now only use the SD card for the intial boot and everything else operates off the usb hard drive.

Some USB hard drives require a delay to spin up on start up, you may have to add bootdelay and or rootdelay options to your /boot/cmdline.txt

One other issue is if you get "Volume was not properly unmounted. Some data may be corrupt. Please run fsck" you will need to reset the Dirty bit on the SD card.
cd ~
umount /boot
git clone http://daniel-baumann.ch/git/software/dosfstools.git
cd dosfstools
make
./fsck.fat -V /dev/mmcblk0p1
./fsck.fat -a /dev/mmcblk0p1
mount /boot

Cheers
Adam


Please Register.


If you wish to add comments.
Cheers
Adam