--- trunk/include/mtools.minc 2009/06/12 17:25:14 6 +++ branches/magellan-next/include/mtools.minc 2011/06/02 21:21:16 19 @@ -1,4 +1,4 @@ -# $Header: /magellan-cvs/mage/include/mtools.minc,v 1.3 2008/02/10 12:13:56 niro Exp $ +# $Id$ # adds services to runlevels and starts them # mstartservice service {/path/to/service_exec} @@ -7,7 +7,23 @@ local service="$1" local service_exec="$2" - [[ -z ${service_exec} ]] && service_exec="$(which ${service})" + # only run if sysV init was found + if [[ ! -f /etc/rc.d/init.d/functions ]] + then + echo "sysV init not found, not adding service ${service} to runlevels!" + return + fi + + if [[ -z ${service_exec} ]] + then + case ${service} in + *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; + *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; + *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; + *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; + *) service_exec="$(which ${service} 2> /dev/null)" ;; + esac + fi # add service to default runlevels echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding ${service} to default runlevels ..." @@ -35,7 +51,23 @@ local service="$1" local service_exec="$2" - [[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" + # only run if sysV init was found + if [[ ! -f /etc/rc.d/init.d/functions ]] + then + echo "sysV init not found, not adding service ${service} to runlevels!" + return + fi + + if [[ -z ${service_exec} ]] + then + case ${service} in + *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; + *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; + *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; + *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; + *) service_exec="$(which ${service} 2> /dev/null)" ;; + esac + fi # only stop the service if ${service_exec} does not exist [[ -f ${MROOT}/${service_exec} ]] && return @@ -64,7 +96,23 @@ local service="$1" local service_exec="$2" - [[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" + # only run if sysV init was found + if [[ ! -f /etc/rc.d/init.d/functions ]] + then + echo "sysV init not found, not adding service ${service} to runlevels!" + return + fi + + if [[ -z ${service_exec} ]] + then + case ${service} in + *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;; + *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;; + *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;; + *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;; + *) service_exec="$(which ${service} 2> /dev/null)" ;; + esac + fi # do not stop services on bootstrap or MROOT!=/ if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] @@ -76,3 +124,124 @@ fi fi } + + +# adds systemd units to runlevels and starts them +# mstartunit service {/path/to/service_exec} +mstartunit() +{ + local service="$1" + local service_exec="$2" + + # only run if systemd was found + if [[ ! -x /bin/systemctl ]] + then + echo "systemd not found, not adding unit ${service} to runlevels!" + return + fi + + # we do not support $MROOT atm + if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] + then + echo "\$MROOT is set, doing nothing!" + echo "Please add unit ${service} manually to the runlevels!" + echo "Run 'systemctl enable ${service}' in your chroot later on." + return + fi + + [[ -z ${service_exec} ]] && service_exec="$(which ${service})" + + # add service to default runlevels + echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding unit ${service} to default runlevels ..." + # reload daemon to honor changed unit files + systemctl daemon-reload + systemctl enable ${service} + + # do not start services on bootstrap or MROOT!=/ + if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] + then + # start or restart the service + systemctl try-restart ${service} + fi +} + +# removes systemd units from runlevels and stops them +# mstopunit service {/path/to/service_exec} +mstopunit() +{ + local service="$1" + local service_exec="$2" + + # only run if systemd was found + if [[ ! -x /bin/systemctl ]] + then + echo "systemd not found, not removing unit ${service} from runlevels!" + return + fi + + # we do not support $MROOT atm + if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] + then + echo "\$MROOT is set, doing nothing!" + echo "Please remove unit ${service} manually from the runlevels!" + echo "Run 'systemctl disable ${service}' in your chroot later on." + return + fi + + [[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" + + # only stop the service if ${service_exec} does not exist + [[ -f ${MROOT}/${service_exec} ]] && return + + # del services from runlevel regardless if they exist or not + echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing unit ${service} from default runlevels ..." + # reload daemon to honor changed unit files + systemctl daemon-reload + systemctl disable ${service} + + # do not stop services on bootstrap or MROOT!=/ + if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] + then + systemctl stop ${service} + # try harder + if [[ -n $(pidof ${service_exec}) ]] + then + killall -15 ${service_exec} &> /dev/null + sleep 1 + killall -9 ${service_exec} &> /dev/null + fi + fi +} + +# reloads a systemd unit if already running +# mreloadunit service {/path/to/service_exec} +mreloadunit() +{ + local service="$1" + local service_exec="$2" + + # only run if systemd was found + if [[ ! -x /bin/systemctl ]] + then + echo "systemd not found, not removing unit ${service} from runlevels!" + return + fi + + # we do not support $MROOT atm + if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]] + then + echo "\$MROOT is set, doing nothing!" + echo "Please remove unit ${service} manually from the runlevels!" + echo "Run 'systemctl disable ${service}' in your chroot later on." + return + fi + + [[ -z ${service_exec} ]] && service_exec="$(which ${service} 2> /dev/null)" + + # do not stop services on bootstrap or MROOT!=/ + if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]] + then + # only reload the service if running + systemctl reload-or-try-restart ${service} + fi +}