add option --ignore-kernel-modules
[dracut.git] / modules.d / 99base / dracut-lib.sh
blob06816723d3857a92e37456b136aaa8b4fc371745
2 # returns OK if $1 contains $2
3 strstr() {
4 [ "${1#*$2*}" != "$1" ]
7 getarg() {
8 set +x
9 local o line val
10 if [ -z "$CMDLINE" ]; then
11 if [ -e /etc/cmdline ]; then
12 while read line; do
13 CMDLINE_ETC="$CMDLINE_ETC $line";
14 done </etc/cmdline;
16 read CMDLINE </proc/cmdline;
17 CMDLINE="$CMDLINE $CMDLINE_ETC"
19 for o in $CMDLINE; do
20 if [ "$o" = "$1" ]; then
21 [ "$RDDEBUG" = "yes" ] && set -x;
22 return 0;
24 [ "${o%%=*}" = "${1%=}" ] && val=${o#*=};
25 done
26 if [ -n "$val" ]; then
27 echo $val;
28 [ "$RDDEBUG" = "yes" ] && set -x;
29 return 0;
31 [ "$RDDEBUG" = "yes" ] && set -x
32 return 1
35 getargs() {
36 set +x
37 local o line found
38 if [ -z "$CMDLINE" ]; then
39 if [ -e /etc/cmdline ]; then
40 while read line; do
41 CMDLINE_ETC="$CMDLINE_ETC $line";
42 done </etc/cmdline;
44 read CMDLINE </proc/cmdline;
45 CMDLINE="$CMDLINE $CMDLINE_ETC"
47 for o in $CMDLINE; do
48 if [ "$o" = "$1" ]; then
49 [ "$RDDEBUG" = "yes" ] && set -x;
50 return 0;
52 if [ "${o%%=*}" = "${1%=}" ]; then
53 echo -n "${o#*=} ";
54 found=1;
56 done
57 if [ -n "$found" ]; then
58 [ "$RDDEBUG" = "yes" ] && set -x
59 return 0;
61 [ "$RDDEBUG" = "yes" ] && set -x
62 return 1;
65 # Prints value of given option. If option is a flag and it's present,
66 # it just returns 0. Otherwise 1 is returned.
67 # $1 = options separated by commas
68 # $2 = option we are interested in
70 # Example:
71 # $1 = cipher=aes-cbc-essiv:sha256,hash=sha256,verify
72 # $2 = hash
73 # Output:
74 # sha256
75 getoptcomma() {
76 local line=",$1,"; local opt="$2"; local tmp
78 case "${line}" in
79 *,${opt}=*,*)
80 tmp="${line#*,${opt}=}"
81 echo "${tmp%%,*}"
82 return 0
84 *,${opt},*) return 0 ;;
85 esac
87 return 1
90 setdebug() {
91 if [ -z "$RDDEBUG" ]; then
92 if [ -e /proc/cmdline ]; then
93 RDDEBUG=no
94 if getarg rdinitdebug || getarg rdnetdebug; then
95 RDDEBUG=yes
98 export RDDEBUG
100 [ "$RDDEBUG" = "yes" ] && set -x
103 setdebug
105 source_all() {
106 local f
107 [ "$1" ] && [ -d "/$1" ] || return
108 for f in "/$1"/*.sh; do [ -e "$f" ] && . "$f"; done
111 check_finished() {
112 local f
113 for f in /initqueue-finished/*.sh; do { [ -e "$f" ] && ( . "$f" ) ; } || return 1 ; done
114 return 0
117 source_conf() {
118 local f
119 [ "$1" ] && [ -d "/$1" ] || return
120 for f in "/$1"/*.conf; do [ -e "$f" ] && . "$f"; done
123 die() {
125 echo "<1>dracut: FATAL: $@";
126 echo "<1>dracut: Refusing to continue";
127 } > /dev/kmsg
130 echo "dracut: FATAL: $@";
131 echo "dracut: Refusing to continue";
132 } >&2
134 exit 1
137 check_quiet() {
138 if [ -z "$DRACUT_QUIET" ]; then
139 DRACUT_QUIET="yes"
140 getarg rdinfo && DRACUT_QUIET="no"
141 getarg quiet || DRACUT_QUIET="yes"
145 warn() {
146 check_quiet
147 echo "<4>dracut Warning: $@" > /dev/kmsg
148 [ "$DRACUT_QUIET" != "yes" ] && \
149 echo "dracut Warning: $@" >&2
152 info() {
153 check_quiet
154 echo "<6>dracut: $@" > /dev/kmsg
155 [ "$DRACUT_QUIET" != "yes" ] && \
156 echo "dracut: $@"
159 vinfo() {
160 while read line; do
161 info $line;
162 done
165 check_occurances() {
166 # Count the number of times the character $ch occurs in $str
167 # Return 0 if the count matches the expected number, 1 otherwise
168 local str="$1"
169 local ch="$2"
170 local expected="$3"
171 local count=0
173 while [ "${str#*$ch}" != "${str}" ]; do
174 str="${str#*$ch}"
175 count=$(( $count + 1 ))
176 done
178 [ $count -eq $expected ]
181 incol2() {
182 local dummy check;
183 local file="$1";
184 local str="$2";
186 [ -z "$file" ] && return;
187 [ -z "$str" ] && return;
189 while read dummy check restofline; do
190 [ "$check" = "$str" ] && return 0
191 done < $file
192 return 1
195 udevsettle() {
196 [ -z "$UDEVVERSION" ] && UDEVVERSION=$(udevadm --version)
198 if [ $UDEVVERSION -ge 143 ]; then
199 udevadm settle --exit-if-exists=/initqueue/work $settle_exit_if_exists
200 else
201 udevadm settle --timeout=30
205 udevproperty() {
206 [ -z "$UDEVVERSION" ] && UDEVVERSION=$(udevadm --version)
208 if [ $UDEVVERSION -ge 143 ]; then
209 for i in "$@"; do udevadm control --property=$i; done
210 else
211 for i in "$@"; do udevadm control --env=$i; done
215 wait_for_if_up() {
216 local cnt=0
217 while [ $cnt -lt 20 ]; do
218 li=$(ip link show $1)
219 [ -z "${li##*state UP*}" ] && return 0
220 sleep 0.1
221 cnt=$(($cnt+1))
222 done
223 return 1
226 # root=nfs:[<server-ip>:]<root-dir>[:<nfs-options>]
227 # root=nfs4:[<server-ip>:]<root-dir>[:<nfs-options>]
228 nfsroot_to_var() {
229 # strip nfs[4]:
230 local arg="$@:"
231 nfs="${arg%%:*}"
232 arg="${arg##$nfs:}"
234 # check if we have a server
235 if strstr "$arg" ':/*' ; then
236 server="${arg%%:/*}"
237 arg="/${arg##*:/}"
240 path="${arg%%:*}"
242 # rest are options
243 options="${arg##$path}"
244 # strip leading ":"
245 options="${options##:}"
246 # strip ":"
247 options="${options%%:}"
249 # Does it really start with '/'?
250 [ -n "${path%%/*}" ] && path="error";
252 #Fix kernel legacy style separating path and options with ','
253 if [ "$path" != "${path#*,}" ] ; then
254 options=${path#*,}
255 path=${path%%,*}
259 ip_to_var() {
260 local v=${1}:
261 local i
262 set --
263 while [ -n "$v" ]; do
264 if [ "${v#\[*:*:*\]:}" != "$v" ]; then
265 # handle IPv6 address
266 i="${v%%\]:*}"
267 i="${i##\[}"
268 set -- "$@" "$i"
269 v=${v#\[$i\]:}
270 else
271 set -- "$@" "${v%%:*}"
272 v=${v#*:}
274 done
276 unset ip srv gw mask hostname dev autoconf
277 case $# in
278 0) autoconf="error" ;;
279 1) autoconf=$1 ;;
280 2) dev=$1; autoconf=$2 ;;
281 *) ip=$1; srv=$2; gw=$3; mask=$4; hostname=$5; dev=$6; autoconf=$7 ;;
282 esac
285 # Evaluate command for UUIDs either given as arguments for this function or all
286 # listed in /dev/disk/by-uuid. UUIDs doesn't have to be fully specified. If
287 # beginning is given it is expanded to all matching UUIDs. To pass full UUID
288 # to your command use '${full_uuid}'. Remember to escape '$'!
290 # $1 = command to be evaluated
291 # $2 = list of UUIDs separated by space
293 # The function returns after *first successful evaluation* of the given command
294 # with status 0. If evaluation fails for every UUID function returns with
295 # status 1.
297 # Example:
298 # foreach_uuid_until "mount -U \${full_uuid} /mnt; echo OK; umount /mnt" \
299 # "01234 f512 a235567f-12a3-c123-a1b1-01234567abcb"
300 foreach_uuid_until() (
301 cd /dev/disk/by-uuid
303 local cmd="$1"; shift; local uuids_list="$*"
304 local uuid; local full_uuid
306 [ -n "${cmd}" ] || return 1
308 for uuid in ${uuids_list:-*}; do
309 for full_uuid in ${uuid}*; do
310 [ -e "${full_uuid}" ] || continue
311 eval ${cmd} && return 0
312 done
313 done
315 return 1