Use a USB drive for rootfs

In this section, we will go through the steps of the building process…

STEP1 – Install Raspbian on your Raspberry pi and update (this is documented heavily on the web and I will not repeat this step here)

STEP2 – Change your root file system to a USB connected drive
First, we need to identify the partition where your root file system resides by issuing the command shown below in a terminal on your Raspberry Pi:

sudo fdisk -l

We should get a similar message like the one below:

Disk /dev/ram0: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram1: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
…………………………………. after many lines, we look at the bottom of the message highlighted in orange below
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x5f9fde37

Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 131071 122880 60M c W95 FAT32 (LBA)
/dev/mmcblk0p2 131072 30318591 30187520 14.4G 83 Linux

pi@nms01 ~ $ df -h

Filesystem Size Used Avail Use% Mounted on
/dev/root 15G 5.6G 7.9G 42% /
devtmpfs 459M 0 459M 0% /dev
tmpfs 463M 0 463M 0% /dev/shm
tmpfs 463M 6.2M 457M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 463M 0 463M 0% /sys/fs/cgroup
/dev/mmcblk0p1 60M 20M 41M 34% /boot
tmpfs 93M 0 93M 0% /run/user/1000

Please note that /dev/mmcblk0p2 is (almost) the same size as /dev/root. That’s your root partition.

Second, you need to copy your current root partition from the SD card to the USB drive. Execute this command with only your single USB drive inserted, no other drives (other than the SD card):

sudo dd if=/dev/mmcblk0p2 of=/dev/sda bs=4M

Depending on how large your SD card is, this could take time.

Next, we want to check the USB drive for errors with this command:

sudo e2fsck /dev/sda

If that turns out okay, we can resize your new root partition to fill your USB drive:

sudo resize2fs /dev/sda

Now you should mount your USB drive so that we can edit your future fstab file:

sudo mount /dev/sda /mnt

To edit the fstab, use nano with this command:

sudo nano /mnt/etc/fstab

It will look something like this:

proc /proc proc defaults 0 0

/dev/mmcblk0p1 /boot vfat defaults 0 2

/dev/mmcblk0p2 / ext4 defaults,noatime 0 1

# a swapfile is not a swap partition, so no using swapon|off…

You need to change it to look like this:

proc /proc proc defaults 0 0

/dev/mmcblk0p1 /boot vfat defaults 0 2

/dev/sda / ext4 defaults,noatime 0 1

# a swapfile is not a swap partition, so no using swapon|off…

So that /dev/sda1 is mounted as root (/).

Next, unmount the USB drive:

sudo umount /dev/sda

And edit the cmdline.txt file in your boot partition with this command:

sudo nano /boot/cmdline.txt

You will want it to look like this:

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

Notice that root=/dev/sda.

Now you can reboot (sudo reboot) and you should be good to go with your new root partition on the USB drive. You can check it with the df -h command we used earlier and see the new size of rootfs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s