Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 213 lines (160 sloc) 5.154 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
75052c2 @falconindy initial commit
authored
77 __add_slink "$@"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
78 } # }}}
75052c2 @falconindy initial commit
authored
79
80 add_path_to_file() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
81 # add the path leading up to a file
82 # $1: filename with full path
75052c2 @falconindy initial commit
authored
83 [[ $1 ]] && add_dirpath "${1%/*}"
84 } # }}}
85
86 add_module() { # {{{
6438c9a @falconindy cleanup API, flesh out comments
authored
87 # add a kernel module. deps are resolved recursively and added
88 # $1: kernel module by name (no path, with or without extension)
75052c2 @falconindy initial commit
authored
89
90 local -a firmware moddeps
91 local modpath= module= dep=
92
93 module=${1%.ko?(.gz)}
94
95 if [[ -z $basedir ]]; then # fast path =)
96 read -rd '' modpath < <(kmodinfo -n "$module" 2>/dev/null)
97 else # slow path =(
2af2627 @falconindy remove extra call to add_path_to_file
authored
98 read -rd '' modpath < <(find "$basedir$moduledir/kernel" -type f -name "$mod.ko" -o -name "$mod.ko.gz" -print0)
75052c2 @falconindy initial commit
authored
99 fi
100
101 [[ -z $modpath ]] && return 1
102
103 add_file "$modpath" || return 1
104
105 # grab firmware
106 IFS=',' read -rd '' -a firmware < <(kmodinfo -F firmware "$modpath")
107 for fw in "${firmware[@]}"; do
108 __add_file "$basedir/lib/firmware/$fw" "/lib/firmware/$fw"
109 done
110
6438c9a @falconindy cleanup API, flesh out comments
authored
111 # resolve deps
75052c2 @falconindy initial commit
authored
112 IFS=',' read -rd '' -a moddeps < <(kmodinfo -F depends "$modpath")
113 for dep in "${moddeps[@]}"; do
114 add_module "$dep"
115 done
116
117 # add in any quirks
118 if [[ ${modquirks[$module]} ]]; then
119 add_module "${modquirks[$module]}"
120 fi
121
122 return 0
123 } # }}}
124
125 add_binary() { # {{{
126 # comment goes here
127 # this needs reworking to use $basedir
128
129 [[ -f $basedir$1 ]] || { err "$1 not found"; return 1; }
130
131 local -a sodeps
517c50b @falconindy reroute non-binaries passed to add_binary
authored
132 local binary=$basedir$1 sodep=
133
134 if ! "$ld_so" --verify "$binary"; then
135 # this isn't a binary!
136 add_file "$binary"
137 return $? # return with add_file's return val
138 fi
75052c2 @falconindy initial commit
authored
139
140 IFS=$'\n' read -rd '' -a sodeps < <(ldd "$basedir$1" |\
141 sed -n '2,$s#.\+=>[[:blank:]]\+\([^ ]\+\) (.\+#\1#p')
142
143 for sodep in "${sodeps[@]}"; do
144 [[ -e $sodep ]] || continue
145
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
146 if [[ -f $sodep ]]; then # but wait! a symlink is a regular file! FFFUUUUU
147 if [[ ! -L $sodep ]]; then
148 add_file "$sodep"
149 else
150 __add_slink "$sodep" "$(readlink -e "$sodep")"
151 add_file "$(readlink -e "$sodep")"
152 fi
153 fi
75052c2 @falconindy initial commit
authored
154 done
155
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
156 # the binary itself
75052c2 @falconindy initial commit
authored
157 add_file "$basedir$1" "${2:-$1}"
158
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
159 # we always need the big bad linker
160 __add_slink "$ld_so" $(readlink -e "$ld_so")
161 add_file "$(readlink -e "$ld_so")"
75052c2 @falconindy initial commit
authored
162 } # }}}
163
164 add_device() { # {{{
165 # add a device node to cpio_filelist
166 # $1: pathname on initcpio
167 # $2: device type (b=block or c=character)
168 # $3: device major
169 # $4: device minor
170 # $5: mode (optional, defaults to 644)
171
172 # nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>
173 (( $# == 4 )) || (( $# == 5 )) || return 1
174 [[ $2 = [bc] ]] || return 1
175
176 qgrep "^nod $1 " "$cpio_filelist" && return 1
177 printf 'nod %s %s 0 0 %s %s %s\n' "$1" "${5:-644}" "${@:2:3}" >> "$cpio_filelist"
178 } # }}}
179
f1b4dae @falconindy introduce use_hookscript public API call
authored
180 use_hookscript() { # {{{
181 # specifies that a builder should install a hookscript
9fd7d2b @falconindy fix grep call in add_driver_classes one more time
authored
182 # $builder comes from the builder parsing loop in main
183
f1b4dae @falconindy introduce use_hookscript public API call
authored
184 [[ $builder ]] || return 1
185 add_file "$basedir$_sharedir/hooks/$builder" "/hooks/$builder"
186 } # }}}
187
75052c2 @falconindy initial commit
authored
188 add_driver_classes() { # {{{
189 local -a mods
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
190 local -i ret=0
6438c9a @falconindy cleanup API, flesh out comments
authored
191 local mod=
75052c2 @falconindy initial commit
authored
192
193 # find modules by class (e.g. drivers/ata)
194 pushd "$moduledir/kernel" &>/dev/null
195 while read -rd '' mod; do
196 mod=${mod##*/}
197 mods+=("${mod%.ko?(.gz)}")
2af2627 @falconindy remove extra call to add_path_to_file
authored
198 done < <(find "$@" -type f \( -name '*.ko' -o -name '*.ko.gz' \) -print0 2>/dev/null)
75052c2 @falconindy initial commit
authored
199 popd &>/dev/null
200
201 # only add what autodetect found if we have a module cache
ee088cb @falconindy standardize -|_ in autodetected mod pruning
authored
202 [[ -s $autodetect_cache ]] &&
203 IFS=$'\n' read -rd '' -a mods < <(grep -xf <(printf '%s\n' "${mods[@]//-/_}") "$autodetect_cache")
75052c2 @falconindy initial commit
authored
204
205 for mod in "${mods[@]}"; do
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
206 add_module "$mod" && (( ++ret ))
75052c2 @falconindy initial commit
authored
207 done
dbffde7 @falconindy give add_driver_classes meaningful return values
authored
208
209 # return 0 on modules added, else 1
210 return $(( ! ret ))
75052c2 @falconindy initial commit
authored
211 } # }}}
212
Something went wrong with that request. Please try again.