raspberry_piNot long ago I set up a Bitcoin node on a Debian Wheezy VPS to teach myself about Bitcoin and the blockchain in general. It was a great learning experience but the memory requirements of the Bitcoin software kept causing my VPS instance to run out of memory so the kernel would constantly be killing off the bitcoind process.

A growing blockchain also meant the node was getting close to the limit of its available disk space as well. Simply increasing the amount of disk space available to the VPS instance with the plans offered by the hosting provider was going to be prohibitive for the purpose so I decided to assess other solutions.

Luckily I had a spare Raspberry Pi 2 that I had yet to do anything with at home. So I made the decision to try and install a Bitcoin node on that instead to save some money.

The main expense with using the Raspberry Pi was going to be finding enough storage for the block chain. I didn’t have an SD card that was big enough at home to¬†accommodate the operating system image and the block chain.

So I opted to buy a cheap external USB flash drive to keep it separate from the root filesystem. After formatting with ext4 the flash drive has 57Gb of available space, at the time of writing the blockchain takes up 40Gb of space so that leaves a further 17Gb for future growth.

Hardware Used

RaspberryPi 2
Clear plastic case
Kingston 64GB USB Flash Drive
16GB Micro SD card with the default Raspbian distro image
Standard power supply with micro USB connector

Increasing the Swap

As I mentioned before I was originally running a Bitcoin node on a SolusVM based VPS with 1GB of ram and no swap. The Bitcoin process after running for a while would consume all the available memory and kept getting killed off by the Kernel.

The Raspberry Pi 2 also has 1GB of RAM so I wanted to give it a fairly big swap file to hopefully act as a buffer to avoid exhausting the memory again. Raspbian uses dphys-swapfile to manage a swapfile on the Pi so increasing the size of the allocated space is very simple:

nano /etc/dphys-swapfile

By default Raspbian has a 100mb allocated to swap so I changed this to 1024 for 1gb of swap space.

CONF_SWAPSIZE=1024

After saving the changes the dphys-swapfile service needs to be restarted for the changes to take effect.

/etc/init.d/dphys-swapfile stop
/etc/init.d/dphys-swapfile start

Preparing the USB Stick

fdisk -l

This should list your USB drive so you can find its device identifier. In my case it was /dev/sda, so with this information I then used fdisk to delete the current partition on the device and create a new one.

fdisk /dev/sda

After writing the new partition to the device, the mkfs command was used to format the newly created partition with an ext4 file system.

mkfs -t ext4 /dev/sda1

After formatting the USB drive I created a new mount point for the drive to be mounted on the root filesytem.

mkdir /media/external

Then mounted the drive to the newly created mount point:

mount /dev/sda1 external/

Cleaning The System

Raspbian is a great distro, but out of the box I feel alot of excess software comes pre installed for the purpose of running a Bitcoin node. Samat Jain has some good pointers on his blog  for removing some of the bloat. He has even been nice enough to put up a script on github to remove the uneeded packages automatically by simply running the following command:

wget https://gist.githubusercontent.com/samatjain/4dda24e14a5b73481e2a/raw/5d9bac8ec40b94833b4e9938121945be252fdee1/Slim-Raspbian.sh -O Slim-Raspbian.sh

Building The Bitcoin Software

First up the package list on the systems need to be updated then upgraded to the latest versions available.

apt-get update
apt-get upgrade

Some important packages ten need to be installed that are required by to build and run the Bitcoin software on the Raspberry Pi.

apt-get install dpkg-dev qt4-qmake libdb5.1++-dev libqt4-dev libqrencode-dev libminiupnpc-dev libboost-test1.49-dev zlib1g lib32z1 libc6-i386 debhelper devscripts bash-completion libboost-system-dev libssl-dev pkg-config libboost-filesystem-dev libboost-program-options-dev libboost-thread-dev libboost-test-dev dh-autoreconf libboost-chrono1.49.0 libboost-chrono-dev libboost-all-dev libprotobuf-dev protobuf-compiler libc6-dev libz1

git clone https://github.com/bitcoin/bitcoin.git v0.10.2
cd v0.10.2
./autogen.sh
./configure --with-incompatible-bdb

With the configure process finished its time to run make. Be forewarned this command is going to take a long time to complete so find yourself something to do in the interim. The Raspberry Pi 2 has a quad core processor but the build process will only use one core by default. So you may want to try speeding things up by appending the compiler flag ” -j 4″ after the make command to spawn 4 processes that can keep each of the processor cores busy.

make
make install

Create Bitcoind User

Up until now you have probably been logged into your machine as root either natively or using sudo to elevate your system privileges. For the sake of security its not a great ideato run the Bitcoin process as a user with elevated privileges. So I then added a dedicated user account called bitcoin for this purpose:

adduser bitcoin

After this I then logged in as the newly created bitcoin user to complete the rest of the setup process.

su bitcoin

Bitcoin Configuration

A new configuration file was then created for the Bitcoin service.

pico /home/bitcoin/.bitcoin/bitcoin.conf

In this file at a minimum add two lines ( see below for example ) defining a user and password for the JSON RPC web service that allows you to interact with the node.

rpcuser=bitcoinrpc
rpcpassword=D5vrfdZUeQpeF3GCSp5nUofdsJgs5fojgdpB81ZmwVeSmz2

For an example file config file showing some of the other possible parameters I recommend taking a look at the Bitcoin Wiki to see all of the options available.

Start the Bitcoind Process

At this point all the bit steps have been taken care of its simply a matter of starting the bitcoind process:

bitcoind –daemon

Looking in the same directory as the .bitcoin config file a file named debug.log will now exist. The tail command can be used to follow the log file ( i.e tail -f debug.log ) and check what the node is up to. After first starting the Bitcoin node the software will sync with the network by downloading the blockchain. Due to the size of the Blockchain this will take a long time depending on your network speed and other factors. In my case over an ADSL2 connection this step took around 4 days to complete.

Checking on Bitcoind

After getting the Bitcoin software installed and running I wanted to ensure it would restart if it occasionally crashed / or got killed off by the Kernel like on the VPS. So I wrote a small shell script to check the process is running and if its not to start it.

#!/bin/bash
x=`pgrep bitcoind `

if [ $x > 0 ];
then
echo "Bitcoind Running"
else
echo "Starting Bitcoind"
/usr/local/bin/bitcoind --deamon &
fi

The file was then made executable and a line added to the bitcoin users crontab for to run the script every 15 minutes.

*/15 * * * * /media/external/bitcoin/.bitcoin/service_check.sh >/dev/null 2>&1

Beer money?

Are you still awake after reading all that? Are you feeling rich? Beer money is always greatly appreciated!

Send BTC to:

1HYtEBszMQZwFNomhD78h7UWE9aeY7Kru1

Bitcoin Donations

Related Links:

Running a Bitcoin Node on a Debian Wheezy VPS

Further Reading:

Bitcoin Project

Bitcoin @ Github

Raspbian – Debian based distro optimised for the Raspberry Pi