Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 207 lines (153 sloc) 4.915 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
34 local src=$basedir$1 dest=${2:-$1} mode=
35
36 if [[ ! -f $src ]]; then
37 err "$src: No such file"
38 return 1
39 fi
40
41 mode=$(stat -c %a "$src")
42 if [[ -z $mode ]]; then
f404baa @falconindy add_file: call add_path_to_file before __add_file
authored
43 err "failed to stat file: \`$src'"
75052c2 @falconindy initial commit
authored
44 return 1
45 fi
46
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
47 #add_path_to_file "$dest"
f404baa @falconindy add_file: call add_path_to_file before __add_file
authored
48
e37a454 @falconindy avoid using public API from within the API itself
authored
49 __add_file "${dest#$basedir}" "$src" "$mode"
75052c2 @falconindy initial commit
authored
50 } # }}}
51
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
52 add_symlink() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
53 # add a symlink
54 # $1: name on initcpio
55 # $2: target of $1
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
56
ce83c05 @falconindy cleanup API documentation
authored
57 (( $# == 2 )) || return 1
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
58
c8af038 @falconindy fix regression spotted by commit 809b5b2
authored
59 add_path_to_file "$1"
75052c2 @falconindy initial commit
authored
60 __add_slink "$@"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
61 } # }}}
75052c2 @falconindy initial commit
authored
62
63 add_path_to_file() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
64 # add the path leading up to a file
65 # $1: filename with full path
ce83c05 @falconindy cleanup API documentation
authored
66
67 (( $# )) || return 1
68
69 add_dir "${1%/*}"
75052c2 @falconindy initial commit
authored
70 } # }}}
71
72 add_module() { # {{{
ce83c05 @falconindy cleanup API documentation
authored
73 # add a kernel module with dependencies
6438c9a @falconindy cleanup API, flesh out comments
authored
74 # $1: kernel module by name (no path, with or without extension)
75052c2 @falconindy initial commit
authored
75
ce83c05 @falconindy cleanup API documentation
authored
76 (( $# )) || return 1
77
75052c2 @falconindy initial commit
authored
78 local -a firmware moddeps
e37a454 @falconindy avoid using public API from within the API itself
authored
79 local modpath= module= mod= dep=
75052c2 @falconindy initial commit
authored
80
81 module=${1%.ko?(.gz)}
82
83 if [[ -z $basedir ]]; then # fast path =)
84 read -rd '' modpath < <(kmodinfo -n "$module" 2>/dev/null)
85 else # slow path =(
e37a454 @falconindy avoid using public API from within the API itself
authored
86 read -rd '' modpath < <(find "$basedir$moduledir/kernel" -type f \
87 -name "$module.ko" -o -name "$module.ko.gz" -print0)
75052c2 @falconindy initial commit
authored
88 fi
89
e37a454 @falconindy avoid using public API from within the API itself
authored
90 [[ -z $modpath ]] && { echo 'modpath no eist'; return 1; }
75052c2 @falconindy initial commit
authored
91
e37a454 @falconindy avoid using public API from within the API itself
authored
92 __add_file "${modpath#$basedir}" "$modpath" 644 || return 1
75052c2 @falconindy initial commit
authored
93
94 # grab firmware
95 IFS=',' read -rd '' -a firmware < <(kmodinfo -F firmware "$modpath")
96 for fw in "${firmware[@]}"; do
e37a454 @falconindy avoid using public API from within the API itself
authored
97 __add_file "/lib/firmware/$fw" "/lib/firmware/$fw" 644
75052c2 @falconindy initial commit
authored
98 done
99
6438c9a @falconindy cleanup API, flesh out comments
authored
100 # resolve deps
75052c2 @falconindy initial commit
authored
101 IFS=',' read -rd '' -a moddeps < <(kmodinfo -F depends "$modpath")
102 for dep in "${moddeps[@]}"; do
103 add_module "$dep"
104 done
105
106 # add in any quirks
f2136d7 @falconindy add quirks file for quirky modules
authored
107 for mod in ${modquirks[$module]}; do
108 add_module "$mod"
109 done
75052c2 @falconindy initial commit
authored
110
111 return 0
112 } # }}}
113
114 add_binary() { # {{{
ce83c05 @falconindy cleanup API documentation
authored
115 # add a binary file with .so depends
116 # $1: path to binary
117
118 (( $# )) || return 1
75052c2 @falconindy initial commit
authored
119
120 [[ -f $basedir$1 ]] || { err "$1 not found"; return 1; }
121
122 local -a sodeps
e37a454 @falconindy avoid using public API from within the API itself
authored
123 local binary=$basedir$1 mode= sodep= resolved= dirname=
124
125 mode=$(stat -c %a "$binary")
517c50b @falconindy reroute non-binaries passed to add_binary
authored
126
127 if ! "$ld_so" --verify "$binary"; then
128 # this isn't a binary!
e37a454 @falconindy avoid using public API from within the API itself
authored
129 __add_file "${binary#$basedir}" "$binary" "$mode"
517c50b @falconindy reroute non-binaries passed to add_binary
authored
130 return $? # return with add_file's return val
131 fi
75052c2 @falconindy initial commit
authored
132
133 IFS=$'\n' read -rd '' -a sodeps < <(ldd "$basedir$1" |\
134 sed -n '2,$s#.\+=>[[:blank:]]\+\([^ ]\+\) (.\+#\1#p')
135
136 for sodep in "${sodeps[@]}"; do
137 [[ -e $sodep ]] || continue
138
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
139 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
140 mode=$(stat -c %a "$sodep")
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
141 if [[ ! -L $sodep ]]; then
e37a454 @falconindy avoid using public API from within the API itself
authored
142 __add_file "$sodep"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
143 else
e37a454 @falconindy avoid using public API from within the API itself
authored
144 resolved=$(readlink -e "$basedir$sodep")
145 dirname=${resolved%/*}
146 __add_dir "${dirname#$basedir}" 755
147 __add_slink "$sodep" "${resolved#$basedir}"
148 __add_file "$resolved" "$resolved" 755
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
149 fi
150 fi
75052c2 @falconindy initial commit
authored
151 done
152
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
153 # the binary itself
e37a454 @falconindy avoid using public API from within the API itself
authored
154 __add_file "${1#$basedir}" "$1" 755
75052c2 @falconindy initial commit
authored
155
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
156 # we always need the big bad linker
e37a454 @falconindy avoid using public API from within the API itself
authored
157 __add_slink "$ld_so" "$(readlink -e "$ld_so")"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
158 add_file "$(readlink -e "$ld_so")"
75052c2 @falconindy initial commit
authored
159 } # }}}
160
ce83c05 @falconindy cleanup API documentation
authored
161 add_pipe() { # {{{
162 # add a fifo
163 # $1: path of pipe
164 # $2: mode (optional, defaults to 644)
165
166 (( $# )) || return 1
167
168 __add_pipe "$1" "${2:-644}"
169 } # }}}
75052c2 @falconindy initial commit
authored
170
f1b4dae @falconindy introduce use_hookscript public API call
authored
171 use_hookscript() { # {{{
172 # specifies that a builder should install a hookscript
ce83c05 @falconindy cleanup API documentation
authored
173 # "$builder" comes from the builder parsing loop in main
9fd7d2b @falconindy fix grep call in add_driver_classes one more time
authored
174
f1b4dae @falconindy introduce use_hookscript public API call
authored
175 [[ $builder ]] || return 1
176 add_file "$basedir$_sharedir/hooks/$builder" "/hooks/$builder"
177 } # }}}
178
75052c2 @falconindy initial commit
authored
179 add_driver_classes() { # {{{
ce83c05 @falconindy cleanup API documentation
authored
180 # add a class of drivers, as a dir relative to /lib/modules/$kernver/kernel
181 # $@: directories
182
75052c2 @falconindy initial commit
authored
183 local -a mods
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
184 local -i ret=0
6438c9a @falconindy cleanup API, flesh out comments
authored
185 local mod=
75052c2 @falconindy initial commit
authored
186
187 # find modules by class (e.g. drivers/ata)
e37a454 @falconindy avoid using public API from within the API itself
authored
188 pushd "$basedir$moduledir/kernel" &>/dev/null
75052c2 @falconindy initial commit
authored
189 while read -rd '' mod; do
190 mod=${mod##*/}
191 mods+=("${mod%.ko?(.gz)}")
2af2627 @falconindy remove extra call to add_path_to_file
authored
192 done < <(find "$@" -type f \( -name '*.ko' -o -name '*.ko.gz' \) -print0 2>/dev/null)
75052c2 @falconindy initial commit
authored
193 popd &>/dev/null
194
195 # only add what autodetect found if we have a module cache
ee088cb @falconindy standardize -|_ in autodetected mod pruning
authored
196 [[ -s $autodetect_cache ]] &&
197 IFS=$'\n' read -rd '' -a mods < <(grep -xf <(printf '%s\n' "${mods[@]//-/_}") "$autodetect_cache")
75052c2 @falconindy initial commit
authored
198
199 for mod in "${mods[@]}"; do
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
200 add_module "$mod" && (( ++ret ))
75052c2 @falconindy initial commit
authored
201 done
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
202
203 # return 0 on modules added, else 1
204 return $(( ! ret ))
75052c2 @falconindy initial commit
authored
205 } # }}}
206
Something went wrong with that request. Please try again.