#!/bin/bash # Copyright 1999-2005 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 source /sbin/functions.sh usage() { cat << FOO usage: rc-update -a|add script runlevel2 [runlevel2 ...] rc-update -d|del script [runlevel1 ...] rc-update -s|show [runlevel1 ...] examples: # rc-update add net.eth0 default Adds the net.eth0 script (in /etc/init.d) to the "default" runlevel. # rc-update del sysklogd Deletes the sysklogd script from all runlevels. The original script is not deleted, just any symlinks to the script in /etc/runlevels/*. # rc-update del net.eth2 default wumpus Delete the net.eth2 script from the default and wumpus runlevels. All other runlevels are unaffected. Again, the net.eth2 script residing in /etc/init.d is not deleted, just any symlinks in /etc/runlevels/default and /etc/runlevels/wumpus. # rc-update show Show all the available scripts and list at which runlevels they will execute. FOO exit 1 } add() { local x= local myscript= if [[ $# -lt 3 ]] ; then eerror "$0: at least two arguments expected after \"$1\"." exit 1 fi shift myscript="$1" if [[ ! -e ${ROOT}/etc/init.d/${myscript} ]] ; then eerror "$0: '${ROOT}/etc/init.d/${myscript}' not found; aborting." exit 1 fi shift for x in $* ; do if [[ ! -e ${ROOT}/etc/runlevels/${x} ]] ; then ewarn "runlevel ${x} not found; skipping" continue fi if [[ -L ${ROOT}/etc/runlevels/${x}/${myscript} ]] ; then ewarn "${myscript} already installed in runlevel ${x}; skipping" continue fi if [[ ! -x ${ROOT}/etc/init.d/${myscript} ]] ; then ewarn "${myscript} not executable; skipping" continue fi ln -snf "/etc/init.d/${myscript}" "${ROOT}/etc/runlevels/${x}/${myscript}" if [[ $? -ne 0 ]] ; then eerror "$0: failed to add ${myscript} to ${x}." exit 1 fi einfo "${myscript} added to runlevel ${x}" done } del() { local x= local mylevels= local myscript= local remlevels= if [[ $# -lt 2 ]] ; then eerror "$0: at least one argument expected after \"$1\"." exit 1 fi shift myscript=$1 shift if [[ $# -eq 0 ]] ; then mylevels=$(cd "${ROOT}"/etc/runlevels/; ls) else mylevels="$*" fi remlevels="" for x in ${mylevels} ; do if [[ -L ${ROOT}/etc/runlevels/${x}/${myscript} ]] ; then rm -f "${ROOT}/etc/runlevels/${x}/${myscript}" remlevels="${remlevels} ${x}" fi done if [[ -z ${remlevels} ]] ; then einfo "${myscript} not found in any of the specified runlevels." else einfo "${myscript} removed from the following runlevels:${remlevels}" fi } show() { local x= local y= local mylevels= local myscripts= shift if [[ $# -eq 0 ]] ; then mylevels=$(cd "${ROOT}"/etc/runlevels/; ls) else mylevels="$*" fi myscripts=$(cd "${ROOT}"/etc/init.d; ls) for x in ${myscripts} ; do if [[ ${x%%.sh} = "${x}" ]] ; then printf "%20s | " ${x:0:19} for y in ${mylevels} ; do if [[ -L ${ROOT}/etc/runlevels/${y}/${x} ]] ; then echo -n "${y} " else printf "%${#y}s " " " fi done echo "" fi done } check_is_root() { if [[ ${EUID} -ne 0 ]] ; then eerror "$0: must be root." exit 1 fi } if [[ $# -lt 1 ]] ; then usage exit 1 fi if [[ -n ${ROOT} ]] ; then [[ ${ROOT:0-1} == "/" ]] && export ROOT=${ROOT:0:${#ROOT}-1} einfo "Working with files in root ${ROOT} ..." fi case "$1" in add|-a) check_is_root add "$@" ;; del|delete|-d) check_is_root del "$@" ;; show|-s) show "$@" ;; *) usage exit 1 ;; esac # vim:ts=4