Skip to content

HTTPS clone URL

Subversion checkout URL

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