Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 209 lines (157 sloc) 4.985 kb
75052c2e »
2011-04-26 initial commit
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() { # {{{
6438c9ad »
2011-04-27 cleanup API, flesh out comments
15 # add all elements of a path (equivalent of mkdir -p)
75052c2e »
2011-04-26 initial commit
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
6438c9ad »
2011-04-27 cleanup API, flesh out comments
32 add_dir() { # {{{
33 # add a directory (equivalent of mkdir *without* -p)
34 # $1: absolute path on initcpio
75052c2e »
2011-04-26 initial commit
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"
6438c9ad »
2011-04-27 cleanup API, flesh out comments
42 } # }}}
75052c2e »
2011-04-26 initial commit
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
f404baa5 »
2011-04-27 add_file: call add_path_to_file before __add_file
61 err "failed to stat file: \`$src'"
75052c2e »
2011-04-26 initial commit
62 return 1
63 fi
64
f404baa5 »
2011-04-27 add_file: call add_path_to_file before __add_file
65 add_path_to_file "$dest"
66
75052c2e »
2011-04-26 initial commit
67 __add_file "$dest" "$src" "$mode"
68 } # }}}
69
adc8b850 »
2011-04-27 fix add_binary so it adds more than just broken symlinks
70 add_symlink() { # {{{
6438c9ad »
2011-04-27 cleanup API, flesh out comments
71 # add a symlink
72 # $1: name on initcpio
73 # $2: target of $1
adc8b850 »
2011-04-27 fix add_binary so it adds more than just broken symlinks
74
6438c9ad »
2011-04-27 cleanup API, flesh out comments
75 (( $# != 2 )) || return 1
adc8b850 »
2011-04-27 fix add_binary so it adds more than just broken symlinks
76
75052c2e »
2011-04-26 initial commit
77 __add_slink "$@"
adc8b850 »
2011-04-27 fix add_binary so it adds more than just broken symlinks
78 } # }}}
75052c2e »
2011-04-26 initial commit
79
80 add_path_to_file() { # {{{
6438c9ad »
2011-04-27 cleanup API, flesh out comments
81 # add the path leading up to a file
82 # $1: filename with full path
75052c2e »
2011-04-26 initial commit
83 [[ $1 ]] && add_dirpath "${1%/*}"
84 } # }}}
85
86 add_module() { # {{{
6438c9ad »
2011-04-27 cleanup API, flesh out comments
87 # add a kernel module. deps are resolved recursively and added
88 # $1: kernel module by name (no path, with or without extension)
75052c2e »
2011-04-26 initial commit
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 =(
98 read -rd '' modpath < <(find "$basedir$moduledir" -name "$mod.ko" -o -name "$mod.ko.gz" -print0)
99 fi
100
101 [[ -z $modpath ]] && return 1
102
103 add_path_to_file "$modpath"
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
109 __add_file "$basedir/lib/firmware/$fw" "/lib/firmware/$fw"
110 done
111
6438c9ad »
2011-04-27 cleanup API, flesh out comments
112 # resolve deps
75052c2e »
2011-04-26 initial commit
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
119 if [[ ${modquirks[$module]} ]]; then
120 add_module "${modquirks[$module]}"
121 fi
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
133 local sodep=
134
135 IFS=$'\n' read -rd '' -a sodeps < <(ldd "$basedir$1" |\
136 sed -n '2,$s#.\+=>[[:blank:]]\+\([^ ]\+\) (.\+#\1#p')
137
138 for sodep in "${sodeps[@]}"; do
139 [[ -e $sodep ]] || continue
140
141 add_dirpath "${sodep%/*}"
142
adc8b850 »
2011-04-27 fix add_binary so it adds more than just broken symlinks
143 if [[ -f $sodep ]]; then # but wait! a symlink is a regular file! FFFUUUUU
144 if [[ ! -L $sodep ]]; then
145 add_file "$sodep"
146 else
147 __add_slink "$sodep" "$(readlink -e "$sodep")"
148 add_file "$(readlink -e "$sodep")"
149 fi
150 fi
75052c2e »
2011-04-26 initial commit
151 done
152
adc8b850 »
2011-04-27 fix add_binary so it adds more than just broken symlinks
153 # the binary itself
75052c2e »
2011-04-26 initial commit
154 add_file "$basedir$1" "${2:-$1}"
155
adc8b850 »
2011-04-27 fix add_binary so it adds more than just broken symlinks
156 # we always need the big bad linker
157 __add_slink "$ld_so" $(readlink -e "$ld_so")
158 add_file "$(readlink -e "$ld_so")"
75052c2e »
2011-04-26 initial commit
159 } # }}}
160
161 add_device() { # {{{
162 # add a device node to cpio_filelist
163 # $1: pathname on initcpio
164 # $2: device type (b=block or c=character)
165 # $3: device major
166 # $4: device minor
167 # $5: mode (optional, defaults to 644)
168
169 # nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>
170 (( $# == 4 )) || (( $# == 5 )) || return 1
171 [[ $2 = [bc] ]] || return 1
172
173 qgrep "^nod $1 " "$cpio_filelist" && return 1
174 printf 'nod %s %s 0 0 %s %s %s\n' "$1" "${5:-644}" "${@:2:3}" >> "$cpio_filelist"
175 } # }}}
176
f1b4daef »
2011-04-27 introduce use_hookscript public API call
177 use_hookscript() { # {{{
178 # specifies that a builder should install a hookscript
179 [[ $builder ]] || return 1
180 add_file "$basedir$_sharedir/hooks/$builder" "/hooks/$builder"
181 } # }}}
182
75052c2e »
2011-04-26 initial commit
183 add_driver_classes() { # {{{
184 local -a mods
dbffde77 »
2011-04-28 give add_driver_classes meaningful return values
185 local -i ret=0
6438c9ad »
2011-04-27 cleanup API, flesh out comments
186 local mod=
75052c2e »
2011-04-26 initial commit
187
188 # find modules by class (e.g. drivers/ata)
189 pushd "$moduledir/kernel" &>/dev/null
190 while read -rd '' mod; do
191 mod=${mod##*/}
192 mods+=("${mod%.ko?(.gz)}")
193 done < <(find "$@" \( -name '*.ko' -o -name '*.ko.gz' \) -print0 2>/dev/null | sort -z)
194 popd &>/dev/null
195
196 # only add what autodetect found if we have a module cache
197 if [[ -s $autodetect_cache ]]; then
a6a76fa1 »
2011-04-28 grep Ff is faster than comm
198 IFS=$'\n' read -rd '' -a mods < <(grep -Ff <(printf '%s\n' "${mods[@]}") "$autodetect_cache")
75052c2e »
2011-04-26 initial commit
199 fi
200
201 for mod in "${mods[@]}"; do
dbffde77 »
2011-04-28 give add_driver_classes meaningful return values
202 add_module "$mod" && (( ++ret ))
75052c2e »
2011-04-26 initial commit
203 done
dbffde77 »
2011-04-28 give add_driver_classes meaningful return values
204
205 # return 0 on modules added, else 1
206 return $(( ! ret ))
75052c2e »
2011-04-26 initial commit
207 } # }}}
208
Something went wrong with that request. Please try again.