隔壁网

[玩法/技巧] 群晖NAS启动分析及添加optware启动脚本支持

2014-5-5 15:41
64973
本帖最后由 yumin9822 于 2014-5-16 10:51 编辑

http://forum.synology.com/enu/viewtopic.php?f=40&t=51025    上面网址里面有详细的分析DSM系统启动的分析,我贴出给大家分享以下。作者是在DSM4.0 安装了optware环境后的分析。本人觉得对于以后大家添加自己的开机运行脚本的位置大有裨益!蓝色字体部分为作者分析的DSM启动和关闭过程脚本的执行顺序,按顺序执行!即开机运行顺序为
   /usr/syno/etc/rc.d/ ---> /etc/rc.optware ---> /opt/etc/rc.optware --->/opt/etc/init.d/ ---> /usr/local/etc/rc.d/
关机运行顺序为
   /usr/local/etc/rc.d/scriptname.sh stop ---> /usr/syno/etc/rc.d/S[00->99]scriptname.sh stop

如果按照作者的方法,修改为以下脚本后,开机关机顺序变成下面的
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.
    作者推荐自己制作的开机运行脚本放在/usr/local/etc/rc.d/目录下面。
     本人白裙DS213J DSM5.0 4482系统安装好optware环境后,测试通过!(optware环境安装有兴趣,可以查看我的帖子http://www.gebi1.com/thread-83866-1-1.html)


以下为原文,供你参考!
---------------------------------------------------
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, 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:
  • grep 'REAL_OPT_DIR' /etc/rc.optware


Backup the old stuff
  • Code:
  • 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:
  • #! /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 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:
  • cp /opt/etc/rc.optware{,.bak}


copy this below script into /opt/etc/rc.optware using vi or whatever you have installed.
  • Code:
  • #!/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:
  • #! /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 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:
  • 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 Wed Jun 06, 2012 4:16 pm, edited 1 time in total.

分享到 :
2 人收藏

3 个回复

倒序浏览
囧.X  魔仙 | 2014-5-7 15:05:25
支持下。。。。。。
kfqzhw  魔仙 | 2014-5-7 15:56:53
有点复杂
gogovip  走火入魔 | 2014-10-5 09:40:15
支持下。。。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

微信扫一扫关注公众号,获取注册邀请码!

小黑屋|手机版|Archiver|隔壁网 ( 粤ICP备14056481号-1 )

Powered by Discuz! X3.3 © 2001-2016 Comsenz Inc.

返回顶部