-
Notifications
You must be signed in to change notification settings - Fork 2
/
kickseed.sh
320 lines (298 loc) · 7.32 KB
/
kickseed.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#! /bin/sh
# Caller should set -e.
if [ -z "$HANDLERS" ]; then
HANDLERS=/lib/kickseed/handlers
fi
if [ -z "$SPOOL" ]; then
SPOOL=/var/spool/kickseed
fi
if [ -z "$POSTSPOOL" ]; then
POSTSPOOL="$SPOOL/parse/post"
fi
if [ -z "$SUPPORTEDLOCALES" ]; then
SUPPORTEDLOCALES=/etc/SUPPORTED-short
fi
warn () {
ks_log "$@"
}
die () {
warn "$@"
exit 1
}
warn_getopt () {
warn "Failed to parse $1 options"
}
warn_bad_opt () {
warn "Unimplemented $1 option $2"
}
warn_bad_arg () {
warn "Unimplemented $1 $2 argument $3"
}
# Allow handler files to register functions to be called at the end of
# processing; this lets them build up preseed answers over several handler
# calls.
final_handlers=
register_final () {
final_handlers="$final_handlers $1"
}
# Save %post script for later execution.
save_post_script () {
if [ -e "$SPOOL/parse/post.section" ]; then
mkdir -p "$POSTSPOOL"
i=0
while [ -e "$POSTSPOOL/$i.script" ]; do
i="$(($i + 1))"
done
mv "$SPOOL/parse/post.section" "$POSTSPOOL/$i.script"
chmod +x "$POSTSPOOL/$i.script"
if [ "$post_chroot" = 1 ]; then
touch "$POSTSPOOL/$i.chroot"
if [ "$post_interpreter" ]; then
echo "$post_interpreter" \
> "$POSTSPOOL/$i.interpreter"
fi
else
rm -f "$POSTSPOOL/$i.chroot"
fi
fi
}
# Load all handlers.
for handler in "$HANDLERS"/*; do
. "$handler"
done
# Expects kickstart file as $1.
kickseed () {
rm -rf "$SPOOL/parse"
mkdir -p "$SPOOL/parse"
# Parse and execute %pre sections first.
SECTION=main
while read -r line; do
line="${line%%}"
keyword="${line%%[ ]*}"
case $keyword in
%pre)
SECTION=pre
pre_handler_section "${line#*[ ]}"
> "$SPOOL/parse/pre.section"
continue
;;
%packages|%post)
SECTION="${keyword#%}"
continue
;;
esac
if [ "$SECTION" = pre ]; then
echo "$line" >> "$SPOOL/parse/pre.section"
fi
done < "$1"
if [ -e "$SPOOL/parse/pre.section" ]; then
chmod +x "$SPOOL/parse/pre.section"
CODE=0
ks_run_script pre /bin/sh 0 "$SPOOL/parse/pre.section" || CODE="$?"
if [ "$CODE" != 0 ]; then
warn "%pre script exited with error code $CODE"
fi
rm -f "$SPOOL/parse/pre.section"
fi
# Parse all other sections.
SECTION=main
(while read -r line; do
line="${line%%}"
keyword="${line%%[ ]*}"
# Deal with %include directives.
if [ "$keyword" = '%include' ]; then
rest="${line#*[ ]}"
arg="${rest%%[ ]*}"
cat "$arg"
elif [ "$keyword" = '%final' ]; then
die "%final reserved for internal use"
else
echo "$line"
fi
done < "$1"; echo %final) | while read -r line; do
# Work out the section.
keyword="${line%%[ ]*}"
if [ "$keyword" = '%packages' ]; then
save_post_script
SECTION=packages
continue
elif [ "$keyword" = '%pre' ]; then
save_post_script
SECTION=pre
continue
elif [ "$keyword" = '%post' ]; then
save_post_script
args="${line#*[ ]}"
if [ "$args" = "$line" ]; then
# No arguments.
args=
fi
eval post_handler_section "$args"
SECTION=post
> "$SPOOL/parse/post.section"
continue
elif [ "$keyword" = '%final' ]; then
save_post_script
for handler in $final_handlers; do
$handler
done
break
fi
if [ "$SECTION" = main ]; then
if [ -z "$keyword" ] || [ "${keyword#\#}" != "$keyword" ]; then
# Ignore empty lines and comments.
continue
fi
# Delegate to directive handlers.
if type "${keyword}_handler" >/dev/null 2>&1; then
args="${line#*[ ]}"
if [ "$args" = "$line" ]; then
# No arguments.
args=
fi
# This gets ...='\$foo' wrong, but it's
# better than the alternative (broken
# crypted passwords) for now.
args="$(printf %s "$args" | sed 's/\$/\\$/g')"
eval "${keyword}_handler" "$args"
else
warn "Unrecognised kickstart command: $keyword"
fi
elif [ "$SECTION" = packages ]; then
if [ -z "$keyword" ] || [ "${keyword#\#}" != "$keyword" ]; then
# Ignore empty lines and comments.
continue
fi
group=
if [ "$keyword" = '@' ]; then
group="${line#*[ ]}"
elif [ "${keyword#@}" != "$keyword" ]; then
group="${line#@}"
fi
if [ "$group" ]; then
# TODO: temporary hack to make at least the
# standard desktop work
case $group in
*\ Standard)
echo 'task:standard' >> "$SPOOL/parse/$SECTION.section"
;;
Ubuntu\ Desktop)
echo 'task:ubuntu-desktop' >> "$SPOOL/parse/$SECTION.section"
;;
Kubuntu\ Desktop)
echo 'task:kubuntu-desktop' >> "$SPOOL/parse/$SECTION.section"
;;
*\ *)
warn "Package group '$group' not implemented"
;;
*)
# Anything without a space
# is assumed to be the name
# of a task; useful for
# customisers.
echo "task:$group" >> "$SPOOL/parse/$SECTION.section"
;;
esac
else
echo "pkg:$line" >> "$SPOOL/parse/$SECTION.section"
fi
elif [ "$SECTION" = pre ]; then
# already handled
continue
else
echo "$line" >> "$SPOOL/parse/$SECTION.section"
fi
done || exit $?
if [ -s "$SPOOL/parse/packages.section" ]; then
# Handle %packages.
# TODO: doesn't allow removal of packages from ubuntu-base
packages="$(cat "$SPOOL/parse/packages.section")"
positives=.
negatives=.
for pkg in $packages; do
case $pkg in
task:-*|pkg:-*)
negatives="$negatives $pkg"
;;
*)
positives="$positives $pkg"
;;
esac
done
# pattern gets: (~nPOS|~nPOS|~nPOS)!~nNEG!~nNEG!~nNEG
joinpositives=
for pkg in $positives; do
case $pkg in
.) continue ;;
task:*)
element="~t^${pkg#task:}\$"
tasklist="${tasklist:+$tasklist, }${pkg#task:}"
;;
pkg:*)
element="~n^${pkg#pkg:}\$"
packagelist="${packagelist:+$packagelist }${pkg#pkg:}"
;;
esac
joinpositives="${joinpositives:+$joinpositives|}$element"
done
pattern="($joinpositives)"
hasnegatives=false
for pkg in $negatives; do
case $pkg in
.) continue ;;
task:-*)
element="~t^${pkg#task:-}\$"
hasnegatives=:
;;
pkg:-*)
element="~n^${pkg#pkg:-}\$"
hasnegatives=:
;;
esac
pattern="$pattern!$element"
done
# requires pkgsel 0.04ubuntu1; obsolete as of pkgsel
# 0.07ubuntu1
ks_preseed d-i pkgsel/install-pattern string "$pattern"
# requires pkgsel 0.07ubuntu1/0.08
ks_preseed tasksel tasksel/first multiselect "$tasklist"
ks_preseed d-i pkgsel/include string "$packagelist"
if $hasnegatives; then
warn "exclusions in %packages not supported; remove them manually in %post instead"
fi
fi
# Kickstart installations always run at critical priority.
ks_preseed d-i debconf/priority 'select' critical
}
kickseed_post () {
# Post-installation parts of handlers.
for dir in "$POSTSPOOL"/*.handler; do
[ -d "$dir" ] || continue
name="${dir##*/}"
if type "${name%.handler}_post" >/dev/null 2>&1; then
ks_run_handler "${name%.handler}_post"
else
warn "Missing post-installation handler: $name"
fi
done
# User-supplied post-installation scripts.
# TODO: sort numerically
for script in "$POSTSPOOL"/*.script; do
[ -f "$script" ] || continue
CHROOTED=0
if [ -e "${script%.script}.chroot" ]; then
CHROOTED=1
fi
INTERPRETER=/bin/sh
if [ -e "${script%.script}.interpreter" ]; then
INTERPRETER="$(cat "${script%.script}.interpreter")"
fi
CODE=0
ks_run_script post "$INTERPRETER" "$CHROOTED" "$script" || CODE="$?"
if [ "$CODE" != 0 ]; then
warn "%post script exited with error code $CODE"
fi
done
}