-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_preseeds_to_iso.sh
executable file
·430 lines (357 loc) · 10.1 KB
/
add_preseeds_to_iso.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/bin/bash
# We follow these methods:
# https://wiki.debian.org/DebianInstaller/Preseed/EditIso
# https://wiki.debian.org/DebianInstaller/Modify/CD
# ------------------------------------------------
check_prerequisites() {
if ! which 7z &> /dev/null; then
echo "We need to have 7z installed (from package \"7z\")." >&2
exit 1
fi
if ! which xorriso &> /dev/null; then
echo "We need to have xorriso installed (from package \"xorriso\")." >&2
exit 1
fi
if ! which isoinfo &> /dev/null; then
echo "We need to have isoinfo installed (from package \"genisoimage\")." >&2
exit 1
fi
}
search_isolinux() {
# Search for an isolinux isohdpfx.bin image
# in the default locations and the content of
# ISOLINUX_HINT.
# If none is found at the usual locations
# return 1
for path in "$ISOLINUX_HINT" /usr/lib/ISOLINUX; do
if [ -f "$path/isohdpfx.bin" ]; then
ISOLINUX_BIN="$path/isohdpfx.bin"
return 0
fi
done
return 1
}
ask_for_locale() {
# Ask user for the locale and keyboard layout he wishes to use
#
# Stores values in $LOCALE and $LAYOUT
local QUERY
if ! QUERY=$(setxkbmap -query); then
LAYOUT="en"
else
LAYOUT="$(echo "$QUERY" | awk '
/^layout:/ { printf $2 }
# As far as I know the installer does not recognise this
# /^variant:/ { printf "(" $2 ")" }
END { print "" }
')"
fi
read -p "Enter locale to use: " -e -i "$LANG" LOCALE
read -p "Enter keyboard layout to use: " -e -i "$LAYOUT" LAYOUT
}
ask_for_authorised_keys() {
# Ask user for the authorised keys to include as login keys for
# the root user.
#
# Stores the concatinated content in AUTHORISED_KEYS
echo " You may store a number of ssh keys on the installation medium, "
echo " which may be used as the inital authorized_keys for root. "
echo " Enter them as a colon(:) separated list."
local LINE
for key in $HOME/.ssh/id_*.pub $HOME/.ssh/authorized_keys; do
[ ! -f "$key" ] && continue
if [ -z "$LINE" ]; then
LINE="$key"
else
LINE="$LINE:$key"
fi
done
read -p "Enter ssh key files: " -e -i "$LINE" LINE
AUTHORISED_KEYS=$(
IFS=":"
for key in $LINE; do
if [ -r "$key" ]; then
cat "$key"
else
echo "Key file not readable: $key" >&2
return 1
fi
done
)
}
find_firmware_file() {
FIRMWARE="firmware.tar.gz"
if [ ! -f "$FIRMWARE" ]; then
echo " No non-free firmware found. "
echo " Check 'http://github.com/mfherbst/debian-preansible' for details."
FIRMWARE=""
return 0
fi
echo " Found debian non-free firmware file '$FIRMWARE'"
read -p "Do you wish to add this file to the iso? (Y/n)" RES
if [ "$RES" == "n" -o "$RES" == "N" ]; then
FIRMWARE=""
fi
}
extract_info() {
# $1: isofile
# fills the variables
# ORIG_VOLUMEID original volume id of the iso
local INFO=$(isoinfo -d -i "$1")
ORIG_VOLUMEID=$(echo "$INFO" | awk -F ": " '/^Volume id:/ { print $2 }')
}
copy_iso() {
#$1: isofile
# echos the dir where the extracted copy can be found
# returns 1 if problems
local ISO="$1"
local EXTRACTDIR=$(mktemp -d)
if ! 7z -o"$EXTRACTDIR" x "$ISO" >/dev/null; then
echo "Error extracting iso file" >&2
return 1
fi
# delete the [BOOT] folder
rm -rf "$EXTRACTDIR/[BOOT]"
echo "$EXTRACTDIR"
return 0
}
add_preseed() {
#$1 dir where extracted iso files are located.
#
# dumps the contents of the variable AUTHORISED_KEYS
# for installation as the initial authorized keys for
# root.
local EXTRACTDIR="$1"
if [ ! -d "$EXTRACTDIR" ] ; then
echo "Loopdir does not exist: $EXTRACTDIR" >&2
return 1
fi
# dir containing this file:
local SOURCEPREFIX="$(dirname "${BASH_SOURCE[0]}" )"
# dirname of the preseeds top level folder.
local PRESEEDSDIR="preseeds"
# check if we can find the preseeds:
if [ ! -d "$SOURCEPREFIX/$PRESEEDSDIR" ]; then
echo "Could not find preseed files at the expected location">&2
echo " (\"$SOURCEPREFIX/$PRESEEDSDIR\")" >&2
return 1
fi
# sanity check: is the "menu begin advanced" there
if ! < "$EXTRACTDIR/isolinux/menu.cfg" grep -q 'menu begin advanced'; then
echo "\"$EXTRACTDIR/isolinux/main.cfg\" has an unknown format." >&2
return 1
fi
# copy the preseedsdir over
if ! rsync -a -H "$SOURCEPREFIX/$PRESEEDSDIR" "$EXTRACTDIR/"; then
echo "Error copying the preseeds dir \"$SOURCEPREFIX/$PRESEEDSDIR\"" >&2
return 1
fi
# copy the authorized_keys for root
echo "$AUTHORISED_KEYS" > "$EXTRACTDIR/$PRESEEDSDIR/root_keys"
# drop the locale.cfg
{
echo "d-i debian-installer/locale string $LOCALE"
echo "d-i keyboard-configuration/xkb-keymap select $LAYOUT"
} > "$EXTRACTDIR/$PRESEEDSDIR/parts/locale.cfg"
# Determine linux kernel and initrd to use:
local BOOTVMLINUZ="/install.amd/vmlinuz"
local INITRD="/install.amd/initrd.gz"
if [ ! -f "$EXTRACTDIR/install.amd/vmlinuz" ]; then
echo "No amd64 linux kernel found. Falling back to i386" >&2
BOOTVMLINUZ="/install/vmlinuz"
INITRD="/install/initrd.gz"
fi
#
# Extra menu for isolinux boot
#
local MENUFILE="menu_extra.cfg" # menu files
if ! cp "$SOURCEPREFIX/$MENUFILE" "$EXTRACTDIR/isolinux/preseed.cfg"; then
echo "Error copying the preseed menu: \"$SOURCEPREFIX/$MENUFILE\"" >&2
return 1
fi
local SUBFILE="$EXTRACTDIR/isolinux/preseedsub.cfg"
for preseedfile in $SOURCEPREFIX/$PRESEEDSDIR/*.cfg; do
[ ! -f "$preseedfile" ] && continue
NAME=$(echo "$preseedfile" | sed "s/\.cfg$//; s#^$SOURCEPREFIX/$PRESEEDSDIR/##; s/[^a-zA-Z0-9]/_/g")
cat <<-EOF
label $NAME
menu label Preseed with $(basename "$preseedfile")
kernel $BOOTVMLINUZ
append auto=true file=/cdrom/$PRESEEDSDIR/$(basename "$preseedfile") preseed-md5=$(md5sum "$preseedfile" | cut -f 1 -d " ") priority=critical vga=788 initrd=$INITRD --- quiet
EOF
done > "$SUBFILE"
# finally enable the preseed stuff:
sed --in-place "/^menu begin advanced/iinclude preseed.cfg" "$EXTRACTDIR/isolinux/menu.cfg"
#
# Extra menu for grub (efi) boot
#
local GRUBCFG="$EXTRACTDIR/boot/grub/grub.cfg"
local GRUBCFG_EXTRA=$({
cat <<-EOF
submenu 'Preansible Debian automatic install ...' {
set menu_color_normal=cyan/blue
set menu_color_highlight=white/blue
set theme=/boot/grub/theme/1-1
EOF
for preseedfile in $SOURCEPREFIX/$PRESEEDSDIR/*.cfg; do
[ ! -f "$preseedfile" ] && continue
NAME=$(echo "$preseedfile" | sed "s/\.cfg$//; s#^$SOURCEPREFIX/$PRESEEDSDIR/##; s/[^a-zA-Z0-9]/_/g")
cat <<-EOF
menuentry 'Preseeded with $(basename "$preseedfile")' {
set background_color=black
linux $BOOTVMLINUZ auto=true file=/cdrom/$PRESEEDSDIR/$(basename "$preseedfile") preseed-md5=$(md5sum "$preseedfile" | cut -f 1 -d " ") priority=critical vga=788 --- quiet
initrd $INITRD
}
EOF
done
echo "}"
})
< "$GRUBCFG" awk -v "extra=$GRUBCFG_EXTRA" '
/^submenu (--hotkey=a )?.Advanced options/ { print extra }
{print}
' > "$GRUBCFG.new"
if diff "$GRUBCFG.new" "$GRUBCFG" &> /dev/null; then
echo "Error: No replacement occurred in $GRUBCFG, where one was exepected." >&2
exit 1
fi
mv "$GRUBCFG.new" "$GRUBCFG"
# and adjust the md5sums:
(
cd $EXTRACTDIR
md5sum `find ! -name "md5sum.txt" ! -path "./isolinux/*" -follow -type f` > md5sum.txt
)
return 0
}
add_firmware() {
local FIRMWARE="$1"
local EXTRACTDIR="$2"
tar -C "$EXTRACTDIR/firmware" -xzf "$FIRMWARE"
}
make_new_iso() {
# Reads the global variables
# ORIG_VOLUMEID the original volume id
# ISOLINUX_BIN the location of the isolinx binary to use
# or "" if no isolinux boot image should be
# available.
local LOOPDIR="$1"
local ISOIMAGE="$2"
local MBR=()
if [ "$ISOLINUX_BIN" ]; then
MBR=(-isohybrid-mbr "$ISOLINUX_BIN")
fi
OPTIONS=(
#
# Generic options
#
# Output file
-o "$ISOIMAGE"
#
# Volume ID
-V "Preseeded $ORIG_VOLUMEID" -A ""
#
# Use long joliet (more than 64char filenames)
# as well as Rock Ridge extension
-J -r -joliet-long
#
# Copy an ISOLINUX mbr template, which executes the boot image from BIOS
# Also announce it as a GPT partition for booting via EFI and as MBR partition
${MBR[@]}
#
# First boot option
#
# an legacy iso image
-b isolinux/isolinux.bin -c isolinux/boot.cat -boot-load-size 4 -boot-info-table -no-emul-boot
#
# have another:
-eltorito-alt-boot
#
# an efi iso image
-e boot/grub/efi.img -no-emul-boot -isohybrid-gpt-basdat
)
QUIET="-quiet"
if [ "$DEBUG" == "y" ]; then
QUIET=""
fi
xorriso -as mkisofs $QUIET "${OPTIONS[@]}" "$LOOPDIR"
}
usage() {
cat <<-EOF
$(basename "$0") [ -h | --help | --debug | -d ] <iso>
Add preseeds to a debian iso file.
--debug or -d causes debug files to be kept after the
script has run.
EOF
}
cleanup() {
# delete temporary directory
if [ "$DEBUG" == "y" ]; then
echo "MODIFYDIR is $MODIFYDIR"
else
rm -r "$MODIFYDIR"
fi
}
# ------------------------------------------------
check_prerequisites
DEBUG=n
while [ "$1" ]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-d|--debug)
DEBUG="y"
;;
*)
break
;;
esac
shift
done
ISOFILE="$1"
if [ ! -f "$ISOFILE" ]; then
echo "The first argument \"$ISOFILE\" is not a valid debian iso" >&2
exit 1
fi
NEWISO="$(basename "$ISOFILE" ".iso")-preseeded.iso"
if [ -f "$NEWISO" ]; then
echo "Output iso: $NEWISO already exists" >&2
exit 1
fi
if ! search_isolinux; then
echo "WARNING: Could not find ISOLINUX on the system" >&2
echo " Either install \"isolinux\" package or set ISOLINUX_HINT to" >&2
echo " the isolinux directory (e.g. /usr/lib/ISOLINUX)" >&2
echo "" >&2
echo " Proceeding without ISOLINUX MBR image. Perhaps iso is not bootable ...">&2
fi
ask_for_locale || exit 1
echo
ask_for_authorised_keys || exit 1
echo
find_firmware_file || exit 1
echo
echo Please wait ...
# extract some info from the iso file
extract_info "$ISOFILE"
if ! MODIFYDIR=$(copy_iso "$ISOFILE"); then
echo "Error copying the iso: Is it a valid iso file?" >&2
exit 1
fi
trap cleanup EXIT SIGTERM
if [ "$FIRMWARE" ]; then
if ! add_firmware "$FIRMWARE" "$MODIFYDIR"; then
echo "Adding $FIRMWARE failed" >&2
exit 1
fi
fi
if ! add_preseed "$MODIFYDIR"; then
echo "Error adding the preseed stuff." >&2
exit 1
fi
if ! make_new_iso "$MODIFYDIR" "$NEWISO"; then
echo "Creating new iso failed" >&2
exit 1
fi
echo "Preseeded iso can be found in \"$NEWISO\"."
exit 0