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