Boot M2 Bananian 16.04 from HDD/SDD


(Shane van Jaarsveldt) #1

This is content I have found from a few sources - I can’t remember who were the authors - so all credit to them. The instructions below have worked for me and I have rebuilt this 5 times now and each time it has worked as expected.

BOOT from HDD/SDD

PREPARE THE PARTITIONS

So, first thing first, power down your Banana Pi and connect the cables. If you make a custom power cable like me, be careful of the polarity. The pin next to the micro USB that powers the Banana is the +5V while the other one on the edge is the ground. Power up the Banana again, SSH into it and…

fdisk -l

That should give you a list of the disks connected to your Banana. You will most probably see a disk /dev/sda, and that is what you want. From here on you will need to delete all partitions from your hard drive, so don’t continue unless you backup any data you might have in there. Once you are ready to proceed…

fdisk /dev/sda

There are several options there. You will need to delete all partitions and then make a new one. So the options you should select are:

d

If there are multiple partitions, first delete partition 1(type 1 and enter). Repeat the process until all partitions are gone. Then to write the partition table changes:

w

Let’s enter the partition manager again:

fdisk /dev/sda

Create a new partition:

n

and make it a primary one:

p

Enter 1 as the partition number and select the default values of start and end sector(simply press enter), and at the end write the changes to the partition table:

w

Now to format our new partition to ext4 and mount it as /dev/sda1:

sudo mke2fs -t ext4 -L rootfs /dev/sda1

COPY THE OS TO THE SATA DRIVE

Now to copy everything to our hard drive. First, we need a temporary mount point where we will mount our SATA drive:

mkdir /tmp/sata

mount /dev/sda1 /tmp/sata

and let’s copy everything. That should take a few minutes, depending on your SD card, your SATA drive and how much data there is to copy:

rsync -arx --progress / /tmp/sata

PREPARE THE BOOT PARTITION

Once the copy is over, let’s prepare the boot partition so that the Banana will use the SATA drive.

First of all, mount the boot partition

mkdir /tmp/boot mount /dev/mmcblk0p1 /tmp/boot

Now, inside /tmp/boot you will notice a file named boot.cmd which is the file we need to change. In order to have easy access to our SD card in case something goes wrong, let’s first of all make a copy of this file and then change its contents.

cp /tmp/boot/boot.cmd /tmp/boot/boot.cmd.sd nano /tmp/boot/boot.cmd

See the part of the file that reads root=/dev/mmcblk0p2 and change it to root=/dev/sda1 and then save the file and exit the editor.

And let’s make a new copy with the settings for the SATA drive so we can easily swap between these files anytime we want.

cp /tmp/boot.cmd /tmp/boot/boot.cmd.sata

We need to install some tools to manage the boot script (.scr) creation

$ apt-get install u-boot-tools

…and now make copies of the various boot scripts so you can switch between them easily if needed.

  • $ cd /tmp/boot
  • cp boot.scr boot.scr.sd
  • $ rm boot.scr
  • $ mkimage -C none -A arm -T script -d boot.cmd boot.scr
  • cp boot.scr boot.scr.sata

Synchronize the drives using

sync

and reboot. Voila, when the Banana Pi boots again you will see that you are using your SATA drive instead of the SD card. To check it, you can try to run this command:

root@bpi ~ # df -h

Filesystem Size Used Avail Use% Mounted on rootfs 230G 1.6G 217G 1% / /dev/root 230G 1.6G 217G 1% / devtmpfs 486M 0 486M 0% /dev tmpfs 98M 224K 97M 1% /run tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 195M 0 195M 0% /run/shm

Notice how my rootfs is 230GB.

Now, in case anything goes wrong, you can go back to the OS on the SD card by simply placing your SD in a card reader and overwriting boot.scr with a copy of the boot.cmd.sd. And you can do the same with the boot.scr.sata in case you want to use your SATA drive again.

Before I close this post, I’ll add some information on how to use TRIM if you are to use an SSD.

USE TRIM WITH YOUR SSD AS A CRON JOB EVERY DAY

First of all, check if you have fstrim in your system(which you should).

fstrim -v /

This should return an output of bytes were trimmed. Then let’s add a script to run daily as a cron job:

nano /etc/cron.daily/trim

and inside put paste the following script:

#!/bin/sh LOG=/var/log/trim.log echo “*** $(date -R) ***” >> $LOG fstrim -v / >> $LOG

and make that script executable:

chmod +x /etc/cron.daily/trim

And that’s it! You should see a log of the script running anytime using this command:

cat /var/log/trim.log


#2

Thanks for the instructions. Is there anything here that is particular to BPI-M2 that would/should not work with BPI-M3?


(Andrew Woo) #3

I followed the instructions above, but BPi did not boot from SATA and kept booting from the SD card.

I have changed the /boot/boot.cmd file and compiled the boot.scr again. After reboot, dmesg always show that “Kernel command line: console=… … root=/dev/mmcblk0p2 …”

Any hints for further troubleshoot ?

Thanks.