Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 544 lines (442 sloc) 15.001 kB
75052c2 @falconindy initial commit
authored
1 #!/bin/bash
e5a8d5b @falconindy standardize file headers and add MIT licensee
authored
2 #
3 # geninit - modular initramfs creation tool
4 #
5 # Copyright (C) 2011 by Dave Reisner <d@falconindy.com>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
75052c2 @falconindy initial commit
authored
25
26 shopt -s extglob
d51765f @falconindy fix linker resolution for i686 and multilib
authored
27 shopt -s nullglob
75052c2 @falconindy initial commit
authored
28
d666aa2 @falconindy reorganize globals. because i can
authored
29 # error codes
30 declare -ri ENOENT=2
31 declare -ri EACCES=13
32 declare -ri EEXIST=17
33 declare -ri EINVAL=22
34
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored
35 # tender vittles
cee912e @falconindy mark static variables as readonly
authored
36 declare -r _sysconfdir=/etc
37 declare -r _sharedir=.
38 declare -r builderdir=${_sharedir}/builders
39 declare -r baseconfig=${_sysconfdir}/geninit.conf
40 declare -r carch=$(uname -m)
75052c2 @falconindy initial commit
authored
41
d666aa2 @falconindy reorganize globals. because i can
authored
42 # options
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored
43 declare -i automods=0
8584669 @falconindy replace old and busted output functions with makepkg's
authored
44 declare -i color=1
f1562ff @falconindy dryrun is an integer value
authored
45 declare -i dryrun=0
d666aa2 @falconindy reorganize globals. because i can
authored
46 declare -i savebuildroot=0
d2f545c @falconindy add verbose mode, listing additions to the image
authored
47 declare -i verbose=0
d666aa2 @falconindy reorganize globals. because i can
authored
48
49 declare kernver=$(uname -r)
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored
50 declare basedir=
51 declare config=
52 declare imagename=
d666aa2 @falconindy reorganize globals. because i can
authored
53 declare ocompress=
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored
54 declare preset=
55 declare skipbuilders=
56
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
57 # -------------------------- #
58 ## general util functions ##
59 # -------------------------- #
75052c2 @falconindy initial commit
authored
60
8584669 @falconindy replace old and busted output functions with makepkg's
authored
61 plain() { # {{{
62 local mesg=$1; shift
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
63 printf "$BOLD $mesg$NC\n" "$@" >&2
8584669 @falconindy replace old and busted output functions with makepkg's
authored
64 } # }}}
65
66 msg() { # {{{
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
67 local mesg=$1; shift
68 printf "$GREEN==>$NC$BOLD $mesg$NC\n" "$@" >&2
8584669 @falconindy replace old and busted output functions with makepkg's
authored
69 } # }}}
75052c2 @falconindy initial commit
authored
70
8584669 @falconindy replace old and busted output functions with makepkg's
authored
71 msg2() { # {{{
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
72 local mesg=$1; shift
73 printf "$BLUE ->$NC$BOLD $mesg$NC\n" "$@" >&2
8584669 @falconindy replace old and busted output functions with makepkg's
authored
74 } # }}}
75
76 warning() { # {{{
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
77 local mesg=$1; shift
78 printf "$YELLOW==> WARNING:$NC$BOLD $mesg$NC\n" "$@" >&2
8584669 @falconindy replace old and busted output functions with makepkg's
authored
79 } # }}}
80
81 error() { # {{{
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
82 local mesg=$1; shift
83 printf "$RED==> ERROR:$NC$BOLD $mesg$NC\n" "$@" >&2
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
84 } # }}}
75052c2 @falconindy initial commit
authored
85
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
86 die() { # {{{
8584669 @falconindy replace old and busted output functions with makepkg's
authored
87 error "$*"
75052c2 @falconindy initial commit
authored
88 cleanup 1
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
89 } # }}}
75052c2 @falconindy initial commit
authored
90
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
91 in_array() { # {{{
75052c2 @falconindy initial commit
authored
92 local item needle=$1; shift
93
94 for item; do
58c9f9f @falconindy go on a quoting spree.
authored
95 [[ "$item" == $needle ]] && return 0 # Found
75052c2 @falconindy initial commit
authored
96 done
97 return 1 # Not Found
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
98 } # }}}
75052c2 @falconindy initial commit
authored
99
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
100 cleanup() { # {{{
58c9f9f @falconindy go on a quoting spree.
authored
101 (( SHLVL == 2 )) && [[ "$2" ]] && error "$2"
102 if (( savebuildroot )) && [[ -d "$buildroot" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
103 msg "buildroot preserved at: $buildroot"
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
104 else
105 rm -rf "$tmpdir"
106 fi
75052c2 @falconindy initial commit
authored
107 exit $1
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
108 } # }}}
75052c2 @falconindy initial commit
authored
109
25a82f8 @falconindy add a usage function
authored
110 usage() { # {{{
111 cat<<USAGE
a113c0c @falconindy cleanup usage function
authored
112 usage: geninit [options]
25a82f8 @falconindy add a usage function
authored
113
114 Options:
115 -b <path> Use base directory (default: /)
116 -c <file> Specify a different config file (default: $_sysconfdir/geninit.conf)
117 -g <path> Path and name of generated image (dry-run if unspecified)
38f816a @falconindy add -H and -L options for getting help with builders
authored
118 -H <builder> Display help for a specified builder
25a82f8 @falconindy add a usage function
authored
119 -h Display this help message
a113c0c @falconindy cleanup usage function
authored
120 -k <kver> Specify a kernel version (default: $kernver)
38f816a @falconindy add -H and -L options for getting help with builders
authored
121 -L List available builders
a113c0c @falconindy cleanup usage function
authored
122 -p <preset> Build using a preset file (any of $_sysconfdir/geninit.d/*.preset)
25a82f8 @falconindy add a usage function
authored
123 -S <names> Skip the specified builders during the build process
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
124 -s Preserve buildroot after image creation
6dda899 @falconindy allow changing location of build dir
authored
125 -t <path> Specify an alternate location for temporary workspace
25a82f8 @falconindy add a usage function
authored
126 -z <method> Override compression method
127
128 USAGE
129 exit
130 } # }}}
131
38f816a @falconindy add -H and -L options for getting help with builders
authored
132 builderlist() { # {{{
133 local -a builders=("$_sharedir"/builders/*)
134
135 (( ${#builders[*]} )) || die "no builders found"
136
8584669 @falconindy replace old and busted output functions with makepkg's
authored
137 msg "Available builders -- use -H <builder> for help with a specific builder"
38f816a @falconindy add -H and -L options for getting help with builders
authored
138 printf '%s\n' "${builders[@]##*/}" | column -c$(tput cols)
139 cleanup 0
140 } # }}}
141
142 builderhelp() { # {{{
143 local builder=$1
144
58c9f9f @falconindy go on a quoting spree.
authored
145 [[ -f "$_sharedir/builders/$builder" ]] || die "no such builder: $builder"
38f816a @falconindy add -H and -L options for getting help with builders
authored
146 . "$_sharedir/builders/$builder"
147 type -t helpmsg >/dev/null || die "no help for builder: $builder"
8584669 @falconindy replace old and busted output functions with makepkg's
authored
148 msg "$builder"
38f816a @falconindy add -H and -L options for getting help with builders
authored
149 helpmsg
150 cleanup 0
151 } # }}}
152
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
153 # --------------- #
154 ## private API ##
155 # --------------- #
75052c2 @falconindy initial commit
authored
156
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
157 __get_kernver() { # {{{
158 local kernel=$1
159
58c9f9f @falconindy go on a quoting spree.
authored
160 if [[ "${kernel:0:1}" != / ]]; then
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
161 echo "$kernel"
162 return 0
163 fi
164
58c9f9f @falconindy go on a quoting spree.
authored
165 [[ -r "$kernel" ]] || return $ENOENT
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
166
58c9f9f @falconindy go on a quoting spree.
authored
167 read _ kernver < <(file -b "$basedir$kernel" | grep -o 'version [^ ]\+')
168 if [[ "$kernver" ]]; then
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
169 echo "$kernver"
170 return 0
171 fi
172
173 return $EINVAL
174 } # }}}
175
75052c2 @falconindy initial commit
authored
176 __add_file() { # {{{
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
177 # add a file to $buildroot
75052c2 @falconindy initial commit
authored
178 # $1: pathname on initcpio
179 # $2: source on disk
180 # $3: mode
181
182 (( $# == 3 )) || return $EINVAL
58c9f9f @falconindy go on a quoting spree.
authored
183 [[ -e "$buildroot$1" ]] && return $EEXIST
75052c2 @falconindy initial commit
authored
184
d2f545c @falconindy add verbose mode, listing additions to the image
authored
185 (( verbose )) && plain "adding file: %s" "$1"
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
186 install -Dm$3 "$2" "$buildroot$1"
75052c2 @falconindy initial commit
authored
187 } # }}}
188
189 __add_dir() { # {{{
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
190 # add a directory (with parents) to $buildroot
75052c2 @falconindy initial commit
authored
191 # $1: pathname on initcpio
192 # $2: mode
193
58c9f9f @falconindy go on a quoting spree.
authored
194 (( $# == 2 )) || [[ "$1" == /?* ]] || return $EINVAL
195 [[ -e "$buildroot$1" ]] && return $EEXIST
75052c2 @falconindy initial commit
authored
196
d2f545c @falconindy add verbose mode, listing additions to the image
authored
197 (( verbose )) && plain "adding dir: %s" "$1"
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
198 install -dm$2 "$buildroot$1"
75052c2 @falconindy initial commit
authored
199 } # }}}
200
201 __add_pipe() { # {{{
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
202 # add a pipe to $buildroot
75052c2 @falconindy initial commit
authored
203 # $1: pathname on initcpio
204 # $2: mode
205
206 (( $# == 2 )) || return $EINVAL
58c9f9f @falconindy go on a quoting spree.
authored
207 [[ -e "$buildroot$1" ]] && return $EEXIST
75052c2 @falconindy initial commit
authored
208
d2f545c @falconindy add verbose mode, listing additions to the image
authored
209 (( verbose )) && plain "adding pipe: %s" "$1"
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
210 mkfifo -m$2 "$buildroot$1"
75052c2 @falconindy initial commit
authored
211 } # }}}
212
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored
213 __add_slink() { # {{{
214 # add a symlink to $buildroot
215 # $1: name on initcpio
216 # $2: target of $1
217
218 (( $# == 2 )) || return $EINVAL
58c9f9f @falconindy go on a quoting spree.
authored
219 [[ -L "$buildroot$1" ]] && return $EEXIST
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored
220
d2f545c @falconindy add verbose mode, listing additions to the image
authored
221 (( verbose )) && plain "adding symlink: %s -> %s" "$2" "$1"
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored
222 ln -s "$2" "$buildroot$1"
223 } # }}}
224
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
225 __build_image() { # {{{
b6714ed @falconindy ARRRGH. always use local inside a function
authored
226 local -a zopts pipesave
1c2c46f @falconindy try to warn the user when a build may have failed
authored
227 local -i errors=0
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
228 local builder= module= line= mod= file= errmsg=
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
229
d406ecd @falconindy move final kernel version check to __build_image
authored
230 # check our kernel version
58c9f9f @falconindy go on a quoting spree.
authored
231 [[ -d "$basedir/lib/modules/$kernver" ]] || die "kernel \`$kernver' not found"
d406ecd @falconindy move final kernel version check to __build_image
authored
232
f6f7761 @falconindy refactor depmod creation
authored
233 # this must be resolved to an absolute path
58c9f9f @falconindy go on a quoting spree.
authored
234 if [[ "$imagename" && "${imagename:0:1}" != / ]]; then
f6f7761 @falconindy refactor depmod creation
authored
235 imagename=$(readlink -f "$imagename")
58c9f9f @falconindy go on a quoting spree.
authored
236 [[ "$imagename" ]] || die "Failed to resolve path to imagename"
f6f7761 @falconindy refactor depmod creation
authored
237 fi
238
d406ecd @falconindy move final kernel version check to __build_image
authored
239 (( dryrun )) && msg "Starting dry run: %s" "$kernver" || msg "Starting build: %s" "$kernver"
240
c90b659 @falconindy add files from config prior to hooks
authored
241 # we always need the linker
242 file=$(readlink -e "$ld_so")
1c2c46f @falconindy try to warn the user when a build may have failed
authored
243 add_path_to_file "${ld_so#$basedir}" || (( ++errors ))
244 __add_slink "${ld_so#$basedir}" "${file#$basedir}" || (( ++errors ))
245 __add_file "${file#$basedir}" "${file#$basedir}" 755 || (( ++errors ))
c90b659 @falconindy add files from config prior to hooks
authored
246
247 # add extra modules from config
248 for mod in ${modules[@]}; do
1c2c46f @falconindy try to warn the user when a build may have failed
authored
249 add_module "$mod" || { (( ++errors)); error "Module not found: $mod"; }
c90b659 @falconindy add files from config prior to hooks
authored
250 done
251
252 # add extra files from config
253 for file in "${files[@]}"; do
254 local src=${file%%::*}
255 local dest=${file##*::}
58c9f9f @falconindy go on a quoting spree.
authored
256 [[ "$dest" ]] || dest=$src
1c2c46f @falconindy try to warn the user when a build may have failed
authored
257 add_binary "$src" "$dest" || { (( ++errors)); error "File not found: $file"; }
c90b659 @falconindy add files from config prior to hooks
authored
258 done
259
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
260 # parse builder array
261 for builder in "${builders[@]}"; do
262 in_array "$builder" "${skipbuilders[@]}" && continue
263
58c9f9f @falconindy go on a quoting spree.
authored
264 if [[ ! -f "$builderdir/$builder" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
265 error "cannot find builder '$builder': No such file"
1c2c46f @falconindy try to warn the user when a build may have failed
authored
266 (( ++errors ))
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
267 continue
268 fi
269
87b0e96 @falconindy add subshell as the comment says there ought to be
authored
270 ( # subshell to prevent namespace pollution
271 . "$builderdir/$builder"
272 if ! type -t build >/dev/null; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
273 error "no build function found in builder '$builder'"
1e7cf0e @falconindy subshell repeat garbage
authored
274 exit 1
87b0e96 @falconindy add subshell as the comment says there ought to be
authored
275 fi
276
8584669 @falconindy replace old and busted output functions with makepkg's
authored
277 msg2 "Building: [%s]" "$builder"
87b0e96 @falconindy add subshell as the comment says there ought to be
authored
278 build
279 )
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
280 done
281
f6f7761 @falconindy refactor depmod creation
authored
282 # only create depmod files if there's modules added
58c9f9f @falconindy go on a quoting spree.
authored
283 if [[ -d "$buildroot$moduledir" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
284 msg "Generating module dependencies"
1c2c46f @falconindy try to warn the user when a build may have failed
authored
285 depmod -b "$buildroot" "$kernver" || (( ++errors ))
77b1458 @falconindy trim excess depmod files. we dont need all of them
authored
286 # trim excess depmod files
e37a454 @falconindy avoid using public API from within the API itself
authored
287 rm "$buildroot$moduledir"/modules.!(dep|alias|symbols)
f6f7761 @falconindy refactor depmod creation
authored
288 fi
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored
289
69e7d7e @falconindy throw errors on user-specfied file/module not found
authored
290 { # create delicious config
bac42f3 @falconindy move config creation to geninit
authored
291 printf '%s %d' '%MODULES%' "${#modules[*]}"
292 printf ' %s' "${modules[@]}"
293 printf '\n'
294
295 printf '%s' '%HOOKS%'
296 printf ' %s' "${builders[@]}"
297 printf '\n'
298 } > "$buildroot/config"
299
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
300 if (( dryrun )); then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
301 msg "Dry run complete. Use -g <path> to create an initramfs."
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
302 return
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
303 fi
304
036fdb1 @falconindy refactor addition of zopts to compress command
authored
305 [[ "$compress" != cat ]] && zopts=(${compressquirks[$compress]} '-9')
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
306
58c9f9f @falconindy go on a quoting spree.
authored
307 msg "Creating$([[ "$compress" == cat ]] || printf " $compress") initramfs: $imagename"
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
308
309 pushd "$buildroot" &>/dev/null
036fdb1 @falconindy refactor addition of zopts to compress command
authored
310 (( verbose )) && plain "compress cmd: $compress ${zopts[@]}"
7acf0f7 @falconindy use libarchive's bsdcpio instead of GNU cpio
authored
311 find . -print0 | bsdcpio -0oH newc | "$compress" "${zopts[@]}" > "$imagename.tmp"
7966d90 @falconindy fix stupid ordering bug that clobbered PIPESTATUS
authored
312 pipesave=("${PIPESTATUS[@]}") # save immediately
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
313 popd &>/dev/null
314
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
315 (( pipesave[0] )) && errmsg="failed to create filelist (find reported error)"
7acf0f7 @falconindy use libarchive's bsdcpio instead of GNU cpio
authored
316 (( pipesave[1] )) && errmsg="failed to create archive (bsdcpio reported error)"
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
317 (( pipesave[2] )) && errmsg="$compress reported error compressing image"
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
318
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
319 # check for fatal errors
58c9f9f @falconindy go on a quoting spree.
authored
320 [[ "$errmsg" ]] && { rm "$imagename.tmp"; die "$errmsg"; }
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
321
322 # less than fatal errors
323 if (( !errors )); then
324 msg "Image creation completed successfully"
325 else
326 warning "Errors were encountered during creation. The image may not be complete."
327 fi
328
329 mv "$imagename"{.tmp,}
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
330
1c2c46f @falconindy try to warn the user when a build may have failed
authored
331 return 0 # ignore error when (( errors )) evalutes to false
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
332 } # }}}
333
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
334 __build_preset() { # {{{
b6714ed @falconindy ARRRGH. always use local inside a function
authored
335 local preset= presetfile=$_sysconfdir/geninit.d/$1.preset
336 local var= cfg= opts= imagename=
337 local -a presets
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
338
58c9f9f @falconindy go on a quoting spree.
authored
339 if [[ ! -f "$basedir$presetfile" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
340 error "preset not found: $1"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
341 return $ENOENT
342 fi
343
344 if ! . "$basedir$presetfile"; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
345 error "failed to read preset: $1"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
346 return $EACCES
347 fi
348
349 # preset pulls in a known array 'presets'
350 for preset in "${presets[@]}"; do
ba1417a @falconindy allow disabling presets with a prefixed !
authored
351 if [[ ${preset:0:1} == ! ]]; then # marked disabled
352 continue;
353 fi
354
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
355 # resolve kernel version
58c9f9f @falconindy go on a quoting spree.
authored
356 if [[ -z "$ALL_kver" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
357 error "No kernel version defined for preset \`$1'"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
358 return $EINVAL
359 fi
360
361 # resolve image name
362 var=${preset}_image
639fcd1 @falconindy tighten up preset var parsing
authored
363 imagename=${!var}
58c9f9f @falconindy go on a quoting spree.
authored
364 if [[ -z "$imagename" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
365 error "No imagename defined for preset \`$1'"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
366 return $EINVAL
367 fi
368
639fcd1 @falconindy tighten up preset var parsing
authored
369 # use extra config, falling back on ALL_config, falling back on the default
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
370 var=${preset}_config
639fcd1 @falconindy tighten up preset var parsing
authored
371 cfg=${!var:-${ALL_config:-$_sysconfigdir/geninit.conf}}
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
372
373 # extra options are optional
374 var=${preset}_options
375 opts=${!var}
376
377 # this should always resolve to something (i hope)
58c9f9f @falconindy go on a quoting spree.
authored
378 if [[ ! -f "$basedir$cfg" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
379 error "preset config file not found: \`$basedir$cfg'"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
380 return $ENOENT
381 fi
382
d2f545c @falconindy add verbose mode, listing additions to the image
authored
383 # explicitly retain some options
384 (( verbose )) && opts+=' -v'
385 (( !color )) && opts+=' -C'
386
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
387 # we've got enough to relaunch geninit now
8584669 @falconindy replace old and busted output functions with makepkg's
authored
388 msg "Building image from preset: $1-$preset"
389 msg2 "-k $ALL_kver -c $cfg -g $basedir$imagename $opts"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
390 "$0" -b "$basedir" -k "$ALL_kver" -c "$cfg" -g "$basedir$imagename" $opts || return 1
391 echo
392 done
393 } # }}}
394
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored
395 __kmodinfo() { # {{{
396 modinfo -0k $kernver "$@"
397 } # }}}
398
399 __ldd() { # {{{
400 LD_TRACE_LOADED_OBJECTS=1 "$ld_so" "$@"
401 } # }}}
402
75052c2 @falconindy initial commit
authored
403 # its a trap!
67de740 @falconindy allow cleanup to take a message as a second param
authored
404 trap 'cleanup 130 "Aborted by user! Exiting..."' SIGINT
405 trap 'cleanup 143 "TERM signal caught. Exiting..."' SIGTERM
75052c2 @falconindy initial commit
authored
406
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
407 # source public API
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
408 . "$_sharedir/geninit.api"
75052c2 @falconindy initial commit
authored
409
f2136d7 @falconindy add quirks file for quirky modules
authored
410 # source module quirks file
411 . "$_sharedir/geninit.quirks"
412
75052c2 @falconindy initial commit
authored
413 # ------------------ #
414 ## option parsing ##
415 # ------------------ #
416
d2f545c @falconindy add verbose mode, listing additions to the image
authored
417 while getopts ':b:Cc:g:H:hk:LMp:S:st:vz:' flag; do
75052c2 @falconindy initial commit
authored
418 case $flag in
419 b) basedir=$OPTARG ;;
420 c) config=$OPTARG ;;
8584669 @falconindy replace old and busted output functions with makepkg's
authored
421 C) color=0 ;;
75052c2 @falconindy initial commit
authored
422 g) imagename=$OPTARG ;;
38f816a @falconindy add -H and -L options for getting help with builders
authored
423 H) builderhelp $OPTARG ;;
25a82f8 @falconindy add a usage function
authored
424 h) usage ;;
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
425 k) kern=$OPTARG ;;
38f816a @falconindy add -H and -L options for getting help with builders
authored
426 L) builderlist ;;
a7370a0 @falconindy add -M option for dumping autodetected modules
authored
427 M) automods=1 ;;
1f2aff1 @falconindy add support for kernel presets
authored
428 p) preset=$OPTARG ;;
429 S) skipbuilders=(${OPTARG//,/ }) ;;
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
430 s) savebuildroot=1 ;;
54d2b38 @falconindy The -t option should set a base tmpdir, not the workdir itself
authored
431 t) otmpdir=$OPTARG ;;
d2f545c @falconindy add verbose mode, listing additions to the image
authored
432 v) verbose=1 ;;
75052c2 @falconindy initial commit
authored
433 z) ocompress=$OPTARG ;; # named differently to allow overriding
434 :) die "option requires an argument -- '$OPTARG'" ;;
435 \?) die "invalid option -- '$OPTARG'" ;;
436 esac
437 done
438
ddb5e48 @falconindy use declare for all globals
authored
439 # Alter PATH. We want to make sure that /bin and /sbin are favored, since
440 # we specifically rely on GNU coreutils
441 PATH=/sbin:/bin:$PATH
442
8584669 @falconindy replace old and busted output functions with makepkg's
authored
443 if [[ -t 2 ]] && (( color )); then
444 # prefer terminal safe colored and bold text when tput is supported
445 if tput setaf 0 &>/dev/null; then
446 NC="$(tput sgr0)"
447 BOLD="$(tput bold)"
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
448 BLUE="$BOLD$(tput setaf 4)"
449 GREEN="$BOLD$(tput setaf 2)"
450 RED="$BOLD$(tput setaf 1)"
451 YELLOW="$BOLD$(tput setaf 3)"
8584669 @falconindy replace old and busted output functions with makepkg's
authored
452 else
453 NC="\e[1;0m"
454 BOLD="\e[1;1m"
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
455 BLUE="$BOLD\e[1;34m"
456 GREEN="$BOLD\e[1;32m"
457 RED="$BOLD\e[1;31m"
458 YELLOW="$BOLD\e[1;33m"
8584669 @falconindy replace old and busted output functions with makepkg's
authored
459 fi
460 fi
461 readonly NC BOLD BLUE GREEN RED YELLOW
462
58c9f9f @falconindy go on a quoting spree.
authored
463 if [[ "$preset" ]]; then
ee64612 @falconindy catch $preset earlier, to avoid needless sanity checks
authored
464 __build_preset "$preset"
465 exit $?
466 fi
467
75052c2 @falconindy initial commit
authored
468 # ----------------- #
469 ## sanity checks ##
470 # ----------------- #
471
c8b5677 @falconindy check basedir first. make sure the config check is basedir aware
authored
472 # if specified, does the basedir exist? trim any trailing slash
58c9f9f @falconindy go on a quoting spree.
authored
473 if [[ "$basedir" ]]; then
c8b5677 @falconindy check basedir first. make sure the config check is basedir aware
authored
474 basedir=${basedir%/}
475
476 # if $basedir still exists (some noodle noggin might use -b /), make sure the
477 # dir can be resolved
58c9f9f @falconindy go on a quoting spree.
authored
478 [[ "$basedir" && ! -d $basedir ]] && die "basedir \`$basedir' not found"
c8b5677 @falconindy check basedir first. make sure the config check is basedir aware
authored
479 fi
480
481 # make sure our config exists, and source it
482 config=${config:-$baseconfig}
58c9f9f @falconindy go on a quoting spree.
authored
483 [[ ! -f "$basedir$config" ]] && die "failed to find config file: \`$basedir$config'" || . "$basedir$config"
c8b5677 @falconindy check basedir first. make sure the config check is basedir aware
authored
484
9c03a6d @falconindy fix linker resolution for multilib platforms
authored
485 # declared as an array to force expansion. we have to be careful that the wrong
486 # linker isn't picked for mulitlib systems, so we branch on $carch.
487 case $carch in
488 i686) ld_so=("$basedir"/lib/ld-linux.so.?*) ;;
489 x86_64) ld_so=("$basedir"/lib/ld-linux-${carch//_/-}.so.?*) ;;
490 *) die "unknown architecture: $carch" ;;
491 esac
492
75052c2 @falconindy initial commit
authored
493 if (( ${#ld_so[*]} != 1 )); then # uh oh...
494 die "failed to resolve the location of /lib/ld.so. Please report this bug."
495 fi
496
54d2b38 @falconindy The -t option should set a base tmpdir, not the workdir itself
authored
497 # mktemp takes care of our error handling here
0daef77 @falconindy oops. use the proper default value expansion
authored
498 tmpdir=$(readlink -e "$(mktemp -d "${otmpdir:-/tmp}"/${0##*/}.XXXXXX)") || cleanup 1
6dda899 @falconindy allow changing location of build dir
authored
499
9e86cb0 @falconindy slight refactor on kern => kernver logic
authored
500 # does the kernel exist inside the basedir? we may need to resolve it...
58c9f9f @falconindy go on a quoting spree.
authored
501 [[ "$kern" ]] && kernver=$(__get_kernver "$kern")
502 [[ "$kernver" ]] || die "Invalid kernel specifier: $kern"
75052c2 @falconindy initial commit
authored
503
504 # is our supplied compression method (if supplied) valid?
505 compress=${ocompress:-$compress}
73b26ca @falconindy always use == for equality
authored
506 if [[ -z $compress || $compress == none ]]; then
6048c99 @falconindy allow 'none' as a compression option
authored
507 compress=cat # NOOP compressor
508 else
1a5e61c @falconindy Add support for LZO compression via lzop
authored
509 [[ "$compress" == @(gzip|bzip2|lzop|lzma|xz) ]] || die "unknown compression method: $compress"
75052c2 @falconindy initial commit
authored
510 type -P "$compress" >/dev/null || die "failed to find \`$compress' binary in PATH"
511 fi
512
340a7c4 @falconindy add support for dry runs
authored
513 # if $imagename is provided, its path needs to be valid
58c9f9f @falconindy go on a quoting spree.
authored
514 if [[ "$imagename" ]]; then
aeea626 @falconindy fail when imagepath's dirname isn't writeable
authored
515 imagepath=$(readlink -f "$imagename")
58c9f9f @falconindy go on a quoting spree.
authored
516 [[ "$imagepath" ]] || die "invalid path to imagename: $imagename"
aeea626 @falconindy fail when imagepath's dirname isn't writeable
authored
517 [[ -w ${imagepath%/*} ]] || die "no permission to write to specified path: \`${imagepath%/*}'"
340a7c4 @falconindy add support for dry runs
authored
518 else # no $imagename, so we're doing a dry run
519 dryrun=1
520 fi
75052c2 @falconindy initial commit
authored
521
522 # ------------ #
523 ## int main ##
524 # ------------ #
525
6dda899 @falconindy allow changing location of build dir
authored
526 # define a few more paths for convenience
cee912e @falconindy mark static variables as readonly
authored
527 declare -r buildroot=$tmpdir/root
528 declare -r autodetect_cache=$tmpdir/autodetect.cache
529 declare -r moduledir=/lib/modules/$kernver
75052c2 @falconindy initial commit
authored
530
a7370a0 @falconindy add -M option for dumping autodetected modules
authored
531 if (( automods )); then
532 . "$_sharedir/builders/autodetect" || die "unable to source autodetect builder"
533 build
8584669 @falconindy replace old and busted output functions with makepkg's
authored
534 msg "Autodetected modules"
58c9f9f @falconindy go on a quoting spree.
authored
535 [[ -s "$autodetect_cache" ]] && cat "$autodetect_cache"
a7370a0 @falconindy add -M option for dumping autodetected modules
authored
536 cleanup 0
537 fi
538
ee64612 @falconindy catch $preset earlier, to avoid needless sanity checks
authored
539 __build_image
75052c2 @falconindy initial commit
authored
540
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
541 cleanup $?
75052c2 @falconindy initial commit
authored
542
49078e2 @falconindy general builder code standardized/cleanup
authored
543 # vim: set et sw=2 ft=sh:
Something went wrong with that request. Please try again.