acb's technical journal

Installing BitTorrent Sync on a QNAP NAS

Recently I have been looking for a way to synchronise files between several machines, including my (OSX) laptop and a Linux-based network attached storage appliance. After looking at various alternatives, I settled on BitTorrent Sync, the file synchronisation system based on the BitTorrent file transfer protocol. This essentially works much like Dropbox (in that it automatically synchronises the contents of one or more directories between computers), only without there being a centralised server accessing or storing the data; synchronisation is done directly between peers, and the files are encrypted end to end.

One drawback of Dropbox (notwithstanding the still open question of privacy; can one prove that they're not compelled by secret directive to funnel everyone's documents to the NSA as they're uploaded, just in case?) has been the paucity of clients for more unusual systems, such as, for example, the NAS I mentioned. While it's fine for synchronising PCs, Macs and x86-based Linux boxes (their Linux client, whilst preferring a GUI, can be coaxed into working from the command line), it does not cover the new generation of lightweight servers and storage devices, which are often based on ARM CPUs. As I have such a server (a QNAP TS-412), I have been looking for a way to keep some files synchronised between it and other machines, including a MacBook running OSX. BitTorrent Sync seems to work quite nicely, though setting it up on the NAS box can require some thought.

First, I installed BitTorrent Sync on my MacBook, which was a straightforward operation, much like any other application. Running the app prompted me to create a folder, with a random secret to be used for synchronising it with other machines. (BitTorrent Sync, as far as I understand it, uses a hash of the secret for finding shares in a central repository and the secret for accessing them from the serving machine itself.) Next, I downloaded the ARM Linux binary, which has been designed partly for installation on any ARM-based NAS one can get a shell on. (One could also run it on a device such as a Raspberry Pi or a CubieBoard, assuming that one runs a Linux distribution on that.)

Aside: concerning the QNAP TS-412

The TS-412 is a network attached storage appliance, or essentially a small ARM-based Linux box with four removable hard drive bays, which, out of the box, is configured to act as a storage appliance. However, unlike some other units (such as the Iomega StorCenter IX2 I used previously, which, whilst jailbreakable, was not designed with hackability in mind), there is an officially sanctioned means of installing user packages, enable a login shell and using the device as a general-purpose Linux box (albeit one of limited CPU power). QNAP have a packaging system named QPKG, which is mostly used for installing monolithic packaged applications (such as various kinds of servers); one QPKG package, however, is another packaging system named ipkg, which is more similar in philosophy to Debian's APT. Once you install ipkg, you can log into the NAS, run processes and install software (both from ipkg and elsewhere). The range of packages ipkg gives you is broad, including most of the things you'd find in a mainstream Linux distribution.

Installing btsync on the TS-412

BitTorrent conveniently provide an ARM Linux binary of btsync. Unusually for precompiled Linux syncing clients, it does not assume that the user is running a desktop Ubuntu machine and wants a nice graphical interface, which would have made it less than useful on NAS boxes. btsync thoughtfully eschews a traditional GUI altogether, but instead contains a web server and a web-based interface which runs on port 8888.

To play nice with the QNAP system, we make a virtual QPKG package for btsync and put its files there; from now on, all files related to btsync (though not, of course, the folders it synchronises) will live in a subdirectory of the QPKG working directory, which is at /share/MD0_DATA/.qpkg/:

mkdir /share/MD0_DATA/.qpkg/btsync
cd /share/MD0_data/.qpkg/btsync
tar zxvf /path/to/download/btsync_arm.tar.gz

Running btsync from here will start the daemon and create a .sync directory it uses to store its state. The daemon will be running as whichever user you start it as, and any files it creates will be owned by that user; you probably don't want this to be the case, so you will need to make it run as your personal user ID. You can achieve this by changing the executable's ownership and setting the setuid bit:

chmod 4775 /share/MD0_DATA/.qpkg/btsync/btsync
chown yourusername /share/MD0_DATA/.qpkg/btsync/btsync

We can then start btsync and, with a web browser, go to port 8888 on the NAS device, and add shared folders through the web UI. Another option is to configure directories entirely through the configuration file. Whichever option we use, it is recommended to create a configuration file; without one, the web interface has no password, which could be a problem if anyone who isn't trusted might have access to the local network. So we create one, with:

cd /share/MD0_DATA/.qpkg/btsync/
./btsync --dump-sample-config >btsync.conf

We edit this file, setting a login and password under webui, so parts of it look like:

  "webui" :
    "listen" : "",
    "login" : "admin",
    "password" : "topsecret123"

From then on, we can launch btsync with --config btsync.conf.

Making btsync start automatically on boot

We want btsync to start automatically whenever the NAS reboots. On a normal UNIX system, we'd put a script in /etc/init.d, though this won't work on the TS-412. According to this QNAP wiki page, there is a recommended way of adding automatically starting applications, by creating a virtual QPKG package entry; we do this by adding something like the following lines to /etc/config/qpkg.conf:

Name = btsync
Version = 0.1
Author = local
Date = 2013-08-10
Shell = /share/MD0_DATA/.qpkg/btsync/
Install_Path = /share/MD0_DATA/.qpkg/btsync
Enable = TRUE

This file will run a script named on startup; this script should behave like an init.d script, i.e., accepting one argument, which is start, stop or restart, as an operation to perform. Our script, therefore, will look like:




case "$1" in
    $BTSYNC --config ${BTSYNCDIR}/btsync.conf
    kill `cat $BTSYNCDIR/.sync/`
    $0 stop
    $0 start
    echo "Usage: $0 {start|stop|restart}"
    exit 1

We make this executable (chmod +x /share/MD0_DATA/.qpkg/btsync/ ), and from then on, can start the server using start. Finally, we reboot the NAS and log in to check that btsync has started:

[~] # ps ax |grep btsync
 8756 yourusername 6556 S   /share/MD0_DATA/.qpkg/btsync/btsync --config /share/M
 8871 admin        516 R   grep btsync

There are 1 comments on "Installing BitTorrent Sync on a QNAP NAS":

Posted by: nobody

Thu Aug 29 04:24:25 2013

QNAP has release BitTorrent Sync QPKG, check the QNAP wiki here,

Want to say something? Do so here.

Post pseudonymously

Display name:
To prove that you are not a bot,
please enter the text in the image on the right
in the field below it.

Your Comment:

Please keep comments on topic and to the point. Inappropriate comments may be deleted.

Note that markup is stripped from comments; URLs will be automatically converted into links.