Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 214 lines (161 sloc) 5.133 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
14 add_dirpath() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
15 # add all elements of a path (equivalent of mkdir -p)
75052c2 @falconindy initial commit
authored
16 # $1: an absolute path
17
18 [[ $1 == /?* ]] || return 1 # must be an absolute path, but not root itself
19
20 local path=
21 local -a pieces
22
23 IFS='/' read -ra pathparts <<< "$1"
24
25 for part in "${pathparts[@]}"; do
26 [[ $part ]] || continue
27 path+=/$part
28 add_dir "$path"
29 done
30 } # }}}
31
6438c9a @falconindy cleanup API, flesh out comments
authored
32 add_dir() { # {{{
33 # add a directory (equivalent of mkdir *without* -p)
34 # $1: absolute path on initcpio
75052c2 @falconindy initial commit
authored
35 # $2: mode (optional, defaults to 755)
36
37 (( ! $# )) && return 1 # NOOP
38
39 local path=$1 mode=${2:-755}
40
41 __add_dir "$path" "$mode"
6438c9a @falconindy cleanup API, flesh out comments
authored
42 } # }}}
75052c2 @falconindy initial commit
authored
43
44 add_file() { # {{{
45 # add a regular file. no parsing done.
46 # $1: source on disk
47 # $2: destination on initcpio (optional: assumes same as source)
48
49 (( ! $# )) && return 1 # NOOP
50
51 # determine source and destination
52 local src=$basedir$1 dest=${2:-$1} mode=
53
54 if [[ ! -f $src ]]; then
55 err "$src: No such file"
56 return 1
57 fi
58
59 mode=$(stat -c %a "$src")
60 if [[ -z $mode ]]; then
f404baa @falconindy add_file: call add_path_to_file before __add_file
authored
61 err "failed to stat file: \`$src'"
75052c2 @falconindy initial commit
authored
62 return 1
63 fi
64
f404baa @falconindy add_file: call add_path_to_file before __add_file
authored
65 add_path_to_file "$dest"
66
75052c2 @falconindy initial commit
authored
67 __add_file "$dest" "$src" "$mode"
68 } # }}}
69
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
70 add_symlink() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
71 # add a symlink
72 # $1: name on initcpio
73 # $2: target of $1
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
74
6438c9a @falconindy cleanup API, flesh out comments
authored
75 (( $# != 2 )) || return 1
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
76
c8af038 @falconindy fix regression spotted by commit 809b5b2
authored
77 add_path_to_file "$1"
75052c2 @falconindy initial commit
authored
78 __add_slink "$@"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
79 } # }}}
75052c2 @falconindy initial commit
authored
80
81 add_path_to_file() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
82 # add the path leading up to a file
83 # $1: filename with full path
75052c2 @falconindy initial commit
authored
84 [[ $1 ]] && add_dirpath "${1%/*}"
85 } # }}}
86
87 add_module() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
88 # add a kernel module. deps are resolved recursively and added
89 # $1: kernel module by name (no path, with or without extension)
75052c2 @falconindy initial commit
authored
90
91 local -a firmware moddeps
92 local modpath= module= dep=
93
94 module=${1%.ko?(.gz)}
95
96 if [[ -z $basedir ]]; then # fast path =)
97 read -rd '' modpath < <(kmodinfo -n "$module" 2>/dev/null)
98 else # slow path =(
2af2627 @falconindy remove extra call to add_path_to_file
authored
99 read -rd '' modpath < <(find "$basedir$moduledir/kernel" -type f -name "$mod.ko" -o -name "$mod.ko.gz" -print0)
75052c2 @falconindy initial commit
authored
100 fi
101
102 [[ -z $modpath ]] && return 1
103
104 add_file "$modpath" || return 1
105
106 # grab firmware
107 IFS=',' read -rd '' -a firmware < <(kmodinfo -F firmware "$modpath")
108 for fw in "${firmware[@]}"; do
f7a8bd2 @falconindy fix erroneous call to backend function
authored
109 add_file "/lib/firmware/$fw"
75052c2 @falconindy initial commit
authored
110 done
111
6438c9a @falconindy cleanup API, flesh out comments
authored
112 # resolve deps
75052c2 @falconindy initial commit
authored
113 IFS=',' read -rd '' -a moddeps < <(kmodinfo -F depends "$modpath")
114 for dep in "${moddeps[@]}"; do
115 add_module "$dep"
116 done
117
118 # add in any quirks
f2136d7 @falconindy add quirks file for quirky modules
authored
119 for mod in ${modquirks[$module]}; do
120 add_module "$mod"
121 done
75052c2 @falconindy initial commit
authored
122
123 return 0
124 } # }}}
125
126 add_binary() { # {{{
127 # comment goes here
128 # this needs reworking to use $basedir
129
130 [[ -f $basedir$1 ]] || { err "$1 not found"; return 1; }
131
132 local -a sodeps
517c50b @falconindy reroute non-binaries passed to add_binary
authored
133 local binary=$basedir$1 sodep=
134
135 if ! "$ld_so" --verify "$binary"; then
136 # this isn't a binary!
137 add_file "$binary"
138 return $? # return with add_file's return val
139 fi
75052c2 @falconindy initial commit
authored
140
141 IFS=$'\n' read -rd '' -a sodeps < <(ldd "$basedir$1" |\
142 sed -n '2,$s#.\+=>[[:blank:]]\+\([^ ]\+\) (.\+#\1#p')
143
144 for sodep in "${sodeps[@]}"; do
145 [[ -e $sodep ]] || continue
146
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
147 if [[ -f $sodep ]]; then # but wait! a symlink is a regular file! FFFUUUUU
148 if [[ ! -L $sodep ]]; then
149 add_file "$sodep"
150 else
c8af038 @falconindy fix regression spotted by commit 809b5b2
authored
151 add_symlink "$sodep" "$(readlink -e "$sodep")"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
152 add_file "$(readlink -e "$sodep")"
153 fi
154 fi
75052c2 @falconindy initial commit
authored
155 done
156
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
157 # the binary itself
75052c2 @falconindy initial commit
authored
158 add_file "$basedir$1" "${2:-$1}"
159
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
160 # we always need the big bad linker
161 __add_slink "$ld_so" $(readlink -e "$ld_so")
162 add_file "$(readlink -e "$ld_so")"
75052c2 @falconindy initial commit
authored
163 } # }}}
164
165 add_device() { # {{{
166 # add a device node to cpio_filelist
167 # $1: pathname on initcpio
168 # $2: device type (b=block or c=character)
169 # $3: device major
170 # $4: device minor
171 # $5: mode (optional, defaults to 644)
172
173 # nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>
174 (( $# == 4 )) || (( $# == 5 )) || return 1
175 [[ $2 = [bc] ]] || return 1
176
177 qgrep "^nod $1 " "$cpio_filelist" && return 1
178 printf 'nod %s %s 0 0 %s %s %s\n' "$1" "${5:-644}" "${@:2:3}" >> "$cpio_filelist"
179 } # }}}
180
f1b4dae @falconindy introduce use_hookscript public API call
authored
181 use_hookscript() { # {{{
182 # specifies that a builder should install a hookscript
9fd7d2b @falconindy fix grep call in add_driver_classes one more time
authored
183 # $builder comes from the builder parsing loop in main
184
f1b4dae @falconindy introduce use_hookscript public API call
authored
185 [[ $builder ]] || return 1
186 add_file "$basedir$_sharedir/hooks/$builder" "/hooks/$builder"
187 } # }}}
188
75052c2 @falconindy initial commit
authored
189 add_driver_classes() { # {{{
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)
195 pushd "$moduledir/kernel" &>/dev/null
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 ]] &&
204 IFS=$'\n' read -rd '' -a mods < <(grep -xf <(printf '%s\n' "${mods[@]//-/_}") "$autodetect_cache")
75052c2 @falconindy initial commit
authored
205
206 for mod in "${mods[@]}"; do
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
207 add_module "$mod" && (( ++ret ))
75052c2 @falconindy initial commit
authored
208 done
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
209
210 # return 0 on modules added, else 1
211 return $(( ! ret ))
75052c2 @falconindy initial commit
authored
212 } # }}}
213
Something went wrong with that request. Please try again.