Part 1: Setting up rtorrent on LEDE/OpenWrt

rtorrent running as an unprivileged user inside a tmux session.
rtorrent running within a tmux session

This first part deals with getting rtorrent to work optimally on a low-end device like a router. Our goals:

  • rtorrent should only start if external storage is attached.
  • rtorrent should run under normal privileges (ie non-root user).
  • rtorrent should auto-start upon system boot up, and always restart after reboots.

Lets start by setting up our storage, I observed that using the ext4 filesystem is less taxing on the system compared to NTFS (ntfs-3g). Since we aim to download a lot of data using rtorrent, we can’t afford to lose performance over filesystem/IO operations.

We will install a tool to format the drive (you can skip this step if you have a Linux desktop/LiveCD which you could use to prepare the drive, then simply attach it to the router).

Assuming you want to do everything with the router itself, and you have no data on the drive/wish to wipe it clean, ssh into your router and:

opkg update
opkg install fdisk block-mount e2fsprogs kmod-fs-ext4 kmod-usb-storage

Plug your USB drive to the router and lets see what we got:

block info
ls /dev/sd*

Either of these two should give you information about your connected drive, the output of the second command will look something like:
/dev/sda /dev/sda1.
This tells us our drive is at /dev/sda (and /dev/sda1 is the single partition currently available on it). Next we delete this partition and create an ext4 partition for use by the router. This will destroy all current data on the drive. To delete/create partitions we’ll use fdisk:

fdisk /dev/sda

I will paste the output of fdisk as I was using it on the router, my input is bold:

root@LEDE:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.32).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1,2, default 2): <press enter>

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): <press enter>
Partition number (1-4, default 1): <press enter>
First sector (2048-30375935, default 2048): <press enter>
Last sector, +sectors or (.. default 30375935): <press enter>
Created a new partition 1 of type 'Linux' and of size 14.5 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks

I hope that was self-explanatory, we deleted (d) the existing partitions, and created (n) a new primary partition of type Linux, and finally wrote (w) changes to disk.

Some might wonder if it might have been a good idea to create a swap partition, if your router has > 256MB RAM then it will be good enough, performance that requires a swap partition will be beyond the router’s CPU anyway.

And now we format the newly created partition using ext4:

mkfs.ext4 /dev/sda1

Wait for it to finish and we’re done with prepping our disk. Next we wish to auto-mount the disk upon reboots:

block detect > /etc/config/fstab
uci set fstab.@mount[-1].enabled='1'
uci set fstab.@mount[-1].target='/mnt/storage'
uci commit fstab

We first generated a new fstab file which will contain info about our disk, we enable auto-mounting and finally set the mount point to /mnt/storage. Now if you issue “block mount”, or simply reboot your router, /mnt/storage should be available.

With storage out of the way, we need to setup the environment for running rtorrent. Lets install some tools:

opkg update && opkg install tmux shadow-useradd sudo

tmux is a terminal multiplexer, it allows us to run a CLI program and “minimize” it while its still running, we need this because we will keep rtorrent running in the background and later use the WebUI to control it.
shadow-useradd allows us to add a new, non-root, user. sudo allows us to execute commands as another user. The big picture will become clear as we go along:

useradd -d /mnt/storage rtorrent

We created a new user, named rtorrent, and set its home directory to /mnt/storage.

We can finally start setting up rtorrent itself! Let me paste my rtorrent.rc (the config file for rtorrent):

# rtorrent.rc. Good for a router with 256MB RAM

# min and max peers per torrent
min_peers = 15
max_peers = 30

# max concurrent uploads
max_uploads = 10
max_uploads_global = 30

# upload and download rates
download_rate = 0
upload_rate = 5120

# max open files
network.max_open_files.set = 256

# ratio, when reached close torrent

# system.method.set = group.seeding.ratio.command, d.close=

# Enable encryption, this will limit the number of users that
# can connect to you, comment this out to connect to more users
# but your torrent traffic will be unencrypted.
encryption = require,allow_incoming,require_RC4

# dir to save downloads
directory = /mnt/storage/torrent_downloads/

session = /mnt/storage/torrent_downloads/.session

# watch dir, auto download new .torrent files placed into this dir
schedule = watch_directory,10,10,load.start=/mnt/storage/torfiles/*.torrent

# ports to use, these need to be opened on the firewall
port_range = 30305-30307
port_random = yes

# check hash of completed downloads, set to no if single core SoC
check_hash = yes

# dht
dht = auto
dht_port = 30308

# connect to UDP trackers
# use_udp_tracekrs = yes

# scgi_local is used for communication with the webUI.
# if you plan to use rtorrent in CLI only, delete this line.
scgi_local = /tmp/rtorrent.sock

Now explaining all that is beyond the scope of this walk-through. But I commented in the config file itself to hopefully give you a general idea. The contents of the file above need to be placed in /mnt/storage/.rtorrent.rc.

You can either use WinSCP (on Windows) or scp on Linux to copy this file to your router. Alternatively, you can pull a copy from our website directly into your router, but you will need to enable SSL by installing a couple of additional apps onto your router:

opkg update && opkg install libustream-mbedtls ca-bundle
cd /mnt/storage
mv rtorrent.rc .rtorrent.rc

In the last command above, we renamed (moved) rtorrent.rc to .rtorrent.rc (the default config filename that rtorrent looks for).
While we are still in /mnt/storage, lets create a couple of directories which rtorrent will be expecting:

cd /mnt/storage
mkdir torrent_downloads
mkdir torrent_downloads/.session
chown -R rtorrent /mnt/storage

torrent_downloads is where rtorrent will download all the files, and .session is required to store run time files. We finally changed ownership of entire storage directory to rtorrent (the user we created earlier).

Install rtorrent

opkg install rtorrent-rpc

With rtorrent installed, our storage ready, config file in place, we can test run rtorrent:

sudo -u rtorrent rtorrent

If you get an error like: Error opening terminal: rxvt-unicode-256color. Run this:

export TERM=xterm

Then try running it again (sudo -u rtorrent rtorrent). If you see something similar to the picture at the top of this post, then great, we finished setting up rtorrent.
You can exit the program by hitting Ctrl+Q. Now let me show you the command we will use to run rtorrent at startup and keep it running in background:

tmux new-session -d -s rtorrent 'sudo -u rtorrent -S rtorrent -n -o import=/mnt/storage/.rtorrent.rc' &

The first part of the command: tmux new-session -d -s rtorrent creates a new tmux session, we want this session to be (-d) detached, why detached? Because later we will put this in a script to auto-start at system startup, so tmux needs to start in a “minimized/detached” mode. (-s) is used to name the tmux session (think of it like a tab name), we named it rtorrent.

What follows, the command between the single quotes:
‘sudo -u rtorrent -S rtorrent -n -o import=/mnt/storage/.rtorrent.rc’
is what will be executed within the tmux session. We want this to be executed as user (-u) rtorrent.
Finally, while rtorrent will look for the .rtorrent.rc file by default, we still explicitly specify it just to be sure 🙂

If you run the command, and all went well, you will have a detached rtorrent instance running. To reattach to it, simply run:

tmux at -t rtorrent

We wish to at(tach) to session named rtorrent. And now you should see the green bar at the bottom telling us the program we are using is running inside tmux. To detach (minimize), simply: Ctrl+b then d.
You can look up tmux and how to use it, but in the context of using rtorrent, all you’ll really need is:

In terminal:
tmux at -t rtorrent (attach to our session).

Within tmux itself:
Ctrl+b followed by d (detach from our session).

Now what if you detach from your tmux session, keep rtorrent running in the background, but then you restart your router? rtorrent will not come back up.
We want it to auto-start. For that we will use this simple script:

# rtorrent starter script

# If config file doesn't exist then don't start
sleep 10
if [ -f "$CONFIG" ]
	rm /tmp/rtorrent.sock
	sleep 2
	tmux new-session -d -s rtorrent 'sudo -u rtorrent -S rtorrent -n -o import=/mnt/storage/.rtorrent.rc' &
	logger "No storage mounted, not starting rtorrent.."

exit 0

Here’s a copy on our website (you can copy this directly to router with wget like we did earlier).
The script checks whether a configuration file for rtorrent exists (which we stored on external storage), if it finds one then that means storage is mounted, config ready, and all is good to go.. so it runs rtorrent. We need to make the script executable:

chmod +x /etc/

You can place the script in /etc or even /mnt/storage. Add one line to /etc/rc.local to run it on system startup, so if I put in /etc, I would add this line to /etc/rc.local:

# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

/etc/ &

exit 0

We finished Part 1, as you can probably see, you have a fully functional torrent client now, and its very light on the system.
If you want to add torrents or test your setup, check the rtorrent CLI user guide at the author’s github. Part 2 will deal with setting up the environment for ruTorrent (the webUI).

Leave a Reply