Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notes for OpenWRT on NanoPi R1

OpenWRT on NanoPi R1

This guide assumes you're using linux or some kind of OS with a shell, you can use Windows with some modifications.

Links

Install Latest OpenWRT

Download the latest NanoPi R1, extract the 7z file.

Image will look something like nanopi-r1_eflasher_friendlywrt_4.14_armhf_20190826.img to an sdcard.

sudo dd if=./nanopi-r1_eflasher_friendlywrt_4.14_armhf_20190826.img of=/dev/your_sd_card bs=4096

Boot From Serial

Insert the SD card, plugin the unit to MicroUSB and the USB to serial.

Use the board headers, not the UART. I could never get the UART to do anything.

USB to Serial DIN to Serial

Plug everything in and connect to your USB tty.

lsusb

Should show something like:

Bus 006 Device 008: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter

Then you can do:

sudo tio /dev/ttyUSB0

You'll want to hit [enter] a couple of times to show the shell.

Follow the rest of the steps on the wiki.

Setup

Plugin your network cable and from the tty do:

cd ~/
./setup.sh

This will do a basic DHCP connection for your ethernet.

Check your ip addresses.

ifconfig

LuCI

From here you can go to your unit's ip address in your browser to open LuCI.

Setting Up an SDCARD for /root and swap

opkg update
opkg install fdisk kmod-fs-ext4 e2fsprogs swap-utils block-mount

Finding the SDCARD

Then insert your sdcard and find it.

fdisk -l

There will be several entries, you are looking for something like this. I'm using an 8GB SD card, it recognizes it as 7.2 GiB.

Disk /dev/mmcblk1: 7.2 GiB, 7746879488 bytes, 15130624 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: gpt
Disk identifier: 2FB1A165-A292-432E-8B58-F2E2C51BB656

Take note of the /dev/mmcblk1 or whatever yours shows. If you use the wrong one you can screw up your device.

Unmounting the SDCARD

If your SDCARD is currently mounted, you will need to unmount it to modify the partition table.

umount /dev/mmcblk1

If you are not mounted it'll show this error.

umount: can't unmount /dev/mmcblk1: Invalid argument

Showing Free Memory and Swap

free

Should display something like this, note the swap currently shows 0 for all entries.

             total       used       free     shared    buffers     cached
Mem:       1025092     115444     909648       1728       2444      57876
-/+ buffers/cache:      55124     969968
Swap:            0          0          0

Create Two Partitions

You will need to create two partitions on your SDCARD.

If your SDCARD already has a partition, it will show an error, which you can ignore.

fdisk /dev/mmcblk1

This section has several inputs which you'll need to make, I noted them in the code sections with ⟵⟵⟵.

Once in the fdisk program it will prompt you with this. I recommend hitting m to see the commands available.

Command (m for help):

Change type to gpt.

Command (m for help): g ⟵⟵⟵
Created a new GPT disklabel (GUID: 012345678-9ABCD-EF01-2345-56789ABCDEF0).

You'll probably want to start with F to determine how much free space you have. I'm using an 8GB SDCARD, it shows:

Command (m for help): F ⟵⟵⟵
    Unpartitioned space /dev/mmcblk0: 7.2 GiB, 7745830912 bytes, 15128576 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

Start      End  Sectors  Size
 2048 15130623 15128576  7.2G

Next up, create a 2GB swap partition with the n command. You will need to make several entries.

Command (m for help): n ⟵⟵⟵
Partition number (1-128, default 1): 1 ⟵⟵⟵
First sector (34-15130590, default 2048): 2048 ⟵⟵⟵
Last sector, +sectors or +size{K,M,G,T,P} (2048-15130590, default 15130590): +2G ⟵⟵⟵

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

Now create the partition you'll use for /root, again with the n command, unless you are doing something special, you may just hit enter fore each of the prompts, I marked them anyway.

Command (m for help): n ⟵⟵⟵
Partition number (2-128, default 2): 2 ⟵⟵⟵
First sector (4196352-15130590, default 4196352): 4196352 ⟵⟵⟵
Last sector, +sectors or +size{K,M,G,T,P} (4196352-15130590, default 15130590): 15130590 ⟵⟵⟵

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

Set the swap partition type to swap with the t command. Again you will need to make several entries.

Command (m for help): t ⟵⟵⟵
Partition number (1,2, default 2): 1 ⟵⟵⟵
Partition type (type L to list all types): 19 ⟵⟵⟵

Changed type of partition 'Linux' to 'Linux swap'.

You may now print the partition table with the p command to verify what you have done. It should show something like this.

Command (m for help): p ⟵⟵⟵
Disk /dev/mmcblk1: 7.2 GiB, 7746879488 bytes, 15130624 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: gpt
Disk identifier: 2FB1A165-A292-432E-8B58-F2E2C51BB656

Device           Start      End  Sectors  Size Type
/dev/mmcblk1p1    2048  4196351  4194304    2G Linux swap
/dev/mmcblk1p2 4196352 15130590 10934239  5.2G Linux filesystem

If all looks good, write the partition with the w command.

Command (m for help): w ⟵⟵⟵
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

This will drop you back to the shell, run fdisk -l again and you should see your partitions have changed and shows something like this.

Disk /dev/mmcblk1: 7.2 GiB, 7746879488 bytes, 15130624 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: gpt
Disk identifier: 2FB1A165-A292-432E-8B58-F2E2C51BB656

Device           Start      End  Sectors  Size Type
/dev/mmcblk1p1    2048  4196351  4194304    2G Linux swap
/dev/mmcblk1p2 4196352 15130590 10934239  5.2G Linux filesystem

Take note of your Linux swap and Linux partition Devices.

Format the SDCARD

Format your Linux partition as ext4.

mkfs.ext4 /dev/mmcblk1p2

This will take a minute and output something like the following.

mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks: done
Creating filesystem with 1366779 4k blocks and 342048 inodes
Filesystem UUID: f22e65c8-55a7-45f5-80bb-986e7c9a36e2
Superblock backups stored on blocks:
  32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

Format your Linux swap partition and enable the swap.

mkswap /dev/mmcblk1p1
swapon /dev/mmcblk1p1

Again use the free command.

free -k

It should now show your updated swap space.

             total       used       free     shared    buffers     cached
Mem:       1025092     123108     901984       1728       2448      58268
-/+ buffers/cache:      62392     962700
Swap:      2097148          0    2097148

You may also use the swapon command to show your swap space.

swapon -s

This should display something like this.

Filename        Type      Size    Used  Priority
/dev/mmcblk1p1  partition 2097148 0     -2

Mount the /root Partition

If you have anything in your /root currently, move it to /tmp.

mkdir /tmp/root_temp
mv /root/* /tmp/root_temp
ls -lah /root

Mount the drive and try writing to it.

mount /dev/mmcblk1p2 /root
cd /root
echo "test" > test.txt
cat test.txt

This should display test. Now you can move your stuff back from /tmp to /root.

mv /tmp/root_temp/* /root
rmdir /tmp/root_temp/

Mount the SDCARD at Boot

Enable the fstab.

/etc/init.d/fstab enable

Detect the mounts.

block detect > /etc/config/fstab
cat /etc/config/fstab

This should display something similar to this, take note of your uuid, they are always different.

config 'global'
  option  anon_swap '0'
  option  anon_mount  '0'
  option  auto_swap '1'
  option  auto_mount  '1'
  option  delay_root  '5'
  option  check_fs  '0'

config 'mount'
  option  target  '/mnt/mmcblk0p1'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '0'

config 'mount'
  option  target  '/mnt/mmcblk0p2'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '0'

config 'mount'
  option  target  '/mnt/mmcblk0p3'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '0'

config 'mount'
  option  target  '/mnt/mmcblk1'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '0'

config 'swap'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '0'

config 'mount'
  option  target  '/mnt/mmcblk1p2'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '0'

You'll want to edit the fstab.

vi /etc/config/fstab

Make it look like this.

config 'global'
  option  anon_swap '0'
  option  anon_mount  '0'
  option  auto_swap '1'
  option  auto_mount  '1'
  option  delay_root  '5'
  option  check_fs  '0'

config 'mount'
  option  target  '/mnt/mmcblk0p1'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '1'

config 'mount'
  option  target  '/mnt/mmcblk0p2'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '1'

config 'mount'
  option  target  '/mnt/mmcblk0p3'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '1'

config 'mount'
  option  target  '/mnt/mmcblk1'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '1'

config 'swap'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '1'

config 'mount'
  option  target  '/root'
  option  uuid  'YOUR_UUID_HERE"
  option  enabled '1'

Test it, it should throw no error messages.

mount -a

Reboot and exit.

reboot && exit

SSH back into the unit and make sure the mount worked.

df -h

Should display something like this.

Filesystem                Size      Used Available Use% Mounted on
udev                    512.0K         0    512.0K   0% /dev
tmpfs                   100.1M    124.0K    100.0M   0% /run
overlay                   6.7G     22.5M      6.4G   0% /
tmpfs                   500.5M    636.0K    499.9M   0% /tmp
tmpfs                   512.0K         0    512.0K   0% /dev
/dev/mmcblk0p3            6.7G     22.5M      6.4G   0% /mnt/mmcblk0p3
/dev/mmcblk0p1           39.9M     11.3M     28.6M  28% /mnt/mmcblk0p1
/dev/mmcblk1p2            5.1G     21.5M      4.8G   0% /root
/dev/mmcblk0p1           39.9M     11.3M     28.6M  28% /boot

Then check the swap.

swapon -s

Should still display something like this.

Filename        Type      Size    Used  Priority
/dev/mmcblk1p1  partition 2097148 0     -2

Software

Update opkg

opkg update

Encryption

opkg install libustream-openssl ca-bundle ca-certificates

Install BASH

# INSTALL BASH
opkg update
opkg install bash

# SET SHELL TO BASH
sed -i 's/\/ash/\/bash/' /etc/passwd

NANO & VIM

opkg install nano vim-full

Utilities

opkg install usbutils pciutils coreutils

collectd-mod- for LuCI Graphing

You can view these in LuCI Statistics ⟶ Graphs.

opkg install collectd collectd-mod-rrdtool rrdtool1

For other graphs.

opkg list |grep collectd-mod

CPU

opkg install collectd-mod-cpu

Disk Related

opkg install collectd-mod-df collectd-mod-disk

neofetch

mkdir ~/bin
wget https://raw.githubusercontent.com/dylanaraps/neofetch/master/neofetch -O ~/bin/neofetch
chmod +x ~/bin/neofetch

Create Your Profile

Create your local bin.

mkdir ~/bin

Touch your profile.

touch ~/.profile

Edit it.

vi ~/.profile

Add some stuff to it.

~/bin/neofetch
alias l='ls -lah'
export PATH=~/bin:$PATH

Source it, this applies the changes you made.

source ~/.profile

Generate SSH Keys

mkdir ~/.ssh
dropbearkey -t rsa -f ~/.ssh/id_rsa
dropbearkey -y -f ~/.ssh/id_rsa | sed -n 2p > ~/.ssh/id_rsa.pub

All ssh keys go in:

/etc/dropbear/authorized_keys

Upgrade Packages

Upgrading some packages such as netifd may cause the unit to have problems.

opkg update
opkg list-upgradable
opkg upgrade opkg

GIT

Install packages.

opkg install git git-http

Add Your SSH Key

If you have a git account, put your ssh key into your account. Go to GitHub's SSH keys page.

cat ~/.ssh/id_rsa.pub

Copy and paste the out put into GitHub, do not make this public.

Can't Log In

You can clone stuff with git via https, but I didn't want to do that for reasons.

With the NanoPi by default you'll get an unfriendly message if you try to ssh into github.

ssh git@github.com

ssh: Connection to git@github.com:22 exited: No auth methods could be used.

What does this mean? From what I've read it means it isn't using your generated ssh key to login, because OpenWRT uses dropbear's ssh instead of OpenSSH.

Fixing Logging In

This section is largely derived from this answer on stackoverflow.

Normally you should be able to ssh into github as shown above, it won't give you a shell, but it will give you a friendly message.

You can ssh specifying your user's specific id_rsa key and get the friendly authentication message.

ssh -i ~/.ssh/id_rsa git@github.com

Hi pjobson! You've successfully authenticated, but GitHub does not provide shell access.

Get the ssh-git script in this repo.

wget https://gist.githubusercontent.com/pjobson/95b67bc4f17c2f60278ec40bf15ca99d/raw/91f4dcaee3afe5c321899b6d656a0c8ff9fcc45b/ssh-git.sh -O ~/bin/ssh-git
chmod +x ~/bin/ssh-git

Edit your .profile.

vi ~/.profile

Add these exports, replace your info where relevant.

export GIT_SSH=~/bin/ssh-git
export GIT_AUTHOR_NAME="USER NAME"
export GIT_AUTHOR_EMAIL="user@email.address"
export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL

Now try to clone this gist.

git clone git@gist.github.com:95b67bc4f17c2f60278ec40bf15ca99d.git
#!/bin/bash
# This is derived from: https://stackoverflow.com/questions/19608313/cloning-from-github-on-android-using-terminal-ide/22062806#22062806
exec ssh -i ~/.ssh/id_rsa "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.