/[vps]/baselayout-vserver/trunk/sbin/rc-update
Gentoo

Diff of /baselayout-vserver/trunk/sbin/rc-update

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 251 Revision 252
1#!/bin/bash 1#!/bin/bash
2# Copyright 1999-2006 Gentoo Foundation 2# Copyright 1999-2006 Gentoo Foundation
3# Distributed under the terms of the GNU General Public License v2 3# Distributed under the terms of the GNU General Public License v2
4 4
5source /sbin/functions.sh 5source /sbin/functions.sh
6esyslog() { :; }
7
8argv0=${0##*/}
6 9
7usage() { 10usage() {
8cat << FOO 11cat << FOO
9usage: rc-update -a|add script runlevel2 [runlevel2 ...] 12usage: ${argv0} -a|add script runlevel1 [runlevel2 ...]
10 rc-update -d|del script [runlevel1 ...] 13 ${argv0} -d|del script [runlevel1 ...]
11 rc-update -s|show [runlevel1 ...] 14 ${argv0} -s|show [runlevel1 ...]
12 15
13examples: 16examples:
14 # rc-update add net.eth0 default 17 # ${argv0} add net.eth0 default
15 Adds the net.eth0 script (in /etc/init.d) to the "default" runlevel. 18 Adds the net.eth0 script (in /etc/init.d) to the "default" runlevel.
16 19
17 # rc-update del sysklogd 20 # ${argv0} del sysklogd
18 Deletes the sysklogd script from all runlevels. The original script 21 Deletes the sysklogd script from all runlevels. The original script
19 is not deleted, just any symlinks to the script in /etc/runlevels/*. 22 is not deleted, just any symlinks to the script in /etc/runlevels/*.
20 23
21 # rc-update del net.eth2 default wumpus 24 # ${argv0} del net.eth2 default wumpus
22 Delete the net.eth2 script from the default and wumpus runlevels. 25 Delete the net.eth2 script from the default and wumpus runlevels.
23 All other runlevels are unaffected. Again, the net.eth2 script 26 All other runlevels are unaffected. Again, the net.eth2 script
24 residing in /etc/init.d is not deleted, just any symlinks in 27 residing in /etc/init.d is not deleted, just any symlinks in
25 /etc/runlevels/default and /etc/runlevels/wumpus. 28 /etc/runlevels/default and /etc/runlevels/wumpus.
26 29
27 # rc-update show 30 # ${argv0} show
28 Show all the available scripts and list at which runlevels they 31 Show all the available scripts and list at which runlevels they
29 will execute. 32 will execute.
30FOO 33FOO
31 exit 1 34 exit ${1:-0}
32} 35}
33 36
34add() { 37add() {
38 local ret=0
35 local x= 39 local x=
36 local myscript= 40 local myscript=
37 41
38 if [[ $# -lt 3 ]] ; then 42 if [[ $# -lt 2 ]] ; then
39 eerror "$0: at least two arguments expected after \"$1\"." 43 eerror "Usage: ${argv0} add <script> runlevel1 [runlevel2 ...]" 1>&2
40 exit 1 44 exit 1
41 fi 45 fi
42 shift 46
47 [[ ${quiet} -gt 0 ]] && exec 2> /dev/null
48
43 myscript="$1" 49 myscript=$1
44 if [[ ! -e ${ROOT}/etc/init.d/${myscript} ]] ; then 50 if [[ ! -e ${ROOT}/etc/init.d/${myscript} ]] ; then
45 eerror "$0: '${ROOT}/etc/init.d/${myscript}' not found; aborting." 51 eerror "${argv0}: '${ROOT}etc/init.d/${myscript}' not found; aborting" 1>&2
46 exit 1 52 exit 1
47 fi 53 fi
48 shift 54 shift
49 for x in $* ; do 55 for x in $* ; do
50 if [[ ! -e ${ROOT}/etc/runlevels/${x} ]] ; then 56 if [[ ! -e ${ROOT}etc/runlevels/${x} ]] ; then
51 ewarn "runlevel ${x} not found; skipping" 57 ewarn "runlevel '${x}' not found; skipping" 1>&2
58 ((++ret))
52 continue 59 continue
53 fi 60 fi
54 if [[ -L ${ROOT}/etc/runlevels/${x}/${myscript} ]] ; then 61 if [[ -L ${ROOT}etc/runlevels/${x}/${myscript} ]] ; then
55 ewarn "${myscript} already installed in runlevel ${x}; skipping" 62 ewarn "${myscript} already installed in runlevel '${x}'; skipping" 1>&2
56 continue 63 continue
57 fi 64 fi
58 if [[ ! -x ${ROOT}/etc/init.d/${myscript} ]] ; then 65 if [[ ! -x ${ROOT}etc/init.d/${myscript} ]] ; then
59 ewarn "${myscript} not executable; skipping" 66 ewarn "${myscript} not executable; skipping" 1>&2
67 ((++ret))
60 continue 68 continue
61 fi 69 fi
62 ln -snf "/etc/init.d/${myscript}" "${ROOT}/etc/runlevels/${x}/${myscript}" 70 ln -snf "/etc/init.d/${myscript}" "${ROOT}etc/runlevels/${x}/${myscript}"
63 if [[ $? -ne 0 ]] ; then 71 if [[ $? -ne 0 ]] ; then
64 eerror "$0: failed to add ${myscript} to ${x}." 72 eerror "${argv0}: failed to add '${myscript}' to '${x}'" 1>&2
65 exit 1 73 ((++ret))
74 continue
66 fi 75 fi
67 einfo "${myscript} added to runlevel ${x}" 76 einfo "${myscript} added to runlevel ${x}"
68 done 77 done
78
79 return ${ret}
69} 80}
70 81
71del() { 82del() {
72 local x= 83 local x=
73 local mylevels= 84 local mylevels=
74 local myscript= 85 local myscript=
75 local remlevels= 86 local remlevels=
76 87
77 if [[ $# -lt 2 ]] ; then 88 if [[ $# -lt 1 ]] ; then
78 eerror "$0: at least one argument expected after \"$1\"." 89 eerror "Usage: ${argv0} del <script> [runlevel1 ...]" 1>&2
79 exit 1 90 exit 1
80 fi 91 fi
81 shift 92
93 [[ ${quiet} -gt 0 ]] && exec 2> /dev/null
94
82 myscript=$1 95 myscript=$1
83 shift 96 shift
84 if [[ $# -eq 0 ]] ; then 97 if [[ $# -eq 0 ]] ; then
85 mylevels=$(cd "${ROOT}"/etc/runlevels/; ls) 98 mylevels=$(cd "${ROOT}"etc/runlevels/; ls)
86 else 99 else
87 mylevels="$*" 100 mylevels="$*"
88 fi 101 fi
89 remlevels="" 102 remlevels=""
90 for x in ${mylevels} ; do 103 for x in ${mylevels} ; do
104 # -e will return false for broken symlinks so we need the extra -L
91 if [[ -L ${ROOT}/etc/runlevels/${x}/${myscript} ]] ; then 105 [[ ! -L ${ROOT}etc/runlevels/${x}/${myscript} ]] && \
92 rm -f "${ROOT}/etc/runlevels/${x}/${myscript}" 106 [[ ! -e ${ROOT}etc/runlevels/${x}/${myscript} ]] && continue
107
93 remlevels="${remlevels} ${x}" 108 remlevels="${remlevels} ${x}"
109 if [[ ! -L ${ROOT}etc/runlevels/${x}/${myscript} ]] ; then
110 ewarn "Removing invalid init.d script: '${ROOT}etc/runlevels/${x}/${myscript}'" 1>&2
94 fi 111 fi
112 rm -f "${ROOT}etc/runlevels/${x}/${myscript}"
113 ((ret+=$?))
95 done 114 done
96 if [[ -z ${remlevels} ]] ; then 115 if [[ -z ${remlevels} ]] ; then
97 einfo "${myscript} not found in any of the specified runlevels." 116 ewarn "'${myscript}' not found in any of the specified runlevels" 1>&2
98 else 117 elif [[ ${quiet} -eq 0 ]] ; then
99 einfo "${myscript} removed from the following runlevels:${remlevels}" 118 einfo "'${myscript}' removed from the following runlevels:${remlevels}"
100 fi 119 fi
120 return ${ret}
101} 121}
102 122
103show() { 123show() {
104 local x= 124 local x=
105 local y= 125 local y=
106 local mylevels= 126 local mylevels=
107 local myscripts= 127 local myscripts=
108 128
109 shift 129 shopt -s nullglob
110 if [[ $# -eq 0 ]] ; then 130 if [[ $# -eq 0 ]] ; then
111 mylevels=$(cd "${ROOT}"/etc/runlevels/; ls) 131 mylevels=$(cd "${ROOT}"etc/runlevels/; ls)
112 else 132 else
113 mylevels="$*" 133 mylevels="$*"
134 # verify runlevels provided by user
135 for y in ${mylevels} ; do
136 [[ ! -d ${ROOT}etc/runlevels/${y} ]] && ewarn "Runlevel doesn't exist: ${y}"
137 done
114 fi 138 fi
115 myscripts=$(cd "${ROOT}"/etc/init.d; ls) 139 myscripts=$(cd "${ROOT}"etc/init.d; ls)
140
141 # Sanity check to make sure everything is kosher ...
142 for x in $(find "${ROOT}"etc/runlevels -xtype l) ; do
143 ewarn "Broken runlevel entry: ${x}"
144 done
145 for x in $(find "${ROOT}"etc/runlevels ! -type l -a ! -type d) ; do
146 ewarn "Invalid runlevel entry: ${x}"
147 done
116 148
117 for x in ${myscripts} ; do 149 for x in ${myscripts} ; do
118 if [[ ${x%%.sh} = "${x}" ]] ; then 150 # skip *.sh scripts as they are helpers, not init.d scripts
119 printf "%20s | " ${x:0:19} 151 [[ ${x} == *.sh ]] && continue
152
153 # unless we are running in verbose mode, don't display scripts
154 # that aren't in any runlevels that we are examining
155 if [[ ${verbose} -eq 0 ]] ; then
156 local found_it=0
120 for y in ${mylevels} ; do 157 for y in ${mylevels} ; do
121 if [[ -L ${ROOT}/etc/runlevels/${y}/${x} ]] ; then 158 if [[ -L ${ROOT}etc/runlevels/${y}/${x} ]] ; then
122 echo -n "${y} " 159 found_it=1
123 else 160 break
124 printf "%${#y}s " " "
125 fi 161 fi
126 done 162 done
163 [[ ${found_it} -eq 0 ]] && continue
164 fi
165
166 # ok, let's show this script already !
167 printf "%20s | " ${x:0:19}
168 for y in ${mylevels} ; do
169 if [[ -L ${ROOT}etc/runlevels/${y}/${x} ]] ; then
170 echo -n "${y} "
171 else
172 printf "%${#y}s " " "
173 fi
174 done
127 echo "" 175 echo ""
128 fi
129 done 176 done
130} 177}
131 178
132check_is_root() { 179check_is_root() {
133 if [[ ${EUID} -ne 0 ]] ; then 180 if [[ ${EUID} -ne 0 ]] ; then
134 eerror "$0: must be root." 181 eerror "${argv0}: must be root to complete this operation" 1>&2
135 exit 1 182 exit 1
136 fi 183 fi
137} 184}
138 185
139if [[ $# -lt 1 ]] ; then 186export ROOT=${ROOT%/}/
187[[ ${ROOT} != "/" ]] && einfo "Working with files in root ${ROOT} ..."
188
189verbose=0
190quiet=0
191action=""
192opts=""
193check_root=0
194
195[[ ${RC_VERBOSE} == "yes" ]] && ((++verbose))
196
197while [[ -n $* ]] ; do
198 case "$1" in
199 add|-a)
200 check_root=1
201 action="add"
202 ;;
203 del|delete|-d)
204 check_root=1
205 action="del"
206 ;;
207 show|-s)
208 action="show"
209 ;;
210 help|-h|--help)
140 usage 211 usage 0
212 ;;
213 verbose|-v|--verbose)
214 ((++verbose))
215 ;;
216 quiet|-q|--quiet)
217 ((++quiet))
218 ;;
219 -*)
220 echo -e "${argv0}: Unknown option $1\n" 1>&2
221 usage 1
222 ;;
223 *)
224 opts="${opts} $1"
225 ;;
226 esac
227 shift
228done
229
230if [[ -z ${action} ]] ; then
231 eerror "${argv0}: gimme something to do!" 1>&2
141 exit 1 232 exit 1
142fi 233fi
143 234
144if [[ -n ${ROOT} ]] ; then 235[[ ${check_root} -eq 1 ]] && check_is_root
145 [[ ${ROOT:0-1} == "/" ]] && export ROOT=${ROOT:0:${#ROOT}-1} 236${action} ${opts}
146 einfo "Working with files in root ${ROOT} ..."
147fi
148
149case "$1" in
150 add|-a)
151 check_is_root
152 add "$@"
153 ;;
154 del|delete|-d)
155 check_is_root
156 del "$@"
157 ;;
158 show|-s)
159 show "$@"
160 ;;
161 *)
162 usage
163 exit 1
164 ;;
165esac
166 237
167# vim:ts=4 238# vim:ts=4

Legend:
Removed from v.251  
changed lines
  Added in v.252

  ViewVC Help
Powered by ViewVC 1.1.20