Monday, January 7, 2013

Building a Headless Torrent Client Using A Raspberry Pi

The Raspberry Pi makes a great low powered torrent client machine which you can leave on 24/7. The main reason I’ve done this is to save power by running the torrent client on the extremely low powered Raspberry Pi. It also frees up my computer and keeps things a bit neater.
Before getting started this guide assumes that you’ve got a running Raspberry Pi. I’d recommend setting a static IP address which you can do using this guide here. I’d also recommend enabling SSH so that you can remote administer your Pi. The easiest way to do this is to run the following command:
sudo raspi-config
This brings you back into the first menu you encountered when you setup the Raspberry Pi. There’s a menu item to Enable / Disable SSH. Go in there and enable it. While you’re at it it’s also a good idea to disable the boot into desktop option using the menus as well (unless you are using the GUI, in that case keep it!)
You can also use putty to remotely access the command line on your Pi. Using Putty is outside of the scope of this tutorial but essentially you plug in the IP address of your Pi and connect to it. If you don’t know your IP address you can use the following command:
ifconfig
Other than a working Pi the only other thing you need is a USB drive. I’d recommend getting something that’s a decent size, at least 16GB as the size of the files you’re working with can add up fast. I’ve gone with a thumb drive as I want the unit self contained, low power draw and want to minimise the number of external power packs I’m using.
First we’re going to plug the USB thumb drive into the Raspberry Pi. There’s a good chance you already have a partition on the USB drive. Either way we’re going to remove all the partitions on the drive and start fresh. To partition the disk (typically /dev/sda) run the following command:
sudo fdisk /dev/sda

Press ‘p’ to list all the partitions on the disk. If there’s any existing partitions, use ‘d’ to delete them. Once all the partitions have been removed press ‘n’ to create a new partition. Follow the default prompts to create a primary partition that uses the entire disk. When you’re done press ‘w’ to write the partition table to the disk and exit fdisk.
The new partition will be /dev/sda1, that is the first partition on the first attached disk. The next step is to format the disk with the ext4 file system.
sudo mkfs -t ext4 /dev/sda1
Now that the partition has been formatted we need somewhere to mount it to. Create a new mount point under the media folder:
sudo mkdir /media/usbdrive

We want to mount the USB drive automatically on boot. To do this we need to edit the /etc/fstab file.
sudo echo '/dev/sda1 /media/usbdrive ext4 defaults 0 1' >> /etc/fstab
Now we can mount all the entries in the /etc/fstab file using:
sudo mount -a
With the USB file system mounted we can now setup the directory structure that transmission will be using to upload / download files.
sudo mkdir /media/usbdrive/torrents
sudo mkdir /media/usbdrive/torrents/torrents
sudo mkdir /media/usbdrive/torrents/incomplete
sudo mkdir /media/usbdrive/torrents/complete
sudo chmod 777 -Rf /media/usbdrive/torrents/
Next we can install transmission on the Pi.
sudo apt-get install transmission-daemon
Time to take a break while the install takes place. After the packages have been downloaded it takes quite some time to unpack and install the packages.

Once transmission has been installed it’s time to configure the service. We need to stop the service first though as it locks the configuration file and will overwrite any changes you make to it.
sudo service transmission-daemon stop
Next we edit the transmission-daemon configuration file:
sudo nano /etc/transmission-daemon/settings.json
Below I’ve put the contents of my settings.json file. There’s many configuration items you can play with however the key lines to update are:
  • ‘blocklist-enabled’ - enables the use of blocklists. These filter out IP ranges from connecting to the server.
  • ‘blocklist-url’ – The URL of the rules file that contains the blocked IP addresses. These can be downloaded from www.iblocklist.com. I use the level 3 blocklist which is the ‘paranoid’ setting and blocks a lot of IP ranges. You’ll be more secure but your download speeds will suffer.
  • ‘download-dir’ – set this to your completed downloads folder (/media/usbdrive/torrents/complete)
  • ‘incomplete-dir’ – set this to /media/usbdrive/torrents/incomplete. Partial downloads will be stored here until they are completed. Then they will be moved to the completed folder.
  • ‘incomplete-dir-enabled’ – set to true. This enables the folder above.
  • ‘rpc-whitelist’ – these are the IP ranges that can access the transmission web interface
  • ‘rpc-whitelist-enabled’ – set to true. This enforces the whitelist.
  • ‘watch-dir’ – torrent files placed into this folder will be added to the download queue.
  • ‘watch-dir-enabled’ – set to true. Enables the watch-dir.
    "alt-speed-down": 50,
    "alt-speed-enabled": false,
    "alt-speed-time-begin": 120,
    "alt-speed-time-day": 127,
    "alt-speed-time-enabled": true,
    "alt-speed-time-end": 360,
    "alt-speed-up": 350,
    "bind-address-ipv4": "0.0.0.0",
    "bind-address-ipv6": "::",
    "blocklist-enabled": true,
    "blocklist-url": "http://list.iblocklist.com/?list=bt_level3&fileformat=p2p&archiveformat=gz",
    "cache-size-mb": 4,
    "dht-enabled": true,
    "download-dir": "/media/usbdrive2/torrents/complete",
    "download-limit": 200,
    "download-limit-enabled": 1,
    "download-queue-enabled": true,
    "download-queue-size": 4,
    "encryption": 0,
    "idle-seeding-limit": 30,
    "idle-seeding-limit-enabled": false,
    "incomplete-dir": "/media/usbdrive/torrents/incomplete",
    "incomplete-dir-enabled": true,
    "lpd-enabled": false,
    "max-peers-global": 200,
    "message-level": 2,
    "peer-congestion-algorithm": "",
    "peer-limit-global": 200,
    "peer-limit-per-torrent": 60,
    "peer-port": 51413,
    "peer-port-random-high": 65535,
    "peer-port-random-low": 49152,
    "peer-port-random-on-start": false,
    "peer-socket-tos": "default",
    "pex-enabled": true,
    "port-forwarding-enabled": true,
    "preallocation": 1,
    "prefetch-enabled": 1,
    "queue-stalled-enabled": true,
    "queue-stalled-minutes": 30,
    "ratio-limit": 1.5000,
    "ratio-limit-enabled": true,
    "rename-partial-files": true,
    "rpc-authentication-required": true,
    "rpc-bind-address": "0.0.0.0",
    "rpc-enabled": true,
    "rpc-password": "{626435a4f51d576b03dbe6d347c4d5251ea5741bt15bhZsv",
    "rpc-port": 9091,
    "rpc-url": "/transmission/",
    "rpc-username": "transmission",
    "rpc-whitelist": "127.0.0.1,192.168.*.*,203.62.187.74",
    "rpc-whitelist-enabled": true,
    "scrape-paused-torrents-enabled": true,
    "script-torrent-done-enabled": false,
    "script-torrent-done-filename": "",
    "seed-queue-enabled": false,
    "seed-queue-size": 10,
    "speed-limit-down": 250,
    "speed-limit-down-enabled": true,
    "speed-limit-up": 40,
    "speed-limit-up-enabled": true,
    "start-added-torrents": true,
    "trash-original-torrent-files": true,
    "umask": 18,
    "upload-limit": 100,
    "upload-limit-enabled": 0,
    "upload-slots-per-torrent": 14,
    "utp-enabled": true,
    "watch-dir": "/media/usbdrive/torrents/torrents",
    "watch-dir-enabled": true

Once configuration has finished we can restart the transmission daemon:
sudo service transmission-daemon start
The transmission daemon should now be running and downloading to the USB drive. You can access the web interface using the URL http://<ip>:9091/transmission/.

That’s the final step! You now run a headless torrent client that uses little power and can be accessed from anywhere using the web interface. There’s a lot more that can be done to add more features to the Pi. For example you can:
  • Automate downloads using flexget
  • Automatically move files off the headless torrent client to you’re media center.
  • Stream files off the Pi using SAMBA
  • Enable access to the transmission web interface from anywhere by adding rules and NAT to your router / firewall.
  • Monitor the performance of your headless server using cacti

1 comment:

  1. Thanks for this quite useful and detailed tutorial :-)

    ReplyDelete