I recently bought a Raspberry Pi 2. At the same time I ordered a USB 3.0 flash disk, because I was afraid the SD-card would be too slow. I'm now going to put the root filesystem of Raspbian on the faster USB flash disk, while keeping the bootloader and /boot on the SD-card, because the rPi can't boot from USB.
Get the Raspbian image
My original idea was to use the Raspbian installer so I could install Raspbian like I'd install Debian. The installer on www.raspbian.org wouldn't boot on my rPi2. According to the timestamp, it was 2.5 years old. Maybe it doesn't support the rPi2 yet.
So instead, I downloaded the Raspbian image from the Raspberry Pi site, at the time the .zip file was 975 MB.
Inside the .zip file is the entire Raspberry OS, 3.1 GB in total. When you extract it, you end up with an .img file.
$ unzip 2015-02-16-raspbian-wheezy.zip Archive: 2015-02-16-raspbian-wheezy.zip inflating: 2015-02-16-raspbian-wheezy.img
Prepare the USB-stick
Insert the USB-stick in your computer and find out which device name it gets, using
Create a partition on your USB-stick for the root filesystem. My stick was 16 GB, so I used 15 GB for / and 1 GB for swap:
Disk /dev/sdc: 14,7 GiB, 15787360256 bytes, 30834688 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x000d032d Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 28733439 28731392 13,7G 83 Linux /dev/sdc2 28735488 30832639 2097152 1G 82 Linux swap / Solaris
Copy / from .img to USB
The .img file contains two partitions, one is
/boot and the second is
/. The contents of the first go on the SD-card, and the other goes on the USB-stick.
The tricky part is dumping from the exact offset so you get all the files (blocks actually) you need, and nothing more. To figure out what the offset of the second partition is, use
fdisk -l on the .img file:
$ fdisk -l 2015-02-16-raspbian-wheezy.img Disk 2015-02-16-raspbian-wheezy.img: 3,1 GiB, 3276800000 bytes, 6400000 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x0009bf4f Device Boot Start End Sectors Size Id Type 2015-02-16-raspbian-wheezy.img1 8192 122879 114688 56M c W95 FAT32 (LBA) 2015-02-16-raspbian-wheezy.img2 122880 6399999 6277120 3G 83 Linux
As you can see, the second partition starts at sector 122880. But
dd, the tool we'll use to dump the data, uses bytes for its offset option. Luckily
fdisk lists the sector size in its output:
Sector size (logical/physical): 512 bytes / 512 bytes
Simply multiply the starting sector of the second partition by 512 and you know the offset: 62914560 bytes!
To verify your calculation, use
losetup to create a loopback device from the second partition inside the .img file:
$ losetup --offset=62914560 /dev/loop0 /tmp/2015-02-16-raspbian-wheezy.img
Now you can use
dd to write the contents from the loopback device onto your USB-stick:
$ dd if=/dev/loop0 of=/dev/sdc1 bs=4M
Resize the USB disk
Mount the USB partition that holds the root filesystem for Raspbian we just dumped:
$ mount /dev/sdc1 /mnt $ df -m /mnt Filesystem 1M-blocks Used Available Use% Mounted on /dev/sdc1 2953 2421 364 87% /mnt
As you can see, the filesystem is only 3 GB in size, but the partition I created is 15 GB! That's because we used
dd, to dump on a block level rather than a file level. To correct this, we just have to use
resize2fs on the partition:
# umount /mnt # fsck -fv /dev/sdc1 fsck from util-linux 2.25.2 e2fsck 1.42.12 (29-Aug-2014) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information ... # resize2fs /dev/sdc1 resize2fs 1.42.12 (29-Aug-2014) Resizing the filesystem on /dev/sdc1 to 3591424 (4k) blocks. The filesystem on /dev/sdc1 is now 3591424 (4k) blocks long. # fsck -fv /dev/sdc1 fsck from util-linux 2.25.2 e2fsck 1.42.12 (29-Aug-2014) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information ... root@thinker:~# mount /dev/sdc1 /mnt root@thinker:~# df -m /mnt Filesystem 1M-blocks Used Available Use% Mounted on /dev/sdc1 13745 2423 10605 19% /mnt
There you go: the entire 15 GB can be used by Raspbian now!
Update /etc/fstab on the USB-stick
While the USB-stick is still mounted, open
/mnt/etc/fstab and update the line for the
/ file system from
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
/dev/sda1 / ext4 defaults,noatime 0 1
Prepare the SD-card
Create a single FAT32 partition on your SD-card. It doesn't have to be big, as we saw in the
fdisk -l 2015-02-16-raspbian-wheezy.img output, the /boot partition is only 56 MB. Just make sure the partition is FAT32 (id C in
fdisk) and that the boot flag is enabled.
Copy /boot from .img to SD-card
/boot partition from the .img file using the offset trick we saw before:
# mount -o loop,offset=$(expr 8192 \* 512) /tmp/2015-02-16-raspbian-wheezy.img /mnt/raspBoot
Mount your SD-card and copy the files required for
/boot to the root of your SD-card. This time we use
cp instead of
dd because it's just a small amount of files:
# mount mount /dev/mmcblk0p1 /mnt/sdCard # cp -av /mnt/raspBoot/* /mnt/sdCard
Update boot parameters
Before unmounting the SD-card, open the file
cmdline.txt and update the
root= part so it looks like
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 rootwait text
umount both the USB-stick and the SD-card and plug them in your rPi2. Power on the rPi2 and it will boot from SD-card but the root file system will be on the USB-stick!
tristan@rpi2 ~ $ df -m Filesystem 1M-blocks Used Available Use% Mounted on rootfs 13745 2424 10604 19% / /dev/root 13745 2424 10604 19% / devtmpfs 460 0 460 0% /dev tmpfs 93 1 93 1% /run tmpfs 5 0 5 0% /run/lock tmpfs 391 0 391 0% /run/shm /dev/mmcblk0p1 253 15 238 6% /boot