Running Your Own Application at Startup
From QNAPedia
"autorun.sh 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. (The default config files are reset on every startup)"
Contents |
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)
HowTo
Method 1
- Log into your QNAP device using SSH or Telnet, for instance by using Putty
- Optional: install nano; use ipkg install nano & edit with nano instead of vi
- TS-201: Mount the config ramblock /dev/mtdblock4:
# mount -t ext2 /dev/mtdblock4 /tmp/config
- TS-109, TS-109P, TS-110, TS-119, TS-209, TS-209P, TS-219, TS-409 (Marvell ARM), 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
- Create/Edit /tmp/config/autorun.sh .
- either using vi:
# vi /tmp/config/autorun.sh
- Get vi editor into edit mode: press a
- Edit whatever you need to
- Exit edit mode: press ESC
- Save and exit: press ZZ
- or editing it using a desktop PC and e.g. SFTP
- either using vi:
- Ensure that /tmp/config/autorun.sh is executable:
# chmod +x /tmp/config/autorun.sh
- IMPORTANT: Unmount the mounted flash partition:
# umount /tmp/config
Method 2
If you edit this file regularly you can save some time by creating a shell script (e.g. editautorun.sh) 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/autorun.sh chmod +x /tmp/config/autorun.sh echo . echo "unmounting /tmp/config..." umount /tmp/config
For TS-109, TS-109P, TS-119, TS-209, TS-209P, TS-219, TS-409 (Marvell ARM) use ...
mount -t ext2 /dev/mtdblock5 /tmp/config vi /tmp/config/autorun.sh chmod +x /tmp/config/autorun.sh 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/autorun.sh chmod +x /tmp/config/autorun.sh echo . echo "unmounting /tmp/config..." umount /tmp/config
Method 3
Always mounting and editing the autorun.sh 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 mtdblock device driver does little to prevent their wear. Read more on this on the linux mtd website.
To avoid this, you could configure autorun.sh 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 autorun.sh with:
mkdir /share/HDA_DATA/.qpkg/autorun cd /share/HDA_DATA/.qpkg/autorun/ touch autorun.sh chmod +x autorun.sh
The autorun.sh located on the flash could be something like this (just two lines that won't need many changes!):
#!/bin/sh /share/HDA_DATA/.qpkg/autorun/autorun.sh &
and then edit the file /share/HDA_DATA/.qpkg/autorun/autorun.sh to be used to launch all your startup script.
IMPORTANT Notes!
1. Never put any larger files on your flashboot devices and ramdisk, instead create symbolic links to whever you want to put there i.e: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 the Autorun.sh is executed. important! eg:
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 starting svnserver, 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 autorun.sh 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 autorun.sh may look like this
#!/bin/sh 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 where not able to run
/opt/bin/rsyncd-acl.sh start
from the autorun.sh 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/autorun.sh to
#!/bin/sh log=/share/MD0_DATA/.qpkg/Optware/var/log/autorun 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 autorun.sh /opt/etc/autorun.sh 2>> $log >> $log
created a log file directory
mkdir -p /opt/var/log
and created /opt/etc/autorun.sh on the disk
#!/bin/sh /opt/bin/rsyncd-acl.sh start
thus no mounting of the flash partition is necessary anymore.
Sample scripts
Optimized networking
- You can also try different values from SpeedGuide.net
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