Internet · Linux · Making things work · Raspberry Pi

Running a always on torrent box with Raspberry Pi (Deluge on Raspbian)


Raspberry Pi has a very low power consumption to keep it up and running 24/7. When you are downloading torrent files from or/and using it as a media server or local file server, it surely has the winning combination. There are two popular Bittorrent client which are popular as always on torrent client in Linux server and embedded device community including  Raspberry Pi. They are Transmission and Deluge. Deluge has a simpler interface and more complete set of configuration tools, while Transmission has some  advanced bandwidth control feature based on time schedule. In this Tutorial we shall be configuring Deluge. In this tutorial we shall walk though the installation Deluge on a raspberry pi as always on torrent server.

Prerequisites:
  1. Raspberry Pi with Raspbian installed
  2. LAN connectivity over cable of wifi with Internet connectivity
  3. Storage for downloading torrents (with additional usb power for external disks as needed)

You may also want to share the downloaded files with your media players e.g. Mobile, laptop, tab. To do so you shall need to setup a your Raspberry Pi as fileserver or nas box. Either the way you shall need to attach a large storage. Therefore you shall be Interested in completing the Following tutorial before you start setting up an always on torrent server.

Before you start you may want prepare your Pi Box by completing the following tutorial
  1. Setting up your Raspberry Pi headless
  2. Attaching External USB storage with your Raspberry Pi
  3. Setting Up your Raspberry Pi as a file sharing/Nas server

1. Setting up Deluge on Raspberry Pi:

Once you have a storage attached with your Raspberry Pi, you are ready to install the Deluge Torrent Client on your Raspberry Pi box.

Login to the Raspberry Pi terminal

1.A. Update and upgrade (on Raspberry Pi)::

Update your Raspbian Raspberry Pi Box to the latest distribution using the following command in the console:

sudo apt-get update
sudo apt-get upgrade -y
1.B. Install Deluge (on Raspberry Pi)::

Next you we shall download the Deluge daemon and necessary component for thin client setup with the following command in the console:

sudo apt-get install deluged -y
sudo apt-get install deluge-console -y
1.C. Setup credential for remote connection (on Raspberry Pi):

From this point on wards we shall do all the configuration changes as Raspbian system user user “pi” on the Raspberry Pi console. Eventually when we shall add this user as a daemon we shall run it as the user “pi”. To check run

id -nu

The out put should be the following, before you proceed on for the next configuration.

pi

Now lets run the deluged demon once and kill it again. By doing so it will create the initial configuration files e.g. “~/.config/deluge/auth”. Run the following command in the console:

deluged
sudo pkill deluged

Now that the default configuration files are created you may edit them and add authentication information. This authentication information will be used for remote login from windows/mac to the Deluge daemon. Next we shall make a backup of the default configuration file and add an authentication line for remote login to the same.

cp ~/.config/deluge/auth ~/.config/deluge/auth.old
nano ~/.config/deluge/auth

Once the file is open in “nano” text editor, add the following line to the existing configuration as a new line. The format shall be

User-name-for-remote-login:Password-for-remote-login:Access-level

Example:

pi:Set-your-Own-Password:10

Deluge-config-edit

Once the changes are made as described above, save the nano file by pressing “Ctrl-x”  and “y” when prompted for confirmation of saving the file and followed by an “Enter” key.

Next we shall start the Deluged daemon and run the Deluge console to make some additional configuration changes

deluged
deluge-console

If starting the console gives you an error code instead of nice cleanly formatted console interface type “exit” and then make sure you’ve started up the daemon.

Once you are logged in to the console, we shall make some configuration change to allow remote client connection, by entering the following command on deluge-console

config -s allow_remote True
config allow_remote
exit

Deluge-console

The deluge terminal output shall confirm you that the configuration changes are saved. Now we shall need to restart deluged command one more time to let the configuration changes to take effect.

sudo pkill deluged
deluged

deluge daemon will start again and ready for remote access.

2. Setting up client environment ( On windows):

We shall now install a desktop client of deluge and connect remotely to the deluge running on our Raspberry Pi. To do so we shall first download the Deluge desktop client appropriate for your desktop environment

2.A. Download deluge client (on windows)

Download and install appropriate desktop client based on your OS/Distribution from Deluge Downloads

Once installed we shall make some quick configuration changes to make the client application to control the deluge installed on the Raspberry Pi.

2.B. Run deluge client on classic mode:

When you first lunch Deluge from your desktop environment we shall check check if the interface is running on “classic mode”. To so you will need to go to preference menu by navigating to:

Edit –> preference

or

press Crtl – p (on your windows machine)

On the preference windows you will see list of categories on the left side, select interface. Under interface you shall find Class mode. If the option under “Classic mode” is selected green un-select it, if it is unselected leave it as it is. If you have un-selected it, press ok. To apply this change( disabling classic mode) by pressing ok, you shall be prompted for a restart. Say yes and start deluge client again.

Deluge-disable-classsic-mode

2.C. Connecting Raspberry Pi deluge daemon with the client:

Once you Restart Deluge, the connection manager should automatically appear. if it does not you may need to perform the following steps:

edit –> and click on connection manager

or Press on your windows machine

Ctrl-m

This shall bring up the deluge connection manager. Once the connection manager is up, click on add.

deluge-conection-manager

Place the Raspberry Pi ip address in to the hostname section. Enter the login and password as we have setup on the section “Setup credential for remote connection:” For this tutorial the server hostname is “192.168.1.201” and username and password is in the file “~/.config/deluge/auth”. To view this file use:

cat ~/.config/deluge/auth

Once password is entered click on “add”

deluge-conection-manager-add-host

This shall take you back to the connection manager. This shall also display the newly added host ip address with a green status indicator on the right side of the host name. You may now select this host and click on connect.

deluge-conection-manager-connect

You are now successfully connected your raspberry pi deluge daemon with your desktop client.

3. Deluge Configuration

3.A. Configuring deluge daemon for download location:

The internal storage for the configured Raspberry Pi is probably not sufficient for downloading large files. To complete this step, you need have a external storage configured and mounted with sufficient space. As we have mentioned at the beginning of this post complete this step process if Attaching External USB storage with your Raspberry Pi. Once completed you shall have your desired storage attached under “/nas” folder in your Raspberry Pi.

3.B. Creating torrent folders on Raspberry Pi:

Under the /nas folder we shall create some more directories to store all our downloaded files. To start with we shall create a folder called “torrent-download”. Under that folder we shall create fore more directories to store files based on its download status and file type. We shall create all these directory as the user “pi”. The daemon “deluged” shall run as the user “pi” therefore the user pi must have write access to all the folders “deluged” shall be using. If we have mounted a disk (pen drive or a hard disk formatted on windows, this might be a problem as, we shall not be able to change folder permission of a non unix file system after they are mounted.

a File system permission with msdos/vfat or b File system permission with ext3/ext4

3.B.a. Caution If it is a msdos/vfat file system, we shall make sure that it is mounted with group ownership of  “users” and umask of “0002”. If it is an automounted folder as described in the tutorial mount usb storage fat on raspberry pi, unless you have already done so, make sure you have the following entry on your /etc/fstab, and reboot the system.

LABEL=32GB       /nas      vfat    umask=0002,gid=users        0       0

After rebooting the system create this /nas/torrent-download folder with the following command

mkdir /nas/torrent-download

Or

3.B.b. Caution If it is an ext3/ext4 file system e.g. formatted on linux you shall need to change group ownership of the folder /nas/torrent-download to pi and continue on creating the folders

sudo mkdir /nas/torrent-download

If you your storage device is on a etc3/ext4 file system run the following command or else ignore the next command

sudo chwon pi /nas/torrent-download

Now create the rest of the folders as the user “pi”

mkdir /nas/torrent-download/downloading
mkdir /nas/torrent-download/completed
mkdir /nas/torrent-download/auto-download
mkdir /nas/torrent-download/backup-torrent

Setting torrent folder location on deluge client preference (on Windows):

3.C. Creating torrent folders on Raspberry Pi(continues):

Run deluge client on windows if it is not already running

go to preference of Deluge by navigating to edit –> preference –> Downloads ( selected)

or press Ctrl-p (on windows)

One the preference window is open, and Downloads is selected, you shall see folder options under “Downloads”

Enable the option and set the folder path as following

Set “Download to” text box to: “/nas/torrent-download/downloading
Enable “Mode to completed to” and set the text box to: “/nas/torrent-download/completed”
Enable “Auto add .torrents from:” and set the text box to: “/nas/torrent-download/auto-download”
Enable “copy .torrents files to:” and set the text box to: “/nas/torrent-download/backup-torrent”
(do not include the quotes while entering the file path)

deluge-Preferences-Downloads

Once you are done enabling the above options and filling up the folder names press ok and we are ready to download.

4. Testing your always on torrent download box setup:

You may try any torrent as you wish as this point for testing. However for the completeness of this tutorial, I am providing the torrent link I tested the system with.

Loading a torrent using .torrent file on Deluge:

We shall try to download Raspbian Jessie image from the following link. https://downloads.raspberrypi.org/raspbian_latest.torrent

By clicking this link should automatically download the torrent file. Once you try to open it Deluge shall automatically lunch and it shall prompt you to add the torrent to deluge as per following. You shall need to click add to continue.

deluge-Auto-add-torrents

However if you have any other application which associated in your pc with your .torrent extension, you shall need to open it manually. You may do so by Browsing to the download location from Deluge client interface by clicking on File-> add torrent –> URL  and you shall be prompted with a + From URL box, Fill the box with the following url “https://downloads.raspberrypi.org/raspbian_latest.torrent” and press ok

deluge-add-torrent-from-url

The torrent should be loaded automatically in to the add torrents dialog. Click on add to start downloading the torrent. Check is the download has started.

deluge-downloading

Loading a torrent using magnet link on Deluge:

Downloading torrent using magnet link is a little different than using a .torrent file. Here is an example site, Once you get into it, click on the magnet icon, if you hover over shall say “Download via magnet link”. right click (left click you your mouse is left handed) ) on  magnet icon and select copy link address from the option menu.

Deluge-magnetDeluge-magnet-copy-link-address

Now open up Deluge File-> add torrent –> URL  (or Ctrl+o on windows) Once url is button is selected, the copy field is automatically filled in on the box called “+ From URL”. If the url box is empty, click on it and paste the copied URL. Click “OK”  and after that click add “ADD”. The file should start downloading.

image

5. Configuring Deluge to Run on Startup

Now that we have Deluge up and running we shall make some changes in the system so that the deluge daemon kicks in at the start of the raspberry pi (Raspbian boot). Deluge developer has provided with a deluge daemon script for Ubuntu, which works fine with Raspbian on Raspberry Pi with a single change specific for this installation, which is on the first file “/etc/default/deluge-daemon” set DELUGED_USER=”pi”

sudo nano /etc/default/deluge-daemon

Content for the file “/etc/default/deluge-daemon” is the following

# Configuration for /etc/init.d/deluge-daemon

# The init.d script will only run if this variable non-empty.
DELUGED_USER=””             # !!!CHANGE THIS!!!!

# Should we run at startup?
RUN_AT_STARTUP=”YES”

Copy the above content and paste it in the following file in your Raspberry Pi console inside the opened files on “nano”.

sudo nano /etc/default/deluge-daemon

Save and exit nano

Content for the file “/etc/init.d/deluge-daemon” is the following

#!/bin/sh
### BEGIN INIT INFO
# Provides:          deluge-daemon
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Should-Start:      $network
# Should-Stop:       $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Daemonized version of deluge and webui.
# Description:       Starts the deluge daemon with the user specified in
#                    /etc/default/deluge-daemon.
### END INIT INFO

# Author: Adolfo R. Brandes
# Updated by: Jean-Philippe “Orax” Roemer

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC=”Deluge Daemon”
NAME1=”deluged”
NAME2=”deluge”
DAEMON1=/usr/bin/deluged
DAEMON1_ARGS=”-d”             # Consult `man deluged` for more options
DAEMON2=/usr/bin/deluge-web
DAEMON2_ARGS=””               # Consult `man deluge-web` for more options
PIDFILE1=/var/run/$NAME1.pid
PIDFILE2=/var/run/$NAME2.pid
UMASK=022                     # Change this to 0 if running deluged as its own user
PKGNAME=deluge-daemon
SCRIPTNAME=/etc/init.d/$PKGNAME

# Exit if the package is not installed
[ -x “$DAEMON1” -a -x “$DAEMON2” ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$PKGNAME ] && . /etc/default/$PKGNAME

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

if [ -z “$RUN_AT_STARTUP” -o “$RUN_AT_STARTUP” != “YES” ]
then
log_warning_msg “Not starting $PKGNAME, edit /etc/default/$PKGNAME to start it.”
exit 0
fi

if [ -z “$DELUGED_USER” ]
then
log_warning_msg “Not starting $PKGNAME, DELUGED_USER not set in /etc/default/$PKGNAME.”
exit 0
fi

#
# Function to verify if a pid is alive
#
is_alive()
{
pid=`cat $1` > /dev/null 2>&1
kill -0 $pid > /dev/null 2>&1
return $?
}

#
# Function that starts the daemon/service
#
do_start()
{
# Return
#   0 if daemon has been started
#   1 if daemon was already running
#   2 if daemon could not be started

is_alive $PIDFILE1
RETVAL1=”$?”

if [ $RETVAL1 != 0 ]; then
rm -f $PIDFILE1
start-stop-daemon –start –background –quiet –pidfile $PIDFILE1 –make-pidfile \
–exec $DAEMON1 –chuid $DELUGED_USER –user $DELUGED_USER –umask $UMASK — $DAEMON1_ARGS
RETVAL1=”$?”
else
is_alive $PIDFILE2
RETVAL2=”$?”
[ “$RETVAL2” = “0” -a “$RETVAL1” = “0” ] && return 1
fi

is_alive $PIDFILE2
RETVAL2=”$?”

if [ $RETVAL2 != 0 ]; then
sleep 2
rm -f $PIDFILE2
start-stop-daemon –start –background –quiet –pidfile $PIDFILE2 –make-pidfile \
–exec $DAEMON2 –chuid $DELUGED_USER –user $DELUGED_USER –umask $UMASK — $DAEMON2_ARGS
RETVAL2=”$?”
fi
[ “$RETVAL1” = “0” -a “$RETVAL2” = “0” ] || return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
# Return
#   0 if daemon has been stopped
#   1 if daemon was already stopped
#   2 if daemon could not be stopped
#   other if a failure occurred

start-stop-daemon –stop –quiet –retry=TERM/30/KILL/5 –user $DELUGED_USER –pidfile $PIDFILE2
RETVAL2=”$?”
start-stop-daemon –stop –quiet –retry=TERM/30/KILL/5 –user $DELUGED_USER –pidfile $PIDFILE1
RETVAL1=”$?”
[ “$RETVAL1” = “2” -o “$RETVAL2” = “2” ] && return 2

rm -f $PIDFILE1 $PIDFILE2

[ “$RETVAL1” = “0” -a “$RETVAL2” = “0” ] && return 0 || return 1
}

case “$1” in
start)
[ “$VERBOSE” != no ] && log_daemon_msg “Starting $DESC” “$NAME1”
do_start
case “$?” in
0|1) [ “$VERBOSE” != no ] && log_end_msg 0 ;;
2) [ “$VERBOSE” != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ “$VERBOSE” != no ] && log_daemon_msg “Stopping $DESC” “$NAME1”
do_stop
case “$?” in
0|1) [ “$VERBOSE” != no ] && log_end_msg 0 ;;
2) [ “$VERBOSE” != no ] && log_end_msg 1 ;;
esac
;;
restart|force-reload)
log_daemon_msg “Restarting $DESC” “$NAME1”
do_stop
case “$?” in
0|1)
do_start
case “$?” in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart|force-reload}” >&2
exit 3
;;
esac

:

Copy the above content and paste it in the following file in your Raspberry Pi console inside the opened files on “nano”.

sudo nano /etc/init.d/deluge-daemon

Save and exit nano

Make the script executable by root

sudo chmod 755 /etc/init.d/deluge-daemon

Run this script on start up

sudo update-rc.d deluge-daemon defaults

Start the daemon

sudo invoke-rc.d deluge-daemon start

At this point your startup service for deluged should be ready and working.

Reboot the Raspberry Pi with the following command

sudo reboot

Connect to the deluged daemon with your windows/desktop deluge client to verify.

Congratulations, you have successfully installed your always on torrent box using Raspberry Pi and Deluge.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s