How to enable custom startup scripts

MD-253 & MD-500 Discussion
Post Reply
Protheus
Posts: 21
Joined: Sun May 30, 2010 11:18 am

How to enable custom startup scripts

Post by Protheus » Sun Sep 05, 2010 4:57 pm

Hi all,

I wrote a guide, how to include a custom startup script, that will enable you to do everything you like on system startup.

Unfortunately the normal startup script is in /etc/init.d/rc.sysinit - which is in a nonwritable area. Everything you change in here is lost after a reboot.

I used the following way:

change the /etc/init.d/rc.sysinit script to include a start of a custom script /etc/sysconfig/config/custom/rc.local

This change of rc.sysinit has to be done in the flashfile of the bootfs and written back to the flash.

So please keep in mind, that this is a high risk process, where you can loose your complete NAS.

The complete guide is in the wiki:

http://sitecom.nas-central.org/wiki/Ena ... rtupScript

Cheers

seansan
Posts: 19
Joined: Wed Apr 07, 2010 10:25 am

Re: How to enable custom startup scripts

Post by seansan » Sat Mar 05, 2011 11:18 am

Can we move the knowledge to custom FW, then we only have to do it oncce

viewtopic.php?f=235&t=2677

nanocool
Posts: 12
Joined: Tue Feb 15, 2011 12:01 am

Re: How to enable custom startup scripts

Post by nanocool » Sat Mar 12, 2011 1:55 am

NOTE: Made the changes on firmware SitecomNas_v2.3.33
NOTE: This script will only be readed when u reboot your NAS.
NOTE: After an change and give the command for reboot, occasionally i needed to restart my NAS manually.
NOTE: New to vi text editor! Please google and learn the basics.

The following is working to me, but as always be careful when accessing your device through SSH when u don't have much experience.

In Short:
We are going to remove an symlink (/etc/sysconfig/config/twonky) to twonkymedia.sh that´s is pointing in /etc/init.d/rc.sysinit (Startup script).
In the writable area for where we removed the symlink, we place our own script that replaces the removes symlink. In our script we can add NEW commands or startup other scripts or make changes to the write protected area (changes to write protected get lost after an reboot).

The things u can do is preventing hard-drives to spin down (for severals reasons I don´t think it´s a good idea to let it spin down, for sure it wont extend the drives lifetime), make sure Twonky won´t start, alter the fan speed, make changes to the webgui (like add an link to start or stop Twonky, i did).
These changes are semi persistent changes. I say semi, because when u remove this script, all changes that is been made to the write protected files will be lost after an reboot.


Making the changes:

1) Locate the symlink
cd /etc/sysconfig/config/twonky
ls -al
lrwxrwxrwx 1 root root 43 Jan 1 1970 twonkymedia.sh -> /etc/sysconfig/system-script/twonkymedia.sh


2) destroy existing symlink
rm twonkymedia.sh


3) create the small script:
vi twonkymedia.sh
(entering the vi editor)


#!/bin/sh
/usr/bin/hdparm -B255 /dev/sda
/usr/bin/hdparm -B255 /dev/sdb
/usr/bin/hdparm -S0 /dev/sda
/usr/bin/hdparm -S0 /dev/sdb
/etc/sysconfig/system-script/twonkymedia.sh stop
/usr/bin/echo "Just stopped Twonky"
stop
# Just add more lines when u feel like, don't forget, include the complete path with command while scripting!


(save file & quit vi)

chmod 777 twonkymedia.sh



4) restore symlink, loose the benefits...
cd /etc/sysconfig/config/twonky
rm twonkymedia.sh
ln -s /etc/sysconfig/system-script/twonkymedia.sh twonkymedia.sh



5) Check if Thwonky is disabled after your test reboot
/etc/sysconfig/system-script/twonkymedia.sh status

=====================
(checking if twonky is down by CLI, you also can see the status in the WebGUI'

/etc/sysconfig/config/twonky # /etc/sysconfig/system-script/twonkymedia.sh status
ps: unrecognized option `--no-headers'
BusyBox v1.10.3 (2009-06-11 11:13:48 CST) multi-call binary

Usage: ps

Report process status

Options:
-o col1,col2=header Select columns for display

No twonky server is running

=====================
=====================
explanation why this change will work.
=====================
=====================

After you boot the NAS, *nix starts up, the following script will started as first:

/etc/init.d/rc.sysinit

You can't change this script, because it residence in a non writable area.
When reading it, i learned the following.

Somewhere within the script it starts the following:

service_mount_hdd

This script activates the hard-drive spin-down. When reading the script rc.sysinit, there wont be another scrip after this that execute the hdparm command.
/bin/hdparm -S241 /dev/sdb >/dev/null 2>&1


The last line of rc.sysinit is: and it wont execute other scripts anymore. Everything that has be made, can done be unmade now by us :)
/usr/local/TwonkyVision/twonkymedia.sh start


Following the path to twonkymedia.sh, Hey, /usr/loacal/ that residence within the writable area...
Watch out, /usr/local/TwonkyVision/ its a symlink

TwonkyVision -> /etc/sysconfig/config/twonky

but it redirects again to an writeable area, nice.
There u have it /etc/sysconfig/config/twonky/twonkymedia.sh, but again a symlink... to a non write-able area.

Ok, here we have to remove the symlink. Place in the folder /etc/sysconfig/config/twonky a new file called twonkymedia.sh, within that file we can do what we want to do... like stopping twonky forever :D and stopping spin-down, and other things...


=====================
=====================
Enable / Disable Twonky by WEBGui
This can only work when u made the changes that has been described as before!!
An example how to add an link in your WebGui (on your status TAB) to start and stop Twonky.
I am very bad at HTML, so it will look very simple.

=====================
=====================

Create the following folder
mkdir /home/.system/

Create the following symlink
/usr/bin/ln -s /home/.system/ /var/www/cgi-bin/custom-scripts/

---------------- add line to script ------------------------------------
Location script:
cd /etc/sysconfig/config/twonky/twonkymedia.sh
Purpose, these values are needed to made it persistent after an reboot
------------ Start adding lines ----------------------------------------------------

/usr/bin/echo "<verbatim> Stop and Start Twonky </verbatim><br><a href="/cgi-bin/custom-scripts/twonky-start.sh">Start Twonky</a> &nbsp;&nbsp;&nbsp; <a href="/cgi-bin/custom-scripts/twonky-stop.sh">Stop Twonky</a></br>" >> /var/www/status.htm


------------ End adding lines -----------------------------------------------------

Now u have to create two new scripts.
Create and add the following scripts in:

cd /home/.system/

Make script (nr 1) twonky start:
vi /home/.system/twonky-start.sh


Copy the following lines in vi

#!/bin/sh
/etc/sysconfig/system-script/twonkymedia.sh start >/dev/null 2>&1 &
/usr/bin/echo "Content-type: text/html"
/usr/bin/echo ""
/usr/bin/echo "<html><head><title>Demo shell script</title></head><body>"
/usr/bin/echo "Twonky Service is been started"
/usr/bin/echo "</body></html>"


and save and close


Make script (nr 2) twonky stop:
vi /home/.system/twonky-stop.sh


Copy the following lines in vi

#!/bin/sh
/etc/sysconfig/system-script/twonkymedia.sh stop >/dev/null 2>&1 &
/usr/bin/echo "Content-type: text/html"
/usr/bin/echo ""
/usr/bin/echo "<html><head><title>Demo shell script</title></head><body>"
/usr/bin/echo "Twonky service is been stopped"
/usr/bin/echo "</body></html>"


and save and close

=====================
Good luck and have fun.
edit: Made some changes to be more clear.

Pepsi
Posts: 4
Joined: Sun Jul 03, 2011 10:10 pm

Re: How to enable custom startup scripts

Post by Pepsi » Mon Jul 04, 2011 5:42 am

@nanocool

may i say great post.

I had some minnor changes to get it to work.

the symbolic link has to be in the replaced twonky script [ /etc/sysconfig/config/twonky/twonkymedia.sh ] but you have to remove the last /
/usr/bin/ln -s /home/.system/ /var/www/cgi-bin/custom-scripts

the command cd /etc/sysconfig/config/twonky/twonkymedia.sh can't prformed because it is a start script not a directory

also done a chmod 777 to /home/.system/twonky-start.sh and /home/.system/twonky-stop.sh

I also edit /etc/sysconfig/config/twonky/twonkymedia.sh and edit this line
/usr/bin/echo "<verbatim> Stop and Start Twonky </verbatim><br><a href="/cgi-bin/custom-scripts/twonky-start.sh">Start Twonky</a> &nbsp;&nbsp;&nbsp; <a href="/cgi-bin/custom-scripts/twonky-stop.sh">Stop Twonky</a></br>" >> /var/www/status.htm

after a reboot indeed this part is added to status.htm
<verbatim> Stop and Start Twonky </verbatim><br><a href="/cgi-bin/custom-scripts/twonky-start.sh">Start Twonky</a> &nbsp;&nbsp;&nbsp; <a href="/cgi-bin/custom-scripts/twonky-stop.sh">Stop Twonky</a></br>

but there is noting in the web interface.
what am i doing wrong?

Mijzelf
Posts: 6226
Joined: Mon Jun 16, 2008 10:45 am

Re: How to enable custom startup scripts

Post by Mijzelf » Mon Jul 04, 2011 7:54 am

Your adding is outside the <body> tags, so it's ignored by the browser. When the close tag has it's own line, you could strip it away using grep:

Code: Select all

mv /var/www/status.htm /var/www/status.bak
cat /var/www/status.bak | grep -v \</body\> >/var/www/status.htm
rm /var/www/status.bak

/usr/bin/echo "<verbatim> Stop and Start Twonky </verbatim><br><a href="/cgi-bin/custom-scripts/twonky-start.sh">Start Twonky</a> &nbsp;&nbsp;&nbsp; <a href="/cgi-bin/custom-scripts/twonky-stop.sh">Stop Twonky</a></br></body>" >> /var/www/status.htm
When it doesn't have it's own line, you can strip it away using sed, but I don't know the syntax.

Pepsi
Posts: 4
Joined: Sun Jul 03, 2011 10:10 pm

Re: How to enable custom startup scripts

Post by Pepsi » Mon Jul 04, 2011 4:51 pm

thanks for the quick response (dank voor de snelle reactie)

I've to remove more then just the body tag.
But you gave me an pointer where to look

this is at the end of status.htm

Code: Select all

 </body>
</html>
<script type="text/javascript">
GetStatusInfo();
</script>
the advantage is that status.htm is in the read only memory. so everything that i do wrong is gone after a reboot :lol:

Pepsi
Posts: 4
Joined: Sun Jul 03, 2011 10:10 pm

Re: How to enable custom startup scripts

Post by Pepsi » Thu Jul 07, 2011 5:26 pm

grrrr. in IE it worked perfect. but in Firefox not... it was the browser cache that was the problem.

i updated the status.htm to show the start and stop function like it was build in.
as you know i'm not a html expert. I had hoped to read the status of the UPnP service to show only start or stop. but maybe in the future.

I'm just looking/making a script to split status.htm, add the new lines and merg it back together.

And if i've time left maybe i make a script to automate everything (a md253 out of the box and after 3 min it is altert to a quite box) without linux script knowledge

Pepsi
Posts: 4
Joined: Sun Jul 03, 2011 10:10 pm

Re: How to enable custom startup scripts

Post by Pepsi » Thu Jul 07, 2011 8:19 pm

yes finaly the script works
it is still work in progress. but the result for now

Image

monzo
Posts: 1
Joined: Mon Dec 05, 2011 3:03 pm

Re: How to enable custom startup scripts

Post by monzo » Fri Dec 09, 2011 2:44 pm

Pepsi wrote:yes finaly the script works
it is still work in progress. but the result for now
Nice work pepsi! Did you complete the scripts? How did you reach above result?

Thanks!

Post Reply