Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 245 lines (187 sloc) 6.085 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
58c9f9f @falconindy go on a quoting spree.
authored
34 if [[ "${1:0:1}" == @ ]]; then # assert an absolute path
9ca666e @falconindy more fixes for adventures in $basedir land
authored
35 src=${1:1}
36 else
37 src=$basedir$1
75052c2 @falconindy initial commit
authored
38 fi
39
58c9f9f @falconindy go on a quoting spree.
authored
40 [[ -f "$src" ]] || { error "$src: No such file"; return 1; }
9ca666e @falconindy more fixes for adventures in $basedir land
authored
41
75052c2 @falconindy initial commit
authored
42 mode=$(stat -c %a "$src")
58c9f9f @falconindy go on a quoting spree.
authored
43 if [[ -z "$mode" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
44 error "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
e800382 @falconindy make fewer calls to modinfo
authored
77 local -a deps
78 local modpath= module= mod= dep= field= value=
75052c2 @falconindy initial commit
authored
79
52dcddc @falconindy Simplify module discovery, knowing that . is illegal
authored
80 module=${1%%.*}
75052c2 @falconindy initial commit
authored
81
e800382 @falconindy make fewer calls to modinfo
authored
82 while IFS=':= ' read -r -d '' field value; do
83 case "$field" in
84 filename)
85 modpath=$value
86 ;;
87 depends)
88 IFS=',' read -r -a deps <<< "$value"
89 for dep in "${deps[@]}"; do
90 add_module "$dep"
91 done
92 ;;
93 firmware)
94 if [[ -e "$basedir/lib/firmware/$value" ]]; then
95 __add_file "/lib/firmware/$value" "$basedir/lib/firmware/$value"
96 fi
97 ;;
98 esac
99 done < <(modinfo "$module" -b "$basedir" -0k $kernver 2>/dev/null)
100
101 if [[ -z $modpath ]]; then
72866c6 @falconindy error on module not found in API
authored
102 error "Module not found: $module"
103 return 1
104 fi
75052c2 @falconindy initial commit
authored
105
c5b484a @falconindy api: ignore EEXIST return in add_module
authored
106 __add_file "${modpath#$basedir}" "$modpath" 644
107 [[ $? = @(0|$EEXIST) ]] || return 1
75052c2 @falconindy initial commit
authored
108
109 # add in any quirks
f2136d7 @falconindy add quirks file for quirky modules
authored
110 for mod in ${modquirks[$module]}; do
111 add_module "$mod"
112 done
75052c2 @falconindy initial commit
authored
113
114 return 0
115 } # }}}
116
117 add_binary() { # {{{
ce83c05 @falconindy cleanup API documentation
authored
118 # add a binary file with .so depends
119 # $1: path to binary
9ca666e @falconindy more fixes for adventures in $basedir land
authored
120 # $2: destination on initcpio (optional: assumes same as source)
ce83c05 @falconindy cleanup API documentation
authored
121
122 (( $# )) || return 1
75052c2 @falconindy initial commit
authored
123
124 local -a sodeps
b19f848 @falconindy refactor sodep parsing by reimplementing ldd
authored
125 local regex= binary= dest= mode= sodep= resolved= dirname=
9ca666e @falconindy more fixes for adventures in $basedir land
authored
126
58c9f9f @falconindy go on a quoting spree.
authored
127 if [[ "${1:0:1}" == @ ]]; then # assert an absolute path
9ca666e @falconindy more fixes for adventures in $basedir land
authored
128 binary=${1:1}
129 else
130 binary=$basedir$1
131 fi
132
58c9f9f @falconindy go on a quoting spree.
authored
133 [[ -f "$binary" ]] || { error "$binary not found"; return 1; }
e37a454 @falconindy avoid using public API from within the API itself
authored
134
9ca666e @falconindy more fixes for adventures in $basedir land
authored
135 dest=${2:-$binary}
e37a454 @falconindy avoid using public API from within the API itself
authored
136 mode=$(stat -c %a "$binary")
517c50b @falconindy reroute non-binaries passed to add_binary
authored
137
d507fd0 @falconindy api: condense logic. always add, return when its not a binary
authored
138 # the file itself
139 __add_file "$dest" "$binary" "$mode"
75052c2 @falconindy initial commit
authored
140
d507fd0 @falconindy api: condense logic. always add, return when its not a binary
authored
141 "$ld_so" --verify "$binary" &>/dev/null || return # not a binary!
a04ee4e @falconindy dont add deps for a binary if it cant be successfully added to the image
authored
142
b19f848 @falconindy refactor sodep parsing by reimplementing ldd
authored
143 # resolve sodeps
af9ae20 @falconindy filter harder on __ldd output
authored
144 regex='^[[:space:]]*[^/].+ => (.*) \(.*\)'
b19f848 @falconindy refactor sodep parsing by reimplementing ldd
authored
145 while read line; do
58c9f9f @falconindy go on a quoting spree.
authored
146 [[ "$line" =~ $regex ]] && sodep="${BASH_REMATCH[1]}" || continue
75052c2 @falconindy initial commit
authored
147
58c9f9f @falconindy go on a quoting spree.
authored
148 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
149 if [[ ! -L $sodep ]]; then
b19f848 @falconindy refactor sodep parsing by reimplementing ldd
authored
150 __add_file "$sodep" "$basedir$sodep" "$(stat -c %a "$sodep")"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
151 else
e37a454 @falconindy avoid using public API from within the API itself
authored
152 resolved=$(readlink -e "$basedir$sodep")
153 dirname=${resolved%/*}
154 __add_dir "${dirname#$basedir}" 755
155 __add_slink "$sodep" "${resolved#$basedir}"
9ca666e @falconindy more fixes for adventures in $basedir land
authored
156 __add_file "${resolved#$basedir}" "$resolved" 755
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
157 fi
158 fi
b19f848 @falconindy refactor sodep parsing by reimplementing ldd
authored
159 done < <(__ldd "$binary")
75052c2 @falconindy initial commit
authored
160
161 } # }}}
162
ce83c05 @falconindy cleanup API documentation
authored
163 add_pipe() { # {{{
164 # add a fifo
165 # $1: path of pipe
166 # $2: mode (optional, defaults to 644)
167
168 (( $# )) || return 1
169
b709a97 @falconindy call add_path_to_file prior to __add_pipe
authored
170 add_path_to_file "$1"
ce83c05 @falconindy cleanup API documentation
authored
171 __add_pipe "$1" "${2:-644}"
172 } # }}}
75052c2 @falconindy initial commit
authored
173
f1b4dae @falconindy introduce use_hookscript public API call
authored
174 use_hookscript() { # {{{
175 # specifies that a builder should install a hookscript
ce83c05 @falconindy cleanup API documentation
authored
176 # "$builder" comes from the builder parsing loop in main
4d2713d @falconindy allow specifying a alternate name for the hookscript
authored
177 # $1: script name (optional, defaults to the name of the builder)
178 local script=${1:-$builder}
9fd7d2b @falconindy fix grep call in add_driver_classes one more time
authored
179
4d2713d @falconindy allow specifying a alternate name for the hookscript
authored
180 if [[ ! -f $basedir$_sharedir/hooks/$script ]]; then
181 error "unable to find hook script: $script"
182 return 1
183 fi
184
9953e57 @falconindy fix reversed arguments in use_hookscript
authored
185 __add_file "/hooks/$script" "$basedir$_sharedir/hooks/$script" 755
f1b4dae @falconindy introduce use_hookscript public API call
authored
186 } # }}}
187
5fccc08 @falconindy api: add basic method for adding mount hooks
authored
188 use_mounthook() { # {{{
189 # specifies that a builder should install a custom mount handler.
190
191 local mounthook=${1:-$builder}
192
193 if [[ ! -f $basedir$_sharedir/mount/$mounthook ]]; then
194 error "unable to find mount hook: $mounthook"
195 return 1
196 fi
197
198 __add_file "/mount/$mounthook" "$basedir$_sharedir/mount/$mounthook" 755
199 } # }}}
200
e735fbc @falconindy change add_driver_classes -> add_checked_modules, add add_all_modules
authored
201 add_checked_modules() { # {{{
202 # add a class of modules, as a dir relative to /lib/modules/$kernver/kernel.
7b78689 @falconindy implement module filtering for add_driver_classes
authored
203 # if you need to block addition of modules, you can add glob patterns to the
7682d99 @falconindy modulefilter => MODFILTER
authored
204 # 'MODFILTER' array within your builder. They must be quoted and the
46ccd58 @falconindy cleanup API comments
authored
205 # filter is cleared after parsing of each builder.
ce83c05 @falconindy cleanup API documentation
authored
206 # $@: directories
207
75052c2 @falconindy initial commit
authored
208 local -a mods
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
209 local -i ret=0
6438c9a @falconindy cleanup API, flesh out comments
authored
210 local mod=
75052c2 @falconindy initial commit
authored
211
212 # find modules by class (e.g. drivers/ata)
e37a454 @falconindy avoid using public API from within the API itself
authored
213 pushd "$basedir$moduledir/kernel" &>/dev/null
75052c2 @falconindy initial commit
authored
214 while read -rd '' mod; do
52dcddc @falconindy Simplify module discovery, knowing that . is illegal
authored
215 mods+=("${mod%%.*}")
29583f3 @falconindy api: leverage gnu find's -printf flag
authored
216 done < <(find "$@" -type f -name '*.ko*' -printf '%f\0' 2>/dev/null)
75052c2 @falconindy initial commit
authored
217 popd &>/dev/null
218
219 # only add what autodetect found if we have a module cache
58c9f9f @falconindy go on a quoting spree.
authored
220 [[ -s "$autodetect_cache" && -z "$NOAUTO" ]] &&
afb7ec8 @falconindy supply grep with -F to avoid interpretation
authored
221 IFS=$'\n' read -rd '' -a mods < <(grep -xFf <(printf '%s\n' "${mods[@]//-/_}") "$autodetect_cache")
75052c2 @falconindy initial commit
authored
222
223 for mod in "${mods[@]}"; do
7b78689 @falconindy implement module filtering for add_driver_classes
authored
224 # check for filter match
7682d99 @falconindy modulefilter => MODFILTER
authored
225 for filter in "${MODFILTER[@]}"; do
58c9f9f @falconindy go on a quoting spree.
authored
226 [[ "$mod" == $filter ]] && continue 2
7b78689 @falconindy implement module filtering for add_driver_classes
authored
227 done
228
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
229 add_module "$mod" && (( ++ret ))
75052c2 @falconindy initial commit
authored
230 done
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
231
232 # return 0 on modules added, else 1
233 return $(( ! ret ))
75052c2 @falconindy initial commit
authored
234 } # }}}
235
e735fbc @falconindy change add_driver_classes -> add_checked_modules, add add_all_modules
authored
236 add_all_modules() { # {{{
237 # add a class of modules, as a dir relative to /lib/modules/$kernver/kernel.
238 # this function honors 'MODFILTER', but does not check against the autodetected
239 # cache of modules.
240
241 NOAUTO=1 add_checked_modules "$@"
242 } # }}}
243
49078e2 @falconindy general builder code standardized/cleanup
authored
244 # vim: set et sw=2 ft=sh:
Something went wrong with that request. Please try again.