Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 227 lines (171 sloc) 5.572 kB
75052c2 @falconindy initial commit
authored
1 #!/bin/bash
2
3 #
4 # geninit public API
5 #
6 # To authors of builders:
7 # these functions should be used over other functions in geninit such as
8 # __add_file, which generally have no awareness of things such as $basedir or
9 # $kernver.
10 #
11
12 shopt -s extglob
13
6438c9a @falconindy cleanup API, flesh out comments
authored
14 add_dir() { # {{{
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
15 # add a directory (with parents)
6438c9a @falconindy cleanup API, flesh out comments
authored
16 # $1: absolute path on initcpio
75052c2 @falconindy initial commit
authored
17 # $2: mode (optional, defaults to 755)
18
19 (( ! $# )) && return 1 # NOOP
20
21 local path=$1 mode=${2:-755}
22
23 __add_dir "$path" "$mode"
6438c9a @falconindy cleanup API, flesh out comments
authored
24 } # }}}
75052c2 @falconindy initial commit
authored
25
26 add_file() { # {{{
27 # add a regular file. no parsing done.
28 # $1: source on disk
29 # $2: destination on initcpio (optional: assumes same as source)
30
ce83c05 @falconindy cleanup API documentation
authored
31 (( $# )) || return 1 # NOOP
75052c2 @falconindy initial commit
authored
32
33 # determine source and destination
9ca666e @falconindy more fixes for adventures in $basedir land
authored
34 local src= dest=${2:-$1} mode=
75052c2 @falconindy initial commit
authored
35
9ca666e @falconindy more fixes for adventures in $basedir land
authored
36 if [[ ${1:0:1} == @ ]]; then # assert an absolute path
37 src=${1:1}
38 else
39 src=$basedir$1
75052c2 @falconindy initial commit
authored
40 fi
41
9ca666e @falconindy more fixes for adventures in $basedir land
authored
42 [[ -f $src ]] || { err "$src: No such file"; return 1; }
43
75052c2 @falconindy initial commit
authored
44 mode=$(stat -c %a "$src")
45 if [[ -z $mode ]]; then
f404baa @falconindy add_file: call add_path_to_file before __add_file
authored
46 err "failed to stat file: \`$src'"
75052c2 @falconindy initial commit
authored
47 return 1
48 fi
49
e37a454 @falconindy avoid using public API from within the API itself
authored
50 __add_file "${dest#$basedir}" "$src" "$mode"
75052c2 @falconindy initial commit
authored
51 } # }}}
52
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
53 add_symlink() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
54 # add a symlink
9c28450 @falconindy reverse parameters to add_symlink to model common form of ls invocation
authored
55 # $1: target of $2
56 # $2: name on initcpio
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
57
ce83c05 @falconindy cleanup API documentation
authored
58 (( $# == 2 )) || return 1
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
59
c8af038 @falconindy fix regression spotted by commit 809b5b2
authored
60 add_path_to_file "$1"
9c28450 @falconindy reverse parameters to add_symlink to model common form of ls invocation
authored
61 __add_slink "$2" "$1"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
62 } # }}}
75052c2 @falconindy initial commit
authored
63
64 add_path_to_file() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
65 # add the path leading up to a file
66 # $1: filename with full path
ce83c05 @falconindy cleanup API documentation
authored
67
68 (( $# )) || return 1
69
70 add_dir "${1%/*}"
75052c2 @falconindy initial commit
authored
71 } # }}}
72
73 add_module() { # {{{
ce83c05 @falconindy cleanup API documentation
authored
74 # add a kernel module with dependencies
6438c9a @falconindy cleanup API, flesh out comments
authored
75 # $1: kernel module by name (no path, with or without extension)
75052c2 @falconindy initial commit
authored
76
ce83c05 @falconindy cleanup API documentation
authored
77 (( $# )) || return 1
78
75052c2 @falconindy initial commit
authored
79 local -a firmware moddeps
e37a454 @falconindy avoid using public API from within the API itself
authored
80 local modpath= module= mod= dep=
75052c2 @falconindy initial commit
authored
81
82 module=${1%.ko?(.gz)}
83
84 if [[ -z $basedir ]]; then # fast path =)
85 read -rd '' modpath < <(kmodinfo -n "$module" 2>/dev/null)
86 else # slow path =(
e37a454 @falconindy avoid using public API from within the API itself
authored
87 read -rd '' modpath < <(find "$basedir$moduledir/kernel" -type f \
88 -name "$module.ko" -o -name "$module.ko.gz" -print0)
75052c2 @falconindy initial commit
authored
89 fi
90
2144b1a @falconindy remove debag statement
authored
91 [[ -z $modpath ]] && return 1
75052c2 @falconindy initial commit
authored
92
e37a454 @falconindy avoid using public API from within the API itself
authored
93 __add_file "${modpath#$basedir}" "$modpath" 644 || return 1
75052c2 @falconindy initial commit
authored
94
95 # grab firmware
96 IFS=',' read -rd '' -a firmware < <(kmodinfo -F firmware "$modpath")
97 for fw in "${firmware[@]}"; do
e37a454 @falconindy avoid using public API from within the API itself
authored
98 __add_file "/lib/firmware/$fw" "/lib/firmware/$fw" 644
75052c2 @falconindy initial commit
authored
99 done
100
6438c9a @falconindy cleanup API, flesh out comments
authored
101 # resolve deps
75052c2 @falconindy initial commit
authored
102 IFS=',' read -rd '' -a moddeps < <(kmodinfo -F depends "$modpath")
103 for dep in "${moddeps[@]}"; do
104 add_module "$dep"
105 done
106
107 # add in any quirks
f2136d7 @falconindy add quirks file for quirky modules
authored
108 for mod in ${modquirks[$module]}; do
109 add_module "$mod"
110 done
75052c2 @falconindy initial commit
authored
111
112 return 0
113 } # }}}
114
115 add_binary() { # {{{
ce83c05 @falconindy cleanup API documentation
authored
116 # add a binary file with .so depends
117 # $1: path to binary
9ca666e @falconindy more fixes for adventures in $basedir land
authored
118 # $2: destination on initcpio (optional: assumes same as source)
ce83c05 @falconindy cleanup API documentation
authored
119
120 (( $# )) || return 1
75052c2 @falconindy initial commit
authored
121
122 local -a sodeps
9ca666e @falconindy more fixes for adventures in $basedir land
authored
123 local binary= dest= mode= sodep= resolved= dirname=
124
125 if [[ ${1:0:1} == @ ]]; then # assert an absolute path
126 binary=${1:1}
127 else
128 binary=$basedir$1
129 fi
130
131 [[ -f $binary ]] || { err "$binary not found"; return 1; }
e37a454 @falconindy avoid using public API from within the API itself
authored
132
9ca666e @falconindy more fixes for adventures in $basedir land
authored
133 dest=${2:-$binary}
e37a454 @falconindy avoid using public API from within the API itself
authored
134 mode=$(stat -c %a "$binary")
517c50b @falconindy reroute non-binaries passed to add_binary
authored
135
136 if ! "$ld_so" --verify "$binary"; then
137 # this isn't a binary!
e37a454 @falconindy avoid using public API from within the API itself
authored
138 __add_file "${binary#$basedir}" "$binary" "$mode"
517c50b @falconindy reroute non-binaries passed to add_binary
authored
139 return $? # return with add_file's return val
140 fi
75052c2 @falconindy initial commit
authored
141
9ca666e @falconindy more fixes for adventures in $basedir land
authored
142 IFS=$'\n' read -rd '' -a sodeps < <(ldd "$binary" |\
75052c2 @falconindy initial commit
authored
143 sed -n '2,$s#.\+=>[[:blank:]]\+\([^ ]\+\) (.\+#\1#p')
144
145 for sodep in "${sodeps[@]}"; do
146 [[ -e $sodep ]] || continue
147
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
148 if [[ -f $sodep ]]; then # but wait! a symlink is a regular file! FFFUUUUU
e37a454 @falconindy avoid using public API from within the API itself
authored
149 mode=$(stat -c %a "$sodep")
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
150 if [[ ! -L $sodep ]]; then
e37a454 @falconindy avoid using public API from within the API itself
authored
151 __add_file "$sodep"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
152 else
e37a454 @falconindy avoid using public API from within the API itself
authored
153 resolved=$(readlink -e "$basedir$sodep")
154 dirname=${resolved%/*}
155 __add_dir "${dirname#$basedir}" 755
156 __add_slink "$sodep" "${resolved#$basedir}"
9ca666e @falconindy more fixes for adventures in $basedir land
authored
157 __add_file "${resolved#$basedir}" "$resolved" 755
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
158 fi
159 fi
75052c2 @falconindy initial commit
authored
160 done
161
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
162 # the binary itself
9ca666e @falconindy more fixes for adventures in $basedir land
authored
163 __add_file "${dest#$basedir}" "${binary#$basedir}" 755
75052c2 @falconindy initial commit
authored
164
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
165 # we always need the big bad linker
74585f2 @falconindy Don't try to add /lib/ld.so every add_binary call
authored
166 if [[ ! -e $buildroot$ld_so ]]; then
167 resolved=$(readlink -e "$basedir$ld_so")
168 __add_slink "$ld_so" "$resolved"
169 __add_file "${resolved#$basedir}" "$resolved" 755
170 fi
75052c2 @falconindy initial commit
authored
171 } # }}}
172
ce83c05 @falconindy cleanup API documentation
authored
173 add_pipe() { # {{{
174 # add a fifo
175 # $1: path of pipe
176 # $2: mode (optional, defaults to 644)
177
178 (( $# )) || return 1
179
180 __add_pipe "$1" "${2:-644}"
181 } # }}}
75052c2 @falconindy initial commit
authored
182
f1b4dae @falconindy introduce use_hookscript public API call
authored
183 use_hookscript() { # {{{
184 # specifies that a builder should install a hookscript
ce83c05 @falconindy cleanup API documentation
authored
185 # "$builder" comes from the builder parsing loop in main
9fd7d2b @falconindy fix grep call in add_driver_classes one more time
authored
186
f1b4dae @falconindy introduce use_hookscript public API call
authored
187 [[ $builder ]] || return 1
188 add_file "$basedir$_sharedir/hooks/$builder" "/hooks/$builder"
189 } # }}}
190
75052c2 @falconindy initial commit
authored
191 add_driver_classes() { # {{{
7b78689 @falconindy implement module filtering for add_driver_classes
authored
192 # add a class of drivers, as a dir relative to /lib/modules/$kernver/kernel.
193 # if you need to block addition of modules, you can add glob patterns to the
194 # 'modulefilter' array. They must be quoted and the filter is cleared after
195 # parsing of every builder.
ce83c05 @falconindy cleanup API documentation
authored
196 # $@: directories
197
75052c2 @falconindy initial commit
authored
198 local -a mods
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
199 local -i ret=0
6438c9a @falconindy cleanup API, flesh out comments
authored
200 local mod=
75052c2 @falconindy initial commit
authored
201
202 # find modules by class (e.g. drivers/ata)
e37a454 @falconindy avoid using public API from within the API itself
authored
203 pushd "$basedir$moduledir/kernel" &>/dev/null
75052c2 @falconindy initial commit
authored
204 while read -rd '' mod; do
205 mod=${mod##*/}
206 mods+=("${mod%.ko?(.gz)}")
2af2627 @falconindy remove extra call to add_path_to_file
authored
207 done < <(find "$@" -type f \( -name '*.ko' -o -name '*.ko.gz' \) -print0 2>/dev/null)
75052c2 @falconindy initial commit
authored
208 popd &>/dev/null
209
210 # only add what autodetect found if we have a module cache
ee088cb @falconindy standardize -|_ in autodetected mod pruning
authored
211 [[ -s $autodetect_cache ]] &&
afb7ec8 @falconindy supply grep with -F to avoid interpretation
authored
212 IFS=$'\n' read -rd '' -a mods < <(grep -xFf <(printf '%s\n' "${mods[@]//-/_}") "$autodetect_cache")
75052c2 @falconindy initial commit
authored
213
214 for mod in "${mods[@]}"; do
7b78689 @falconindy implement module filtering for add_driver_classes
authored
215 # check for filter match
216 for filter in "${modulefilter[@]}"; do
217 [[ $mod = $filter ]] && continue 2
218 done
219
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
220 add_module "$mod" && (( ++ret ))
75052c2 @falconindy initial commit
authored
221 done
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
222
223 # return 0 on modules added, else 1
224 return $(( ! ret ))
75052c2 @falconindy initial commit
authored
225 } # }}}
226
Something went wrong with that request. Please try again.