Tutorial 1: Setting up RPi as a Torrent Server

by lezigg

In this tutorial I document what I did to set up Raspberry Pi (RPi) as a torrent server. Before you begin, please set up your RPi correctly. See: http://elinux.org/RPi_Easy_SD_Card_Setup.

I used Debian Squeeze 6.0.4 for my OS. Instructions may differ for Arch, Fedora, or for some of the other microOS distributions that are out there. Because the GUI on Debian Squeeze (LXDE) can be a bit of a memory hog (LXDE is actually quite light weight…but we only have 256mb of ram to work with), the entire tutorial will be done in command line. In addition, you need the following parts:

  1. A USB External Hard Drive
  2. USB wire (for External Drive)
  3. Ethernet wire
  4. A GOOD quality power supply (>= 2A current maximum).

This tutorial is presented in three parts. I give the basic setup on the RPi itself to get the task done. After this, more advanced tweaking options for transmission torrent client are given. Depending on your kernel or possible errors that occur, I outline some additional problems I encountered, and how they were solved.

Note: For the code examples given, single arrow “>” means command line input. Double arrow “>>”means the lines one puts in the configuration files.

Basic Setup:

1) First let’s make our lives easier. On Debian, file permissions can cause some obstructions when running and editing files. Lets setup root and login as root. By default root access on Debian is done with sudo, and the “root” user is not setup. Go to terminal and type:

> sudo passwd root

and give root a password. Now log out of the system, and sign in as root.

2) With root access, lets get our torrent client. I chose transmission because its well supported and already available in our Debian repository. Type:

> apt-get update
> apt-get install transmission transmission-daemon transmission-cli

There are actually three different packages for the torrent client: transmission, transmission-daemon, and transmission-cli. transmission is the regular application that a desktop user would run; in an X windows environment it would launch a GUI front end.

Transmission is a lot more robust than this; transmission-daemon is a ‘headless’ (non-GUI) form of transmission used for more complex setups (such as, having multiple instances of transmission running across multiple computers in a LAN).  We only need one instance of transmision-daemon for our purposes.

3) Once installed, locate the transmission-daemon folder on your system. Type:

> cd /etc/transmission-daemon ; ls

You will see a number of files, but we care about the settings.json. Type:

> nano settings.json

And open it up. We are going to set up the ports for transmission-daemon traffic, and for the webUI port. Specifically, look for lines that start with “rpc”. Here are the RPC lines:

“rpc-enabled”: true,
“rpc-password”: “<your password>”,
“rpc-port”: “<your chosen port>”,
“rpc-username”: “<your username>”,

Here we are enabling the Remote client. Set “rpc-enabled” to true, as well as your chosen port, password and user name.

Note #1: Security conscious users might be worried about storing their password in plain text. Dont worry: when transmission starts running, it reads this file and replaces the password entered with a hashed password. You can check this out yourself to verify it.

Note #2: You need to check to make sure your ports are free. Before doing this, type:

> netstat -nap.

and see if your desired port is already registered!

4) Next,  configure your LAN router to allow for outside connections. Get your IP address for your RPi by typing:

> ifconfig eth0

If you are on a LAN, your IP address should have the form 192.168.X.Y.

Log into your router, and in the ‘Virtual Servers’ tab, associate your chosen port number to your RPi’s given IP address. Here it is for my router: I’ve chosen port 8002 for the webconsole, and 8001 for incoming torrent traffic.

Most routers have DHCP enabled, so we need a static IP address for port mappings to hold. Configure your router to permanently assign the RPi and IP address, so the mappings sticks. Again, it’s up to you to find this option on your router. On my router (a D-link), I was able to do this under “Network Settings”.

Mounting a Hard Drive:

5) We need to connect an external hard drive to Linux, to store our torrent files. I happened to have an NFTS file system on my external hard drive. RPi will recognize the drive, but it may not mount it correctly.

To start, connect your hard drive via USB and let it initialize. Now type:

> fdisk -l

The tables that are produced represent each physical hard drive; every row of the table is a partition on the hard drive. Look for the table with “NFTS” and record the device boot path. In my example, it is /dev/sda1. Sample console output below:

6) For Debian, mounted drives are accessed through the /media folder. Lets mount the drive:

> cd /media
> mkdir <your directory name>
> mount -t ntfs-3g /dev/sda1 /media/<your directory name>

Above, we made a directory for our mount command to associate the mounted drive to a folder. Now that this is done, let’s go to settings.json and set up our folders for transmission-daemon:

>cd /etc/transmission-daemon
> nano settings.json

Look for the following lines, and type in the following information:

>>”download-dir”: “/media/<your directory name>/<folder on drive>”,
>>”incomplete-dir”: “/media/<your directory name>/<some other folder on drive>”,
>>”incomplete-dir-enabled”: true,

Where <your directory name> is the name chosen when we made the directory (above). Save the file.

7) Finally, we would like our transmission torrent client to start up automatically when our RPi is turned on. We need to edit the rc.local start up
file to make this possible.

> cd /etc
> nano rc.local

Add the following lines to the very bottom of the file:

>> mount -t ntfs-3g /dev/sda1 /media/<your directory name>
>> transmission-daemon &

Restart your RPi. Basic setup is done!

Check that RPi is Working Correctly:

1) First check if you can access your torrent server from inside your LAN. In a browser, type: <RPi’s LAN address>:<web client port number>. A login box should appear:

Login with your creditials. If successful, you will see the web console like so:

2) Now check to see if you can access from outside your LAN. You will need the IP address assigned to your modem, from your ISP (google: whats my ip? to get this). In a browser type: http://<your modem ISP>:<your webUI port number>.  You should go straight to the webUI console, if you already logged in in step (1).

Additional Setup:

There are a number of other features in settings.json that can be set. Here are some of note:

  • “cache-size-mb”: Sets our cache size before writing to the External USB drive.
  • “download-limit”, “download-limit-enabled”: Set your download limits.
  • “encryption”: Set to 1 to turn it on.
  • “max-peers-global”, “peer-limit-global”,”peer-limit-per-torrent”: Control the number of peers that connect to your server.
  •  “open-file-limit”: Limit how many torrent files can download at once.
  • “ratio-limit”, “ratio-limit-enabled”: Limit your Leech:Seed Ratio. Be nice and set it to 1.000 or above :).
  • “script-torrent-done-enabled”,”script-torrent-done-filename”: Quite an interesting feature. When your torrent file finishes, you can run a script to process it, or move it to another directory.
  • “speed-limit-down”, “speed-limit-down-enabled”, “speed-limit-up”, “speed-limit-up-enabled”, “upload-limit”, “upload-limit-enabled”, “upload-slots-per-torrent”: This whole list of options controls download and upload speeds.

Problems and Hangups:

Alas, doing the basic setup probably won’t work for everyone. I had a number of issues I had to solve before I could get things working.

1) Incomplete torrent files don’t stick around: Make sure the “incomplete-dir-enabled” option is set to True (1).

2) My RPi’s Ethernet Driver keeps crashing!

This is by far the biggest issue for the whole setup. You might find that doing file transfers or Downloads at high speeds (> 500k/sec) causes the RPi ethernet driver to crash. You won’t be able to SSH into the RPi, or access the internet with it, but the RPi runs fine otherwise.
The reasons for this vary. Perhaps a bit of background is in order.

The RPi Ethernet and USB ports are controlled by one single chip, which share the same system bus. Naturally, our torrent box application puts a lot of load on this chip and bus. Not only is a lot of traffic flowing through the ethernet port, but we are also using the same system bus to write to the USB connected hard drive.  More specifically, Ethernet packets are being saved to a cache in memory (RAM), as are the incomplete torrent files. RPi also writes cached information to the disk, while accessing other disk files to supply information to other leechers on our torrent network. So there is high two-way traffic across the bus. You better have a well compiled kernel to handle all this traffic correctly.

Here are a number of solutions to try (in order of complexity), to avoid an Ethernet Driver Crash:

(a) Use a proper power supply: I’ve mentioned this before. And I know using your microUSB phone charger to power the RPi is tempting. But high External Hard Drive + Ethernet usage needs more than the 700mA minimum quoted in the RPi operating specs. Most phone power supplies are 1A or below. Please try a more beefy power supply. Really.

(b) Change your Kernel: Older versions of the ARM kernel aren’t compiled to handle the high system bus traffic problem outlined above. A newer kernel patch has been recently issued to alleviate this problem. Apparently the Arch Linux RPi distribution has a kernel update that patches this automatically. Other distributions do not, and you have to replace your kernel manually:

In theory, the Linux kernel should be perfectly swappable and your system should boot, after you swap your old kernel for the patched one. For the brave, type the following commands to do so:

> cd /boot
> wget http://positron.dckd.nl/~jeroen/stuff/rpi-ipv6-kernel.img
> mv kernel.img kernel.backup.img
> mv rpi-ipv6-kernel.img kernel.img

and restart your system. I got my kernel from the RPi fourm thread [ http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=5142&hilit=%5Bsolved%5D+ethernet&start=25 ], and it alleviated my Ethernet problems.

Note: Recovering the kernel: This worked for me (and many others), but in the event it doesn’t work for you – or worse, your RPi doesn’t boot, do the following to recover your old kernel. Access your RPi file system by plugging the SD card in another system, and navigate to the /boot folder. Then delete the new kernel.img file, and reinstate the old backup one. So:

> rm -f kernel.img
>mv kernel.backup.img kernel.img

(c) Edit vm.cache: Once your kernel is patched, the torrent server should work fine in most cases. If not, you can edit the sysctrl.conf file to increase cache size, like so:

>cd /etc
> nano sysctl.conf
…. locate the line vm.cache_size….

>> set vm.cache_size = 32000

And save the file.  Restart your system.

Change Log and Thanks:
27/08/2012: Fixed command line typos [therulerofchina]


About these ads