Boot Raspberry Pi from a USB disk

Posted: March 13, 2014 in Hacking and playing, Hardware

So this is more for my own reference, but if it helps others….

This assumes that you are building a new RPi image from scratch. Although you can use this method to transfer an existing SD card to the USB, I found that the 8Gb SD cards ended up being bigger than the 8Gb USB sticks…. so if you want to transfer an existing image try using a bigger USB stick than SD card.

These commands are mostly OSX/Linux based… in fact I’m doing this from memory, so they may even be interchanged.

Step 0
Install a clean image onto the SD card, boot it up and enable ssh. Don’t do anything else, we can do it later.

Step 1
First we need to copy the existing image to the hard drive. Insert your SD card, figure out which disk it is and then backup the image. You may need to unmount the SD card to be able to run the commands.

sudo dd bs=4M if=/dev/sdX of=~/backup.img

If the system complains about the 4M, try changing it to lowercase. On OSX the sdX will probably be replaced with diskX or even rdiskX (rdisk apparently runs faster).

sudo dd bs=4m if=/dev/sdX of=~/backup.img

This will take time… so go make tea or something.

Step 2
Remove the SD card, bung in the USB and make sure that it is formatted to be FAT32.

Now we need to copy the backup image back on to the USB stick

sudo dd bs=4M if=~/backup.img of=/dev/sdY

Surprise, surprise, time for more tea.

Step 3
So now you should have a SD card and a USB stick with exactly the same contents. While the RPi requires the boot directory on the SD card, the rest of the contents are basically redundant. Feel free to remove the contents of the ‘root’ partition from the SD card.

With the USB stick, we don’t need the ‘boot’ partition, just the ‘root’ stuff, so again, feel free to remove the ‘boot’ bit. From experience, it will pay to remove the device you’re NOT modifying to ensure you don’t delete the wrong bit…. repeating Steps 1 & 2 is time consuming.

The RPi setup system won’t allow you to resize a partition on a USB stick – it doesn’t understand it. So if you’ve used a big USB, now would be a good time to resize the various partitions. Personally I use gparted and also delete the ‘boot’ partition to ensure that I am using the full space on the stick.

BUT – and this is important so pay attention, you must leave 10M free on the USB. Use everything else, but for the sake of your sanity, leave 10M free!!!

RPi doesn’t assign the same /dev/sda or /dev/sdb ID to the USB ports on the board every time, so you can’t just modify the ‘boot’ partition to always use /dev/sda or /dev/sdb. If you never, ever, ever want to use another USB device, then you can probably get away with using /dev/sdb2, but better safe than sorry. So to get round this you have to use a trick involving gdisk and a PartitionUUID.

Step 4
Make sure you’ve got gdisk installed. If not..

sudo apt-get install gdisk

Once it’s in, figure out which disk is the USB and run

sudo gdisk /dev/sdX

This will give you a prompt. Hit ‘?’ for a list of commands…. we want “recovery and transformation options (experts only)”.
Type ? again and this time select “load MBR and build fresh GPT from it” from the options.
Finally ? again and “write to disk and exit”.
Remove the USB stick, count slowly to 10 and then stick it back in again. As before

sudo gdisk /dev/sdX

This time we want ‘i’ and then the partition number of your new and shiny root partition. If you haven’t removed the boot partition, it will generally be ‘2’. This will give you a summary of the partition information; things like ‘first sector’, ‘last sector’ and ‘Partition GUID code’. We are interested in the “Partition unique GUID: XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX”. Copy this UUID – we’ll need it in a moment. The 10M space we saved on the end of the USB stick is now holding this new gdisk GPT info.

Step 5
Now we need to tell the RPI where the actual root partition is. This is controlled by the ‘cmdline.txt’ file in the boot partition. Insert the SD card, fire up a suitable text editor and open this file up. We need to modify the file so that the current
is replaced with
Where the XXXX is the UUID we saved earlier. Save the file, put the SD card and USB stick into the RPi and try to boot.

Step 6
If everything went to plan, the RPi should boot as normal, except now it’s using the USB stick. We can now re-run the config, update/upgrade and install all the things we need.

The more observant amounts you may have noticed that we could have copied the SD image directly to the USB stick. I’ve done it this way so that I have a bare image I can then install on multiple USB sticks if I need it. I also have a backup image in case it goes wrong.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s