Monday 23 March 2015

Simple local network with package cacher for Raspberry Pi labs/classes/dojos

We had a great mini-Jam session at CoderDojo Ham yesterday. All the activities that I've put together for Pi Codeclub were available and everyone seemed to enjoy the hardware programming. Making an led blink seems like a simple achievement but everyone seems to find it very satisfying (especially when using a chunky 10mm led).

I wanted to give everyone an opportunity for a bit of Linux sysadmin as well because I think that having a little command line knowledge under your belt can make the difference between a Pi that sits getting dusty on a shelf and one that becomes a really fun and useful device.

Of course installing packages is a key task, but one that requires a network connection. This can be tricky at the youth centre: there are no ethernet ports in the hall, the wifi is a bit flaky and speed can be quite slow, especially if the Scratch sessions are using the online version. Another problems is that we only have a few wifi dongles, not enough for every Pi.

So I wanted a way to enable participants to be able to experience the joy of apt-get. The solution build a simple ethernet network with an apt-caching relay server. This will cache a copy of any packages downloaded so that they can be quickly retrieved, even without an Internet connection.

The dumb switch is not a sophisticated bit of equipment - you can pick up an 8 port TP-LINK one for < £8 on Amazon.

So in the case of my CoderDojo, I simply update and install all the packages that are mentioned in the projects beforehand so that even if there is no Internet connection available at the Youth Centre, participants can still complete the activities.

Here's the procedure I followed.

Server


1. Download the latest Raspbian image and burn an SD card.

2. Change the hostname (edit /etc/hosts and /etc/hostname and change raspberrypi to whatever you want your server to be called).

3. Install these packages using apt-get:

sudo apt-get install dnsmasq
sudo apt-get install apt-cacher
sudo apt-get install apache2

4. Set a static IP address. In this example I'll set it to192.168.50.43 as shown in the diagram above.

5. Start x-windows (startx) and add the wifi network through the GUI.  Obviously if you're preparing this at home like I did, you can test with your own network then add whatever wifi you'll be using at the location where you're running your event when you get there.

6. Dnsmasq will run a DHCP server to allocate dynamic IP addresses to your client Pis. Edit /etc/dnsmasq and set:
interface=eth0
dhcp-option=3,192.168.50.43
dhcp-range=192.168.50.150,192.168.50.165,255.255.255.0,12h
7. To allow your server to act as a router/relay, edit /etc/sysctl.conf and add
net.ipv4.ip_forward=1
8. Edit /etc/default/apt-cacher

AUTOSTART=1 
9.  Create a script to use iptables to act as arealy for packets destined for the Internet. Note that 192.168.1.98 below is the IP address allocated to the wifi interface - you'll need to change this for whatever IP address you receive on the wifi network you're using.

#!/bin/sh
IPT=/sbin/iptables
LOCAL_IFACE=eth0
INET_IFACE=wlan0
INET_ADDRESS=192.168.1.98
# Flush the tables
$IPT -F INPUT
$IPT -F OUTPUT
$IPT -F FORWARD
$IPT -t nat -P PREROUTING ACCEPT
$IPT -t nat -P POSTROUTING ACCEPT
$IPT -t nat -P OUTPUT ACCEPT
# Allow forwarding packets:
$IPT -A FORWARD -p ALL -i $LOCAL_IFACE -j ACCEPT
$IPT -A FORWARD -i $INET_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT
# Packet masquerading
$IPT -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_ADDRESS

 Client

1. Download the latest Raspbian image and burn an SD card.

2. Change the hostname (edit /etc/hosts and /etc/hostname and change raspberrypi to whatever you want your server to be called).

3. Create a file /etc/apt/apt-conf.d/90-apt-proxy.conf with the following contents:

Acquire::http::Proxy "http://192.168.50.43:3142; 

No comments:

Post a Comment