-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkiso
executable file
·383 lines (323 loc) · 9.9 KB
/
mkiso
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
#!/usr/bin/env bash
# Embeds a preseed file into a Debian ISO image. (amd64, arm64)
# Optionally encrypts the filesystem
# Optionally encrypts /boot
# Commands from:
# https://wiki.debian.org/DebianInstaller/Modify/CD
# https://wiki.debian.org/RepackBootableISO
# https://wiki.debian.org/ManipulatingISOs
# https://wiki.debian.org/DebianInstaller/Preseed/EditIso
# https://help.ubuntu.com/community/Grub2/Submenus
# You may need to modify the "version" variable if you're using a different iso
# format than debian-11.0.0-arm64-netinst.iso
# Print commands and their arguments as they are executed
# set -x
iso_files="isofiles"
orig_iso="${1}"
output_dir="output"
preseed_files="files/preseed_files"
settings_file="${2-options.env}"
settings=$(sed -e 's/\r//' -e '/^#/d;/^\s*$/d;/^\w*$/d' "${settings_file}")
shopt -s extglob
arch="${1##"debian-"}"
arch="${arch##+([0-9.])-}"
arch="${arch%%-netinst.iso}"
release="${1##firmware-}"
release="${release##debian-}"
release="${release%%.*}"
version="${1##firmware-}"
version="${version##debian-}"
version="${version%%"-${arch}-netinst.iso"}"
shopt -u extglob
volume_id="Debian ${version} ${arch} n"
# Set variables based on release version
case "${release}" in
'9') codename="stretch";;
'10') codename="buster";;
'11') codename="bullseye";;
'12') codename="bookworm";;
'13') codename="trixie";;
'14') codename="forky";;
esac
# Colored output
code_grn() { tput setaf 2; printf '%s\n' "${1}"; tput sgr0; }
code_red() { tput setaf 1; printf '%s\n' "${1}"; tput sgr0; }
code_yel() { tput setaf 3; printf '%s\n' "${1}"; tput sgr0; }
# Error checks
if ! command -v "xorriso" >/dev/null 2>&1; then
code_red "[ERROR] xorriso not installed!"
echo
exit 1
fi
if [ -z "${1}" ]; then
code_red "[ERROR] Usage:"
printf '%s\n' " ./$(basename "${0}") debian-xx.x.x-xxx64-netinst.iso"
echo
exit 1
fi
if [ ! -f "${settings_file}" ]; then
code_red "[INFO] missing ${settings_file} file"
echo
exit 1
fi
if [ -d "${iso_files}" ]; then
code_red "[ERROR] Directory ${iso_files} exists; Please correct and re-run"
echo
exit 1
fi
[ ! -d "${output_dir}" ] && mkdir "${output_dir}"
# Export environment variables
load_env() {
while IFS='=' read -r key value; do
value="${value//\"/}"
value="${value//\'/}"
# shellcheck disable=SC2163
printf -v "${key}" '%s' "${value}" && export "${key}"
done <<< "${settings}"
}
# Unset environment variables
unload_env() {
while IFS='=' read -r key _; do
unset "${key}"
done <<< "${settings}"
}
# Prompt for amd64 options
preseed_amd64() {
printf '%s\n' "Select which type of ISO you would like to generate: "
PS3='Please choose: '
options=("kvm" "kvm: encrypted" "workstation: encrypted"
"workstation: encrypted boot")
select opt in "${options[@]}"; do
case $REPLY in
1)
printf '%s\n\n' " Configuring preseed iso for $opt"
new_iso="Debian-${version}-PRESEED-kvm.iso"
preseed_file="${preseed_files}/kvm-${codename}.cfg"
break
;;
2)
printf '%s\n\n' " Configuring preseed iso for $opt"
new_iso="Debian-${version}-PRESEED-kvm_encrypted.iso"
preseed_file="${preseed_files}/kvm-${codename}-encrypted.cfg"
break
;;
3)
printf '%s\n\n' " Configuring preseed iso for $opt"
new_iso="Debian-${version}-PRESEED-ws-full_encrypted.iso"
preseed_file="${preseed_files}/workstation-${codename}-encrypted.cfg"
break
;;
4)
printf '%s\n\n' " Configuring preseed iso for $opt"
new_iso="Debian-${version}-PRESEED-ws-full_encrypted.iso"
preseed_file="${preseed_files}/workstation-${codename}-encrypted_boot.cfg"
break
;;
*) code_red "[ERROR] Invalid option: $REPLY" ;;
esac
done
}
# Prompt for arm64 options
preseed_arm64() {
printf '%s\n' "Select which type of ISO you would like to generate: "
PS3='Please choose: '
options=("utm" "utm: encrypted")
select opt in "${options[@]}"; do
case $REPLY in
1)
printf '%s\n\n' " Configuring preseed iso for $opt"
new_iso="Debian-${version}-PRESEED-utm.iso"
preseed_file="${preseed_files}/utm-${codename}.cfg"
break
;;
2)
printf '%s\n\n' " Configuring preseed iso for $opt"
new_iso="Debian-${version}-PRESEED-utm_encrypted.iso"
preseed_file="${preseed_files}/utm-${codename}-encrypted.cfg"
break
;;
*) code_red "[ERROR] Invalid option: $REPLY" ;;
esac
done
}
# Validate the preseed file
check_debconf_selections() {
local updated_seed_file="${1}"
debconf-set-selections --checkonly "${updated_seed_file}" || {
code_red "[ERROR] debconf-set-selections failed. Error in preseed file?"
exit 1
}
}
# Error if $new_iso exists
iso_exists() {
if [ -f "${new_iso}" ]; then
code_red "[ERROR] ISO ${new_iso} exists; Please correct and re-run"
exit 1
fi
}
# amd64 preparation
extract_mbr_amd64() {
# Extract mbr from ISO
dd if="${orig_iso}" bs=1 count=446 of="${mbr_template}"
}
# arm64 preparation
extract_efi_arm64() {
code_yel "[INFO] sudo access needed for fdisk operation on ${orig_iso}"
start_block=$(sudo fdisk -l "${orig_iso}" | grep -F "${orig_iso}"2 | awk '{print $2}')
block_count=$(sudo fdisk -l "${orig_iso}" | grep -F "${orig_iso}"2 | awk '{print $4}')
# Extract EFI partition image
if test "${start_block}" -gt 0 -a "${block_count}" -gt 0 2> /dev/null; then
dd if="${orig_iso}" bs=512 skip="${start_block}" count="${block_count}" of="${efi_template}"
else
code_red "[ERROR] Cannot read start block and block count from fdisk" >&2
fi
}
# Unpack the ISO image
unpack_iso() {
mkdir -p "${iso_files}"
xorriso -osirrox on -indev "${orig_iso}" -extract / "${iso_files}"
}
# Copy preseed files to the extracted ISO
put_preseed() {
load_env
envsubst < "${preseed_file}" > "${iso_files}/preseed.cfg"
check_debconf_selections "${iso_files}/preseed.cfg"
unload_env
chmod 0644 "${iso_files}/preseed.cfg"
cp -a "files/preseed_scripts" "${iso_files}"
}
# Configure the amd64 install menu
auto_select_amd64() {
# Enable write permissions on menu files
chmod u+w "${iso_files}/isolinux" "${iso_files}/isolinux/isolinux.cfg"
# Configure default selection
sed -i 's/^default .*/default auto/g' "${iso_files}/isolinux/isolinux.cfg"
# Disable write permissions on menu files
chmod u-w "${iso_files}/isolinux" "${iso_files}/isolinux/isolinux.cfg"
# Set permissions for grub menu
chmod -R u+w "${iso_files}/boot/grub"
# Autoselect uefi menu
sed -i "/^menuentry --hotkey=g/i set default=\'Advanced options ...>... Automated install\'\nset timeout=5" isofiles/boot/grub/grub.cfg
# Disable write permissions on grub menu
chmod -R u-w "${iso_files}/boot/grub"
}
# Configure the arm64 install menu
auto_select_arm64() {
# Set permissions for grub menu
chmod -R u+w "${iso_files}/boot/grub"
# Autoselect uefi menu
sed -i "/^insmod gzio/a set default=\'Advanced options ...>... Automated install\'\nset timeout=5" isofiles/boot/grub/grub.cfg
# Disable write permissions on grub dir
chmod -R u-w "${iso_files}/boot/grub"
}
# Update the initrd
mod_initrd() {
# Enter the isofiles directory
cd "${iso_files}" || exit
# Enable write permissions on initrd directory
chmod -R u+w "${initrd_dir}/"
# Unzip initrd
gunzip "${initrd_dir}/initrd.gz"
# Add the preseed to the initrd
echo preseed.cfg | cpio -H newc -o -A -F "${initrd_dir}/initrd"
# Re-Zip initrd
gzip "${initrd_dir}/initrd"
# Enable write permissions on md5sum.txt
chmod u+w md5sum.txt
# Generate new md5sum.txt
find . ! -name "md5sum.txt" ! -path "./isolinux/*" -follow -type f -print0 |
xargs -0 md5sum > md5sum.txt
# Disable write permissions on md5sum.txt before writing iso
chmod u-w md5sum.txt
# Disable write permissions on initrd directory before writing iso
chmod -R u-w "${initrd_dir}/"
# Move back a directory
cd .. || exit
}
# Re-pack amd64 ISO image
pack_amd64() {
xorriso -as mkisofs \
-r -V "${volume_id}" \
-o "${output_dir}/${new_iso}" \
-J -joliet-long -cache-inodes \
-isohybrid-mbr "${mbr_template}" \
-b isolinux/isolinux.bin \
-c isolinux/boot.cat \
-boot-load-size 4 -boot-info-table -no-emul-boot \
-eltorito-alt-boot \
-e boot/grub/efi.img -no-emul-boot \
-isohybrid-gpt-basdat -isohybrid-apm-hfsplus \
"${iso_files}" 2> /dev/null
}
# Re-pack arm64 ISO image
pack_arm64() {
xorriso -as mkisofs \
-r -V "${volume_id}" \
-o "${output_dir}/${new_iso}" \
-J -joliet-long -cache-inodes \
-e boot/grub/efi.img \
-no-emul-boot \
-append_partition 2 0xef "${efi_template}" \
-partition_cyl_align all \
"${iso_files}" 2> /dev/null
}
# Generate SHA512SUM file
gen_sha() {
cd "${output_dir}" || exit
shasum=$(sha512sum "${new_iso}")
printf '%s\n' "${shasum}" >> "SHA512SUMS"
cd .. || exit
}
# Clean up temporary files
clean_up() {
# Need write permissions so we can delete
chmod -R u+w "${iso_files}"
[ -f "${efi_template}" ] && rm -f "${efi_template}"
[ -f "${mbr_template}" ] && rm -f "${mbr_template}"
rm -rf "${iso_files}"
}
# Do the things..
case "${arch}" in
"amd64")
mbr_template="/tmp/isohdpfx.bin"
initrd_dir="install.amd"
preseed_amd64
iso_exists
extract_mbr_amd64
unpack_iso
put_preseed
auto_select_amd64
mod_initrd
pack_amd64
gen_sha
;;
"arm64")
efi_template="/tmp/efi.img"
initrd_dir="install.a64"
preseed_arm64
iso_exists
extract_efi_arm64
unpack_iso
put_preseed
auto_select_arm64
mod_initrd
pack_arm64
gen_sha
;;
esac
# Newline for cleaner output formatting
echo
# Clean up
read -rp "Would you like to remove ${iso_files} directory? [Yy|Nn] " response
case "${response}" in
"Y" | "y")
printf '%s\n' "[INFO] Cleaning up!"
clean_up
;;
*)
printf '%s\n' "[INFO] Exiting without cleaning up"
exit 0
;;
esac
code_grn "Done, enjoy!"
# vim: ft=sh ts=2 sts=2 sw=2 sr et