Networking HowTos
Networking HowTos

Raspbian on Raspberry Pi using SD card + USB memory stick

August 5, 2012 Linux, Other

Raspbian is a Debian based Linux distribution, specifically for use with the Raspberry Pi. The Raspberry Pi requires that the system is initially booted of SD. Because of this, a default install of Raspbian requires an SD card of at least 2gb.
I received my Raspberry Pi the other day, and didn’t have a spare 2gb SD card that wasn’t already being used, but I had a few smaller ones around the place (128mb, 256mb, etc). I wanted to find a way to utilize one of these smaller SD cards just for the system boot files, and then run the main system off a USB memory stick.
It turns out that the Raspbian image contains a boot partition, and as long as you can copy this to a SD card (and modify the cmdline.txt file to point to the root partition on the USB memory stick), you will be able to initially boot of a small SD card, then load the rest of the system off a larger USB memory stick.

Requirements:

  • A linux PC (Only required for this guide. The same process could be done using tools on other operating systems).
  • SD card (minimum 64mb)
  • USB memory stick (minimum 2gb)

Determining device names:
Find the device names for both your USB device, and your SD device. You can usually use the ‘dmesg’ command to find the device names for your storage devices.
Eg: This shows the dmesg output after plugging in a USB memory stick.

$ dmesg | tail
[4489268.141393] scsi 18:0:0:0: Direct-Access     SanDisk  Cruzer Switch    1.20 PQ: 0 ANSI: 5
[4489268.143286] sd 18:0:0:0: Attached scsi generic sg10 type 0
[4489268.146251] sd 18:0:0:0: [sdj] 7821312 512-byte logical blocks: (4.00 GB/3.72 GiB)
[4489268.150144] sd 18:0:0:0: [sdj] Write Protect is off
[4489268.150156] sd 18:0:0:0: [sdj] Mode Sense: 43 00 00 00
[4489268.150163] sd 18:0:0:0: [sdj] Assuming drive cache: write through
[4489268.158814] sd 18:0:0:0: [sdj] Assuming drive cache: write through
[4489268.162381]  sdj: sdj1 sdj2
[4489268.173591] sd 18:0:0:0: [sdj] Assuming drive cache: write through
[4489268.176965] sd 18:0:0:0: [sdj] Attached SCSI removable disk

In my examples, /dev/sdf is the SD card, and /dev/sdj is the USB memory stick.
Obtaining Raspbian:
Download the latest Raspbian release from http://www.raspberrypi.org/downloads
At the time of writing, the latest release was 2012-07-15-wheezy-raspbian.zip
Installing Raspbian to a USB memory stick:
Extract the zip to a location on your PC.

unzip 2012-07-15-wheezy-raspbian.zip

This should extract a single file named “2012-07-15-wheezy-raspbian.img”.
Write this image to your USB memory stick device. Make sure you get the “of” (output file) part of the command line parameters correct, so you don’t overwrite your hard drive, or any other storage device.

sudo dd if=2012-07-15-wheezy-raspbian.img of=/dev/sdj

2012-07-15-wheezy-raspbian.img being the input file.
/dev/sdj being the output file/device (the USB memory stick device).
Preparing the SD card:
Make sure you obtain the correct device name for your SD card. In this example, my SD card is /dev/sdf.
Remove any partitions that may already exist on the SD card.
(you can do this via fdisk, or use ‘sudo dd if=/dev/zero of=/dev/sdf’)
Create a single FAT32 partition.

$ sudo fdisk /dev/sdf

You should be presented with the following prompt:

Command (m for help):

press ‘n’ to create a new partition

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)

Press ‘p’ to create a primary partition.

p
Partition number (1-4):

Press ‘1’ to create the first partition.

Partition number (1-4): 1
First cylinder (1-1024, default 1):

Press enter to automatically select the default first cylinder.

Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1024, default 1024):

Press enter again to automatically select the last cylinder.

Using default value 1024
Command (m for help):

Press ‘t’ to change the partition id

Command (m for help): t
Selected partition 1
Hex code (type L to list codes):

Enter in the hex code ‘c’. This will change this partition to be FAT32.

Hex code (type L to list codes): c
Changed system type of partition 1 to c (W95 FAT32 (LBA))

Press ‘w’ to write changes to the device, and exit.

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.
$

Now that you have a FAT32 partition created, you need to put a filesystem on it.

sudo mkfs.msdos -v /dev/sdf1

eg:

$ sudo mkfs.msdos -v /dev/sdf1
mkfs.msdos 3.0.7 (24 Dec 2009)
/dev/sdf1 has 8 heads and 61 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 499650 sectors;
file system has 2 16-bit FATs and 8 sectors per cluster.
FAT size is 244 sectors, and provides 62391 clusters.
Root directory contains 512 slots.
Volume ID is 43181ecb, no volume label.
$

Installing the boot files onto the SD card:
Create a couple of folders under /tmp for use when mounting the USB memory stick, and the SD card.

$ sudo mkdir /tmp/mnt_usb
$ sudo mkdir /tmp/mnt_sd

Mount the USB memory stick.

$ sudo mount /dev/sdj1 /tmp/mnt_usb

Mount the SD card.

$ sudo mount /dev/sdf1 /tmp/mnt_sd

Copy across the boot files from the USB memory stick to the SD card.

$ sudo cp /tmp/mnt_usb/* /tmp/mnt_sd/

Modify the cmdline.txt file to tell the system where to find the root partition.

$ sudo nano /tmp/mnt_sd/cmdline.txt

modify this line:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

to read:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda2 rootfstype=ext4 elevator=deadline rootwait

(This is just changing /dev/mmcblk0p2 (the 2nd partition on the SD card) to /dev/sda2 (the second partition on the USB memory stick). This tells the Raspberri Pi device where to find the root partition.
Save and exit the editor.
Finishing up:
Unmount the devices.

$ sudo umount /tmp/mnt_usb
$ sudo umount /tmp/mnt_sd

Remove the temporary mount folders. (Make sure they unmounted OK first, otherwise it will delete the data on the device itself).

$ sudo rm -rf /tmp/mnt_usb
$ sudo rm -rf /tmp/mnt_sd

You can now unplug your USB memory stick, and SD card, and insert them into your Raspberry Pi.
Power up your Raspberry Pi, and make sure it boots up into Raspbian correctly. If all goes well, it should boot up fine.

You Might Also Like

  • Schop August 27, 2012 at 1:31 pm

    A little addition to this howto: You can create a new, empty, partition table in fdisk with the ‘o’ command.
    Command (m for help): o
    This is easier than the dd command in the section below, and less dangerous.
    “Remove any partitions that may already exist on the SD card. (you can do this via fdisk, or use ‘sudo dd if=/dev/zero of=/dev/sdf’)”

  • Simon October 3, 2012 at 11:42 pm

    If you want to move an existing Raspbian installation from SD card to USB memory stick, have a look at this post here
    https://zeroset.wordpress.com/2012/10/03/move-an-existing-raspbian-installation-from-memory-sd-card-to-usb-flash-drive-stick/
    It works pretty straight forward.

  • matthijs December 27, 2012 at 3:08 am

    Why is the minimum size of the sd card 64MB?
    Would a 32MB sd card also work?

    • admin December 27, 2012 at 12:43 pm

      The kernel/boot up files total about 34mb, so its just over the 32mb limit, and the next biggest size SD card would be 64mb.
      There is a ‘cutdown’ and an ’emergency’ kernel image on there, which totals about 20mb, so if you removed those, it would fit on a 32mb SD card, however I haven’t tested this.

  • Funman1111 December 29, 2012 at 9:23 am

    Is there a way to make it boot from an external hard drive I want to make a portable com…I will most likely do the card but I still want to no

    • admin December 29, 2012 at 4:55 pm

      Hi,
      You should be able to use a external HDD in place of the USB flash memory stick, but you will still need a SD card. The Raspberry Pi only looks on the SD card for the initial boot/kernel files.

  • Sudar January 11, 2013 at 2:43 am

    Thanks for the excellent guide.
    I found two typos. Would be good if you can correct them.
    sudo mkfs.msdos -v /mnt/sdf1 should be sudo mkfs.msdos -v /dev/sdf1
    and
    sudo copy /tmp/mnt_usb/* /tmp/mnt_sd/ should be sudo cp /tmp/mnt_usb/* /tmp/mnt_sd/

    • admin January 11, 2013 at 9:08 am

      Hi Sudar,
      Thanks for the comments. I have updated the article with the typo’s corrected.
      Cheers.

  • Sudar January 11, 2013 at 4:19 am

    I have followed your method and I am able to boot now.
    But I used a 4GB USB stick and if I try to do expand roots in raspi-config, I am getting a error.
    Do you how I can expand the root filesystem after following the above method?

    • Sudar January 12, 2013 at 2:43 am

      I found out how to do it. I just need to edit the raspi-config script and change reference to SD card with USB Pen drive

      • admin January 12, 2013 at 10:11 pm

        Thanks for the update. I’m sure others will find this information useful too.

  • peer February 6, 2013 at 8:23 pm

    Won’t this leave you with an unused boot partition on the usb disk?
    I use
    $ sfdisk -uS -l image.img
    and
    $ sudo mount -o loop,offset=$(( 512 * 9999)) image.img /mnt/boot
    to get access to the boot files
    and cpio to copy the root partition on usb after
    $ sudo mount -o loop,offset=$(( 512 * 9999)) image.img /mnt/root

    • admin February 6, 2013 at 9:53 pm

      Thanks for the comment.
      You’re right, it will leave an unused partition on the USB device. I wasn’t too concerned by the wasted space on the USB memory stick, as it was minimal, and I wanted to keep the filesystem layout the same in case there were issues in /etc/fstab.
      I haven’t tested your method, but it looks like an easier way to do it. Always good to see alternate methods.

  • dark.tweek March 21, 2013 at 9:49 pm

    Hi, (sorry for my bad english)
    Verry good “how to”!
    Just think about adding controls to resize the usb drive after with rasp-config:
    cp /usr/bin/raspi-config ~
    sed -i ‘s/mmcblk0p2/sda2/’ ~/raspi-config
    sed -i ‘s/mmcblk0/sda/’ ~/raspi-config
    sudo ~/raspi-config
    And do the resize normaly with a reboot at the end.