Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 549 lines (447 sloc) 15.149 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
1b82455 @falconindy geninit: write msg/msg2/plan to stdout
authored
63 printf "$BOLD $mesg$NC\n" "$@" >&1
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
1b82455 @falconindy geninit: write msg/msg2/plan to stdout
authored
68 printf "$GREEN==>$NC$BOLD $mesg$NC\n" "$@" >&1
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
1b82455 @falconindy geninit: write msg/msg2/plan to stdout
authored
73 printf "$BLUE ->$NC$BOLD $mesg$NC\n" "$@" >&1
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
96bac76 @falconindy inject VERSION into geninit and lsinitramfs
authored
112 geninit %VERSION%
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
58c9f9f @falconindy go on a quoting spree.
authored
146 [[ -f "$_sharedir/builders/$builder" ]] || die "no such builder: $builder"
38f816a @falconindy add -H and -L options for getting help with builders
authored
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
58c9f9f @falconindy go on a quoting spree.
authored
161 if [[ "${kernel:0:1}" != / ]]; then
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
162 echo "$kernel"
163 return 0
164 fi
165
58c9f9f @falconindy go on a quoting spree.
authored
166 [[ -r "$kernel" ]] || return $ENOENT
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
167
58c9f9f @falconindy go on a quoting spree.
authored
168 read _ kernver < <(file -b "$basedir$kernel" | grep -o 'version [^ ]\+')
169 if [[ "$kernver" ]]; then
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
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
58c9f9f @falconindy go on a quoting spree.
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
58c9f9f @falconindy go on a quoting spree.
authored
195 (( $# == 2 )) || [[ "$1" == /?* ]] || return $EINVAL
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
58c9f9f @falconindy go on a quoting spree.
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
58c9f9f @falconindy go on a quoting spree.
authored
220 [[ -L "$buildroot$1" ]] && return $EEXIST
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored
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
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
229 local builder= module= line= mod= file= errmsg=
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
230
d406ecd @falconindy move final kernel version check to __build_image
authored
231 # check our kernel version
58c9f9f @falconindy go on a quoting spree.
authored
232 [[ -d "$basedir/lib/modules/$kernver" ]] || die "kernel \`$kernver' not found"
d406ecd @falconindy move final kernel version check to __build_image
authored
233
f6f7761 @falconindy refactor depmod creation
authored
234 # this must be resolved to an absolute path
58c9f9f @falconindy go on a quoting spree.
authored
235 if [[ "$imagename" && "${imagename:0:1}" != / ]]; then
f6f7761 @falconindy refactor depmod creation
authored
236 imagename=$(readlink -f "$imagename")
58c9f9f @falconindy go on a quoting spree.
authored
237 [[ "$imagename" ]] || die "Failed to resolve path to imagename"
f6f7761 @falconindy refactor depmod creation
authored
238 fi
239
d406ecd @falconindy move final kernel version check to __build_image
authored
240 (( dryrun )) && msg "Starting dry run: %s" "$kernver" || msg "Starting build: %s" "$kernver"
62bee4e @falconindy geninit: show config file in use with ((verbose))
authored
241 (( verbose )) && msg2 "using config file: %s" "$basedir$config"
d406ecd @falconindy move final kernel version check to __build_image
authored
242
c90b659 @falconindy add files from config prior to hooks
authored
243 # we always need the linker
244 file=$(readlink -e "$ld_so")
1c2c46f @falconindy try to warn the user when a build may have failed
authored
245 add_path_to_file "${ld_so#$basedir}" || (( ++errors ))
246 __add_slink "${ld_so#$basedir}" "${file#$basedir}" || (( ++errors ))
247 __add_file "${file#$basedir}" "${file#$basedir}" 755 || (( ++errors ))
c90b659 @falconindy add files from config prior to hooks
authored
248
249 # add extra modules from config
250 for mod in ${modules[@]}; do
00c0296 @falconindy geninit: dont error on EEXIST for adding explicit modules
authored
251 add_module "$mod"
252 [[ $? = @(0|$EEXIST) ]] || (( ++errors ))
c90b659 @falconindy add files from config prior to hooks
authored
253 done
254
255 # add extra files from config
256 for file in "${files[@]}"; do
257 local src=${file%%::*}
258 local dest=${file##*::}
b9f399c @falconindy api: fix src::dest file adding when not a binary
authored
259 [[ $dest ]] || dest=$src
260 add_binary "$src" "$dest" || { (( ++errors )); error "File not found: $file"; }
c90b659 @falconindy add files from config prior to hooks
authored
261 done
262
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
263 # parse builder array
264 for builder in "${builders[@]}"; do
265 in_array "$builder" "${skipbuilders[@]}" && continue
266
58c9f9f @falconindy go on a quoting spree.
authored
267 if [[ ! -f "$builderdir/$builder" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
268 error "cannot find builder '$builder': No such file"
1c2c46f @falconindy try to warn the user when a build may have failed
authored
269 (( ++errors ))
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
270 continue
271 fi
272
87b0e96 @falconindy add subshell as the comment says there ought to be
authored
273 ( # subshell to prevent namespace pollution
274 . "$builderdir/$builder"
275 if ! type -t build >/dev/null; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
276 error "no build function found in builder '$builder'"
1e7cf0e @falconindy subshell repeat garbage
authored
277 exit 1
87b0e96 @falconindy add subshell as the comment says there ought to be
authored
278 fi
279
8584669 @falconindy replace old and busted output functions with makepkg's
authored
280 msg2 "Building: [%s]" "$builder"
87b0e96 @falconindy add subshell as the comment says there ought to be
authored
281 build
282 )
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
283 done
284
f6f7761 @falconindy refactor depmod creation
authored
285 # only create depmod files if there's modules added
58c9f9f @falconindy go on a quoting spree.
authored
286 if [[ -d "$buildroot$moduledir" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
287 msg "Generating module dependencies"
1c2c46f @falconindy try to warn the user when a build may have failed
authored
288 depmod -b "$buildroot" "$kernver" || (( ++errors ))
77b1458 @falconindy trim excess depmod files. we dont need all of them
authored
289 # trim excess depmod files
bbba074 @falconindy geninit: use binary depmod files
authored
290 rm "$buildroot$moduledir"/modules.!(dep.bin|alias.bin|symbols.bin)
f6f7761 @falconindy refactor depmod creation
authored
291 fi
570ed43 @falconindy add -s option for saving the filelist. declare globals in an obvious …
authored
292
69e7d7e @falconindy throw errors on user-specfied file/module not found
authored
293 { # create delicious config
8b715d5 @falconindy __build_image: dont assume modules has a length
authored
294 if (( ${#modules[*]} )); then
295 printf '%s %d' '%MODULES%' "${#modules[*]}"
296 printf ' %s' "${modules[@]}"
297 printf '\n'
298 fi
bac42f3 @falconindy move config creation to geninit
authored
299
300 printf '%s' '%HOOKS%'
301 printf ' %s' "${builders[@]}"
302 printf '\n'
303 } > "$buildroot/config"
304
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
305 if (( dryrun )); then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
306 msg "Dry run complete. Use -g <path> to create an initramfs."
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
307 return
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
308 fi
309
036fdb1 @falconindy refactor addition of zopts to compress command
authored
310 [[ "$compress" != cat ]] && zopts=(${compressquirks[$compress]} '-9')
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
311
58c9f9f @falconindy go on a quoting spree.
authored
312 msg "Creating$([[ "$compress" == cat ]] || printf " $compress") initramfs: $imagename"
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
313
314 pushd "$buildroot" &>/dev/null
036fdb1 @falconindy refactor addition of zopts to compress command
authored
315 (( verbose )) && plain "compress cmd: $compress ${zopts[@]}"
7acf0f7 @falconindy use libarchive's bsdcpio instead of GNU cpio
authored
316 find . -print0 | bsdcpio -0oH newc | "$compress" "${zopts[@]}" > "$imagename.tmp"
7966d90 @falconindy fix stupid ordering bug that clobbered PIPESTATUS
authored
317 pipesave=("${PIPESTATUS[@]}") # save immediately
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
318 popd &>/dev/null
319
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
320 (( pipesave[0] )) && errmsg="failed to create filelist (find reported error)"
7acf0f7 @falconindy use libarchive's bsdcpio instead of GNU cpio
authored
321 (( pipesave[1] )) && errmsg="failed to create archive (bsdcpio reported error)"
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
322 (( pipesave[2] )) && errmsg="$compress reported error compressing image"
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
323
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
324 # check for fatal errors
58c9f9f @falconindy go on a quoting spree.
authored
325 [[ "$errmsg" ]] && { rm "$imagename.tmp"; die "$errmsg"; }
806dcde @falconindy rotate image into place to avoid bricking a good image
authored
326
327 # less than fatal errors
328 if (( !errors )); then
329 msg "Image creation completed successfully"
330 else
331 warning "Errors were encountered during creation. The image may not be complete."
332 fi
333
334 mv "$imagename"{.tmp,}
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
335
1c2c46f @falconindy try to warn the user when a build may have failed
authored
336 return 0 # ignore error when (( errors )) evalutes to false
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
337 } # }}}
338
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
339 __build_preset() { # {{{
b6714ed @falconindy ARRRGH. always use local inside a function
authored
340 local preset= presetfile=$_sysconfdir/geninit.d/$1.preset
341 local var= cfg= opts= imagename=
342 local -a presets
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
343
58c9f9f @falconindy go on a quoting spree.
authored
344 if [[ ! -f "$basedir$presetfile" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
345 error "preset not found: $1"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
346 return $ENOENT
347 fi
348
349 if ! . "$basedir$presetfile"; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
350 error "failed to read preset: $1"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
351 return $EACCES
352 fi
353
354 # preset pulls in a known array 'presets'
355 for preset in "${presets[@]}"; do
ba1417a @falconindy allow disabling presets with a prefixed !
authored
356 if [[ ${preset:0:1} == ! ]]; then # marked disabled
357 continue;
358 fi
359
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
360 # resolve kernel version
58c9f9f @falconindy go on a quoting spree.
authored
361 if [[ -z "$ALL_kver" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
362 error "No kernel version defined for preset \`$1'"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
363 return $EINVAL
364 fi
365
366 # resolve image name
367 var=${preset}_image
639fcd1 @falconindy tighten up preset var parsing
authored
368 imagename=${!var}
58c9f9f @falconindy go on a quoting spree.
authored
369 if [[ -z "$imagename" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
370 error "No imagename defined for preset \`$1'"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
371 return $EINVAL
372 fi
373
639fcd1 @falconindy tighten up preset var parsing
authored
374 # 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
375 var=${preset}_config
639fcd1 @falconindy tighten up preset var parsing
authored
376 cfg=${!var:-${ALL_config:-$_sysconfigdir/geninit.conf}}
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
377
378 # extra options are optional
379 var=${preset}_options
380 opts=${!var}
381
382 # this should always resolve to something (i hope)
58c9f9f @falconindy go on a quoting spree.
authored
383 if [[ ! -f "$basedir$cfg" ]]; then
8584669 @falconindy replace old and busted output functions with makepkg's
authored
384 error "preset config file not found: \`$basedir$cfg'"
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
385 return $ENOENT
386 fi
387
d2f545c @falconindy add verbose mode, listing additions to the image
authored
388 # explicitly retain some options
389 (( verbose )) && opts+=' -v'
390 (( !color )) && opts+=' -C'
391
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
392 # we've got enough to relaunch geninit now
8584669 @falconindy replace old and busted output functions with makepkg's
authored
393 msg "Building image from preset: $1-$preset"
394 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
395 "$0" -b "$basedir" -k "$ALL_kver" -c "$cfg" -g "$basedir$imagename" $opts || return 1
396 echo
397 done
398 } # }}}
399
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored
400 __kmodinfo() { # {{{
f6c3da3 @falconindy geninit: always set a basedir for modinfo
authored
401 modinfo -b "$basedir" -0k $kernver "$@"
d1be2d7 @falconindy rename kmodinfo as __kmodinfo
authored
402 } # }}}
403
404 __ldd() { # {{{
405 LD_TRACE_LOADED_OBJECTS=1 "$ld_so" "$@"
406 } # }}}
407
75052c2 @falconindy initial commit
authored
408 # its a trap!
67de740 @falconindy allow cleanup to take a message as a second param
authored
409 trap 'cleanup 130 "Aborted by user! Exiting..."' SIGINT
410 trap 'cleanup 143 "TERM signal caught. Exiting..."' SIGTERM
75052c2 @falconindy initial commit
authored
411
04d836e @falconindy relocate build_preset. rename it to __build_preset and declare a "pri…
authored
412 # source public API
5aad559 @falconindy cleanup commenting in main file, do a little reorg
authored
413 . "$_sharedir/geninit.api"
75052c2 @falconindy initial commit
authored
414
f2136d7 @falconindy add quirks file for quirky modules
authored
415 # source module quirks file
416 . "$_sharedir/geninit.quirks"
417
75052c2 @falconindy initial commit
authored
418 # ------------------ #
419 ## option parsing ##
420 # ------------------ #
421
d2f545c @falconindy add verbose mode, listing additions to the image
authored
422 while getopts ':b:Cc:g:H:hk:LMp:S:st:vz:' flag; do
75052c2 @falconindy initial commit
authored
423 case $flag in
424 b) basedir=$OPTARG ;;
425 c) config=$OPTARG ;;
8584669 @falconindy replace old and busted output functions with makepkg's
authored
426 C) color=0 ;;
75052c2 @falconindy initial commit
authored
427 g) imagename=$OPTARG ;;
38f816a @falconindy add -H and -L options for getting help with builders
authored
428 H) builderhelp $OPTARG ;;
25a82f8 @falconindy add a usage function
authored
429 h) usage ;;
192f56a @falconindy allow specifying kver by providing a path to the kernel
authored
430 k) kern=$OPTARG ;;
38f816a @falconindy add -H and -L options for getting help with builders
authored
431 L) builderlist ;;
a7370a0 @falconindy add -M option for dumping autodetected modules
authored
432 M) automods=1 ;;
1f2aff1 @falconindy add support for kernel presets
authored
433 p) preset=$OPTARG ;;
434 S) skipbuilders=(${OPTARG//,/ }) ;;
61ed3b4 @falconindy use find|cpio instead of gen_init_cpio
authored
435 s) savebuildroot=1 ;;
54d2b38 @falconindy The -t option should set a base tmpdir, not the workdir itself
authored
436 t) otmpdir=$OPTARG ;;
d2f545c @falconindy add verbose mode, listing additions to the image
authored
437 v) verbose=1 ;;
75052c2 @falconindy initial commit
authored
438 z) ocompress=$OPTARG ;; # named differently to allow overriding
439 :) die "option requires an argument -- '$OPTARG'" ;;
440 \?) die "invalid option -- '$OPTARG'" ;;
441 esac
442 done
443
ddb5e48 @falconindy use declare for all globals
authored
444 # Alter PATH. We want to make sure that /bin and /sbin are favored, since
445 # we specifically rely on GNU coreutils
446 PATH=/sbin:/bin:$PATH
447
8584669 @falconindy replace old and busted output functions with makepkg's
authored
448 if [[ -t 2 ]] && (( color )); then
449 # prefer terminal safe colored and bold text when tput is supported
450 if tput setaf 0 &>/dev/null; then
451 NC="$(tput sgr0)"
452 BOLD="$(tput bold)"
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
453 BLUE="$BOLD$(tput setaf 4)"
454 GREEN="$BOLD$(tput setaf 2)"
455 RED="$BOLD$(tput setaf 1)"
456 YELLOW="$BOLD$(tput setaf 3)"
8584669 @falconindy replace old and busted output functions with makepkg's
authored
457 else
458 NC="\e[1;0m"
459 BOLD="\e[1;1m"
1fd11ce @falconindy remove NOOP gettext calls and superfluous braces
authored
460 BLUE="$BOLD\e[1;34m"
461 GREEN="$BOLD\e[1;32m"
462 RED="$BOLD\e[1;31m"
463 YELLOW="$BOLD\e[1;33m"
8584669 @falconindy replace old and busted output functions with makepkg's
authored
464 fi
465 fi
466 readonly NC BOLD BLUE GREEN RED YELLOW
467
58c9f9f @falconindy go on a quoting spree.
authored
468 if [[ "$preset" ]]; then
ee64612 @falconindy catch $preset earlier, to avoid needless sanity checks
authored
469 __build_preset "$preset"
470 exit $?
471 fi
472
75052c2 @falconindy initial commit
authored
473 # ----------------- #
474 ## sanity checks ##
475 # ----------------- #
476
c8b5677 @falconindy check basedir first. make sure the config check is basedir aware
authored
477 # if specified, does the basedir exist? trim any trailing slash
58c9f9f @falconindy go on a quoting spree.
authored
478 if [[ "$basedir" ]]; then
c8b5677 @falconindy check basedir first. make sure the config check is basedir aware
authored
479 basedir=${basedir%/}
480
481 # if $basedir still exists (some noodle noggin might use -b /), make sure the
482 # dir can be resolved
58c9f9f @falconindy go on a quoting spree.
authored
483 [[ "$basedir" && ! -d $basedir ]] && die "basedir \`$basedir' not found"
c8b5677 @falconindy check basedir first. make sure the config check is basedir aware
authored
484 fi
485
486 # make sure our config exists, and source it
487 config=${config:-$baseconfig}
58c9f9f @falconindy go on a quoting spree.
authored
488 [[ ! -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
489
9c03a6d @falconindy fix linker resolution for multilib platforms
authored
490 # declared as an array to force expansion. we have to be careful that the wrong
491 # linker isn't picked for mulitlib systems, so we branch on $carch.
492 case $carch in
493 i686) ld_so=("$basedir"/lib/ld-linux.so.?*) ;;
494 x86_64) ld_so=("$basedir"/lib/ld-linux-${carch//_/-}.so.?*) ;;
495 *) die "unknown architecture: $carch" ;;
496 esac
497
75052c2 @falconindy initial commit
authored
498 if (( ${#ld_so[*]} != 1 )); then # uh oh...
499 die "failed to resolve the location of /lib/ld.so. Please report this bug."
500 fi
501
54d2b38 @falconindy The -t option should set a base tmpdir, not the workdir itself
authored
502 # mktemp takes care of our error handling here
0daef77 @falconindy oops. use the proper default value expansion
authored
503 tmpdir=$(readlink -e "$(mktemp -d "${otmpdir:-/tmp}"/${0##*/}.XXXXXX)") || cleanup 1
6dda899 @falconindy allow changing location of build dir
authored
504
9e86cb0 @falconindy slight refactor on kern => kernver logic
authored
505 # does the kernel exist inside the basedir? we may need to resolve it...
58c9f9f @falconindy go on a quoting spree.
authored
506 [[ "$kern" ]] && kernver=$(__get_kernver "$kern")
507 [[ "$kernver" ]] || die "Invalid kernel specifier: $kern"
75052c2 @falconindy initial commit
authored
508
509 # is our supplied compression method (if supplied) valid?
510 compress=${ocompress:-$compress}
73b26ca @falconindy always use == for equality
authored
511 if [[ -z $compress || $compress == none ]]; then
6048c99 @falconindy allow 'none' as a compression option
authored
512 compress=cat # NOOP compressor
513 else
1a5e61c @falconindy Add support for LZO compression via lzop
authored
514 [[ "$compress" == @(gzip|bzip2|lzop|lzma|xz) ]] || die "unknown compression method: $compress"
75052c2 @falconindy initial commit
authored
515 type -P "$compress" >/dev/null || die "failed to find \`$compress' binary in PATH"
516 fi
517
340a7c4 @falconindy add support for dry runs
authored
518 # if $imagename is provided, its path needs to be valid
58c9f9f @falconindy go on a quoting spree.
authored
519 if [[ "$imagename" ]]; then
aeea626 @falconindy fail when imagepath's dirname isn't writeable
authored
520 imagepath=$(readlink -f "$imagename")
58c9f9f @falconindy go on a quoting spree.
authored
521 [[ "$imagepath" ]] || die "invalid path to imagename: $imagename"
aeea626 @falconindy fail when imagepath's dirname isn't writeable
authored
522 [[ -w ${imagepath%/*} ]] || die "no permission to write to specified path: \`${imagepath%/*}'"
340a7c4 @falconindy add support for dry runs
authored
523 else # no $imagename, so we're doing a dry run
524 dryrun=1
525 fi
75052c2 @falconindy initial commit
authored
526
527 # ------------ #
528 ## int main ##
529 # ------------ #
530
6dda899 @falconindy allow changing location of build dir
authored
531 # define a few more paths for convenience
cee912e @falconindy mark static variables as readonly
authored
532 declare -r buildroot=$tmpdir/root
533 declare -r autodetect_cache=$tmpdir/autodetect.cache
534 declare -r moduledir=/lib/modules/$kernver
75052c2 @falconindy initial commit
authored
535
a7370a0 @falconindy add -M option for dumping autodetected modules
authored
536 if (( automods )); then
537 . "$_sharedir/builders/autodetect" || die "unable to source autodetect builder"
538 build
8584669 @falconindy replace old and busted output functions with makepkg's
authored
539 msg "Autodetected modules"
58c9f9f @falconindy go on a quoting spree.
authored
540 [[ -s "$autodetect_cache" ]] && cat "$autodetect_cache"
a7370a0 @falconindy add -M option for dumping autodetected modules
authored
541 cleanup 0
542 fi
543
ee64612 @falconindy catch $preset earlier, to avoid needless sanity checks
authored
544 __build_image
75052c2 @falconindy initial commit
authored
545
2fa6276 @falconindy move main loop logic into its own function. localize variables
authored
546 cleanup $?
75052c2 @falconindy initial commit
authored
547
49078e2 @falconindy general builder code standardized/cleanup
authored
548 # vim: set et sw=2 ft=sh:
Something went wrong with that request. Please try again.