Posted on Sun 10/26/08 in FreeBSD
How to get BTG up and running on FreeBSD and start as a daemon at boot time.
The world of torrent daemons is expanding and there are numerous options nowadays. While some of these daemons are available in FreeBSD ports, my favourite torrent daemon BTG is not and has to be compiled manually.
The official site does offer a howto, but does not tell us how to start the daemon at boot time. To be thorough, I will list the whole process in this article.
First of all we need all the libraries BTG uses. You can install them using pkg_add or compile them, whatever suits you best. I used a combination since I wanted to set some config options for PHP. Install these ports / packages:
Make sure you set apache2_enable="YES" in your rc.conf so apache starts when starting the computer. To start apache immediately, execute /usr/local/etc/rc.d/apache2 start.
After installing all these packages it is time to set up the big one, BTG itself. At the time of writing the stable version is 0.9.8, with 0.9.9-RC1 being the unstable release. To get the most recent version, check the builds and releases on the official site.
Grab yourself a release and untar it somewhere. Then go into the untarred directory and start up autogen (make sure you are in the bash shell for this!):
./autogen.sh
After autogen is complete, we will configure the program. I will configure BTG with a web UI, session support, cURL support and CLI support. Also, some BSD specific code has to be added.
./configure --enable-www --enable-cli --enable-url \
--enable-session-saving \
LIBTORRENT_LIBS="-L/usr/local/lib -ltorrent" \
LIBTORRENT_CFLAGS="-DTORRENT_USE_OPENSSL -D_THREAD_SAFE -pthread
-I/usr/local/include -I/usr/local/include/libtorrent" \
LIBS="-lpthread"
This code differs slightly from what the official site (currently) tells us to use. The official instructions did not work for me. However, should the above configure fail for you, replace LIBTORRENT_LIBS="-L/usr/local/lib -ltorrent" with LIBTORRENT_LIBS="-L/usr/local/lib -ltorrent-rasterbar".
Dec 9th '08: I found out the -ltorrent-rasterbar is required with rasterbar libtorrent 0.14 and up, while -ltorrent should be used with lower versions! Good to know ;-)
After configuration do the usual make and make install.
It's time to configure our new torrent daemon! I could post a link to the official howto, but I want this article to be complete so I list the instructions here as well. BTG has a btg-config tool (which needs to run in bash and requires cdialog to be installed) which helps you set up your configuration, but I have not tested this on BSD. It's not needed either, since configuration is easy.
I wanted BTG to run under my own account, so I made a .btg directory in my home. In this directory, place the default daemon.ini file and change file paths and torrent ports where needed. Do the same for the client.ini.
Now we have to determine where BTG does its magic on the file system. On my server, I have a mounted disk for my downloads, which is mounted on /merry/zoro (yes, my network is One Piece themed ;-)). I made the following directory structure:
/merry/zoro/torrent/
/merry/zoro/torrent/finished/
/merry/zoro/torrent/seed/
/merry/zoro/torrent/torrents/
/merry/zoro/torrent/work/
Now we add a user account to BTG and specify the directory structure we just made. By doing this, BTG knows where to write files. If you provide non-existing directories, BTG will refuse to set up a torrent session.
btgpasswd --create -a -t "/merry/zoro/torrent/torrents" \
-w "/merry/zoro/torrent/work" \
-d "/merry/zoro/torrent/finished" \
-s "/merry/zoro/torrent/seed" -p -u <DESIRED_USERNAME>
Now we have a user ready to log on to BTG. Time for the daemon itself.
Starting BTG is as easy as typing btgdaemon. By providing the daemon with some more flags we can set it up to our likings. Type btgdaemon --help to see all available flags.
By default there is no init script for BTG, so I had to cook up my own. Being quite new to BSD and its init system, my script may be somewhat of a hack, but it works for me :-)
I wanted the BTG daemon to run under a specific user account (my own, in this case) and start at boot time. To stop the daemon I'd have to know its pid, so I used the -p flag to specify a pidfile I could cat in order to get this pid. Here's the init script I cooked up (with added comments):
#!/bin/sh
# This part is needed for proper startup at boot time and proper shutdown
# PROVIDE: btg
# REQUIRE: DEAMON
# KEYWORD: shutdown
. /etc/rc.subr
# Set daemon name and the user running the btgdaemon
name="btg"
rcvar=`set_rcvar`
user="jurrie"
# pidfile, your ${user} must be able to write this file!
btg_pidfile="/var/run/${name}.pid"
# Location of the BTG configuration
configfile="/usr/home/jurrie/.btg/daemon.ini"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
# Command to execute when starting the daemon
btg_start()
{
su -l ${user} -c "/usr/local/bin/btgdaemon -p ${btg_pidfile} -c ${configfile}"
}
# Stopping the daemon (a bit of a hack)
btg_stop()
{
kill -9 `cat ${btg_pidfile}`
}
load_rc_config ${name}
run_rc_command "$1"
Get the script file here. Make sure to set the executable bit!
To complete the configuration, set btg_enable="YES" in your rc.conf and start the daemon by running /usr/local/etc/rc.d/btg start.
I don't really have any conclusions, just felt like it should be here to end the article ;-) I hope this article will be able to help someone and if you see any faults, please report them!
Is there a specific reason why BTG is your favorite? How does it differ from rtorrent, besides the gui options?
— x140x1n · Nov 11, 12:54 PM · #
I must admit I have never used rTorrent. I went from Torrentflux to BTG on my Linux server.
I did read some posts from people who disliked rTorrent and said libtorrent from Rasterbar (used by BTG) is superior. I’ll give rTorrent a shot when I find some time and may post results later :-)
rTorrent also uses libtorrent. I just finished setting up a ‘leech’ server with freebsd and rtorrent. works great, escpecially when you do some modifications on the source before compiling :-)
— x140x1n · Nov 11, 04:56 PM · #
Oh really? Then again, the post I read about the libtorrent thing dated back to january ’07 I think :-)
I’m not competent enough to edit the actual source code, but being clueless yet intrigued, I will undoubtedly take a shot at rTorrent!
Rasterbar libtorrent and libtorrent used with rtorrent are two seperate and different libaries.RasterBar Libtorrent is normally called “rb-libtorrent” while the rtorren variation is “libtorrent”
By far the RasterBar LibTorrent is superior in every fashion.