Booting from an external disk

This recipe explains how to copy the root filesystem from the SD card to an external disk and boot from it. This is an advanced recipe. You will still require an SD card as the kernel with the necessary filesystem drivers located at the first partition of the SD card.

You may want to move your root filesystem from the SD card to an external drive for performance and/or space reasons. Also when having a lot of I/O operations on the SD card, the SD card may become unstable. A filesystem on a SATA attached disk is more stable in general.

Getting ready

The following ingredients are needed when using an external disk as the root filesystem:

  • A working Linux system on the Banana Pi
  • A SATA HDD or SSD drive
  • A working connection to your Banana Pi and a suitable power supply
  • Access to the Banana Pi's shell

How to do it…

This recipe is split into two subtopics, the preparing (that is formatting) of the external disk and the copying and configuring of the root filesystem.

Formatting the drive

We need to prepare the HDD or SSD to have at least one formatted ext4 partition.

Note

In the following steps, we are completely erasing all the existing partitions on the hard drive to get one fresh ext4 partition. If you do not use a brand new drive, make a backup of its contents, as the drive will be wiped!

Tip

If you have enough free space, you can also create a new ext4 partition without erasing the previous ones. In this case, you can use a tool like gparted for reducing a partition and creating another one.

  1. Open a shell.
  2. If your external drive is already mounted, unmount all partitions:
    $ sudo umount /dev/sda*
    
  3. Execute the fdisk program with the SATA disk as parameter:
    $ sudo fdisk /dev/sda
    
  4. When entering p, you get a list of all the currently available partitions.
  5. We are going to delete all partitions.
  6. Enter d followed by 1 to delete the first partition.
  7. Use the d operation again, until all partitions are deleted.
  8. Enter n followed by 4x. Press Enter to create a new partition with the full size of the drive.
  9. Enter p again to check whether the partition table is correct.
  10. Check again to see whether everything is correct. The next step is the point of no return.
  11. Enter w to write the changes to the drive. Otherwise enter q to quit without changes.

    The following screenshot shows the previous steps done in fdisk:

    Formatting the drive
  12. Create the ext4 filesystem on the newly created partition:
    $ sudo mkfs.ext4 /dev/sda1
    
  13. After a few seconds to minutes, the new ext4 partition is created.

You have created a fresh ext4 partition successfully.

Copying the root filesystem and editing uEnv.txt

The second step is to copy the whole root filesystem (rootfs) to the external disk and edit the uEnv.txt file, which contains essential information for the kernel at boot time:

  1. Create two mount directories (if not existent) to mount the first partition of the SD card and the SATA drive:
    $ sudo mkdir /mnt/boot
    $ sudo mkdir /mnt/sata_drive
    
  2. Mount the partitions of your SD card:
    $ sudo mount /dev/mmcblk0p1 /mnt/boot
    
  3. Mount the new ext4 partition of your SATA drive:
    $ sudo mount /dev/sda1 /mnt/sata_drive
    
  4. Synchronize the rootfs to the SATA drive:
    $ sudo rsync -arxP / /mnt/sata_drive/
    
  5. You will see that all the files are synchronized from the root to the SATA drive. This will take some time.
  6. As soon as the progress is done, edit the uEnv.txt to change the used root:
    $ sudo nano /mnt/boot/uEnv.txt
    
  7. In the last line, you will find a configuration option root=/dev/mmcblk0p2. You might need to scroll to the right-hand side by pressing the right arrow key.
  8. Edit this option to root=/dev/sda1:
    Copying the root filesystem and editing uEnv.txt
  9. Exit and save nano by pressing Ctrl + X, followed by Y and Enter.
  10. To verify that everything is written to the SD card, execute sync:
    $ sync
    
  11. Reboot your Banana Pi from the external drive:
    $ sudo shutdown -r now
    

You successfully booted your Banana Pi from the external disk.

How it works…

The Linux kernel is located on the first partition of the SD card (filename uImage). When powering the Banana Pi, the boot loader (called UBoot) tries to load the kernel from the SD card. The kernel itself requires some parameters to boot successfully. These parameters and options are configured in the uEnv.txt file.

When writing the SD card to prepare a Linux distribution for the Banana Pi, the image contains two partitions. The first partition includes the kernel, the uEnv.txt and a script.bin files. The second partition is the actual root filesystem. The rootfs contains all the actual programs, configurations, home directories, and so on of your Linux system.

We are synchronizing the rootfs from the SD card to a partition on the external drive using the synchronization tool rsync. The tool rsync is used to synchronize files over a network or on two directories, as in this case. The parameters -a mean archive (preserving permissions, symbolic links, and so on), -r recursive (recursive into directories), and -x is to prevent crossing filesystem boundaries. The parameter -P shows the progress of each synchronization.

Then we change the desired root from the rootfs of the SD card (that is the second partition) to the partition of the external drive in the uEnv.txt.

As the boot loader requires the kernel and further information from the first partition of an SD card, the SD card cannot be omitted when moving the rootfs to an external drive.

To use the SD card again as root, you simply mount the first SD card again and change the root option back to root=/dev/mmcblk0p2.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.62.42