Until now I have used Raspberry Pi with micro SD card and while it works for some times I have had lot of corruptions. Not shutting Pi properly down plus unstable power wreak havoc on the SD card. And as you may have read from my previous tutorials I like to run servers on my Raspberry Pi so yeah SQL + logging = high read write are no way to treat small SD cards.

So for loads like that there should be more robust solution.

And for that here is small write up how to use USB thumb drive or USB hard drive as your Raspberry Pi-s root drive. You still need SD card for boot but as you learn from this tutorial it is much easier to replace the static boot drive then whole system, especially  if you haven’t backed up your system 10 seconds prior to failure.

We will be using 8GB MicroSD and 32GB Sandisk thumbdrive.
And Raspbian Jessie Lite 2017-07-05

Prep) Format your SD card and thumb drive. Connect the ethernet cable to raspberry.
Also you should know how to check your RPi-s IP from your router.
Note) It is easyer if you have also monitor connected to see the boot process, because it happens before the
ssh is possible and without monitor you have no way knowing if and what went wrong.

1) Write Raspbian to the SD card.

2) Open boot partition of SD card and add empty file called “ssh”

3) Insert your cleanly formatted USB drive to Raspberry USB port.

4) Insert the SD card to RP and boot.

5) SSH into PI.

If you run:

df -h

you should see something similar to this:


6) Run:

sudo fdisk -l

At the end should be our USB thumb drive ( /dev/sda1):

Now we going to check if the USB drive is working and is compatible.

7) Run:

sudo mount /dev/sda1 /mnt

If usb is compatible it will mount without problems and running:

df -l

We see that our tuhmb drive is mounted correctly.


Lets unmount our drive:

sudo umount /dev/sda1

8) Suhtt down RP

9) Take USB thumb drive and write Raspbian on it.

10) Add “ssh” file to boot partition.

11) and put it back to RP.

12) Take the SD card and edit the cmdline.txt file in boot partition:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=906d65dc-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

change the part with “root=PARTUUID….” to “root=/dev/sda2” so it looks something like this:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/sda2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Save it.

13) SD card back to RP and boot.

14) SSH to RP

15) Now if you run:

df -h

and result will be similar to:

And now you are basically done.

BUT

As you can see the root filesystem size is only 1.6G so we need to expand it.
Because Raspbian cant expand USB root file system the default way there are couple of steps we have to do.

16) First run:

sudo fdisk /dev/sda

17) After that run to display drive info and partitions. IMPORTANT! Remember the start sector of partition of sda2.

Command (m for help): p
Disk /dev/sda: 29 GiB, 31104958464 bytes, 60751872 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: 0xa8790229

Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 93596 85405 41.7M c W95 FAT32 (LBA)
/dev/sda2 94208 3370369 3276162 1.6G 83 Linux

18) Now lets start deleting partitions. Run d.

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

19) Lets check:

Command (m for help): p
Disk /dev/sda: 29 GiB, 31104958464 bytes, 60751872 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: 0xa8790229

Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 93596 85405 41.7M c W95 FAT32 (LBA)

20) Lets add new partition (IMPORTANT! First sector will be the number you saved previously, the deleted partition start):

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-60751871, default 2048): 94208
Last sector, +sectors or +size{K,M,G,T,P} (94208-60751871, default 60751871):

Created a new partition 2 of type 'Linux' and of size 28.9 GiB.

21) Check:

Command (m for help): p
Disk /dev/sda: 29 GiB, 31104958464 bytes, 60751872 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: 0xa8790229

Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 93596 85405 41.7M c W95 FAT32 (LBA)
/dev/sda2 94208 60751871 60657664 28.9G 83 Linux

22) Finally we going to write changes to disk:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

23) Reboot

24) SSH in and run:

pi@raspberrypi:~ $ sudo resize2fs /dev/sda2
resize2fs 1.43.3 (04-Sep-2016)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 4
The filesystem on /dev/sda2 is now 7582208 (4k) blocks long.

This can take some time depending on the file system size. After its done…

25) Reboot

26) Run for check:

pi@raspberrypi:~ $ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 29G 931M 27G 4% /
devtmpfs 458M 0 458M 0% /dev
tmpfs 462M 0 462M 0% /dev/shm
tmpfs 462M 6.2M 456M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 462M 0 462M 0% /sys/fs/cgroup
/dev/sda1 42M 21M 21M 51% /boot

AND DONE.

Now why this works ?

Because after deleting the partition and creating the new partition start exactly from where the previous root partition started before we write changes to the disk, we won’t  erase the existing data from the root partition.

On my current version of Raspbian this approach works and it works also for other linux systems but there are distros (like arch linux for arm) that have changed the partition structure so for systems like that you have to tweak some things.

BONUS:

Now you can make disk image of the SD cards boot partition, format the card and only write the boot partition on the card. Or take the smaller card. The boot partition is around 50MB so 1GB will suffice and boot from that. And backup boot image won’t  take any room and you can quickly write it to disk if occasion should arise.