Boot into USB disk with Raspberry Pi

posted on 2013-03-10

For some reason my SD card in my Raspberry Pi got corrupted. And after doing an fsck in my laptop and fixing all the problems, the filesystem got corrupted again in seconds after booting my Raspberry Pi with it. It might be because it's a cheap 32 GB card, but anyway. I still had a 512 MB old SD, so I decided to put the boot partition on that and lock it so no disk corruption could possibly occur and place the root partition on my USB disk.

The basic process is really simple: write the boot partition onto the 512 MB card, edit the configuration to point to /dev/sda1 and write the second partition (the ArchLinux root) onto an USB disk. Then everything just worked. Below are the steps I did to get it working.

Create the booting SD card

Download the ArchLinux image from Raspberry Pi downloads page.

Then write it onto the SD card from your linux console. This will halt half-way, but that is not a problem:

sudo dd if=archlinux-hf-2013-01-22.img of=/dev/mmcblk0

Now we need to remove the broken second partition (which did not fit on the disk). You can either use gparted or fdisk to remove the second partition on /dev/mmcblk0.

With fdisk /dev/mmcblk0 use d followed by 2 to remove the second partition and close with w.

With gparted just click, click, choose remove and click again.

Now mount the boot partition and edit the cmdline.txt file on there. Change the root parameter to /dev/sda1, which means the complete line will become something like this:

smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=noop rootwait

all on a single line.

Create the root partition

To create the root partition, we first create a partition table with a single primary partition on the USB disk and then copy the file system from the image onto that partition. This will leave room after the end of the file system, which we can later fill in with resize2fs.

Use kpartx to connect the partitions inside the image as loopback devices. I've created a script to create loopback devices for the partitions on the image called and mount them. It's called and I posted about it in this earlier post.

Use rawmount to mount the partitions and verify they have been mounted:

sudo bash archlinux-hf-2013-01-22.img 

The output should be something like this:

Image      : /home/bram/Downloads/archlinux-hf-2013-01-22.img
Mount base : /tmp/mount_6763/
Partitions : loop0p1 loop0p2 
Mounting: loop0p1
Mounting: loop0p2

Because the partitions are automatically mounted by, we will first unmount them to make sure the file system is not live during the copy. If you are not sure about the device names at this point, execute mount first to get a listing:

sudo umount /tmp/mount_6763/0
sudo umount /tmp/mount_6763/1

Now we can copy the root partition (set as loop0p2) to our USB disk partition we created earlier. Make sure you are confident you are picking the right devices here, otherwise you will be overwriting disk data. This is a check twice, execute once situation:

sudo dd if=/dev/mapper/loop0p2 of=/dev/sdd1

This should take on a little time, as the arch root partition is not very large. If you need to see process you can check out an earlier post on inspecting dd progress.

Now that the partition is on the USB disk, you can do either of two things: boot your raspberry pi and run resize2fs, or open gparted and run resize2fs now. I choose the first because I was to curious to see if it would even boot.

Once you have confirmed that everything boots like normal, you can just flip the SD card lock and change the /boot entry in /etc/fstab to include a read-only flag (ro):

/dev/mmcblk0p1  /boot  vfat defaults,ro  0 0

And your done!