Building a Router
I've known for some time that when I go to university I'll have one wired network port in my room, and that's it. However, I have two computers, Azathoth and Eihort. Today, my new case for Eihort arrived (pictures will be taken at some point…) which, unlike the old one, actually has expansion ports, and my mini-ITX motherboard has one PCI slot. This coincidence isn't accidental :P
So I stuck in a network card, went to Google, and learned how to set up connection sharing. My set-up now looks like this:
Azathoth -> Eihort -> Nyarlathotep -> Internet
Where Nyarlathotep is the router/modem I use to connect to the Internet.
My room has it's own little LAN in it, connected to the bigger LAN of my house through Eihort. At university the set up will be very similar except I doubt that their router(s) will be named Nyarlathotep (alas). In terms of interfaces, that's:
Azathoth (eth0) -> Eihort (eth1)
Eihort (eth0) -> Nyarlathotep -> Internet
Eihort
Firstly, you need iptables installed, and then you can set up one rule, just one! One rule to, err, rule them all…
pacman -S iptables
/etc/rc.d/iptables start
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
/etc/rc.d/iptables save
That's almost it. All to do now is give Eihort a static IP and tell the kernel it's OK to forward packets:
ifconfig eth1 10.1.1.1
echo 1 > /proc/sys/net/ipv4/ip_forward
To avoid having to do that echo on every boot, add net.ipv4.ip_forward = 1 to /etc/sysctl.conf. I also added the ifconfig command to /etc/rc.local (not really worth bothering with a netcfg profile over).
Azathoth
Azathoth needs to be assigned a static IP and told that Eihort is its gateway. Fortunately, using netcfg2, this is incredibly simple:
CONNECTION="ethernet"
DESCRIPTION="Shared network between desktop and server"
INTERFACE=eth0
IP="static"
ADDR="10.1.1.2"
GATEWAY="10.1.1.1"
Simple. Now running netcfg2 shared sets up Azathoth for my little set up. I did also have to edit my /etc/resolv.conf.head and /etc/hosts files to point to the new IP address of Eihort, 10.1.1.1.
And now, like magic, you have network sharing.
Network Block Device
I discovered this wonderful thing yesterday, nbd is, simply, a way of sharing block devices over a network. I currently have my server rsyncing to a backup HDD attached to my laptop.
NBD comes in two parts, the client and the server. The server sits on the host machine, listens for NBD network requests, and translates them into standard filesystem calls. The client sits on the client machine and is used to control the nbd kernel module. It's remarkably simple to share a device, for example:
# On the server:
nbd-server $port /dev/sdb1
# On the client:
modprobe nbd
nbd-client $ip $port /dev/nbd0
And voila, /dev/sdb1 on the server is now accessable on the client as /dev/nbd0. I used this method to partition my new HDD over the network using gparted, unfortunately parted gives an error message after every change complaining that the kernel hasn't re-read the partition table, so you can only do one thing at a time. I should really learn how to use parted properly…
I wrote a helper script to share devices between my laptop and server:
function nbd()
{
device=""
port=2000
host=""
if [[ -z "$3" ]]; then
if ifconfig wlan0 &>/dev/null; then # On my laptop
host=eihort
else # Not on my laptop
host=192.168.1.64
fi
else
host=$3
fi
# Allow specifying a device file, name, label, or UUID.
if [[ -e "$2" ]]; then
device=$2
elif [[ -e "/dev/$2" ]]; then
device="/dev/$2"
elif [[ -e "/dev/disk/by-label/$2" ]]; then
device="/dev/disk/by-label/$2"
elif [[ -e "/dev/disk/by-uuid/$2" ]]; then
device="/dev/disk/by-uuid/$2"
else
echo "Unknown device '$2'" 1>&2
return 1
fi
if [[ "$1" == "share" ]]; then
sudo nbd-server $port $device
elif [[ "$1" == "gain" ]]; then
sudo nbd-client $host $port $device
elif [[ "$1" == "free" ]]; then
sudo nbd-client -d $device
else
echo "Unknown method '$1'." 1>&2
return 2
fi
}
So, with a simple nbd share M2HDb and nbd gain nbd0, I can share my backup device :)







