Skip to content
Newer
Older
100755 455 lines (370 sloc) 12.1 KB
75052c2 @falconindy initial commit
authored Apr 26, 2011
1 #!/bin/bash
e5a8d5b @falconindy standardize file headers and add MIT licensee
authored May 1, 2011
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 Apr 27, 2011
25
26 shopt -s extglob
d51765f @falconindy fix linker resolution for i686 and multilib
authored Apr 30, 2011
27 shopt -s nullglob
75052c2 @falconindy initial commit
authored Apr 27, 2011
28
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored Apr 29, 2011
29 # tender vittles
cee912e @falconindy mark static variables as readonly
authored May 2, 2011
30 declare -r _sysconfdir=/etc
31 declare -r _sharedir=.
32 declare -r builderdir=${_sharedir}/builders
33 declare -r baseconfig=${_sysconfdir}/geninit.conf
34 declare -r carch=$(uname -m)
ddb5e48 @falconindy use declare for all globals
authored Apr 30, 2011
35 declare kernver=$(uname -r)
75052c2 @falconindy initial commit
authored Apr 27, 2011
36
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored Apr 29, 2011
37 # error codes
cee912e @falconindy mark static variables as readonly
authored May 3, 2011
38 declare -ri ENOENT=2
39 declare -ri EACCES=13
40 declare -ri EEXIST=17
41 declare -ri EINVAL=22
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored Apr 29, 2011
42
43 # globals
44 declare -i automods=0
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
45 declare -i savebuildroot=0
f1562ff @falconindy dryrun is an integer value
authored May 1, 2011
46 declare -i dryrun=0
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored Apr 29, 2011
47 declare basedir=
48 declare config=
49 declare imagename=
50 declare preset=
51 declare skipbuilders=
52 declare ocompress=
53
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 27, 2011
54 # -------------------------- #
55 ## general util functions ##
56 # -------------------------- #
75052c2 @falconindy initial commit
authored Apr 27, 2011
57
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
58 err() { # {{{
75052c2 @falconindy initial commit
authored Apr 27, 2011
59 printf 'error: %s\n' "$*"
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
60 } >&2 # }}}
75052c2 @falconindy initial commit
authored Apr 27, 2011
61
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
62 info() { # {{{
75052c2 @falconindy initial commit
authored Apr 27, 2011
63 printf ':: %s\n' "$*"
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
64 } # }}}
75052c2 @falconindy initial commit
authored Apr 27, 2011
65
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
66 die() { # {{{
75052c2 @falconindy initial commit
authored Apr 27, 2011
67 err "$*"
68 cleanup 1
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
69 } # }}}
75052c2 @falconindy initial commit
authored Apr 27, 2011
70
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
71 in_array() { # {{{
75052c2 @falconindy initial commit
authored Apr 27, 2011
72 local item needle=$1; shift
73 [[ -z $1 ]] && return 1 # Not Found
74
75 for item; do
73b26ca @falconindy always use == for equality
authored May 2, 2011
76 [[ $item == $needle ]] && return 0 # Found
75052c2 @falconindy initial commit
authored Apr 27, 2011
77 done
78 return 1 # Not Found
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
79 } # }}}
75052c2 @falconindy initial commit
authored Apr 27, 2011
80
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
81 cleanup() { # {{{
fd3a06c @falconindy dont save the build root if it was never created
authored May 1, 2011
82 if (( savebuildroot )) && [[ -d $buildroot ]]; then
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
83 info "buildroot preserved at: $buildroot"
84 else
85 rm -rf "$tmpdir"
86 fi
75052c2 @falconindy initial commit
authored Apr 27, 2011
87 exit $1
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
88 } # }}}
75052c2 @falconindy initial commit
authored Apr 27, 2011
89
25a82f8 @falconindy add a usage function
authored Apr 28, 2011
90 usage() { # {{{
91 cat<<USAGE
a113c0c @falconindy cleanup usage function
authored Apr 30, 2011
92 usage: geninit [options]
25a82f8 @falconindy add a usage function
authored Apr 29, 2011
93
94 Options:
95 -b <path> Use base directory (default: /)
96 -c <file> Specify a different config file (default: $_sysconfdir/geninit.conf)
97 -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 Apr 30, 2011
98 -H <builder> Display help for a specified builder
25a82f8 @falconindy add a usage function
authored Apr 29, 2011
99 -h Display this help message
a113c0c @falconindy cleanup usage function
authored Apr 30, 2011
100 -k <kver> Specify a kernel version (default: $kernver)
38f816a @falconindy add -H and -L options for getting help with builders
authored Apr 30, 2011
101 -L List available builders
a113c0c @falconindy cleanup usage function
authored Apr 30, 2011
102 -p <preset> Build using a preset file (any of $_sysconfdir/geninit.d/*.preset)
25a82f8 @falconindy add a usage function
authored Apr 29, 2011
103 -S <names> Skip the specified builders during the build process
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
104 -s Preserve buildroot after image creation
6dda899 @falconindy allow changing location of build dir
authored May 1, 2011
105 -t <path> Specify an alternate location for temporary workspace
25a82f8 @falconindy add a usage function
authored Apr 29, 2011
106 -z <method> Override compression method
107
108 USAGE
109 exit
110 } # }}}
111
38f816a @falconindy add -H and -L options for getting help with builders
authored Apr 30, 2011
112 builderlist() { # {{{
113 local -a builders=("$_sharedir"/builders/*)
114
115 (( ${#builders[*]} )) || die "no builders found"
116
117 info "Available builders -- use -H <builder> for help with a specific builder"
118 printf '%s\n' "${builders[@]##*/}" | column -c$(tput cols)
119 cleanup 0
120 } # }}}
121
122 builderhelp() { # {{{
123 local builder=$1
124
125 [[ -f $_sharedir/builders/$builder ]] || die "no such builder: $builder"
126 . "$_sharedir/builders/$builder"
127 type -t helpmsg >/dev/null || die "no help for builder: $builder"
128 info "$builder"
129 helpmsg
130 cleanup 0
131 } # }}}
132
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored Apr 28, 2011
133 # --------------- #
134 ## private API ##
135 # --------------- #
75052c2 @falconindy initial commit
authored Apr 27, 2011
136
137 __add_file() { # {{{
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
138 # add a file to $buildroot
75052c2 @falconindy initial commit
authored Apr 27, 2011
139 # $1: pathname on initcpio
140 # $2: source on disk
141 # $3: mode
142
143 (( $# == 3 )) || return $EINVAL
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
144 [[ -e $buildroot$1 ]] && return $EEXIST
75052c2 @falconindy initial commit
authored Apr 27, 2011
145
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
146 install -Dm$3 "$2" "$buildroot$1"
75052c2 @falconindy initial commit
authored Apr 27, 2011
147 } # }}}
148
149 __add_dir() { # {{{
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
150 # add a directory (with parents) to $buildroot
75052c2 @falconindy initial commit
authored Apr 27, 2011
151 # $1: pathname on initcpio
152 # $2: mode
153
154 (( $# == 2 )) || [[ $1 == /?* ]] || return $EINVAL
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
155 [[ -e $buildroot$1 ]] && return $EEXIST
75052c2 @falconindy initial commit
authored Apr 27, 2011
156
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
157 install -dm$2 "$buildroot$1"
75052c2 @falconindy initial commit
authored Apr 27, 2011
158 } # }}}
159
160 __add_pipe() { # {{{
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
161 # add a pipe to $buildroot
75052c2 @falconindy initial commit
authored Apr 27, 2011
162 # $1: pathname on initcpio
163 # $2: mode
164
165 (( $# == 2 )) || return $EINVAL
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
166 [[ -e $buildroot$1 ]] && return $EEXIST
75052c2 @falconindy initial commit
authored Apr 27, 2011
167
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
168 mkfifo -m$2 "$buildroot$1"
75052c2 @falconindy initial commit
authored Apr 27, 2011
169 } # }}}
170
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored May 2, 2011
171 __add_slink() { # {{{
172 # add a symlink to $buildroot
173 # $1: name on initcpio
174 # $2: target of $1
175
176 (( $# == 2 )) || return $EINVAL
177 [[ -L $buildroot$1 ]] && return $EEXIST
178
179 ln -s "$2" "$buildroot$1"
180 } # }}}
181
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 28, 2011
182 __build_image() { # {{{
b6714ed @falconindy ARRRGH. always use local inside a function
authored Apr 29, 2011
183 local -i modcount=0
184 local -a zopts pipesave
185 local builder= module= line= mod= file=
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
186
f6f7761 @falconindy refactor depmod creation
authored Apr 29, 2011
187 # this must be resolved to an absolute path
1d41e24 @falconindy be less strict about checking imagename resolution during build
authored Apr 29, 2011
188 if [[ $imagename && ${imagename:0:1} != / ]]; then
f6f7761 @falconindy refactor depmod creation
authored Apr 29, 2011
189 imagename=$(readlink -f "$imagename")
1d41e24 @falconindy be less strict about checking imagename resolution during build
authored Apr 30, 2011
190 [[ $imagename ]] || die "Failed to resolve path to imagename"
f6f7761 @falconindy refactor depmod creation
authored Apr 29, 2011
191 fi
192
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
193 # parse builder array
194 for builder in "${builders[@]}"; do
195 in_array "$builder" "${skipbuilders[@]}" && continue
196
197 if [[ ! -f $builderdir/$builder ]]; then
198 err "cannot find builder '$builder': No such file"
199 continue
200 fi
201
f2136d7 @falconindy add quirks file for quirky modules
authored Apr 28, 2011
202 # subshell to prevent namespace pollution
203 . "$builderdir/$builder"
204 if ! type -t build >/dev/null; then
205 err "no build function found in builder '$builder'"
206 continue
207 fi
208
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
209 info "Building: [$builder]"
f2136d7 @falconindy add quirks file for quirky modules
authored Apr 29, 2011
210 build
7b78689 @falconindy implement module filtering for add_driver_classes
authored Apr 30, 2011
211 unset modulefilter
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
212 done
213
214 # add extra modules from config
215 for mod in ${modules[@]}; do
69e7d7e @falconindy throw errors on user-specfied file/module not found
authored Apr 29, 2011
216 add_module "$mod" || err "Module not found: $mod"
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
217 done
218
219 # add extra files from config
220 for file in "${files[@]}"; do
ff6854c @falconindy uphold geninit.conf's claim of multiple forms to files=()
authored May 1, 2011
221 local src=${file%%::*}
222 local dest=${file##*::}
223 [[ $dest ]] || dest=$src
224 add_binary "$src" "$dest" || err "File not found: $file"
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
225 done
226
6f6f62b @falconindy resolved linker once, during image creation
authored May 1, 2011
227 # we always need the linker
ebcbd8f @falconindy no need to check if the linker is there yet. it wont be. and it needs…
authored May 1, 2011
228 file=$(readlink -e "$ld_so")
229 __add_slink "${ld_so#$basedir}" "${file#$basedir}"
230 __add_file "${file#$basedir}" "${file#$basedir}" 755
6f6f62b @falconindy resolved linker once, during image creation
authored May 1, 2011
231
f6f7761 @falconindy refactor depmod creation
authored Apr 29, 2011
232 # only create depmod files if there's modules added
e37a454 @falconindy avoid using public API from within the API itself
authored Apr 29, 2011
233 if [[ -d $buildroot$moduledir ]]; then
f6f7761 @falconindy refactor depmod creation
authored Apr 29, 2011
234 info "Generating module dependencies"
235 depmod -b "$buildroot" "$kernver"
77b1458 @falconindy trim excess depmod files. we dont need all of them
authored Apr 29, 2011
236 # trim excess depmod files
e37a454 @falconindy avoid using public API from within the API itself
authored Apr 29, 2011
237 rm "$buildroot$moduledir"/modules.!(dep|alias|symbols)
f6f7761 @falconindy refactor depmod creation
authored Apr 29, 2011
238 fi
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored Apr 29, 2011
239
69e7d7e @falconindy throw errors on user-specfied file/module not found
authored Apr 30, 2011
240 { # create delicious config
bac42f3 @falconindy move config creation to geninit
authored Apr 29, 2011
241 printf '%s %d' '%MODULES%' "${#modules[*]}"
242 printf ' %s' "${modules[@]}"
243 printf '\n'
244
245 printf '%s' '%HOOKS%'
246 printf ' %s' "${builders[@]}"
247 printf '\n'
248 } > "$buildroot/config"
249
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
250 if (( dryrun )); then
251 info "Dry run complete. Use -g <path> to create an initramfs."
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
252 return
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
253 fi
254
255 if [[ $compress != cat ]]; then
256 zopts=('-9')
73b26ca @falconindy always use == for equality
authored May 3, 2011
257 [[ $compress == xz ]] && zopts+=('--check=crc32')
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
258 fi
259
73b26ca @falconindy always use == for equality
authored May 3, 2011
260 info "Creating$([[ $compress == cat ]] || printf " $compress") initramfs: $imagename"
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
261
262 pushd "$buildroot" &>/dev/null
263 find . -print0 | cpio -0oH newc | "$compress" "${zopts[@]}" > "$imagename"
7966d90 @falconindy fix stupid ordering bug that clobbered PIPESTATUS
authored May 2, 2011
264 pipesave=("${PIPESTATUS[@]}") # save immediately
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
265 popd &>/dev/null
266
267 (( pipesave[0] )) && die "failed to create filelist (find reported error)"
268 (( pipesave[1] )) && die "failed to create cpio archive (cpio reported error)"
269 (( pipesave[2] )) && die "$compress reported error compressing image"
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
270
271 info "Image creation completed successfully"
272
273 } # }}}
274
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored Apr 29, 2011
275 __build_preset() { # {{{
b6714ed @falconindy ARRRGH. always use local inside a function
authored Apr 29, 2011
276 local preset= presetfile=$_sysconfdir/geninit.d/$1.preset
277 local var= cfg= opts= imagename=
278 local -a presets
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored Apr 29, 2011
279
280 if [[ ! -f $basedir$presetfile ]]; then
281 err "preset not found: $1"
282 return $ENOENT
283 fi
284
285 if ! . "$basedir$presetfile"; then
286 err "failed to read preset: $1"
287 return $EACCES
288 fi
289
290 # preset pulls in a known array 'presets'
291 for preset in "${presets[@]}"; do
ba1417a @falconindy allow disabling presets with a prefixed !
authored Apr 30, 2011
292 if [[ ${preset:0:1} == ! ]]; then # marked disabled
293 continue;
294 fi
295
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored Apr 29, 2011
296 # resolve kernel version
297 if [[ -z $ALL_kver ]]; then
298 err "No kernel version defined for preset \`$1'"
299 return $EINVAL
300 fi
301
302 # resolve image name
303 var=${preset}_image
639fcd1 @falconindy tighten up preset var parsing
authored May 2, 2011
304 imagename=${!var}
305 if [[ -z $imagename ]]; then
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored Apr 29, 2011
306 err "No imagename defined for preset \`$1'"
307 return $EINVAL
308 fi
309
639fcd1 @falconindy tighten up preset var parsing
authored May 3, 2011
310 # 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 Apr 29, 2011
311 var=${preset}_config
639fcd1 @falconindy tighten up preset var parsing
authored May 3, 2011
312 cfg=${!var:-${ALL_config:-$_sysconfigdir/geninit.conf}}
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored Apr 29, 2011
313
314 # extra options are optional
315 var=${preset}_options
316 opts=${!var}
317
318 # this should always resolve to something (i hope)
319 if [[ ! -f $basedir$cfg ]]; then
320 err "preset config file not found: \`$basedir$cfg'"
321 return $ENOENT
322 fi
323
324 # we've got enough to relaunch geninit now
325 # TODO: don't relaunch. Do this the sane way.
326 info "Building image from preset: $1-$preset"
327 printf ' ==> %s\n' "-k $ALL_kver -c $cfg -g $basedir$imagename $opts"
328 "$0" -b "$basedir" -k "$ALL_kver" -c "$cfg" -g "$basedir$imagename" $opts || return 1
329 echo
330 done
331 } # }}}
332
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored May 3, 2011
333 __kmodinfo() { # {{{
334 modinfo -0k $kernver "$@"
335 } # }}}
336
337 __ldd() { # {{{
338 LD_TRACE_LOADED_OBJECTS=1 "$ld_so" "$@"
339 } # }}}
340
75052c2 @falconindy initial commit
authored Apr 27, 2011
341 # its a trap!
342 trap 'cleanup 130' SIGINT
343 trap 'cleanup 143' SIGTERM
344
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored Apr 29, 2011
345 # source public API
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored Apr 28, 2011
346 . "$_sharedir/geninit.api"
75052c2 @falconindy initial commit
authored Apr 27, 2011
347
f2136d7 @falconindy add quirks file for quirky modules
authored Apr 29, 2011
348 # source module quirks file
349 . "$_sharedir/geninit.quirks"
350
75052c2 @falconindy initial commit
authored Apr 27, 2011
351 # ------------------ #
352 ## option parsing ##
353 # ------------------ #
354
6dda899 @falconindy allow changing location of build dir
authored May 1, 2011
355 while getopts ':b:c:g:H:hk:LMp:S:st:z:' flag; do
75052c2 @falconindy initial commit
authored Apr 27, 2011
356 case $flag in
357 b) basedir=$OPTARG ;;
358 c) config=$OPTARG ;;
359 g) imagename=$OPTARG ;;
38f816a @falconindy add -H and -L options for getting help with builders
authored Apr 30, 2011
360 H) builderhelp $OPTARG ;;
25a82f8 @falconindy add a usage function
authored Apr 29, 2011
361 h) usage ;;
75052c2 @falconindy initial commit
authored Apr 27, 2011
362 k) kernver=$OPTARG ;;
38f816a @falconindy add -H and -L options for getting help with builders
authored Apr 30, 2011
363 L) builderlist ;;
a7370a0 @falconindy add -M option for dumping autodetected modules
authored Apr 29, 2011
364 M) automods=1 ;;
1f2aff1 @falconindy add support for kernel presets
authored Apr 28, 2011
365 p) preset=$OPTARG ;;
366 S) skipbuilders=(${OPTARG//,/ }) ;;
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored Apr 29, 2011
367 s) savebuildroot=1 ;;
54d2b38 @falconindy The -t option should set a base tmpdir, not the workdir itself
authored May 2, 2011
368 t) otmpdir=$OPTARG ;;
75052c2 @falconindy initial commit
authored Apr 27, 2011
369 z) ocompress=$OPTARG ;; # named differently to allow overriding
370 :) die "option requires an argument -- '$OPTARG'" ;;
371 \?) die "invalid option -- '$OPTARG'" ;;
372 esac
373 done
374
ddb5e48 @falconindy use declare for all globals
authored Apr 30, 2011
375 # Alter PATH. We want to make sure that /bin and /sbin are favored, since
376 # we specifically rely on GNU coreutils
377 PATH=/sbin:/bin:$PATH
378
75052c2 @falconindy initial commit
authored Apr 27, 2011
379 # ----------------- #
380 ## sanity checks ##
381 # ----------------- #
382
9c03a6d @falconindy fix linker resolution for multilib platforms
authored May 1, 2011
383 # declared as an array to force expansion. we have to be careful that the wrong
384 # linker isn't picked for mulitlib systems, so we branch on $carch.
385 case $carch in
386 i686) ld_so=("$basedir"/lib/ld-linux.so.?*) ;;
387 x86_64) ld_so=("$basedir"/lib/ld-linux-${carch//_/-}.so.?*) ;;
388 *) die "unknown architecture: $carch" ;;
389 esac
390
75052c2 @falconindy initial commit
authored Apr 27, 2011
391 if (( ${#ld_so[*]} != 1 )); then # uh oh...
392 die "failed to resolve the location of /lib/ld.so. Please report this bug."
393 fi
394
395 # make sure our config exists, and source it
396 config=${config:-$baseconfig}
397 [[ ! -f $config ]] && die "failed to find config file: \`$config'" || . "$config"
398
e17a205 @falconindy fix inaccurate comment wrt basedir
authored Apr 29, 2011
399 # if specified, does the basedir exist? trim any trailing slash
75052c2 @falconindy initial commit
authored Apr 27, 2011
400 if [[ $basedir ]]; then
401 basedir=${basedir%/}
929cb54 @falconindy make an unfortunate exception for passing -b /
authored Apr 30, 2011
402 [[ $basedir ]] && { [[ -d $basedir ]] || die "basedir \`$basedir' not found"; }
75052c2 @falconindy initial commit
authored Apr 27, 2011
403 fi
404
54d2b38 @falconindy The -t option should set a base tmpdir, not the workdir itself
authored May 3, 2011
405 # mktemp takes care of our error handling here
0daef77 @falconindy oops. use the proper default value expansion
authored May 2, 2011
406 tmpdir=$(readlink -e "$(mktemp -d "${otmpdir:-/tmp}"/${0##*/}.XXXXXX)") || cleanup 1
6dda899 @falconindy allow changing location of build dir
authored May 1, 2011
407
75052c2 @falconindy initial commit
authored Apr 27, 2011
408 # does the kernel exist inside the basedir?
409 [[ -d $basedir/lib/modules/$kernver ]] || die "kernel \`$kernver' not found"
410
411 # is our supplied compression method (if supplied) valid?
412 compress=${ocompress:-$compress}
73b26ca @falconindy always use == for equality
authored May 3, 2011
413 if [[ -z $compress || $compress == none ]]; then
6048c99 @falconindy allow 'none' as a compression option
authored Apr 28, 2011
414 compress=cat # NOOP compressor
415 else
73b26ca @falconindy always use == for equality
authored May 3, 2011
416 [[ $compress == @(gzip|bzip2|lzma|xz) ]] || die "unknown compression method: $compress"
75052c2 @falconindy initial commit
authored Apr 27, 2011
417 type -P "$compress" >/dev/null || die "failed to find \`$compress' binary in PATH"
418 fi
419
340a7c4 @falconindy add support for dry runs
authored Apr 28, 2011
420 # if $imagename is provided, its path needs to be valid
421 if [[ $imagename ]]; then
aeea626 @falconindy fail when imagepath's dirname isn't writeable
authored Apr 28, 2011
422 imagepath=$(readlink -f "$imagename")
423 [[ $imagepath ]] || die "invalid path to imagename: $imagename"
424 [[ -w ${imagepath%/*} ]] || die "no permission to write to specified path: \`${imagepath%/*}'"
340a7c4 @falconindy add support for dry runs
authored Apr 28, 2011
425 else # no $imagename, so we're doing a dry run
426 dryrun=1
427 fi
75052c2 @falconindy initial commit
authored Apr 27, 2011
428
429 # ------------ #
430 ## int main ##
431 # ------------ #
432
6dda899 @falconindy allow changing location of build dir
authored May 1, 2011
433 # define a few more paths for convenience
cee912e @falconindy mark static variables as readonly
authored May 3, 2011
434 declare -r buildroot=$tmpdir/root
435 declare -r autodetect_cache=$tmpdir/autodetect.cache
436 declare -r moduledir=/lib/modules/$kernver
75052c2 @falconindy initial commit
authored Apr 27, 2011
437
a7370a0 @falconindy add -M option for dumping autodetected modules
authored Apr 29, 2011
438 if (( automods )); then
439 . "$_sharedir/builders/autodetect" || die "unable to source autodetect builder"
440 build
441 info "Autodetected modules"
442 [[ -s $autodetect_cache ]] && cat "$autodetect_cache"
443 cleanup 0
444 fi
445
1f2aff1 @falconindy add support for kernel presets
authored Apr 28, 2011
446 if [[ $preset ]]; then
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored Apr 29, 2011
447 __build_preset "$preset"
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
448 else
80edf1c @falconindy dont report dry run when using a preset
authored Apr 29, 2011
449 (( dryrun )) && info "Starting dry run"
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
450 __build_image
75052c2 @falconindy initial commit
authored Apr 27, 2011
451 fi
452
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored Apr 29, 2011
453 cleanup $?
75052c2 @falconindy initial commit
authored Apr 27, 2011
454
Something went wrong with that request. Please try again.