/[vps]/baselayout-vserver/branches/rc-scripts-1_6/sbin/rc-update
Gentoo

Diff of /baselayout-vserver/branches/rc-scripts-1_6/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
13note: After rc-update executes, the script dependency cache is automatically
14 updated.
15 15
16examples: 16examples:
17 rc-update add net.eth0 default 17 # ${argv0} add net.eth0 default
18 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.
19 19
20 rc-update del sysklogd 20 # ${argv0} del sysklogd
21 Deletes the sysklogd script from all runlevels. The original script 21 Deletes the sysklogd script from all runlevels. The original script
22 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/*.
23 23
24 rc-update del net.eth2 default wumpus 24 # ${argv0} del net.eth2 default wumpus
25 Delete the net.eth2 script from the default and wumpus runlevels. 25 Delete the net.eth2 script from the default and wumpus runlevels.
26 All other runlevels are unaffected. Again, the net.eth2 script 26 All other runlevels are unaffected. Again, the net.eth2 script
27 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
28 /etc/runlevels/default and /etc/runlevels/wumpus. 28 /etc/runlevels/default and /etc/runlevels/wumpus.
29 29
30 rc-update show 30 # ${argv0} show
31 Show all the available scripts and list at which runlevels they 31 Show all the available scripts and list at which runlevels they
32 will execute. 32 will execute.
33FOO 33FOO
34 exit 1 34 exit ${1:-0}
35} 35}
36 36
37add() { 37add() {
38 local ret=0
38 local x= 39 local x=
39 local myscript= 40 local myscript=
40 41
41 if [ $# -lt 3 ] 42 if [[ $# -lt 2 ]] ; then
42 then 43 eerror "Usage: ${argv0} add <script> runlevel1 [runlevel2 ...]" 1>&2
43 eerror "${0}: at least two arguments expected after \"$1\"." 44 exit 1
45 fi
46
47 [[ ${quiet} -gt 0 ]] && exec 2> /dev/null
48
49 myscript=$1
50 if [[ ! -e ${ROOT}/etc/init.d/${myscript} ]] ; then
51 eerror "${argv0}: '${ROOT}etc/init.d/${myscript}' not found; aborting" 1>&2
44 exit 1 52 exit 1
45 fi 53 fi
46 shift 54 shift
47 myscript="$1"
48 if [ ! -e "/etc/init.d/${myscript}" ]
49 then
50 eerror "$0: /etc/init.d/${myscript} not found; aborting."
51 exit 1
52 fi
53 shift
54 for x in $* 55 for x in $* ; do
55 do
56 if [ ! -e "/etc/runlevels/${x}" ] 56 if [[ ! -e ${ROOT}etc/runlevels/${x} ]] ; then
57 then
58 ewarn "runlevel ${x} not found; skipping" 57 ewarn "runlevel '${x}' not found; skipping" 1>&2
58 ((++ret))
59 continue 59 continue
60 fi 60 fi
61 if [ -L "/etc/runlevels/${x}/${myscript}" ] 61 if [[ -L ${ROOT}etc/runlevels/${x}/${myscript} ]] ; then
62 then
63 ewarn "${myscript} already installed in runlevel ${x}; skipping" 62 ewarn "${myscript} already installed in runlevel '${x}'; skipping" 1>&2
64 continue 63 continue
65 fi 64 fi
66 if [ ! -x "/etc/init.d/${myscript}" ] 65 if [[ ! -x ${ROOT}etc/init.d/${myscript} ]] ; then
67 then
68 ewarn "${myscript} not executable; skipping" 66 ewarn "${myscript} not executable; skipping" 1>&2
67 ((++ret))
69 continue 68 continue
70 fi 69 fi
71 ln -snf "/etc/init.d/${myscript}" "/etc/runlevels/${x}/${myscript}" 70 ln -snf "/etc/init.d/${myscript}" "${ROOT}etc/runlevels/${x}/${myscript}"
72 if [ "$?" -ne 0 ] 71 if [[ $? -ne 0 ]] ; then
73 then
74 eerror "$0: failed to add ${myscript} to ${x}." 72 eerror "${argv0}: failed to add '${myscript}' to '${x}'" 1>&2
75 exit 1 73 ((++ret))
74 continue
76 fi 75 fi
77 regen=1
78 einfo "${myscript} added to runlevel ${x}" 76 einfo "${myscript} added to runlevel ${x}"
79 done 77 done
78
79 return ${ret}
80} 80}
81 81
82del() { 82del() {
83 local x= 83 local x=
84 local mylevels= 84 local mylevels=
85 local myscript= 85 local myscript=
86 local remlevels= 86 local remlevels=
87 87
88 if [ $# -lt 2 ] 88 if [[ $# -lt 1 ]] ; then
89 then 89 eerror "Usage: ${argv0} del <script> [runlevel1 ...]" 1>&2
90 eerror "$0: at least one argument expected after \"$1\"."
91 exit 1 90 exit 1
92 fi 91 fi
93 shift 92
93 [[ ${quiet} -gt 0 ]] && exec 2> /dev/null
94
94 myscript=$1 95 myscript=$1
95 shift 96 shift
96 if [ $# -eq 0 ] 97 if [[ $# -eq 0 ]] ; then
97 then
98 mylevels="`( cd /etc/runlevels; ls )`" 98 mylevels=$(cd "${ROOT}"etc/runlevels/; ls)
99 else 99 else
100 mylevels="$*" 100 mylevels="$*"
101 fi 101 fi
102 remlevels="" 102 remlevels=""
103 for x in ${mylevels} 103 for x in ${mylevels} ; do
104 do 104 # -e will return false for broken symlinks so we need the extra -L
105 if [ -L "/etc/runlevels/${x}/${myscript}" ] 105 [[ ! -L ${ROOT}etc/runlevels/${x}/${myscript} ]] && \
106 then 106 [[ ! -e ${ROOT}etc/runlevels/${x}/${myscript} ]] && continue
107 regen=1 107
108 rm -f "/etc/runlevels/${x}/${myscript}"
109 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
110 fi 111 fi
112 rm -f "${ROOT}etc/runlevels/${x}/${myscript}"
113 ((ret+=$?))
111 done 114 done
112 if [ "${remlevels}" = "" ] 115 if [[ -z ${remlevels} ]] ; then
113 then
114 einfo "${myscript} not found in any of the specified runlevels." 116 ewarn "'${myscript}' not found in any of the specified runlevels" 1>&2
115 else 117 elif [[ ${quiet} -eq 0 ]] ; then
116 einfo "${myscript} removed from the following runlevels:${remlevels}" 118 einfo "'${myscript}' removed from the following runlevels:${remlevels}"
117 fi 119 fi
120 return ${ret}
118} 121}
119 122
120show() { 123show() {
121 local x= 124 local x=
122 local y= 125 local y=
123 local mylevels= 126 local mylevels=
124 local myscripts= 127 local myscripts=
125 128
126 shift 129 shopt -s nullglob
127 if [ $# -eq 0 ] 130 if [[ $# -eq 0 ]] ; then
128 then
129 mylevels="`( cd /etc/runlevels; ls )`" 131 mylevels=$(cd "${ROOT}"etc/runlevels/; ls)
130 else 132 else
131 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
132 fi 138 fi
133 myscripts="`( cd /etc/init.d; ls )`" 139 myscripts=$(cd "${ROOT}"etc/init.d; ls)
134 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
148
135 for x in ${myscripts} 149 for x in ${myscripts} ; do
136 do 150 # skip *.sh scripts as they are helpers, not init.d scripts
137 if [ "${x%%.sh}" = "${x}" ] 151 [[ ${x} == *.sh ]] && continue
138 then 152
139 printf "%20s | " ${x:0:19} 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
140 for y in ${mylevels} 157 for y in ${mylevels} ; do
141 do
142 if [ -L "/etc/runlevels/${y}/${x}" ] 158 if [[ -L ${ROOT}etc/runlevels/${y}/${x} ]] ; then
143 then 159 found_it=1
144 echo -n "${y} " 160 break
145 else
146 printf "%${#y}s " " "
147 fi 161 fi
148 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
149 echo "" 175 echo ""
150 fi
151 done 176 done
152} 177}
153 178
154check_is_root() { 179check_is_root() {
155 if [[ ${EUID} -ne 0 ]] ; then 180 if [[ ${EUID} -ne 0 ]] ; then
156 eerror "$0: must be root." 181 eerror "${argv0}: must be root to complete this operation" 1>&2
157 exit 1 182 exit 1
158 fi 183 fi
159} 184}
160 185
161if [ $# -lt 1 ] 186export ROOT=${ROOT%/}/
162then 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)
163 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
164 exit 1 232 exit 1
165fi 233fi
166 234
167regen=0 235[[ ${check_root} -eq 1 ]] && check_is_root
168 236${action} ${opts}
169case "$1" in
170 add|-a)
171 check_is_root
172 add "$@"
173 ;;
174 del|delete|-d)
175 check_is_root
176 del "$@"
177 ;;
178 show|-s)
179 show "$@"
180 ;;
181 *)
182 usage
183 exit 1
184 ;;
185esac
186
187if [ "${regen}" -eq 1 ]
188then
189 # depscan doesnt actually read runlevels so no point ...
190 #/sbin/depscan.sh
191 einfo "rc-update complete."
192fi
193
194 237
195# 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