Skip to content
Newer
Older
100644 194 lines (144 sloc) 4.32 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
15 add_dirpath() { # {{{
16 # add all elements of a path to cpio_filelist
17 # $1: an absolute path
18
19 [[ $1 == /?* ]] || return 1 # must be an absolute path, but not root itself
20
21 local path=
22 local -a pieces
23
24 IFS='/' read -ra pathparts <<< "$1"
25
26 for part in "${pathparts[@]}"; do
27 [[ $part ]] || continue
28 path+=/$part
29 add_dir "$path"
30 done
31 } # }}}
32
33 add_dir() {
34 # add a directory (without leading components)
35 # $1: path on initcpio
36 # $2: mode (optional, defaults to 755)
37
38 (( ! $# )) && return 1 # NOOP
39
40 local path=$1 mode=${2:-755}
41
42 __add_dir "$path" "$mode"
43 }
44
45 add_file() { # {{{
46 # add a regular file. no parsing done.
47 # $1: source on disk
48 # $2: destination on initcpio (optional: assumes same as source)
49
50 (( ! $# )) && return 1 # NOOP
51
52 # determine source and destination
53 local src=$basedir$1 dest=${2:-$1} mode=
54
55 if [[ ! -f $src ]]; then
56 err "$src: No such file"
57 return 1
58 fi
59
60 mode=$(stat -c %a "$src")
61 if [[ -z $mode ]]; then
62 err ""
63 return 1
64 fi
65
66 __add_file "$dest" "$src" "$mode"
67 } # }}}
68
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
69 add_symlink() { # {{{
75052c2 @falconindy initial commit
authored
70 # $1: name on initcpio
71 # $2: target of $1
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
72
73 (( $# != 2 )) || return $EINVAL
74
75052c2 @falconindy initial commit
authored
75 __add_slink "$@"
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
76 } # }}}
75052c2 @falconindy initial commit
authored
77
78 add_path_to_file() { # {{{
79 [[ $1 ]] && add_dirpath "${1%/*}"
80 } # }}}
81
82 add_module() { # {{{
83 # comment goes hurrr
84
85 local -a firmware moddeps
86 local modpath= module= dep=
87
88 module=${1%.ko?(.gz)}
89
90 if [[ -z $basedir ]]; then # fast path =)
91 read -rd '' modpath < <(kmodinfo -n "$module" 2>/dev/null)
92 else # slow path =(
93 read -rd '' modpath < <(find "$basedir$moduledir" -name "$mod.ko" -o -name "$mod.ko.gz" -print0)
94 fi
95
96 [[ -z $modpath ]] && return 1
97
98 add_path_to_file "$modpath"
99 add_file "$modpath" || return 1
100
101 # grab firmware
102 IFS=',' read -rd '' -a firmware < <(kmodinfo -F firmware "$modpath")
103 for fw in "${firmware[@]}"; do
104 __add_file "$basedir/lib/firmware/$fw" "/lib/firmware/$fw"
105 done
106
107 # resolve deps, yo
108 IFS=',' read -rd '' -a moddeps < <(kmodinfo -F depends "$modpath")
109 for dep in "${moddeps[@]}"; do
110 add_module "$dep"
111 done
112
113 # add in any quirks
114 if [[ ${modquirks[$module]} ]]; then
115 add_module "${modquirks[$module]}"
116 fi
117
118 return 0
119 } # }}}
120
121 add_binary() { # {{{
122 # comment goes here
123 # this needs reworking to use $basedir
124
125 [[ -f $basedir$1 ]] || { err "$1 not found"; return 1; }
126
127 local -a sodeps
128 local sodep=
129
130 IFS=$'\n' read -rd '' -a sodeps < <(ldd "$basedir$1" |\
131 sed -n '2,$s#.\+=>[[:blank:]]\+\([^ ]\+\) (.\+#\1#p')
132
133 for sodep in "${sodeps[@]}"; do
134 [[ -e $sodep ]] || continue
135
136 add_dirpath "${sodep%/*}"
137
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
138 if [[ -f $sodep ]]; then # but wait! a symlink is a regular file! FFFUUUUU
139 if [[ ! -L $sodep ]]; then
140 add_file "$sodep"
141 else
142 __add_slink "$sodep" "$(readlink -e "$sodep")"
143 add_file "$(readlink -e "$sodep")"
144 fi
145 fi
75052c2 @falconindy initial commit
authored
146 done
147
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
148 # the binary itself
75052c2 @falconindy initial commit
authored
149 add_file "$basedir$1" "${2:-$1}"
150
adc8b85 @falconindy fix add_binary so it adds more than just broken symlinks
authored
151 # we always need the big bad linker
152 __add_slink "$ld_so" $(readlink -e "$ld_so")
153 add_file "$(readlink -e "$ld_so")"
75052c2 @falconindy initial commit
authored
154 } # }}}
155
156 add_device() { # {{{
157 # add a device node to cpio_filelist
158 # $1: pathname on initcpio
159 # $2: device type (b=block or c=character)
160 # $3: device major
161 # $4: device minor
162 # $5: mode (optional, defaults to 644)
163
164 # nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>
165 (( $# == 4 )) || (( $# == 5 )) || return 1
166 [[ $2 = [bc] ]] || return 1
167
168 qgrep "^nod $1 " "$cpio_filelist" && return 1
169 printf 'nod %s %s 0 0 %s %s %s\n' "$1" "${5:-644}" "${@:2:3}" >> "$cpio_filelist"
170 } # }}}
171
172 add_driver_classes() { # {{{
173 local -a mods
174 local mod
175
176 # find modules by class (e.g. drivers/ata)
177 pushd "$moduledir/kernel" &>/dev/null
178 while read -rd '' mod; do
179 mod=${mod##*/}
180 mods+=("${mod%.ko?(.gz)}")
181 done < <(find "$@" \( -name '*.ko' -o -name '*.ko.gz' \) -print0 2>/dev/null | sort -z)
182 popd &>/dev/null
183
184 # only add what autodetect found if we have a module cache
185 if [[ -s $autodetect_cache ]]; then
186 IFS=$'\n' read -rd '' -a mods < <(comm -12 <(printf '%s\n' "${mods[@]}" | sort) "$autodetect_cache")
187 fi
188
189 for mod in "${mods[@]}"; do
190 add_module "$mod"
191 done
192 } # }}}
193
Something went wrong with that request. Please try again.