You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
4.2 KiB
98 lines
4.2 KiB
#!/usr/bin/env bash
|
|
#
|
|
# Initial script for UBLinux
|
|
# This script are launching before starting init from initrd script
|
|
# Current dir allways must be set to root (/)
|
|
# All system path must be relative, except initrd dirs
|
|
|
|
ENABLED=yes
|
|
[[ ${ENABLED} == "yes" ]] || exit 0
|
|
DEBUGMODE=no
|
|
|
|
PATH=.:/:/usr/bin:/usr/local/bin:/usr/local/sbin
|
|
|
|
[[ -d /usr/lib/ublinux ]] && { unset ROOTFS; unset CMD_CHROOT; } || { ROOTFS="/sysroot"; CMD_CHROOT="chroot ${ROOTFS} "; }
|
|
SOURCE=${ROOTFS}/usr/lib/ublinux/functions; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
|
|
SOURCE=${ROOTFS}/usr/lib/ublinux/default; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
|
|
debug_mode "$0" "$@"
|
|
|
|
SYSCONF="${ROOTFS}${SYSCONF}"
|
|
SOURCE=${SYSCONF}/config; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
|
|
SOURCE=${SYSCONF}/network; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
|
|
|
|
# List NTP servers: PART_SYSTEMDPATH/Service_Name Process_Name
|
|
NTP_SERVICES_PROCESS="
|
|
sysinit.target.wants/systemd-timesyncd.service systemd-timesyncd
|
|
multi-user.target.wants/ntpd.service ntpd
|
|
multi-user.target.wants/ntpdate.service ntpdate
|
|
multi-user.target.wants/chronyd.service chronyd
|
|
multi-user.target.wants/openntpd.service ntpd
|
|
multi-user.target.wants/ptp4l.service ptp4l
|
|
multi-user.target.wants/phc2sys.service phc2sys"
|
|
|
|
exec_ntp_servers_set(){
|
|
restart_systemd_ntp(){
|
|
if [[ -n ${ROOTFS} ]]; then
|
|
${CMD_CHROOT} /usr/bin/systemctl --quiet enable ${NTP_SYSTEMSERVICE} &>/dev/null
|
|
else
|
|
/usr/bin/systemctl daemon-reload
|
|
/usr/bin/systemctl --quiet enable ${NTP_SYSTEMSERVICE} &>/dev/null
|
|
/usr/bin/systemctl --quiet restart ${NTP_SYSTEMSERVICE} &>/dev/null
|
|
fi
|
|
}
|
|
ISSYSTEMD=$(readlink -fq ${ROOTFS}/usr/bin/init | grep "lib/systemd/systemd$")
|
|
if [[ ${NTPSERVERS,,} == "dhcp" ]]; then
|
|
# Вызывая из NetworkManager скрипта, переменна ${NTPSERVERS} перезаписана на сервера NTP из DHCP
|
|
[[ -n $1 ]] && local NTPSERVERS="$1"
|
|
fi
|
|
if [[ ${NTPSERVERS,,} == @(stop|no|disable) ]]; then
|
|
exec_ntp_servers_stop
|
|
elif [[ ${NTPSERVERS,,} == "dhcp" ]]; then
|
|
# Активная systemd и выбран сервис NTP_SYSTEMSERVICE=systemd-timesyncd.service
|
|
if [[ -n ${ISSYSTEMD} && ${NTP_SYSTEMSERVICE} == "systemd-timesyncd.service" && -f ${ROOTFS}/usr/lib/systemd/system/systemd-timesyncd.service ]]; then
|
|
restart_systemd_ntp
|
|
fi
|
|
elif [[ -n ${NTPSERVERS} ]]; then
|
|
[[ ${NTPSERVERS,,} == "default" ]] && NTPSERVERS=${NTPSERVERS_DEFAULT}
|
|
[[ ${NTPSERVERS,,} == "ntp-ru" ]] && NTPSERVERS=${NTPSERVERS_RU}
|
|
# Активная systemd и выбран сервис NTP_SYSTEMSERVICE=systemd-timesyncd.service
|
|
if [[ -n ${ISSYSTEMD} && ${NTP_SYSTEMSERVICE} == "systemd-timesyncd.service" && -f ${ROOTFS}/usr/lib/systemd/system/systemd-timesyncd.service ]]; then
|
|
NTPSERVERS=$(tr ',;' ' ' <<< ${NTPSERVERS})
|
|
# CONFIG_TIMESYNCD="${ROOTFS}/etc/systemd/timesyncd.conf.d/ublinux${ETH_INTERFACE}.conf"
|
|
CONFIG_TIMESYNCD="${ROOTFS}/etc/systemd/timesyncd.conf.d/ubconfig.conf"
|
|
[[ -d ${CONFIG_TIMESYNCD%/*} ]] || mkdir -p ${CONFIG_TIMESYNCD%/*}
|
|
rm -f ${CONFIG_TIMESYNCD%/*}/ubconfig*.conf
|
|
cat <<-EOF > "${CONFIG_TIMESYNCD}"
|
|
[Time]
|
|
NTP=${NTPSERVERS}
|
|
FallbackNTP=${NTPSERVERS_FALLBACK}
|
|
EOF
|
|
restart_systemd_ntp
|
|
fi
|
|
fi
|
|
}
|
|
exec_ntp_servers_stop(){
|
|
# Определяем активную systemd
|
|
ISSYSTEMD=$(readlink -fq ${ROOTFS}/usr/bin/init | grep "lib/systemd/systemd$")
|
|
while IFS=' ' read ITEM_NTP_SERVICE ITEM_NTP_PROCESS; do
|
|
[[ -z ${ITEM_NTP_SERVICE} || -z ${ITEM_NTP_PROCESS} ]] && continue
|
|
if [[ -n ${ISSYSTEMD} && -n ${ROOTFS} ]]; then
|
|
${CMD_CHROOT} /usr/bin/systemctl --quiet disable ${ITEM_NTP_SERVICE##*/} &>/dev/null
|
|
elif [[ -n ${ISSYSTEMD} ]]; then
|
|
/usr/bin/systemctl --quiet disable --now ${ITEM_NTP_SERVICE##*/} &>/dev/null || pkill -f ${ITEM_NTP_PROCESS}
|
|
fi
|
|
done < <(tr -d '\t' <<< ${NTP_SERVICES_PROCESS})
|
|
}
|
|
|
|
################
|
|
##### MAIN #####
|
|
################
|
|
|
|
if [[ -z $@ || $1 == @("set="|"set+="|"set++=") ]]; then
|
|
shift
|
|
exec_ntp_servers_set $@
|
|
elif [[ $1 == @("set-="|"set--="|"remove") ]]; then
|
|
shift
|
|
exec_ntp_servers_stop $@
|
|
fi
|