How startup and shutdown work incl. optware [solved]

Questions about IPKG can go here!
Forum rules
Please note the disclaimer before modifying your Synology Product.
khaki54
Trainee
Trainee
Posts: 11
Joined: Sun Apr 29, 2012 8:39 pm

How startup and shutdown work incl. optware [solved]

Postby khaki54 » Fri May 04, 2012 11:45 pm

So after installing ipkg and a couple packages (pound, named) I noticed that my DSM 4.0 NAS wouldn't shut down gracefully.

After much work I have it figured out. This info is for DSM 4.0 and 5.0, but probably works for the previous release.

On startup, scripts run in this order:
1. /usr/syno/etc/rc.d/S[00->99]scriptname.sh start
These are all of the Synology Scripts. Unless you have some compelling reason, do not put any init scripts in here. That defeats the purpose of the whole optware concept. They have theirs and you have yours.

2. /etc/rc.optware start (executed by rc.local)
This script mounts your /opt with your /volume1/@optware
Then by default it calls /opt/etc/rc.optware start

3. /opt/etc/rc.optware start (executed by /etc/rc.optware)
This executes a /opt/etc/init.d/S[00->99]scriptname.sh start

4. /opt/etc/init.d/S[00->99]scriptname.sh start (executed by /opt/etc/rc.optware)
This is where optware packages will install their startup scripts. This is the correct place.

5. /usr/local/etc/rc.d/scriptname.sh start (executed by unknown syno script)
This is an important startup directory. You are allowed to put scripts in here.

On shutdown, scripts run in this order:
1. /usr/local/etc/rc.d/scriptname.sh stop (executed by unknown syno script)
This is an important shutdown directory. You are allowed to put scripts in here.

2. /usr/syno/etc/rc.d/S[00->99]scriptname.sh stop
These are all of the Synology Scripts. Unless you have some compelling reason, do not put any init scripts in here. That defeats the purpose of the whole optware concept. They have theirs and you have yours.

But what about the other startup scripts? None of them run by default. This is why you're having problems. What makes it worse is if your services don't shutdown, DSM unmounts the disk out from under them with a -f flag, causing the blinking light of doom. Also any additional processes residing in or symlinked to /opt/ that you have running including bash! will prevent DSM from shutting down. I thought it would be a smart idea to change root's default shell to /opt/bin/bash. It made it impossible to shutdown while I was logged in. Also, it made it impossible to login to root when /opt wasn't mounted. Do not set any default shells to anything residing in /opt!

Ok so how do we fix this best? There are several ways to make it work, but this one is I think the closest to the developers' intentions and also improves your logging to /var/log/messages for troubleshooting later.

Replace or add the below scripts.
1. /etc/rc.optware
Please note what your REAL_OPT_DIR value is

Code: Select all

grep 'REAL_OPT_DIR' /etc/rc.optware

Backup the old stuff

Code: Select all

cp /etc/rc.optware{,.bak}

copy this below script into /etc/rc.optware using vi or whatever you have installed.
make sure you replace the REAL_OPT_DIR line with whatever you got from grep.

Code: Select all

#! /bin/sh

if test -z "${REAL_OPT_DIR}"; then
# next line to be replaced according to OPTWARE_TARGET
REAL_OPT_DIR=/volume1/@optware
fi

logger -s -p daemon.error "$0 $1 executing."
case "$1" in
    start)
        logger -s -p daemon.error "Starting Optware."

        if test -n "${REAL_OPT_DIR}"; then
            if ! grep ' /opt ' /proc/mounts >/dev/null 2>&1 ; then
                mkdir -p /opt
      logger -s -p daemon.error "Nothing mounted to /opt.  Mounting $REAL_OPT_DIR to /opt."
                mount -o bind ${REAL_OPT_DIR} /opt
      logger -s -p daemon.error "/opt mounted"
            fi   
        fi
#   [ -x /opt/etc/rc.optware ] && /opt/etc/rc.optware $1 || logger -s -p daemon.error "/opt/etc/rc.optware not executable or doesn't exist."
    ;;
    reconfig)
   true
    ;;
    stop)
        logger -s -p daemon.error "Shutting down Optware."
   if [ -n "`/opt/bin/pidof umount`" ]; then
      logger -s -p daemon.error "umount is already running"
      logger -s -p daemon.error "`ps aux | grep [u]mount`"
   fi   
#   [ -x /opt/etc/rc.optware ] && /opt/etc/rc.optware $1 || logger -s -p daemon.error "/opt/etc/rc.optware not executable or doesn't exist."
   true
    ;;
    *)
        echo "Usage: $0 {start|stop|reconfig}"
        exit 1
esac
logger -s -p daemon.error "$0 $1 complete."
logger -s -p daemon.error "Optware $1 complete."
exit 0


2. /opt/etc/rc.optware
Backup the old stuff

Code: Select all

cp /opt/etc/rc.optware{,.bak}

copy this below script into /opt/etc/rc.optware using vi or whatever you have installed.

Code: Select all

#!/bin/sh

# Optware setup
# Alternatives Optware Startup und Shutdown Script #/usr/local/etc/rc.d/optware.sh
#

logger -s -p daemon.error "$0 $1 executing."
case $1 in
start)
       for i in /opt/etc/init.d/S??* ;do
#
               # Ignore dangling symlinks (if any).
               [ ! -f "$i" ] && continue
      logger -s -p daemon.error "Beginning $1 of $i."
               case "$i" in
                  *.sh)
                       # Source shell script for speed.
                       (
                               trap - INT QUIT TSTP
                               set start
                               . $i
                       )
                       ;;
                  *\~)
                       # Tilde is a backup, ignore
                      logger -s -p daemon.error "Ignoring $i. It's a backup."
                       ;;
                  *)
                       # No sh extension, so fork subprocess.
                       $i start
                       ;;
               esac
          logger -s -p daemon.error "$1 of $i complete."
       done
       ;;
#
stop)
#
       for i in /opt/etc/init.d/K??* ;do
#
               # Ignore dangling symlinks (if any).
          [ ! -f "$i" ] && continue
      logger -s -p daemon.error "Beginning $1 of $i."
               case "$i" in
                  *.sh)
                       # Source shell script for speed.
                       (
                               trap - INT QUIT TSTP
                               set stop
                              . $i
                       )
                       ;;
                  *\~)
                       # Tilde is a backup, ignore
                      logger -s -p daemon.error "Ignoring $i. It's a backup."
                       ;;
                  *)
                       # No sh extension, so fork subprocess.
                       $i stop                       ;;
               esac
          logger -s -p daemon.error "$1 of $i complete."
         done
         ;;
#
*)
         echo "Usage: $0 [start|stop]"
         ;;
esac
logger -s -p daemon.error "$0 $1 complete."
#
# End


3. /usr/local/etc/rc.d/optware.sh
copy this below script into /usr/local/etc/rc.d/optware.sh using vi or whatever you have installed.
make sure you replace the REAL_OPT_DIR line with whatever you got from grep in step 1.

Code: Select all

#! /bin/sh

if test -z "${REAL_OPT_DIR}"; then
# next line to be replaced according to OPTWARE_TARGET
REAL_OPT_DIR=/volume1/@optware
fi

logger -s -p daemon.error "$0 $1 executing."
case "$1" in
    start)
        logger -s -p daemon.error "Starting Optware."

        if test -n "${REAL_OPT_DIR}"; then
            if ! grep ' /opt ' /proc/mounts >/dev/null 2>&1 ; then
                mkdir -p /opt
      logger -s -p daemon.error "Nothing mounted to /opt.  Mounting $REAL_OPT_DIR to /opt."
                mount -o bind ${REAL_OPT_DIR} /opt
      logger -s -p daemon.error "/opt mounted"
            fi   
        fi
   [ -x /opt/etc/rc.optware ] && /opt/etc/rc.optware $1 || logger -s -p daemon.error "/opt/etc/rc.optware not executable or doesn't exist."
    ;;
    reconfig)
   true
    ;;
    stop)
        logger -s -p daemon.error "Shutting down Optware."
   if [ -n "`/opt/bin/pidof umount`" ]; then
      logger -s -p daemon.error "umount is already running"
      logger -s -p daemon.error "`ps aux | grep [u]mount`"
   fi   
   [ -x /opt/etc/rc.optware ] && /opt/etc/rc.optware $1 || logger -s -p daemon.error "/opt/etc/rc.optware not executable or doesn't exist."
   true
    ;;
    *)
        echo "Usage: $0 {start|stop|reconfig}"
        exit 1
esac
logger -s -p daemon.error "$0 $1 complete."
logger -s -p daemon.error "Optware $1 complete."
exit 0


4. Set the permissions on everything correctly

Code: Select all

chmod 775 /etc/rc.optware /opt/etc/rc.optware /usr/local/etc/rc.d/optware.sh


You should be done! Init scripts running in /opt/etc/init.d/ will run correctly.
your directory should look like this for reference:
(I have 2 services, named and pound running)
nas> ls -la /opt/etc/init.d
drwxr-xr-x 2 root root 4096 May 4 14:25 .
drwxr-xr-x 5 root root 4096 May 1 18:33 ..
lrwxrwxrwx 1 root root 8 Apr 30 18:16 K90pound -> S10pound
lrwxrwxrwx 1 root root 8 Apr 30 18:17 K91named -> S09named
-rwxr-xr-x 1 root root 1181 May 1 17:22 S09named
-rwxr-xr-x 1 root root 1143 May 1 10:18 S09named~
-rwxr-xr-x 1 root root 1117 May 1 10:32 S10pound
-rwxr-xr-x 1 root root 1074 May 1 10:30 S10pound~

The key here is that the S scripts run 0-99 to start, while the K scripts run 0-99 to kill.
note that my K scripts are just symlinked to my S scripts. Most optware will do that automatically, but for reference the command looks like 'ln -s S09named K91named'

If for whatever reason you have problems, double check the instructions, and 'tail -f /var/log/messages' Worst case, replace your backups.

Enjoy.
Last edited by khaki54 on Sat Apr 04, 2015 6:20 pm, edited 3 times in total.
botte
I'm New!
I'm New!
Posts: 3
Joined: Sat Jun 02, 2012 4:05 pm

Re: Services Startup / Shutdown Failure [solved]

Postby botte » Mon Jun 04, 2012 7:32 pm

Hi khaki54,

really great post, but if /etc/rc.optware shall call opt/etc/rc.optware the #'s have to be removed in /etc/rc.optware:

Code: Select all

    #! /bin/sh

    if test -z "${REAL_OPT_DIR}"; then
    # next line to be replaced according to OPTWARE_TARGET
    REAL_OPT_DIR=/volume1/@optware
    fi

    logger -s -p daemon.error "$0 $1 executing."
    case "$1" in
        start)
            logger -s -p daemon.error "Starting Optware."

            if test -n "${REAL_OPT_DIR}"; then
                if ! grep ' /opt ' /proc/mounts >/dev/null 2>&1 ; then
                    mkdir -p /opt
          logger -s -p daemon.error "Nothing mounted to /opt.  Mounting $REAL_OPT_DIR to /opt."
                    mount -o bind ${REAL_OPT_DIR} /opt
          logger -s -p daemon.error "/opt mounted"
                fi   
            fi
       [ -x /opt/etc/rc.optware ] && /opt/etc/rc.optware $1 || logger -s -p daemon.error "/opt/etc/rc.optware not executable or doesn't exist."
        ;;
        reconfig)
       true
        ;;
        stop)
            logger -s -p daemon.error "Shutting down Optware."
       if [ -n "`/opt/bin/pidof umount`" ]; then
          logger -s -p daemon.error "umount is already running"
          logger -s -p daemon.error "`ps aux | grep [u]mount`"
       fi   
       [ -x /opt/etc/rc.optware ] && /opt/etc/rc.optware $1 || logger -s -p daemon.error "/opt/etc/rc.optware not executable or doesn't exist."
       true
        ;;
        *)
            echo "Usage: $0 {start|stop|reconfig}"
            exit 1
    esac
    logger -s -p daemon.error "$0 $1 complete."
    logger -s -p daemon.error "Optware $1 complete."
    exit 0


The everything worked for me like a charm :)

enjoy
khaki54
Trainee
Trainee
Posts: 11
Joined: Sun Apr 29, 2012 8:39 pm

Re: Services Startup / Shutdown Failure [solved]

Postby khaki54 » Mon Jun 04, 2012 11:37 pm

My reasoning for commenting that out is because /etc/rc.optware is only run at start, where /usr/local/etc/rc.d/optware.sh is run at start and stop. So I moved that part of the code in /etc/rc.optware to /usr/local/etc/rc.d/optware.sh and then I commented it out, in case they later fix how they do startup/shutdown. I don't think you will hurt anything by uncommenting those lines, but now it will run the start twice. Since everything is actually logged now, you can verify what I'm saying by looking at /var/log/messages.

Thanks for the reply, I was beginning to wonder if anyone read this 'opus' of a post.
User avatar
sjeph8je
Enlightened
Enlightened
Posts: 484
Joined: Fri Jan 29, 2010 10:06 pm
Location: Netherlands

Re: Services Startup / Shutdown Failure [solved]

Postby sjeph8je » Tue Jun 05, 2012 6:57 am

Hi,

the post was of great value :). I had my DS209 suddenly not rebooting anymore when I ordered it to. It just kept staying on with the blue light flashing. Don't know what changed because I always could reboot my DS209 with optware installed. After applying the scripts it works perfectly again.

Many thxs.
Synology DS-916+ | DSM 6.1.4-15217-1 | 2 x WD40EFRX (RAID1-btrfs) | 2 x WD10EFRX (RAID1-btrfs)
  • Startech S3510SMU33 | WD20EFRX (ext-4)
Synology DS-213J | DSM 6.1.4-15217-1 | 2 x WD5000AAKS (RAID0-ext4) (Testing, Download Station)
Synology DS-209 (DS211j modded) | DSM 6.1.4-15217-1 | 2 x WD20EFRX (RAID1-ext4) (Remote backup)
Synology DS-209 (DS211j modded) | DSM 6.2 beta | 1 x WD20EARS (Resurrected as TestStation)

Corruption wears infinite disguises.
botte
I'm New!
I'm New!
Posts: 3
Joined: Sat Jun 02, 2012 4:05 pm

Re: Services Startup / Shutdown Failure [solved]

Postby botte » Wed Jun 06, 2012 9:48 am

Hi khaki,

the "problem" with your Post is, that you have to be loggend in to view the post. So when you change that, a lot more people will read your "opus" :wink:

But back to the subject. I don't know if I understand everything correctly, but to get the optware applications like for example apache started, the /etc/rc.optware scripts must call the /opt/etc/rc.optware script which calles all scripts in /opt/etc/init.d/, or?

My log looks like this at the moment:

Code: Select all

Jun  4 20:25:54 root: start of /opt/etc/init.d/S80apache.sh complete.                                       
Jun  4 20:25:54 root: /opt/etc/rc.optware start complete.                                                   
Jun  4 20:25:54 root: /etc/rc.optware start complete.                                                       
Jun  4 20:25:54 root: Optware start complete.                                                               


So even with my uncommented version of /etc/rc.optware the S80apache.sh is only called only once. There seems to be no other path to arrive your Step 4 than by Step 1, 2 and 3.

What is my mistake?
khaki54
Trainee
Trainee
Posts: 11
Joined: Sun Apr 29, 2012 8:39 pm

Re: Services Startup / Shutdown Failure [solved]

Postby khaki54 » Wed Jun 06, 2012 4:16 pm

Oh that's weird, I don't know how to make it viewable without logging in.

So basically after my mod to /etc/rc.optware, all it does now is mount your /volume1/@optware directory.

Step 5 (/usr/local/etc/rc.d/optware.sh) is what actually calls /opt/etc/rc.optware now. The reason for this is because /usr/local/etc/rc.d/optware.sh is run in stop AND start, where /etc/rc.optware (Step 2)is only run on start.

So after my changes, step 3 and 4 move to the end.

Basically my diagnosis of the steps in the first post are the stock paradigm
Post change they would look like this:

On startup post-edit, scripts run in this order:
1. /usr/syno/etc/rc.d/S[00->99]scriptname.sh start
These are all of the Synology Scripts.

2. /etc/rc.optware start (executed by rc.local)
This script mounts your /opt with your /volume1/@optware

3. /usr/local/etc/rc.d/optware.sh start (executed by unknown syno script)
This now rechecks your mounts on startup, and executes /opt/etc/rc.optware start.

4. /opt/etc/rc.optware start (executed by /usr/local/etc/rc.d/optware.sh)
This executes a /opt/etc/init.d/S[00->99]scriptname.sh start

5. /opt/etc/init.d/S[00->99]scriptname.sh start (executed by /opt/etc/rc.optware)
This is where optware packages will install their startup scripts. This is the correct place.

On shutdown, scripts run in this order:
1. /usr/local/etc/rc.d/optware.sh stop (executed by unknown syno script)
This script does some logging on umount, then executes /opt/etc/rc.optware stop.

2. /opt/etc/rc.optware stop (executed by /usr/local/etc/rc.d/optware.sh)
This executes a /opt/etc/init.d/K[00->99]scriptname.sh stop

3. /opt/etc/init.d/K[00->99]scriptname.sh stop (executed by /opt/etc/rc.optware)
This is where optware packages will install their shutdown scripts.

4. /usr/syno/etc/rc.d/S[00->99]scriptname.sh stop
These are all of the Synology Scripts.
yureawen
I'm New!
I'm New!
Posts: 3
Joined: Fri Jun 15, 2012 8:52 am

Re: Services Startup / Shutdown Failure [solved]

Postby yureawen » Fri Jun 15, 2012 9:06 am

khaki54 wrote:Oh that's weird, I don't know how to make it viewable without logging in.


I think maybe it is because of the subforum where it is located.

Anyway, many thanks for your post, I will try to apply your suggested changes to my Synology later and see what happens. Just yesterday I restarted the NAS and realized that these issues that you talk about where happening, I had to unplug the Synology to be able to use it again. So, great post, really :)
botte
I'm New!
I'm New!
Posts: 3
Joined: Sat Jun 02, 2012 4:05 pm

Re: Services Startup / Shutdown Failure [solved]

Postby botte » Tue Jun 26, 2012 11:18 am

Hi khaki,

after a while I had today some time left to come back to this subject.

It turned out that I had a problem with my /usr/local/etc/rc.d/optware.sh file. After I created it again with your code everything is working now as expected :D

Again, thank you very much for your work and sharing it :)

Have a nice day!
khaki54
Trainee
Trainee
Posts: 11
Joined: Sun Apr 29, 2012 8:39 pm

Re: Services Startup / Shutdown Failure [solved]

Postby khaki54 » Tue Jun 26, 2012 1:17 pm

good to hear, you're welcome
malach1
Trainee
Trainee
Posts: 16
Joined: Thu Nov 25, 2010 4:59 pm
Location: KC, MO
Contact:

Re: Services Startup / Shutdown Failure [solved]

Postby malach1 » Tue Jun 26, 2012 11:58 pm

Works great, thank you! (This was driving me crazy.)

Thanks,
Phil
pjs
Trainee
Trainee
Posts: 19
Joined: Tue Aug 14, 2012 8:11 pm

Re: Services Startup / Shutdown Failure [solved]

Postby pjs » Thu Aug 16, 2012 7:56 am

I knew there was something wrong with the rc scripts, and i'm so glad i came across your "opus" before i started digging. This thread is IMO essential to anyone who is using optware (service optware at least). You rock man, thanks for fixing this!

Return to “IPKG”

Who is online

Users browsing this forum: No registered users and 2 guests