The QNAP NAS (in my case TS-209 II) uses an embedded Linux, during the system startup, a clean runtime environment is restored, then customized configurations are copied over to the default files, this makes a more stable system. After boot, the /etc folder is on a ramdisk (/dev/ram0), thus you can’t modify it as you usually do on a normal Linux.
If you wanna execute some custom scripts during system startup, you can’t simply edit /etc/rc*.d files because these files are on ramdisk. There’re some ways to let system execute your personal scripts during startup, the most dangerous way is to modify the initrd directly, in case you messed up something, your QNAP will be rendered useless unless doing a mtd restore through serial port.
I personally like to use pure software methods to do the trick.
Method 1
Utilize the autorun.sh script. This script is on /dev/mtdblock5, it is called each time system boots, so any modifications to it will take effect in the next reboot. To change its contents:
mount -o loop /dev/mtdblock5 /tmp/config
Then use your favorite editor to edit /tmp/config/autorun.sh, when you’re done, enter:
umount /tmp/config
This method is easy, but you can’t let the script do some cleanup before shutdown because the script is only called during system boot. If you would like some more controls, try the method shown below.
Method 2
QNAP supports QPKG packages, the system checks for installed QPKG applications and call their initializing scripts one by one, by taking control of the config file, we can make QNAP call our script during system boot and before system shutdown.
During system boot, a configuration file /etc/config/qpkg.conf is examined, the Shell item under each section will be converted to symbolic links in /etc/init.d, /etc/rcS.d, as well as /etc/rcK_init.d folders.
To modify /etc/config/qpkg.conf, add a new section (I’ll use MyPackage) to it, then add a Shell item under this section, like this:
[MyPackage]
Shell = /share/MD0_DATA/path_to_my_script/myscript.sh
During reboot, three symbolic links will be created:
/etc/init.d/myscript.sh -> /share/MD0_DATA/path_to_my_script/myscript.sh /etc/rcS.d/QSxxxmyscript.sh -> /share/MD0_DATA/path_to_my_script/myscript.sh /etc/rcK_init.d/QKxxxmyscript.sh -> /share/MD0_DATA/path_to_my_script/myscript.sh
The QSxxxmyscript.sh will be automatically executed during system boot, while the QKxxxmyscript.sh will be executed before system shutdown, this way we can do some specific jobs during system on and off.
