Download Box Using a Raspberry Pi

Deluge Desktop Client

Having a dedicated device for handling downloads becomes handy if you have a lot of downloading to do throughout the day. But maintaining a separate computer just for downloading is an overkill. Network Attached Storage (NAS) devices that are capable of downloading are quite expensive as well. Fortunately, we no longer have to look far thanks to the variety of inexpensive, single board computers that are rapidly gaining popularity today. The Raspberry Pi is in the forefront of this arena.

This post describes how to setup an inexpensive download box using a Raspberry Pi.

Goals

I had the following goals in mind when I started working on this project.

  1. Headless server setup which handles all the downloads
  2. Support for multiple protocols (BitTorrent, FTP and HTTP)
  3. Support for file sharing sites such as Mediafire, 4Shared, etc
  4. Ability to schedule and throttle bandwidth usage
  5. Accessible over the local network as well as the Internet
  6. Support for multiple platforms and devices (web, desktop and mobile clients)

Required Hardware

Given below is a list of hardware I used together with their respective prices at the time of writing.

Item Price
Raspberry Pi 2 Kit $49.95
Raspberry Pi Clear Acrylic Case $12.99
MicroSD Card (8 GB, Class 10) $6.99
External Hard Drive  N/A

Note – Raspberry Pi is not capable of powering the external hard drive through the USB port. Therefore please make sure to get a hard drive with an external power supply, or use a flash drive with sufficient capacity for storage.

I was not able to find a single application for the Raspberry Pi which can handle both HTTP/FTP and BitTorrent downloads satisfactorily. Hence I decided to go ahead with 2 separate applications. I used PyLoad for HTTP/FTP downloads and Deluge for BitTorrent downloads.

Install Raspbian

  1. Follow the official guide here to get Raspbian installed on your SD card.
  2. Boot up the Raspberry Pi and go through the initial configuration (using raspi-config).
    • Expand file system.
    • Change the password for pi user.
    • Set timezone using ‘Internationalization Options’.
    • Enable SSH using ‘Advanced Options’.
  3. Configure Ethernet or WiFi (or both) for internet access (refer this link).
  4. Update and upgrade repositories:

sudo apt-get update

sudo apt-get upgrade

Mount USB Drive

  1. Attached the hard drive to Raspberry Pi and power it up.
  2. SSH into the Raspberry Pi using a tool such as Putty.
  3. Add NTFS support for Raspbian using the following command:

sudo apt-get install ntfs-3g

  1. Use the following command to see a list of attached hard drives and their partitions:

sudo fdisk -l

At a minimum you should be able to see two disks as shown in the screenshot below:

fdisk Output
fdisk Output

The first disk /dev/mmcb1k0 is the SD card inside the Raspberry Pi that has the Raspbian installation. The second disk, /dev/sda is the external hard drive that we are looking for. It has a single partition by the name of /sda1. Make a note of this.

  1. Create a folder to mount the hard drive:

sudo mkdir /media/USBHDD

  1. Mount the hard drive using the following command:

sudo mount -t auto /dev/sda1 /media/USBHDD

  1. We can create additional folders inside the hard drive to keep HTTP/FTP and BitTorrent downloads separate:

sudo mkdir /media/USBHDD/Downloads

sudo mkdir /media/USBHDD/Downloads/Files

sudo mkdir /media/USBHDD/Downloads/Torrents

  1. Install Samba so that we can access the downloads folder over the local network:

sudo apt-get install samba samba-common-bin

  1. Make a backup of Samba configuration file

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old

  1. Edit Samba configuration file using the following command:

sudo nano /etc/samba/smb.conf

The first thing to change is the workgroup identifier, by default workgroup = WORKGROUP. If you’re using a different name for your home workgroup, go ahead and change that now, otherwise leave it as the default.

Next step is to turn on user authentication for the file share. Otherwise anyone who has access to the network will be able to access your downloads folder. Scroll down in the Samba configuration file until you come to the following section:

Samba Authentication Section
Samba Authentication Section

Remove the # symbol from ‘security = user’ line in order to enable username/password based authentication in Samba.

Next, we need to add a new section into the configuration file in order to define the file share and the users who are permitted to access it. You can add the following section to the bottom of the configuration file:

[Downloads]
comment = Downloads Folder
path = /media/USBHDD/Downloads
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no

Note: What appears in square brackets in the first line is the name of the file share. Feel free to change it as you see fit.

Save changes to the Samba configuration file.

  1. Restart Samba daemon using the following command:

sudo /etc/init.d/samba restart

  1. Next we need to add a user who can access the downloads folder. We are going to create a new user account with username ‘downloads’ and password ‘1234’. You should change the username and password to your preference since this is the user who can access the downloads folder over the network.

sudo useradd downloads -m -G users

sudo passwd downloads

Enter the password twice in order to confirm.

  1. Add ‘downloads’ user as a valid Samba user using the following command:

sudo smbpasswd -a downloads

Enter the password for ‘downloads’ account when prompted.

  1. Use another machine to confirm whether the Samba share is visible and accessible over the local network. It should prompt and accept the credentials for ‘downloads’ user.
Downloads Folder Network Login
Downloads Folder Network Login

Once logged in, check whether you are able to see ‘Downloads’ folder, and ‘Files’ and ‘Torrents’ folders which are inside the downloads folder. Also make sure that you have permissions to write to downloads folder by creating a new file inside it.

Install PyLoad

  1. Create a new system user for running PyLoad:

sudo adduser –system pyload

  1. Add the following two lines to your /etc/apt/sources.list:

deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi
deb-src http://archive.raspbian.org/raspbian/ jessie main contrib non-free rpi

  1. Update the package list and install dependencies that are needed by PyLoad:

sudo apt-get update
sudo apt-get -y install git liblept4 python python-crypto python-pycurl python-imaging tesseract-ocr zip unzip python-openssl libmozjs-24-bin
sudo apt-get -y build-dep rar unrar-nonfree
sudo apt-get source -b unrar-nonfree
sudo dpkg -i unrar_*_armhf.deb
sudo rm -rf unrar-*

cd /usr/bin
ln -s js24 js

  1. Download the current version of PyLoad:

cd /opt
sudo git clone https://github.com/pyload/pyload.git
cd pyload

  1. Now you can run PyLoad and start configuring it:

sudo -u pyload python pyLoadCore.py

When pyLoadCore is run for the first time, it will walk you through a wizard which will configure the PyLoad installation for you. This wizard is self explanatory and has plenty of explanations on what is being configured, thus I will not go into details of this step here.

PyLoad runs on port 8000 or 7227 by default. This can be configured by using the above mentioned wizard.

Open up a browser and browse your PyLoad installation. Use the IP address of the Raspberry Pi and the port you configured during the installation wizard. If everything went fine, you should now be able to see the login page as shown below:

PyLoad Login Page
PyLoad Login Page

Log in, click on ‘Config’ button and go through the configuration options. Set download folder to ‘/media/USBHDD/Downloads/Files’ folder.

  1. You can create a systemd service file if you want to start PyLoad when the raspberry pi boots up. Create a new service file first:

sudo nano /etc/systemd/system/pyload.service

Add the following into the service file:

[Unit]
Description=Python Downloader
After=network.target

[Service]
User=pyload
ExecStart=/usr/bin/python /opt/pyload/pyLoadCore.py

[Install]
WantedBy=multi-user.target

Activate the service using following command:

sudo systemctl enable pyload.service

A screenshot of PyLoad web interface is given below:

PyLoad Web Interface
PyLoad Web Interface

PyLoad also have mobile clients for Android, iOS and Windows Phone. A screenshot of the Android application is shown below:

PyLoad Mobile Client (Android)
PyLoad Mobile Client (Android)

PyLoad has a cross platform desktop client as well. However, it was not very mature at the time of writing.

Install Deluge

  1. Install Deluge daemon and other required components:

sudo apt-get install deluged

sudo apt-get install deluge-console

  1. Run Deluge daemon and shut it down afterwards. This will create a configuration file which we will be editing in the next step:

deluged

sudo pkill deluged

  1. Make a backup of the configuration file and open the original for editing:

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

nano ~/.config/deluge/auth

  1. Add a line to the bottom of the configuration file which has the following format:

user:password:level

‘user’ and ‘password’ are the username and password we want for the Deluge installation. Please change the values as per your preference. Level can be set to 10 which represents full-access/administrative level for the daemon. I used ‘pi:raspberry:10’ for this.

Save changes to the configuration file.

  1. Start up Deluge daemon again and then the console:

deluged

deluge-console

If starting the console gives an error, type ‘exit’ and make sure you have started the daemon first.

  1. Enter the following commands once you are inside the console:

config -s allow_remote True

config allow_remote

exit

This enables remote connections to your Deluge daemon and double checks whether the config variable has been set.

Deluge Console
Deluge Console
  1. Kill the daemon and restart it one more time so that the config changes take effect:

sudo pkill deluged

deluged

At this point your Deluge daemon is ready for remote access. We need to install Deluge desktop client on your PC in order to connect to the daemon running on the Raspberry Pi.

  1. Download and install Deluge installer for your operating system from the downloads page.
  2. Open Deluge client and navigate to Preferences -> Interface. Within the interface sub menu, you’ll see a check box for ‘Classic Mode’. By default it is checked. Uncheck it.
Deluge Classic Mode
Deluge Classic Mode
  1. Click OK and then restart the Deluge desktop client.

Next time when Deluge starts, it will show the Connection Manager. Click the Add button and insert information about your Raspberry Pi and the Deluge daemon running inside it.

Add Host
Add Host

You will need to input the IP address of the Raspberry Pi on your network, as well as the username and password you set during the earlier configuration. Leave the port at the default 58846. Click Add.

Back in the Connection Manager, you will see the entry for the Raspberry Pi. If all goes well, the indicator light will turn green as shown below:

Deluge Connection Manager
Deluge Connection Manager
  1. Click on the Connect button. Deluge client will connect to the Daemon running on your Raspberry Pi and show the main interface as shown below:
Deluge Desktop Client
Deluge Desktop Client

Configure Deluge Download Location

We need to configure Deluge to download torrents into the external hard drive. We will also be creating a few additional folders to keep downloading and completed torrents separate. A watch folder will also be created and configured so that we can add torrents we need to download directly into the watch folder.

  1. SSH into the Raspberry Pi and run the following commands:

sudo mkdir /media/USBHDD/Downloads/Torrents/Downloading

sudo mkdir /media/USBHDD/Downloads/Torrents/Completed

sudo mkdir /media/USBHDD/Downloads/Torrents/Watch

sudo mkdir /media/USBHDD/Downloads/Torrents/Backup

  1. Open Deluge desktop client and navigate to Edit > Preferences > Downloads. Set download locations as shown below:
Deluge Download Locations
Deluge Download Locations

Install Deluge WebUI

Deluge WebUI allows you to use almost all the features of the desktop client at the convenience of a web interface. All you need to access it is a web browser, no additional client software installations are necessary. Follow the steps given below to install and configure Deluge WebUI.

  1. SSH into the Raspberry Pi and enter following commands:

sudo apt-get install python-mako

sudo apt-get install deluge-web

deluge-web

Above commands install Mako, a template gallery for Python that the WebUI needs, and the WebUI itself. It then starts the WebUI program.

The default port for the WebUI is 8112. If you wish to change it use the following commands:

sudo pkill deluge-web

nano ~/.config/deluge/web.conf

This stops the WebUI and opens up the configuration file for it. Use nano to edit the line: “port”: 8112, and replace 8112 with a preferred port number.

After that, start WebUI again using the following command:

deluge-web

  1. Once you have the WebUI up and running, it’s time to connect into it using a web browser. Open up a browser on your regular desktop machine and point it at the IP address of your Pi (e.g. http://192.168.0.3:8112).

You’ll be greeted with a password prompt (the default password is ‘deluge’) and be immediately encouraged to change it after you enter it for the first time. After that, you’ll be able to interact with Deluge via the lightweight web interface:

Deluge Web Interface
Deluge Web Interface

Configure Deluge to Run on Startup

The last step in configuring Deluge is to set up Deluge daemon and WebUI to run automatically when the Raspberry Pi boots up.

  1. SSH into the Raspberry Pi and execute the following commands:

sudo wget -O /etc/default/deluge-daemon http://www.howtogeek.com/wp-content/uploads/gg/up/sshot5151a8c86fb85.txt

  1. Edit downloaded startup script:

sudo nano /etc/default/deluge-daemon

Change DELUGED_USER value to the username you used for Deluge daemon to run. After that, save changes and close nano editor.

  1. Make the script executable and update the startup file to include it:

sudo chmod 755 /etc/default/deluge-daemon

sudo update-rc.d deluge-daemon defaults

  1. Add the startup script to Raspbian:

sudo wget -O /etc/init.d/deluge-daemon http://www.howtogeek.com/wp-content/uploads/gg/up/sshot5151aa042ad11.txt

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

sudo update-rc.d deluge-daemon defaults

  1. Restart Raspbian by using the following command:

sudo reboot

After the Raspberry Pi restarts, attempt to connect to Deluge daemon by using either the desktop client or WebUI. If everything went smoothly, you should be able to connect and interact with the Deluge daemon.

References

http://www.htpcguides.com/install-pyload-raspberry-pi-for-download-management

http://www.howtogeek.com/142044/how-to-turn-a-raspberry-pi-into-an-always-on-bittorrent-box

http://www.howtogeek.com/139433/how-to-turn-a-raspberry-pi-into-a-low-power-network-storage-device

Champika Samarasinghe

A Software Engineer currently residing in Colombo, Sri Lanka. Passionate about Software Engineering, Electronics, Mechatronics and Embedded Systems. Enjoys reading, watching movies and playing computer games. You can find me as 'champikasam' in most of the social networks.

Leave a Reply

Your email address will not be published. Required fields are marked *