Raspberry Pi – booting from USB

So! RPi post 3!
NOTE: This was pretty much an abject failure and is only included here for completion purposes (i.e. it was on the wrong blog and I wanted it moved!)

Objective
Get the Pi booting from a USB flash drive.

Why?
For reasons of speed and to allow more to be installed on the operating system.

Caveat
It’s impossible to boot from a USB flash drive directly. What you will be doing is to use the SD card as a ‘bios’ and then bootstrap it to jump to the flash drive.

Main reference
I decided not to try and do this all by myself. I tried the guide here:

but found that it didn’t give me enough information to do anything with.
I then found this guide from Andrew Ord in Somerset, UK:

http://andreword.co.uk/Applications/Raspberry_PI/Entries/2012/6/4_Preparing_the_destination_drive.html

It’s not completely accurate (or wasn’t when I ran through it on July 4/5 2012, so here’s an updated version.

My set-up

  • Raspberry Pi model B.
  • 2GB SD card (from MobyMemory, their own brand).
  • 16GB flash drive. (from Play.com)
  • USB keyboard.
  • Ethernet cable.
  • A 3.5″ car rear view camera screen. (You can use any monitor/screen that will connect)
  • Laptop running Windows 7 and the Putty terminal.

A – Preparation
Install the Debian Wheezy Beta distribution. This was done on Windows. You may find that the image writer doesn’t work. There are alternatives, but the most import thing is to get this done. You can’t go any further unless you do.

  1. Download from the link above.
  2. Download the newest version of win32 diskimager from  https://launchpad.net/win32-image-writer/+download
  3. Using it, write the image you downloaded to your SD card.
  4. Put the card in pi.
B – Devices and Partitions and Swaps, Oh My!
Sort out partitions and swaps and mount points. NB This will wipe your USB card
  1. Boot up the Pi with your flash drive and other cables plugged in. All the steps from here on (up to the networking step) were done over a Putty terminal.
  2. Log in as root with password root. (You’re going to sudo loads of stuff anyway, so logging in as root seems sensible).
  3. ls /dev and look for a device called ‘sda’. This is the whole USB flash drive. You are now going to sort it out so you can actually use it.
  4. fdisk /dev/sda
  5. Do p <enter> to see the partition table.
  6. Delete any existing partitions by doing d <enter> and picking the partition if necessary.
  7. Create new partitions:
  8. Do n <enter> p <enter> 1 <enter>
  9. Hit <enter> so it takes the default ‘start’ position.
  10. Choose an appropriate size for the main boot partition by following the instructions on that line. I think it’s something like size=+12GB or something. It says it on screen.
  11. Once you’re back to the fdisk console, do n <enter> p <enter> 2 <enter>.
  12. Use the default ‘start’ and ‘end’ positions (so that it automatically takes up the whole disk).
  13. This gives you two partitions. Exit from fdisk.
  14. At the prompt, type mkfs.ext4 /dev/sda1 (formats the main partition)
  15. Then do mkswap /dev/sda2 (which formats the swap drive).


Using the most recent Debian SD card image, I’ve booted up with the external drive attached to the second USB port

1.Logged in to debian with the pi/raspberry account
2.sudo su (switch to su mode without the root password)
3.ls /dev
(list all devices, depending on the type of drive)
Look for /dev/sda
Note your SD card will be listed as /dev/mmcblk0 (with partitions p1, p2, p3)
4.fdisk /dev/sda (obviously change this to match your HDD configuration)
5.p (print the partition table)
6.d <Enter> <number> (for each partition to delete them)
7.n <Enter> p <Enter> 1 <Enter> (Choose appropriate size for your main file area)
8.n <Enter> p <Enter> 2 <Enter> (make sure to have left yourself some swapspace)
9.You can set the boot flag if you want, but it isn’t needed since your booting from the SD card
10.mkfs.ext4 /dev/sda1 (format the first partition)
11.mkswap /dev/sda2 (format the swap space)

Now we need to prepare the installation onto this new drive ready to boot
1.mkdir /a (create a folder called a in the root, this is where we will build our new install)
2.mount /dev/sda1 /a (mount our new drive on /a)
3.apt-get update (update our package sources)
4.apt-get install debootstrap (install debootstrap)
5.debootstrap squeeze /a http://192.168.1.11:3142/ftp.debian.org/debian/
(do a minimal build onto our folder)
I use apt-cacher-ng on another machine, use http://ftp.debian.org/debian/

Once the packages have downloaded and installed we should have an extremely minimal debian install in /a
Let’s move across into this new environment and start to build it up to be able to operate on it’s own

Download the kernel file from http://www.bootc.net/raspberrypi/linux- … _armel.deb
(Note : You may find that the version has been updated since I wrote this note, please go to http://www.bootc.net/raspberrypi/ to find the latest file name if needed)

1. wget http://www.bootc.net/raspberrypi/linux- … _armel.deb
Copy the downloaded files, linux-image-3.2.18-rpi1+_5_armel.deb and linux-raspberrypi-3.1.9-18-arm.pkg.tar to your new area (/a/root/installation was where I put the files)

1.chroot /a (Change root to our new folder)
2.passwd <Enter> (follow instructions to set the new passwd for root on your new install)
3.apt-get update
4.apt-get install aptitude openssh-server locales ntp
5.dpkg-reconfigure locales

This should provide us with a remotely accessible platform once we get the kernel installed

Let’s get the kernel and modules installed
1.cd /root/installations
2.dpkg -i linux-image-3.2.18-rpi1+_5_armel.deb

The new kernel is now stored in /boot. In order to update your SD card to use this kernel you now need to do a little slight of hand

The file you need is in /boot called vmlinuz-3.2.18-rpi1+, and to use it we need to copy this file and rename it to kernel.img and store it on the SD card

When I applied the kernel update on my SD card, I did the following
1.mkdir /tempSD
2.mount /dev/mmcblk0p1 /tempSD
3.mv /tempSD/kernel.img /tempSD/kernel.img.original
4.cp /boot/vmlinuz-3.2.18-rpi1+ /tempSD/kernel.img
5.nano /tempSD/cmdline.txt
6.(modify the boot command by changing root=/dev/mmcblk0p2 to root=/dev/sda1)
7.CTRL+O,<Enter>,CTRL+X
8.umount /tempSD
9.rmdir /tempSD

These changes now mean you have the up to date kernel on the SD card and you’ve modified the boot commands to use your USB HDD as the root file system

Now do a reboot (reboot <Enter>) and let’s see what happens

The first thing you should notice is that it boots up fairly quickly. Always a good sign I think

The second is that you should be able to logon as root, using the password you set earlier

Now we need to make a few modifications to the configuration in order to automatically get the internet connection up and running

1.Log on to the PI
2.nano /etc/network/interfaces
3.Add the following lines to the file if they are not already present
auto eth0
iface eth0 inet dhcp
4.Before we forget, we need to turn on our swap if it hasn’t already
5.nano /etc/fstab
add the following line
none    /dev/sda2    swap    sw    0    0
6.exit
7.swapon /dev/sda2
8.To address a bug I keep finding with the network connection, edit the sysctl.conf file in /etc and include the line vm.min_free_kbytes=32768
9.Now you should be able to reboot and have networking available

XBMC time (incomplete text, still trying to make this bit work correctly)

Edit apt sources.list file to include a local mirror for debian-multimedia

When you try to do an apt-get update you will get an error due to the gpg key, use the following command and substitude the code returned in the error message in order to correct the problem
1. wget -q “http://keyserver.ubuntu.com:11371/pks/lookup?op=get&search=0x4874D3686E80C6B7” -O- | sudo apt-key add –

Now we can install xbmc using “apt-get install xbmc lxde”. File sizes are around 260Mb alone for XBMC, probably about the same for LXDE packages so be prepared to wait

1. dpkg-reconfigure x11-common
2. Choose Anybody

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.