topamax once a day

Raspberry Pi Root FS on USB Drive

  

This tutorial will demonstrate how to install the Debian root filestem (ie. ‘/’) on a USB drive instead of the SD card. The SD card still retains the /boot partion and swap space. This requires a modified kernel to support the USB storage. You can download the minimal modified kernel and modules here  or follow my guide on how to compile it for yourself.

In either case, begin by installing the Debian image as you normally would to an SD card. Manually format the USB drive you intend to use as the root filesystem as ext4 using a utility like gparted.

Next, copy the root filesytem from the Debian install on the SD card to the USB drive, preserving attributes with the -a flag, substituting “sdcard-rootfs-partition-uuid” and “usbdrive-rootfs-partition-uuid” with the respective identifiers as the filesystem are mounted in Ubuntu.

sudo cp -a /media/sdcard-rootfs-partition-uuid/* /media/usbdrive-rootfs-partition-uuid/

sync

Now, delete the existing kernel.img and replace it with the new one, substituting “sdcard-boot-partition-uuid” with the identifier of the partion as it is mounted in Ubuntu.

sudo rm /media/sdcard-boot-partition-uuid/kernel.img

sudo mv kernel.img /media/sdcard-boot-partition-uuid/

Next, remove the existing /lib/modules and lib/firmware directories,

sudo rm -rf /media/usbdrive-rootfs-partition-uuid/lib/modules/

sudo rm -rf /media/usbdrive-rootfs-partition-uuid/lib/firmware/

and copy the new modules and firmware in their place:

cd ../../kernel/

sudo cp -a lib/modules/ /media/usbdrive-rootfs-partition-uuid/lib/

sudo cp -a lib/firmware/ /media/usbdrive-rootfs-partition-uuid/lib/

sync

Now we need to modify the location of the root partition in the /boot partion. This is contained within the file, cmdline.txt. In this file, change:

root=/dev/mmcblk0p2

to:

root=/dev/sda1

That’s it! Plug in and boot up the root filesystem from the USB stick!

 

16 Comments

  1. Do you get a performance boost doing this? I mean the best SD cards are 10Mbps whereas USB2 is good for 400+.

    • It certainly seems faster but I haven’t run any benchmarks to be sure. I will give some filesystem benchmarks a try (maybe iozone, postmark, or bonnie++) and let you know the results!

  2. Great news! But is the kernel-creation-copy stuff with the new official debian image still necessary?

    Best regards
    triggerhurt

    • I just gave it a try with the 2012-06-18-wheezy-beta.zip. It looks like the kernels included with the newer images already have necessary support!

  3. Managed to make the switch by changing the files on the live Raspberry system!

    Mounted the USB drive at /media/usb-stick. Copied root filesystem with rsync:

    rsync -av / /media/usb-stick –exclude={/dev/,/proc/,/sys/,/tmp/,/run/,/mnt/,/media/,/lost+found,/lib/init/rw/}

    I think you can drop the “cd ../../kernel/” from the “copy the new modules and firmware in their place:” box.

    Thanks,

    Stephan

    • Gareth

      The rysnc command you specified doesn’t work for me. It would still include all these directories, so would keep recursively copying the files already copied.

      I ended up using the following:
      sudo rsync -av / /media/usb-hdd/ –exclude “/dev/” –exclude “/proc/” –exclude “/sys/” –exclude “/tmp/” –exclude “/run/” –exclude “/mnt/” –exclude “/media/” –exclude “/lost+found” –exclude “/lib/init/rw/”

      I could’ve also put all these in a text file and used –exclude-from “textFile”

  4. I used the following commands
    sudo dd if=/dev/the-SD-root-partition of=/dev/my_external_usb_drive bs=4096
    e2fsck -f /dev/my_external_usb_drive
    resize2fs /dev/my_external_usb_drive
    The last two commands is for expanding partition space (or else it will be as big as the SD-partition)

    Don’t forget to change “root=/dev/mmcblk0p2″ into “root=/dev/your_root_device”
    Just use nano /boot/cmdline.txt (don’t forget sudo)

  5. alberto

    I am not managing to boot from Usb.
    What I am using : raspbian wheezy
    My procedure: Installed on the SD, then cloned the ext4 root partition from SD to USB with a partition software.
    First step:
    I can see in raspberry the usb is infact /dev/sda1 , so changed the cmdline.txt boot to that: Did not work, will not boot!!!

    Second step (seemed logical maybe is not) : Changed also fstab from /dev/mmcblk blah to /dev/sda1
    Will not boot?
    Any ideas? The fsystem was infact copied correctly and since I cloned the partition there shouldn’t be any modification..

  6. Hi alberto,
    Are any error messages displayed when the device fails to boot?

  7. damn, cant get it to completely work.

    It boots and all, but I cant log in, it gives me a

    Could not chdir to home directory /home/pi: Permission denied

    Tried both using the cp method and rsync. Putting the usb stick back into a normal linux computer, the permissions look fine (right owners, right flags, etc). Im confused. :-/

  8. freakqncf

    As stated elsewhere by user lewmur:
    It is possible to place all of the Raspbian files on the ext3 partition and then edit the /boot/config.txt file to point to that partition, but it wouldn’t be the easiest way.

    It would be much easier to use gparted to resize the ext3 partition and then create a new partition for the Raspbian image. The problem with trying to just copy the files to the existing partition is getting all of the permissions correct.

  9. Costas

    so, is the copy of the kernel, modules and firmware needed or not? Please clarify.

  10. Jason

    Hi, Michael. I have a similar problem to Alberto. The error I get on booting is that the kernel is unable to mount the root fs. It’s trying to mount it on “unknown-block(0,0)” which makes no sense. I specified /dev/sda2 in the cmdline.txt file in place of /dev/mmcblk0p2. (/dev/sda2 is the root fs) I have not seen anyone else with this same issue. Any thoughts?

    Thanks!

    Jason

    • Jason

      Belay my last. I reread the boot messages and for some reason my USB HDD is not recognized by the kernel at boot-time. It is afterward, in that I can alter the cmdline.txt to boot from the SD card and then I can mount the USB HDD later. That’s how I partitioned the drive and copied my rootfs to it in the first place. Any thoughts on why the rpi does not recognize the HDD at boot-time? I’m using Gentoo Linux as my OS.

      Thanks!

      Jason

  11. how to create a minimal root file system for raspberry pi witj kernel compilation for raspberry pi board with custom boot logo?how to do this please tell me..

Trackbacks/Pingbacks

  1. Raspberry Pi บูตจาก USB แทนที่จะเป็น SD Card | Raspberry Pi Thailand - […] http://mitchtech.net/raspberry-pi-root-fs-on-usb-drive/ และ […]
  2. Raspberry Pi บูตจาก USB แทนที่จะเป็น SD Card | Unofficial of Raspberry Pi Fan in Thailand - […] http://mitchtech.net/raspberry-pi-root-fs-on-usb-drive/ และ […]

Leave a Reply