Running Your Own Application at Startup

From QNAPedia
Jump to: navigation, search

Some advanced uses of your NAS may require you to run custom applications at boot-time. Even though QNAP NAS are Linux-based, you cannot use the usual Linux methods for launching an application at startup: default config files are reset on every startup.

Skills required

  • must be able to remote login via ssh or telnet (e.g. use SSH PuTTY)
  • must know how to edit files using nano, vi, or edit via SFTP (e.g. use WinSCP)

MTD-based method is a script which will be executed on every startup of the TS-x09, TS-x19 and TS-x39. Editing this file allows you to start your own programs or overwrite config files with your own copies.

Manual edit of

  1. Log into your QNAP device using SSH or Telnet, for instance by using Putty
  2. Optional: install nano; use ipkg install nano & edit with nano instead of vi
  3. Mount config ramblock by finding your specific model below:

    Mount the config ramblock /dev/mtdblock4:
    # mount -t ext2 /dev/mtdblock4 /tmp/config

    1 bay: TS-109, TS-109P, TS-110, TS-119,
    2 bay: TS-209, TS-209P, TS-212, TS-219 (TS-219P II: since the new firmware update you maybe have to use ext4 instead of ext2),
    4 bay: TS-409 (Marvell ARM), TS-412, TS-419P:
    Mount the config ramblock /dev/mtdblock5:
    # mount -t ext2 /dev/mtdblock5 /tmp/config

    TS-439, TS-509, TS-639, TS-809, TS-809U (x86):
    Mount the config ramblock /dev/sdx6:
    # mount -t ext2 /dev/sdx6 /tmp/config

    Mount the config ramblock /dev/sdc6:
    # mount -t ext2 /dev/sdc6 /tmp/config
  4. Create/Edit /tmp/config/ .
    1. either using vi:
      # vi /tmp/config/
  1. Get vi editor into edit mode: press a
  2. Edit whatever you need to
      1. Exit edit mode: press ESC
      2. Save and exit: press ZZ
    1. or editing it using a desktop PC and e.g. SFTP
  3. Ensure that /tmp/config/ is executable:
    # chmod +x /tmp/config/
  1. IMPORTANT: Unmount the mounted flash partition:
    # umount /tmp/config script to ease edit

If you edit this file regularly you can save some time by creating a shell script (e.g. to automate the process. You can call the script by either putting it in the environment path, or add its folder to the path or call it by an alias.

The script contents are:

For TS-201 use ...

mount -t ext2 /dev/mtdblock4 /tmp/config
vi /tmp/config/
chmod +x /tmp/config/
echo .
echo "unmounting /tmp/config..."
umount /tmp/config

For TS-109, TS-109P, TS-119, TS-209, TS-209P, TS-219, TS-412, TS-409 (Marvell ARM) use ...

mount -t ext2 /dev/mtdblock5 /tmp/config
vi /tmp/config/
chmod +x /tmp/config/
echo .
echo "unmounting /tmp/config..."
umount /tmp/config

TS-439, TS-509, TS-639, TS-809, TS-809U (x86) use ...

mount -t ext2 /dev/sdx6 /tmp/config
vi /tmp/config/
chmod +x /tmp/config/
echo .
echo "unmounting /tmp/config..."
umount /tmp/config one script to rule them all

Frequently mounting and editing on the flash could be an annoying task. More important, it may reduce the lifetime of some flash blocks. Flash blocks have limited write/erase cycles, and the mtdblock device driver does little to prevent their wear. Read more on this on the Linux mtd web site.

To avoid this, you could configure to launch another script located in the inner drive: in this way there no need to always mount and modify the file inside the flash. but only edit the script file located on your drive.

Create the directory /share/HDA_DATA/.qpkg/autorun and file with:

mkdir /share/HDA_DATA/.qpkg/autorun
cd /share/HDA_DATA/.qpkg/autorun/
chmod +x

The located on the flash could be something like this (just two lines that won't need many changes!):

/share/HDA_DATA/.qpkg/autorun/ &

and then edit the file /share/HDA_DATA/.qpkg/autorun/ to be used to launch all your startup scripts.


1. Never put any larger files on your flashboot devices and ramdisk; instead, create symbolic links to whatever you want to put there, e.g.:
Create a link from /usr (which is in ramdisk) to /share/MD0_DATA/jre1.6.0_10 (which is on a hard disk) at the mountpoint /usr/java

# ln -sf /share/MD0_DATA/jre1.6.0_10 /usr/java

2. Always use the full system path because locations like /opt/bin or /opt/sbin might not have been exported yet when is executed, e.g:

No good.

svnserve -d --listen-port=4000 -r /share/subversion

This is better.

/opt/bin/svnserve -d --listen-port=4000 -r /share/subversion

If it still fails to start svnserve, you might try adding this line to your autoexec script:

/bin/ln -sf /opt/bin/ /share/HDA_DATA/opt/bin/

3. Many startup scripts in /etc/init.d overwrite their corresponding configuration files in /etc. In this case overwriting the config file using is not enough; we also have to overwrite the startup script itself. Moreover, many startup scripts get excecuted before autorun, such that we also have to restart the service. In this case an may look like this:


cp /share/MD0_DATA/admin/nfs /etc/init.d/nfs
cp /share/MD0_DATA/admin/exports /etc

/etc/init.d/nfs restart

Very ugly, indeed! However, it seems this is the only way to make it work (unless you want to throw out the QNAP OS and install a 'better' OS on your NAS).

4. On our QNAP TS-879 Pro we were not able to run

/opt/bin/ start

from the as /opt is not the one from Optware but a directory containing one file, i.e. nasconfig_fs.img.tgz.

Thus we modified /tmp/config/ to

date > $log
# removing bogus /opt
/bin/rm /opt/nasconfig_fs.img.tgz /opt 2>> $log >> $log
/bin/rmdir /opt 2>> $log >> $log
# link correct /opt
/bin/ln -s /share/MD0_DATA/.qpkg/Optware /opt 2>> $log >> $log
# run
/opt/etc/ 2>> $log >> $log

created a log file directory

mkdir -p /opt/var/log

and created /opt/etc/ on the disk

/opt/bin/ start

thus no mounting of the flash partition is necessary anymore.

QPKG-based method

With firmware 3.8.2, the #MTD-based_method was broken. With the next firmware update, this bug was corrected, but in the meanwhile, the below workaround has been devised.

This method consists of declaring a dummy QPKG which launches your script at startup.

  • Log into your QNAP device using SSH or Telnet, for instance by using Putty
  • Edit QPKG config file:
# vi /etc/config/qpkg.conf
  • Declare a new dummy package by adding something like that in this file, but take care about the order. e.g. if you would like to start a service from a optware package, be sure optware is initialized before:
Name = autorun
Version = 0.1
Author = neomilium
Date = 2013-05-06
Shell = /share/MD0_DATA/.qpkg/autorun/
Install_Path = /share/MD0_DATA/.qpkg/autorun
Enable = TRUE

As you can see, Shell is the interesting variable: at boot-time, QNAP OS will launch each QPKG's Shell variable content.

Note: if your NAS doesn't have /share/MD0_DATA (i.e. is a one-drive NAS), put the right directory into the Shell and Install_Path variables and adapt the following commands to your needs.

  • Create the dummy package directory:
# mkdir /share/MD0_DATA/.qpkg/autorun
  • Create the autorun script with the contents of your choice:
# vi /share/MD0_DATA/.qpkg/autorun/

Note: don't forget "#!/bin/sh" at the beginning of script.

  • Set the execute bit:
# chmod +x /share/MD0_DATA/.qpkg/autorun/
  • Reboot and enjoy!

MTD-based method (old)

This section is here only to make sure existing anchor links continue to work. The meat is in the section MTD-based method.

QPKG-based method (new)

This section is here only to make sure existing anchor links continue to work. The meat is in the section QPKG-based method

Trick & tips

Waiting for encrypted partitions

If your data partition is encrypted, you might have some script that has to wait until the encrypted partition is available. I added a script called in my autorun-directory:

#! /bin/sh

# This script ends after the encrypted filesystem has been mounted.

# The following exits successfully (0) if MD0 is mounted
cat /etc/mtab | grep -q MD0
while [[ $? -ne 0 ]] ; do
        sleep 5
        cat /etc/mtab | grep -q MD0

And now I'm able to call scripts *after* the encrypted partition is available, without blocking other scripts:

(./; /etc/init.d/ restart ) &

Calling all scripts in a certain directory

Place a file called in a directory, create a subdirectory called scripts, add to your autorun:

#! /bin/sh

BASEDIR=$(dirname $0)

echo "" > log/userfiles.log

for i in scripts/*.sh ; do
        if [[ -x $i ]] ; then
                echo -n "$i " >> log/userfiles.log
                echo `date` >> log/userfiles.log
                $i 2>&1 >> log/userfiles.log
                cd $BASEDIR

Optimized networking

ifconfig eth0 txqueuelen 50000
ifconfig eth1 txqueuelen 50000
echo 1 > /proc/sys/net/ipv4/tcp_rfc1337
echo 2 > /proc/sys/net/ipv4/tcp_frto
echo 2 > /proc/sys/net/ipv4/tcp_frto_response
echo 1 > /proc/sys/net/ipv4/tcp_mtu_probing
echo 1 > /proc/sys/net/ipv4/tcp_window_scaling
echo 1 > /proc/sys/net/ipv4/tcp_workaround_signed_windows
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 0 > /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_low_latency
echo 1 > /proc/sys/net/ipv4/tcp_ecn