Wednesday, 8 May 2013

How To move your BOOT partition to a USB flash drive or external HDD under Xbian

I wanted to try this to see if my 1.0.5a Frodo 12.2 Xbian build would boot any faster with the Xbian boot partition on a USB2 flash drive or portable hard drive.

As my video library was already on a Western Digital 320GB 2.5" portable HDD, it made sense to just create another partition on there to support the boot process. What follows is how I achieved this.

First off, credit to @Jameson for this post which is what all my effort was based on, thank you. But wait, before you get started and waste 30 minutes of your life, please be aware that it was no faster than booting from my 4GB Class 10 Strontium SD Card. The boot times I got are listed below:

  • Boot from 4GB Class 10 Strontium SD Card > 45s
  • Boot from USB2 4GB Flash Drive > 60s
  • Boot from USB2 4GB HDD partition > 64s
Some are saying there may be benefits in using a USB3 drive even under USB2 on the Pi but I haven't tested that yet. I'll update this post when I do.

What you'll need if you still want to try it:
  • A USB flash drive or portable HDD with a partition formatted in FAT or FAT32 (others may be supported but not tested) that is at least the same size as your SD Card.
  • A computer you can SSH into your Pi from and a SD card slot or reader just in case things go wonky.
  • Knowledge of how to SSH into your XBMC build.
  • The ability to use Nano to edit two files on your Pi.
As my media drive was fully formatted with NTFS for my library, I removed all content, deleted the main partition under Windows and created two new partitions; one at 4GB, the same size as my SD card. The rest of the drive was reformatted as NTFS so I could move my library back. I also tried this with a 4GB USB2 thumb drive and the steps are the same albeit the drive letter is different under Linux (/dev/sda1 vs. /dev/sda2).

Now assuming your Pi is all connected up and working and you can SSH into it, follow these steps. All commands that you need to type are in bold and italics.
  1. Hook up your external drive. Run lsusb to ensure it populates on that list. If it doesn't, it may pay just your reboot XBMC to see if it gets recognised.
  2. Run df -h to define what the paritions are for your external drive.
    1. If it's a USB drive, it should report back as /dev/sda1
    2. If it's a 2nd parition on an external drive (like mine), it will report as /dev/sda2.
  3. Assuming your USB stick is /dev/sda1 (usually is…), run sudo dd if=/dev/mmcblk0p2 of=/dev/sda1 bs=512. This will copy your SD card boot partition to your external drive. This took about 15 minutes to execute on my system.
  4. When the SSH session has returned you to the command prompt, run: sudo nano /boot/cmdline.txt 
  5. Edit where it says root=/dev/mmcblk0p2 to /dev/sda1 if you're using a thumb drive (with one partition) or in my case /dev/sda2
  6. Next, run: sudo nano /etc/fstab 
  7. Edit the line that says /dev/mmcblk0p2 to /dev/sda1 or in my case (you guessed it), /dev/sda2.
  8. Reboot by typing (sudo shutdown -r now). 
  9. You should now be booting on your USB stick!
Some points to note:

  • If it fails to boot for any reason, you can insert the SD Card back into your PC/MAC and edit the cmdline.txt file manually to restore the boot line back to root=/dev/mmcblk0p2. Your PI should now boot even though the /etc/fstab is incorrectly pointing to your external drive. SSH back into your device and follow steps 4 and 5 again to change your fstab file back to the defaults.
  • If you modify sudo nano /etc/fstab for /dev/sda1, df -h will no longer be able to see the external drive/s if you change the cmdline.txt back to root=/dev/mmcblk0p2 for any reason, at least that was my experience.
  • Even though the boot partition may only be consuming 1GB of the 3.7GB available, your external drive must be the same size or larger than the whole boot partition size.
  • To use Nano, edit the line you need and on a Windows machine type ALT+X to close the editing process, Type Y when prompted to confirm the edit and just hit Enter to accept that file name that is presented.
Good luck.