This tutorial is written assuming you are using a Linux machine or Mac OS. Windows does not use BASH.

raspbian_os

There is no SATA port on the Raspberry Pi which is kind of annoying because who wants to write to and from an SD card all day? SD cards are annoying because they have a limited read/write cycle and when you are doing stuff like databases it wont be long before you start to get corruption. This tutorial will outline how to get Raspbian (the official Debian Linux for the Pi) installed on and booted off of a USB drive.

Step 1: Download the Raspbian Image

The Raspbian image is available for free from the official Raspberry Pi website. You will need this, nothing special here but regardless you can download it here.

Step 2: Install Raspbian To The Drive

You will need to mount your USB drive and launch your terminal. There is a command you will have to run in order to get the identifier of your disk. Enter:

1
diskutil list

A list of all the attached drives will appear. You will need to note the correct identifier here because you can do some serious damage later if you don’t know which one is the correct one. Often the drive will appear like /dev/disk1. If you do not know which one is the correct one, remove the drive and run the command again. The one that is missing will be the one you need to note. After you have identified the correct drive you will need to unmount the disk from the system:

1
diskutil unmountDisk /dev/disk1

Now comes the fun part, we will copy the files to the drive. If you have downloaded the files and left them in your downloads folder then chances are the directory is ~/downloads (~ represents the home directory). You will need to change directories to the downloads folder.

1
cd ~/Downloads

Next we will run the command to copy the files. It is incredibly important that you select the correct drive. You WILL do damage if you choose the wrong one. The numbers in the .img file may be slightly different if you downloaded it after I wrote this.

1
sudo dd bs=1m if=2013-07-26-wheezy-raspbian.img of=/dev/disk1

This takes anything from 5 to 20 mins depending on the speed of your USB Flash Drive. The computer is now moving all the data from your computer onto the drive.

Step 3: Configure the SD card

If you are using Ubuntu (or any other version of Linux) you can open up gparted and if you are using Mac OS you can open up Disk Utility (both format disks, how about that?). You will need to select the SD card and erase any partition currently there. Once that is done you will want to create a FAT 32 (MUST be FAT 32) partition. Once this is done you will be able to mount the drive. You will notice you have a mounted SD card and a mounted USB drive that you just copied your files to.

You will need to copy over the files from the USB drive to the SD card. This copies the all-important files and instructions to tell your Raspberry Pi to boot from the USB Flash Drive.

Step 4: Change the boot path

We are going to need to tell the Raspberry Pi that it needs to boot from the USB drive. Since it is generally looking to boot from the SD card, we will need to tell it where to look.  Open up the file called cmdline.txt in nano or vim (or whatever) and amend the line:

1
root=/dev/mmcblk0p2

To this:

1
root=/dev/sda2

Now this will tell your Raspberry Pi to boot from the USB Flash Drive instead of from the SD card. You are all set, you can now boot your Raspberry Pi off your USB drive.

Step 5: Expand the Raspbian partition

At this point you should boot into the Raspberry Pi

This is optional but I am not sure why you would not do this. This allows you to utilize the entire USB drive because why wouldn’t you? If you are happy without using this space you are already done. If not you can start by running this command:

1
sudo fdisk /dev/sda

Now there are several steps that you will need to follow:

  • Press P to view partitions
  • Note that the starting position is sda2
  • Press D when prompted
  • Press 2
  • Press enter, this will delete the partition

Now you will need to create a new partition:

  • Press n to create a new partition.
  • Press p for primary
  • Press 2 for the partition number
  • Press enter.

You are going be asked for a first sector, set this as the start of partition 2. After you will be asked for an ending position, you can just press enter to use the default settings. After this is selected you will need to press w to commit any changes. You can ignore any information that pops up about the kernel. You will now need to reboot.

1
sudo reboot

After the Raspberry Pi has rebooted, you can run this command to resize the partition.

1
sudo resize2fs /dev/sda2

This will take some time, when it is done reboot again. Once the machine has rebooted you are all done. You have a Raspberry Pi that boots off of and uses the entire space of a USB drive.