]>
Commit | Line | Data |
---|---|---|
efb675bf AM |
1 | # Functions used from inside of initrd. |
2 | ||
3 | ||
4 | # Wait for specific /dev files to appear but only one | |
5 | # time regardless how often this function is called | |
6 | # in one initrd run. Needs writable /tmp. | |
7 | wait_for_files() { | |
8 | local i=0 timeout=30 all_found not_found wff_dir fpath file | |
9 | while getopts t: opt "$@"; do | |
10 | case "$opt" in | |
11 | t) | |
12 | timeout="${OPTARG}" | |
13 | break | |
14 | ;; | |
15 | *) | |
16 | echo "$0: wait_for_files: unknown option" >&2 | |
17 | return 1 | |
18 | ;; | |
19 | esac | |
20 | done | |
21 | shift $((OPTIND - 1)) | |
22 | ||
23 | wff_dir="/tmp/wait_for_files" | |
24 | ||
25 | [ ! -d "$wff_dir" ] && mkdir "$wff_dir" | |
26 | ||
27 | echo "Waiting for files ($@) with ${timeout}s timeout" | |
28 | i=0 | |
29 | timeout=$((timeout * 4)) | |
30 | while [ $i -lt $timeout ]; do | |
31 | all_found=1 | |
32 | for file in $@; do | |
33 | fpath="$wff_dir/${file}" | |
34 | [ -e "$fpath" -o -e "$file" ] && continue | |
35 | all_found=0 | |
36 | done | |
37 | if [ $all_found -eq 1 ]; then | |
38 | return 0 | |
39 | fi | |
40 | usleep 250000 | |
41 | i=$((i + 1 )) | |
42 | done | |
43 | not_found="" | |
44 | # mark files that we have waited for | |
45 | for file in $@; do | |
46 | fpath="$wff_dir/${file}" | |
47 | [ ! -e "$fpath" ] && mkdir -p "$fpath" | |
48 | [ ! -e "$file" ] && not_found="$not_found $file" | |
49 | done | |
50 | echo "Waiting for files ($@) finished. Missing files: $not_found" | |
51 | ||
52 | return 0 | |
53 | } |