[20191230]注意設定SYNC_HWCLOCK=yes.txt

lfree發表於2019-12-30

[20191230]注意設定SYNC_HWCLOCK=yes.txt

--//前一段時間的測試,連結:http://blog.itpub.net/267265/viewspace-2670625/=>[20191226]dmesg與時間戳.txt
--//實際上當時機器出現異常,重啟後硬體時鐘與system時鐘不一致造成的情況:
--//主要幾個原因造成這個問題,這個問題主要出現在Oracle Linux Server release 5.9,我檢查6.X以上版本SYNC_HWCLOCK=yes:

1.環境:
# cat /etc/issue
Oracle Linux Server release 5.9

# cat /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -x"

# Set to 'yes' to sync hw clock after successful ntpdate
SYNC_HWCLOCK=no

# Additional options for ntpdate
NTPDATE_OPTIONS=""

--//預設安裝後加入-x引數的同時,必須修改SYNC_HWCLOCK=yes.不然啟動ntpd服務不會修改硬體時間.

2.幾點說明:
-x 引數:

-x  Normally, the time is slewed if the offset is less than the step threshold, which is 128 ms by default, and stepped
    if above the threshold. This option sets the threshold to 600 s, which is well within the accuracy window to set the
    clock  manually. Note: Since the slew rate of typical Unix kernels is limited to 0.5 ms/s, each second of adjustment
    requires an amortization interval of 2000 s. Thus, an adjustment as much as 600 s will take almost 14 days to
    complete. This option can be used with the -g and -q options. See the tinker command for other options. Note: The
    kernel time discipline is disabled with this option.

通常情況下,如果偏移量小於步長閾值(預設情況下為128ms),則旋轉時間並步進。如果超過閾值。此選項將閾值設定為600s,該閾值在
精確度視窗內,以設定手動計時。注:由於典型Unix核心的旋轉速率僅限於0.5ms/s,因此每秒調整一次因此,多達600秒的調整需要將近
14天的時間才能完成。完成。此選項可與-g和-q選項一起使用。有關其他選項,請參見修補程式命令。注:用此選項禁用核心時間。

--//首先必須設定-x引數,不然即使設定SYNC_HWCLOCK=yes,也不會呼叫hwclock同步硬體時間.

# vi /etc/init.d/ntpd
...
readconf() {
    dostep=''
    dropstr=''
    OPTIND=1
    while getopts ":46aAbc:dD:f:gi:k:l:LnN:p:P:qr:s:t:u:v:V:x" args $OPTIONS;
    do
      case "$args" in
        x) dostep=yes;;
        ~~~~~~~~~~~~~~~
        c) ntpconf="$OPTARG";;
        u) dropstr="-U $(echo $OPTARG | sed 's/:.*//')";;
      esac
    done

    [ -x /sbin/ntpd -a -f $ntpconf ] || exit 0

    tickers=''
    if [ -s "$ntpstep" ]; then
        tickers=$(sed 's/#.*//' $ntpstep)
        echo "$tickers" | grep -qi '[a-z0-9]' && dostep=yes || tickers=''
    fi
    if [ -n "$dostep" -a -z "$tickers" ]; then
        # -x option is used, but step-tickers doesn't exist or contain
        # anything useful, use servers from ntp.conf instead
        tickers=$(awk '$1=="peer"||$1=="server"{print $2}' $ntpconf | \
            fgrep -v 127.127.1.0)
    fi
}

--//注意看下劃線,僅僅設定-x引數,dostep=yes.

start() {
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 1

    readconf;
#   set -x
#   echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    if [ -n "$dostep" ]; then
   ~~~~~~~~~~~~~~~~~~~~~~~~~~        
        echo -n $"$prog: Synchronizing with time server: "
        /sbin/ntpdate $dropstr -s -b $NTPDATE_OPTIONS $tickers &>/dev/null
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success || failure
        echo
        if [ $RETVAL -eq 0 ]; then
            [ "$SYNC_HWCLOCK" = "yes" ] && sync_hwclock
        else
            OPTIONS="$OPTIONS -g"
        fi
    else
        # -g can replace the grep for time servers
        # as it permits ntpd to violate its 1000s limit once.
        OPTIONS="$OPTIONS -g"
    fi
        # Start daemons.
        echo -n $"Starting $prog: "
        daemon ntpd $OPTIONS
    RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ntpd
    return $RETVAL
}

--//注意看下劃線,-n 表示the length of STRING is nonzero,如果$dostep有值才會呼叫裡面的命令.才會執行ntpdate以及同步硬體時
--//間.

--//另外注意一個問題有一些系統有防火牆,必須修改/etc/ntp.conf,必須加入本地時間伺服器.一般這個時間伺服器與上游的時間伺服器
--//同步,不然也不會同步硬體時間.這樣啟動看到的情況是這樣:

# service ntpd restart
Shutting down ntpd:                                        [  OK  ]
ntpd: Synchronising with time server:                      [  OK  ]
Syncing hardware clock to system time                      [  OK  ]
Starting ntpd:                                             [  OK  ]

--//為什麼要這樣設定,實際上即使你啟動ntpd同步硬體時間,隨著執行時間增加,硬體時鐘與系統時鐘會差距越來越遠:
#  hwclock -r ;date
Mon 30 Dec 2019 04:23:38 PM CST  -0.897579 seconds
Mon Dec 30 15:57:24 CST 2019

--//也許養成隔段時間重啟ntpd服務很有必要.
--//兩者相差30分鐘.而且硬體時鐘超前系統時鐘.設想如果系統異常重啟,不會同步系統時鐘到硬體時鐘(參考/etc/init.d/halt指令碼).
--//這樣如果你沒有設定SYNC_HWCLOCK=yes,這樣時間差距太大,ntpd會無法啟動.oracle rac系統時間也不正確.注意看前面-x引數說明.




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2671283/,如需轉載,請註明出處,否則將追究法律責任。

相關文章