Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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