If you’ve read some of the other articles on the site, you may have gathered that I have three RasPis doing useful things around my home:
- Pi #1 is running Raspbmc, an XBMC media centre port for the Raspberry Pi.
- Pi #2 is running this website plus an email server.
- Pi #3 is running the Dropbox replacement OwnCloud.
All three of these ran reasonably quickly out of the box, but because the Pi is such a low powered device, every little performance boost helps.
Having said that, this is no small improvement, and the performance gain is instantly apparent.
Raspbmc’s installer has a built-in boot to SD card function – the first time you power it on, if you have a USB plugged in it will automatically set things up for you. So #1 is taken care of. That leaves #2 and #3, for which the solution is the same (the solution applies to anything running Raspbian, and may work for some of the other Pi distributions too, although I haven’t checked).
The first thing to understand here is that a Raspberry Pi is currently incapable of booting without an SD card. So although you can move your filesystem to a USB, you still need to keep the SD card plugged in.
A normal Raspbian image contains two partitions. One is the boot partition and the other is the root filesystem. The boot partition contains all the bits that your Pi looks for when it starts up, and the root filesystem contains all of the programs that you run once the system is up and running, organised in the familiar filesystem hierarchy.
What we are about to do is tell the Raspberry Pi that the root filesystem is on a USB stick, and then delete the old root on the SD card, leaving only the boot partition.
I’m going to assume that you have already made changes to your Pi that you would like to keep, and are not starting from scratch, as I doubt many people will have been foresighted enough to know that they would like to do this when they started tinkering (I certainly wasn’t!).
All of these modifications are done on a Linux laptop or desktop (the Pi is off for the duration).
Step 1 – back up your SD card
Make a note of which drive your SD card is (you can check this in your partition manager).
Now to make an image of your card and save it to your hard drive, run the following command:
sudo dd bs=4M if=/dev/sdb of=~/backup.img
(Where /dev/sdb is your SD card)
This should take about 5 minutes, just be patient.
Step 2 – copy the image over onto the USB stick
Once you have a backup image, you can insert your USB drive and copy the image over.
sudo dd bs=4M if=~/backup.img of=/dev/sdc
(where /dev/sdc is your USB stick – again, you can check this in your partition manager)
NB: we could have copied directly from the SD card to the USB stick, but it’s useful to have a backup just in case you mess it up, or your drive gets corrupted at a later date.
Step 3 – remove the partitions you don’t need
You should now have one SD card and one USB drive with identical content. You don’t need the boot partition on the USB drive, and you don’t need the root filesystem on the SD card. You don’t have to do this, but you can open your partition manager and delete those partitions to keep things neater (this will also stop you from getting confused about which is the one in use).
You may find it easier if you remove the SD card to edit the USB drive and vice versa, it’s easy to mix the two storage devices up.
Now is also a good time to resize the root filesystem on the USB drive using your partition manager. Extend it to use the free space at the end of the disk, but leave 10 MiB free at the end. I’ll explain why later.
Step 4 – work out how you are going to identify your root filesystem
Now all you need to do is tell the RasPi where the root filesystem is. This is done in a file called cmdline.txt.
The key parameter is
root=/dev/mmcblk0p2. If you are never going to plug another USB device into the Pi, then you can change this to
root=/dev/sda2 (the second partition on
/dev/sda). Note that even if you deleted the first partition, the second will still identify itself as
/dev/sdX2 unless you create a new partition table, so stick with
BUT, if you think you might use another USB device with the Pi, then you may run into problems. You might think that the RasPi always assigns the top USB /dev/sda and the bottom USB /dev/sdb… not so. Which device gets which slot is random, so /dev/sda2 might not be the partition you think it is if more than one USB device is attached.
Usually, on Ubuntu and other Linux distributions, you can refer to USB drives by their Universally Unique ID (UUID). Undortunately, the Raspberry Pi bootloader isn’t as sophisticated as Ubuntu’s GRUB. I’ve tried assigning the root location by UUID, but it doesn’t work.
However, I did manage to find a solution here. While you can’t use the UUID, you can use the PARTUUID.
First, you need to convert your partition table from DOS to GPT. To do this, you’ll need the commandline tool gdisk. * If you didn’t leave that extra 10MiB at the end of the disk, you’ll get an error at this stage about the partition table and a partition overlapping.
sudo apt-get update sudo apt-get install gdisk gdisk /dev/sdb
/dev/sdb is the usb drive).
You will now be greeted with a prompt. Type ? and press enter to list your options.
Now choose “recovery and transformation options (experts only)”.
Again, type ? and then choose “load MBR and build fresh GPT from it”.
Finally, type ? and choose “write table to disk and exit”.
Now take the USB drive out, re-insert it, and run gdisk again:
sudo gdisk /dev/sdb
Now press “i”, and then “2″ to display detailed information on the second partition on the drive /dev/sdb2. You should get output like this:
Partition number (1-2): 2 Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem) Partition unique GUID: CA9D1961-4627-46BB-BACA-7C7106977E5C First sector: 122880 (at 60.0 MiB) Last sector: 60545023 (at 28.9 GiB) Partition size: 60422144 sectors (28.8 GiB) Attribute flags: 0000000000000000 Partition name: 'Linux filesystem'
The “Partition unique GUID” is what we’re after. Paste it into a text editor for now, you’ll need it later.
Step 5 – edit the boot partition on the SD card
Mount the boot partition on the card, locate the partition, and then edit
We need to change
root=PARTUUID=XXXXXX (fill in your Partition Unique GUID in the place of XXXXXX).
cd /media/boot ls sudo nano cmdline.txt
Press CTRL+X when you’re done, and say yes when prompted to save.
Close the terminal window and unmount the USB drive.
Step 6 – Boot your Pi to the USB drive
Now all that’s left is to insert your SD card, your USB drive, and reconnect the power cable.
If all goes well you should boot into a much speedier system, with greater stability and reliability.
I wrote this tutorial in October 2013, and was running an email server and a WordPress website on the Pi from then until March 2014... and then the flash drive bricked and I lost everything because I didn't have a backup!
So, just remember: USB flash drives are faster and more reliable than SD cards, but they aren't really meant to be used for this kind of application. As a result, you should keep regular backups!
The website I was running was generating 1100 visits and about 10k hits every day - imagine how different that is to the occasional reads and writes that the USB drives are designed for. If you want a more reliable server, you can use a self powered HDD and use this tutorial to put the root filesystem on that.