Raspy: a Personal Server with Raspberry PI

Ritratto di feel

In the last years we have seen that Raspberry PI has got a great interest for the Open Source community and, indeed, it is endless the range of applications that RP (Raspberry PI) can be used due in particular for the size. After buying and using it for a while you realize that actually you need to do something more interesting than using it as a Desktop PC.

Moreover, I think it shouldn't be used as a Desktop PC because although RP has good performance considering the size (credit card size) it turns out that RP cannot execute heavy graphical processes.

The idea of create a Personal Server came into my mind several years before the origin of RP and the main purpose for creating a PS (Personal Server) is to serve only a really restricted number of users (usually only one) over the network and to be highly available whenever the users need it and, possibly, it should reduce the power consumption. As you can see Raspberry PI fits perfectly for this purpose.


Note that this tutorial show to build a personal server but you can use whatever laptop or old PC to do that. Nevertheless, one of the goals of the tutorial is to reduce the power consumption that can be reached easily by using Raspberry PI (The overall system will take less than 10W).

To sum up with the property a PS should have:
- Personal server - In the next section we will also see how to start other services such as NFS, MPD and so on...
- High availability - For sure, it is the most important and difficult goal to be achieved. We need to guarantee that the PS is always available and in order to get these results a smart script need to be built.
- Power management - Also in this case we will design a script to reduce the power consumption. In particular, the script should check the traffic in the network and make a decision whether or not the 3G modem should use a UMTS (fast but more expensive in terms of power) and GPRS (slower but reduce a lot the power consumption).

Note: For the sake of brevity the Raspberry PI will be named as RP, Personal Server as PS, High availability as HA and Power management as PM.



The code is available in GitHub: https://github.com/fsquillace/raspy

Hardware requirements


Here there is a list of devices you need in order to get a good PS. As I mentioned, the tutorial is not meant to be restricted for the hardware listed below and you can follow this with whatever computer with Archlinux and systemd installed.

Here it is the list of out flavors:
- Raspberry PI - (really? :) )
- A USB wireless adapter. I used the Alfa Network AWUS036NH because it is awesome! Furthermore, it includes a 5 dB antenna out-of-box! I bought another antenna in order to get 7dB. If you either have a USB wirelees adapter or want to buy another one I suggest to check out the following site:
http://wireless.kernel.org/en/users/Drivers.
In particular, you have to check if the driver of your device runs as access point mode, otherwise it's likely.
- A 3G UMTS modem - Somehow PS has to get internet! I don't have an ADSL connection but I have always used a 3G modem and, actually, one of the reason I made the a PS is to get a wireless network to access in internet with my phone too. So, if you have a ADSL modem you have to figure out how to connect your RP somehow but I suppose it's you will have fun for it too :).
- Powered USB Hub - I strongly suggest you to have a really good powered USB hub, mainly because RP is not able to provide enough current for all the device plugged in and the tension would drop down drammatically and, another reason is that you can supply your RP directly from the hubs itself! Anyway, I tested my PS also without the hub and everything worked properly. It's worth notice that not all the USB hubs are fully compatible with RP. The list of the verified peripherals are in: http://elinux.org/RPi_VerifiedPeripherals#Powered_USB_Hubs.
I bought the Plugable USB2-HUB-AG7. It provides enough current (3 A) in order to plug in also your hungrier devices!

Software requirements


- Archlinux - Didn't you install Archlinux in your computer? :)
- Archlinux - Didn't you install Archlinux in your RP? :)
- Archlinux - Do it!
- Systemd - It is the service manager used to control some services we need to start at boot time.
- hostapd - It is the package that allow you to convert the USB wireless Adapter in an access point
- dhcpd - It is the daemon for managing the assignment of the IP address.
- netcfg - It is an amazing command that allow easily to manage network profiles. We will heavily use it in order to achive the High Availability and Power management goal.

For those of you that don't know how to install packages in Archlinux the command is:
>> pacman -S package_name

Let the game begin!

Create the access point using hostapd


Fortunately creating an access point using hostapd is extremely easy. The wonderful steps are:

  1. In /etc/hostapd.conf:

interface=wlan0
driver=nl80211
ssid=RaspNetwork
channel=1
wpa=1
wpa_passphrase=mypassword


The options in the files are quite self-explanatories. Many wireless adapteres require as driver nl80211. There are many other settings to apply but I wanted to keep it as easy as possible.


  1. Start the service:
    >> systemctl start hostapd.service

  2. Enable the service for the boot:
    >> systemctl enable hostapd.service

Configure the 3G UMTS modem


I admit here that we have a lot of work to do! Please follow this link that explain how to properly configure your 3G modem:
https://wiki.archlinux.org/index.php/3G_and_GPRS_modems_with_pppd.

Basically, we have to add several files in /etc/ppp:


  • /etc/ppp/options-mobile - define all the properties in order to connect properly such as speed, device name and so on;
  • /etc/ppp/peers - is a folder that contains a reference for the chat script and credential information;
  • /etc/ppp/chatscripts - contains a chat script used to communicate with the 3G modem.


Many services! The more the merrier!

Iptables


The PS need to forward the packages from the ppp to wlan0 interface and viceversa. There are many ways to do this an easy way is to use the powerful iptables command. Iptables rules are the following:
#Enable NAT
>> iptables --flush
>> iptables --table nat --flush
>> iptables --delete-chain
>> iptables --table nat --delete-chain
>> iptables --table nat --append POSTROUTING --out-interface ppp0 -j MASQUERADE
>> iptables --append FORWARD --in-interface wlan0 -j ACCEPT


To store the rules to be executed by iptables when the RP is booted:
>> iptables-save > /etc/iptables/iptables.rules

Do not forget to stat the services:
>> systemctl start iptables.service
>> systemctl enable iptables.service


To enable the IP forwarding go to /etc/sysctl.conf and set the ip forwarding property to 1:
net.ipv4.ip_forward = 1

Netcfg


Netcfg is a powerful tool that enable to easily manage several network profiles. In our we have two profiles to manage. The ppp profile is for connecting PS in internet with the 3G modem. The profile file is the following:
/etc/network.d/ppp
CONNECTION='ppp'
DESCRIPTION='Connection with GPRS/3G device'
INTERFACE='ignore'
PEER='provider'
PPP_TIMEOUT=30
PRE_UP='/etc/ppp/wait-dialup-hardware 10'


This profile constists of a hook with the pppd daemon. When starting the ppp profile, netcfg try to connect to the modem through pppd using the data stored in the PEER option (the file is in /etc/ppp/peers/provider).

Since the wlan0 is the wireless interface to be set as access point, the only operation that need to be done is to set the IP address to 10.0.0.1:

/etc/network.d/wireless-open

CONNECTION='ethernet'
DESCRIPTION='A simple opened wireless connection'
INTERFACE='wlan0'
IP='static'
ADDR='10.0.0.1'
NETMASK='255.255.255.0'
SKIPNOCARRIER='yes'


In particular, the SKIPNOCARRIER option says that the adapter doesn't need to connect to a specific ESSID.

In order to connect the profiles at boot edit the NETWORKS option in netcfg file as follows: /etct/conf.d/netcfg

NETWORKS=(wireless-open @ppp)


As usual, let start the netcfg service:
>> systemctl start netcfg
>> systemctl enable netcfg


Dhcpd4


In order to configure properly the dhcpd daemon the file /etc/dhcpd.conf has to be edited as follow:
ddns-update-style none; ignore client-updates; authoritative; option local-wpad code 252 = text; subnet 10.0.0.0 netmask 255.255.255.0 { # --- default gateway option routers 10.0.0.1; # --- Netmask option subnet-mask 255.255.255.0; # --- Broadcast Address option broadcast-address 10.0.0.255; # --- Domain name servers, tells the clients which DNS servers to use option domain-name-servers 10.0.0.1, 8.8.8.8, 8.8.4.4; option time-offset 0; range 10.0.0.3 10.0.0.13; default-lease-time 1209600; max-lease-time 1814400; }

Leaving some details out, what we are saying with the dhcpd.conf file is that we have a subnet (namely the wireless network) with IP address 10.0.0.0 and netmask: 255.255.255.0, the default gateway will be 10.0.0.1 and the default range of IPs which dhcpd will choose for the host is 10.0.0.3-10.0.0.13.

Start and enable the process with:
>> systemctl start dhcpd4.service
>> systemctl enable dhcpd4.service


It is worth notice that dhcpd fails whether it doesn't find the router IP address in one of the interfaces. Therefore, if some services start after dhcpd the system
will not work properly.
Systemd helps for this purpose because it is possible to manage the dependencies between services. Just edit the /etc/systemd/system/multi-users.target.wants/hostapd.service in the [Unit] section in order to start after dhcpd4.service:
/etc/systemd/system/multi-users.target.wants/hostapd.service

[Unit]
After=dhcpd4.service


High availability


The idea for guarantee a reliable system to be always active is to check if all services are working properly. The services that will be checked are:

  • ppp
  • wlan
  • netcfg
  • hostapd
  • dhcpd
  • iptables

There is a counter that count the number of errors. If the script cumulate more than four errors during the iterations, the shutdown will start. The counter will be set to zero if all the services work properly.
The file /usr/bin/check.system is the following:
https://github.com/fsquillace/raspy/blob/master/bin/check-system



Let's create a systemd service in order to manage the previous script using systemd. In particular, the script needs to start after the network target and the dhcpd daemon.
The file /usr/lib/systemd/system/check-system.service is here:
https://github.com/fsquillace/raspy/blob/master/usr/lib/systemd/system/c...

To start the service:
>> systemctl start check-system.service
>> systemctl enable check-system.service


Power management


The file /usr/bin/mode-switcher:
https://github.com/fsquillace/raspy/blob/master/usr/lib systemd/system/mode-switcher.service

In the same way as the High availability section, we create a a systemd service. Also in this case the script needs to start after the network target and the dhcpd daemon.
The file /usr/lib/systemd/system/mode-switcher.service is:
https://github.com/fsquillace/raspy/blob/master bin/mode-switcher

To start the service:
>> systemctl start mode-switcher.service
>> systemctl enable mode-switcher.service


Future works and Drawbacks


TODO ... ;-)